1import ./make-test-python.nix ({ pkgs, ... }: {
2 name = "systemd";
3
4 nodes.machine = { lib, ... }: {
5 imports = [ common/user-account.nix common/x11.nix ];
6
7 virtualisation.emptyDiskImages = [ 512 512 ];
8
9 environment.systemPackages = [ pkgs.cryptsetup ];
10
11 virtualisation.fileSystems = {
12 "/test-x-initrd-mount" = {
13 device = "/dev/vdb";
14 fsType = "ext2";
15 autoFormat = true;
16 noCheck = true;
17 options = [ "x-initrd.mount" ];
18 };
19 };
20
21 systemd.extraConfig = "DefaultEnvironment=\"XXX_SYSTEM=foo\"";
22 systemd.user.extraConfig = "DefaultEnvironment=\"XXX_USER=bar\"";
23 services.journald.extraConfig = "Storage=volatile";
24 test-support.displayManager.auto.user = "alice";
25
26 systemd.shutdown.test = pkgs.writeScript "test.shutdown" ''
27 #!${pkgs.runtimeShell}
28 PATH=${lib.makeBinPath (with pkgs; [ util-linux coreutils ])}
29 mount -t 9p shared -o trans=virtio,version=9p2000.L /tmp/shared
30 touch /tmp/shared/shutdown-test
31 umount /tmp/shared
32 '';
33
34 systemd.services.oncalendar-test = {
35 description = "calendar test";
36 # Japan does not have DST which makes the test a little bit simpler
37 startAt = "Wed 10:00 Asia/Tokyo";
38 script = "true";
39 };
40
41 systemd.services.testservice1 = {
42 description = "Test Service 1";
43 wantedBy = [ "multi-user.target" ];
44 serviceConfig.Type = "oneshot";
45 script = ''
46 if [ "$XXX_SYSTEM" = foo ]; then
47 touch /system_conf_read
48 fi
49 '';
50 };
51
52 systemd.user.services.testservice2 = {
53 description = "Test Service 2";
54 wantedBy = [ "default.target" ];
55 serviceConfig.Type = "oneshot";
56 script = ''
57 if [ "$XXX_USER" = bar ]; then
58 touch "$HOME/user_conf_read"
59 fi
60 '';
61 };
62
63 systemd.watchdog = {
64 device = "/dev/watchdog";
65 runtimeTime = "30s";
66 rebootTime = "10min";
67 kexecTime = "5min";
68 };
69 };
70
71 testScript = ''
72 import re
73 import subprocess
74
75 machine.wait_for_x()
76 # wait for user services
77 machine.wait_for_unit("default.target", "alice")
78
79 with subtest("systemctl edit suggests --runtime"):
80 # --runtime is suggested when using `systemctl edit`
81 ret, out = machine.execute("systemctl edit testservice1.service 2>&1")
82 assert ret == 1
83 assert out.rstrip("\n") == "The unit-directory '/etc/systemd/system' is read-only on NixOS, so it's not possible to edit system-units directly. Use 'systemctl edit --runtime' instead."
84 # editing w/o `--runtime` is possible for user-services, however
85 # it's not possible because we're not in a tty when grepping
86 # (i.e. hacky way to ensure that the error from above doesn't appear here).
87 _, out = machine.execute("systemctl --user edit testservice2.service 2>&1")
88 assert out.rstrip("\n") == "Cannot edit units if not on a tty."
89
90 # Regression test for https://github.com/NixOS/nixpkgs/issues/105049
91 with subtest("systemd reads timezone database in /etc/zoneinfo"):
92 timer = machine.succeed("TZ=UTC systemctl show --property=TimersCalendar oncalendar-test.timer")
93 assert re.search("next_elapse=Wed ....-..-.. 01:00:00 UTC", timer), f"got {timer.strip()}"
94
95 # Regression test for https://github.com/NixOS/nixpkgs/issues/35415
96 with subtest("configuration files are recognized by systemd"):
97 machine.succeed("test -e /system_conf_read")
98 machine.succeed("test -e /home/alice/user_conf_read")
99 machine.succeed("test -z $(ls -1 /var/log/journal)")
100
101 with subtest("regression test for https://bugs.freedesktop.org/show_bug.cgi?id=77507"):
102 retcode, output = machine.execute("systemctl status testservice1.service")
103 assert retcode in [0, 3] # https://bugs.freedesktop.org/show_bug.cgi?id=77507
104
105 # Regression test for https://github.com/NixOS/nixpkgs/issues/35268
106 with subtest("file system with x-initrd.mount is not unmounted"):
107 machine.succeed("mountpoint -q /test-x-initrd-mount")
108 machine.shutdown()
109
110 subprocess.check_call(
111 [
112 "qemu-img",
113 "convert",
114 "-O",
115 "raw",
116 "vm-state-machine/empty0.qcow2",
117 "x-initrd-mount.raw",
118 ]
119 )
120 extinfo = subprocess.check_output(
121 [
122 "${pkgs.e2fsprogs}/bin/dumpe2fs",
123 "x-initrd-mount.raw",
124 ]
125 ).decode("utf-8")
126 assert (
127 re.search(r"^Filesystem state: *clean$", extinfo, re.MULTILINE) is not None
128 ), ("File system was not cleanly unmounted: " + extinfo)
129
130 # Regression test for https://github.com/NixOS/nixpkgs/pull/91232
131 with subtest("setting transient hostnames works"):
132 machine.succeed("hostnamectl set-hostname --transient machine-transient")
133 machine.fail("hostnamectl set-hostname machine-all")
134
135 with subtest("systemd-shutdown works"):
136 machine.shutdown()
137 machine.wait_for_unit("multi-user.target")
138 machine.succeed("test -e /tmp/shared/shutdown-test")
139
140 # Test settings from /etc/sysctl.d/50-default.conf are applied
141 with subtest("systemd sysctl settings are applied"):
142 machine.wait_for_unit("multi-user.target")
143 assert "fq_codel" in machine.succeed("sysctl net.core.default_qdisc")
144
145 # Test systemd is configured to manage a watchdog
146 with subtest("systemd manages hardware watchdog"):
147 machine.wait_for_unit("multi-user.target")
148
149 # It seems that the device's path doesn't appear in 'systemctl show' so
150 # check it separately.
151 assert "WatchdogDevice=/dev/watchdog" in machine.succeed(
152 "cat /etc/systemd/system.conf"
153 )
154
155 output = machine.succeed("systemctl show | grep Watchdog")
156 # assert "RuntimeWatchdogUSec=30s" in output
157 # for some reason RuntimeWatchdogUSec, doesn't seem to be updated in here.
158 assert "RebootWatchdogUSec=10min" in output
159 assert "KExecWatchdogUSec=5min" in output
160
161 # Test systemd cryptsetup support
162 with subtest("systemd successfully reads /etc/crypttab and unlocks volumes"):
163 # create a luks volume and put a filesystem on it
164 machine.succeed(
165 "echo -n supersecret | cryptsetup luksFormat -q /dev/vdc -",
166 "echo -n supersecret | cryptsetup luksOpen --key-file - /dev/vdc foo",
167 "mkfs.ext3 /dev/mapper/foo",
168 )
169
170 # create a keyfile and /etc/crypttab
171 machine.succeed("echo -n supersecret > /var/lib/luks-keyfile")
172 machine.succeed("chmod 600 /var/lib/luks-keyfile")
173 machine.succeed("echo 'luks1 /dev/vdc /var/lib/luks-keyfile luks' > /etc/crypttab")
174
175 # after a reboot, systemd should unlock the volume and we should be able to mount it
176 machine.shutdown()
177 machine.succeed("systemctl status systemd-cryptsetup@luks1.service")
178 machine.succeed("mkdir -p /tmp/luks1")
179 machine.succeed("mount /dev/mapper/luks1 /tmp/luks1")
180
181 # Do some IP traffic
182 output_ping = machine.succeed(
183 "systemd-run --wait -- ping -c 1 127.0.0.1 2>&1"
184 )
185
186 with subtest("systemd reports accounting data on system.slice"):
187 output = machine.succeed("systemctl status system.slice")
188 assert "CPU:" in output
189 assert "Memory:" in output
190
191 assert "IP:" in output
192 assert "0B in, 0B out" not in output
193
194 assert "IO:" in output
195 assert "0B read, 0B written" not in output
196
197 with subtest("systemd per-unit accounting works"):
198 assert "IP traffic received: 84B" in output_ping
199 assert "IP traffic sent: 84B" in output_ping
200
201 with subtest("systemd environment is properly set"):
202 machine.systemctl("daemon-reexec") # Rewrites /proc/1/environ
203 machine.succeed("grep -q TZDIR=/etc/zoneinfo /proc/1/environ")
204 '';
205})