git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/tests: fix flaky cntr test

The cntr sometimes hangs until the 10-hour hydra limit. This behaviour appears to be an edge-case related to the type of TTY in which the cntr command runs during test execution. We can work around this by running the command as a background job. I additionally added a wait_for_open_port to fix nondeterministic test failures I observed after fixing the hanging issue.

commit126ce87b0c7e526c8d1f2eccd868e89ef61deab3 authorJamie McClymont <jamie@kwiius.com> dateThu 3 Mar 2022, 19:01 (5 years ago) parent2f7ef97
1 file changed+16−4
nixos/tests/cntr.nix+16−4
nixos/tests/cntr.nix+16−4
@@ -28,10 +28,16 @@ let
2828 testScript = ''
2929 start_all()
3030 ${backend}.wait_for_unit("${backend}-nginx.service")
31− result = ${backend}.wait_until_succeeds(
32− "cntr attach -t ${backend} nginx sh -- -c 'curl localhost | grep Hello'"
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
34+ ${backend}.execute(
35+ "(cntr attach -t ${backend} nginx sh -- -c 'curl localhost | grep Hello' > /tmp/result; echo $? > /tmp/exitcode; touch /tmp/done) &"
3336 )
34− assert "Hello" in result
37+
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"
3541 '';
3642 };
3743
@@ -54,7 +60,13 @@ let
5460 testScript = ''
5561 machine.start()
5662 machine.wait_for_unit("container@test.service")
57− machine.succeed("cntr attach test sh -- -c 'ping -c5 172.16.0.1'")
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
64+ machine.execute(
65+ "(cntr attach test sh -- -c 'ping -c5 172.16.0.1'; echo $? > /tmp/exitcode; touch /tmp/done) &"
66+ )
67+
68+ machine.wait_for_file("/tmp/done")
69+ assert "0" == machine.succeed("cat /tmp/exitcode").strip(), "non-zero exit code"
5870 '';
5971 };
6072in {