Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions test/system/libs/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ function _setup_docker_registry() {
"${IMAGES[docker-reg]}"
assert_success

_wait_for_docker_registry

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To ensure robust error handling and maintain consistency with the rest of the _setup_docker_registry function (where all commands are executed using BATS' run and verified with assert_success), we should execute _wait_for_docker_registry using run and assert its success. This guarantees that if the registry fails to reach a ready state, the test suite setup will fail immediately and cleanly with a clear error report.

  run _wait_for_docker_registry
  assert_success

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intentional.

First, using only run and assert_success is an anti-pattern. We shouldn't be doing that.

Second, until recently run --separate-stderr didn't work or had undefined behaviour inside setup_suite() because BATS_TEST_TMPDIR isn't defined. Using run in this function used in setup_suite() takes us closer to using some feature of run that doesn't work.


run podman login \
--authfile "${BATS_SUITE_TMPDIR}/authfile.json" \
--username user \
Expand All @@ -223,6 +225,53 @@ function _setup_docker_registry() {
}


function _wait_for_docker_registry() {
local error_details
local error_message
local -i j
local -i num_of_retries=30

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should be max_retries, and j could be just retries or num_retries?

local -i ret_val=1

for ((j = 0; j < num_of_retries; j++)); do
ret_val=0
error_details="$( (timeout 5s openssl s_client \
-CAfile "$DOCKER_REG_CERTS_DIR/domain.crt" \
-connect "$DOCKER_REG_URI" </dev/null >/dev/null) 2>&1)" || ret_val="$?"
Comment on lines +237 to +239

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The double subshell ( ... ) used to capture only stderr can be simplified by using standard redirection order 2>&1 >/dev/null. This avoids spawning an extra subshell process while achieving the exact same result.

    error_details="$(timeout 5s openssl s_client \
                                    -CAfile "$DOCKER_REG_CERTS_DIR/domain.crt" \
                                    -connect "$DOCKER_REG_URI" </dev/null 2>&1 >/dev/null)" || ret_val="$?"


if [ "$ret_val" -eq 0 ]; then

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is a little easier to read as a case statement? Something like

case "$ret_val" in
  0)
    break
    ;;
  125)
    error_message="timeout(1) failed"
    break
    ;;
  126)
    error_message="Failed to invoke openssl(1) or timeout(1)"
    break
    ;;
  127)
    error_message="openssl(1) not found"
    break
    ;;
  137)
    error_message="SIGKILL received by openssl(1) or timeout(1)"
    break
    ;;
esac

WDYT?

break
elif [ "$ret_val" -eq 125 ]; then
error_message="timeout(1) failed"
break
elif [ "$ret_val" -eq 126 ]; then
error_message="Failed to invoke openssl(1) or timeout(1)"
break
elif [ "$ret_val" -eq 127 ]; then
error_message="openssl(1) not found"
break
elif [ "$ret_val" -eq 137 ]; then
error_message="SIGKILL received by openssl(1) or timeout(1)"
break
fi

sleep 1
done

if [ "$ret_val" -ne 0 ]; then
if [ "$j" -eq "$num_of_retries" ]; then
error_message="Docker registry at $DOCKER_REG_URI did not reach a ready state"
fi

echo "$error_message" >&2
if [ "$error_details" != "" ]; then
echo "$error_details" >&2
fi
fi

return "$ret_val"
}


# Stop, removes and cleans after a locally hosted Docker registry
function _clean_docker_registry() {
# Stop Docker registry container
Expand Down
Loading