1
0
mirror of https://github.com/kata-containers/kata-containers.git synced 2025-05-08 16:37:32 +00:00

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 <Hyounggyu.Choi@ibm.com>
This commit is contained in:
Hyounggyu Choi 2024-09-06 14:12:30 +02:00
parent 374b8d2534
commit 9cff9271bc
2 changed files with 15 additions and 13 deletions
tests/integration/kubernetes

View File

@ -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

View File

@ -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() {