Skip to content

Commit 2d1a179

Browse files
author
ChengyuZhu6
committed
ci: Add test cases for CoCo image pulling without forked containerd
Additional tests are necessary to verify new feature that pulling image without forked containerd in CoCo. Fixes kata-containers#5763 Depends: kata-containers/kata-containers#7688 kata-containers/kata-containers#7676 Signed-off-by: ChengyuZhu6 <[email protected]>
1 parent 021bf58 commit 2d1a179

File tree

3 files changed

+201
-1
lines changed

3 files changed

+201
-1
lines changed

Diff for: integration/confidential/lib.sh

+50
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ source "${BATS_TEST_DIRNAME}/../../../lib/common.bash"
1212
source "${BATS_TEST_DIRNAME}/../../../.ci/lib.sh"
1313
FIXTURES_DIR="${BATS_TEST_DIRNAME}/fixtures"
1414
SHARED_FIXTURES_DIR="${BATS_TEST_DIRNAME}/../../confidential/fixtures"
15+
NYDUS_SNAPSHOTTER_CONFIG="/opt/confidential-containers/share/remote-snapshotter/config.toml"
16+
NYDUS_SNAPSHOTTER_BINARY="/opt/confidential-containers/bin/containerd-nydus-grpc"
17+
CONTAINERD_CONFIG="/etc/containerd/config.toml"
1518

1619
# Toggle between true and false the service_offload configuration of
1720
# the Kata agent.
@@ -440,3 +443,50 @@ EOF
440443
EOF
441444
fi
442445
}
446+
447+
###############################################################################
448+
449+
# remote-snapshotter
450+
451+
configure_remote_snapshotter() {
452+
case "${SNAPSHOTTER:-}" in
453+
"nydus")
454+
configure_nydus_snapshotter
455+
;;
456+
*) ;;
457+
458+
esac
459+
}
460+
check_containerd_version() {
461+
containerd_version=$(containerd --version | awk '{print $3}' | sort -V | tail -n 1)
462+
if echo $containerd_version | grep -q "^v1.7"; then
463+
return 1
464+
else
465+
return 0
466+
fi
467+
}
468+
configure_containerd_for_nydus_snapshotter() {
469+
sed -i 's/disable_snapshot_annotations = .*/disable_snapshot_annotations = false/g' "$CONTAINERD_CONFIG"
470+
if check_containerd_version; then
471+
sed -i '/\[plugins\."io\.containerd\.grpc\.v1\.cri"\.containerd\.runtimes\.'"$RUNTIMECLASS"'\]/a\ snapshotter = "nydus"\n' "$CONTAINERD_CONFIG"
472+
else
473+
sed -i 's/snapshotter = .*/snapshotter = "nydus"/g' "$CONTAINERD_CONFIG"
474+
fi
475+
}
476+
remove_nydus_snapshotter_from_containerd() {
477+
sed -i 's/disable_snapshot_annotations = .*/disable_snapshot_annotations = true/g' "$CONTAINERD_CONFIG"
478+
if check_containerd_version; then
479+
sed -i '/\[plugins\."io\.containerd\.grpc\.v1\.cri"\.containerd\.runtimes\.'"$RUNTIMECLASS"'\]/,/\[/{/snapshotter = "nydus"/d;}' "$CONTAINERD_CONFIG"
480+
else
481+
sed -i 's/snapshotter = .*/snapshotter = "overlayfs"/g' "$CONTAINERD_CONFIG"
482+
fi
483+
}
484+
restart_nydus_snapshotter() {
485+
echo "Restart nydus snapshotter"
486+
}
487+
488+
configure_nydus_snapshotter() {
489+
echo "Configure nydus snapshotter"
490+
sed -i "s/export_mode = .*/export_mode = \"$EXPORT_MODE\"/" $NYDUS_SNAPSHOTTER_CONFIG
491+
restart_nydus_snapshotter
492+
}

