dryang / rpms / systemd

Forked from rpms/systemd a year ago
Clone
7e7c9f
From 94da4f86eab658c14feac290663634ca29348a60 Mon Sep 17 00:00:00 2001
7e7c9f
From: David Tardon <dtardon@redhat.com>
7e7c9f
Date: Thu, 3 Oct 2019 19:05:06 +0200
7e7c9f
Subject: [PATCH] add test for ExecStopPost
7e7c9f
7e7c9f
This is a follow-up to #4843.
7e7c9f
7e7c9f
(cherry picked from commit 02baf239d87295362740d961765091b778795573)
7e7c9f
7e7c9f
Related: #1733998
7e7c9f
---
7e7c9f
 test/TEST-42-EXECSTOPPOST/Makefile     |  10 +++
7e7c9f
 test/TEST-42-EXECSTOPPOST/test.sh      |  95 ++++++++++++++++++++
7e7c9f
 test/TEST-42-EXECSTOPPOST/testsuite.sh | 119 +++++++++++++++++++++++++
7e7c9f
 3 files changed, 224 insertions(+)
7e7c9f
 create mode 100644 test/TEST-42-EXECSTOPPOST/Makefile
7e7c9f
 create mode 100755 test/TEST-42-EXECSTOPPOST/test.sh
7e7c9f
 create mode 100755 test/TEST-42-EXECSTOPPOST/testsuite.sh
