2 webserverFor = hostAddress: localAddress: {
3 inherit hostAddress localAddress;
8 adminAddr = "foo@example.org";
10 networking.firewall.allowedTCPPorts = [ 80 ];
14 in import ./make-test-python.nix ({ pkgs, lib, ... }: {
15 name = "containers-ipv4-ipv6";
17 maintainers = with lib.maintainers; [ aristid aszlig kampfschlaefer ];
22 virtualisation.writableStore = true;
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 ];
29 testScript = { nodes, ... }: ''
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"
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}"
45 return "${nodes.machine.config.containers.webserver6.localAddress}"
48 for container in "webserver4", "webserver6":
49 assert container in machine.succeed("nixos-container list")
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
56 # Since "start" returns after the container has reached
57 # multi-user.target, we should now be able to access it.
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))
64 with subtest(f"Stop container {container}"):
65 machine.succeed(f"nixos-container stop {container}")
66 machine.fail(curl_host(ip))
68 # Destroying a declarative container should fail.
69 machine.fail(f"nixos-container destroy {container}")