Skip to content

Commit 6d7723a

Browse files
authored
Merge pull request kata-containers#5768 from stevenhorsman/CCv0-kubernetes-tests-common
tests: Refactor out common functions
2 parents 6e28155 + ad48887 commit 6d7723a

File tree

3 files changed

+120
-91
lines changed

3 files changed

+120
-91
lines changed

Diff for: integration/kubernetes/confidential/agent_image.bats

+3-91
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
# SPDX-License-Identifier: Apache-2.0
55
#
66

7-
load "${BATS_TEST_DIRNAME}/lib.sh"
8-
load "${BATS_TEST_DIRNAME}/../../confidential/lib.sh"
7+
load "${BATS_TEST_DIRNAME}/tests_common.sh"
98

109
tag_suffix=""
1110
if [ "$(uname -m)" != "x86_64" ]; then
@@ -27,84 +26,12 @@ image_unsigned_unprotected="quay.io/prometheus/busybox:latest"
2726
## Authenticated Image
2827
image_authenticated="quay.io/kata-containers/confidential-containers-auth:test"
2928

30-
original_kernel_params=$(get_kernel_params)
3129
# Allow to configure the runtimeClassName on pod configuration.
3230
RUNTIMECLASS="${RUNTIMECLASS:-kata}"
3331
test_tag="[cc][agent][kubernetes][containerd]"
3432

35-
# Create the test pod.
36-
#
37-
# Note: the global $sandbox_name, $pod_config should be set
38-
# already. It also relies on $CI and $DEBUG exported by CI scripts or
39-
# the developer, to decide how to set debug flags.
40-
#
41-
create_test_pod() {
42-
# On CI mode we only want to enable the agent debug for the case of
43-
# the test failure to obtain logs.
44-
if [ "${CI:-}" == "true" ]; then
45-
enable_full_debug
46-
elif [ "${DEBUG:-}" == "true" ]; then
47-
enable_full_debug
48-
enable_agent_console
49-
fi
50-
51-
echo "Create the test sandbox"
52-
echo "Pod config is: $pod_config"
53-
kubernetes_create_cc_pod $pod_config
54-
}
55-
56-
# Create a pod configuration out of a template file.
57-
#
58-
# Parameters:
59-
# $1 - the container image.
60-
# Return:
61-
# the path to the configuration file. The caller should not care about
62-
# its removal afterwards as it is created under the bats temporary
63-
# directory.
64-
#
65-
# Environment variables:
66-
# RUNTIMECLASS: set the runtimeClassName value from $RUNTIMECLASS.
67-
#
68-
new_pod_config() {
69-
local base_config="${FIXTURES_DIR}/pod-config.yaml.in"
70-
local image="$1"
71-
72-
local new_config=$(mktemp "${BATS_FILE_TMPDIR}/$(basename ${base_config}).XXX")
73-
IMAGE="$image" RUNTIMECLASS="$RUNTIMECLASS" envsubst < "$base_config" > "$new_config"
74-
echo "$new_config"
75-
}
76-
7733
setup() {
78-
start_date=$(date +"%Y-%m-%d %H:%M:%S")
79-
80-
pod_config="$(new_pod_config "$image_simple_signed")"
81-
pod_id=""
82-
83-
kubernetes_delete_all_cc_pods_if_any_exists || true
84-
85-
echo "Prepare containerd for Confidential Container"
86-
SAVED_CONTAINERD_CONF_FILE="/etc/containerd/config.toml.$$"
87-
configure_cc_containerd "$SAVED_CONTAINERD_CONF_FILE"
88-
89-
echo "Reconfigure Kata Containers"
90-
switch_image_service_offload on
91-
clear_kernel_params
92-
add_kernel_params "${original_kernel_params}"
93-
94-
setup_proxy
95-
switch_measured_rootfs_verity_scheme none
96-
}
97-
98-
# Check the logged messages on host have a given message.
99-
# Parameters:
100-
# $1 - the message
101-
#
102-
# Note: get the logs since the global $start_date.
103-
#
104-
assert_logs_contain() {
105-
local message="$1"
106-
# Note: with image-rs we get more that the default 1000 lines of logs
107-
journalctl -x -t kata --since "$start_date" -n 100000 | grep "$message"
34+
setup_common
10835
}
10936

