1import ./make-test-python.nix (
2 { pkgs, ... }:
3 {
4 name = "restart-by-activation-script";
5 meta = with pkgs.lib.maintainers; {
6 maintainers = [ das_j ];
7 };
8
9 nodes.machine =
10 { pkgs, ... }:
11 {
12 imports = [ ../modules/profiles/minimal.nix ];
13
14 system.switch.enable = true;
15
16 systemd.services.restart-me = {
17 wantedBy = [ "multi-user.target" ];
18 serviceConfig = {
19 Type = "oneshot";
20 RemainAfterExit = true;
21 ExecStart = "${pkgs.coreutils}/bin/true";
22 };
23 };
24
25 systemd.services.reload-me = {
26 wantedBy = [ "multi-user.target" ];
27 serviceConfig = rec {
28 Type = "oneshot";
29 RemainAfterExit = true;
30 ExecStart = "${pkgs.coreutils}/bin/true";
31 ExecReload = ExecStart;
32 };
33 };
34
35 system.activationScripts.test = {
36 supportsDryActivation = true;
37 text = ''
38 if [ -e /test-the-activation-script ]; then
39 if [ "$NIXOS_ACTION" != dry-activate ]; then
40 touch /activation-was-run
41 echo restart-me.service > /run/nixos/activation-restart-list
42 echo reload-me.service > /run/nixos/activation-reload-list
43 else
44 echo restart-me.service > /run/nixos/dry-activation-restart-list
45 echo reload-me.service > /run/nixos/dry-activation-reload-list
46 fi
47 fi
48 '';
49 };
50 };
51
52 testScript = # python
53 ''
54 machine.wait_for_unit("multi-user.target")
55
56 with subtest("nothing happens when the activation script does nothing"):
57 out = machine.succeed("/run/current-system/bin/switch-to-configuration dry-activate 2>&1")
58 assert 'restart' not in out
59 assert 'reload' not in out
60 out = machine.succeed("/run/current-system/bin/switch-to-configuration test")
61 assert 'restart' not in out
62 assert 'reload' not in out
63
64 machine.succeed("touch /test-the-activation-script")
65
66 with subtest("dry activation"):
67 out = machine.succeed("/run/current-system/bin/switch-to-configuration dry-activate 2>&1")
68 assert 'would restart the following units: restart-me.service' in out
69 assert 'would reload the following units: reload-me.service' in out
70 machine.fail("test -f /run/nixos/dry-activation-restart-list")
71 machine.fail("test -f /run/nixos/dry-activation-reload-list")
72
73 with subtest("real activation"):
74 out = machine.succeed("/run/current-system/bin/switch-to-configuration test 2>&1")
75 assert 'restarting the following units: restart-me.service' in out
76 assert 'reloading the following units: reload-me.service' in out
77 machine.fail("test -f /run/nixos/activation-restart-list")
78 machine.fail("test -f /run/nixos/activation-reload-list")
79 '';
80 }
81)