Diff for: integration/kubernetes/confidential/fixtures/pod-config.yaml.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
apiVersion: v1
66
kind: Pod
77
metadata:
8-
name: busybox-cc
8+
name: busybox-cc$INDEX
99
spec:
1010
runtimeClassName: $RUNTIMECLASS
1111
containers:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#!/usr/bin/env bats
2+
# Copyright (c) 2022 IBM Corporation
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
load "${BATS_TEST_DIRNAME}/lib.sh"
8+
load "${BATS_TEST_DIRNAME}/../../confidential/lib.sh"
9+
10+
tag_suffix=""
11+
if [ "$(uname -m)" != "x86_64" ]; then
12+
tag_suffix="-$(uname -m)"
13+
fi
14+
15+
# Images used on the tests.
16+
17+
image_unsigned_protected="quay.io/kata-containers/confidential-containers:unsigned${tag_suffix}"
18+
19+
original_kernel_params=$(get_kernel_params)
20+
# Allow to configure the runtimeClassName on pod configuration.
21+
RUNTIMECLASS="${RUNTIMECLASS:-kata}"
22+
test_tag="[cc][agent][kubernetes][containerd]"
23+
24+
# Create the test pod.
25+
#
26+
# Note: the global $sandbox_name, $pod_config should be set
27+
# already. It also relies on $CI and $DEBUG exported by CI scripts or
28+
# the developer, to decide how to set debug flags.
29+
#
30+
create_test_pod() {
31+
local pod_config="$1"
32+
# On CI mode we only want to enable the agent debug for the case of
33+
# the test failure to obtain logs.
34+
if [ "${CI:-}" == "true" ]; then
35+
enable_full_debug
36+
elif [ "${DEBUG:-}" == "true" ]; then
37+
enable_full_debug
38+
enable_agent_console
39+
fi
40+
41+
echo "Create the test sandbox"
42+
echo "Pod config is: $pod_config"
43+
kubernetes_create_cc_pod $pod_config
44+
}
45+
46+
# Create a pod configuration out of a template file.
47+
#
48+
# Parameters:
49+
# $1 - the container image.
50+
# Return:
51+
# the path to the configuration file. The caller should not care about
52+
# its removal afterwards as it is created under the bats temporary
53+
# directory.
54+
#
55+
# Environment variables:
56+
# RUNTIMECLASS: set the runtimeClassName value from $RUNTIMECLASS.
57+
#
58+
new_pod_config() {
59+
local base_config="${FIXTURES_DIR}/pod-config.yaml.in"
60+
local image="$1"
61+
local index="$2"
62+
63+
local new_config=$(mktemp "${BATS_FILE_TMPDIR}/$(basename ${base_config}).XXX")
64+
IMAGE="$image" RUNTIMECLASS="$RUNTIMECLASS" INDEX="$2" envsubst < "$base_config" > "$new_config"
65+
echo "$new_config"
66+
}
67+
68+
setup() {
69+
start_date=$(date +"%Y-%m-%d %H:%M:%S")
70+
71+
pod_config="$(new_pod_config "$image_simple_signed")"
72+
pod_id=""
73+
74+
kubernetes_delete_all_cc_pods_if_any_exists || true
75+
76+
echo "Prepare containerd for Confidential Container"
77+
SAVED_CONTAINERD_CONF_FILE="/etc/containerd/config.toml.$$"
78+
configure_cc_containerd "$SAVED_CONTAINERD_CONF_FILE"
79+
80+
echo "Reconfigure Kata Containers"
81+
switch_image_service_offload off
82+
clear_kernel_params
83+
add_kernel_params "${original_kernel_params}"
84+
85+
setup_proxy
86+
switch_measured_rootfs_verity_scheme none
87+
}
88+
89+
# Check the logged messages on host have a given message.
90+
# Parameters:
91+
# $1 - the message
92+
#
93+
# Note: get the logs since the global $start_date.
94+
#
95+
assert_logs_contain() {
96+
local message="$1"
97+
# Note: with image-rs we get more that the default 1000 lines of logs
98+
journalctl -x -t kata --since "$start_date" -n 100000 | grep "$message"
99+
}
100+
101+
@test "$test_tag Test can pull an image as a raw block disk image to guest with dm-verity enabled" {
102+
if [ "${SNAPSHOTTER}" = "nydus" ]; then
103+
EXPORT_MODE="image_block_with_verity" RUNTIMECLASS="$RUNTIMECLASS" configure_remote_snapshotter
104+
pod_config="$(new_pod_config "$image_unsigned_unprotected")"
105+
echo $pod_config
106+
create_test_pod "$pod_config"
107+
fi
108+
}
109+
110+
@test "$test_tag Test can create two pods by pulling the image only once" {
111+
if [ "${SNAPSHOTTER}" = "nydus" ]; then
112+
EXPORT_MODE="image_block_with_verity" RUNTIMECLASS="$RUNTIMECLASS" configure_remote_snapshotter
113+
114+
pod_config_1="$(new_pod_config "$image_unsigned_unprotected" "1")"
115+
echo $pod_config_1
116+
create_test_pod $pod_config_1
117+
118+
pod_config_2="$(new_pod_config "$image_unsigned_unprotected" "2")"
119+
echo $pod_config_2
120+
create_test_pod $pod_config_2
121+
fi
122+
}
123+
124+
@test "$test_tag Test can pull an image inside the guest with remote-snapshotter" {
125+
if [ "${SNAPSHOTTER}" = "nydus" ]; then
126+
switch_image_service_offload on
127+
EXPORT_MODE="image_guest_pull" RUNTIMECLASS="$RUNTIMECLASS" configure_remote_snapshotter
128+
create_test_pod
129+
fi
130+
}
131+
132+
teardown() {
133+
# Print the logs and cleanup resources.
134+
echo "-- Kata logs:"
135+
sudo journalctl -xe -t kata --since "$start_date" -n 100000
136+
137+
# Allow to not destroy the environment if you are developing/debugging
138+
# tests.
139+
if [[ "${CI:-false}" == "false" && "${DEBUG:-}" == true ]]; then
140+
echo "Leaving changes and created resources untouched"
141+
return
142+
fi
143+
144+
kubernetes_delete_all_cc_pods_if_any_exists || true
145+
clear_kernel_params
146+
add_kernel_params "${original_kernel_params}"
147+
switch_image_service_offload off
148+
remove_nydus_snapshotter_from_containerd
149+
disable_full_debug
150+
}

0 commit comments

Comments
 (0)