1let
2 webserverFor = hostAddress: localAddress: {
3 inherit hostAddress localAddress;
4 privateNetwork = true;
5 config = {
6 services.httpd = {
7 enable = true;
8 adminAddr = "foo@example.org";
9 };
10 networking.firewall.allowedTCPPorts = [ 80 ];
11 };
12 };
13
14in import ./make-test-python.nix ({ pkgs, lib, ... }: {
15 name = "containers-ipv4-ipv6";
16 meta = {
17 maintainers = with lib.maintainers; [ aristid aszlig kampfschlaefer ];
18 };
19
20 nodes.machine =
21 { pkgs, ... }: {
22 virtualisation.writableStore = true;
23
24 containers.webserver4 = webserverFor "10.231.136.1" "10.231.136.2";
25 containers.webserver6 = webserverFor "fc00::2" "fc00::1";
26 virtualisation.additionalPaths = [ pkgs.stdenv ];
27 };
28
29 testScript = { nodes, ... }: ''
30 import time
31
32
33 def curl_host(ip):
34 # put [] around ipv6 addresses for curl
35 host = ip if ":" not in ip else f"[{ip}]"
36 return f"curl --fail --connect-timeout 2 http://{host}/ > /dev/null"
37
38
39 def get_ip(container):
40 # need to distinguish because show-ip won't work for ipv6
41 if container == "webserver4":
42 ip = machine.succeed(f"nixos-container show-ip {container}").rstrip()
43 assert ip == "${nodes.machine.config.containers.webserver4.localAddress}"
44 return ip
45 return "${nodes.machine.config.containers.webserver6.localAddress}"
46
47
48 for container in "webserver4", "webserver6":
49 assert container in machine.succeed("nixos-container list")
50
51 with subtest(f"Start container {container}"):
52 machine.succeed(f"nixos-container start {container}")
53 # wait 2s for container to start and network to be up
54 time.sleep(2)
55
56 # Since "start" returns after the container has reached
57 # multi-user.target, we should now be able to access it.
58
59 ip = get_ip(container)
60 with subtest(f"{container} reacts to pings and HTTP requests"):
61 machine.succeed(f"ping -n -c1 {ip}")
62 machine.succeed(curl_host(ip))
63
64 with subtest(f"Stop container {container}"):
65 machine.succeed(f"nixos-container stop {container}")
66 machine.fail(curl_host(ip))
67
68 # Destroying a declarative container should fail.
69 machine.fail(f"nixos-container destroy {container}")
70 '';
71})