github.com/kata-containers/tests@v0.0.0-20240307153542-772105b56064/integration/kubernetes/e2e_conformance/run.sh (about)

     1  #!/bin/bash
     2  #
     3  # Copyright (c) 2019 Intel Corporation
     4  #
     5  # SPDX-License-Identifier: Apache-2.0
     6  #
     7  # This script runs the Sonobuoy e2e Conformance tests.
     8  # Run this script once your K8s cluster is running.
     9  # WARNING: it is prefered to use containerd as the
    10  # runtime interface instead of cri-o as we have seen
    11  # errors with cri-o that still need to be debugged.
    12  
    13  set -o errexit
    14  set -o nounset
    15  set -o pipefail
    16  
    17  DEBUG=${DEBUG:-}
    18  [ -n "$DEBUG" ] && set -o xtrace
    19  
    20  export KUBECONFIG=$HOME/.kube/config
    21  SCRIPT_PATH=$(dirname "$(readlink -f "$0")")
    22  source "${SCRIPT_PATH}/../../../lib/common.bash"
    23  source "${SCRIPT_PATH}/../../../.ci/lib.sh"
    24  
    25  CI=${CI:-false}
    26  RUNTIME="${RUNTIME:-containerd-shim-kata-v2}"
    27  CRI_RUNTIME="${CRI_RUNTIME:-containerd}"
    28  MINIMAL_K8S_E2E="${MINIMAL_K8S_E2E:-false}"
    29  KATA_HYPERVISOR="${KATA_HYPERVISOR:-}"
    30  RUNTIME_CLASS="${RUNTIME_CLASS:-kata}"
    31  E2E_PARALLEL="${E2E_PARALLEL:-false}"
    32  
    33  # Overall Sonobuoy timeout in minutes.
    34  WAIT_TIME=${WAIT_TIME:-180}
    35  
    36  JOBS_FILE="${SCRIPT_PATH}/e2e_k8s_jobs.yaml"
    37  
    38  create_kata_webhook() {
    39  	pushd "${SCRIPT_PATH}/../../../kata-webhook" >>/dev/null
    40  	# Create certificates for the kata webhook
    41  	./create-certs.sh
    42  
    43  	# Apply kata-webhook deployment
    44  	kubectl apply -f deploy/
    45  
    46  	# Ensure the kata-webhook is working
    47  	./webhook-check.sh
    48  	popd
    49  }
    50  
    51  delete_kata_webhook() {
    52  	pushd "${SCRIPT_PATH}/../../../kata-webhook" >>/dev/null
    53  
    54  	# Apply kata-webhook deployment
    55  	kubectl delete -f deploy/
    56  
    57  	popd
    58  }
    59  
    60  get_sonobuoy() {
    61  	sonobuoy_repo=$(get_test_version "externals.sonobuoy.url")
    62  	version=$(get_test_version "externals.sonobuoy.version")
    63  	arch="$(${SCRIPT_PATH}/../../../.ci/kata-arch.sh --golang)"
    64  	sonobuoy_tar="sonobuoy_${version}_linux_${arch}.tar.gz"
    65  	install_path="/usr/bin"
    66  
    67  	curl -LO "${sonobuoy_repo}/releases/download/v${version}/${sonobuoy_tar}"
    68  	sudo tar -xzf "${sonobuoy_tar}" -C "$install_path"
    69  	sudo chmod +x "${install_path}/sonobuoy"
    70  	rm -f "${sonobuoy_tar}"
    71  }
    72  
    73  # Input:
    74  # - yaml list key
    75  # - yaml file
    76  # Output
    77  # - string: | separated values of the list
    78  yaml_list_to_str_regex() {
    79  	local list="${1}"
    80  	local yaml_file=${2:-"|"}
    81  	local query=".${list}"
    82  	query+=' | join("|")?'
    83  	"${GOPATH}/bin/yq" -j read "${yaml_file}" | jq -r "${query}"
    84  }
    85  
    86  run_sonobuoy() {
    87  	# Run Sonobuoy e2e tests
    88  	info "Starting sonobuoy execution."
    89  	info "When using kata as k8s runtime, the tests take around 2 hours to finish."
    90  
    91  	local skipped_tests_file="${SCRIPT_PATH}/skipped_tests_e2e.yaml"
    92  	local skipped_tests=$("${GOPATH}/bin/yq" read "${skipped_tests_file}" "${CRI_RUNTIME}")
    93  
    94  	# Default skipped tests for Conformance testing:
    95  	skip_options="Alpha|\[(Disruptive|Feature:[^\]]+|Flaky)\]"
    96  	local skip_list
    97  	skip_list=$(yaml_list_to_str_regex "\"${CRI_RUNTIME}\"" "${skipped_tests_file}")
    98  	if [ "${skip_list}" != "" ]; then
    99  		skip_options+="|${skip_list}"
   100  	fi
   101  
   102  	skip_list=$(yaml_list_to_str_regex "hypervisor.\"${KATA_HYPERVISOR}\"" "${skipped_tests_file}")
   103  	if [ "${skip_list}" != "" ]; then
   104  		skip_options+="|${skip_list}"
   105  	fi
   106  
   107  	local cmd="sonobuoy"
   108  	cmd+=" run"
   109  	cmd+=" --wait=${WAIT_TIME}"
   110  
   111  	if [ "${MINIMAL_K8S_E2E}" == "true" ]; then
   112  		minimal_focus=$(yaml_list_to_str_regex "jobs.minimal.focus" "${JOBS_FILE}")
   113  		# Not required to skip as only what is defined in toml should be executed.
   114  		if [ "${minimal_focus}" != "" ]; then
   115  			cmd+=" --e2e-focus=\"${minimal_focus}\""
   116  		else
   117  			# For MINIMAL_K8S_E2E focus list should not be empty
   118  			die "minimal focus query returned empty list"
   119  		fi
   120  	else
   121  		if [ "${skip_options}" != "" ]; then
   122  			cmd+=" --e2e-skip=\"${skip_options}\""
   123  		fi
   124  	fi
   125  
   126  	cmd+=" --plugin-env e2e.E2E_PARALLEL=${E2E_PARALLEL}"
   127  
   128  	echo "running: ${cmd}"
   129  	eval "${cmd}"
   130  
   131  	e2e_result_dir="$(mktemp -d /tmp/kata_e2e_results.XXXXX)"
   132  	{
   133  		sonobuoy status --json
   134  		if ! results=$(sonobuoy retrieve "${e2e_result_dir}"); then
   135  			die "failed to retrieve results"
   136  		fi
   137  
   138  		sonobuoy results "${results}" --mode=dump
   139  
   140  		pushd "${e2e_result_dir}"
   141  		tar -xvf "${results}"
   142  		e2e_result_log=$(find "${e2e_result_dir}/plugins/e2e" -name "e2e.log")
   143  		info "Results of the e2e tests can be found on: $e2e_result_log"
   144  		popd
   145  
   146  		failed_query='.plugins | .[] | select( ."result-status" | contains("failed"))'
   147  		failed=$(sonobuoy status --json | jq "${failed_query}")
   148  		if [ "${failed}" != "" ]; then
   149  			if [ "$CI" == true ]; then
   150  				cat "$e2e_result_log"
   151  			fi
   152  			sonobuoy status --json | jq "${failed_query}"
   153  			die "Found failed tests in end-to-end k8s test"
   154  		fi
   155  		local expected_passed_query="jobs.${CI_JOB:-}.passed"
   156  		local expected_passed=$("${GOPATH}/bin/yq" read "${JOBS_FILE}" "${expected_passed_query}")
   157  		if [ "${expected_passed}" != "" ]; then
   158  			passed_query='.plugins | [ .[]."result-counts".passed] | add'
   159  			passed=$(sonobuoy status --json | jq "${passed_query}")
   160  			if [ "${expected_passed}" != "${passed}" ]; then
   161  				die "expected ${expected_passed} tests to pass, but ${passed} passed"
   162  			else
   163  				info "All ${passed} tests passed as expected"
   164  			fi
   165  		else
   166  			info "Not found ${expected_passed_query} for job ${CI_JOB:-} in ${JOBS_FILE}"
   167  		fi
   168  	} | tee "${e2e_result_dir}/summary"
   169  }
   170  
   171  cleanup() {
   172  	if [ -d "${e2e_result_dir:-}" ]; then
   173  		info "Results directory "${e2e_result_dir}" will not be deleted"
   174  		log_file="${e2e_result_dir}/plugins/e2e/results/global/e2e.log"
   175  		if [ -f "${log_file}" ]; then
   176  			info "View results"
   177  			cat ${log_file}
   178  		else
   179  			warn "Tests results file ${log_file} not found"
   180  		fi
   181  	fi
   182  	{
   183  		if command -v sonobuoy &>/dev/null; then
   184  			info "View sonobuoy status"
   185  			sonobuoy status
   186  			# Remove sonobuoy execution pods
   187  			sonobuoy delete
   188  		fi
   189  	} || true
   190  
   191  	if [ "$RUNTIME" == "containerd-shim-kata-v2" ]; then
   192  		delete_kata_webhook
   193  	fi
   194  
   195  	# Revert the changes applied by the integration/kubernetes/init.sh
   196  	# script when it was called in our setup.sh.
   197  	info "Clean up the environment"
   198  	bash -c "$(readlink -f ${SCRIPT_PATH}/../cleanup_env.sh)" || true
   199  }
   200  
   201  trap "{ cleanup; }" EXIT
   202  
   203  main() {
   204  	if [ "$RUNTIME" == "containerd-shim-kata-v2" ]; then
   205  		create_kata_webhook
   206  	fi
   207  
   208  	get_sonobuoy
   209  	run_sonobuoy
   210  }
   211  
   212  main