From 8239ecf0b4b8bbe5b3c17964d230d13cee4d900a Mon Sep 17 00:00:00 2001
From: Frantisek Sumsal <frantisek@sumsal.cz>
Date: Mon, 5 Aug 2019 14:38:45 +0200
Subject: [PATCH] test: add a simple sanity check for systems without NUMA
 support

(cherry picked from commit 92f8e978923f962a57d744c5f358520ac06f7892)

Related: #1808940
---
 test/TEST-36-NUMAPOLICY/testsuite.sh | 350 ++++++++++++++-------------
 1 file changed, 180 insertions(+), 170 deletions(-)

diff --git a/test/TEST-36-NUMAPOLICY/testsuite.sh b/test/TEST-36-NUMAPOLICY/testsuite.sh
index 1c8cf7e6b6..a5ac788178 100755
--- a/test/TEST-36-NUMAPOLICY/testsuite.sh
+++ b/test/TEST-36-NUMAPOLICY/testsuite.sh
@@ -50,11 +50,12 @@ startJournalctl() {
 }
 
 stopJournalctl() {
+    local unit="${1:-init.scope}"
     # Using journalctl --sync should be better than using SIGRTMIN+1, as
     # the --sync wait until the synchronization is complete
     echo "Force journald to write all queued messages"
     journalctl --sync
-    journalctl -u init.scope --cursor-file="$journalCursorFile" > "$journalLog"
+    journalctl -u $unit --cursor-file="$journalCursorFile" > "$journalLog"
 }
 
 checkNUMA() {
@@ -125,181 +126,190 @@ systemctlCheckNUMAProperties() {
     fi
 }
 
-if ! checkNUMA; then
-    echo >&2 "NUMA is not supported on this machine, skipping the test"
-
-    # FIXME: add some sanity checks to verify systemd behaves correctly with
-    #        NUMA disabled together with NUMAPolicy= and NUMAMask=
-
-    systemd-analyze log-level info
-    echo OK > /testok
-
-    exit 0
-fi
-
 writeTestUnit
 
 # Create systemd config drop-in directory
 confDir="/etc/systemd/system.conf.d/"
 mkdir -p "$confDir"
 
-echo "PID1 NUMAPolicy support - Default policy w/o mask"
-writePID1NUMAPolicy "default"
-pid1ReloadWithStrace
-# Kernel requires that nodemask argument is set to NULL when setting default policy
-grep "set_mempolicy(MPOL_DEFAULT, NULL" $straceLog
-
-echo "PID1 NUMAPolicy support - Default policy w/ mask"
-writePID1NUMAPolicy "default" "0"
-pid1ReloadWithStrace
-grep "set_mempolicy(MPOL_DEFAULT, NULL" $straceLog
-
-echo "PID1 NUMAPolicy support - Bind policy w/o mask"
-writePID1NUMAPolicy "bind"
-pid1ReloadWithJournal
-grep "Failed to set NUMA memory policy: Invalid argument" $journalLog
-
-echo "PID1 NUMAPolicy support - Bind policy w/ mask"
-writePID1NUMAPolicy "bind" "0"
-pid1ReloadWithStrace
-grep -P "set_mempolicy\(MPOL_BIND, \[0x0*1\]" $straceLog
-
-echo "PID1 NUMAPolicy support - Interleave policy w/o mask"
-writePID1NUMAPolicy "interleave"
-pid1ReloadWithJournal
-grep "Failed to set NUMA memory policy: Invalid argument" $journalLog
-
-echo "PID1 NUMAPolicy support - Interleave policy w/ mask"
-writePID1NUMAPolicy "interleave" "0"
-pid1ReloadWithStrace
-grep -P "set_mempolicy\(MPOL_INTERLEAVE, \[0x0*1\]" $straceLog
-
-echo "PID1 NUMAPolicy support - Preferred policy w/o mask"
-writePID1NUMAPolicy "preferred"
-pid1ReloadWithJournal
-# Preferred policy with empty node mask is actually allowed and should reset allocation policy to default
-! grep "Failed to set NUMA memory policy: Invalid argument" $journalLog
-
-echo "PID1 NUMAPolicy support - Preferred policy w/ mask"
-writePID1NUMAPolicy "preferred" "0"
-pid1ReloadWithStrace
-grep -P "set_mempolicy\(MPOL_PREFERRED, \[0x0*1\]" $straceLog
-
-echo "PID1 NUMAPolicy support - Local policy w/o mask"
-writePID1NUMAPolicy "local"
-pid1ReloadWithStrace
-# Kernel requires that nodemask argument is set to NULL when setting default policy
-# The unpatched versions of strace don't recognize the MPOL_LOCAL constant and
-# return a numerical constant instead (with a comment):
-#   set_mempolicy(0x4 /* MPOL_??? */, NULL, 0) = 0
-# Let's cover this scenario as well
-grep -E "set_mempolicy\((MPOL_LOCAL|0x4 [^,]*), NULL" $straceLog
-
-echo "PID1 NUMAPolicy support - Local policy w/ mask"
-writePID1NUMAPolicy "local" "0"
-pid1ReloadWithStrace
-grep -E "set_mempolicy\((MPOL_LOCAL|0x4 [^,]*), NULL" $straceLog
-
-echo "Unit file NUMAPolicy support - Default policy w/o mask"
-writeTestUnitNUMAPolicy "default"
-pid1StartUnitWithStrace $testUnit
-systemctlCheckNUMAProperties $testUnit "default"
-pid1StopUnit $testUnit
-grep "set_mempolicy(MPOL_DEFAULT, NULL" $straceLog
-
-echo "Unit file NUMAPolicy support - Default policy w/ mask"
-writeTestUnitNUMAPolicy "default" "0"
-pid1StartUnitWithStrace $testUnit
-systemctlCheckNUMAProperties $testUnit "default" "0"
-pid1StopUnit $testUnit
-# Maks must be ignored
-grep "set_mempolicy(MPOL_DEFAULT, NULL" $straceLog
-
-echo "Unit file NUMAPolicy support - Bind policy w/o mask"
-writeTestUnitNUMAPolicy "bind"
-pid1StartUnitWithJournal $testUnit
-pid1StopUnit $testUnit
-grep "numa-test.service: Main process exited, code=exited, status=242/NUMA" $journalLog
-
-echo "Unit file NUMAPolicy support - Bind policy w/ mask"
-writeTestUnitNUMAPolicy "bind" "0"
-pid1StartUnitWithStrace $testUnit
-systemctlCheckNUMAProperties $testUnit "bind" "0"
-pid1StopUnit $testUnit
-grep -P "set_mempolicy\(MPOL_BIND, \[0x0*1\]" $straceLog
-
-echo "Unit file NUMAPolicy support - Interleave policy w/o mask"
-writeTestUnitNUMAPolicy "interleave"
-pid1StartUnitWithStrace $testUnit
-pid1StopUnit $testUnit
-grep "numa-test.service: Main process exited, code=exited, status=242/NUMA" $journalLog
-
-echo "Unit file NUMAPolicy support - Interleave policy w/ mask"
-writeTestUnitNUMAPolicy "interleave" "0"
-pid1StartUnitWithStrace $testUnit
-systemctlCheckNUMAProperties $testUnit "interleave" "0"
-pid1StopUnit $testUnit
-grep -P "set_mempolicy\(MPOL_INTERLEAVE, \[0x0*1\]" $straceLog
-
-echo "Unit file NUMAPolicy support - Preferred policy w/o mask"
-writeTestUnitNUMAPolicy "preferred"
-pid1StartUnitWithJournal $testUnit
-systemctlCheckNUMAProperties $testUnit "preferred"
-pid1StopUnit $testUnit
-! grep "numa-test.service: Main process exited, code=exited, status=242/NUMA" $journalLog
-
-echo "Unit file NUMAPolicy support - Preferred policy w/ mask"
-writeTestUnitNUMAPolicy "preferred" "0"
-pid1StartUnitWithStrace $testUnit
-systemctlCheckNUMAProperties $testUnit "preferred" "0"
-pid1StopUnit $testUnit
-grep -P "set_mempolicy\(MPOL_PREFERRED, \[0x0*1\]" $straceLog
-
-echo "Unit file NUMAPolicy support - Local policy w/o mask"
-writeTestUnitNUMAPolicy "local"
-pid1StartUnitWithStrace $testUnit
-systemctlCheckNUMAProperties $testUnit "local"
-pid1StopUnit $testUnit
-grep -E "set_mempolicy\((MPOL_LOCAL|0x4 [^,]*), NULL" $straceLog
-
-echo "Unit file NUMAPolicy support - Local policy w/ mask"
-writeTestUnitNUMAPolicy "local" "0"
-pid1StartUnitWithStrace $testUnit
-systemctlCheckNUMAProperties $testUnit "local" "0"
-pid1StopUnit $testUnit
-# Maks must be ignored
-grep -E "set_mempolicy\((MPOL_LOCAL|0x4 [^,]*), NULL" $straceLog
-
-echo "systemd-run NUMAPolicy support"
-runUnit='numa-systemd-run-test.service'
-
-systemd-run -p NUMAPolicy=default --unit $runUnit sleep 1000
-systemctlCheckNUMAProperties $runUnit "default"
-pid1StopUnit $runUnit
-
-systemd-run -p NUMAPolicy=default -p NUMAMask=0 --unit $runUnit sleep 1000
-systemctlCheckNUMAProperties $runUnit "default" ""
-pid1StopUnit $runUnit
-
-systemd-run -p NUMAPolicy=bind -p NUMAMask=0 --unit $runUnit sleep 1000
-systemctlCheckNUMAProperties $runUnit "bind" "0"
-pid1StopUnit $runUnit
-
-systemd-run -p NUMAPolicy=interleave -p NUMAMask=0 --unit $runUnit sleep 1000
-systemctlCheckNUMAProperties $runUnit "interleave" "0"
-pid1StopUnit $runUnit
-
-systemd-run -p NUMAPolicy=preferred -p NUMAMask=0 --unit $runUnit sleep 1000
-systemctlCheckNUMAProperties $runUnit "preferred" "0"
-pid1StopUnit $runUnit
-
-systemd-run -p NUMAPolicy=local --unit $runUnit sleep 1000
-systemctlCheckNUMAProperties $runUnit "local"
-pid1StopUnit $runUnit
-
-systemd-run -p NUMAPolicy=local -p NUMAMask=0 --unit $runUnit sleep 1000
-systemctlCheckNUMAProperties $runUnit "local" ""
-pid1StopUnit $runUnit
+if ! checkNUMA; then
+    echo >&2 "NUMA is not supported on this machine, switching to a simple sanity check"
+
+    echo "PID1 NUMAPolicy=default && NUMAMask=0 check without NUMA support"
+    writePID1NUMAPolicy "default" "0"
+    startJournalctl
+    systemctl daemon-reload
+    stopJournalctl
+    grep "NUMA support not available, ignoring" "$journalLog"
+
+    echo "systemd-run NUMAPolicy=default && NUMAMask=0 check without NUMA support"
+    runUnit='numa-systemd-run-test.service'
+    startJournalctl
+    systemd-run -p NUMAPolicy=default -p NUMAMask=0 --unit $runUnit sleep 1000
+    sleep $sleepAfterStart
+    pid1StopUnit $runUnit
+    stopJournalctl $runUnit
+    grep "NUMA support not available, ignoring" "$journalLog"
+
+else
+    echo "PID1 NUMAPolicy support - Default policy w/o mask"
+    writePID1NUMAPolicy "default"
+    pid1ReloadWithStrace
+    # Kernel requires that nodemask argument is set to NULL when setting default policy
+    grep "set_mempolicy(MPOL_DEFAULT, NULL" $straceLog
+
+    echo "PID1 NUMAPolicy support - Default policy w/ mask"
+    writePID1NUMAPolicy "default" "0"
+    pid1ReloadWithStrace
+    grep "set_mempolicy(MPOL_DEFAULT, NULL" $straceLog
+
+    echo "PID1 NUMAPolicy support - Bind policy w/o mask"
+    writePID1NUMAPolicy "bind"
+    pid1ReloadWithJournal
+    grep "Failed to set NUMA memory policy: Invalid argument" $journalLog
+
+    echo "PID1 NUMAPolicy support - Bind policy w/ mask"
+    writePID1NUMAPolicy "bind" "0"
+    pid1ReloadWithStrace
+    grep -P "set_mempolicy\(MPOL_BIND, \[0x0*1\]" $straceLog
+
+    echo "PID1 NUMAPolicy support - Interleave policy w/o mask"
+    writePID1NUMAPolicy "interleave"
+    pid1ReloadWithJournal
+    grep "Failed to set NUMA memory policy: Invalid argument" $journalLog
+
+    echo "PID1 NUMAPolicy support - Interleave policy w/ mask"
+    writePID1NUMAPolicy "interleave" "0"
+    pid1ReloadWithStrace
+    grep -P "set_mempolicy\(MPOL_INTERLEAVE, \[0x0*1\]" $straceLog
+
+    echo "PID1 NUMAPolicy support - Preferred policy w/o mask"
+    writePID1NUMAPolicy "preferred"
+    pid1ReloadWithJournal
+    # Preferred policy with empty node mask is actually allowed and should reset allocation policy to default
+    ! grep "Failed to set NUMA memory policy: Invalid argument" $journalLog
+
+    echo "PID1 NUMAPolicy support - Preferred policy w/ mask"
+    writePID1NUMAPolicy "preferred" "0"
+    pid1ReloadWithStrace
+    grep -P "set_mempolicy\(MPOL_PREFERRED, \[0x0*1\]" $straceLog
+
+    echo "PID1 NUMAPolicy support - Local policy w/o mask"
+    writePID1NUMAPolicy "local"
+    pid1ReloadWithStrace
+    # Kernel requires that nodemask argument is set to NULL when setting default policy
+    # The unpatched versions of strace don't recognize the MPOL_LOCAL constant and
+    # return a numerical constant instead (with a comment):
+    #   set_mempolicy(0x4 /* MPOL_??? */, NULL, 0) = 0
+    # Let's cover this scenario as well
+    grep -E "set_mempolicy\((MPOL_LOCAL|0x4 [^,]*), NULL" $straceLog
+
+    echo "PID1 NUMAPolicy support - Local policy w/ mask"
+    writePID1NUMAPolicy "local" "0"
+    pid1ReloadWithStrace
+    grep -E "set_mempolicy\((MPOL_LOCAL|0x4 [^,]*), NULL" $straceLog
+
+    echo "Unit file NUMAPolicy support - Default policy w/o mask"
+    writeTestUnitNUMAPolicy "default"
+    pid1StartUnitWithStrace $testUnit
+    systemctlCheckNUMAProperties $testUnit "default"
+    pid1StopUnit $testUnit
+    grep "set_mempolicy(MPOL_DEFAULT, NULL" $straceLog
+
+    echo "Unit file NUMAPolicy support - Default policy w/ mask"
+    writeTestUnitNUMAPolicy "default" "0"
+    pid1StartUnitWithStrace $testUnit
+    systemctlCheckNUMAProperties $testUnit "default" "0"
+    pid1StopUnit $testUnit
+    # Maks must be ignored
+    grep "set_mempolicy(MPOL_DEFAULT, NULL" $straceLog
+
+    echo "Unit file NUMAPolicy support - Bind policy w/o mask"
+    writeTestUnitNUMAPolicy "bind"
+    pid1StartUnitWithJournal $testUnit
+    pid1StopUnit $testUnit
+    grep "numa-test.service: Main process exited, code=exited, status=242/NUMA" $journalLog
+
+    echo "Unit file NUMAPolicy support - Bind policy w/ mask"
+    writeTestUnitNUMAPolicy "bind" "0"
+    pid1StartUnitWithStrace $testUnit
+    systemctlCheckNUMAProperties $testUnit "bind" "0"
+    pid1StopUnit $testUnit
+    grep -P "set_mempolicy\(MPOL_BIND, \[0x0*1\]" $straceLog
+
+    echo "Unit file NUMAPolicy support - Interleave policy w/o mask"
+    writeTestUnitNUMAPolicy "interleave"
+    pid1StartUnitWithStrace $testUnit
+    pid1StopUnit $testUnit
+    grep "numa-test.service: Main process exited, code=exited, status=242/NUMA" $journalLog
+
+    echo "Unit file NUMAPolicy support - Interleave policy w/ mask"
+    writeTestUnitNUMAPolicy "interleave" "0"
+    pid1StartUnitWithStrace $testUnit
+    systemctlCheckNUMAProperties $testUnit "interleave" "0"
+    pid1StopUnit $testUnit
+    grep -P "set_mempolicy\(MPOL_INTERLEAVE, \[0x0*1\]" $straceLog
+
+    echo "Unit file NUMAPolicy support - Preferred policy w/o mask"
+    writeTestUnitNUMAPolicy "preferred"
+    pid1StartUnitWithJournal $testUnit
+    systemctlCheckNUMAProperties $testUnit "preferred"
+    pid1StopUnit $testUnit
+    ! grep "numa-test.service: Main process exited, code=exited, status=242/NUMA" $journalLog
+
+    echo "Unit file NUMAPolicy support - Preferred policy w/ mask"
+    writeTestUnitNUMAPolicy "preferred" "0"
+    pid1StartUnitWithStrace $testUnit
+    systemctlCheckNUMAProperties $testUnit "preferred" "0"
+    pid1StopUnit $testUnit
+    grep -P "set_mempolicy\(MPOL_PREFERRED, \[0x0*1\]" $straceLog
+
+    echo "Unit file NUMAPolicy support - Local policy w/o mask"
+    writeTestUnitNUMAPolicy "local"
+    pid1StartUnitWithStrace $testUnit
+    systemctlCheckNUMAProperties $testUnit "local"
+    pid1StopUnit $testUnit
+    grep -E "set_mempolicy\((MPOL_LOCAL|0x4 [^,]*), NULL" $straceLog
+
+    echo "Unit file NUMAPolicy support - Local policy w/ mask"
+    writeTestUnitNUMAPolicy "local" "0"
+    pid1StartUnitWithStrace $testUnit
+    systemctlCheckNUMAProperties $testUnit "local" "0"
+    pid1StopUnit $testUnit
+    # Maks must be ignored
+    grep -E "set_mempolicy\((MPOL_LOCAL|0x4 [^,]*), NULL" $straceLog
+
+    echo "systemd-run NUMAPolicy support"
+    runUnit='numa-systemd-run-test.service'
+
+    systemd-run -p NUMAPolicy=default --unit $runUnit sleep 1000
+    systemctlCheckNUMAProperties $runUnit "default"
+    pid1StopUnit $runUnit
+
+    systemd-run -p NUMAPolicy=default -p NUMAMask=0 --unit $runUnit sleep 1000
+    systemctlCheckNUMAProperties $runUnit "default" ""
+    pid1StopUnit $runUnit
+
+    systemd-run -p NUMAPolicy=bind -p NUMAMask=0 --unit $runUnit sleep 1000
+    systemctlCheckNUMAProperties $runUnit "bind" "0"
+    pid1StopUnit $runUnit
+
+    systemd-run -p NUMAPolicy=interleave -p NUMAMask=0 --unit $runUnit sleep 1000
+    systemctlCheckNUMAProperties $runUnit "interleave" "0"
+    pid1StopUnit $runUnit
+
+    systemd-run -p NUMAPolicy=preferred -p NUMAMask=0 --unit $runUnit sleep 1000
+    systemctlCheckNUMAProperties $runUnit "preferred" "0"
+    pid1StopUnit $runUnit
+
+    systemd-run -p NUMAPolicy=local --unit $runUnit sleep 1000
+    systemctlCheckNUMAProperties $runUnit "local"
+    pid1StopUnit $runUnit
+
+    systemd-run -p NUMAPolicy=local -p NUMAMask=0 --unit $runUnit sleep 1000
+    systemctlCheckNUMAProperties $runUnit "local" ""
+    pid1StopUnit $runUnit
+fi
 
 # Cleanup
 rm -rf $testDir
