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