teknoraver / rpms / systemd

Forked from rpms/systemd 3 months ago
Clone

Blame SOURCES/0427-test-add-test-for-cgroup-v2-freezer-support.patch

c2dfb7
From 762959047c1e1a5d4b6a6fe85cfa8a14c622e4da Mon Sep 17 00:00:00 2001
c2dfb7
From: =?UTF-8?q?Michal=20Sekleta=CC=81r?= <msekleta@redhat.com>
c2dfb7
Date: Fri, 3 Apr 2020 09:13:51 +0200
c2dfb7
Subject: [PATCH] test: add test for cgroup v2 freezer support
c2dfb7
c2dfb7
(cherry picked from commit d446ae89c0168f17eed7135ac06df3b294b3fcc6)
c2dfb7
c2dfb7
Related: #1830861
c2dfb7
---
c2dfb7
 ci/travis-centos-rhel8.sh         |   2 +-
c2dfb7
 test/TEST-38-FREEZER/Makefile     |   1 +
c2dfb7
 test/TEST-38-FREEZER/test.sh      |  53 ++++++
c2dfb7
 test/TEST-38-FREEZER/testsuite.sh | 293 ++++++++++++++++++++++++++++++
c2dfb7
 4 files changed, 348 insertions(+), 1 deletion(-)
c2dfb7
 create mode 120000 test/TEST-38-FREEZER/Makefile
c2dfb7
 create mode 100755 test/TEST-38-FREEZER/test.sh
c2dfb7
 create mode 100755 test/TEST-38-FREEZER/testsuite.sh
c2dfb7
c2dfb7
diff --git a/ci/travis-centos-rhel8.sh b/ci/travis-centos-rhel8.sh
c2dfb7
index a1502e15ee..cd0857fd29 100755
c2dfb7
--- a/ci/travis-centos-rhel8.sh
c2dfb7
+++ b/ci/travis-centos-rhel8.sh
c2dfb7
@@ -15,7 +15,7 @@ CONT_NAME="${CONT_NAME:-centos-$CENTOS_RELEASE-$RANDOM}"
c2dfb7
 DOCKER_EXEC="${DOCKER_EXEC:-docker exec -it $CONT_NAME}"
c2dfb7
 DOCKER_RUN="${DOCKER_RUN:-docker run}"
c2dfb7
 REPO_ROOT="${REPO_ROOT:-$PWD}"
c2dfb7
-ADDITIONAL_DEPS=(libasan libubsan net-tools strace nc e2fsprogs quota dnsmasq)
c2dfb7
+ADDITIONAL_DEPS=(libasan libubsan net-tools strace nc e2fsprogs quota dnsmasq diffutils)
c2dfb7
 # RHEL8 options
