]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
TEST-55-OOMD: split into small testcases
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 16 Oct 2024 06:04:07 +0000 (15:04 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 16 Oct 2024 06:17:11 +0000 (15:17 +0900)
Then, we can run each small test cases separately.

test/units/TEST-55-OOMD.sh

index 2b003c9f73d433333607e7d751d41cb933fb24aa..43a9919165a04ce8f3af66251011f11ee1f5c495 100755 (executable)
@@ -3,8 +3,10 @@
 set -eux
 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
+. "$(dirname "$0")"/util.sh
 
 systemd-analyze log-level debug
 
@@ -93,62 +95,52 @@ else
     systemd-run -t -p MemoryMax=10M -p MemorySwapMax=0 -p MemoryZSwapMax=0 /bin/true
 fi
 
-systemctl start TEST-55-OOMD-testchill.service
-systemctl start TEST-55-OOMD-testbloat.service
+test_basic() {
+    systemctl "$@" start TEST-55-OOMD-testchill.service
+    systemctl "$@" start TEST-55-OOMD-testbloat.service
 
-# Verify systemd-oomd is monitoring the expected units
-timeout 1m bash -xec 'until oomctl | grep "/TEST-55-OOMD-workload.slice"; do sleep 1; done'
-oomctl | grep "/TEST-55-OOMD-workload.slice"
-oomctl | grep "20.00%"
+    # Verify systemd-oomd is monitoring the expected units.
+    timeout 1m bash -xec 'until oomctl | grep "/TEST-55-OOMD-workload.slice"; do sleep 1; done'
+    oomctl | grep "/TEST-55-OOMD-workload.slice"
+    oomctl | grep "20.00%"
 
-systemctl status TEST-55-OOMD-testchill.service
+    systemctl "$@" status TEST-55-OOMD-testchill.service
 
-# systemd-oomd watches for elevated pressure for 2 seconds before acting.
-# It can take time to build up pressure so either wait 2 minutes or for the service to fail.
-for _ in {0..59}; do
-    if ! systemctl status TEST-55-OOMD-testbloat.service; then
-        break
-    fi
-    oomctl
-    sleep 2
-done
+    # systemd-oomd watches for elevated pressure for 2 seconds before acting.
+    # It can take time to build up pressure so either wait 2 minutes or for the service to fail.
+    for _ in {0..59}; do
+        if ! systemctl "$@" status TEST-55-OOMD-testbloat.service; then
+            break
+        fi
+        oomctl
+        sleep 2
+    done
 
-# testbloat should be killed and testchill should be fine
-if systemctl status TEST-55-OOMD-testbloat.service; then exit 42; fi
-if ! systemctl status TEST-55-OOMD-testchill.service; then exit 24; fi
+    # testbloat should be killed and testchill should be fine
+    if systemctl "$@" status TEST-55-OOMD-testbloat.service; then exit 42; fi
+    if ! systemctl "$@" status TEST-55-OOMD-testchill.service; then exit 24; fi
+}
 
-# Make sure we also work correctly on user units.
-loginctl enable-linger testuser
+testcase_basic_system() {
+    test_basic
+}
 
-systemctl start --machine "testuser@.host" --user TEST-55-OOMD-testchill.service
-systemctl start --machine "testuser@.host" --user TEST-55-OOMD-testbloat.service
+testcase_basic_user() {
+    # Make sure we also work correctly on user units.
+    loginctl enable-linger testuser
 
-# Verify systemd-oomd is monitoring the expected units
-# Try to avoid racing the oomctl output check by checking in a loop with a timeout
-timeout 1m bash -xec 'until oomctl | grep "/TEST-55-OOMD-workload.slice"; do sleep 1; done'
-oomctl | grep -E "/user.slice.*/TEST-55-OOMD-workload.slice"
-oomctl | grep "20.00%"
+    test_basic --machine "testuser@.host" --user
 
-systemctl --machine "testuser@.host" --user status TEST-55-OOMD-testchill.service
+    loginctl disable-linger testuser
+}
 
-# systemd-oomd watches for elevated pressure for 2 seconds before acting.
-# It can take time to build up pressure so either wait 2 minutes or for the service to fail.
-for _ in {0..59}; do
-    if ! systemctl --machine "testuser@.host" --user status TEST-55-OOMD-testbloat.service; then
-        break
+testcase_preference_avoid() {
+    # only run this portion of the test if we can set xattrs
+    if ! cgroupfs_supports_user_xattrs; then
+        echo "cgroup does not support user xattrs, skipping test for ManagedOOMPreference=avoid"
+        return 0
     fi
-    oomctl
-    sleep 2
-done
-
-# testbloat should be killed and testchill should be fine
-if systemctl --machine "testuser@.host" --user status TEST-55-OOMD-testbloat.service; then exit 42; fi
-if ! systemctl --machine "testuser@.host" --user status TEST-55-OOMD-testchill.service; then exit 24; fi
-
-loginctl disable-linger testuser
 
-# only run this portion of the test if we can set xattrs
-if cgroupfs_supports_user_xattrs; then
     sleep 120 # wait for systemd-oomd kill cool down and elevated memory pressure to come down
 
     mkdir -p /run/systemd/system/TEST-55-OOMD-testbloat.service.d/
@@ -174,7 +166,9 @@ EOF
     if ! systemctl status TEST-55-OOMD-testbloat.service; then exit 25; fi
     if systemctl status TEST-55-OOMD-testmunch.service; then exit 43; fi
     if ! systemctl status TEST-55-OOMD-testchill.service; then exit 24; fi
-fi
+}
+
+run_testcases
 
 systemd-analyze log-level info