2 { system ? builtins.currentSystem, config ? { }
3 , pkgs ? import ../.. { inherit system config; }, lib ? pkgs.lib }:
6 inherit (import ../lib/testing-python.nix { inherit system pkgs; }) makeTest;
10 name = "cntr-${backend}";
12 meta = { maintainers = with lib.maintainers; [ sorki mic92 ]; };
15 ${backend} = { pkgs, ... }: {
16 environment.systemPackages = [ pkgs.cntr ];
17 virtualisation.oci-containers = {
20 image = "nginx-container";
21 imageFile = pkgs.dockerTools.examples.nginx;
22 ports = [ "8181:80" ];
30 ${backend}.wait_for_unit("${backend}-nginx.service")
31 ${backend}.wait_for_open_port(8181)
32 # For some reason, the cntr command hangs when run without the &.
33 # As such, we have to do some messy things to ensure we check the exitcode and output in a race-condition-safe manner
35 "(cntr attach -t ${backend} nginx sh -- -c 'curl localhost | grep Hello' > /tmp/result; echo $? > /tmp/exitcode; touch /tmp/done) &"
38 ${backend}.wait_for_file("/tmp/done")
39 assert "0" == ${backend}.succeed("cat /tmp/exitcode").strip(), "non-zero exit code"
40 assert "Hello" in ${backend}.succeed("cat /tmp/result"), "no greeting in output"
44 mkContainersTest = makeTest {
45 name = "cntr-containers";
47 meta = with pkgs.lib.maintainers; { maintainers = [ sorki mic92 ]; };
49 nodes.machine = { lib, ... }: {
50 environment.systemPackages = [ pkgs.cntr ];
53 privateNetwork = true;
54 hostAddress = "172.16.0.1";
55 localAddress = "172.16.0.2";
62 machine.wait_for_unit("container@test.service")
63 # I haven't observed the same hanging behaviour in this version as in the OCI version which necessetates this messy invocation, but it's probably better to be safe than sorry and use it here as well
65 "(cntr attach test sh -- -c 'ping -c5 172.16.0.1'; echo $? > /tmp/exitcode; touch /tmp/done) &"
68 machine.wait_for_file("/tmp/done")
69 assert "0" == machine.succeed("cat /tmp/exitcode").strip(), "non-zero exit code"
73 nixos-container = mkContainersTest;
74 } // (lib.foldl' (attrs: backend: attrs // { ${backend} = mkOCITest backend; })
75 { } [ "docker" "podman" ])