github.com/dmaizel/tests@v0.0.0-20210728163746-cae6a2d9cee8/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  set -o errexit
     9  set -o nounset
    10  set -o pipefail
    11  set -o errtrace
    12  
    13  # runc is installed in /usr/local/sbin/ add that path
    14  export PATH="$PATH:/usr/local/sbin"
    15  
    16  # Runtime to be used for testing
    17  RUNTIME=${RUNTIME:-containerd-shim-kata-v2}
    18  SHIMV2_TEST=${SHIMV2_TEST:-""}
    19  FACTORY_TEST=${FACTORY_TEST:-""}
    20  KILL_VMM_TEST=${KILL_VMM_TEST:-""}
    21  KATA_HYPERVISOR="${KATA_HYPERVISOR:-qemu}"
    22  ARCH=$(uname -m)
    23  
    24  default_runtime_type="io.containerd.runc.v2"
    25  # Type of containerd runtime to be tested
    26  containerd_runtime_type="${default_runtime_type}"
    27  # Runtime to be use for the test in containerd
    28  containerd_runtime_test=${RUNTIME}
    29  if [ -n "${SHIMV2_TEST}" ]; then
    30  	containerd_runtime_type="io.containerd.kata.v2"
    31  	containerd_runtime_test="io.containerd.kata.v2"
    32  fi
    33  
    34  readonly runc_runtime_bin=$(command -v "runc")
    35  
    36  readonly CRITEST=${GOPATH}/bin/critest
    37  
    38  # Flag to do tasks for CI
    39  SNAP_CI=${SNAP_CI:-""}
    40  CI=${CI:-""}
    41  
    42  containerd_shim_path="$(command -v containerd-shim)"
    43  readonly cri_containerd_repo="github.com/containerd/containerd"
    44  readonly cri_repo="github.com/containerd/cri"
    45  
    46  #containerd config file
    47  readonly tmp_dir=$(mktemp -t -d test-cri-containerd.XXXX)
    48  export REPORT_DIR="${tmp_dir}"
    49  readonly CONTAINERD_CONFIG_FILE="${tmp_dir}/test-containerd-config"
    50  readonly default_containerd_config="/etc/containerd/config.toml"
    51  readonly default_containerd_config_backup="$CONTAINERD_CONFIG_FILE.backup"
    52  readonly kata_config="/etc/kata-containers/configuration.toml"
    53  readonly default_kata_config="/usr/share/defaults/kata-containers/configuration.toml"
    54  
    55  info() {
    56  	echo -e "INFO: $*"
    57  }
    58  
    59  die() {
    60  	echo >&2 "ERROR: $*"
    61  	exit 1
    62  }
    63  
    64  ci_config() {
    65  	sudo mkdir -p $(dirname "${kata_config}")
    66  	sudo cp "${default_kata_config}" "${kata_config}"
    67  
    68  	source /etc/os-release || source /usr/lib/os-release
    69  	ID=${ID:-""}
    70  	if [ "$ID" == ubuntu ] &&  [ -n "${CI}" ] ;then
    71  		# https://github.com/kata-containers/tests/issues/352
    72  		if [ -n "${FACTORY_TEST}" ]; then
    73  			sudo sed -i -e 's/^#enable_template.*$/enable_template = true/g' "${kata_config}"
    74  			echo "init vm template"
    75  			sudo -E PATH=$PATH "$RUNTIME" factory init
    76  		fi
    77  	fi
    78  
    79  	SCRIPT_PATH=$(dirname "$(readlink -f "$0")")
    80  	if [ -n "${CI}" ]; then
    81  		(
    82  		echo "Install cni config"
    83  		${SCRIPT_PATH}/../../../.ci/configure_cni.sh
    84  		)
    85  	fi
    86  
    87  	echo "enable debug for kata-runtime"
    88  	sudo sed -i 's/^#enable_debug =/enable_debug =/g' ${kata_config}
    89  	sudo sed -i 's/^#enable_debug =/enable_debug =/g' ${default_kata_config}
    90  }
    91  
    92  ci_cleanup() {
    93  	source /etc/os-release || source /usr/lib/os-release
    94  
    95  	if [ -n "${FACTORY_TEST}" ]; then
    96  		echo "destroy vm template"
    97  		sudo -E PATH=$PATH "$RUNTIME" factory destroy
    98  	fi
    99  
   100  	if [ -n "${KILL_VMM_TEST}" ] && [ -e "$default_containerd_config_backup" ]; then
   101  		echo "restore containerd config"
   102  		sudo systemctl stop containerd
   103  		sudo cp "$default_containerd_config_backup" "$default_containerd_config"
   104  	fi
   105  
   106  	ID=${ID:-""}
   107  	if [ "$ID" == ubuntu ]; then
   108  		if [ -n "${SNAP_CI}" ]; then
   109  			# restore default configuration
   110  			sudo cp "${default_kata_config}" "${kata_config}"
   111  		elif [ -n "${CI}" ] ;then
   112  			[ -f "${kata_config}" ] && sudo rm "${kata_config}"
   113  		fi
   114  	fi
   115  }
   116  
   117  create_containerd_config() {
   118  	local runtime="$1"
   119  	[ -n "${runtime}" ] || die "need runtime to create config"
   120  
   121  	local runtime_type="${containerd_runtime_type}"
   122  	if [ "${runtime}" == "runc" ]; then
   123  		runtime_type="io.containerd.runc.v2"
   124  	fi
   125  	local containerd_runtime="${runtime}"
   126  	if [ "${runtime_type}" == "${default_runtime_type}" ];then
   127  		local containerd_runtime=$(command -v "${runtime}")
   128  	fi
   129  	# Remove dots.  Dots are used by toml syntax as atribute separator
   130  	runtime="${runtime//./-}"
   131  
   132  	cat << EOT | sudo tee "${CONTAINERD_CONFIG_FILE}"
   133  [plugins]
   134    [plugins.cri]
   135      [plugins.cri.containerd]
   136  	default_runtime_name = "$runtime"
   137        [plugins.cri.containerd.runtimes.${runtime}]
   138          runtime_type = "${runtime_type}"
   139          [plugins.cri.containerd.runtimes.${runtime}.options]
   140            Runtime = "${containerd_runtime}"
   141  [plugins.linux]
   142         shim = "${containerd_shim_path}"
   143  EOT
   144  
   145  if [ "$KATA_HYPERVISOR" == "firecracker" ]; then
   146  	sudo sed -i 's|^\(\[plugins\]\).*|\1\n  \[plugins.devmapper\]\n    pool_name = \"contd-thin-pool\"\n    base_image_size = \"4096MB\"|' ${CONTAINERD_CONFIG_FILE}
   147  	echo "Devicemapper configured"
   148  	cat "${CONTAINERD_CONFIG_FILE}"
   149  fi
   150  
   151  }
   152  
   153  cleanup() {
   154  	[ -d "$tmp_dir" ] && rm -rf "${tmp_dir}"
   155  	ci_cleanup
   156  }
   157  
   158  trap cleanup EXIT
   159  
   160  err_report() {
   161  	echo "ERROR: containerd log :"
   162  	echo "-------------------------------------"
   163  	cat "${REPORT_DIR}/containerd.log"
   164  	echo "-------------------------------------"
   165  }
   166  
   167  trap err_report ERR
   168  
   169  restart_docker() {
   170  	info "restart docker service"
   171  
   172  	back_file=$(mktemp)
   173  
   174  	#avoid the "Start request repeated too quickly" error
   175          if [ -f "/lib/systemd/system/docker.service" ]; then
   176  		cp /lib/systemd/system/docker.service ${back_file}
   177                  sudo sed -i 's/StartLimitBurst.*$/StartLimitBurst=0/g' /lib/systemd/system/docker.service
   178          elif [ -f "/usr/lib/systemd/system/docker.service" ]; then
   179  		cp /usr/lib/systemd/system/docker.service ${back_file}
   180                  sudo sed -i 's/StartLimitBurst.*$/StartLimitBurst=0/g' /usr/lib/systemd/system/docker.service
   181          fi
   182          sudo systemctl daemon-reload
   183          sudo systemctl restart docker
   184  
   185  	#recover docker service file
   186  	if [ -f "/lib/systemd/system/docker.service" ]; then
   187  		sudo mv ${back_file} /lib/systemd/system/docker.service
   188  	elif [ -f "/usr/lib/systemd/system/docker.service" ]; then
   189  		sudo mv ${back_file} /usr/lib/systemd/system/docker.service
   190  	fi
   191          sudo systemctl daemon-reload
   192  }
   193  
   194  check_daemon_setup() {
   195  	info "containerd(cri): Check daemon works with runc"
   196  	create_containerd_config "runc"
   197  
   198  	#restart docker service as TestImageLoad depends on it
   199  	restart_docker
   200  
   201  	sudo -E PATH="${PATH}:/usr/local/bin" \
   202  		REPORT_DIR="${REPORT_DIR}" \
   203  		FOCUS="TestImageLoad" \
   204  		RUNTIME="" \
   205  		CONTAINERD_CONFIG_FILE="$CONTAINERD_CONFIG_FILE" \
   206  		make -e test-integration
   207  }
   208  
   209  testContainerStart() {
   210  	local pod_yaml=${REPORT_DIR}/pod.yaml
   211  	local container_yaml=${REPORT_DIR}/container.yaml
   212  	local image="busybox:latest"
   213  
   214  	cat << EOF > "${pod_yaml}"
   215  metadata:
   216    name: busybox-sandbox1
   217  EOF
   218  
   219  	cat << EOF > "${container_yaml}"
   220  metadata:
   221    name: busybox-killed-vmm
   222  image:
   223    image: "$image"
   224  command:
   225  - top
   226  EOF
   227  
   228  	sudo cp "$default_containerd_config" "$default_containerd_config_backup"
   229  	sudo cp $CONTAINERD_CONFIG_FILE "$default_containerd_config"
   230  
   231  	sudo systemctl restart containerd
   232  
   233  	sudo crictl pull $image
   234  	podid=$(sudo crictl runp $pod_yaml)
   235  	cid=$(sudo crictl create $podid $container_yaml $pod_yaml)
   236  	sudo crictl start $cid
   237  }
   238  
   239  testContainerStop() {
   240  	info "stop pod $podid"
   241  	sudo crictl stopp $podid
   242  	info "remove pod $podid"
   243  	sudo crictl rmp $podid
   244  
   245  	sudo cp "$default_containerd_config_backup" "$default_containerd_config"
   246  	sudo systemctl restart containerd
   247  }
   248  
   249  TestKilledVmmCleanup() {
   250  	if [ -z "${SHIMV2_TEST}" ] || [ -z "${KILL_VMM_TEST}" ]; then
   251  		return
   252  	fi
   253  
   254  	info "test killed vmm cleanup"
   255  
   256  	testContainerStart
   257  
   258  	qemu_pid=$(ps aux|grep qemu|grep -v grep|awk '{print $2}')
   259  	info "kill qemu $qemu_pid"
   260  	sudo kill -SIGKILL $qemu_pid
   261  	# sleep to let shimv2 exit
   262  	sleep 1
   263  	remained=$(ps aux|grep shimv2|grep -v grep || true)
   264  	[ -z $remained ] || die "found remaining shimv2 process $remained"
   265  
   266  	testContainerStop
   267  
   268  	info "stop containerd"
   269  }
   270  
   271  TestContainerMemoryUpdate() {
   272  	if [[ "${KATA_HYPERVISOR}" != "qemu" ]] || [[ "${ARCH}" == "ppc64le" ]] || [[ "${ARCH}" == "s390x" ]]; then
   273  		return
   274  	fi
   275  
   276  	test_virtio_mem=$1
   277  
   278  	if [ $test_virtio_mem -eq 1 ]; then
   279  		if [[ "$ARCH" != "x86_64" ]]; then
   280  			return
   281  		fi
   282  		info "Test container memory update with virtio-mem"
   283  
   284  		sudo sed -i -e 's/^#enable_virtio_mem.*$/enable_virtio_mem = true/g' "${kata_config}"
   285  	else
   286  		info "Test container memory update without virtio-mem"
   287  
   288  		sudo sed -i -e 's/^enable_virtio_mem.*$/#enable_virtio_mem = true/g' "${kata_config}"
   289  	fi
   290  
   291  	testContainerStart
   292  
   293  	vm_size=$(($(crictl exec $cid cat /proc/meminfo | grep "MemTotal:" | awk '{print $2}')*1024))
   294  	if [ $vm_size -gt $((2*1024*1024*1024)) ] || [ $vm_size -lt $((2*1024*1024*1024-128*1024*1024)) ]; then
   295  		testContainerStop
   296  		die "The VM memory size $vm_size before update is not right"
   297  	fi
   298  
   299  	sudo crictl update --memory $((2*1024*1024*1024)) $cid
   300  	sleep 1
   301  
   302  	vm_size=$(($(crictl exec $cid cat /proc/meminfo | grep "MemTotal:" | awk '{print $2}')*1024))
   303  	if [ $vm_size -gt $((4*1024*1024*1024)) ] || [ $vm_size -lt $((4*1024*1024*1024-128*1024*1024)) ]; then
   304  		testContainerStop
   305  		die "The VM memory size $vm_size after increase is not right"
   306  	fi
   307  
   308  	if [ $test_virtio_mem -eq 1 ]; then
   309  		sudo crictl update --memory $((1*1024*1024*1024)) $cid
   310  		sleep 1
   311  
   312  		vm_size=$(($(crictl exec $cid cat /proc/meminfo | grep "MemTotal:" | awk '{print $2}')*1024))
   313  		if [ $vm_size -gt $((3*1024*1024*1024)) ] || [ $vm_size -lt $((3*1024*1024*1024-128*1024*1024)) ]; then
   314  			testContainerStop
   315  			die "The VM memory size $vm_size after decrease is not right"
   316  		fi
   317  	fi
   318  
   319  	testContainerStop
   320  }
   321  
   322  main() {
   323  
   324  	info "Stop crio service"
   325  	systemctl is-active --quiet crio && sudo systemctl stop crio
   326  
   327  	info "Stop containerd service"
   328  	systemctl is-active --quiet containerd && sudo systemctl stop containerd
   329  
   330  	# Configure enviroment if running in CI
   331  	ci_config
   332  
   333  	# make sure cri-containerd test install the proper critest version its testing
   334  	rm -f "${CRITEST}"
   335  
   336  	go get ${cri_repo}
   337  	pushd "${GOPATH}/src/${cri_repo}"
   338  
   339  	git reset HEAD
   340  	git checkout master
   341  	# switch to the default pause image set by containerd:1.5.x
   342  	sed -i 's#k8s.gcr.io/pause:3.[0-9]#k8s.gcr.io/pause:3.5#' integration/main_test.go
   343  	cp "${SCRIPT_PATH}/container_restart_test.go.patch" ./integration/container_restart_test.go
   344  
   345  	#test cri using the built/installed containerd instead of containerd built in cri
   346  	sed -i 's#${ROOT}/_output/containerd#/usr/local/bin/containerd#' hack/test-utils.sh
   347  
   348  	# Make sure the right artifacts are going to be built
   349  	make clean
   350  
   351  	check_daemon_setup
   352  
   353  	info "containerd(cri): testing using runtime: ${containerd_runtime_test}"
   354  
   355  	create_containerd_config "${containerd_runtime_test}"
   356  
   357  	info "containerd(cri): Running cri-tools"
   358  	sudo -E PATH="${PATH}:/usr/local/bin" \
   359  		FOCUS="runtime should support basic operations on container" \
   360  		RUNTIME="" \
   361  		SKIP="runtime should support execSync with timeout" \
   362  		REPORT_DIR="${REPORT_DIR}" \
   363  		CONTAINERD_CONFIG_FILE="$CONTAINERD_CONFIG_FILE" \
   364  		make -e test-cri
   365  
   366  	info "containerd(cri): Running test-integration"
   367  
   368  	passing_test=(
   369  	TestContainerStats
   370  	TestContainerRestart
   371  	TestContainerListStatsWithIdFilter
   372  	TestContainerListStatsWithIdSandboxIdFilter
   373  	TestDuplicateName
   374  	TestImageLoad
   375  	TestImageFSInfo
   376  	TestSandboxCleanRemove
   377  	)
   378  
   379  	if [[ "${KATA_HYPERVISOR}" == "cloud-hypervisor" || \
   380  		"${KATA_HYPERVISOR}" == "qemu" ]]; then
   381  		issue="https://github.com/kata-containers/tests/issues/2318"
   382  		info "${KATA_HYPERVISOR} fails with TestContainerListStatsWithSandboxIdFilter }"
   383  		info "see ${issue}"
   384  	else
   385  		passing_test+=("TestContainerListStatsWithSandboxIdFilter")
   386  	fi
   387  
   388  	for t in "${passing_test[@]}"
   389  	do
   390  		sudo -E PATH="${PATH}:/usr/local/bin" \
   391  			REPORT_DIR="${REPORT_DIR}" \
   392  			FOCUS="${t}" \
   393  			RUNTIME="" \
   394  			CONTAINERD_CONFIG_FILE="$CONTAINERD_CONFIG_FILE" \
   395  			make -e test-integration
   396  	done
   397  
   398  	TestContainerMemoryUpdate 1
   399  	TestContainerMemoryUpdate 0
   400  
   401  	TestKilledVmmCleanup
   402  
   403  	popd
   404  }
   405  
   406  main