1# Test for cntr tool
2
3{ runTest, lib }:
4
5let
6 mkOCITest =
7 backend:
8 runTest {
9 name = "cntr-${backend}";
10
11 meta.maintainers = with lib.maintainers; [
12 sorki
13 mic92
14 ];
15
16 nodes.${backend} =
17 { pkgs, ... }:
18 {
19 environment.systemPackages = [ pkgs.cntr ];
20 virtualisation.oci-containers = {
21 inherit backend;
22 containers.nginx = {
23 image = "nginx-container";
24 imageStream = pkgs.dockerTools.examples.nginxStream;
25 ports = [ "8181:80" ];
26 };
27 };
28 };
29
30 testScript = ''
31 start_all()
32 ${backend}.wait_for_unit("${backend}-nginx.service")
33 ${backend}.wait_for_open_port(8181)
34 # For some reason, the cntr command hangs when run without the &.
35 # As such, we have to do some messy things to ensure we check the exitcode and output in a race-condition-safe manner
36 ${backend}.execute(
37 "(cntr attach -t ${backend} nginx sh -- -c 'curl localhost | grep Hello' > /tmp/result; echo $? > /tmp/exitcode; touch /tmp/done) &"
38 )
39
40 ${backend}.wait_for_file("/tmp/done")
41 assert "0" == ${backend}.succeed("cat /tmp/exitcode").strip(), "non-zero exit code"
42 assert "Hello" in ${backend}.succeed("cat /tmp/result"), "no greeting in output"
43 '';
44 };
45
46 mkContainersTest = runTest {
47 name = "cntr-containers";
48
49 meta.maintainers = with lib.maintainers; [
50 sorki
51 mic92
52 ];
53
54 nodes.machine =
55 { pkgs, ... }:
56 {
57 environment.systemPackages = [ pkgs.cntr ];
58 containers.test = {
59 autoStart = true;
60 privateNetwork = true;
61 hostAddress = "172.16.0.1";
62 localAddress = "172.16.0.2";
63 config = { };
64 };
65 };
66
67 testScript = ''
68 machine.start()
69 machine.wait_for_unit("container@test.service")
70 # 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
71 machine.execute(
72 "(cntr attach test sh -- -c 'ping -c5 172.16.0.1'; echo $? > /tmp/exitcode; touch /tmp/done) &"
73 )
74
75 machine.wait_for_file("/tmp/done")
76 assert "0" == machine.succeed("cat /tmp/exitcode").strip(), "non-zero exit code"
77 '';
78 };
79in
80{
81 nixos-container = mkContainersTest;
82}
83// (lib.genAttrs [ "docker" "podman" ] mkOCITest)