summaryrefslogtreecommitdiffstats
path: root/nixos/tests/systemd-capsules.nix
blob: a7a23e11131136e4996d4ce17b5269f8f7608f24 (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
{ lib, ... }:
{
  name = "systemd-capsules";

  meta.maintainers = with lib.maintainers; [ fpletz ];

  nodes.machine =
    { pkgs, ... }:
    {
      environment.systemPackages = [ pkgs.hello ];
      systemd.user.services.alice-sleep = {
        wantedBy = [ "capsule@alice.target" ];
        serviceConfig = {
          ExecStart = "${pkgs.coreutils}/bin/sleep 999";
        };
      };
    };

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

      with subtest("capsule setup"):
        machine.succeed("systemctl start capsule@alice.service")

      with subtest("imperative user service in capsule"):
        machine.succeed("systemd-run --capsule=alice --unit=sleeptest.service /run/current-system/sw/bin/sleep 999")
        machine.succeed("systemctl --capsule=alice status sleeptest.service")

      with subtest("declarative user service in capsule"):
        machine.succeed("systemctl --capsule=alice status alice-sleep.service")
        machine.succeed("systemctl --capsule=alice stop alice-sleep.service")
        machine.fail("systemctl --capsule=alice status alice-sleep.service")
        machine.succeed("systemctl --capsule=alice start alice-sleep.service")
        machine.succeed("systemctl --capsule=alice status alice-sleep.service")

      with subtest("interactive shell with terminal in capsule"):
        hello_output = machine.succeed("systemd-run -t --capsule=alice /run/current-system/sw/bin/bash -i -c 'hello | tee ~/hello'")
        assert hello_output == "Hello, world!\r\n"
        machine.copy_from_machine("/var/lib/capsules/alice/hello")

      with subtest("capsule cleanup"):
        machine.succeed("systemctl --capsule=alice stop sleeptest.service")
        machine.succeed("systemctl stop capsule@alice.service")
        machine.succeed("systemctl clean --all capsule@alice.service")
    '';
}