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