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 eelco kampfschlaefer ];
18 };
19
20 nodes.machine =
21 { pkgs, ... }: {
22 imports = [ ../modules/installer/cd-dvd/channel.nix ];
23 virtualisation = {
24 writableStore = true;
25 };
26
27 containers.webserver4 = webserverFor "10.231.136.1" "10.231.136.2";
28 containers.webserver6 = webserverFor "fc00::2" "fc00::1";
29 virtualisation.additionalPaths = [ pkgs.stdenv ];
30 };
31
32 testScript = { nodes, ... }: ''
33 import time
34
35
36 def curl_host(ip):
37 # put [] around ipv6 addresses for curl
38 host = ip if ":" not in ip else f"[{ip}]"
39 return f"curl --fail --connect-timeout 2 http://{host}/ > /dev/null"
40
41
42 def get_ip(container):
43 # need to distinguish because show-ip won't work for ipv6
44 if container == "webserver4":
45 ip = machine.succeed(f"nixos-container show-ip {container}").rstrip()
46 assert ip == "${nodes.machine.config.containers.webserver4.localAddress}"
47 return ip
48 return "${nodes.machine.config.containers.webserver6.localAddress}"
49
50
51 for container in "webserver4", "webserver6":
52 assert container in machine.succeed("nixos-container list")
53
54 with subtest(f"Start container {container}"):
55 machine.succeed(f"nixos-container start {container}")
56 # wait 2s for container to start and network to be up
57 time.sleep(2)
58
59 # Since "start" returns after the container has reached
60 # multi-user.target, we should now be able to access it.
61
62 ip = get_ip(container)
63 with subtest(f"{container} reacts to pings and HTTP requests"):
64 machine.succeed(f"ping -n -c1 {ip}")
65 machine.succeed(curl_host(ip))
66
67 with subtest(f"Stop container {container}"):
68 machine.succeed(f"nixos-container stop {container}")
69 machine.fail(curl_host(ip))
70
71 # Destroying a declarative container should fail.
72 machine.fail(f"nixos-container destroy {container}")
73 '';
74})