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