From 90dda340e4adeb1126639a849d4f31ae327fdc4b Mon Sep 17 00:00:00 2001
From: Frantisek Sumsal <frantisek@sumsal.cz>
Date: Tue, 25 Jun 2019 23:01:40 +0200
Subject: [PATCH] test: introduce TEST-36-NUMAPOLICY

(cherry picked from commit 8f65e26508969610ac934d1aadbade8223bfcaac)

Related: #1808940
---
 test/TEST-36-NUMAPOLICY/Makefile     |   1 +
 test/TEST-36-NUMAPOLICY/test.sh      |  51 +++++
 test/TEST-36-NUMAPOLICY/testsuite.sh | 292 +++++++++++++++++++++++++++
 3 files changed, 344 insertions(+)
 create mode 120000 test/TEST-36-NUMAPOLICY/Makefile
 create mode 100755 test/TEST-36-NUMAPOLICY/test.sh
 create mode 100755 test/TEST-36-NUMAPOLICY/testsuite.sh

diff --git a/test/TEST-36-NUMAPOLICY/Makefile b/test/TEST-36-NUMAPOLICY/Makefile
new file mode 120000
index 0000000000..e9f93b1104
--- /dev/null
+++ b/test/TEST-36-NUMAPOLICY/Makefile
@@ -0,0 +1 @@
+../TEST-01-BASIC/Makefile
\ No newline at end of file
diff --git a/test/TEST-36-NUMAPOLICY/test.sh b/test/TEST-36-NUMAPOLICY/test.sh
new file mode 100755
index 0000000000..a0d8623e8e
--- /dev/null
+++ b/test/TEST-36-NUMAPOLICY/test.sh
@@ -0,0 +1,51 @@
+#!/bin/bash
+set -e
+TEST_DESCRIPTION="test MUMAPolicy= and NUMAMask= options"
+TEST_NO_NSPAWN=1
+QEMU_OPTIONS="-numa node,nodeid=0"
+
+. $TEST_BASE_DIR/test-functions
+
+test_setup() {
+    create_empty_image
+    mkdir -p $TESTDIR/root
+    mount ${LOOPDEV}p1 $TESTDIR/root
+
+    (
+        LOG_LEVEL=5
+        eval $(udevadm info --export --query=env --name=${LOOPDEV}p2)
+
+        setup_basic_environment
+        inst_binary mktemp
+
+        # mask some services that we do not want to run in these tests
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-hwdb-update.service
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-journal-catalog-update.service
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-networkd.service
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-networkd.socket
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-resolved.service
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-machined.service
+
+        # setup the testsuite service
+        cat >$initdir/etc/systemd/system/testsuite.service <<EOF
+[Unit]
+Description=Testsuite service
+
+[Service]
+ExecStart=/bin/bash -x /testsuite.sh
+Type=oneshot
+StandardOutput=tty
+StandardError=tty
+NotifyAccess=all
+EOF
+        cp testsuite.sh $initdir/
+
+        setup_testsuite
+    ) || return 1
+    setup_nspawn_root
+
+    ddebug "umount $TESTDIR/root"
+    umount $TESTDIR/root
+}
+
+do_test "$@"
diff --git a/test/TEST-36-NUMAPOLICY/testsuite.sh b/test/TEST-36-NUMAPOLICY/testsuite.sh
new file mode 100755
index 0000000000..e15087b137
--- /dev/null
+++ b/test/TEST-36-NUMAPOLICY/testsuite.sh
@@ -0,0 +1,292 @@
+#!/bin/bash
+set -ex
+set -o pipefail
+
+at_exit() {
+    if [ $? -ne 0 ]; then
+        # We're exiting with a non-zero EC, let's dump test artifacts
+        # for easier debugging
+        [ -f "$straceLog" ] && cat "$straceLog"
+        [ -f "$journalLog" ] && cat "$journalLog"
+    fi
+}
+
+trap at_exit EXIT
+
+systemd-analyze log-level debug
+systemd-analyze log-target journal
+
+# Log files
+straceLog='strace.log'
+journalLog='journal.log'
+
+# Systemd config files
+testUnit='numa-test.service'
+testUnitFile="/etc/systemd/system/$testUnit"
+testUnitNUMAConf="$testUnitFile.d/numa.conf"
+
+# Sleep constants (we should probably figure out something better but nothing comes to mind)
+journalSleep=5
+sleepAfterStart=1
+
+startStrace() {
+    coproc strace -qq -p 1 -o $straceLog -e set_mempolicy -s 1024 $1
+}
+
+stopStrace() {
+    kill -s TERM $COPROC_PID
+}
+
+startJournalctl() {
+    coproc journalctl -u init.scope -f > $journalLog
+}
+
+stopJournalctl() {
+    # Wait a few seconds until the messages get properly queued...
+    sleep $journalSleep
+    # ...and then force journald to write them to the backing storage
+    # Also, 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
+    kill -s TERM $COPROC_PID
+}
+
+checkNUMA() {
+    # NUMA enabled system should have at least NUMA node0
+    test -e /sys/devices/system/node/node0
+}
+
+writePID1NUMAPolicy() {
+    echo [Manager] > $confDir/numa.conf
+    echo NUMAPolicy=$1 >> $confDir/numa.conf
+    echo NUMAMask=$2>> $confDir/numa.conf
+}
+
+writeTestUnit() {
+    echo [Service] > $testUnitFile
+    echo ExecStart=/bin/sleep 3600 >> $testUnitFile
+    mkdir -p $testUnitFile.d/
+}
+
+writeTestUnitNUMAPolicy() {
+    echo [Service] > $testUnitNUMAConf
+    echo NUMAPolicy=$1 >> $testUnitNUMAConf
+    echo NUMAMask=$2>> $testUnitNUMAConf
+    systemctl daemon-reload
+}
+
+pid1ReloadWithStrace() {
+    startStrace
+    systemctl daemon-reload
+    stopStrace
+}
+
+pid1ReloadWithJournal() {
+    startJournalctl
+    systemctl daemon-reload
+    stopJournalctl
+}
+
+pid1StartUnitWithStrace() {
+    startStrace '-f'
+    systemctl start $1
+    sleep $sleepAfterStart
+    stopStrace
+}
+
+pid1StartUnitWithJournal() {
+    startJournalctl
+    systemctl start $1
+    sleep $sleepAfterStart
+    stopJournalctl
+}
+
+pid1StopUnit() {
+    systemctl stop $1
+}
+
+systemctlCheckNUMAProperties() {
+    local LOGFILE="$(mktemp)"
+    systemctl show -p NUMAPolicy $1 > "$LOGFILE"
+    grep "NUMAPolicy=$2" "$LOGFILE"
+
+    > "$LOGFILE"
+
+    if [ -n $3 ]; then
+        systemctl show -p NUMAMask $1 > "$LOGFILE"
+        grep "NUMAMask=$3" "$LOGFILE"
+    fi
+}
+
+checkNUMA
+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
+grep "set_mempolicy(MPOL_LOCAL, NULL" $straceLog
+
+echo "PID1 NUMAPolicy support - Local policy w/ mask"
+writePID1NUMAPolicy "local" "0"
+pid1ReloadWithStrace
+grep "set_mempolicy(MPOL_LOCAL, 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 "set_mempolicy(MPOL_LOCAL, 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 "set_mempolicy(MPOL_LOCAL, 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
+
+# Cleanup
+rm -rf $testDir
+rm -rf $confDir
+systemctl daemon-reload
+
+systemd-analyze log-level info
+
+echo OK > /testok
+
+exit 0
