Merge pull request #74196 from tfc/nixos-test-containers-python-port

nixosTests.containers*: port to python
This commit is contained in:
Florian Klink 2019-11-25 22:24:30 +01:00 committed by GitHub
commit 19ba87f9fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 93 additions and 74 deletions

View File

@ -1,6 +1,6 @@
# Test for NixOS' container support. # Test for NixOS' container support.
import ./make-test.nix ({ pkgs, ...} : { import ./make-test-python.nix ({ pkgs, ...} : {
name = "containers-ephemeral"; name = "containers-ephemeral";
machine = { pkgs, ... }: { machine = { pkgs, ... }: {
@ -16,10 +16,10 @@ import ./make-test.nix ({ pkgs, ...} : {
services.nginx = { services.nginx = {
enable = true; enable = true;
virtualHosts.localhost = { virtualHosts.localhost = {
root = (pkgs.runCommand "localhost" {} '' root = pkgs.runCommand "localhost" {} ''
mkdir "$out" mkdir "$out"
echo hello world > "$out/index.html" echo hello world > "$out/index.html"
''); '';
}; };
}; };
networking.firewall.allowedTCPPorts = [ 80 ]; networking.firewall.allowedTCPPorts = [ 80 ];
@ -28,29 +28,27 @@ import ./make-test.nix ({ pkgs, ...} : {
}; };
testScript = '' testScript = ''
$machine->succeed("nixos-container list") =~ /webserver/ or die; assert "webserver" in machine.succeed("nixos-container list")
# Start the webserver container. machine.succeed("nixos-container start webserver")
$machine->succeed("nixos-container start webserver");
# Check that container got its own root folder with subtest("Container got its own root folder"):
$machine->succeed("ls /run/containers/webserver"); machine.succeed("ls /run/containers/webserver")
# Check that container persistent directory is not created with subtest("Container persistent directory is not created"):
$machine->fail("ls /var/lib/containers/webserver"); machine.fail("ls /var/lib/containers/webserver")
# Since "start" returns after the container has reached # Since "start" returns after the container has reached
# multi-user.target, we should now be able to access it. # multi-user.target, we should now be able to access it.
my $ip = $machine->succeed("nixos-container show-ip webserver"); ip = machine.succeed("nixos-container show-ip webserver").rstrip()
chomp $ip; machine.succeed(f"ping -n -c1 {ip}")
$machine->succeed("ping -n -c1 $ip"); machine.succeed(f"curl --fail http://{ip}/ > /dev/null")
$machine->succeed("curl --fail http://$ip/ > /dev/null");
# Stop the container. with subtest("Stop the container"):
$machine->succeed("nixos-container stop webserver"); machine.succeed("nixos-container stop webserver")
$machine->fail("curl --fail --connect-timeout 2 http://$ip/ > /dev/null"); machine.fail(f"curl --fail --connect-timeout 2 http://{ip}/ > /dev/null")
# Check that container's root folder was removed with subtest("Container's root folder was removed"):
$machine->fail("ls /run/containers/webserver"); machine.fail("ls /run/containers/webserver")
''; '';
}) })

View File

@ -1,6 +1,6 @@
# Test for NixOS' container support. # Test for NixOS' container support.
import ./make-test.nix ({ pkgs, ...} : { import ./make-test-python.nix ({ pkgs, ...} : {
name = "containers-hosts"; name = "containers-hosts";
meta = with pkgs.stdenv.lib.maintainers; { meta = with pkgs.stdenv.lib.maintainers; {
maintainers = [ montag451 ]; maintainers = [ montag451 ];
@ -42,11 +42,11 @@ import ./make-test.nix ({ pkgs, ...} : {
}; };
testScript = '' testScript = ''
startAll; start_all()
$machine->waitForUnit("default.target"); machine.wait_for_unit("default.target")
# Ping the containers using the entries added in /etc/hosts with subtest("Ping the containers using the entries added in /etc/hosts"):
$machine->succeed("ping -n -c 1 simple.containers"); for host in "simple.containers", "netmask.containers":
$machine->succeed("ping -n -c 1 netmask.containers"); machine.succeed(f"ping -n -c 1 {host}")
''; '';
}) })

View File

@ -1,7 +1,7 @@
import ./make-test.nix ({ pkgs, lib, ...} : import ./make-test-python.nix ({ pkgs, lib, ...} :
let let
client_base = { client_base = {
containers.test1 = { containers.test1 = {
autoStart = true; autoStart = true;
config = { config = {
@ -48,18 +48,25 @@ in {
c1System = nodes.client_c1.config.system.build.toplevel; c1System = nodes.client_c1.config.system.build.toplevel;
c2System = nodes.client_c2.config.system.build.toplevel; c2System = nodes.client_c2.config.system.build.toplevel;
in '' in ''
$client->start(); client.start()
$client->waitForUnit("default.target"); client.wait_for_unit("default.target")
$client->succeed("[[ \$(nixos-container run test1 cat /etc/check) == client_base ]] >&2");
$client->succeed("${c1System}/bin/switch-to-configuration test >&2"); assert "client_base" in client.succeed("nixos-container run test1 cat /etc/check")
$client->succeed("[[ \$(nixos-container run test1 cat /etc/check) == client_c1 ]] >&2");
$client->succeed("systemctl status httpd -M test1 >&2");
$client->succeed("${c2System}/bin/switch-to-configuration test >&2"); with subtest("httpd is available after activating config1"):
$client->succeed("[[ \$(nixos-container run test1 cat /etc/check) == client_c2 ]] >&2"); client.succeed(
$client->fail("systemctl status httpd -M test1 >&2"); "${c1System}/bin/switch-to-configuration test >&2",
$client->succeed("systemctl status nginx -M test1 >&2"); "[[ $(nixos-container run test1 cat /etc/check) == client_c1 ]] >&2",
"systemctl status httpd -M test1 >&2",
)
with subtest("httpd is not available any longer after switching to config2"):
client.succeed(
"${c2System}/bin/switch-to-configuration test >&2",
"[[ $(nixos-container run test1 cat /etc/check) == client_c2 ]] >&2",
"systemctl status nginx -M test1 >&2",
)
client.fail("systemctl status httpd -M test1 >&2")
''; '';
}) })

View File

@ -1,6 +1,6 @@
# Test for NixOS' container support. # Test for NixOS' container support.
import ./make-test.nix ({ pkgs, ...} : { import ./make-test-python.nix ({ pkgs, ...} : {
name = "containers-tmpfs"; name = "containers-tmpfs";
meta = with pkgs.stdenv.lib.maintainers; { meta = with pkgs.stdenv.lib.maintainers; {
maintainers = [ kampka ]; maintainers = [ kampka ];
@ -31,49 +31,63 @@ import ./make-test.nix ({ pkgs, ...} : {
virtualisation.pathsInNixDB = [ pkgs.stdenv ]; virtualisation.pathsInNixDB = [ pkgs.stdenv ];
}; };
testScript = testScript = ''
'' machine.wait_for_unit("default.target")
$machine->waitForUnit("default.target"); assert "tmpfs" in machine.succeed("nixos-container list")
$machine->succeed("nixos-container list") =~ /tmpfs/ or die;
# Start the tmpfs container. with subtest("tmpfs container is up"):
#$machine->succeed("nixos-container status tmpfs") =~ /up/ or die; assert "up" in machine.succeed("nixos-container status tmpfs")
# Verify that /var is mounted as a tmpfs
#$machine->succeed("nixos-container run tmpfs -- systemctl status var.mount --no-pager 2>/dev/null") =~ /What: tmpfs/ or die;
$machine->succeed("nixos-container run tmpfs -- mountpoint -q /var 2>/dev/null");
# Verify that /var/log is mounted as a tmpfs
$machine->succeed("nixos-container run tmpfs -- systemctl status var-log.mount --no-pager 2>/dev/null") =~ /What: tmpfs/ or die;
$machine->succeed("nixos-container run tmpfs -- mountpoint -q /var/log 2>/dev/null");
# Verify that /some/random/path is mounted as a tmpfs
$machine->succeed("nixos-container run tmpfs -- systemctl status some-random-path.mount --no-pager 2>/dev/null") =~ /What: tmpfs/ or die;
$machine->succeed("nixos-container run tmpfs -- mountpoint -q /some/random/path 2>/dev/null");
# Verify that files created in the container in a non-tmpfs directory are visible on the host.
# This establishes legitimacy for the following tests
$machine->succeed("nixos-container run tmpfs -- touch /root/test.file 2>/dev/null");
$machine->succeed("nixos-container run tmpfs -- ls -l /root | grep -q test.file 2>/dev/null");
$machine->succeed("test -e /var/lib/containers/tmpfs/root/test.file");
# Verify that /some/random/path is writable and that files created there def tmpfs_cmd(command):
# are not in the hosts container dir but in the tmpfs return f"nixos-container run tmpfs -- {command} 2>/dev/null"
$machine->succeed("nixos-container run tmpfs -- touch /some/random/path/test.file 2>/dev/null");
$machine->succeed("nixos-container run tmpfs -- test -e /some/random/path/test.file 2>/dev/null");
$machine->fail("test -e /var/lib/containers/tmpfs/some/random/path/test.file");
# Verify that files created in the hosts container dir in a path where a tmpfs file system has been mounted with subtest("/var is mounted as a tmpfs"):
# are not visible to the container as the do not exist in the tmpfs machine.succeed(tmpfs_cmd("mountpoint -q /var"))
$machine->succeed("touch /var/lib/containers/tmpfs/var/test.file");
$machine->succeed("test -e /var/lib/containers/tmpfs/var/test.file"); with subtest("/var/log is mounted as a tmpfs"):
$machine->succeed("ls -l /var/lib/containers/tmpfs/var/ | grep -q test.file 2>/dev/null"); assert "What: tmpfs" in machine.succeed(
tmpfs_cmd("systemctl status var-log.mount --no-pager")
)
machine.succeed(tmpfs_cmd("mountpoint -q /var/log"))
$machine->fail("nixos-container run tmpfs -- ls -l /var | grep -q test.file 2>/dev/null"); with subtest("/some/random/path is mounted as a tmpfs"):
assert "What: tmpfs" in machine.succeed(
tmpfs_cmd("systemctl status some-random-path.mount --no-pager")
)
machine.succeed(tmpfs_cmd("mountpoint -q /some/random/path"))
with subtest(
"files created in the container in a non-tmpfs directory are visible on the host."
):
# This establishes legitimacy for the following tests
machine.succeed(
tmpfs_cmd("touch /root/test.file"),
tmpfs_cmd("ls -l /root | grep -q test.file"),
"test -e /var/lib/containers/tmpfs/root/test.file",
)
with subtest(
"/some/random/path is writable and that files created there are not "
+ "in the hosts container dir but in the tmpfs"
):
machine.succeed(
tmpfs_cmd("touch /some/random/path/test.file"),
tmpfs_cmd("test -e /some/random/path/test.file"),
)
machine.fail("test -e /var/lib/containers/tmpfs/some/random/path/test.file")
with subtest(
"files created in the hosts container dir in a path where a tmpfs "
+ "file system has been mounted are not visible to the container as "
+ "the do not exist in the tmpfs"
):
machine.succeed(
"touch /var/lib/containers/tmpfs/var/test.file",
"test -e /var/lib/containers/tmpfs/var/test.file",
"ls -l /var/lib/containers/tmpfs/var/ | grep -q test.file 2>/dev/null",
)
machine.fail(tmpfs_cmd("ls -l /var | grep -q test.file"))
''; '';
}) })