From ca46781c5ffa3aaa7a8fb6f09976357d003c4aae Mon Sep 17 00:00:00 2001 From: Frantisek Sumsal Date: Mon, 31 Oct 2022 12:11:59 +0100 Subject: [PATCH 1/4] test: add a couple of sanity tests for journalctl --- test/units/testsuite-04.sh | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/units/testsuite-04.sh b/test/units/testsuite-04.sh index b5468cbea4..fdc3273fea 100755 --- a/test/units/testsuite-04.sh +++ b/test/units/testsuite-04.sh @@ -119,6 +119,44 @@ systemctl start silent-success journalctl --sync [[ -z "$(journalctl -b -q -u silent-success.service)" ]] +# Exercise the matching machinery +SYSTEMD_LOG_LEVEL=debug journalctl -b -n 1 /dev/null /dev/zero /dev/null /dev/null /dev/null +journalctl -b -n 1 /bin/true /bin/false +journalctl -b -n 1 /bin/true + /bin/false +journalctl -b -n 1 -r --unit "systemd*" + +systemd-run --user -M "testuser@.host" /bin/echo hello +journalctl --sync +journalctl -b -n 1 -r --user-unit "*" + +(! journalctl -b /dev/lets-hope-this-doesnt-exist) +(! journalctl -b /dev/null /dev/zero /dev/this-also-shouldnt-exist) +(! journalctl -b --unit "this-unit-should-not-exist*") + +# Facilities & priorities +journalctl --facility help +journalctl --facility kern -n 1 +journalctl --facility syslog --priority 0..3 -n 1 +journalctl --facility syslog --priority 3..0 -n 1 +journalctl --facility user --priority 0..0 -n 1 +journalctl --facility daemon --priority warning -n 1 +journalctl --facility daemon --priority warning..info -n 1 +journalctl --facility daemon --priority notice..crit -n 1 +journalctl --facility daemon --priority 5..crit -n 1 + +(! journalctl --facility hopefully-an-unknown-facility) +(! journalctl --priority hello-world) +(! journalctl --priority 0..128) +(! journalctl --priority 0..systemd) + +# Other options +journalctl --disk-usage +journalctl --dmesg -n 1 +journalctl --fields +journalctl --list-boots +journalctl --update-catalog +journalctl --list-catalog + # Add new tests before here, the journald restarts below # may make tests flappy. From 70e9066bc2eaf159e9cde7d95bbee99e44f3045a Mon Sep 17 00:00:00 2001 From: Frantisek Sumsal Date: Tue, 1 Nov 2022 09:17:58 +0100 Subject: [PATCH 2/4] test: add a couple of sanity tests for loginctl --- test/units/testsuite-35.sh | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/test/units/testsuite-35.sh b/test/units/testsuite-35.sh index 4ef0f0c11c..85925f2471 100755 --- a/test/units/testsuite-35.sh +++ b/test/units/testsuite-35.sh @@ -338,6 +338,50 @@ EOF assert_eq "$(loginctl --no-legend | awk '$3=="logind-test-user" { print $5 }')" "tty2" } +test_sanity_check() { + # Exercise basic loginctl options + + if [[ ! -c /dev/tty2 ]]; then + echo "/dev/tty2 does not exist, skipping test ${FUNCNAME[0]}." + return + fi + + trap cleanup_session RETURN + create_session + + # Run most of the loginctl commands from a user session to make + # the seat/session autodetection work-ish + systemd-run --user --pipe --wait -M "logind-test-user@.host" bash -eux <<\EOF + loginctl list-sessions + loginctl session-status + loginctl show-session + loginctl show-session -P DelayInhibited + + # We're not in the same session scope, so in this case we need to specify + # the session ID explicitly + session=$(loginctl --no-legend | awk '$3 == "logind-test-user" { print $1; exit; }') + loginctl kill-session --signal=SIGCONT "$session" + # FIXME(?) + #loginctl kill-session --signal=SIGCONT --kill-who=leader "$session" + + loginctl list-users + loginctl user-status + loginctl show-user -a + loginctl show-user -P IdleAction + loginctl kill-user --signal=SIGCONT "" + + loginctl list-seats + loginctl seat-status + loginctl show-seat + loginctl show-seat -P IdleActionUSec +EOF + + # Requires root privileges + loginctl lock-sessions + loginctl unlock-sessions + loginctl flush-devices +} + test_session() { local dev @@ -537,6 +581,7 @@ test_properties test_started test_suspend_on_lid test_shutdown +test_sanity_check test_session test_lock_idle_action test_session_properties From c5c258ae0a4a0cfc829ed07ff96c7fab79b6ca71 Mon Sep 17 00:00:00 2001 From: Frantisek Sumsal Date: Tue, 1 Nov 2022 17:53:42 +0100 Subject: [PATCH 3/4] test: rename TEST-26-SETENV to TEST-26-SYSTEMCTL --- test/{TEST-26-SETENV => TEST-26-SYSTEMCTL}/Makefile | 0 test/{TEST-26-SETENV => TEST-26-SYSTEMCTL}/test.sh | 2 +- test/units/testsuite-26.service | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename test/{TEST-26-SETENV => TEST-26-SYSTEMCTL}/Makefile (100%) rename test/{TEST-26-SETENV => TEST-26-SYSTEMCTL}/test.sh (79%) diff --git a/test/TEST-26-SETENV/Makefile b/test/TEST-26-SYSTEMCTL/Makefile similarity index 100% rename from test/TEST-26-SETENV/Makefile rename to test/TEST-26-SYSTEMCTL/Makefile diff --git a/test/TEST-26-SETENV/test.sh b/test/TEST-26-SYSTEMCTL/test.sh similarity index 79% rename from test/TEST-26-SETENV/test.sh rename to test/TEST-26-SYSTEMCTL/test.sh index b38e37bfce..64accf850f 100755 --- a/test/TEST-26-SETENV/test.sh +++ b/test/TEST-26-SYSTEMCTL/test.sh @@ -2,7 +2,7 @@ # SPDX-License-Identifier: LGPL-2.1-or-later set -e -TEST_DESCRIPTION="test setenv" +TEST_DESCRIPTION="systemctl-related tests" # shellcheck source=test/test-functions . "${TEST_BASE_DIR:?}/test-functions" diff --git a/test/units/testsuite-26.service b/test/units/testsuite-26.service index aa553b61a6..d8fdaffb06 100644 --- a/test/units/testsuite-26.service +++ b/test/units/testsuite-26.service @@ -1,6 +1,6 @@ # SPDX-License-Identifier: LGPL-2.1-or-later [Unit] -Description=TEST-26-SETENV +Description=TEST-26-SYSTEMCTL [Service] ExecStartPre=rm -f /failed /testok From d16684fe13e1d56e55df19b57b6c01b9a9303086 Mon Sep 17 00:00:00 2001 From: Frantisek Sumsal Date: Tue, 1 Nov 2022 20:47:37 +0100 Subject: [PATCH 4/4] test: add a couple of sanity tests for systemctl --- test/units/testsuite-26.sh | 215 +++++++++++++++++++++++++++++++++++-- 1 file changed, 205 insertions(+), 10 deletions(-) diff --git a/test/units/testsuite-26.sh b/test/units/testsuite-26.sh index ad08415317..b83f85917b 100755 --- a/test/units/testsuite-26.sh +++ b/test/units/testsuite-26.sh @@ -3,32 +3,227 @@ set -eux set -o pipefail -# Make sure PATH is set -systemctl show-environment | grep -q '^PATH=' +at_exit() { + if [[ -v UNIT_NAME && -e "/usr/lib/systemd/system/$UNIT_NAME" ]]; then + rm -fv "/usr/lib/systemd/system/$UNIT_NAME" + fi +} -# Let's add an entry and override a built-in one -systemctl set-environment PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/testaddition FOO=BAR +trap at_exit EXIT -# Check that both are set -systemctl show-environment | grep -q '^PATH=.*testaddition$' -systemctl show-environment | grep -q '^FOO=BAR$' +# Create a simple unit file for testing +# Note: the service file is created under /usr on purpose to test +# the 'revert' verb as well +UNIT_NAME="systemctl-test-$RANDOM.service" +cat >"/usr/lib/systemd/system/$UNIT_NAME" <<\EOF +[Unit] +Description=systemctl test + +[Service] +ExecStart=sleep infinity +ExecReload=true + +# For systemctl clean +CacheDirectory=%n +ConfigurationDirectory=%n +LogsDirectory=%n +RuntimeDirectory=%n +StateDirectory=%n + +[Install] +WantedBy=multi-user.target +EOF + +# Configure the preset setting for the unit file +mkdir /run/systemd/system-preset/ +echo "disable $UNIT_NAME" >/run/systemd/system-preset/99-systemd-test.preset systemctl daemon-reload +# Argument help +systemctl --state help +systemctl --signal help +systemctl --type help + +# list-dependencies +systemctl list-dependencies systemd-journald +systemctl list-dependencies --after systemd-journald +systemctl list-dependencies --before systemd-journald +systemctl list-dependencies --after --reverse systemd-journald +systemctl list-dependencies --before --reverse systemd-journald +systemctl list-dependencies --plain systemd-journald + +# list-* verbs +systemctl list-units +systemctl list-units --recursive +systemctl list-units --type=socket +systemctl list-units --type=service,timer +systemctl list-units --legend=yes -a "systemd-*" +systemctl list-units --state=active +systemctl list-units --with-dependencies systemd-journald.service +systemctl list-units --with-dependencies --after systemd-journald.service +systemctl list-units --with-dependencies --before --reverse systemd-journald.service +systemctl list-sockets +systemctl list-sockets --legend=no -a "*journal*" +systemctl list-sockets --show-types +systemctl list-sockets --state=listening +systemctl list-timers -a -l +systemctl list-unit-files +systemctl list-unit-files "*journal*" +systemctl list-jobs +systemctl list-jobs --after +systemctl list-jobs --before +systemctl list-jobs --after --before +systemctl list-jobs "*" + +# Basic service management +systemctl start --show-transaction "$UNIT_NAME" +systemctl status -n 5 "$UNIT_NAME" +systemctl is-active "$UNIT_NAME" +systemctl reload -T "$UNIT_NAME" +systemctl restart -T "$UNIT_NAME" +systemctl try-restart --show-transaction "$UNIT_NAME" +systemctl try-reload-or-restart --show-transaction "$UNIT_NAME" +systemctl kill "$UNIT_NAME" +(! systemctl is-active "$UNIT_NAME") +systemctl restart "$UNIT_NAME" +systemctl is-active "$UNIT_NAME" +systemctl restart "$UNIT_NAME" +systemctl stop "$UNIT_NAME" +(! systemctl is-active "$UNIT_NAME") + +# enable/disable/preset +(! systemctl is-enabled "$UNIT_NAME") +systemctl enable "$UNIT_NAME" +systemctl is-enabled -l "$UNIT_NAME" +# We created a preset file for this unit above with a "disable" policy +systemctl preset "$UNIT_NAME" +(! systemctl is-enabled "$UNIT_NAME") +systemctl reenable "$UNIT_NAME" +systemctl is-enabled "$UNIT_NAME" +systemctl preset --preset-mode=enable-only "$UNIT_NAME" +systemctl is-enabled "$UNIT_NAME" +systemctl preset --preset-mode=disable-only "$UNIT_NAME" +(! systemctl is-enabled "$UNIT_NAME") +systemctl enable --runtime "$UNIT_NAME" +[[ -e "/run/systemd/system/multi-user.target.wants/$UNIT_NAME" ]] +systemctl is-enabled "$UNIT_NAME" +systemctl disable "$UNIT_NAME" +# The unit should be still enabled, as we didn't use the --runtime switch +systemctl is-enabled "$UNIT_NAME" +systemctl disable --runtime "$UNIT_NAME" +(! systemctl is-enabled "$UNIT_NAME") + +# mask/unmask/revert +systemctl disable "$UNIT_NAME" +[[ "$(systemctl is-enabled "$UNIT_NAME")" == disabled ]] +systemctl mask "$UNIT_NAME" +[[ "$(systemctl is-enabled "$UNIT_NAME")" == masked ]] +systemctl unmask "$UNIT_NAME" +[[ "$(systemctl is-enabled "$UNIT_NAME")" == disabled ]] +systemctl mask "$UNIT_NAME" +[[ "$(systemctl is-enabled "$UNIT_NAME")" == masked ]] +systemctl revert "$UNIT_NAME" +[[ "$(systemctl is-enabled "$UNIT_NAME")" == disabled ]] +systemctl mask --runtime "$UNIT_NAME" +[[ "$(systemctl is-enabled "$UNIT_NAME")" == masked-runtime ]] +# This should be a no-op without the --runtime switch +systemctl unmask "$UNIT_NAME" +[[ "$(systemctl is-enabled "$UNIT_NAME")" == masked-runtime ]] +systemctl unmask --runtime "$UNIT_NAME" +[[ "$(systemctl is-enabled "$UNIT_NAME")" == disabled ]] + +# add-wants/add-requires +(! systemctl show -P Wants "$UNIT_NAME" | grep "systemd-journald.service") +systemctl add-wants "$UNIT_NAME" "systemd-journald.service" +systemctl show -P Wants "$UNIT_NAME" | grep "systemd-journald.service" +(! systemctl show -P Requires "$UNIT_NAME" | grep "systemd-journald.service") +systemctl add-requires "$UNIT_NAME" "systemd-journald.service" +systemctl show -P Requires "$UNIT_NAME" | grep "systemd-journald.service" + +# set-property +systemctl set-property "$UNIT_NAME" IPAccounting=yes MemoryMax=1234567 +systemctl cat "$UNIT_NAME" +# These properties should be saved to a persistent storage +grep -r "IPAccounting=yes" "/etc/systemd/system.control/${UNIT_NAME}.d/" +grep -r "MemoryMax=1234567" "/etc/systemd/system.control/${UNIT_NAME}.d" +systemctl revert "$UNIT_NAME" +(! grep -r "IPAccounting=" "/etc/systemd/system.control/${UNIT_NAME}.d/") +(! grep -r "MemoryMax=" "/etc/systemd/system.control/${UNIT_NAME}.d/") +# Same stuff, but with --runtime, which should use /run +systemctl set-property --runtime "$UNIT_NAME" CPUAccounting=no CPUQuota=10% +systemctl cat "$UNIT_NAME" +grep -r "CPUAccounting=no" "/run/systemd/system.control/${UNIT_NAME}.d/" +grep -r "CPUQuota=10%" "/run/systemd/system.control/${UNIT_NAME}.d/" +systemctl revert "$UNIT_NAME" +(! grep -r "CPUAccounting=" "/run/systemd/system.control/${UNIT_NAME}.d/") +(! grep -r "CPUQuota=" "/run/systemd/system.control/${UNIT_NAME}.d/") + +# Failed-unit related tests +systemd-run --unit "failed.service" /bin/false +systemctl is-failed failed.service +systemctl --state=failed | grep failed.service +systemctl --failed | grep failed.service +systemctl reset-failed "fail*.service" +(! systemctl is-failed failed.service) + +# clean +systemctl restart "$UNIT_NAME" +systemctl stop "$UNIT_NAME" +# Check if the directories from *Directory= directives exist +# (except RuntimeDirectory= in /run, which is removed when the unit is stopped) +for path in /var/lib /var/cache /var/log /etc; do + [[ -e "$path/$UNIT_NAME" ]] +done +# Run the cleanup +for what in "" configuration state cache logs runtime all; do + systemctl clean ${what:+--what="$what"} "$UNIT_NAME" +done +# All respective directories should be removed +for path in /run /var/lib /var/cache /var/log /etc; do + [[ ! -e "$path/$UNIT_NAME" ]] +done + +# --timestamp +for value in pretty us µs utc us+utc µs+utc; do + systemctl show -P KernelTimestamp --timestamp="$value" +done + +# Aux verbs & assorted checks +systemctl is-active "*-journald.service" +systemctl cat "*journal*" +systemctl cat "$UNIT_NAME" +systemctl help "$UNIT_NAME" + +# show/set-environment +# Make sure PATH is set +systemctl show-environment | grep -q '^PATH=' +# Let's add an entry and override a built-in one +systemctl set-environment PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/testaddition FOO=BAR +# Check that both are set +systemctl show-environment | grep -q '^PATH=.*testaddition$' +systemctl show-environment | grep -q '^FOO=BAR$' +systemctl daemon-reload # Check again after the reload systemctl show-environment | grep -q '^PATH=.*testaddition$' systemctl show-environment | grep -q '^FOO=BAR$' - # Check that JSON output is supported systemctl show-environment --output=json | grep -q '^{.*"FOO":"BAR".*}$' - # Drop both systemctl unset-environment FOO PATH - # Check that one is gone and the other reverted to the built-in systemctl show-environment | grep '^FOO=$' && exit 1 systemctl show-environment | grep '^PATH=.*testaddition$' && exit 1 systemctl show-environment | grep -q '^PATH=' +# Check import-environment +export IMPORT_THIS=hello +export IMPORT_THIS_TOO=world +systemctl import-environment IMPORT_THIS IMPORT_THIS_TOO +systemctl show-environment | grep "^IMPORT_THIS=$IMPORT_THIS" +systemctl show-environment | grep "^IMPORT_THIS_TOO=$IMPORT_THIS_TOO" +systemctl unset-environment IMPORT_THIS IMPORT_THIS_TOO +(! systemctl show-environment | grep "^IMPORT_THIS=") +(! systemctl show-environment | grep "^IMPORT_THIS_TOO=") echo OK >/testok