2019-08-18 20:37:38 +01:00
|
|
|
# Test for NixOS' container support.
|
|
|
|
|
2019-11-25 20:57:27 +00:00
|
|
|
import ./make-test-python.nix ({ pkgs, ...} : {
|
2019-08-18 20:37:38 +01:00
|
|
|
name = "containers-ephemeral";
|
|
|
|
|
|
|
|
machine = { pkgs, ... }: {
|
|
|
|
virtualisation.memorySize = 768;
|
|
|
|
virtualisation.writableStore = true;
|
|
|
|
|
|
|
|
containers.webserver = {
|
|
|
|
ephemeral = true;
|
|
|
|
privateNetwork = true;
|
|
|
|
hostAddress = "10.231.136.1";
|
|
|
|
localAddress = "10.231.136.2";
|
|
|
|
config = {
|
|
|
|
services.nginx = {
|
|
|
|
enable = true;
|
|
|
|
virtualHosts.localhost = {
|
2019-11-25 20:57:27 +00:00
|
|
|
root = pkgs.runCommand "localhost" {} ''
|
2019-08-18 20:37:38 +01:00
|
|
|
mkdir "$out"
|
|
|
|
echo hello world > "$out/index.html"
|
2019-11-25 20:57:27 +00:00
|
|
|
'';
|
2019-08-18 20:37:38 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
networking.firewall.allowedTCPPorts = [ 80 ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testScript = ''
|
2019-11-25 20:57:27 +00:00
|
|
|
assert "webserver" in machine.succeed("nixos-container list")
|
2019-08-18 20:37:38 +01:00
|
|
|
|
2019-11-25 20:57:27 +00:00
|
|
|
machine.succeed("nixos-container start webserver")
|
2019-08-18 20:37:38 +01:00
|
|
|
|
2019-11-25 20:57:27 +00:00
|
|
|
with subtest("Container got its own root folder"):
|
|
|
|
machine.succeed("ls /run/containers/webserver")
|
2019-08-18 20:37:38 +01:00
|
|
|
|
2019-11-25 20:57:27 +00:00
|
|
|
with subtest("Container persistent directory is not created"):
|
|
|
|
machine.fail("ls /var/lib/containers/webserver")
|
2019-08-18 20:37:38 +01:00
|
|
|
|
|
|
|
# Since "start" returns after the container has reached
|
|
|
|
# multi-user.target, we should now be able to access it.
|
2019-11-25 20:57:27 +00:00
|
|
|
ip = machine.succeed("nixos-container show-ip webserver").rstrip()
|
|
|
|
machine.succeed(f"ping -n -c1 {ip}")
|
|
|
|
machine.succeed(f"curl --fail http://{ip}/ > /dev/null")
|
2019-08-18 20:37:38 +01:00
|
|
|
|
2019-11-25 20:57:27 +00:00
|
|
|
with subtest("Stop the container"):
|
|
|
|
machine.succeed("nixos-container stop webserver")
|
|
|
|
machine.fail(f"curl --fail --connect-timeout 2 http://{ip}/ > /dev/null")
|
2019-08-18 20:37:38 +01:00
|
|
|
|
2019-11-25 20:57:27 +00:00
|
|
|
with subtest("Container's root folder was removed"):
|
|
|
|
machine.fail("ls /run/containers/webserver")
|
2019-08-18 20:37:38 +01:00
|
|
|
'';
|
|
|
|
})
|