ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
ac3a84
From 680d2b33d3b2a0bed17c2c1594690155bdb910bb Mon Sep 17 00:00:00 2001
ac3a84
From: Frantisek Sumsal <frantisek@sumsal.cz>
ac3a84
Date: Tue, 1 Nov 2022 20:47:37 +0100
ac3a84
Subject: [PATCH] test: add a couple of sanity tests for systemctl
ac3a84
ac3a84
(cherry picked from commit d16684fe13e1d56e55df19b57b6c01b9a9303086)
ac3a84
ac3a84
Related #2138081
ac3a84
---
ac3a84
 test/units/testsuite-26.sh | 209 +++++++++++++++++++++++++++++++++++--
ac3a84
 1 file changed, 202 insertions(+), 7 deletions(-)
ac3a84
ac3a84
diff --git a/test/units/testsuite-26.sh b/test/units/testsuite-26.sh
ac3a84
index ad08415317..b83f85917b 100755
ac3a84
--- a/test/units/testsuite-26.sh
ac3a84
+++ b/test/units/testsuite-26.sh
ac3a84
@@ -3,32 +3,227 @@
ac3a84
 set -eux
ac3a84
 set -o pipefail
ac3a84
 
ac3a84
+at_exit() {
ac3a84
+    if [[ -v UNIT_NAME && -e "/usr/lib/systemd/system/$UNIT_NAME" ]]; then
ac3a84
+        rm -fv "/usr/lib/systemd/system/$UNIT_NAME"
ac3a84
+    fi
ac3a84
+}
ac3a84
+
ac3a84
+trap at_exit EXIT
ac3a84
+
ac3a84
+# Create a simple unit file for testing
ac3a84
+# Note: the service file is created under /usr on purpose to test
ac3a84
+#       the 'revert' verb as well
ac3a84
+UNIT_NAME="systemctl-test-$RANDOM.service"
ac3a84
+cat >"/usr/lib/systemd/system/$UNIT_NAME" <<\EOF
ac3a84
+[Unit]
ac3a84
+Description=systemctl test
ac3a84
+
ac3a84
+[Service]
ac3a84
+ExecStart=sleep infinity
ac3a84
+ExecReload=true
ac3a84
+
ac3a84
+# For systemctl clean
ac3a84
+CacheDirectory=%n
ac3a84
+ConfigurationDirectory=%n
ac3a84
+LogsDirectory=%n
ac3a84
+RuntimeDirectory=%n
ac3a84
+StateDirectory=%n
ac3a84
+
ac3a84
+[Install]
ac3a84
+WantedBy=multi-user.target
ac3a84
+EOF
ac3a84
+
ac3a84
+# Configure the preset setting for the unit file
ac3a84
+mkdir /run/systemd/system-preset/
ac3a84
+echo "disable $UNIT_NAME" >/run/systemd/system-preset/99-systemd-test.preset
ac3a84
+
ac3a84
+systemctl daemon-reload
ac3a84
+
ac3a84
+# Argument help
ac3a84
+systemctl --state help
ac3a84
+systemctl --signal help
ac3a84
+systemctl --type help
ac3a84
+
ac3a84
+# list-dependencies
ac3a84
+systemctl list-dependencies systemd-journald
ac3a84
+systemctl list-dependencies --after systemd-journald
ac3a84
+systemctl list-dependencies --before systemd-journald
ac3a84
+systemctl list-dependencies --after --reverse systemd-journald
ac3a84
+systemctl list-dependencies --before --reverse systemd-journald
ac3a84
+systemctl list-dependencies --plain systemd-journald
ac3a84
+
ac3a84
+# list-* verbs
ac3a84
+systemctl list-units
ac3a84
+systemctl list-units --recursive
ac3a84
+systemctl list-units --type=socket
ac3a84
+systemctl list-units --type=service,timer
ac3a84
+systemctl list-units --legend=yes -a "systemd-*"
ac3a84
+systemctl list-units --state=active
ac3a84
+systemctl list-units --with-dependencies systemd-journald.service
ac3a84
+systemctl list-units --with-dependencies --after systemd-journald.service
ac3a84
+systemctl list-units --with-dependencies --before --reverse systemd-journald.service
ac3a84
+systemctl list-sockets
ac3a84
+systemctl list-sockets --legend=no -a "*journal*"
ac3a84
+systemctl list-sockets --show-types
ac3a84
+systemctl list-sockets --state=listening
ac3a84
+systemctl list-timers -a -l
ac3a84
+systemctl list-unit-files
ac3a84
+systemctl list-unit-files "*journal*"
ac3a84
+systemctl list-jobs
ac3a84
+systemctl list-jobs --after
ac3a84
+systemctl list-jobs --before
ac3a84
+systemctl list-jobs --after --before
ac3a84
+systemctl list-jobs "*"
ac3a84
+
ac3a84
+# Basic service management
ac3a84
+systemctl start --show-transaction "$UNIT_NAME"
ac3a84
+systemctl status -n 5 "$UNIT_NAME"
ac3a84
+systemctl is-active "$UNIT_NAME"
ac3a84
+systemctl reload -T "$UNIT_NAME"
ac3a84
+systemctl restart -T "$UNIT_NAME"
ac3a84
+systemctl try-restart --show-transaction "$UNIT_NAME"
ac3a84
+systemctl try-reload-or-restart --show-transaction "$UNIT_NAME"
ac3a84
+systemctl kill "$UNIT_NAME"
ac3a84
+(! systemctl is-active "$UNIT_NAME")
ac3a84
+systemctl restart "$UNIT_NAME"
ac3a84
+systemctl is-active "$UNIT_NAME"
ac3a84
+systemctl restart "$UNIT_NAME"
ac3a84
+systemctl stop "$UNIT_NAME"
ac3a84
+(! systemctl is-active "$UNIT_NAME")
ac3a84
+
ac3a84
+# enable/disable/preset
ac3a84
+(! systemctl is-enabled "$UNIT_NAME")
ac3a84
+systemctl enable "$UNIT_NAME"
ac3a84
+systemctl is-enabled -l "$UNIT_NAME"
ac3a84
+# We created a preset file for this unit above with a "disable" policy
ac3a84
+systemctl preset "$UNIT_NAME"
ac3a84
+(! systemctl is-enabled "$UNIT_NAME")
ac3a84
+systemctl reenable "$UNIT_NAME"
ac3a84
+systemctl is-enabled "$UNIT_NAME"
ac3a84
+systemctl preset --preset-mode=enable-only "$UNIT_NAME"
ac3a84
+systemctl is-enabled "$UNIT_NAME"
ac3a84
+systemctl preset --preset-mode=disable-only "$UNIT_NAME"
ac3a84
+(! systemctl is-enabled "$UNIT_NAME")
ac3a84
+systemctl enable --runtime "$UNIT_NAME"
ac3a84
+[[ -e "/run/systemd/system/multi-user.target.wants/$UNIT_NAME" ]]
ac3a84
+systemctl is-enabled "$UNIT_NAME"
ac3a84
+systemctl disable "$UNIT_NAME"
ac3a84
+# The unit should be still enabled, as we didn't use the --runtime switch
ac3a84
+systemctl is-enabled "$UNIT_NAME"
ac3a84
+systemctl disable --runtime "$UNIT_NAME"
ac3a84
+(! systemctl is-enabled "$UNIT_NAME")
ac3a84
+
ac3a84
+# mask/unmask/revert
ac3a84
+systemctl disable "$UNIT_NAME"
ac3a84
+[[ "$(systemctl is-enabled "$UNIT_NAME")" == disabled ]]
ac3a84
+systemctl mask "$UNIT_NAME"
ac3a84
+[[ "$(systemctl is-enabled "$UNIT_NAME")" == masked ]]
ac3a84
+systemctl unmask "$UNIT_NAME"
ac3a84
+[[ "$(systemctl is-enabled "$UNIT_NAME")" == disabled ]]
ac3a84
+systemctl mask "$UNIT_NAME"
ac3a84
+[[ "$(systemctl is-enabled "$UNIT_NAME")" == masked ]]
ac3a84
+systemctl revert "$UNIT_NAME"
ac3a84
+[[ "$(systemctl is-enabled "$UNIT_NAME")" == disabled ]]
ac3a84
+systemctl mask --runtime "$UNIT_NAME"
ac3a84
+[[ "$(systemctl is-enabled "$UNIT_NAME")" == masked-runtime ]]
ac3a84
+# This should be a no-op without the --runtime switch
ac3a84
+systemctl unmask "$UNIT_NAME"
ac3a84
+[[ "$(systemctl is-enabled "$UNIT_NAME")" == masked-runtime ]]
ac3a84
+systemctl unmask --runtime "$UNIT_NAME"
ac3a84
+[[ "$(systemctl is-enabled "$UNIT_NAME")" == disabled ]]
ac3a84
+
ac3a84
+# add-wants/add-requires
ac3a84
+(! systemctl show -P Wants "$UNIT_NAME" | grep "systemd-journald.service")
ac3a84
+systemctl add-wants "$UNIT_NAME" "systemd-journald.service"
ac3a84
+systemctl show -P Wants "$UNIT_NAME" | grep "systemd-journald.service"
ac3a84
+(! systemctl show -P Requires "$UNIT_NAME" | grep "systemd-journald.service")
ac3a84
+systemctl add-requires "$UNIT_NAME" "systemd-journald.service"
ac3a84
+systemctl show -P Requires "$UNIT_NAME" | grep "systemd-journald.service"
ac3a84
+
ac3a84
+# set-property
ac3a84
+systemctl set-property "$UNIT_NAME" IPAccounting=yes MemoryMax=1234567
ac3a84
+systemctl cat "$UNIT_NAME"
ac3a84
+# These properties should be saved to a persistent storage
ac3a84
+grep -r "IPAccounting=yes" "/etc/systemd/system.control/${UNIT_NAME}.d/"
ac3a84
+grep -r "MemoryMax=1234567" "/etc/systemd/system.control/${UNIT_NAME}.d"
ac3a84
+systemctl revert "$UNIT_NAME"
ac3a84
+(! grep -r "IPAccounting=" "/etc/systemd/system.control/${UNIT_NAME}.d/")
ac3a84
+(! grep -r "MemoryMax=" "/etc/systemd/system.control/${UNIT_NAME}.d/")
ac3a84
+# Same stuff, but with --runtime, which should use /run
ac3a84
+systemctl set-property --runtime "$UNIT_NAME" CPUAccounting=no CPUQuota=10%
ac3a84
+systemctl cat "$UNIT_NAME"
ac3a84
+grep -r "CPUAccounting=no" "/run/systemd/system.control/${UNIT_NAME}.d/"
ac3a84
+grep -r "CPUQuota=10%" "/run/systemd/system.control/${UNIT_NAME}.d/"
ac3a84
+systemctl revert "$UNIT_NAME"
ac3a84
+(! grep -r "CPUAccounting=" "/run/systemd/system.control/${UNIT_NAME}.d/")
ac3a84
+(! grep -r "CPUQuota=" "/run/systemd/system.control/${UNIT_NAME}.d/")
ac3a84
+
ac3a84
+# Failed-unit related tests
ac3a84
+systemd-run --unit "failed.service" /bin/false
ac3a84
+systemctl is-failed failed.service
ac3a84
+systemctl --state=failed | grep failed.service
ac3a84
+systemctl --failed | grep failed.service
ac3a84
+systemctl reset-failed "fail*.service"
ac3a84
+(! systemctl is-failed failed.service)
ac3a84
+
ac3a84
+# clean
ac3a84
+systemctl restart "$UNIT_NAME"
ac3a84
+systemctl stop "$UNIT_NAME"
ac3a84
+# Check if the directories from *Directory= directives exist
ac3a84
+# (except RuntimeDirectory= in /run, which is removed when the unit is stopped)
ac3a84
+for path in /var/lib /var/cache /var/log /etc; do
ac3a84
+    [[ -e "$path/$UNIT_NAME" ]]
ac3a84
+done
ac3a84
+# Run the cleanup
ac3a84
+for what in "" configuration state cache logs runtime all; do
ac3a84
+    systemctl clean ${what:+--what="$what"} "$UNIT_NAME"
ac3a84
+done
ac3a84
+# All respective directories should be removed
ac3a84
+for path in /run /var/lib /var/cache /var/log /etc; do
ac3a84
+    [[ ! -e "$path/$UNIT_NAME" ]]
ac3a84
+done
ac3a84
+
ac3a84
+# --timestamp
ac3a84
+for value in pretty us µs utc us+utc µs+utc; do
ac3a84
+    systemctl show -P KernelTimestamp --timestamp="$value"
ac3a84
+done
ac3a84
+
ac3a84
+# Aux verbs & assorted checks
ac3a84
+systemctl is-active "*-journald.service"
ac3a84
+systemctl cat "*journal*"
ac3a84
+systemctl cat "$UNIT_NAME"
ac3a84
+systemctl help "$UNIT_NAME"
ac3a84
+
ac3a84
+# show/set-environment
ac3a84
 # Make sure PATH is set
