tests: Fail fast in assert_pod_fail()

`assert_pod_fail()` currently calls `k8s_create_pod()` to ensure that a pod
does not become ready within the default 120s. However, this delays the test's
completion even if an error message is detected earlier in the journal.

This commit removes the use of `k8s_create_pod()` and modifies `assert_pod_fail()`
to fail as soon as the pod enters a failed state.

All failing pods end up in one of the following states:

- CrashLoopBackOff
- ImagePullBackOff

The function now polls the pod's state every 5 seconds to check for these conditions.
If the pod enters a failed state, the function immediately returns 0. If the pod
does not reach a failed state within 120 seconds, it returns 1.

Signed-off-by: Hyounggyu Choi <Hyounggyu.Choi@ibm.com>
This commit is contained in:
Hyounggyu Choi 2024-09-24 14:33:44 +02:00
parent e738054ddb
commit 2c2941122c

View File

@ -90,15 +90,39 @@ assert_logs_contain() {
# #
# Parameters: # Parameters:
# $1 - the pod configuration file. # $1 - the pod configuration file.
# $2 - the duration to wait for the container to fail. Defaults to 120. (optional)
# #
assert_pod_fail() { assert_pod_fail() {
local container_config="$1" local container_config="$1"
local duration="${2:-120}"
echo "In assert_pod_fail: $container_config" echo "In assert_pod_fail: $container_config"
echo "Attempt to create the container but it should fail" echo "Attempt to create the container but it should fail"
! k8s_create_pod "$container_config" || /bin/false
}
kubectl apply -f "${container_config}"
if ! pod_name=$(kubectl get pods -o jsonpath='{.items..metadata.name}'); then
echo "Failed to create the pod"
return 1
fi
local elapsed_time=0
local sleep_time=5
while true; do
echo "Waiting for a container to fail"
sleep ${sleep_time}
elapsed_time=$((elapsed_time+sleep_time))
if [[ $(kubectl get pod "${pod_name}" \
-o jsonpath='{.status.containerStatuses[0].state.waiting.reason}') = *BackOff* ]]; then
return 0
fi
if [ $elapsed_time -gt $duration ]; then
echo "The container does not get into a failing state" >&2
break
fi
done
return 1
}
# Check the pulled rootfs on host for given node and sandbox_id # Check the pulled rootfs on host for given node and sandbox_id
# #