1 import ./make-test-python.nix ({ pkgs, ... }: {
4 nodes.machine = { config, lib, ... }: {
5 imports = [ common/user-account.nix common/x11.nix ];
7 virtualisation.emptyDiskImages = [ 512 512 ];
9 environment.systemPackages = [ pkgs.cryptsetup ];
11 virtualisation.fileSystems = {
12 "/test-x-initrd-mount" = {
17 options = [ "x-initrd.mount" ];
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";
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
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";
41 systemd.services.testDependency1 = {
42 description = "Test Dependency 1";
43 wantedBy = [ config.systemd.services."testservice1".name ];
44 serviceConfig.Type = "oneshot";
50 systemd.services.testservice1 = {
51 description = "Test Service 1";
52 wantedBy = [ config.systemd.targets.multi-user.name ];
53 serviceConfig.Type = "oneshot";
55 if [ "$XXX_SYSTEM" = foo ]; then
56 touch /system_conf_read
61 systemd.user.services.testservice2 = {
62 description = "Test Service 2";
63 wantedBy = [ "default.target" ];
64 serviceConfig.Type = "oneshot";
66 if [ "$XXX_USER" = bar ]; then
67 touch "$HOME/user_conf_read"
73 device = "/dev/watchdog";
79 environment.etc."systemd/system-preset/10-testservice.preset".text = ''
80 disable ${config.systemd.services.testservice1.name}
84 testScript = { nodes, ... }: ''
88 # Will not succeed unless ConditionFirstBoot=yes
89 machine.wait_for_unit("first-boot-complete.target")
92 # wait for user services
93 machine.wait_for_unit("default.target", "alice")
95 with subtest("systemctl edit suggests --runtime"):
96 # --runtime is suggested when using `systemctl edit`
97 ret, out = machine.execute("systemctl edit testservice1.service 2>&1")
99 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."
100 # editing w/o `--runtime` is possible for user-services, however
101 # it's not possible because we're not in a tty when grepping
102 # (i.e. hacky way to ensure that the error from above doesn't appear here).
103 _, out = machine.execute("systemctl --user edit testservice2.service 2>&1")
104 assert out.rstrip("\n") == "Cannot edit units if not on a tty."
106 # Regression test for https://github.com/NixOS/nixpkgs/issues/105049
107 with subtest("systemd reads timezone database in /etc/zoneinfo"):
108 timer = machine.succeed("TZ=UTC systemctl show --property=TimersCalendar oncalendar-test.timer")
109 assert re.search("next_elapse=Wed ....-..-.. 01:00:00 UTC", timer), f"got {timer.strip()}"
111 # Regression test for https://github.com/NixOS/nixpkgs/issues/35415
112 with subtest("configuration files are recognized by systemd"):
113 machine.succeed("test -e /system_conf_read")
114 machine.succeed("test -e /home/alice/user_conf_read")
115 machine.succeed("test -z $(ls -1 /var/log/journal)")
117 with subtest("regression test for https://bugs.freedesktop.org/show_bug.cgi?id=77507"):
118 retcode, output = machine.execute("systemctl status testservice1.service")
119 assert retcode in [0, 3] # https://bugs.freedesktop.org/show_bug.cgi?id=77507
121 # Regression test for https://github.com/NixOS/nixpkgs/issues/35268
122 with subtest("file system with x-initrd.mount is not unmounted"):
123 machine.succeed("mountpoint -q /test-x-initrd-mount")
126 subprocess.check_call(
132 "vm-state-machine/empty0.qcow2",
133 "x-initrd-mount.raw",
136 extinfo = subprocess.check_output(
138 "${pkgs.e2fsprogs}/bin/dumpe2fs",
139 "x-initrd-mount.raw",
143 re.search(r"^Filesystem state: *clean$", extinfo, re.MULTILINE) is not None
144 ), ("File system was not cleanly unmounted: " + extinfo)
146 # Regression test for https://github.com/NixOS/nixpkgs/pull/91232
147 with subtest("setting transient hostnames works"):
148 machine.succeed("hostnamectl set-hostname --transient machine-transient")
149 machine.fail("hostnamectl set-hostname machine-all")
151 with subtest("systemd-shutdown works"):
153 machine.wait_for_unit("multi-user.target")
154 machine.succeed("test -e /tmp/shared/shutdown-test")
156 # Test settings from /etc/sysctl.d/50-default.conf are applied
157 with subtest("systemd sysctl settings are applied"):
158 machine.wait_for_unit("multi-user.target")
159 assert "fq_codel" in machine.succeed("sysctl net.core.default_qdisc")
161 # Test systemd is configured to manage a watchdog
162 with subtest("systemd manages hardware watchdog"):
163 machine.wait_for_unit("multi-user.target")
165 # It seems that the device's path doesn't appear in 'systemctl show' so
166 # check it separately.
167 assert "WatchdogDevice=/dev/watchdog" in machine.succeed(
168 "cat /etc/systemd/system.conf"
171 output = machine.succeed("systemctl show | grep Watchdog")
172 # assert "RuntimeWatchdogUSec=30s" in output
173 # for some reason RuntimeWatchdogUSec, doesn't seem to be updated in here.
174 assert "RebootWatchdogUSec=10min" in output
175 assert "KExecWatchdogUSec=5min" in output
177 # Test systemd cryptsetup support
178 with subtest("systemd successfully reads /etc/crypttab and unlocks volumes"):
179 # create a luks volume and put a filesystem on it
181 "echo -n supersecret | cryptsetup luksFormat -q /dev/vdc -",
182 "echo -n supersecret | cryptsetup luksOpen --key-file - /dev/vdc foo",
183 "mkfs.ext3 /dev/mapper/foo",
186 # create a keyfile and /etc/crypttab
187 machine.succeed("echo -n supersecret > /var/lib/luks-keyfile")
188 machine.succeed("chmod 600 /var/lib/luks-keyfile")
189 machine.succeed("echo 'luks1 /dev/vdc /var/lib/luks-keyfile luks' > /etc/crypttab")
191 # after a reboot, systemd should unlock the volume and we should be able to mount it
193 machine.succeed("systemctl status systemd-cryptsetup@luks1.service")
194 machine.succeed("mkdir -p /tmp/luks1")
195 machine.succeed("mount /dev/mapper/luks1 /tmp/luks1")
198 output_ping = machine.succeed(
199 "systemd-run --wait -- ping -c 1 127.0.0.1 2>&1"
202 with subtest("systemd reports accounting data on system.slice"):
203 output = machine.succeed("systemctl status system.slice")
204 assert "CPU:" in output
205 assert "Memory:" in output
207 assert "IP:" in output
208 assert "0B in, 0B out" not in output
210 assert "IO:" in output
211 assert "0B read, 0B written" not in output
213 with subtest("systemd per-unit accounting works"):
214 assert "IP traffic received: 84B sent: 84B" in output_ping
216 with subtest("systemd environment is properly set"):
217 machine.systemctl("daemon-reexec") # Rewrites /proc/1/environ
218 machine.succeed("grep -q TZDIR=/etc/zoneinfo /proc/1/environ")
220 with subtest("systemd presets are ignored"):
221 machine.succeed("systemctl preset ${nodes.machine.systemd.services.testservice1.name}")
222 machine.succeed("test -e /etc/systemd/system/${nodes.machine.systemd.services.testservice1.name}")