From e1745ef9c27710d3c6d4d1292c26d9f91134dc22 Mon Sep 17 00:00:00 2001 From: Debarshi Ray Date: Fri, 22 Sep 2023 20:14:28 +0200 Subject: [PATCH] test/system: Ensure failure if an invalid distribution is specified Contrary to what the documentation might seem to imply [1], Bats' 'fail' helper only aborts a test case under certain circumstances. eg., when called from setup_suite(), but not from within a child function, and a @test case, but not from within the 'run' helper. If 'fail' is called from within 'run', then the code after it will continue to execute. The test case will only fail if 'run' eventually catches a non-zero exit code that's caught by 'assert_success' [2]. Similarly, it doesn't abort if called from within a child function in setup_suite(). Currently, _pull_and_cache_distro_image() is a child function called from setup_suite(). So 'fail' won't abort if an invalid distribution is specified. Fortunately, pull_distro_image() is being called from within @test cases, but outside 'run'. So, there's no problem with it now. However, some future code changes can unknowingly alter this reality and it too can run into unexpected behaviour. Therefore, it's better to be safe, and explicitly specify a non-zero exit code after 'fail'. It will ensure that it works as expected under all circumstances. [1] https://github.com/bats-core/bats-support [2] https://github.com/bats-core/bats-assert https://github.com/containers/toolbox/pull/1375 --- test/system/libs/helpers.bash | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/system/libs/helpers.bash b/test/system/libs/helpers.bash index 7283799..debc8fb 100644 --- a/test/system/libs/helpers.bash +++ b/test/system/libs/helpers.bash @@ -88,6 +88,7 @@ function _pull_and_cache_distro_image() { if [ -z "${IMAGES[$distro]+x}" ]; then fail "Requested distro (${distro}) does not have a matching image" + return 1 fi image="${IMAGES[$distro]}" @@ -309,6 +310,7 @@ function pull_distro_image() { if [ -z "${IMAGES[$distro]+x}" ]; then fail "Requested distro (${distro}) does not have a matching image" + return 1 fi image="${IMAGES[$distro]}"