2 # SPDX-License-Identifier: LGPL-2.1-or-later
7 # shellcheck source=test/units/assert.sh
8 .
"$(dirname "$0")"/assert.sh
11 if [[ -e /tmp
/hostname.bak
]]; then
12 mv /tmp
/hostname.bak
/etc
/hostname
21 if [[ -f /etc
/hostname
]]; then
22 cp /etc
/hostname
/tmp
/hostname.bak
23 orig
=$
(cat /etc
/hostname
)
26 trap restore_hostname RETURN
28 # should activate daemon and work
29 if [[ -n "$orig" ]]; then
30 assert_in
"Static hostname: $orig" "$(hostnamectl)"
32 assert_in
"Kernel: $(uname -s) $(uname -r)" "$(hostnamectl)"
35 assert_rc
0 hostnamectl set-hostname testhost
36 assert_eq
"$(cat /etc/hostname)" "testhost"
37 assert_in
"Static hostname: testhost" "$(hostnamectl)"
39 if [[ -n "$orig" ]]; then
41 assert_rc
0 hostnamectl set-hostname
"$orig"
42 assert_eq
"$(cat /etc/hostname)" "$orig"
43 assert_in
"Static hostname: $orig" "$(hostnamectl)"
47 restore_machine_info
() {
48 if [[ -e /tmp
/machine-info.bak
]]; then
49 mv /tmp
/machine-info.bak
/etc
/machine-info
51 rm -f /etc
/machine-info
56 # shellcheck source=/dev/null
65 if [[ -f /etc
/machine-info
]]; then
66 cp /etc
/machine-info
/tmp
/machine-info.bak
69 trap restore_machine_info RETURN
71 # Invalid chassis type is refused
72 assert_rc
1 hostnamectl chassis hoge
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"
81 systemctl stop systemd-hostnamed.service
82 rm -f /etc
/machine-info
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
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
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."
106 trap restore_sysfs_dmi RETURN
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
112 Environment="SYSTEMD_DEVICE_VERIFY_SYSFS=0"
114 systemctl daemon-reload
116 mount
-t tmpfs none
/sys
/class
/dmi
/id
117 echo '1' > /sys
/class
/dmi
/id
/uevent
119 echo '01/01/2000' > /sys
/class
/dmi
/id
/bios_date
120 systemctl stop systemd-hostnamed
121 assert_in
'2000-01-01' "$(hostnamectl)"
123 echo '2022' > /sys
/class
/dmi
/id
/bios_date
124 systemctl stop systemd-hostnamed
125 assert_not_in
'Firmware Date' "$(hostnamectl)"
127 echo 'garbage' > /sys
/class
/dmi
/id
/bios_date
128 systemctl stop systemd-hostnamed
129 assert_not_in
'Firmware Date' "$(hostnamectl)"