tests: Fix shellcheck issues in nydus_tests.sh

Fix shellcheck warnings and notes identified by running
shellcheck --severity=style.

Signed-off-by: Fabiano Fidêncio <ffidencio@nvidia.com>
This commit is contained in:
Fabiano Fidêncio
2026-04-21 12:32:07 +02:00
parent 1161249197
commit 011e0178e1

View File

@@ -12,6 +12,7 @@ set -o pipefail
set -o errtrace
dir_path=$(dirname "$0")
# shellcheck source=/dev/null
source "${dir_path}/../../common.bash"
source "/etc/os-release" || source "/usr/lib/os-release"
KATA_HYPERVISOR="${KATA_HYPERVISOR:-qemu}"
@@ -29,12 +30,12 @@ containerd_config_backup="/tmp/containerd.config.toml"
# test image for container
IMAGE="${IMAGE:-ghcr.io/dragonflyoss/image-service/alpine:nydus-latest}"
if [ "$KATA_HYPERVISOR" != "qemu" ] && [ "$KATA_HYPERVISOR" != "clh" ] && [ "$KATA_HYPERVISOR" != "dragonball" ]; then
echo "Skip nydus test for $KATA_HYPERVISOR, it only works for QEMU/CLH/DB now."
if [[ "${KATA_HYPERVISOR}" != "qemu" ]] && [[ "${KATA_HYPERVISOR}" != "clh" ]] && [[ "${KATA_HYPERVISOR}" != "dragonball" ]]; then
echo "Skip nydus test for ${KATA_HYPERVISOR}, it only works for QEMU/CLH/DB now."
exit 0
fi
case "$KATA_HYPERVISOR" in
case "${KATA_HYPERVISOR}" in
dragonball)
SYSCONFIG_FILE="/etc/kata-containers/runtime-rs/configuration.toml"
;;
@@ -44,8 +45,8 @@ esac
function setup_nydus() {
# Config nydus snapshotter
sudo -E cp "$dir_path/nydusd-config.json" /etc/
sudo -E cp "$dir_path/snapshotter-config.toml" /etc/
sudo -E cp "${dir_path}/nydusd-config.json" /etc/
sudo -E cp "${dir_path}/snapshotter-config.toml" /etc/
# start nydus-snapshotter
sudo nohup /usr/local/bin/containerd-nydus-grpc \
@@ -54,13 +55,13 @@ function setup_nydus() {
}
function config_kata() {
sudo mkdir -p $(dirname $SYSCONFIG_FILE)
if [ -f "$SYSCONFIG_FILE" ]; then
sudo mkdir -p "$(dirname "${SYSCONFIG_FILE}")"
if [[ -f "${SYSCONFIG_FILE}" ]]; then
need_restore_kata_config=true
sudo cp -a "${SYSCONFIG_FILE}" "${kata_config_backup}"
elif [ "$KATA_HYPERVISOR" == "qemu" ]; then
elif [[ "${KATA_HYPERVISOR}" == "qemu" ]]; then
sudo cp -a "${DEFAULT_CONFIG_FILE}" "${SYSCONFIG_FILE}"
elif [ "$KATA_HYPERVISOR" == "dragonball" ]; then
elif [[ "${KATA_HYPERVISOR}" == "dragonball" ]]; then
sudo cp -a "${DB_CONFIG_FILE}" "${SYSCONFIG_FILE}"
else
sudo cp -a "${CLH_CONFIG_FILE}" "${SYSCONFIG_FILE}"
@@ -70,7 +71,7 @@ function config_kata() {
sudo sed -i -e 's/^#\(enable_debug\).*=.*$/\1 = true/g' "${SYSCONFIG_FILE}"
sudo sed -i -e 's/^kernel_params = "\(.*\)"/kernel_params = "\1 agent.log=debug"/g' "${SYSCONFIG_FILE}"
if [ "$KATA_HYPERVISOR" != "dragonball" ]; then
if [[ "${KATA_HYPERVISOR}" != "dragonball" ]]; then
sudo sed -i 's|^shared_fs.*|shared_fs = "virtio-fs-nydus"|g' "${SYSCONFIG_FILE}"
sudo sed -i 's|^virtio_fs_daemon.*|virtio_fs_daemon = "/usr/local/bin/nydusd"|g' "${SYSCONFIG_FILE}"
fi
@@ -97,7 +98,7 @@ function config_containerd() {
awk '/^[[:space:]]*version[[:space:]]*=/ {print; exit}')
# 3) If no "version = X" line is found, return
if [ -z "$version_line" ]; then
if [[ -z "${version_line}" ]]; then
echo "[ERROR] Cannot find version key in containerd config, defaulting to v1 config"
return
fi
@@ -105,7 +106,7 @@ function config_containerd() {
# 4) Extract the numeric version from the matched line
# - Remove leading/trailing spaces around the value
# - Remove surrounding double quotes if any
version_num=$(echo "$version_line" | awk -F'=' '
version_num=$(echo "${version_line}" | awk -F'=' '
{
gsub(/^[[:space:]]+|[[:space:]]+$/, "", $2) # trim spaces
gsub(/"/, "", $2) # remove double quotes
@@ -114,27 +115,29 @@ function config_containerd() {
# 5) Validate that the extracted value is strictly numeric
# If not numeric, fall back to v1 configuration
if ! echo "$version_num" | grep -Eq '^[0-9]+$'; then
echo "[ERROR] Invalid version format: \"$version_num\". Defaulting to v1 config"
if ! echo "${version_num}" | grep -Eq '^[0-9]+$'; then
echo "[ERROR] Invalid version format: \"${version_num}\". Defaulting to v1 config"
return
fi
# 6) Based on version number, run the appropriate configuration function
echo "[INFO] Running config for containerd version $version_num"
echo "[INFO] Running config for containerd version ${version_num}"
config_containerd_core
}
function config_containerd_core() {
readonly runc_path=$(command -v runc)
local runc_path
runc_path=$(command -v runc)
readonly runc_path
sudo mkdir -p /etc/containerd/
if [ -f "$containerd_config" ]; then
if [[ -f "${containerd_config}" ]]; then
need_restore_containerd_config=true
sudo cp -a "${containerd_config}" "${containerd_config_backup}"
else
sudo rm "${containerd_config}"
fi
cat <<EOF | sudo tee $containerd_config
cat <<EOF | sudo tee "${containerd_config}"
[proxy_plugins]
[proxy_plugins.nydus]
type = "snapshot"
@@ -161,7 +164,7 @@ EOF
function check_nydus_snapshotter_exist() {
echo "check_nydus_snapshotter_exist"
bin="containerd-nydus-grpc"
if pgrep -f "$bin" >/dev/null; then
if pgrep -f "${bin}" >/dev/null; then
echo "nydus-snapshotter is running"
else
die "nydus-snapshotter is not running"
@@ -180,23 +183,23 @@ function setup() {
function run_test() {
sudo -E crictl --timeout=20s pull "${IMAGE}"
pod=$(sudo -E crictl --timeout=20s runp -r kata-${KATA_HYPERVISOR} $dir_path/nydus-sandbox.yaml)
echo "Pod $pod created"
cnt=$(sudo -E crictl --timeout=20s create $pod $dir_path/nydus-container.yaml $dir_path/nydus-sandbox.yaml)
echo "Container $cnt created"
sudo -E crictl --timeout=20s start $cnt
echo "Container $cnt started"
pod=$(sudo -E crictl --timeout=20s runp -r "kata-${KATA_HYPERVISOR}" "${dir_path}/nydus-sandbox.yaml")
echo "Pod ${pod} created"
cnt=$(sudo -E crictl --timeout=20s create "${pod}" "${dir_path}/nydus-container.yaml" "${dir_path}/nydus-sandbox.yaml")
echo "Container ${cnt} created"
sudo -E crictl --timeout=20s start "${cnt}"
echo "Container ${cnt} started"
# ensure container is running
state=$(sudo -E crictl --timeout=20s inspect $cnt | jq .status.state | tr -d '"')
[ $state == "CONTAINER_RUNNING" ] || die "Container is not running($state)"
state=$(sudo -E crictl --timeout=20s inspect "${cnt}" | jq .status.state | tr -d '"')
[[ "${state}" == "CONTAINER_RUNNING" ]] || die "Container is not running(${state})"
# run a command in container
sudo -E crictl --timeout=20s exec $cnt ls
sudo -E crictl --timeout=20s exec "${cnt}" ls
# cleanup containers
sudo -E crictl --timeout=20s stop $cnt
sudo -E crictl --timeout=20s stopp $pod
sudo -E crictl --timeout=20s rmp $pod
sudo -E crictl --timeout=20s stop "${cnt}"
sudo -E crictl --timeout=20s stopp "${pod}"
sudo -E crictl --timeout=20s rmp "${pod}"
}
function teardown() {
@@ -205,35 +208,35 @@ function teardown() {
local pid
for bin in containerd-nydus-grpc nydusd; do
pid=$(pidof $bin)
if [ -n "$pid" ]; then
echo "Killing $bin processes"
pid=$(pidof "${bin}" || true)
if [[ -n "${pid}" ]]; then
echo "Killing ${bin} processes"
# shellcheck disable=SC2086
sudo -E kill -9 $pid || true
if [ -n "$(pidof $bin)" ]; then
echo "$bin is still running ($pid) but it should not"
sudo -E kill -9 ${pid} || true
if [[ -n "$(pidof "${bin}")" ]]; then
echo "${bin} is still running (${pid}) but it should not"
rc=1
fi
fi
done
# restore kata configuratiom.toml if needed
if [ "${need_restore_kata_config}" == "true" ]; then
sudo mv "$kata_config_backup" "$SYSCONFIG_FILE"
if [[ "${need_restore_kata_config}" == "true" ]]; then
sudo mv "${kata_config_backup}" "${SYSCONFIG_FILE}"
else
sudo rm "$SYSCONFIG_FILE"
sudo rm "${SYSCONFIG_FILE}"
fi
# restore containerd config.toml if needed
if [ "${need_restore_containerd_config}" == "true" ]; then
sudo mv "$containerd_config_backup" "$containerd_config"
if [[ "${need_restore_containerd_config}" == "true" ]]; then
sudo mv "${containerd_config_backup}" "${containerd_config}"
else
sudo rm "$containerd_config"
sudo rm "${containerd_config}"
fi
clean_env_ctr || rc=1
check_processes
return $rc
return "${rc}"
}
trap teardown EXIT