summaryrefslogtreecommitdiffstats
path: root/nixos/tests/containers-ephemeral.nix
blob: ade0cd8b275ef4cf8e348b4cf07153a4c7b27251 (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
{ pkgs, lib, ... }:
{
  name = "containers-ephemeral";
  meta = {
    maintainers = with lib.maintainers; [ patryk27 ];
  };

  nodes.machine =
    { pkgs, ... }:
    {
      virtualisation.writableStore = true;

      containers.webserver = {
        ephemeral = true;
        privateNetwork = true;
        hostAddress = "10.231.136.1";
        localAddress = "10.231.136.2";
        config = {
          services.nginx = {
            enable = true;
            virtualHosts.localhost = {
              root = pkgs.runCommand "localhost" { } ''
                mkdir "$out"
                echo hello world > "$out/index.html"
              '';
            };
          };
          networking.firewall.allowedTCPPorts = [ 80 ];
          nix.enable = false; # disabled by default on the test's host. See all-tests.nix / tag(no-nix-by-default)
        };
      };
    };

  testScript = ''
    assert "webserver" in machine.succeed("nixos-container list")

    machine.succeed("nixos-container start webserver")

    with subtest("Container got its own root folder"):
        machine.succeed("ls /run/nixos-containers/webserver")

    with subtest("Container persistent directory is not created"):
        machine.fail("ls /var/lib/nixos-containers/webserver")

    # Since "start" returns after the container has reached
    # multi-user.target, we should now be able to access it.
    ip = machine.succeed("nixos-container show-ip webserver").rstrip()
    machine.succeed(f"ping -n -c1 {ip}")
    machine.succeed(f"curl --fail http://{ip}/ > /dev/null")

    with subtest("Stop the container"):
        machine.succeed("nixos-container stop webserver")
        machine.fail(f"curl --fail --connect-timeout 2 http://{ip}/ > /dev/null")

    with subtest("Container's root folder was removed"):
        machine.fail("ls /run/nixos-containers/webserver")
  '';
}