ac3a84
 systemctl show-environment | grep -q '^PATH='
ac3a84
-
ac3a84
 # Let's add an entry and override a built-in one
ac3a84
 systemctl set-environment PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/testaddition FOO=BAR
ac3a84
-
ac3a84
 # Check that both are set
ac3a84
 systemctl show-environment | grep -q '^PATH=.*testaddition$'
ac3a84
 systemctl show-environment | grep -q '^FOO=BAR$'
ac3a84
-
ac3a84
 systemctl daemon-reload
ac3a84
-
ac3a84
 # Check again after the reload
ac3a84
 systemctl show-environment | grep -q '^PATH=.*testaddition$'
ac3a84
 systemctl show-environment | grep -q '^FOO=BAR$'
ac3a84
-
ac3a84
 # Check that JSON output is supported
ac3a84
 systemctl show-environment --output=json | grep -q '^{.*"FOO":"BAR".*}$'
ac3a84
-
ac3a84
 # Drop both
ac3a84
 systemctl unset-environment FOO PATH
ac3a84
-
ac3a84
 # Check that one is gone and the other reverted to the built-in
ac3a84
 systemctl show-environment | grep '^FOO=$' && exit 1
ac3a84
 systemctl show-environment | grep '^PATH=.*testaddition$' && exit 1
ac3a84
 systemctl show-environment | grep -q '^PATH='
ac3a84
+# Check import-environment
ac3a84
+export IMPORT_THIS=hello
ac3a84
+export IMPORT_THIS_TOO=world
ac3a84
+systemctl import-environment IMPORT_THIS IMPORT_THIS_TOO
ac3a84
+systemctl show-environment | grep "^IMPORT_THIS=$IMPORT_THIS"
ac3a84
+systemctl show-environment | grep "^IMPORT_THIS_TOO=$IMPORT_THIS_TOO"
ac3a84
+systemctl unset-environment IMPORT_THIS IMPORT_THIS_TOO
ac3a84
+(! systemctl show-environment | grep "^IMPORT_THIS=")
ac3a84
+(! systemctl show-environment | grep "^IMPORT_THIS_TOO=")
ac3a84
 
ac3a84
 echo OK >/testok
ac3a84