7e7c9f
7e7c9f
diff --git a/test/TEST-42-EXECSTOPPOST/Makefile b/test/TEST-42-EXECSTOPPOST/Makefile
7e7c9f
new file mode 100644
7e7c9f
index 0000000000..5e89a29eff
7e7c9f
--- /dev/null
7e7c9f
+++ b/test/TEST-42-EXECSTOPPOST/Makefile
7e7c9f
@@ -0,0 +1,10 @@
7e7c9f
+all:
7e7c9f
+	@make -s --no-print-directory -C ../.. all
7e7c9f
+	@basedir=../.. TEST_BASE_DIR=../ ./test.sh --all
7e7c9f
+setup:
7e7c9f
+	@make --no-print-directory -C ../.. all
7e7c9f
+	@basedir=../.. TEST_BASE_DIR=../ ./test.sh --setup
7e7c9f
+clean:
7e7c9f
+	@basedir=../.. TEST_BASE_DIR=../ ./test.sh --clean
7e7c9f
+run:
7e7c9f
+	@basedir=../.. TEST_BASE_DIR=../ ./test.sh --run
7e7c9f
diff --git a/test/TEST-42-EXECSTOPPOST/test.sh b/test/TEST-42-EXECSTOPPOST/test.sh
7e7c9f
new file mode 100755
7e7c9f
index 0000000000..0958e01d68
7e7c9f
--- /dev/null
7e7c9f
+++ b/test/TEST-42-EXECSTOPPOST/test.sh
7e7c9f
@@ -0,0 +1,95 @@
7e7c9f
+#!/bin/bash
7e7c9f
+TEST_DESCRIPTION="test that ExecStopPost= is always run"
7e7c9f
+
7e7c9f
+. $TEST_BASE_DIR/test-functions
7e7c9f
+
7e7c9f
+check_result_qemu() {
7e7c9f
+    ret=1
7e7c9f
+    mkdir -p $TESTDIR/root
7e7c9f
+    mount ${LOOPDEV}p1 $TESTDIR/root
7e7c9f
+    [[ -e $TESTDIR/root/testok ]] && ret=0
7e7c9f
+    [[ -f $TESTDIR/root/failed ]] && cp -a $TESTDIR/root/failed $TESTDIR
7e7c9f
+    [[ -f $TESTDIR/root/var/log/journal ]] && cp -a $TESTDIR/root/var/log/journal $TESTDIR
7e7c9f
+    umount $TESTDIR/root
7e7c9f
+    [[ -f $TESTDIR/failed ]] && cat $TESTDIR/failed
7e7c9f
+    ls -l $TESTDIR/journal/*/*.journal
7e7c9f
+    test -s $TESTDIR/failed && ret=$(($ret+1))
7e7c9f
+    return $ret
7e7c9f
+}
7e7c9f
+
7e7c9f
+test_run() {
7e7c9f
+    if run_qemu; then
7e7c9f
+        check_result_qemu || return 1
7e7c9f
+    else
7e7c9f
+        dwarn "can't run QEMU, skipping"
7e7c9f
+    fi
7e7c9f
+    if check_nspawn; then
7e7c9f
+        run_nspawn
7e7c9f
+        check_result_nspawn || return 1
7e7c9f
+    else
7e7c9f
+        dwarn "can't run systemd-nspawn, skipping"
7e7c9f
+    fi
7e7c9f
+    return 0
7e7c9f
+}
7e7c9f
+
7e7c9f
+test_setup() {
7e7c9f
+    create_empty_image
7e7c9f
+    mkdir -p $TESTDIR/root
7e7c9f
+    mount ${LOOPDEV}p1 $TESTDIR/root
7e7c9f
+
7e7c9f
+    (
7e7c9f
+        LOG_LEVEL=5
7e7c9f
+        eval $(udevadm info --export --query=env --name=${LOOPDEV}p2)
7e7c9f
+
7e7c9f
+        setup_basic_environment
7e7c9f
+        dracut_install false true env touch
7e7c9f
+
7e7c9f
+        # mask some services that we do not want to run in these tests
7e7c9f
+        ln -s /dev/null $initdir/etc/systemd/system/systemd-hwdb-update.service
7e7c9f
+        ln -s /dev/null $initdir/etc/systemd/system/systemd-journal-catalog-update.service
7e7c9f
+        ln -s /dev/null $initdir/etc/systemd/system/systemd-networkd.service
7e7c9f
+        ln -s /dev/null $initdir/etc/systemd/system/systemd-networkd.socket
7e7c9f
+        ln -s /dev/null $initdir/etc/systemd/system/systemd-resolved.service
7e7c9f
+
7e7c9f
+        # setup policy for Type=dbus test
7e7c9f
+        mkdir -p $initdir/etc/dbus-1/system.d
7e7c9f
+        cat > $initdir/etc/dbus-1/system.d/systemd.test.ExecStopPost.conf <
7e7c9f
+
7e7c9f
+
7e7c9f
+        "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
7e7c9f
+<busconfig>
7e7c9f
+    <policy user="root">
7e7c9f
+        <allow own="systemd.test.ExecStopPost"/>
7e7c9f
+    </policy>
7e7c9f
+</busconfig>
7e7c9f
+EOF
7e7c9f
+
7e7c9f
+        # setup the testsuite service
7e7c9f
+        cat >$initdir/etc/systemd/system/testsuite.service <
7e7c9f
+[Unit]
7e7c9f
+Description=Testsuite service
7e7c9f
+
7e7c9f
+[Service]
7e7c9f
+ExecStart=/bin/bash -x /testsuite.sh
7e7c9f
+Type=oneshot
7e7c9f
+StandardOutput=tty
7e7c9f
+StandardError=tty
7e7c9f
+NotifyAccess=all
7e7c9f
+EOF
7e7c9f
+        cp testsuite.sh $initdir/
7e7c9f
+
7e7c9f
+        setup_testsuite
7e7c9f
+    )
7e7c9f
+    setup_nspawn_root
7e7c9f
+
7e7c9f
+    ddebug "umount $TESTDIR/root"
7e7c9f
+    umount $TESTDIR/root
7e7c9f
+}
7e7c9f
+
7e7c9f
+test_cleanup() {
7e7c9f
+    umount $TESTDIR/root 2>/dev/null
7e7c9f
+    [[ $LOOPDEV ]] && losetup -d $LOOPDEV
7e7c9f
+    return 0
7e7c9f
+}
7e7c9f
+
7e7c9f
+do_test "$@"
7e7c9f
diff --git a/test/TEST-42-EXECSTOPPOST/testsuite.sh b/test/TEST-42-EXECSTOPPOST/testsuite.sh
7e7c9f
new file mode 100755
7e7c9f
index 0000000000..a7e663adb3
7e7c9f
--- /dev/null
7e7c9f
+++ b/test/TEST-42-EXECSTOPPOST/testsuite.sh
7e7c9f
@@ -0,0 +1,119 @@
7e7c9f
+#!/bin/bash
7e7c9f
+set -ex
7e7c9f
+
7e7c9f
+systemd-analyze set-log-level debug
7e7c9f
+
7e7c9f
+# Emulates systemd-run to overcome its limitations on RHEL-7, like nonexistent
7e7c9f
+# support for --wait and only a handful of supported properties.
7e7c9f
+systemd-run-wait() {
7e7c9f
+    local unit="$1"
7e7c9f
+    local unitfile=/etc/systemd/system/$unit
7e7c9f
+
7e7c9f
+    shift
7e7c9f
+
7e7c9f
+    cat > $unitfile <
7e7c9f
+[Service]
7e7c9f
+StandardOutput=tty
7e7c9f
+StandardError=tty
7e7c9f
+EOF
7e7c9f
+
7e7c9f
+    # The $OPTIND variable used by geopts is NOT reset on function exit in the
7e7c9f
+    # same shell session, so let's do it manually to always get relevant results
7e7c9f
+    OPTIND=1
7e7c9f
+
7e7c9f
+    while getopts p: opt ; do
7e7c9f
+        case $opt in
7e7c9f
+            p) echo "$OPTARG" >> $unitfile ;;
7e7c9f
+        esac
7e7c9f
+    done
7e7c9f
+    shift $((OPTIND - 1))
7e7c9f
+    echo "ExecStart=/usr/bin/env $@" >> $unitfile
7e7c9f
+    systemctl daemon-reload
7e7c9f
+
7e7c9f
+    systemctl start $unit
7e7c9f
+    while systemctl is-active -q $unit ; do
7e7c9f
+        sleep 1
7e7c9f
+    done
7e7c9f
+    ! systemctl is-failed -q $unit
7e7c9f
+}
7e7c9f
+
7e7c9f
+systemd-run-wait simple1.service -p Type=simple -p ExecStopPost='/bin/touch /run/simple1' true
7e7c9f
+test -f /run/simple1
7e7c9f
+
7e7c9f
+! systemd-run-wait simple2.service -p Type=simple -p ExecStopPost='/bin/touch /run/simple2' false
7e7c9f
+test -f /run/simple2
7e7c9f
+
7e7c9f
+cat > /tmp/forking1.sh <
7e7c9f
+#!/bin/bash
7e7c9f
+
7e7c9f
+set -eux
7e7c9f
+
7e7c9f
+sleep 4 &
7e7c9f
+MAINPID=\$!
7e7c9f
+disown
7e7c9f
+
7e7c9f
+systemd-notify MAINPID=\$MAINPID
7e7c9f
+EOF
7e7c9f
+chmod +x /tmp/forking1.sh
7e7c9f
+
7e7c9f
+# RHEL 7 doesn't support NotifyAccess=exec
7e7c9f
+systemd-run-wait forking1.service -p Type=forking -p NotifyAccess=main -p ExecStopPost='/bin/touch /run/forking1' /tmp/forking1.sh
7e7c9f
+test -f /run/forking1
7e7c9f
+
7e7c9f
+cat > /tmp/forking2.sh <
7e7c9f
+#!/bin/bash
7e7c9f
+
7e7c9f
+set -eux
7e7c9f
+
7e7c9f
+( sleep 4; exit 1 ) &
7e7c9f
+MAINPID=\$!
7e7c9f
+disown
7e7c9f
+
7e7c9f
+systemd-notify MAINPID=\$MAINPID
7e7c9f
+EOF
7e7c9f
+chmod +x /tmp/forking2.sh
7e7c9f
+
7e7c9f
+# RHEL 7 doesn't support NotifyAccess=exec
7e7c9f
+! systemd-run-wait forking2.service -p Type=forking -p NotifyAccess=main -p ExecStopPost='/bin/touch /run/forking2' /tmp/forking2.sh
7e7c9f
+test -f /run/forking2
7e7c9f
+
7e7c9f
+systemd-run-wait oneshot1.service -p Type=oneshot -p ExecStopPost='/bin/touch /run/oneshot1' true
7e7c9f
+test -f /run/oneshot1
7e7c9f
+
7e7c9f
+! systemd-run-wait oneshot2.service -p Type=oneshot -p ExecStopPost='/bin/touch /run/oneshot2' false
7e7c9f
+test -f /run/oneshot2
7e7c9f
+
7e7c9f
+systemd-run-wait dbus1.service -p Type=dbus -p BusName=systemd.test.ExecStopPost -p ExecStopPost='/bin/touch /run/dbus1' \
7e7c9f
+    busctl call org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus RequestName su systemd.test.ExecStopPost 4 \
7e7c9f
+    || :
7e7c9f
+test -f /run/dbus1
7e7c9f
+
7e7c9f
+! systemd-run-wait dbus2.service -p Type=dbus -p BusName=systemd.test.ExecStopPost -p ExecStopPost='/bin/touch /run/dbus2' true
7e7c9f
+test -f /run/dbus2
7e7c9f
+
7e7c9f
+cat > /tmp/notify1.sh <
7e7c9f
+#!/bin/bash
7e7c9f
+
7e7c9f
+set -eux
7e7c9f
+
7e7c9f
+systemd-notify --ready
7e7c9f
+EOF
7e7c9f
+chmod +x /tmp/notify1.sh
7e7c9f
+
7e7c9f
+systemd-run-wait notify1.service -p Type=notify -p ExecStopPost='/bin/touch /run/notify1' /tmp/notify1.sh
7e7c9f
+test -f /run/notify1
7e7c9f
+
7e7c9f
+! systemd-run-wait notify2.service -p Type=notify -p ExecStopPost='/bin/touch /run/notify2' true
7e7c9f
+test -f /run/notify2
7e7c9f
+
7e7c9f
+systemd-run-wait idle1.service -p Type=idle -p ExecStopPost='/bin/touch /run/idle1' true
7e7c9f
+test -f /run/idle1
7e7c9f
+
7e7c9f
+! systemd-run-wait idle2.service -p Type=idle -p ExecStopPost='/bin/touch /run/idle2' false
7e7c9f
+test -f /run/idle2
7e7c9f
+
7e7c9f
+systemd-analyze log-level info
7e7c9f
+
7e7c9f
+echo OK > /testok
7e7c9f
+
7e7c9f
+exit 0