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