Skip to content

Commit 5ab8ee9

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 5ab8ee9

File tree

3 files changed

+206
-0
lines changed

3 files changed

+206
-0
lines changed

Diff for: integration/confidential/lib.sh

+49
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ 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+
CONTAINERD_CONFIG="/etc/containerd/config.toml"
1517

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

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

+9
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,15 @@ assert_logs_contain() {
223223
assert_logs_contain 'failed to pull manifest Not authorized'
224224
}
225225

226+
@test "$test_tag Test can pull an image as a raw block disk image to guest with dm-verity enabled" {
227+
if [ "${SNAPSHOTTER}" = "nydus" ]; then
228+
EXPORT_MODE="image_block_with_verity" RUNTIMECLASS="$RUNTIMECLASS" configure_remote_snapshotter
229+
pod_config="$(new_pod_config "$image_unsigned_unprotected")"
230+
echo $pod_config
231+
create_test_pod
232+
fi
233+
}
234+
226235
teardown() {
227236
# Print the logs and cleanup resources.
228237
echo "-- Kata logs:"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
## Cosign
17+
image_cosigned="quay.io/kata-containers/confidential-containers:cosign-signed${tag_suffix}"
18+
image_cosigned_other="quay.io/kata-containers/confidential-containers:cosign-signed-key2"
19+
20+
## Simple Signing
21+
22+
image_simple_signed="quay.io/kata-containers/confidential-containers:signed${tag_suffix}"
23+
image_signed_protected_other="quay.io/kata-containers/confidential-containers:other_signed${tag_suffix}"
24+
image_unsigned_protected="quay.io/kata-containers/confidential-containers:unsigned${tag_suffix}"
25+
image_unsigned_unprotected="quay.io/prometheus/busybox:latest"
26+
27+
## Authenticated Image
28+
image_authenticated="quay.io/kata-containers/confidential-containers-auth:test"
29+
30+
original_kernel_params=$(get_kernel_params)
31+
# Allow to configure the runtimeClassName on pod configuration.
32+
RUNTIMECLASS="${RUNTIMECLASS:-kata}"
33+
test_tag="[cc][agent][kubernetes][containerd]"
34+
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+
77+
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"
108+
}
109+
110+
@test "$test_tag Test can pull an image as a raw block disk image to guest with dm-verity enabled" {
111+
if [ "${SNAPSHOTTER}" = "nydus" ]; then
112+
EXPORT_MODE="image_block_with_verity" RUNTIMECLASS="$RUNTIMECLASS" configure_remote_snapshotter
113+
pod_config="$(new_pod_config "$image_unsigned_unprotected")"
114+
echo $pod_config
115+
create_test_pod
116+
fi
117+
}
118+
119+
@test "$test_tag Test can pull an unencrypted image inside the guest" {
120+
create_test_pod
121+
122+
echo "Check the image was not pulled in the host"
123+
local pod_id=$(kubectl get pods -o jsonpath='{.items..metadata.name}')
124+
retrieve_sandbox_id
125+
rootfs=($(find /run/kata-containers/shared/sandboxes/${sandbox_id}/shared \
126+
-name rootfs))
127+
[ ${#rootfs[@]} -eq 1 ]
128+
}
129+
130+
131+
teardown() {
132+
# Print the logs and cleanup resources.
133+
echo "-- Kata logs:"
134+
sudo journalctl -xe -t kata --since "$start_date" -n 100000
135+
136+
# Allow to not destroy the environment if you are developing/debugging
137+
# tests.
138+
if [[ "${CI:-false}" == "false" && "${DEBUG:-}" == true ]]; then
139+
echo "Leaving changes and created resources untouched"
140+
return
141+
fi
142+
143+
kubernetes_delete_all_cc_pods_if_any_exists || true
144+
clear_kernel_params
145+
add_kernel_params "${original_kernel_params}"
146+
switch_image_service_offload off
147+
disable_full_debug
148+
}

0 commit comments

Comments
 (0)