ac3a84
From e0d51a65a8bbe8c86af4bb843a5f9ac7d590fa01 Mon Sep 17 00:00:00 2001
ac3a84
From: Frantisek Sumsal <frantisek@sumsal.cz>
ac3a84
Date: Thu, 3 Nov 2022 13:13:03 +0100
ac3a84
Subject: [PATCH] test: introduce sanity coverage for auxiliary utils
ac3a84
ac3a84
(cherry picked from commit d1020334fd15e0cffe68cb4d7e862a36253cc481)
ac3a84
ac3a84
Related #2138081
ac3a84
---
ac3a84
 test/TEST-74-AUX-UTILS/Makefile      |   1 +
ac3a84
 test/TEST-74-AUX-UTILS/test.sh       |  10 ++
ac3a84
 test/units/testsuite-74.cgls.sh      |  26 +++++
ac3a84
 test/units/testsuite-74.cgtop.sh     |  32 ++++++
ac3a84
 test/units/testsuite-74.delta.sh     |  59 +++++++++++
ac3a84
 test/units/testsuite-74.firstboot.sh | 145 +++++++++++++++++++++++++++
ac3a84
 test/units/testsuite-74.service      |   8 ++
ac3a84
 test/units/testsuite-74.sh           |  14 +++
ac3a84
 8 files changed, 295 insertions(+)
ac3a84
 create mode 120000 test/TEST-74-AUX-UTILS/Makefile
ac3a84
 create mode 100755 test/TEST-74-AUX-UTILS/test.sh
ac3a84
 create mode 100755 test/units/testsuite-74.cgls.sh
ac3a84
 create mode 100755 test/units/testsuite-74.cgtop.sh
ac3a84
 create mode 100755 test/units/testsuite-74.delta.sh
ac3a84
 create mode 100755 test/units/testsuite-74.firstboot.sh
ac3a84
 create mode 100644 test/units/testsuite-74.service
ac3a84
 create mode 100755 test/units/testsuite-74.sh
