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