github.com/kata-containers/tests@v0.0.0-20240307153542-772105b56064/integration/nydus/nydus_tests.sh (about)

     1  #!/bin/bash
     2  #
     3  # Copyright (c) 2022 Ant Group
     4  #
     5  # SPDX-License-Identifier: Apache-2.0
     6  #
     7  # This will test the nydus feature is working properly
     8  
     9  set -o errexit
    10  set -o nounset
    11  set -o pipefail
    12  set -o errtrace
    13  
    14  dir_path=$(dirname "$0")
    15  source "${dir_path}/../../lib/common.bash"
    16  source "${dir_path}/../../.ci/lib.sh"
    17  source "/etc/os-release" || source "/usr/lib/os-release"
    18  KATA_HYPERVISOR="${KATA_HYPERVISOR:-qemu}"
    19  
    20  need_restore_kata_config=false
    21  kata_config_backup="/tmp/kata-configuration.toml"
    22  SYSCONFIG_FILE="/etc/kata-containers/configuration.toml"
    23  DEFAULT_CONFIG_FILE="/opt/kata/share/defaults/kata-containers/configuration-qemu.toml"
    24  CLH_CONFIG_FILE="/opt/kata/share/defaults/kata-containers/configuration-clh.toml"
    25  DB_CONFIG_FILE="/opt/kata/share/defaults/kata-containers/configuration-dragonball.toml"
    26  need_restore_containerd_config=false
    27  containerd_config="/etc/containerd/config.toml"
    28  containerd_config_backup="/tmp/containerd.config.toml"
    29  
    30  # test image for container
    31  IMAGE="${IMAGE:-ghcr.io/dragonflyoss/image-service/alpine:nydus-latest}"
    32  
    33  if [ "$KATA_HYPERVISOR" != "qemu" ] && [ "$KATA_HYPERVISOR" != "cloud-hypervisor" ]; then
    34  	echo "Skip nydus test for $KATA_HYPERVISOR, it only works for QEMU/CLH. now."
    35  	exit 0
    36  fi
    37  
    38  arch="$(uname -m)"
    39  if [ "$arch" != "x86_64" ]; then
    40  	echo "Skip nydus test for $arch, it only works for x86_64 now. See https://github.com/kata-containers/tests/issues/4445"
    41  	exit 0
    42  fi
    43  
    44  function install_from_tarball() {
    45  	local package_name="$1"
    46  	local binary_name="$2"
    47  	[ -n "$package_name" ] || die "need package_name"
    48  	[ -n "$binary_name" ] || die "need package release binary_name"
    49  
    50  	local url=$(get_version "externals.${package_name}.url")
    51  	local version=$(get_version "externals.${package_name}.version")
    52  	local tarball_url="${url}/releases/download/${version}/${binary_name}-${version}-$arch.tgz"
    53  	if [ "${package_name}" == "nydus" ]; then
    54  		local goarch="$(${dir_path}/../../.ci/kata-arch.sh --golang)"
    55  		tarball_url="${url}/releases/download/${version}/${binary_name}-${version}-linux-$goarch.tgz"
    56  	fi
    57  	echo "Download tarball from ${tarball_url}"
    58  	curl -Ls "$tarball_url" | sudo tar xfz - -C /usr/local/bin --strip-components=1
    59  }
    60  
    61  function setup_nydus() {
    62  	# install nydus
    63  	install_from_tarball "nydus" "nydus-static"
    64  
    65  	# install nydus-snapshotter
    66  	install_from_tarball "nydus-snapshotter" "nydus-snapshotter"
    67  
    68  	# Config nydus snapshotter
    69  	sudo -E cp "$dir_path/nydusd-config.json" /etc/
    70  	sudo -E cp "$dir_path/snapshotter-config.toml" /etc/
    71  
    72  	# start nydus-snapshotter
    73  	nohup /usr/local/bin/containerd-nydus-grpc \
    74  		--config /etc/snapshotter-config.toml \
    75  		--nydusd-config /etc/nydusd-config.json \
    76  		--log-to-stdout >/dev/null 2>&1 &
    77  }
    78  
    79  function config_kata() {
    80  	sudo mkdir -p /etc/kata-containers
    81  	if [ -f "$SYSCONFIG_FILE" ]; then
    82  		need_restore_kata_config=true
    83  		sudo cp -a "${SYSCONFIG_FILE}" "${kata_config_backup}"
    84  	elif [ "$KATA_HYPERVISOR" == "qemu" ]; then
    85  		sudo cp -a "${DEFAULT_CONFIG_FILE}" "${SYSCONFIG_FILE}"
    86  	elif [ "$KATA_HYPERVISOR" == "dragonball" ]; then
    87  		sudo cp -a "${DB_CONFIG_FILE}" "${SYSCONFIG_FILE}"
    88  	else
    89  		sudo cp -a "${CLH_CONFIG_FILE}" "${SYSCONFIG_FILE}"
    90  	fi
    91  
    92  	echo "Enabling all debug options in file ${SYSCONFIG_FILE}"
    93  	sudo sed -i -e 's/^#\(enable_debug\).*=.*$/\1 = true/g' "${SYSCONFIG_FILE}"
    94  	sudo sed -i -e 's/^kernel_params = "\(.*\)"/kernel_params = "\1 agent.log=debug"/g' "${SYSCONFIG_FILE}"
    95  
    96  	if [ "$KATA_HYPERVISOR" != "dragonball" ]; then
    97  		sudo sed -i 's|^shared_fs.*|shared_fs = "virtio-fs-nydus"|g' "${SYSCONFIG_FILE}"
    98  		sudo sed -i 's|^virtio_fs_daemon.*|virtio_fs_daemon = "/usr/local/bin/nydusd"|g' "${SYSCONFIG_FILE}"
    99  	fi
   100  
   101  	sudo sed -i 's|^virtio_fs_extra_args.*|virtio_fs_extra_args = []|g' "${SYSCONFIG_FILE}"
   102  }
   103  
   104  function config_containerd() {
   105  	readonly runc_path=$(command -v runc)
   106  	sudo mkdir -p /etc/containerd/
   107  	if [ -f "$containerd_config" ]; then
   108  		need_restore_containerd_config=true
   109  		sudo cp -a "${containerd_config}" "${containerd_config_backup}"
   110  	else
   111  		sudo rm "${containerd_config}"
   112  	fi
   113  
   114  	cat <<EOF | sudo tee $containerd_config
   115  [debug]
   116    level = "debug"
   117  [proxy_plugins]
   118    [proxy_plugins.nydus]
   119      type = "snapshot"
   120      address = "/run/containerd-nydus/containerd-nydus-grpc.sock"
   121  [plugins]
   122    [plugins.cri]
   123      disable_hugetlb_controller = false
   124      [plugins.cri.containerd]
   125        snapshotter = "nydus"
   126        disable_snapshot_annotations = false
   127        [plugins.cri.containerd.runtimes]
   128        [plugins.cri.containerd.runtimes.runc]
   129           runtime_type = "io.containerd.runc.v2"
   130           [plugins.cri.containerd.runtimes.runc.options]
   131             BinaryName = "${runc_path}"
   132             Root = ""
   133        [plugins.cri.containerd.runtimes.kata]
   134           runtime_type = "io.containerd.kata.v2"
   135           privileged_without_host_devices = true
   136  EOF
   137  }
   138  
   139  function check_nydus_snapshotter_exist() {
   140  	bin="containerd-nydus-grpc"
   141  	if pgrep -f "$bin" >/dev/null; then
   142  		echo "nydus-snapshotter is running"
   143  	else
   144  		die "nydus-snapshotter is not running"
   145  	fi
   146  }
   147  
   148  function setup() {
   149  	setup_nydus
   150  	config_kata
   151  	config_containerd
   152  	restart_containerd_service
   153  	check_processes
   154  	check_nydus_snapshotter_exist
   155  	extract_kata_env
   156  }
   157  
   158  function run_test() {
   159  	sudo -E crictl pull "${IMAGE}"
   160  	pod=$(sudo -E crictl runp -r kata $dir_path/nydus-sandbox.yaml)
   161  	echo "Pod $pod created"
   162  	cnt=$(sudo -E crictl create $pod $dir_path/nydus-container.yaml $dir_path/nydus-sandbox.yaml)
   163  	echo "Container $cnt created"
   164  	sudo -E crictl start $cnt
   165  	echo "Container $cnt started"
   166  
   167  	# ensure container is running
   168  	state=$(sudo -E crictl inspect $cnt | jq .status.state | tr -d '"')
   169  	[ $state == "CONTAINER_RUNNING" ] || die "Container is not running($state)"
   170  	# run a command in container
   171  	crictl exec $cnt ls
   172  
   173  	# cleanup containers
   174  	sudo -E crictl stop $cnt
   175  	sudo -E crictl stopp $pod
   176  	sudo -E crictl rmp $pod
   177  }
   178  
   179  function teardown() {
   180  	echo "Running teardown"
   181  
   182  	# kill nydus-snapshotter
   183  	bin=containerd-nydus-grpc
   184  	kill -9 $(pidof $bin) || true
   185  	[ "$(pidof $bin)" == "" ] || die "$bin is running"
   186  
   187  	bin=nydusd
   188  	kill -9 $(pidof $bin) || true
   189  	[ "$(pidof $bin)" == "" ] || die "$bin is running"
   190  
   191  	# restore kata configuratiom.toml if needed
   192  	if [ "${need_restore_kata_config}" == "true" ]; then
   193  		sudo mv "$kata_config_backup" "$SYSCONFIG_FILE"
   194  	else
   195  		sudo rm "$SYSCONFIG_FILE"
   196  	fi
   197  
   198  	# restore containerd config.toml if needed
   199  	if [ "${need_restore_containerd_config}" == "true" ]; then
   200  		sudo mv "$containerd_config_backup" "$containerd_config"
   201  	else
   202  		sudo rm "$containerd_config"
   203  	fi
   204  
   205  	clean_env_ctr
   206  	check_processes
   207  }
   208  
   209  trap teardown EXIT
   210  
   211  echo "Running setup"
   212  setup
   213  
   214  echo "Running nydus integration tests"
   215  run_test