From 5184f867e4e6c684da8fe804b366d4fe55e46373 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michal=20Koutn=C3=BD?= <mkoutny@suse.com>
Date: Fri, 4 Oct 2024 20:40:51 +0200
Subject: [PATCH 0908/1160] test: Add test for per-device cgroup properties

Reported in #34126

(cherry picked from commit 321637743313f896e275fd038996b8cfb5a070b3)
(cherry picked from commit 84cd501ae4d2689ac39e510d1d33b7e3234f5199)
---
 test/units/testsuite-19.keyed-properties.sh | 65 +++++++++++++++++++++
 1 file changed, 65 insertions(+)
 create mode 100755 test/units/testsuite-19.keyed-properties.sh

diff --git a/test/units/testsuite-19.keyed-properties.sh b/test/units/testsuite-19.keyed-properties.sh
new file mode 100755
index 0000000000..cadefe26d5
--- /dev/null
+++ b/test/units/testsuite-19.keyed-properties.sh
@@ -0,0 +1,65 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: LGPL-2.1-or-later
+set -ex
+set -o pipefail
+
+# shellcheck source=test/units/test-control.sh
+. "$(dirname "$0")"/test-control.sh
+# shellcheck source=test/units/util.sh
+. "$(dirname "$0")"/util.sh
+
+if [[ "$(get_cgroup_hierarchy)" != unified ]]; then
+    echo "Skipping $0 as we're not running with the unified cgroup hierarchy"
+    exit 0
+fi
+
+testcase_iodevice_dbus () {
+    # Test that per-device properties are applied in configured order even for different devices (because
+    # they may resolve to same underlying device in the end
+    # Note: if device does not exist cgroup attribute write fails but systemd should still track the
+    # configured properties
+    systemd-run --unit=test0.service \
+                --property="IOAccounting=yes" \
+                sleep inf
+
+    systemctl set-property test0.service \
+              IOReadBandwidthMax="/dev/sda1 1M" \
+              IOReadBandwidthMax="/dev/sda2 2M" \
+              IOReadBandwidthMax="/dev/sda3 4M"
+
+    local output
+    output=$(mktemp)
+    trap 'rm -f "$output"' RETURN
+    systemctl show -P IOReadBandwidthMax test0.service >"$output"
+    diff -u "$output" - <<EOF
+/dev/sda1 1000000
+/dev/sda2 2000000
+/dev/sda3 4000000
+EOF
+
+    systemctl stop test0.service
+}
+
+testcase_iodevice_unitfile () {
+    cat >/run/systemd/system/test1.service <<EOF
+[Service]
+ExecStart=/usr/bin/sleep inf
+IOReadBandwidthMax=/dev/sda1 1M
+IOReadBandwidthMax=/dev/sda2 2M
+IOReadBandwidthMax=/dev/sda3 4M
+EOF
+    systemctl daemon-reload
+
+    local output
+    output=$(mktemp)
+    trap 'rm -f "$output"' RETURN
+    systemctl show -P IOReadBandwidthMax test1.service >"$output"
+    diff -u "$output" - <<EOF
+/dev/sda1 1000000
+/dev/sda2 2000000
+/dev/sda3 4000000
+EOF
+    rm -f /run/systemd/system/test1.service
+}
+
+run_testcases
-- 
2.33.0