1let
2 hostIp = "192.168.0.1";
3 hostPort = 10080;
4 containerIp = "192.168.0.100";
5 containerPort = 80;
6in
7
8import ./make-test-python.nix (
9 { pkgs, lib, ... }:
10 {
11 name = "containers-portforward";
12 meta = {
13 maintainers = with lib.maintainers; [
14 aristid
15 aszlig
16 kampfschlaefer
17 ianwookim
18 ];
19 };
20
21 nodes.machine =
22 { pkgs, ... }:
23 {
24 imports = [ ../modules/installer/cd-dvd/channel.nix ];
25 virtualisation.writableStore = true;
26
27 containers.webserver = {
28 privateNetwork = true;
29 hostAddress = hostIp;
30 localAddress = containerIp;
31 forwardPorts = [
32 {
33 protocol = "tcp";
34 hostPort = hostPort;
35 containerPort = containerPort;
36 }
37 ];
38 config = {
39 services.httpd.enable = true;
40 services.httpd.adminAddr = "foo@example.org";
41 networking.firewall.allowedTCPPorts = [ 80 ];
42 };
43 };
44
45 virtualisation.additionalPaths = [ pkgs.stdenv ];
46 };
47
48 testScript = ''
49 container_list = machine.succeed("nixos-container list")
50 assert "webserver" in container_list
51
52 # Start the webserver container.
53 machine.succeed("nixos-container start webserver")
54
55 # wait two seconds for the container to start and the network to be up
56 machine.sleep(2)
57
58 # Since "start" returns after the container has reached
59 # multi-user.target, we should now be able to access it.
60 # ip = machine.succeed("nixos-container show-ip webserver").strip()
61 machine.succeed("ping -n -c1 ${hostIp}")
62 machine.succeed("curl --fail http://${hostIp}:${toString hostPort}/ > /dev/null")
63
64 # Stop the container.
65 machine.succeed("nixos-container stop webserver")
66 machine.fail("curl --fail --connect-timeout 2 http://${hostIp}:${toString hostPort}/ > /dev/null")
67
68 # Destroying a declarative container should fail.
69 machine.fail("nixos-container destroy webserver")
70 '';
71
72 }
73)