c2dfb7
From 90dda340e4adeb1126639a849d4f31ae327fdc4b Mon Sep 17 00:00:00 2001
c2dfb7
From: Frantisek Sumsal <frantisek@sumsal.cz>
c2dfb7
Date: Tue, 25 Jun 2019 23:01:40 +0200
c2dfb7
Subject: [PATCH] test: introduce TEST-36-NUMAPOLICY
c2dfb7
c2dfb7
(cherry picked from commit 8f65e26508969610ac934d1aadbade8223bfcaac)
c2dfb7
c2dfb7
Related: #1808940
c2dfb7
---
c2dfb7
 test/TEST-36-NUMAPOLICY/Makefile     |   1 +
c2dfb7
 test/TEST-36-NUMAPOLICY/test.sh      |  51 +++++
c2dfb7
 test/TEST-36-NUMAPOLICY/testsuite.sh | 292 +++++++++++++++++++++++++++
c2dfb7
 3 files changed, 344 insertions(+)
c2dfb7
 create mode 120000 test/TEST-36-NUMAPOLICY/Makefile
c2dfb7
 create mode 100755 test/TEST-36-NUMAPOLICY/test.sh
c2dfb7
 create mode 100755 test/TEST-36-NUMAPOLICY/testsuite.sh
c2dfb7
c2dfb7
diff --git a/test/TEST-36-NUMAPOLICY/Makefile b/test/TEST-36-NUMAPOLICY/Makefile
c2dfb7
new file mode 120000
c2dfb7
index 0000000000..e9f93b1104
c2dfb7
--- /dev/null
c2dfb7
+++ b/test/TEST-36-NUMAPOLICY/Makefile
c2dfb7
@@ -0,0 +1 @@
c2dfb7
+../TEST-01-BASIC/Makefile
c2dfb7
\ No newline at end of file
c2dfb7
diff --git a/test/TEST-36-NUMAPOLICY/test.sh b/test/TEST-36-NUMAPOLICY/test.sh
c2dfb7
new file mode 100755
c2dfb7
index 0000000000..a0d8623e8e
c2dfb7
--- /dev/null
c2dfb7
+++ b/test/TEST-36-NUMAPOLICY/test.sh
c2dfb7
@@ -0,0 +1,51 @@
c2dfb7
+#!/bin/bash
c2dfb7
+set -e
c2dfb7
+TEST_DESCRIPTION="test MUMAPolicy= and NUMAMask= options"
c2dfb7
+TEST_NO_NSPAWN=1
c2dfb7
+QEMU_OPTIONS="-numa node,nodeid=0"
c2dfb7
+
c2dfb7
+. $TEST_BASE_DIR/test-functions
c2dfb7
+
c2dfb7
+test_setup() {
c2dfb7
+    create_empty_image
c2dfb7
+    mkdir -p $TESTDIR/root
c2dfb7
+    mount ${LOOPDEV}p1 $TESTDIR/root
c2dfb7
+
c2dfb7
+    (
c2dfb7
+        LOG_LEVEL=5
c2dfb7
+        eval $(udevadm info --export --query=env --name=${LOOPDEV}p2)
c2dfb7
+
c2dfb7
+        setup_basic_environment
c2dfb7
+        inst_binary mktemp
c2dfb7
+
c2dfb7
+        # mask some services that we do not want to run in these tests
c2dfb7
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-hwdb-update.service
c2dfb7
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-journal-catalog-update.service
c2dfb7
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-networkd.service
c2dfb7
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-networkd.socket
c2dfb7
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-resolved.service
c2dfb7
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-machined.service
c2dfb7
+
c2dfb7
+        # setup the testsuite service
c2dfb7
+        cat >$initdir/etc/systemd/system/testsuite.service <
c2dfb7
+[Unit]
c2dfb7
+Description=Testsuite service
c2dfb7
+
c2dfb7
+[Service]
c2dfb7
+ExecStart=/bin/bash -x /testsuite.sh
c2dfb7
+Type=oneshot
c2dfb7
+StandardOutput=tty
c2dfb7
+StandardError=tty
c2dfb7
+NotifyAccess=all
c2dfb7
+EOF
c2dfb7
+        cp testsuite.sh $initdir/
c2dfb7
+
c2dfb7
+        setup_testsuite
c2dfb7
+    ) || return 1
c2dfb7
+    setup_nspawn_root
c2dfb7
+
c2dfb7
+    ddebug "umount $TESTDIR/root"
c2dfb7
+    umount $TESTDIR/root
c2dfb7
+}
c2dfb7
+
c2dfb7
+do_test "$@"
c2dfb7
diff --git a/test/TEST-36-NUMAPOLICY/testsuite.sh b/test/TEST-36-NUMAPOLICY/testsuite.sh
c2dfb7
new file mode 100755
c2dfb7
index 0000000000..e15087b137
c2dfb7
--- /dev/null
c2dfb7
+++ b/test/TEST-36-NUMAPOLICY/testsuite.sh
c2dfb7
@@ -0,0 +1,292 @@
c2dfb7
+#!/bin/bash
c2dfb7
+set -ex
c2dfb7
+set -o pipefail
c2dfb7
+
c2dfb7
+at_exit() {
c2dfb7
+    if [ $? -ne 0 ]; then
c2dfb7
+        # We're exiting with a non-zero EC, let's dump test artifacts
c2dfb7
+        # for easier debugging
c2dfb7
+        [ -f "$straceLog" ] && cat "$straceLog"
c2dfb7
+        [ -f "$journalLog" ] && cat "$journalLog"
c2dfb7
+    fi
c2dfb7
+}
c2dfb7
+
c2dfb7
+trap at_exit EXIT
c2dfb7
+
c2dfb7
+systemd-analyze log-level debug
c2dfb7
+systemd-analyze log-target journal
c2dfb7
+
c2dfb7
+# Log files
c2dfb7
+straceLog='strace.log'
c2dfb7
+journalLog='journal.log'
c2dfb7
+
c2dfb7
+# Systemd config files
c2dfb7
+testUnit='numa-test.service'
c2dfb7
+testUnitFile="/etc/systemd/system/$testUnit"
c2dfb7
+testUnitNUMAConf="$testUnitFile.d/numa.conf"
c2dfb7
+
c2dfb7
+# Sleep constants (we should probably figure out something better but nothing comes to mind)
c2dfb7
+journalSleep=5
c2dfb7
+sleepAfterStart=1
c2dfb7
+
c2dfb7
+startStrace() {
c2dfb7
+    coproc strace -qq -p 1 -o $straceLog -e set_mempolicy -s 1024 $1
c2dfb7
+}
c2dfb7
+
c2dfb7
+stopStrace() {
c2dfb7
+    kill -s TERM $COPROC_PID
c2dfb7
+}
c2dfb7
+
c2dfb7
+startJournalctl() {
c2dfb7
+    coproc journalctl -u init.scope -f > $journalLog
c2dfb7
+}
c2dfb7
+
c2dfb7
+stopJournalctl() {
c2dfb7
+    # Wait a few seconds until the messages get properly queued...
c2dfb7
+    sleep $journalSleep
c2dfb7
+    # ...and then force journald to write them to the backing storage
c2dfb7
+    # Also, using journalctl --sync should be better than using SIGRTMIN+1, as
c2dfb7
+    # the --sync wait until the synchronization is complete
c2dfb7
+    echo "Force journald to write all queued messages"
c2dfb7
+    journalctl --sync
c2dfb7
+    kill -s TERM $COPROC_PID
c2dfb7
+}
c2dfb7
+
c2dfb7
+checkNUMA() {
c2dfb7
+    # NUMA enabled system should have at least NUMA node0
c2dfb7
+    test -e /sys/devices/system/node/node0
c2dfb7
+}
c2dfb7
+
c2dfb7
+writePID1NUMAPolicy() {
c2dfb7
+    echo [Manager] > $confDir/numa.conf
c2dfb7
+    echo NUMAPolicy=$1 >> $confDir/numa.conf
c2dfb7
+    echo NUMAMask=$2>> $confDir/numa.conf
c2dfb7
+}
c2dfb7
+
c2dfb7
+writeTestUnit() {
c2dfb7
+    echo [Service] > $testUnitFile
c2dfb7
+    echo ExecStart=/bin/sleep 3600 >> $testUnitFile
c2dfb7
+    mkdir -p $testUnitFile.d/
c2dfb7
+}
c2dfb7
+
c2dfb7
+writeTestUnitNUMAPolicy() {
c2dfb7
+    echo [Service] > $testUnitNUMAConf
c2dfb7
+    echo NUMAPolicy=$1 >> $testUnitNUMAConf
c2dfb7
+    echo NUMAMask=$2>> $testUnitNUMAConf
c2dfb7
+    systemctl daemon-reload
c2dfb7
+}
c2dfb7
+
c2dfb7
+pid1ReloadWithStrace() {
c2dfb7
+    startStrace
c2dfb7
+    systemctl daemon-reload
c2dfb7
+    stopStrace
c2dfb7
+}
c2dfb7
+
c2dfb7
+pid1ReloadWithJournal() {
c2dfb7
+    startJournalctl
c2dfb7
+    systemctl daemon-reload
c2dfb7
+    stopJournalctl
c2dfb7
+}
c2dfb7
+
c2dfb7
+pid1StartUnitWithStrace() {
c2dfb7
+    startStrace '-f'
c2dfb7
+    systemctl start $1
c2dfb7
+    sleep $sleepAfterStart
c2dfb7
+    stopStrace
c2dfb7
+}
c2dfb7
+
c2dfb7
+pid1StartUnitWithJournal() {
c2dfb7
+    startJournalctl
c2dfb7
+    systemctl start $1
c2dfb7
+    sleep $sleepAfterStart
c2dfb7
+    stopJournalctl
c2dfb7
+}
c2dfb7
+
c2dfb7
+pid1StopUnit() {
c2dfb7
+    systemctl stop $1
c2dfb7
+}
c2dfb7
+
c2dfb7
+systemctlCheckNUMAProperties() {
c2dfb7
+    local LOGFILE="$(mktemp)"
c2dfb7
+    systemctl show -p NUMAPolicy $1 > "$LOGFILE"
c2dfb7
+    grep "NUMAPolicy=$2" "$LOGFILE"
c2dfb7
+
c2dfb7
+    > "$LOGFILE"
c2dfb7
+
c2dfb7
+    if [ -n $3 ]; then
c2dfb7
+        systemctl show -p NUMAMask $1 > "$LOGFILE"
c2dfb7
+        grep "NUMAMask=$3" "$LOGFILE"
c2dfb7
+    fi
c2dfb7
+}
c2dfb7
+
c2dfb7
+checkNUMA
c2dfb7
+writeTestUnit
c2dfb7
+
c2dfb7
+# Create systemd config drop-in directory
c2dfb7
+confDir="/etc/systemd/system.conf.d/"
c2dfb7
+mkdir -p "$confDir"
c2dfb7
+
c2dfb7
+echo "PID1 NUMAPolicy support - Default policy w/o mask"
c2dfb7
+writePID1NUMAPolicy "default"
c2dfb7
+pid1ReloadWithStrace
c2dfb7
+# Kernel requires that nodemask argument is set to NULL when setting default policy
c2dfb7
+grep "set_mempolicy(MPOL_DEFAULT, NULL" $straceLog
c2dfb7
+
c2dfb7
+echo "PID1 NUMAPolicy support - Default policy w/ mask"
c2dfb7
+writePID1NUMAPolicy "default" "0"
c2dfb7
+pid1ReloadWithStrace
c2dfb7
+grep "set_mempolicy(MPOL_DEFAULT, NULL" $straceLog
c2dfb7
+
c2dfb7
+echo "PID1 NUMAPolicy support - Bind policy w/o mask"
c2dfb7
+writePID1NUMAPolicy "bind"
c2dfb7
+pid1ReloadWithJournal
c2dfb7
+grep "Failed to set NUMA memory policy: Invalid argument" $journalLog
c2dfb7
+
c2dfb7
+echo "PID1 NUMAPolicy support - Bind policy w/ mask"
c2dfb7
+writePID1NUMAPolicy "bind" "0"
c2dfb7
+pid1ReloadWithStrace
c2dfb7
+grep -P "set_mempolicy\(MPOL_BIND, \[0x0*1\]" $straceLog
c2dfb7
+
c2dfb7
+echo "PID1 NUMAPolicy support - Interleave policy w/o mask"
c2dfb7
+writePID1NUMAPolicy "interleave"
c2dfb7
+pid1ReloadWithJournal
c2dfb7
+grep "Failed to set NUMA memory policy: Invalid argument" $journalLog
c2dfb7
+
c2dfb7
+echo "PID1 NUMAPolicy support - Interleave policy w/ mask"
c2dfb7
+writePID1NUMAPolicy "interleave" "0"
c2dfb7
+pid1ReloadWithStrace
c2dfb7
+grep -P "set_mempolicy\(MPOL_INTERLEAVE, \[0x0*1\]" $straceLog
c2dfb7
+
c2dfb7
+echo "PID1 NUMAPolicy support - Preferred policy w/o mask"
c2dfb7
+writePID1NUMAPolicy "preferred"
c2dfb7
+pid1ReloadWithJournal
c2dfb7
+# Preferred policy with empty node mask is actually allowed and should reset allocation policy to default
c2dfb7
+! grep "Failed to set NUMA memory policy: Invalid argument" $journalLog
c2dfb7
+
c2dfb7
+echo "PID1 NUMAPolicy support - Preferred policy w/ mask"
c2dfb7
+writePID1NUMAPolicy "preferred" "0"
c2dfb7
+pid1ReloadWithStrace
c2dfb7
+grep -P "set_mempolicy\(MPOL_PREFERRED, \[0x0*1\]" $straceLog
c2dfb7
+
c2dfb7
+echo "PID1 NUMAPolicy support - Local policy w/o mask"
c2dfb7
+writePID1NUMAPolicy "local"
c2dfb7
+pid1ReloadWithStrace
c2dfb7
+# Kernel requires that nodemask argument is set to NULL when setting default policy
c2dfb7
+grep "set_mempolicy(MPOL_LOCAL, NULL" $straceLog
c2dfb7
+
c2dfb7
+echo "PID1 NUMAPolicy support - Local policy w/ mask"
c2dfb7
+writePID1NUMAPolicy "local" "0"
c2dfb7
+pid1ReloadWithStrace
c2dfb7
+grep "set_mempolicy(MPOL_LOCAL, NULL" $straceLog
c2dfb7
+
c2dfb7
+echo "Unit file NUMAPolicy support - Default policy w/o mask"
c2dfb7
+writeTestUnitNUMAPolicy "default"
c2dfb7
+pid1StartUnitWithStrace $testUnit
c2dfb7
+systemctlCheckNUMAProperties $testUnit "default"
c2dfb7
+pid1StopUnit $testUnit
c2dfb7
+grep "set_mempolicy(MPOL_DEFAULT, NULL" $straceLog
c2dfb7
+
c2dfb7
+echo "Unit file NUMAPolicy support - Default policy w/ mask"
c2dfb7
+writeTestUnitNUMAPolicy "default" "0"
c2dfb7
+pid1StartUnitWithStrace $testUnit
c2dfb7
+systemctlCheckNUMAProperties $testUnit "default" "0"
c2dfb7
+pid1StopUnit $testUnit
c2dfb7
+# Maks must be ignored
c2dfb7
+grep "set_mempolicy(MPOL_DEFAULT, NULL" $straceLog
c2dfb7
+
c2dfb7
+echo "Unit file NUMAPolicy support - Bind policy w/o mask"
c2dfb7
+writeTestUnitNUMAPolicy "bind"
c2dfb7
+pid1StartUnitWithJournal $testUnit
c2dfb7
+pid1StopUnit $testUnit
c2dfb7
+grep "numa-test.service: Main process exited, code=exited, status=242/NUMA" $journalLog
c2dfb7
+
c2dfb7
+echo "Unit file NUMAPolicy support - Bind policy w/ mask"
c2dfb7
+writeTestUnitNUMAPolicy "bind" "0"
c2dfb7
+pid1StartUnitWithStrace $testUnit
c2dfb7
+systemctlCheckNUMAProperties $testUnit "bind" "0"
c2dfb7
+pid1StopUnit $testUnit
c2dfb7
+grep -P "set_mempolicy\(MPOL_BIND, \[0x0*1\]" $straceLog
c2dfb7
+
c2dfb7
+echo "Unit file NUMAPolicy support - Interleave policy w/o mask"
c2dfb7
+writeTestUnitNUMAPolicy "interleave"
c2dfb7
+pid1StartUnitWithStrace $testUnit
c2dfb7
+pid1StopUnit $testUnit
c2dfb7
+grep "numa-test.service: Main process exited, code=exited, status=242/NUMA" $journalLog
c2dfb7
+
c2dfb7
+echo "Unit file NUMAPolicy support - Interleave policy w/ mask"
c2dfb7
+writeTestUnitNUMAPolicy "interleave" "0"
c2dfb7
+pid1StartUnitWithStrace $testUnit
c2dfb7
+systemctlCheckNUMAProperties $testUnit "interleave" "0"
c2dfb7
+pid1StopUnit $testUnit
c2dfb7
+grep -P "set_mempolicy\(MPOL_INTERLEAVE, \[0x0*1\]" $straceLog
c2dfb7
+
c2dfb7
+echo "Unit file NUMAPolicy support - Preferred policy w/o mask"
c2dfb7
+writeTestUnitNUMAPolicy "preferred"
c2dfb7
+pid1StartUnitWithJournal $testUnit
c2dfb7
+systemctlCheckNUMAProperties $testUnit "preferred"
c2dfb7
+pid1StopUnit $testUnit
c2dfb7
+! grep "numa-test.service: Main process exited, code=exited, status=242/NUMA" $journalLog
c2dfb7
+
c2dfb7
+echo "Unit file NUMAPolicy support - Preferred policy w/ mask"
c2dfb7
+writeTestUnitNUMAPolicy "preferred" "0"
c2dfb7
+pid1StartUnitWithStrace $testUnit
c2dfb7
+systemctlCheckNUMAProperties $testUnit "preferred" "0"
c2dfb7
+pid1StopUnit $testUnit
c2dfb7
+grep -P "set_mempolicy\(MPOL_PREFERRED, \[0x0*1\]" $straceLog
c2dfb7
+
c2dfb7
+echo "Unit file NUMAPolicy support - Local policy w/o mask"
c2dfb7
+writeTestUnitNUMAPolicy "local"
c2dfb7
+pid1StartUnitWithStrace $testUnit
c2dfb7
+systemctlCheckNUMAProperties $testUnit "local"
c2dfb7
+pid1StopUnit $testUnit
c2dfb7
+grep "set_mempolicy(MPOL_LOCAL, NULL" $straceLog
c2dfb7
+
c2dfb7
+echo "Unit file NUMAPolicy support - Local policy w/ mask"
c2dfb7
+writeTestUnitNUMAPolicy "local" "0"
c2dfb7
+pid1StartUnitWithStrace $testUnit
c2dfb7
+systemctlCheckNUMAProperties $testUnit "local" "0"
c2dfb7
+pid1StopUnit $testUnit
c2dfb7
+# Maks must be ignored
c2dfb7
+grep "set_mempolicy(MPOL_LOCAL, NULL" $straceLog
c2dfb7
+
c2dfb7
+echo "systemd-run NUMAPolicy support"
c2dfb7
+runUnit='numa-systemd-run-test.service'
c2dfb7
+
c2dfb7
+systemd-run -p NUMAPolicy=default --unit $runUnit sleep 1000
c2dfb7
+systemctlCheckNUMAProperties $runUnit "default"
c2dfb7
+pid1StopUnit $runUnit
c2dfb7
+
c2dfb7
+systemd-run -p NUMAPolicy=default -p NUMAMask=0 --unit $runUnit sleep 1000
c2dfb7
+systemctlCheckNUMAProperties $runUnit "default" ""
c2dfb7
+pid1StopUnit $runUnit
c2dfb7
+
c2dfb7
+systemd-run -p NUMAPolicy=bind -p NUMAMask=0 --unit $runUnit sleep 1000
c2dfb7
+systemctlCheckNUMAProperties $runUnit "bind" "0"
c2dfb7
+pid1StopUnit $runUnit
c2dfb7
+
c2dfb7
+systemd-run -p NUMAPolicy=interleave -p NUMAMask=0 --unit $runUnit sleep 1000
c2dfb7
+systemctlCheckNUMAProperties $runUnit "interleave" "0"
c2dfb7
+pid1StopUnit $runUnit
c2dfb7
+
c2dfb7
+systemd-run -p NUMAPolicy=preferred -p NUMAMask=0 --unit $runUnit sleep 1000
c2dfb7
+systemctlCheckNUMAProperties $runUnit "preferred" "0"
c2dfb7
+pid1StopUnit $runUnit
c2dfb7
+
c2dfb7
+systemd-run -p NUMAPolicy=local --unit $runUnit sleep 1000
c2dfb7
+systemctlCheckNUMAProperties $runUnit "local"
c2dfb7
+pid1StopUnit $runUnit
c2dfb7
+
c2dfb7
+systemd-run -p NUMAPolicy=local -p NUMAMask=0 --unit $runUnit sleep 1000
c2dfb7
+systemctlCheckNUMAProperties $runUnit "local" ""
c2dfb7
+pid1StopUnit $runUnit
c2dfb7
+
c2dfb7
+# Cleanup
c2dfb7
+rm -rf $testDir
c2dfb7
+rm -rf $confDir
c2dfb7
+systemctl daemon-reload
c2dfb7
+
c2dfb7
+systemd-analyze log-level info
c2dfb7
+
c2dfb7
+echo OK > /testok
c2dfb7
+
c2dfb7
+exit 0