summaryrefslogtreecommitdiffstats
path: root/nixos/tests/restart-by-activation-script.nix
blob: b156aa21b8a5578269030d2c94b157890de34fe2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
{ pkgs, ... }:
{
  name = "restart-by-activation-script";
  meta = with pkgs.lib.maintainers; {
    maintainers = [ das_j ];
  };

  nodes.machine =
    { pkgs, lib, ... }:
    {
      imports = [ ../modules/profiles/minimal.nix ];

      system.switch.enable = true;

      systemd.services.restart-me = {
        wantedBy = [ "multi-user.target" ];
        serviceConfig = {
          Type = "oneshot";
          RemainAfterExit = true;
          ExecStart = "${pkgs.coreutils}/bin/true";
        };
      };

      systemd.services.reload-me = {
        wantedBy = [ "multi-user.target" ];
        serviceConfig = rec {
          Type = "oneshot";
          RemainAfterExit = true;
          ExecStart = "${pkgs.coreutils}/bin/true";
          ExecReload = ExecStart;
        };
      };

      system.activationScripts.test = {
        supportsDryActivation = true;
        text = ''
          if [ -e /test-the-activation-script ]; then
            if [ "$NIXOS_ACTION" != dry-activate ]; then
              touch /activation-was-run
              echo restart-me.service > /run/nixos/activation-restart-list
              echo reload-me.service > /run/nixos/activation-reload-list
            else
              echo restart-me.service > /run/nixos/dry-activation-restart-list
              echo reload-me.service > /run/nixos/dry-activation-reload-list
            fi
          fi
        '';
      };

      # Make sure we don't crash on long activation scripts
      specialisation.longscript.configuration = {
        system.activationScripts.long = {
          supportsDryActivation = true;
          text = lib.concatStringsSep "\n" (lib.genList (i: "# line number ${toString i}") 1000000);
        };
      };
    };

  testScript = # python
    ''
      machine.wait_for_unit("multi-user.target")

      with subtest("nothing happens when the activation script does nothing"):
          out = machine.succeed("/run/current-system/bin/switch-to-configuration dry-activate 2>&1")
          assert 'restart' not in out
          assert 'reload' not in out
          out = machine.succeed("/run/current-system/bin/switch-to-configuration test")
          assert 'restart' not in out
          assert 'reload' not in out

      machine.succeed("touch /test-the-activation-script")

      with subtest("dry activation"):
          out = machine.succeed("/run/current-system/bin/switch-to-configuration dry-activate 2>&1")
          assert 'would restart the following units: restart-me.service' in out
          assert 'would reload the following units: reload-me.service' in out
          machine.fail("test -f /run/nixos/dry-activation-restart-list")
          machine.fail("test -f /run/nixos/dry-activation-reload-list")

      with subtest("real activation"):
          out = machine.succeed("/run/current-system/bin/switch-to-configuration test 2>&1")
          assert 'restarting the following units: restart-me.service' in out
          assert 'reloading the following units: reload-me.service' in out
          machine.fail("test -f /run/nixos/activation-restart-list")
          machine.fail("test -f /run/nixos/activation-reload-list")
    '';
}