594167
From b9724ced388bef0a53be49c4ce45db2c8352f186 Mon Sep 17 00:00:00 2001
594167
From: Yu Watanabe <watanabe.yu+github@gmail.com>
594167
Date: Tue, 14 Jun 2022 09:03:00 +0900
594167
Subject: [PATCH] test: import hostnamed tests from debian/ubuntu test suite
594167
594167
(cherry picked from commit 39f4546199d6394014cf79b76b9ff22d3c149c54)
594167
594167
Related: #2087652
594167
---
594167
 test/TEST-71-HOSTNAME/Makefile  |  1 +
594167
 test/TEST-71-HOSTNAME/test.sh   | 10 +++++++
594167
 test/units/testsuite-71.service | 10 +++++++
594167
 test/units/testsuite-71.sh      | 52 +++++++++++++++++++++++++++++++++
594167
 4 files changed, 73 insertions(+)
594167
 create mode 120000 test/TEST-71-HOSTNAME/Makefile
594167
 create mode 100755 test/TEST-71-HOSTNAME/test.sh
594167
 create mode 100644 test/units/testsuite-71.service
594167
 create mode 100755 test/units/testsuite-71.sh
594167
594167
diff --git a/test/TEST-71-HOSTNAME/Makefile b/test/TEST-71-HOSTNAME/Makefile
594167
new file mode 120000
594167
index 0000000000..e9f93b1104
594167
--- /dev/null
594167
+++ b/test/TEST-71-HOSTNAME/Makefile
594167
@@ -0,0 +1 @@
594167
+../TEST-01-BASIC/Makefile
594167
\ No newline at end of file
594167
diff --git a/test/TEST-71-HOSTNAME/test.sh b/test/TEST-71-HOSTNAME/test.sh
594167
new file mode 100755
594167
index 0000000000..7b3d2d12d4
594167
--- /dev/null
594167
+++ b/test/TEST-71-HOSTNAME/test.sh
594167
@@ -0,0 +1,10 @@
594167
+#!/usr/bin/env bash
594167
+# SPDX-License-Identifier: LGPL-2.1-or-later
594167
+set -e
594167
+
594167
+TEST_DESCRIPTION="test hostnamed"
594167
+
594167
+# shellcheck source=test/test-functions
594167
+. "${TEST_BASE_DIR:?}/test-functions"
594167
+
594167
+do_test "$@"
594167
diff --git a/test/units/testsuite-71.service b/test/units/testsuite-71.service
594167
new file mode 100644
594167
index 0000000000..019e8bff24
594167
--- /dev/null
594167
+++ b/test/units/testsuite-71.service
594167
@@ -0,0 +1,10 @@
594167
+# SPDX-License-Identifier: LGPL-2.1-or-later
594167
+[Unit]
594167
+Description=TEST-71-HOSTNAME
594167
+
594167
+[Service]
594167
+ExecStartPre=rm -f /failed /testok
594167
+ExecStart=/usr/lib/systemd/tests/testdata/units/%N.sh
594167
+StandardOutput=journal+console
594167
+StandardError=journal+console
594167
+Type=oneshot
594167
diff --git a/test/units/testsuite-71.sh b/test/units/testsuite-71.sh
594167
new file mode 100755
594167
index 0000000000..34fcaad961
594167
--- /dev/null
594167
+++ b/test/units/testsuite-71.sh
594167
@@ -0,0 +1,52 @@
594167
+#!/usr/bin/env bash
594167
+# SPDX-License-Identifier: LGPL-2.1-or-later
594167
+
594167
+set -eux
594167
+set -o pipefail
594167
+
594167
+# shellcheck source=test/units/assert.sh
594167
+. "$(dirname "$0")"/assert.sh
594167
+
594167
+restore_hostname() {
594167
+    if [[ -e /tmp/hostname.bak ]]; then
594167
+        mv /tmp/hostname.bak /etc/hostname
594167
+    else
594167
+        rm -f /etc/hostname
594167
+    fi
594167
+}
594167
+
594167
+test_hostname() {
594167
+    local orig=
594167
+
594167
+    if [[ -f /etc/hostname ]]; then
594167
+        cp /etc/hostname /tmp/hostname.bak
594167
+        orig=$(cat /etc/hostname)
594167
+    fi
594167
+
594167
+    trap restore_hostname RETURN
594167
+
594167
+    # should activate daemon and work
594167
+    if [[ -n "$orig" ]]; then
594167
+        assert_in "Static hostname: $orig" "$(hostnamectl)"
594167
+    fi
594167
+    assert_in "Kernel: $(uname -s) $(uname -r)" "$(hostnamectl)"
594167
+
594167
+    # change hostname
594167
+    assert_rc 0 hostnamectl set-hostname testhost
594167
+    assert_eq "$(cat /etc/hostname)" "testhost"
594167
+    assert_in "Static hostname: testhost" "$(hostnamectl)"
594167
+
594167
+    if [[ -n "$orig" ]]; then
594167
+        # reset to original
594167
+        assert_rc 0 hostnamectl set-hostname "$orig"
594167
+        assert_eq "$(cat /etc/hostname)" "$orig"
594167
+        assert_in "Static hostname: $orig" "$(hostnamectl)"
594167
+    fi
594167
+}
594167
+
594167
+: >/failed
594167
+
594167
+test_hostname
594167
+
594167
+touch /testok
594167
+rm /failed