c2dfb7
 CONFIGURE_OPTS=(
c2dfb7
     -Dsysvinit-path=/etc/rc.d/init.d
c2dfb7
diff --git a/test/TEST-38-FREEZER/Makefile b/test/TEST-38-FREEZER/Makefile
c2dfb7
new file mode 120000
c2dfb7
index 0000000000..e9f93b1104
c2dfb7
--- /dev/null
c2dfb7
+++ b/test/TEST-38-FREEZER/Makefile
c2dfb7
@@ -0,0 +1 @@
c2dfb7
+../TEST-01-BASIC/Makefile
c2dfb7
\ No newline at end of file
c2dfb7
diff --git a/test/TEST-38-FREEZER/test.sh b/test/TEST-38-FREEZER/test.sh
c2dfb7
new file mode 100755
c2dfb7
index 0000000000..0d00754b5c
c2dfb7
--- /dev/null
c2dfb7
+++ b/test/TEST-38-FREEZER/test.sh
c2dfb7
@@ -0,0 +1,53 @@
c2dfb7
+#!/bin/bash
c2dfb7
+set -e
c2dfb7
+TEST_DESCRIPTION="test unit freezing and thawing via DBus and systemctl"
c2dfb7
+TEST_NO_NSPAWN=1
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
+        dracut_install tr cut timeout
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
+EOF
c2dfb7
+        cat > $initdir/etc/systemd/system/testsuite-38-sleep.service << EOF
c2dfb7
+[Service]
c2dfb7
+ExecStart=/bin/sleep 3600
c2dfb7
+EOF
c2dfb7
+
c2dfb7
+        cp testsuite.sh $initdir/
c2dfb7
+        setup_testsuite
c2dfb7
+    )
c2dfb7
+    setup_nspawn_root
c2dfb7
+
c2dfb7
+    ddebug "umount $TESTDIR/root"
c2dfb7
+    umount $TESTDIR/root
c2dfb7
+}
c2dfb7
+
c2dfb7
+
c2dfb7
+do_test "$@"
c2dfb7
diff --git a/test/TEST-38-FREEZER/testsuite.sh b/test/TEST-38-FREEZER/testsuite.sh
c2dfb7
new file mode 100755
c2dfb7
index 0000000000..6fcadb8f8e
c2dfb7
--- /dev/null
c2dfb7
+++ b/test/TEST-38-FREEZER/testsuite.sh
c2dfb7
@@ -0,0 +1,293 @@
c2dfb7
+#!/usr/bin/env bash
c2dfb7
+
c2dfb7
+set -ex
c2dfb7
+set -o pipefail
c2dfb7
+
c2dfb7
+systemd-analyze log-level debug
c2dfb7
+systemd-analyze log-target console
c2dfb7
+
c2dfb7
+unit=testsuite-38-sleep.service
c2dfb7
+
c2dfb7
+start_test_service() {
c2dfb7
+    systemctl daemon-reload
c2dfb7
+    systemctl start "${unit}"
c2dfb7
+}
c2dfb7
+
c2dfb7
+dbus_freeze() {
c2dfb7
+    local suffix=
c2dfb7
+    suffix="${1##*.}"
c2dfb7
+
c2dfb7
+    local name="$(echo ${1%.$suffix} | sed s/-/_2d/g)"
c2dfb7
+    local object_path="/org/freedesktop/systemd1/unit/${name}_2e${suffix}"
c2dfb7
+
c2dfb7
+    busctl call \
c2dfb7
+           org.freedesktop.systemd1 \
c2dfb7
+           "${object_path}" \
c2dfb7
+           org.freedesktop.systemd1.Unit \
c2dfb7
+           Freeze
c2dfb7
+}
c2dfb7
+
c2dfb7
+dbus_thaw() {
c2dfb7
+    local suffix=
c2dfb7
+    suffix="${1##*.}"
c2dfb7
+
c2dfb7
+    local name="$(echo ${1%.$suffix} | sed s/-/_2d/g)"
c2dfb7
+    local object_path="/org/freedesktop/systemd1/unit/${name}_2e${suffix}"
c2dfb7
+
c2dfb7
+    busctl call \
c2dfb7
+           org.freedesktop.systemd1 \
c2dfb7
+           "${object_path}" \
c2dfb7
+           org.freedesktop.systemd1.Unit \
c2dfb7
+           Thaw
c2dfb7
+}
c2dfb7
+
c2dfb7
+dbus_freeze_unit() {
c2dfb7
+    busctl call \
c2dfb7
+           org.freedesktop.systemd1 \
c2dfb7
+           /org/freedesktop/systemd1 \
c2dfb7
+           org.freedesktop.systemd1.Manager \
c2dfb7
+           FreezeUnit \
c2dfb7
+           s \
c2dfb7
+           "$1"
c2dfb7
+}
c2dfb7
+
c2dfb7
+dbus_thaw_unit() {
c2dfb7
+    busctl call \
c2dfb7
+           org.freedesktop.systemd1 \
c2dfb7
+           /org/freedesktop/systemd1 \
c2dfb7
+           org.freedesktop.systemd1.Manager \
c2dfb7
+           ThawUnit \
c2dfb7
+           s \
c2dfb7
+           "$1"
c2dfb7
+}
c2dfb7
+
c2dfb7
+dbus_can_freeze() {
c2dfb7
+    local suffix=
c2dfb7
+    suffix="${1##*.}"
c2dfb7
+
c2dfb7
+    local name="$(echo ${1%.$suffix} | sed s/-/_2d/g)"
c2dfb7
+    local object_path="/org/freedesktop/systemd1/unit/${name}_2e${suffix}"
c2dfb7
+
c2dfb7
+    busctl get-property \
c2dfb7
+           org.freedesktop.systemd1 \
c2dfb7
+           "${object_path}" \
c2dfb7
+           org.freedesktop.systemd1.Unit \
c2dfb7
+           CanFreeze
c2dfb7
+}
c2dfb7
+
c2dfb7
+check_freezer_state() {
c2dfb7
+    local suffix=
c2dfb7
+    suffix="${1##*.}"
c2dfb7
+
c2dfb7
+    local name="$(echo ${1%.$suffix} | sed s/-/_2d/g)"
c2dfb7
+    local object_path="/org/freedesktop/systemd1/unit/${name}_2e${suffix}"
c2dfb7
+
c2dfb7
+    state=$(busctl get-property \
c2dfb7
+                   org.freedesktop.systemd1 \
c2dfb7
+                   "${object_path}" \
c2dfb7
+                   org.freedesktop.systemd1.Unit \
c2dfb7
+                   FreezerState | cut -d " " -f2 | tr -d '"')
c2dfb7
+
c2dfb7
+    [ "$state" = "$2" ] || {
c2dfb7
+        echo "error: unexpected freezer state, expected: $2, actual: $state" >&2
c2dfb7
+        exit 1
c2dfb7
+    }
c2dfb7
+}
c2dfb7
+
c2dfb7
+check_cgroup_state() {
c2dfb7
+    grep -q "frozen $2" /sys/fs/cgroup/system.slice/"$1"/cgroup.events
c2dfb7
+}
c2dfb7
+
c2dfb7
+test_dbus_api() {
c2dfb7
+    echo "Test that DBus API works:"
c2dfb7
+    echo -n "  - Freeze(): "
c2dfb7
+    dbus_freeze "${unit}"
c2dfb7
+    check_freezer_state "${unit}" "frozen"
c2dfb7
+    check_cgroup_state "$unit" 1
c2dfb7
+    echo "[ OK ]"
c2dfb7
+
c2dfb7
+    echo -n "  - Thaw(): "
c2dfb7
+    dbus_thaw "${unit}"
c2dfb7
+    check_freezer_state "${unit}" "running"
c2dfb7
+    check_cgroup_state "$unit" 0
c2dfb7
+    echo "[ OK ]"
c2dfb7
+
c2dfb7
+    echo -n "  - FreezeUnit(): "
c2dfb7
+    dbus_freeze_unit "${unit}"
c2dfb7
+    check_freezer_state "${unit}" "frozen"
c2dfb7
+    check_cgroup_state "$unit" 1
c2dfb7
+    echo "[ OK ]"
c2dfb7
+
c2dfb7
+    echo -n "  - ThawUnit(): "
c2dfb7
+    dbus_thaw_unit "${unit}"
c2dfb7
+    check_freezer_state "${unit}" "running"
c2dfb7
+    check_cgroup_state "$unit" 0
c2dfb7
+    echo "[ OK ]"
c2dfb7
+
c2dfb7
+    echo -n "  - CanFreeze(): "
c2dfb7
+    output=$(dbus_can_freeze "${unit}")
c2dfb7
+    [ "$output" = "b true" ]
c2dfb7
+    echo "[ OK ]"
c2dfb7
+
c2dfb7
+    echo
c2dfb7
+}
c2dfb7
+
c2dfb7
+test_jobs() {
c2dfb7
+    local pid_before=
c2dfb7
+    local pid_after=
c2dfb7
+    echo "Test that it is possible to apply jobs on frozen units:"
c2dfb7
+
c2dfb7
+    systemctl start "${unit}"
c2dfb7
+    dbus_freeze "${unit}"
c2dfb7
+    check_freezer_state "${unit}" "frozen"
c2dfb7
+
c2dfb7
+    echo -n "  - restart: "
c2dfb7
+    pid_before=$(systemctl show -p MainPID "${unit}" --value)
c2dfb7
+    systemctl restart "${unit}"
c2dfb7
+    pid_after=$(systemctl show -p MainPID "${unit}" --value)
c2dfb7
+    [ "$pid_before" != "$pid_after" ] && echo "[ OK ]"
c2dfb7
+
c2dfb7
+    dbus_freeze "${unit}"
c2dfb7
+    check_freezer_state "${unit}" "frozen"
c2dfb7
+
c2dfb7
+    echo -n "  - stop: "
c2dfb7
+    timeout 5s systemctl stop "${unit}"
c2dfb7
+    echo "[ OK ]"
c2dfb7
+
c2dfb7
+    echo
c2dfb7
+}
c2dfb7
+
c2dfb7
+test_systemctl() {
c2dfb7
+    echo "Test that systemctl freeze/thaw verbs:"
c2dfb7
+
c2dfb7
+    systemctl start "$unit"
c2dfb7
+
c2dfb7
+    echo -n "  - freeze: "
c2dfb7
+    systemctl freeze "$unit"
c2dfb7
+    check_freezer_state "${unit}" "frozen"
c2dfb7
+    check_cgroup_state "$unit" 1
c2dfb7
+    # Freezing already frozen unit should be NOP and return quickly
c2dfb7
+    timeout 3s systemctl freeze "$unit"
c2dfb7
+    echo "[ OK ]"
c2dfb7
+
c2dfb7
+    echo -n "  - thaw: "
c2dfb7
+    systemctl thaw "$unit"
c2dfb7
+    check_freezer_state "${unit}" "running"
c2dfb7
+    check_cgroup_state "$unit" 0
c2dfb7
+    # Likewise thawing already running unit shouldn't block
c2dfb7
+    timeout 3s systemctl thaw "$unit"
c2dfb7
+    echo "[ OK ]"
c2dfb7
+
c2dfb7
+    systemctl stop "$unit"
c2dfb7
+
c2dfb7
+    echo
c2dfb7
+}
c2dfb7
+
c2dfb7
+test_systemctl_show() {
c2dfb7
+    echo "Test systemctl show integration:"
c2dfb7
+
c2dfb7
+    systemctl start "$unit"
c2dfb7
+
c2dfb7
+    echo -n "  - FreezerState property: "
c2dfb7
+    state=$(systemctl show -p FreezerState --value "$unit")
c2dfb7
+    [ "$state" = "running" ]
c2dfb7
+    systemctl freeze "$unit"
c2dfb7
+    state=$(systemctl show -p FreezerState --value "$unit")
c2dfb7
+    [ "$state" = "frozen" ]
c2dfb7
+    systemctl thaw "$unit"
c2dfb7
+    echo "[ OK ]"
c2dfb7
+
c2dfb7
+    echo -n "  - CanFreeze property: "
c2dfb7
+    state=$(systemctl show -p CanFreeze --value "$unit")
c2dfb7
+    [ "$state" = "yes" ]
c2dfb7
+    echo "[ OK ]"
c2dfb7
+
c2dfb7
+    systemctl stop "$unit"
c2dfb7
+    echo
c2dfb7
+}
c2dfb7
+
c2dfb7
+test_recursive() {
c2dfb7
+    local slice="bar.slice"
c2dfb7
+    local unit="baz.service"
c2dfb7
+
c2dfb7
+    systemd-run --unit "$unit" --slice "$slice" sleep 3600 >/dev/null 2>&1
c2dfb7
+
c2dfb7
+    echo "Test recursive freezing:"
c2dfb7
+
c2dfb7
+    echo -n "  - freeze: "
c2dfb7
+    systemctl freeze "$slice"
c2dfb7
+    check_freezer_state "${slice}" "frozen"
c2dfb7
+    check_freezer_state "${unit}" "frozen"
c2dfb7
+    grep -q "frozen 1" /sys/fs/cgroup/"${slice}"/cgroup.events
c2dfb7
+    grep -q "frozen 1" /sys/fs/cgroup/"${slice}"/"${unit}"/cgroup.events
c2dfb7
+    echo "[ OK ]"
c2dfb7
+
c2dfb7
+    echo -n "  - thaw: "
c2dfb7
+    systemctl thaw "$slice"
c2dfb7
+    check_freezer_state "${unit}" "running"
c2dfb7
+    check_freezer_state "${slice}" "running"
c2dfb7
+    grep -q "frozen 0" /sys/fs/cgroup/"${slice}"/cgroup.events
c2dfb7
+    grep -q "frozen 0" /sys/fs/cgroup/"${slice}"/"${unit}"/cgroup.events
c2dfb7
+    echo "[ OK ]"
c2dfb7
+
c2dfb7
+    systemctl stop "$unit"
c2dfb7
+    systemctl stop "$slice"
c2dfb7
+
c2dfb7
+    echo
c2dfb7
+}
c2dfb7
+
c2dfb7
+test_preserve_state() {
c2dfb7
+    local slice="bar.slice"
c2dfb7
+    local unit="baz.service"
c2dfb7
+
c2dfb7
+    systemd-run --unit "$unit" --slice "$slice" sleep 3600 >/dev/null 2>&1
c2dfb7
+
c2dfb7
+    echo "Test that freezer state is preserved when recursive freezing is initiated from outside (e.g. by manager up the tree):"
c2dfb7
+
c2dfb7
+    echo -n "  - freeze from outside: "
c2dfb7
+    echo 1 > /sys/fs/cgroup/"${slice}"/cgroup.freeze
c2dfb7
+
c2dfb7
+    # Our state should not be affected
c2dfb7
+    check_freezer_state "${slice}" "running"
c2dfb7
+    check_freezer_state "${unit}" "running"
c2dfb7
+
c2dfb7
+    # However actual kernel state should be frozen
c2dfb7
+    grep -q "frozen 1" /sys/fs/cgroup/"${slice}"/cgroup.events
c2dfb7
+    grep -q "frozen 1" /sys/fs/cgroup/"${slice}"/"${unit}"/cgroup.events
c2dfb7
+    echo "[ OK ]"
c2dfb7
+
c2dfb7
+    echo -n "  - thaw from outside: "
c2dfb7
+    echo 0 > /sys/fs/cgroup/"${slice}"/cgroup.freeze
c2dfb7
+    check_freezer_state "${unit}" "running"
c2dfb7
+    check_freezer_state "${slice}" "running"
c2dfb7
+    grep -q "frozen 0" /sys/fs/cgroup/"${slice}"/cgroup.events
c2dfb7
+    grep -q "frozen 0" /sys/fs/cgroup/"${slice}"/"${unit}"/cgroup.events
c2dfb7
+    echo "[ OK ]"
c2dfb7
+
c2dfb7
+    echo -n "  - thaw from outside while inner service is frozen: "
c2dfb7
+    systemctl freeze "$unit"
c2dfb7
+    check_freezer_state "${unit}" "frozen"
c2dfb7
+    echo 1 > /sys/fs/cgroup/"${slice}"/cgroup.freeze
c2dfb7
+    echo 0 > /sys/fs/cgroup/"${slice}"/cgroup.freeze
c2dfb7
+    check_freezer_state "${slice}" "running"
c2dfb7
+    check_freezer_state "${unit}" "frozen"
c2dfb7
+    echo "[ OK ]"
c2dfb7
+
c2dfb7
+    systemctl stop "$unit"
c2dfb7
+    systemctl stop "$slice"
c2dfb7
+
c2dfb7
+    echo
c2dfb7
+}
c2dfb7
+
c2dfb7
+test -e /sys/fs/cgroup/system.slice/cgroup.freeze && {
c2dfb7
+    start_test_service
c2dfb7
+    test_dbus_api
c2dfb7
+    test_systemctl
c2dfb7
+    test_systemctl_show
c2dfb7
+    test_jobs
c2dfb7
+    test_recursive
c2dfb7
+    test_preserve_state
c2dfb7
+}
c2dfb7
+
c2dfb7
+echo OK > /testok
c2dfb7
+exit 0