Debian Codegraph @

script/kind/test.sh

raw
1 #!/bin/bash
2
3 # Copyright The containerd Authors.
4
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8
9 # http://www.apache.org/licenses/LICENSE-2.0
10
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 set -euo pipefail
18
19 CONTEXT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/"
20 REPO="${CONTEXT}../../"
21 REGISTRY_HOST=kind-private-registry
22 REGISTRY_NETWORK=kind_registry_network
23 DUMMYUSER=dummyuser
24 DUMMYPASS=dummypass
25 TESTIMAGE_ORIGIN="ghcr.io/stargz-containers/ubuntu:22.04"
26 TESTIMAGE="${REGISTRY_HOST}:5000/library/ubuntu:22.04"
27 KIND_CLUSTER_NAME=kind-stargz-snapshotter
28 PREPARE_NODE_NAME="cri-prepare-node"
29 PREPARE_NODE_IMAGE="cri-prepare-image"
30
31 source "${REPO}/script/util/utils.sh"
32
33 if [ "${KIND_NO_RECREATE:-}" != "true" ] ; then
34 echo "Preparing preparation node image..."
35 docker build ${DOCKER_BUILD_ARGS:-} -t "${PREPARE_NODE_IMAGE}" --target containerd-base "${REPO}"
36 fi
37
38 AUTH_DIR=$(mktemp -d)
39 DOCKERCONFIG=$(mktemp)
40 DOCKER_COMPOSE_YAML=$(mktemp)
41 KIND_KUBECONFIG=$(mktemp)
42 MIRROR_TMP=$(mktemp -d)
43 function cleanup {
44 local ORG_EXIT_CODE="${1}"
45 rm -rf "${AUTH_DIR}" || true
46 rm "${DOCKER_COMPOSE_YAML}" || true
47 rm "${DOCKERCONFIG}" || true
48 rm "${KIND_KUBECONFIG}" || true
49 rm -rf "${MIRROR_TMP}" || true
50 exit "${ORG_EXIT_CODE}"
51 }
52 trap 'cleanup "$?"' EXIT SIGHUP SIGINT SIGQUIT SIGTERM
53
54 echo "Preparing creds..."
55 prepare_creds "${AUTH_DIR}" "${REGISTRY_HOST}" "${DUMMYUSER}" "${DUMMYPASS}"
56 echo -n '{"auths":{"'"${REGISTRY_HOST}"':5000":{"auth":"'$(echo -n "${DUMMYUSER}:${DUMMYPASS}" | base64 -i -w 0)'"}}}' > "${DOCKERCONFIG}"
57
58 echo "Preparing private registry..."
59 cat <<EOF > "${DOCKER_COMPOSE_YAML}"
60 version: "3.5"
61 services:
62 testenv_registry:
63 image: registry:2
64 container_name: ${REGISTRY_HOST}
65 environment:
66 - HTTP_PROXY=${HTTP_PROXY:-}
67 - HTTPS_PROXY=${HTTPS_PROXY:-}
68 - http_proxy=${http_proxy:-}
69 - https_proxy=${https_proxy:-}
70 - REGISTRY_AUTH=htpasswd
71 - REGISTRY_AUTH_HTPASSWD_REALM="Registry Realm"
72 - REGISTRY_AUTH_HTPASSWD_PATH=/auth/auth/htpasswd
73 - REGISTRY_HTTP_TLS_CERTIFICATE=/auth/certs/domain.crt
74 - REGISTRY_HTTP_TLS_KEY=/auth/certs/domain.key
75 volumes:
76 - ${AUTH_DIR}:/auth
77 image-prepare:
78 image: "${PREPARE_NODE_IMAGE}"
79 container_name: "${PREPARE_NODE_NAME}"
80 privileged: true
81 entrypoint:
82 - sleep
83 - infinity
84 tmpfs:
85 - /tmp:exec,mode=777
86 environment:
87 - REGISTRY_CREDS=${DUMMYUSER}:${DUMMYPASS}
88 volumes:
89 - "kind-prepare-containerd-data:/var/lib/containerd"
90 - "kind-prepare-containerd-stargz-grpc-data:/var/lib/containerd-stargz-grpc"
91 - "${AUTH_DIR}/certs/domain.crt:/usr/local/share/ca-certificates/rgst.crt:ro"
92 - "${REPO}:/go/src/github.com/containerd/stargz-snapshotter:ro"
93 - "${MIRROR_TMP}:/tools/"
94 volumes:
95 kind-prepare-containerd-data:
96 kind-prepare-containerd-stargz-grpc-data:
97 networks:
98 default:
99 external:
100 name: ${REGISTRY_NETWORK}
101 EOF
102
103 cp "${REPO}/script/kind/mirror.sh" "${MIRROR_TMP}/mirror.sh"
104 if ! ( cd "${CONTEXT}" && \
105 docker network create "${REGISTRY_NETWORK}" && \
106 docker-compose -f "${DOCKER_COMPOSE_YAML}" up -d --force-recreate && \
107 docker exec "${PREPARE_NODE_NAME}" /bin/bash /tools/mirror.sh \
108 "${TESTIMAGE_ORIGIN}" "${TESTIMAGE}" ) ; then
109 echo "Failed to prepare private registry"
110 docker-compose -f "${DOCKER_COMPOSE_YAML}" down -v
111 docker network rm "${REGISTRY_NETWORK}"
112 exit 1
113 fi
114
115 echo "Testing in kind cluster (kubeconfig: ${KIND_KUBECONFIG})..."
116 FAIL=
117 if ! ( "${CONTEXT}"/run-kind.sh "${KIND_CLUSTER_NAME}" \
118 "${KIND_KUBECONFIG}" \
119 "${AUTH_DIR}/certs/domain.crt" \
120 "${REPO}" \
121 "${REGISTRY_NETWORK}" \
122 "${DOCKERCONFIG}" && \
123 echo "Waiting until secrets fullly synced..." && \
124 sleep 30 && \
125 echo "Trying to pull private image with secret..." && \
126 "${CONTEXT}"/create-pod.sh "$(kind get nodes --name "${KIND_CLUSTER_NAME}" | sed -n 1p)" \
127 "${KIND_KUBECONFIG}" "${TESTIMAGE}" ) ; then
128 FAIL=true
129 fi
130 docker-compose -f "${DOCKER_COMPOSE_YAML}" down -v
131 kind delete cluster --name "${KIND_CLUSTER_NAME}"
132 docker network rm "${REGISTRY_NETWORK}"
133
134 if [ "${FAIL}" == "true" ] ; then
135 exit 1
136 fi
137
138 exit 0