]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/units/testsuite-71.sh
221068f284f7adbbe99fe1e83c46ccb6273143df
[thirdparty/systemd.git] / test / units / testsuite-71.sh
1 #!/usr/bin/env bash
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 set -eux
5 set -o pipefail
6
7 # shellcheck source=test/units/assert.sh
8 . "$(dirname "$0")"/assert.sh
9
10 restore_hostname() {
11 if [[ -e /tmp/hostname.bak ]]; then
12 mv /tmp/hostname.bak /etc/hostname
13 else
14 rm -f /etc/hostname
15 fi
16 }
17
18 test_hostname() {
19 local orig=
20
21 if [[ -f /etc/hostname ]]; then
22 cp /etc/hostname /tmp/hostname.bak
23 orig=$(cat /etc/hostname)
24 fi
25
26 trap restore_hostname RETURN
27
28 # should activate daemon and work
29 if [[ -n "$orig" ]]; then
30 assert_in "Static hostname: $orig" "$(hostnamectl)"
31 fi
32 assert_in "Kernel: $(uname -s) $(uname -r)" "$(hostnamectl)"
33
34 # change hostname
35 assert_rc 0 hostnamectl set-hostname testhost
36 assert_eq "$(cat /etc/hostname)" "testhost"
37 assert_in "Static hostname: testhost" "$(hostnamectl)"
38
39 if [[ -n "$orig" ]]; then
40 # reset to original
41 assert_rc 0 hostnamectl set-hostname "$orig"
42 assert_eq "$(cat /etc/hostname)" "$orig"
43 assert_in "Static hostname: $orig" "$(hostnamectl)"
44 fi
45 }
46
47 restore_machine_info() {
48 if [[ -e /tmp/machine-info.bak ]]; then
49 mv /tmp/machine-info.bak /etc/machine-info
50 else
51 rm -f /etc/machine-info
52 fi
53 }
54
55 get_chassis() (
56 # shellcheck source=/dev/null
57 . /etc/machine-info
58
59 echo "$CHASSIS"
60 )
61
62 test_chassis() {
63 local i
64
65 if [[ -f /etc/machine-info ]]; then
66 cp /etc/machine-info /tmp/machine-info.bak
67 fi
68
69 trap restore_machine_info RETURN
70
71 # Invalid chassis type is refused
72 assert_rc 1 hostnamectl chassis hoge
73
74 # Valid chassis types
75 for i in vm container desktop laptop convertible server tablet handset watch embedded; do
76 hostnamectl chassis "$i"
77 assert_eq "$(hostnamectl chassis)" "$i"
78 assert_eq "$(get_chassis)" "$i"
79 done
80
81 systemctl stop systemd-hostnamed.service
82 rm -f /etc/machine-info
83
84 # fallback chassis type
85 if systemd-detect-virt --quiet --container; then
86 assert_eq "$(hostnamectl chassis)" container
87 elif systemd-detect-virt --quiet --vm; then
88 assert_eq "$(hostnamectl chassis)" vm
89 fi
90 }
91
92 restore_sysfs_dmi() {
93 umount /sys/class/dmi/id
94 rm -rf /run/systemd/system/systemd-hostnamed.service.d
95 systemctl daemon-reload
96 systemctl stop systemd-hostnamed
97 }
98
99 test_firmware_date() {
100 # No DMI on s390x or ppc
101 if [[ ! -d /sys/class/dmi/id ]]; then
102 echo "/sys/class/dmi/id not found, skipping firmware date tests."
103 return 0
104 fi
105
106 trap restore_sysfs_dmi RETURN
107
108 # Ignore /sys being mounted as tmpfs
109 mkdir -p /run/systemd/system/systemd-hostnamed.service.d/
110 cat >/run/systemd/system/systemd-hostnamed.service.d/override.conf <<EOF
111 [Service]
112 Environment="SYSTEMD_DEVICE_VERIFY_SYSFS=0"
113 EOF
114 systemctl daemon-reload
115
116 mount -t tmpfs none /sys/class/dmi/id
117 echo '1' > /sys/class/dmi/id/uevent
118
119 echo '01/01/2000' > /sys/class/dmi/id/bios_date
120 systemctl stop systemd-hostnamed
121 assert_in '2000-01-01' "$(hostnamectl)"
122
123 echo '2022' > /sys/class/dmi/id/bios_date
124 systemctl stop systemd-hostnamed
125 assert_not_in 'Firmware Date' "$(hostnamectl)"
126
127 echo 'garbage' > /sys/class/dmi/id/bios_date
128 systemctl stop systemd-hostnamed
129 assert_not_in 'Firmware Date' "$(hostnamectl)"
130 }
131
132 : >/failed
133
134 test_hostname
135 test_chassis
136 test_firmware_date
137
138 touch /testok
139 rm /failed