2017-02-03 23:50:02 +03:00
|
|
|
let
|
|
|
|
hostIp = "192.168.0.1";
|
|
|
|
hostPort = 10080;
|
|
|
|
containerIp = "192.168.0.100";
|
|
|
|
containerPort = 80;
|
2020-08-02 12:25:04 +03:00
|
|
|
in
|
2017-02-03 23:50:02 +03:00
|
|
|
|
2021-02-26 18:03:49 +03:00
|
|
|
import ./make-test-python.nix ({ pkgs, lib, ... }: {
|
2017-02-03 23:50:02 +03:00
|
|
|
name = "containers-portforward";
|
2021-02-26 18:03:49 +03:00
|
|
|
meta = {
|
2024-04-21 10:15:22 +03:00
|
|
|
maintainers = with lib.maintainers; [ aristid aszlig kampfschlaefer ianwookim ];
|
2017-02-03 23:50:02 +03:00
|
|
|
};
|
|
|
|
|
2022-03-21 02:15:30 +03:00
|
|
|
nodes.machine =
|
2018-07-20 23:56:59 +03:00
|
|
|
{ pkgs, ... }:
|
2017-02-03 23:50:02 +03:00
|
|
|
{ imports = [ ../modules/installer/cd-dvd/channel.nix ];
|
|
|
|
virtualisation.writableStore = true;
|
|
|
|
|
|
|
|
containers.webserver =
|
|
|
|
{ privateNetwork = true;
|
|
|
|
hostAddress = hostIp;
|
|
|
|
localAddress = containerIp;
|
|
|
|
forwardPorts = [ { protocol = "tcp"; hostPort = hostPort; containerPort = containerPort; } ];
|
|
|
|
config =
|
|
|
|
{ services.httpd.enable = true;
|
|
|
|
services.httpd.adminAddr = "foo@example.org";
|
|
|
|
networking.firewall.allowedTCPPorts = [ 80 ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-10-06 20:19:29 +03:00
|
|
|
virtualisation.additionalPaths = [ pkgs.stdenv ];
|
2017-02-03 23:50:02 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
testScript =
|
|
|
|
''
|
2019-12-01 04:29:24 +03:00
|
|
|
container_list = machine.succeed("nixos-container list")
|
|
|
|
assert "webserver" in container_list
|
2017-02-03 23:50:02 +03:00
|
|
|
|
|
|
|
# Start the webserver container.
|
2019-12-01 04:29:24 +03:00
|
|
|
machine.succeed("nixos-container start webserver")
|
2017-02-03 23:50:02 +03:00
|
|
|
|
|
|
|
# wait two seconds for the container to start and the network to be up
|
2019-12-01 04:29:24 +03:00
|
|
|
machine.sleep(2)
|
2017-02-03 23:50:02 +03:00
|
|
|
|
|
|
|
# Since "start" returns after the container has reached
|
|
|
|
# multi-user.target, we should now be able to access it.
|
2019-12-01 04:29:24 +03:00
|
|
|
# ip = machine.succeed("nixos-container show-ip webserver").strip()
|
|
|
|
machine.succeed("ping -n -c1 ${hostIp}")
|
|
|
|
machine.succeed("curl --fail http://${hostIp}:${toString hostPort}/ > /dev/null")
|
2017-02-03 23:50:02 +03:00
|
|
|
|
|
|
|
# Stop the container.
|
2019-12-01 04:29:24 +03:00
|
|
|
machine.succeed("nixos-container stop webserver")
|
|
|
|
machine.fail("curl --fail --connect-timeout 2 http://${hostIp}:${toString hostPort}/ > /dev/null")
|
2017-02-03 23:50:02 +03:00
|
|
|
|
|
|
|
# Destroying a declarative container should fail.
|
2019-12-01 04:29:24 +03:00
|
|
|
machine.fail("nixos-container destroy webserver")
|
2017-02-03 23:50:02 +03:00
|
|
|
'';
|
|
|
|
|
|
|
|
})
|