naccyde / rpms / systemd

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