2017-02-03 20:50:02 +00:00
|
|
|
let
|
|
|
|
hostIp = "192.168.0.1";
|
|
|
|
hostPort = 10080;
|
|
|
|
containerIp = "192.168.0.100";
|
|
|
|
containerPort = 80;
|
2020-08-02 09:25:04 +00:00
|
|
|
in
|
2017-02-03 20:50:02 +00:00
|
|
|
|
2021-02-26 15:03:49 +00:00
|
|
|
import ./make-test-python.nix ({ pkgs, lib, ... }: {
|
2017-02-03 20:50:02 +00:00
|
|
|
name = "containers-portforward";
|
2021-02-26 15:03:49 +00:00
|
|
|
meta = {
|
|
|
|
maintainers = with lib.maintainers; [ aristid aszlig eelco kampfschlaefer ianwookim ];
|
2017-02-03 20:50:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
machine =
|
2018-07-20 20:56:59 +00:00
|
|
|
{ pkgs, ... }:
|
2017-02-03 20:50:02 +00: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 17:19:29 +00:00
|
|
|
virtualisation.additionalPaths = [ pkgs.stdenv ];
|
2017-02-03 20:50:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
testScript =
|
|
|
|
''
|
2019-12-01 01:29:24 +00:00
|
|
|
container_list = machine.succeed("nixos-container list")
|
|
|
|
assert "webserver" in container_list
|
2017-02-03 20:50:02 +00:00
|
|
|
|
|
|
|
# Start the webserver container.
|
2019-12-01 01:29:24 +00:00
|
|
|
machine.succeed("nixos-container start webserver")
|
2017-02-03 20:50:02 +00:00
|
|
|
|
|
|
|
# wait two seconds for the container to start and the network to be up
|
2019-12-01 01:29:24 +00:00
|
|
|
machine.sleep(2)
|
2017-02-03 20:50:02 +00:00
|
|
|
|
|
|
|
# Since "start" returns after the container has reached
|
|
|
|
# multi-user.target, we should now be able to access it.
|
2019-12-01 01:29:24 +00: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 20:50:02 +00:00
|
|
|
|
|
|
|
# Stop the container.
|
2019-12-01 01:29:24 +00:00
|
|
|
machine.succeed("nixos-container stop webserver")
|
|
|
|
machine.fail("curl --fail --connect-timeout 2 http://${hostIp}:${toString hostPort}/ > /dev/null")
|
2017-02-03 20:50:02 +00:00
|
|
|
|
|
|
|
# Destroying a declarative container should fail.
|
2019-12-01 01:29:24 +00:00
|
|
|
machine.fail("nixos-container destroy webserver")
|
2017-02-03 20:50:02 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
})
|