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