1import ./make-test-python.nix (
2 { pkgs, lib, ... }:
3 {
4 name = "containers-ephemeral";
5 meta = {
6 maintainers = with lib.maintainers; [ patryk27 ];
7 };
8
9 nodes.machine =
10 { pkgs, ... }:
11 {
12 virtualisation.writableStore = true;
13
14 containers.webserver = {
15 ephemeral = true;
16 privateNetwork = true;
17 hostAddress = "10.231.136.1";
18 localAddress = "10.231.136.2";
19 config = {
20 services.nginx = {
21 enable = true;
22 virtualHosts.localhost = {
23 root = pkgs.runCommand "localhost" { } ''
24 mkdir "$out"
25 echo hello world > "$out/index.html"
26 '';
27 };
28 };
29 networking.firewall.allowedTCPPorts = [ 80 ];
30 };
31 };
32 };
33
34 testScript = ''
35 assert "webserver" in machine.succeed("nixos-container list")
36
37 machine.succeed("nixos-container start webserver")
38
39 with subtest("Container got its own root folder"):
40 machine.succeed("ls /run/nixos-containers/webserver")
41
42 with subtest("Container persistent directory is not created"):
43 machine.fail("ls /var/lib/nixos-containers/webserver")
44
45 # Since "start" returns after the container has reached
46 # multi-user.target, we should now be able to access it.
47 ip = machine.succeed("nixos-container show-ip webserver").rstrip()
48 machine.succeed(f"ping -n -c1 {ip}")
49 machine.succeed(f"curl --fail http://{ip}/ > /dev/null")
50
51 with subtest("Stop the container"):
52 machine.succeed("nixos-container stop webserver")
53 machine.fail(f"curl --fail --connect-timeout 2 http://{ip}/ > /dev/null")
54
55 with subtest("Container's root folder was removed"):
56 machine.fail("ls /run/nixos-containers/webserver")
57 '';
58 }
59)