diff --git a/tests/integration/kubernetes/confidential_common.sh b/tests/integration/kubernetes/confidential_common.sh index 5e5fb019ae..5b3e59ba71 100644 --- a/tests/integration/kubernetes/confidential_common.sh +++ b/tests/integration/kubernetes/confidential_common.sh @@ -87,27 +87,30 @@ function is_confidential_hardware() { function create_loop_device(){ local loop_file="${1:-/tmp/trusted-image-storage.img}" + local node="$(get_one_kata_node)" cleanup_loop_device "$loop_file" - sudo dd if=/dev/zero of=$loop_file bs=1M count=2500 - sudo losetup -fP $loop_file >/dev/null 2>&1 - local device=$(sudo losetup -j $loop_file | awk -F'[: ]' '{print $1}') + exec_host "$node" "dd if=/dev/zero of=$loop_file bs=1M count=2500" + exec_host "$node" "losetup -fP $loop_file >/dev/null 2>&1" + local device=$(exec_host "$node" losetup -j $loop_file | awk -F'[: ]' '{print $1}') + echo $device } function cleanup_loop_device(){ local loop_file="${1:-/tmp/trusted-image-storage.img}" + local node="$(get_one_kata_node)" # Find all loop devices associated with $loop_file - local existed_devices=$(sudo losetup -j $loop_file | awk -F'[: ]' '{print $1}') + local existed_devices=$(exec_host "$node" losetup -j $loop_file | awk -F'[: ]' '{print $1}') if [ -n "$existed_devices" ]; then # Iterate over each found loop device and detach it for d in $existed_devices; do - sudo losetup -d "$d" >/dev/null 2>&1 + exec_host "$node" "losetup -d "$d" >/dev/null 2>&1" done fi - sudo rm -f "$loop_file" >/dev/null 2>&1 || true + exec_host "$node" "rm -f "$loop_file" >/dev/null 2>&1 || true" } # This function creates pod yaml. Parameters diff --git a/tests/integration/kubernetes/tests_common.sh b/tests/integration/kubernetes/tests_common.sh index 501a0fc42e..d21e40d0ec 100644 --- a/tests/integration/kubernetes/tests_common.sh +++ b/tests/integration/kubernetes/tests_common.sh @@ -85,7 +85,6 @@ exec_host() { local node="$1" # `kubectl debug` always returns 0, so we hack it to return the right exit code. local command="${@:2}" - command+='; echo -en \\n$?' # Make 7 character hash from the node name local pod_name="custom-node-debugger-$(echo -n "$node" | sha1sum | cut -c1-7)" @@ -97,6 +96,11 @@ exec_host() { kubectl apply -n kube-system -f - > /dev/null # Wait for the newly created pod to be ready kubectl wait pod -n kube-system --timeout="30s" --for=condition=ready "${pod_name}" > /dev/null + # Manually check the exit status of the previous command to handle errors explicitly + # since `set -e` is not enabled, allowing subsequent commands to run if needed. + if [ $? -ne 0 ]; then + return $? + fi fi # Execute the command and capture the output @@ -109,12 +113,7 @@ exec_host() { # [bats-exec-test:38] INFO: k8s configured to use runtimeclass # bash: line 1: $'\r': command not found # ``` - local output="$(kubectl exec -qi -n kube-system "${pod_name}" -- chroot /host bash -c "${command}" | tr -d '\r')" - - # Output the command result - local exit_code="$(echo "${output}" | tail -1)" - echo "$(echo "${output}" | head -n -1)" - return ${exit_code} + kubectl exec -qi -n kube-system "${pod_name}" -- chroot /host bash -c "${command}" | tr -d '\r' } auto_generate_policy_enabled() {