From 9cff9271bc8e9cf001c2706c56f5747c14d29e2b Mon Sep 17 00:00:00 2001 From: Hyounggyu Choi Date: Fri, 6 Sep 2024 14:12:30 +0200 Subject: [PATCH] tests: Run all commands in *_loop_device() using exec_host() If the host running the tests is different from the host where the cluster is running, the *_loop_device() functions do not work as expected because the device is created on the test host, while the cluster expects the device to be local. This commit ensures that all commands for the relevant functions are executed via exec_host() so that a device should be handled on a cluster node. Additionally, it modifies exec_host() to return the exit code of the last executed command because the existing logic with `kubectl debug` sometimes includes unexpected characters that are difficult to handle. `kubectl exec` appears to properly return the exit code for a given command to it. Signed-off-by: Hyounggyu Choi --- .../integration/kubernetes/confidential_common.sh | 15 +++++++++------ tests/integration/kubernetes/tests_common.sh | 13 ++++++------- 2 files changed, 15 insertions(+), 13 deletions(-) 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() {