github.com/kata-containers/tests@v0.0.0-20240307153542-772105b56064/integration/containerd/cri/integration-tests.sh (about) 1 #!/bin/bash 2 # 3 # Copyright (c) 2017-2018 Intel Corporation 4 # 5 # SPDX-License-Identifier: Apache-2.0 6 # 7 8 [[ "${DEBUG}" != "" ]] && set -o xtrace 9 set -o errexit 10 set -o nounset 11 set -o pipefail 12 set -o errtrace 13 14 SCRIPT_PATH=$(dirname "$(readlink -f "$0")") 15 source "${SCRIPT_PATH}/../../../lib/common.bash" 16 source "${SCRIPT_PATH}/../../../.ci/lib.sh" 17 18 # runc is installed in /usr/local/sbin/ add that path 19 export PATH="$PATH:/usr/local/sbin" 20 21 containerd_tarball_version=$(get_version "externals.containerd.version") 22 23 # Runtime to be used for testing 24 RUNTIME=${RUNTIME:-containerd-shim-kata-v2} 25 SHIMV2_TEST=${SHIMV2_TEST:-""} 26 FACTORY_TEST=${FACTORY_TEST:-""} 27 KILL_VMM_TEST=${KILL_VMM_TEST:-""} 28 KATA_HYPERVISOR="${KATA_HYPERVISOR:-qemu}" 29 USE_DEVMAPPER="${USE_DEVMAPPER:-false}" 30 ARCH=$(uname -m) 31 32 default_runtime_type="io.containerd.runc.v2" 33 # Type of containerd runtime to be tested 34 containerd_runtime_type="${default_runtime_type}" 35 # Runtime to be use for the test in containerd 36 containerd_runtime_test=${RUNTIME} 37 if [ -n "${SHIMV2_TEST}" ]; then 38 containerd_runtime_type="io.containerd.kata.v2" 39 containerd_runtime_test="io.containerd.kata.v2" 40 fi 41 42 readonly runc_runtime_bin=$(command -v "runc") 43 44 readonly CRITEST=${GOPATH}/bin/critest 45 46 # Flag to do tasks for CI 47 SNAP_CI=${SNAP_CI:-""} 48 CI=${CI:-""} 49 50 containerd_shim_path="$(command -v containerd-shim)" 51 readonly cri_containerd_repo=$(get_version "externals.containerd.url") 52 readonly cri_containerd_repo_git="https://${cri_containerd_repo}.git" 53 54 #containerd config file 55 readonly tmp_dir=$(mktemp -t -d test-cri-containerd.XXXX) 56 export REPORT_DIR="${tmp_dir}" 57 readonly CONTAINERD_CONFIG_FILE="${tmp_dir}/test-containerd-config" 58 readonly CONTAINERD_CONFIG_FILE_TEMP="${CONTAINERD_CONFIG_FILE}.temp" 59 readonly default_containerd_config="/etc/containerd/config.toml" 60 readonly default_containerd_config_backup="$CONTAINERD_CONFIG_FILE.backup" 61 readonly kata_config="/etc/kata-containers/configuration.toml" 62 readonly kata_config_backup="$kata_config.backup" 63 readonly default_kata_config="/opt/kata/share/defaults/kata-containers/configuration.toml" 64 65 ci_config() { 66 sudo mkdir -p $(dirname "${kata_config}") 67 [ -f "$kata_config" ] && sudo cp "$kata_config" "$kata_config_backup" || \ 68 sudo cp "$default_kata_config" "$kata_config" 69 70 source /etc/os-release || source /usr/lib/os-release 71 ID=${ID:-""} 72 if [ "$ID" == ubuntu ] && [ -n "${CI}" ] ;then 73 # https://github.com/kata-containers/tests/issues/352 74 if [ -n "${FACTORY_TEST}" ]; then 75 sudo sed -i -e 's/^#enable_template.*$/enable_template = true/g' "${kata_config}" 76 echo "init vm template" 77 sudo -E PATH=$PATH "$RUNTIME" factory init 78 fi 79 fi 80 81 echo "enable debug for kata-runtime" 82 sudo sed -i 's/^#enable_debug =/enable_debug =/g' ${kata_config} 83 } 84 85 ci_cleanup() { 86 source /etc/os-release || source /usr/lib/os-release 87 88 if [ -n "${FACTORY_TEST}" ]; then 89 echo "destroy vm template" 90 sudo -E PATH=$PATH "$RUNTIME" factory destroy 91 fi 92 93 if [ -n "${KILL_VMM_TEST}" ] && [ -e "$default_containerd_config_backup" ]; then 94 echo "restore containerd config" 95 sudo systemctl stop containerd 96 sudo cp "$default_containerd_config_backup" "$default_containerd_config" 97 fi 98 99 [ -f "$kata_config_backup" ] && sudo mv "$kata_config_backup" "$kata_config" || \ 100 sudo rm "$kata_config" 101 } 102 103 create_containerd_config() { 104 local runtime="$1" 105 # kata_annotations is set to 1 if caller want containerd setup with 106 # kata annotations support. 107 local kata_annotations=${2-0} 108 [ -n "${runtime}" ] || die "need runtime to create config" 109 110 local runtime_type="${containerd_runtime_type}" 111 if [ "${runtime}" == "runc" ]; then 112 runtime_type="io.containerd.runc.v2" 113 fi 114 local containerd_runtime="${runtime}" 115 if [ "${runtime_type}" == "${default_runtime_type}" ];then 116 local containerd_runtime=$(command -v "${runtime}") 117 fi 118 # Remove dots. Dots are used by toml syntax as atribute separator 119 runtime="${runtime//./-}" 120 121 cat << EOF | sudo tee "${CONTAINERD_CONFIG_FILE}" 122 [debug] 123 level = "debug" 124 [plugins] 125 [plugins.cri] 126 [plugins.cri.containerd] 127 default_runtime_name = "$runtime" 128 [plugins.cri.containerd.runtimes.${runtime}] 129 runtime_type = "${runtime_type}" 130 $( [ $kata_annotations -eq 1 ] && \ 131 echo 'pod_annotations = ["io.katacontainers.*"]' && \ 132 echo ' container_annotations = ["io.katacontainers.*"]' 133 ) 134 [plugins.cri.containerd.runtimes.${runtime}.options] 135 Runtime = "${containerd_runtime}" 136 [plugins.linux] 137 shim = "${containerd_shim_path}" 138 EOF 139 140 if [ "$USE_DEVMAPPER" == "true" ]; then 141 sudo sed -i 's|^\(\[plugins\]\).*|\1\n \[plugins.devmapper\]\n pool_name = \"contd-thin-pool\"\n base_image_size = \"4096MB\"|' ${CONTAINERD_CONFIG_FILE} 142 echo "Devicemapper configured" 143 cat "${CONTAINERD_CONFIG_FILE}" 144 fi 145 146 } 147 148 cleanup() { 149 ci_cleanup 150 [ -d "$tmp_dir" ] && rm -rf "${tmp_dir}" 151 } 152 153 trap cleanup EXIT 154 155 err_report() { 156 local log_file="${REPORT_DIR}/containerd.log" 157 if [ -f "$log_file" ]; then 158 echo "ERROR: containerd log :" 159 echo "-------------------------------------" 160 cat "${log_file}" 161 echo "-------------------------------------" 162 fi 163 } 164 165 166 check_daemon_setup() { 167 info "containerd(cri): Check daemon works with runc" 168 create_containerd_config "runc" 169 170 #restart docker service as TestImageLoad depends on it 171 [ -z "${USE_PODMAN:-}" ] && restart_docker_service 172 173 # containerd cri-integration will modify the passed in config file. Let's 174 # give it a temp one. 175 cp $CONTAINERD_CONFIG_FILE $CONTAINERD_CONFIG_FILE_TEMP 176 # in some distros(AlibabaCloud), there is no btrfs-devel package available, 177 # so pass GO_BUILDTAGS="no_btrfs" to make to not use btrfs. 178 sudo -E PATH="${PATH}:/usr/local/bin" \ 179 REPORT_DIR="${REPORT_DIR}" \ 180 FOCUS="TestImageLoad" \ 181 RUNTIME="" \ 182 CONTAINERD_CONFIG_FILE="$CONTAINERD_CONFIG_FILE_TEMP" \ 183 make GO_BUILDTAGS="no_btrfs" -e cri-integration 184 } 185 186 testContainerStart() { 187 # no_container_yaml set to 1 will not create container_yaml 188 # because caller has created its own container_yaml. 189 no_container_yaml=${1-0} 190 191 local pod_yaml=${REPORT_DIR}/pod.yaml 192 local container_yaml=${REPORT_DIR}/container.yaml 193 local image="busybox:latest" 194 195 cat << EOF > "${pod_yaml}" 196 metadata: 197 name: busybox-sandbox1 198 EOF 199 200 #TestContainerSwap has created its own container_yaml. 201 if [ $no_container_yaml -ne 1 ]; then 202 cat << EOF > "${container_yaml}" 203 metadata: 204 name: busybox-killed-vmm 205 image: 206 image: "$image" 207 command: 208 - top 209 EOF 210 fi 211 212 sudo cp "$default_containerd_config" "$default_containerd_config_backup" 213 sudo cp $CONTAINERD_CONFIG_FILE "$default_containerd_config" 214 215 restart_containerd_service 216 217 sudo crictl pull $image 218 podid=$(sudo crictl runp $pod_yaml) 219 cid=$(sudo crictl create $podid $container_yaml $pod_yaml) 220 sudo crictl start $cid 221 } 222 223 testContainerStop() { 224 info "stop pod $podid" 225 sudo crictl stopp $podid 226 info "remove pod $podid" 227 sudo crictl rmp $podid 228 229 sudo cp "$default_containerd_config_backup" "$default_containerd_config" 230 restart_containerd_service 231 } 232 233 TestKilledVmmCleanup() { 234 if [ -z "${SHIMV2_TEST}" ] || [ -z "${KILL_VMM_TEST}" ]; then 235 return 236 fi 237 238 info "test killed vmm cleanup" 239 240 testContainerStart 241 242 qemu_pid=$(ps aux|grep qemu|grep -v grep|awk '{print $2}') 243 info "kill qemu $qemu_pid" 244 sudo kill -SIGKILL $qemu_pid 245 # sleep to let shimv2 exit 246 sleep 1 247 remained=$(ps aux|grep shimv2|grep -v grep || true) 248 [ -z $remained ] || die "found remaining shimv2 process $remained" 249 250 testContainerStop 251 252 info "stop containerd" 253 } 254 255 TestContainerMemoryUpdate() { 256 if [[ "${KATA_HYPERVISOR}" != "qemu" ]] || [[ "${ARCH}" == "ppc64le" ]] || [[ "${ARCH}" == "s390x" ]]; then 257 return 258 fi 259 260 test_virtio_mem=$1 261 262 if [ $test_virtio_mem -eq 1 ]; then 263 if [[ "$ARCH" != "x86_64" ]]; then 264 return 265 fi 266 info "Test container memory update with virtio-mem" 267 268 sudo sed -i -e 's/^#enable_virtio_mem.*$/enable_virtio_mem = true/g' "${kata_config}" 269 else 270 info "Test container memory update without virtio-mem" 271 272 sudo sed -i -e 's/^enable_virtio_mem.*$/#enable_virtio_mem = true/g' "${kata_config}" 273 fi 274 275 testContainerStart 276 277 vm_size=$(($(crictl exec $cid cat /proc/meminfo | grep "MemTotal:" | awk '{print $2}')*1024)) 278 if [ $vm_size -gt $((2*1024*1024*1024)) ] || [ $vm_size -lt $((2*1024*1024*1024-128*1024*1024)) ]; then 279 testContainerStop 280 die "The VM memory size $vm_size before update is not right" 281 fi 282 283 sudo crictl update --memory $((2*1024*1024*1024)) $cid 284 sleep 1 285 286 vm_size=$(($(crictl exec $cid cat /proc/meminfo | grep "MemTotal:" | awk '{print $2}')*1024)) 287 if [ $vm_size -gt $((4*1024*1024*1024)) ] || [ $vm_size -lt $((4*1024*1024*1024-128*1024*1024)) ]; then 288 testContainerStop 289 die "The VM memory size $vm_size after increase is not right" 290 fi 291 292 if [ $test_virtio_mem -eq 1 ]; then 293 sudo crictl update --memory $((1*1024*1024*1024)) $cid 294 sleep 1 295 296 vm_size=$(($(crictl exec $cid cat /proc/meminfo | grep "MemTotal:" | awk '{print $2}')*1024)) 297 if [ $vm_size -gt $((3*1024*1024*1024)) ] || [ $vm_size -lt $((3*1024*1024*1024-128*1024*1024)) ]; then 298 testContainerStop 299 die "The VM memory size $vm_size after decrease is not right" 300 fi 301 fi 302 303 testContainerStop 304 } 305 306 getContainerSwapInfo() { 307 swap_size=$(($(crictl exec $cid cat /proc/meminfo | grep "SwapTotal:" | awk '{print $2}')*1024)) 308 # NOTE: these below two checks only works on cgroup v1 309 swappiness=$(crictl exec $cid cat /sys/fs/cgroup/memory/memory.swappiness) 310 swap_in_bytes=$(crictl exec $cid cat /sys/fs/cgroup/memory/memory.memsw.limit_in_bytes) 311 } 312 313 TestContainerSwap() { 314 if [[ "${KATA_HYPERVISOR}" != "qemu" ]] || [[ "${ARCH}" != "x86_64" ]]; then 315 return 316 fi 317 318 local container_yaml=${REPORT_DIR}/container.yaml 319 local image="busybox:latest" 320 321 info "Test container with guest swap" 322 323 create_containerd_config "${containerd_runtime_test}" 1 324 sudo sed -i -e 's/^#enable_guest_swap.*$/enable_guest_swap = true/g' "${kata_config}" 325 326 # Test without swap device 327 testContainerStart 328 getContainerSwapInfo 329 # Current default swappiness is 60 330 if [ $swappiness -ne 60 ]; then 331 testContainerStop 332 die "The VM swappiness $swappiness without swap device is not right" 333 fi 334 if [ $swap_in_bytes -lt 1125899906842624 ]; then 335 testContainerStop 336 die "The VM swap_in_bytes $swap_in_bytes without swap device is not right" 337 fi 338 if [ $swap_size -ne 0 ]; then 339 testContainerStop 340 die "The VM swap size $swap_size without swap device is not right" 341 fi 342 testContainerStop 343 344 # Test with swap device 345 cat << EOF > "${container_yaml}" 346 metadata: 347 name: busybox-swap 348 annotations: 349 io.katacontainers.container.resource.swappiness: "100" 350 io.katacontainers.container.resource.swap_in_bytes: "1610612736" 351 linux: 352 resources: 353 memory_limit_in_bytes: 1073741824 354 image: 355 image: "$image" 356 command: 357 - top 358 EOF 359 360 testContainerStart 1 361 getContainerSwapInfo 362 testContainerStop 363 364 if [ $swappiness -ne 100 ]; then 365 die "The VM swappiness $swappiness with swap device is not right" 366 fi 367 if [ $swap_in_bytes -ne 1610612736 ]; then 368 die "The VM swap_in_bytes $swap_in_bytes with swap device is not right" 369 fi 370 if [ $swap_size -ne 536870912 ]; then 371 die "The VM swap size $swap_size with swap device is not right" 372 fi 373 374 # Test without swap_in_bytes 375 cat << EOF > "${container_yaml}" 376 metadata: 377 name: busybox-swap 378 annotations: 379 io.katacontainers.container.resource.swappiness: "100" 380 linux: 381 resources: 382 memory_limit_in_bytes: 1073741824 383 image: 384 image: "$image" 385 command: 386 - top 387 EOF 388 389 testContainerStart 1 390 getContainerSwapInfo 391 testContainerStop 392 393 if [ $swappiness -ne 100 ]; then 394 die "The VM swappiness $swappiness without swap_in_bytes is not right" 395 fi 396 # swap_in_bytes is not set, it should be a value that bigger than 1125899906842624 397 if [ $swap_in_bytes -lt 1125899906842624 ]; then 398 die "The VM swap_in_bytes $swap_in_bytes without swap_in_bytes is not right" 399 fi 400 if [ $swap_size -ne 1073741824 ]; then 401 die "The VM swap size $swap_size without swap_in_bytes is not right" 402 fi 403 404 # Test without memory_limit_in_bytes 405 cat << EOF > "${container_yaml}" 406 metadata: 407 name: busybox-swap 408 annotations: 409 io.katacontainers.container.resource.swappiness: "100" 410 image: 411 image: "$image" 412 command: 413 - top 414 EOF 415 416 testContainerStart 1 417 getContainerSwapInfo 418 testContainerStop 419 420 if [ $swappiness -ne 100 ]; then 421 die "The VM swappiness $swappiness without memory_limit_in_bytes is not right" 422 fi 423 # swap_in_bytes is not set, it should be a value that bigger than 1125899906842624 424 if [ $swap_in_bytes -lt 1125899906842624 ]; then 425 die "The VM swap_in_bytes $swap_in_bytes without memory_limit_in_bytes is not right" 426 fi 427 if [ $swap_size -ne 2147483648 ]; then 428 die "The VM swap size $swap_size without memory_limit_in_bytes is not right" 429 fi 430 431 create_containerd_config "${containerd_runtime_test}" 432 } 433 434 # k8s may restart docker which will impact on containerd stop 435 stop_containerd() { 436 local tmp=$(pgrep kubelet || true) 437 [ -n "$tmp" ] && sudo kubeadm reset -f 438 439 sudo systemctl stop containerd 440 } 441 442 main() { 443 444 info "Stop crio service" 445 systemctl is-active --quiet crio && sudo systemctl stop crio 446 447 info "Stop containerd service" 448 systemctl is-active --quiet containerd && stop_containerd 449 450 # Configure enviroment if running in CI 451 ci_config 452 453 # make sure cri-containerd test install the proper critest version its testing 454 rm -f "${CRITEST}" 455 456 if [ ! -d "${GOPATH}/src/${cri_containerd_repo}" ]; then 457 mkdir -p "${GOPATH}/src/${cri_containerd_repo}" 458 git clone ${cri_containerd_repo_git} "${GOPATH}/src/${cri_containerd_repo}" 459 fi 460 pushd "${GOPATH}/src/${cri_containerd_repo}" 461 462 git reset HEAD 463 git checkout ${containerd_tarball_version} 464 465 # Make sure the right artifacts are going to be built 466 make clean 467 468 check_daemon_setup 469 470 info "containerd(cri): testing using runtime: ${containerd_runtime_test}" 471 472 create_containerd_config "${containerd_runtime_test}" 473 474 info "containerd(cri): Running cri-integration" 475 476 477 passing_test="TestContainerStats|TestContainerRestart|TestContainerListStatsWithIdFilter|TestContainerListStatsWithIdSandboxIdFilter|TestDuplicateName|TestImageLoad|TestImageFSInfo|TestSandboxCleanRemove" 478 479 if [[ "${KATA_HYPERVISOR}" == "cloud-hypervisor" || \ 480 "${KATA_HYPERVISOR}" == "qemu" ]]; then 481 issue="https://github.com/kata-containers/tests/issues/2318" 482 info "${KATA_HYPERVISOR} fails with TestContainerListStatsWithSandboxIdFilter }" 483 info "see ${issue}" 484 else 485 passing_test="${passing_test}|TestContainerListStatsWithSandboxIdFilter" 486 fi 487 488 # in some distros(AlibabaCloud), there is no btrfs-devel package available, 489 # so pass GO_BUILDTAGS="no_btrfs" to make to not use btrfs. 490 # containerd cri-integration will modify the passed in config file. Let's 491 # give it a temp one. 492 cp $CONTAINERD_CONFIG_FILE $CONTAINERD_CONFIG_FILE_TEMP 493 sudo -E PATH="${PATH}:/usr/local/bin" \ 494 REPORT_DIR="${REPORT_DIR}" \ 495 FOCUS="^(${passing_test})$" \ 496 RUNTIME="" \ 497 CONTAINERD_CONFIG_FILE="$CONTAINERD_CONFIG_FILE_TEMP" \ 498 make GO_BUILDTAGS="no_btrfs" -e cri-integration 499 500 # trap error for print containerd log, 501 # containerd's `cri-integration` will print the log itself. 502 trap err_report ERR 503 504 TestContainerSwap 505 506 # TODO: runtime-rs doesn't support memory update currently 507 if [ "$KATA_HYPERVISOR" != "dragonball" ]; then 508 TestContainerMemoryUpdate 1 509 TestContainerMemoryUpdate 0 510 fi 511 512 TestKilledVmmCleanup 513 514 popd 515 } 516 517 main