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