Merge pull request #10164 from BbolroC/make-exec_host-stable

tests: Ensure exec_host() consistently captures command output
This commit is contained in:
Aurélien Bombo 2024-08-15 21:43:32 -07:00 committed by GitHub
commit e1775e4719
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 27 deletions

View File

@ -8,10 +8,8 @@
load "${BATS_TEST_DIRNAME}/../../common.bash" load "${BATS_TEST_DIRNAME}/../../common.bash"
load "${BATS_TEST_DIRNAME}/tests_common.sh" load "${BATS_TEST_DIRNAME}/tests_common.sh"
TEST_INITRD="${TEST_INITRD:-no}" TEST_INITRD="${TEST_INITRD:-no}"
issue="https://github.com/kata-containers/kata-containers/issues/10081"
setup() { setup() {
skip "test not working see: ${issue}"
[ "${KATA_HYPERVISOR}" == "firecracker" ] && skip "test not working see: ${fc_limitations}" [ "${KATA_HYPERVISOR}" == "firecracker" ] && skip "test not working see: ${fc_limitations}"
[ "${KATA_HYPERVISOR}" == "fc" ] && skip "test not working see: ${fc_limitations}" [ "${KATA_HYPERVISOR}" == "fc" ] && skip "test not working see: ${fc_limitations}"
@ -46,7 +44,6 @@ setup() {
} }
@test "Test readonly volume for pods" { @test "Test readonly volume for pods" {
skip "test not working see: ${issue}"
# Create pod # Create pod
kubectl create -f "${test_yaml}" kubectl create -f "${test_yaml}"
@ -59,7 +56,6 @@ setup() {
} }
teardown() { teardown() {
skip "test not working see: ${issue}"
[ "${KATA_HYPERVISOR}" == "firecracker" ] && skip "test not working see: ${fc_limitations}" [ "${KATA_HYPERVISOR}" == "firecracker" ] && skip "test not working see: ${fc_limitations}"
[ "${KATA_HYPERVISOR}" == "fc" ] && skip "test not working see: ${fc_limitations}" [ "${KATA_HYPERVISOR}" == "fc" ] && skip "test not working see: ${fc_limitations}"

View File

@ -76,17 +76,17 @@ get_one_kata_node() {
echo "${resource_name/"node/"}" echo "${resource_name/"node/"}"
} }
# Deletes new_pod it wasn't present in the old_pods array. # Get the new debugger pod that wasn't present in the old_pods array.
delete_pod_if_new() { get_new_debugger_pod() {
declare -r new_pod="$1" local old_pods=("$@")
shift local new_pod_list=($(kubectl get pods -o name | grep node-debugger))
declare -r old_pods=("$@")
for old_pod in "${old_pods[@]}"; do for new_pod in "${new_pod_list[@]}"; do
[ "${old_pod}" == "${new_pod}" ] && return 0 if [[ ! " ${old_pods[*]} " =~ " ${new_pod} " ]]; then
echo "${new_pod}"
return
fi
done done
kubectl delete "${new_pod}" >&2
} }
# Runs a command in the host filesystem. # Runs a command in the host filesystem.
@ -95,14 +95,24 @@ delete_pod_if_new() {
# $1 - the node name # $1 - the node name
# #
exec_host() { exec_host() {
node="$1" local node="$1"
# `kubectl debug` always returns 0, so we hack it to return the right exit code. # `kubectl debug` always returns 0, so we hack it to return the right exit code.
command="${@:2}" local command="${@:2}"
command+='; echo -en \\n$?' command+='; echo -en \\n$?'
# Get the already existing debugger pods. # Get the already existing debugger pods
declare -a old_debugger_pods=( $(kubectl get pods -o name | grep node-debugger) ) local old_debugger_pods=($(kubectl get pods -o name | grep node-debugger))
# Run a debug pod
kubectl debug -q "node/${node}" --image=quay.io/bedrock/ubuntu:latest -- chroot /host bash -c "sleep infinity" >&2
# Identify the new debugger pod
local new_debugger_pod=$(get_new_debugger_pod "${old_debugger_pods[@]}")
# Wait for the newly created pod to be ready
kubectl wait --timeout="30s" --for=condition=ready "${new_debugger_pod}" > /dev/null
# Execute the command and capture the output
# We're trailing the `\r` here due to: https://github.com/kata-containers/kata-containers/issues/8051 # We're trailing the `\r` here due to: https://github.com/kata-containers/kata-containers/issues/8051
# tl;dr: When testing with CRI-O we're facing the following error: # tl;dr: When testing with CRI-O we're facing the following error:
# ``` # ```
@ -112,17 +122,13 @@ exec_host() {
# [bats-exec-test:38] INFO: k8s configured to use runtimeclass # [bats-exec-test:38] INFO: k8s configured to use runtimeclass
# bash: line 1: $'\r': command not found # bash: line 1: $'\r': command not found
# ``` # ```
output="$(kubectl debug -qi "node/${node}" --image=quay.io/bedrock/ubuntu:latest -- chroot /host bash -c "${command}" | tr -d '\r')" local output="$(kubectl exec -qi "${new_debugger_pod}" -- chroot /host bash -c "${command}" | tr -d '\r')"
# Get the updated list of debugger pods. # Delete the newly created pod
declare -a new_debugger_pods=( $(kubectl get pods -o name | grep node-debugger) ) kubectl delete "${new_debugger_pod}" >&2
# Delete the debugger pod created above. # Output the command result
for new_pod in "${new_debugger_pods[@]}"; do local exit_code="$(echo "${output}" | tail -1)"
delete_pod_if_new "${new_pod}" "${old_debugger_pods[@]}"
done
exit_code="$(echo "${output}" | tail -1)"
echo "$(echo "${output}" | head -n -1)" echo "$(echo "${output}" | head -n -1)"
return ${exit_code} return ${exit_code}
} }