ac3a84
ac3a84
diff --git a/test/TEST-74-AUX-UTILS/Makefile b/test/TEST-74-AUX-UTILS/Makefile
ac3a84
new file mode 120000
ac3a84
index 0000000000..e9f93b1104
ac3a84
--- /dev/null
ac3a84
+++ b/test/TEST-74-AUX-UTILS/Makefile
ac3a84
@@ -0,0 +1 @@
ac3a84
+../TEST-01-BASIC/Makefile
ac3a84
\ No newline at end of file
ac3a84
diff --git a/test/TEST-74-AUX-UTILS/test.sh b/test/TEST-74-AUX-UTILS/test.sh
ac3a84
new file mode 100755
ac3a84
index 0000000000..f422c89141
ac3a84
--- /dev/null
ac3a84
+++ b/test/TEST-74-AUX-UTILS/test.sh
ac3a84
@@ -0,0 +1,10 @@
ac3a84
+#!/usr/bin/env bash
ac3a84
+# SPDX-License-Identifier: LGPL-2.1-or-later
ac3a84
+set -e
ac3a84
+
ac3a84
+TEST_DESCRIPTION="Tests for auxiliary utilities"
ac3a84
+
ac3a84
+# shellcheck source=test/test-functions
ac3a84
+. "${TEST_BASE_DIR:?}/test-functions"
ac3a84
+
ac3a84
+do_test "$@"
ac3a84
diff --git a/test/units/testsuite-74.cgls.sh b/test/units/testsuite-74.cgls.sh
ac3a84
new file mode 100755
ac3a84
index 0000000000..120570c9cc
ac3a84
--- /dev/null
ac3a84
+++ b/test/units/testsuite-74.cgls.sh
ac3a84
@@ -0,0 +1,26 @@
ac3a84
+#!/usr/bin/env bash
ac3a84
+# SPDX-License-Identifier: LGPL-2.1-or-later
ac3a84
+set -eux
ac3a84
+set -o pipefail
ac3a84
+
ac3a84
+systemd-cgls
ac3a84
+systemd-cgls --all --full
ac3a84
+systemd-cgls -k
ac3a84
+systemd-cgls --xattr=yes
ac3a84
+systemd-cgls --xattr=no
ac3a84
+systemd-cgls --cgroup-id=yes
ac3a84
+systemd-cgls --cgroup-id=no
ac3a84
+
ac3a84
+systemd-cgls /system.slice/systemd-journald.service
ac3a84
+systemd-cgls /system.slice/systemd-journald.service /init.scope
ac3a84
+systemd-cgls /sys/fs/cgroup/system.slice/systemd-journald.service /init.scope
ac3a84
+(cd /sys/fs/cgroup/init.scope && systemd-cgls)
ac3a84
+systemd-cgls --unit=systemd-journald.service
ac3a84
+# There's most likely no user session running, so we need to create one
ac3a84
+systemd-run --user --wait --pipe -M testuser@.host systemd-cgls --user-unit=app.slice
ac3a84
+
ac3a84
+(! systemd-cgls /foo/bar)
ac3a84
+(! systemd-cgls --unit=hello.world)
ac3a84
+(! systemd-cgls --user-unit=hello.world)
ac3a84
+(! systemd-cgls --xattr=foo)
ac3a84
+(! systemd-cgls --cgroup-id=foo)
ac3a84
diff --git a/test/units/testsuite-74.cgtop.sh b/test/units/testsuite-74.cgtop.sh
ac3a84
new file mode 100755
ac3a84
index 0000000000..8141ec1b1f
ac3a84
--- /dev/null
ac3a84
+++ b/test/units/testsuite-74.cgtop.sh
ac3a84
@@ -0,0 +1,32 @@
ac3a84
+#!/usr/bin/env bash
ac3a84
+# SPDX-License-Identifier: LGPL-2.1-or-later
ac3a84
+set -eux
ac3a84
+set -o pipefail
ac3a84
+
ac3a84
+# Without tty attached cgtop should default to --iterations=1
ac3a84
+systemd-cgtop
ac3a84
+systemd-cgtop --iterations=1
ac3a84
+# Same as --iterations=1
ac3a84
+systemd-cgtop -1
ac3a84
+systemd-cgtop --delay=1ms
ac3a84
+systemd-cgtop --raw
ac3a84
+systemd-cgtop --batch
ac3a84
+systemd-cgtop --cpu=percentage
ac3a84
+systemd-cgtop --cpu=time
ac3a84
+systemd-cgtop -P
ac3a84
+systemd-cgtop -k
ac3a84
+# FIXME: https://github.com/systemd/systemd/issues/25248
ac3a84
+#systemd-cgtop --recursive=no
ac3a84
+systemd-cgtop --depth=0
ac3a84
+systemd-cgtop --depth=100
ac3a84
+
ac3a84
+for order in path tasks cpu memory io; do
ac3a84
+    systemd-cgtop --order="$order"
ac3a84
+done
ac3a84
+systemd-cgtop -p -t -c -m -i
ac3a84
+
ac3a84
+(! systemd-cgtop --cpu=foo)
ac3a84
+(! systemd-cgtop --order=foo)
ac3a84
+(! systemd-cgtop --depth=-1)
ac3a84
+(! systemd-cgtop --recursive=foo)
ac3a84
+(! systemd-cgtop --delay=1foo)
ac3a84
diff --git a/test/units/testsuite-74.delta.sh b/test/units/testsuite-74.delta.sh
ac3a84
new file mode 100755
ac3a84
index 0000000000..a0e1cb52dd
ac3a84
--- /dev/null
ac3a84
+++ b/test/units/testsuite-74.delta.sh
ac3a84
@@ -0,0 +1,59 @@
ac3a84
+#!/usr/bin/env bash
ac3a84
+# SPDX-License-Identifier: LGPL-2.1-or-later
ac3a84
+set -eux
ac3a84
+set -o pipefail
ac3a84
+
ac3a84
+at_exit() {
ac3a84
+    rm -rfv /{run,etc}/systemd/system/delta-test*
ac3a84
+}
ac3a84
+
ac3a84
+trap at_exit EXIT
ac3a84
+
ac3a84
+# Create a couple of supporting units with overrides
ac3a84
+#
ac3a84
+# Extended unit
ac3a84
+cat >"/run/systemd/system/delta-test-unit-extended.service" <
ac3a84
+[Service]
ac3a84
+ExecStart=/bin/true
ac3a84
+EOF
ac3a84
+mkdir -p "/run/systemd/system/delta-test-unit-extended.service.d"
ac3a84
+cat >"/run/systemd/system/delta-test-unit-extended.service.d/override.conf" <
ac3a84
+[Unit]
ac3a84
+Description=Foo Bar
ac3a84
+[Service]
ac3a84
+ExecStartPre=/bin/true
ac3a84
+EOF
ac3a84
+# Masked unit
ac3a84
+cp -fv /run/systemd/system/delta-test-unit-extended.service /run/systemd/system/delta-test-unit-masked.service
ac3a84
+systemctl mask delta-test-unit-masked.service
ac3a84
+# Overridden unit
ac3a84
+cp -fv /run/systemd/system/delta-test-unit-extended.service /run/systemd/system/delta-test-unit-overridden.service
ac3a84
+cp -fv /run/systemd/system/delta-test-unit-overridden.service /etc/systemd/system/delta-test-unit-overridden.service
ac3a84
+echo "ExecStartPost=/bin/true" >>/etc/systemd/system/delta-test-unit-overridden.service
ac3a84
+# Overridden but equivalent unit
ac3a84
+ln -srfv /run/systemd/system/delta-test-unit-extended.service /run/systemd/system/delta-test-unit-equivalent.service
ac3a84
+ln -sfv /run/systemd/system/delta-test-unit-extended.service /etc/systemd/system/delta-test-unit-equivalent.service
ac3a84
+# Redirected unit
ac3a84
+ln -srfv /run/systemd/system/delta-test-unit-extended.service /run/systemd/system/delta-test-unit-redirected.service
ac3a84
+ln -sfv /run/systemd/system/delta-test-unit-overidden.service /etc/systemd/system/delta-test-unit-extended.service
ac3a84
+
ac3a84
+systemctl daemon-reload
ac3a84
+
ac3a84
+systemd-delta
ac3a84
+systemd-delta /run
ac3a84
+systemd-delta systemd/system
ac3a84
+systemd-delta /run systemd/system /run
ac3a84
+systemd-delta /run foo/bar hello/world systemd/system /run
ac3a84
+systemd-delta foo/bar
ac3a84
+systemd-delta --diff=true
ac3a84
+systemd-delta --diff=false
ac3a84
+
ac3a84
+for type in masked equivalent redirected overridden extended unchanged; do
ac3a84
+    systemd-delta --type="$type"
ac3a84
+    systemd-delta --type="$type" /run
ac3a84
+done
ac3a84
+systemd-delta --type=equivalent,redirected
ac3a84
+
ac3a84
+(! systemd-delta --diff=foo)
ac3a84
+(! systemd-delta --type=foo)
ac3a84
+(! systemd-delta --type=equivalent,redirected,foo)
ac3a84
diff --git a/test/units/testsuite-74.firstboot.sh b/test/units/testsuite-74.firstboot.sh
ac3a84
new file mode 100755
ac3a84
index 0000000000..02f9f5cd7a
ac3a84
--- /dev/null
ac3a84
+++ b/test/units/testsuite-74.firstboot.sh
ac3a84
@@ -0,0 +1,145 @@
ac3a84
+#!/usr/bin/env bash
ac3a84
+# SPDX-License-Identifier: LGPL-2.1-or-later
ac3a84
+set -eux
ac3a84
+set -o pipefail
ac3a84
+
ac3a84
+if ! command -v systemd-firstboot >/dev/null; then
ac3a84
+    echo "systemd-firstboot not found, skipping the test"
ac3a84
+    exit 0
ac3a84
+fi
ac3a84
+
ac3a84
+at_exit() {
ac3a84
+    if [[ -v ROOT && -n "$ROOT" ]]; then
ac3a84
+        ls -lR "$ROOT"
ac3a84
+        rm -fr "$ROOT"
ac3a84
+    fi
ac3a84
+}
ac3a84
+
ac3a84
+trap at_exit EXIT
ac3a84
+
ac3a84
+# Generated via `mkpasswd -m sha-512 -S foobarsalt password1`
ac3a84
+# shellcheck disable=SC2016
ac3a84
+ROOT_HASHED_PASSWORD1='$6$foobarsalt$YbwdaATX6IsFxvWbY3QcZj2gB31R/LFRFrjlFrJtTTqFtSfn4dfOAg/km2k4Sl.a2g7LOYDo31wMTaEsCo9j41'
ac3a84
+# Generated via `mkpasswd -m sha-512 -S foobarsalt password2`
ac3a84
+# shellcheck disable=SC2016
ac3a84
+ROOT_HASHED_PASSWORD2='$6$foobarsalt$q.P2932zYMLbKnjFwIxPI8y3iuxeuJ2BgE372LcZMMnj3Gcg/9mJg2LPKUl.ha0TG/.fRNNnRQcLfzM0SNot3.'
ac3a84
+
ac3a84
+# Create a minimal root so we don't modify the testbed
ac3a84
+ROOT=test-root
ac3a84
+mkdir -p "$ROOT/bin"
ac3a84
+# Dummy shell for --root-shell=
ac3a84
+touch "$ROOT/bin/fooshell" "$ROOT/bin/barshell"
ac3a84
+
ac3a84
+systemd-firstboot --root="$ROOT" --locale=foo
ac3a84
+grep -q "LANG=foo" "$ROOT/etc/locale.conf"
ac3a84
+rm -fv "$ROOT/etc/locale.conf"
ac3a84
+# FIXME: https://github.com/systemd/systemd/issues/25249
ac3a84
+#systemd-firstboot --root="$ROOT" --locale-messages=foo
ac3a84
+#grep -q "LC_MESSAGES=foo" "$ROOT/etc/locale.conf"
ac3a84
+#rm -fv "$ROOT/etc/locale.conf"
ac3a84
+systemd-firstboot --root="$ROOT" --locale=foo --locale-messages=bar
ac3a84
+grep -q "LANG=foo" "$ROOT/etc/locale.conf"
ac3a84
+grep -q "LC_MESSAGES=bar" "$ROOT/etc/locale.conf"
ac3a84
+
ac3a84
+systemd-firstboot --root="$ROOT" --keymap=foo
ac3a84
+grep -q "KEYMAP=foo" "$ROOT/etc/vconsole.conf"
ac3a84
+
ac3a84
+systemd-firstboot --root="$ROOT" --timezone=Europe/Berlin
ac3a84
+readlink "$ROOT/etc/localtime" | grep -q "Europe/Berlin"
ac3a84
+
ac3a84
+systemd-firstboot --root="$ROOT" --hostname "foobar"
ac3a84
+grep -q "foobar" "$ROOT/etc/hostname"
ac3a84
+
ac3a84
+systemd-firstboot --root="$ROOT" --machine-id=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
ac3a84
+grep -q "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "$ROOT/etc/machine-id"
ac3a84
+
ac3a84
+rm -fv "$ROOT/etc/passwd" "$ROOT/etc/shadow"
ac3a84
+systemd-firstboot --root="$ROOT" --root-password=foo
ac3a84
+grep -q "^root:x:0:0:" "$ROOT/etc/passwd"
ac3a84
+grep -q "^root:" "$ROOT/etc/shadow"
ac3a84
+rm -fv "$ROOT/etc/passwd" "$ROOT/etc/shadow"
ac3a84
+echo "foo" >root.passwd
ac3a84
+systemd-firstboot --root="$ROOT" --root-password-file=root.passwd
ac3a84
+grep -q "^root:x:0:0:" "$ROOT/etc/passwd"
ac3a84
+grep -q "^root:" "$ROOT/etc/shadow"
ac3a84
+rm -fv "$ROOT/etc/passwd" "$ROOT/etc/shadow" root.passwd
ac3a84
+# Set the shell together with the password, as firstboot won't touch
ac3a84
+# /etc/passwd if it already exists
ac3a84
+systemd-firstboot --root="$ROOT" --root-password-hashed="$ROOT_HASHED_PASSWORD1" --root-shell=/bin/fooshell
ac3a84
+grep -q "^root:x:0:0:.*:/bin/fooshell$" "$ROOT/etc/passwd"
ac3a84
+grep -q "^root:$ROOT_HASHED_PASSWORD1:" "$ROOT/etc/shadow"
ac3a84
+
ac3a84
+systemd-firstboot --root="$ROOT" --kernel-command-line="foo.bar=42"
ac3a84
+grep -q "foo.bar=42" "$ROOT/etc/kernel/cmdline"
ac3a84
+
ac3a84
+# Configs should not get overwritten if they exist unless --force is used
ac3a84
+systemd-firstboot --root="$ROOT" \
ac3a84
+                  --locale=locale-overwrite \
ac3a84
+                  --locale-messages=messages-overwrite \
ac3a84
+                  --keymap=keymap-overwrite \
ac3a84
+                  --timezone=CET \
ac3a84
+                  --hostname=hostname-overwrite \
ac3a84
+                  --machine-id=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb \
ac3a84
+                  --root-password-hashed="$ROOT_HASHED_PASSWORD2" \
ac3a84
+                  --root-shell=/bin/barshell \
ac3a84
+                  --kernel-command-line="hello.world=0"
ac3a84
+grep -q "LANG=foo" "$ROOT/etc/locale.conf"
ac3a84
+grep -q "LC_MESSAGES=bar" "$ROOT/etc/locale.conf"
ac3a84
+grep -q "KEYMAP=foo" "$ROOT/etc/vconsole.conf"
ac3a84
+readlink "$ROOT/etc/localtime" | grep -q "Europe/Berlin$"
ac3a84
+grep -q "foobar" "$ROOT/etc/hostname"
ac3a84
+grep -q "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "$ROOT/etc/machine-id"
ac3a84
+grep -q "^root:x:0:0:.*:/bin/fooshell$" "$ROOT/etc/passwd"
ac3a84
+grep -q "^root:$ROOT_HASHED_PASSWORD1:" "$ROOT/etc/shadow"
ac3a84
+grep -q "foo.bar=42" "$ROOT/etc/kernel/cmdline"
ac3a84
+
ac3a84
+# The same thing, but now with --force
ac3a84
+systemd-firstboot --root="$ROOT" --force \
ac3a84
+                  --locale=locale-overwrite \
ac3a84
+                  --locale-messages=messages-overwrite \
ac3a84
+                  --keymap=keymap-overwrite \
ac3a84
+                  --timezone=CET \
ac3a84
+                  --hostname=hostname-overwrite \
ac3a84
+                  --machine-id=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb \
ac3a84
+                  --root-password-hashed="$ROOT_HASHED_PASSWORD2" \
ac3a84
+                  --root-shell=/bin/barshell \
ac3a84
+                  --kernel-command-line="hello.world=0"
ac3a84
+grep -q "LANG=locale-overwrite" "$ROOT/etc/locale.conf"
ac3a84
+grep -q "LC_MESSAGES=messages-overwrite" "$ROOT/etc/locale.conf"
ac3a84
+grep -q "KEYMAP=keymap-overwrite" "$ROOT/etc/vconsole.conf"
ac3a84
+readlink "$ROOT/etc/localtime" | grep -q "/CET$"
ac3a84
+grep -q "hostname-overwrite" "$ROOT/etc/hostname"
ac3a84
+grep -q "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" "$ROOT/etc/machine-id"
ac3a84
+grep -q "^root:x:0:0:.*:/bin/barshell$" "$ROOT/etc/passwd"
ac3a84
+grep -q "^root:$ROOT_HASHED_PASSWORD2:" "$ROOT/etc/shadow"
ac3a84
+grep -q "hello.world=0" "$ROOT/etc/kernel/cmdline"
ac3a84
+
ac3a84
+# --copy-* options
ac3a84
+rm -fr "$ROOT"
ac3a84
+mkdir "$ROOT"
ac3a84
+# Copy everything at once (--copy)
ac3a84
+systemd-firstboot --root="$ROOT" --copy
ac3a84
+diff /etc/locale.conf "$ROOT/etc/locale.conf"
ac3a84
+diff <(awk -F: '/^root/ { print $7; }' /etc/passwd) <(awk -F: '/^root/ { print $7; }' "$ROOT/etc/passwd")
ac3a84
+diff <(awk -F: '/^root/ { print $2; }' /etc/shadow) <(awk -F: '/^root/ { print $2; }' "$ROOT/etc/shadow")
ac3a84
+[[ -e /etc/vconsole.conf ]] && diff /etc/vconsole.conf "$ROOT/etc/vconsole.conf"
ac3a84
+[[ -e /etc/localtime ]] && diff <(readlink /etc/localtime) <(readlink "$ROOT/etc/localtime")
ac3a84
+rm -fr "$ROOT"
ac3a84
+mkdir "$ROOT"
ac3a84
+# Copy everything at once, but now by using separate switches
ac3a84
+systemd-firstboot --root="$ROOT" --copy-locale --copy-keymap --copy-timezone --copy-root-password --copy-root-shell
ac3a84
+diff /etc/locale.conf "$ROOT/etc/locale.conf"
ac3a84
+diff <(awk -F: '/^root/ { print $7; }' /etc/passwd) <(awk -F: '/^root/ { print $7; }' "$ROOT/etc/passwd")
ac3a84
+diff <(awk -F: '/^root/ { print $2; }' /etc/shadow) <(awk -F: '/^root/ { print $2; }' "$ROOT/etc/shadow")
ac3a84
+[[ -e /etc/vconsole.conf ]] && diff /etc/vconsole.conf "$ROOT/etc/vconsole.conf"
ac3a84
+[[ -e /etc/localtime ]] && diff <(readlink /etc/localtime) <(readlink "$ROOT/etc/localtime")
ac3a84
+
ac3a84
+# Assorted tests
ac3a84
+rm -fr "$ROOT"
ac3a84
+mkdir "$ROOT"
ac3a84
+
ac3a84
+systemd-firstboot --root="$ROOT" --setup-machine-id
ac3a84
+grep -E "[a-z0-9]{32}" "$ROOT/etc/machine-id"
ac3a84
+
ac3a84
+systemd-firstboot --root="$ROOT" --delete-root-password
ac3a84
+diff <(echo) <(awk -F: '/^root/ { print $2; }' "$ROOT/etc/shadow")
ac3a84
diff --git a/test/units/testsuite-74.service b/test/units/testsuite-74.service
ac3a84
new file mode 100644
ac3a84
index 0000000000..f782132a92
ac3a84
--- /dev/null
ac3a84
+++ b/test/units/testsuite-74.service
ac3a84
@@ -0,0 +1,8 @@
ac3a84
+# SPDX-License-Identifier: LGPL-2.1-or-later
ac3a84
+[Unit]
ac3a84
+Description=TEST-74-AUX-UTILS
ac3a84
+
ac3a84
+[Service]
ac3a84
+ExecStartPre=rm -f /failed /testok
ac3a84
+ExecStart=/usr/lib/systemd/tests/testdata/units/%N.sh
ac3a84
+Type=oneshot
ac3a84
diff --git a/test/units/testsuite-74.sh b/test/units/testsuite-74.sh
ac3a84
new file mode 100755
ac3a84
index 0000000000..13c767e490
ac3a84
--- /dev/null
ac3a84
+++ b/test/units/testsuite-74.sh
ac3a84
@@ -0,0 +1,14 @@
ac3a84
+#!/usr/bin/env bash
ac3a84
+# SPDX-License-Identifier: LGPL-2.1-or-later
ac3a84
+set -eux
ac3a84
+set -o pipefail
ac3a84
+
ac3a84
+: >/failed
ac3a84
+
ac3a84
+for script in "${0%.sh}".*.sh; do
ac3a84
+    echo "Running $script"
ac3a84
+    "./$script"
ac3a84
+done
ac3a84
+
ac3a84
+touch /testok
ac3a84
+rm /failed