11037
@test "$test_tag Test can launch pod with measured boot enabled" {
@@ -224,20 +151,5 @@ assert_logs_contain() {
224151
}
225152

226153
teardown() {
227-
# Print the logs and cleanup resources.
228-
echo "-- Kata logs:"
229-
sudo journalctl -xe -t kata --since "$start_date" -n 100000
230-
231-
# Allow to not destroy the environment if you are developing/debugging
232-
# tests.
233-
if [[ "${CI:-false}" == "false" && "${DEBUG:-}" == true ]]; then
234-
echo "Leaving changes and created resources untouched"
235-
return
236-
fi
237-
238-
kubernetes_delete_all_cc_pods_if_any_exists || true
239-
clear_kernel_params
240-
add_kernel_params "${original_kernel_params}"
241-
switch_image_service_offload off
242-
disable_full_debug
154+
teardown_common
243155
}

Diff for: integration/kubernetes/confidential/lib.sh

+13
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,19 @@ assert_pod_fail() {
113113
! kubernetes_create_cc_pod "$container_config" || /bin/false
114114
}
115115

116+
117+
# Check the logged messages on host have a given message.
118+
# Parameters:
119+
# $1 - the message
120+
#
121+
# Note: get the logs since the global $test_start_date.
122+
#
123+
assert_logs_contain() {
124+
local message="$1"
125+
# Note: with image-rs we get more that the default 1000 lines of logs
126+
journalctl -x -t kata --since "$test_start_date" -n 100000 | grep "$message"
127+
}
128+
116129
setup_decryption_files_in_guest() {
117130
checkout_doc_repo_dir
118131
add_kernel_params "agent.aa_kbc_params=offline_fs_kbc::null"

Diff for: integration/kubernetes/confidential/tests_common.sh

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/bin/bash
2+
# Copyright (c) 2021, 2023 IBM Corporation
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# This provides generic functions to use in the tests.
7+
#
8+
9+
load "${BATS_TEST_DIRNAME}/lib.sh"
10+
load "${BATS_TEST_DIRNAME}/../../confidential/lib.sh"
11+
12+
original_kernel_params=$(get_kernel_params)
13+
14+
# Common setup for tests.
15+
#
16+
# Global variables exported:
17+
# $test_start_date - test start time.
18+
# $pod_config - path to default pod configuration file.
19+
# $original_kernel_params - saved the original list of kernel parameters.
20+
#
21+
setup_common() {
22+
test_start_date=$(date +"%Y-%m-%d %H:%M:%S")
23+
24+
pod_config="$(new_pod_config "$image_simple_signed")"
25+
pod_id=""
26+
27+
kubernetes_delete_all_cc_pods_if_any_exists || true
28+
29+
echo "Prepare containerd for Confidential Container"
30+
SAVED_CONTAINERD_CONF_FILE="/etc/containerd/config.toml.$$"
31+
configure_cc_containerd "$SAVED_CONTAINERD_CONF_FILE"
32+
33+
echo "Reconfigure Kata Containers"
34+
switch_image_service_offload on
35+
clear_kernel_params
36+
add_kernel_params "${original_kernel_params}"
37+
38+
setup_proxy
39+
switch_measured_rootfs_verity_scheme none
40+
}
41+
42+
# Common teardown for tests. Use alongside setup_common().
43+
#
44+
teardown_common() {
45+
# Print the logs and cleanup resources.
46+
echo "-- Kata logs:"
47+
sudo journalctl -xe -t kata --since "$test_start_date" -n 100000
48+
49+
# Allow to not destroy the environment if you are developing/debugging
50+
# tests.
51+
if [[ "${CI:-false}" == "false" && "${DEBUG:-}" == true ]]; then
52+
echo "Leaving changes and created resources untouched"
53+
return
54+
fi
55+
56+
kubernetes_delete_all_cc_pods_if_any_exists || true
57+
clear_kernel_params
58+
add_kernel_params "${original_kernel_params}"
59+
switch_image_service_offload off
60+
disable_full_debug
61+
}
62+
63+
64+
# Create the test pod.
65+
#
66+
# Note: the global $pod_config should be set in setup_common
67+
# already. It also relies on $CI and $DEBUG exported by CI scripts or
68+
# the developer, to decide how to set debug flags.
69+
#
70+
create_test_pod() {
71+
# On CI mode we only want to enable the agent debug for the case of
72+
# the test failure to obtain logs.
73+
if [ "${CI:-}" == "true" ]; then
74+
enable_full_debug
75+
elif [ "${DEBUG:-}" == "true" ]; then
76+
enable_full_debug
77+
enable_agent_console
78+
fi
79+
80+
echo "Create the test sandbox"
81+
echo "Pod config is: $pod_config"
82+
kubernetes_create_cc_pod $pod_config
83+
}
84+
85+
# Create a pod configuration out of a template file.
86+
#
87+
# Parameters:
88+
# $1 - the container image.
89+
# Return:
90+
# the path to the configuration file. The caller should not care about
91+
# its removal afterwards as it is created under the bats temporary
92+
# directory.
93+
#
94+
# Environment variables:
95+
# RUNTIMECLASS: set the runtimeClassName value from $RUNTIMECLASS.
96+
#
97+
new_pod_config() {
98+
local base_config="${FIXTURES_DIR}/pod-config.yaml.in"
99+
local image="$1"
100+
101+
local new_config=$(mktemp "${BATS_FILE_TMPDIR}/$(basename ${base_config}).XXX")
102+
IMAGE="$image" RUNTIMECLASS="$RUNTIMECLASS" envsubst < "$base_config" > "$new_config"
103+
echo "$new_config"
104+
}

0 commit comments

Comments
 (0)