git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #162582 from JJJollyjim/cntr-test

nixos/tests: fix flaky cntr test

commitd34f7085a90431b36547226735905f0bb69cbafa authorJörg Thalheim <Mic92@users.noreply.github.com> dateThu 3 Mar 2022, 10:40 (5 years ago) parent0f0fec6126ce87
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 {