| 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=registry-optimize.test |
| 22 |
REPO_PATH=/go/src/github.com/containerd/stargz-snapshotter |
| 23 |
DUMMYUSER=dummyuser |
| 24 |
DUMMYPASS=dummypass |
| 25 |
OPTIMIZE_BASE_IMAGE_NAME="optimize-image-base" |
| 26 |
OPTIMIZE_TEST_IMAGE_NAME="optimize-image-test" |
| 27 |
|
| 28 |
source "${REPO}/script/util/utils.sh" |
| 29 |
|
| 30 |
CNI_PLUGINS_VERSION=$(get_version_from_arg "${REPO}/Dockerfile" "CNI_PLUGINS_VERSION") |
| 31 |
NERDCTL_VERSION=$(get_version_from_arg "${REPO}/Dockerfile" "NERDCTL_VERSION") |
| 32 |
|
| 33 |
if [ "${OPTIMIZE_NO_RECREATE:-}" != "true" ] ; then |
| 34 |
echo "Preparing node image..." |
| 35 |
|
| 36 |
# Enable to check race |
| 37 |
docker build ${DOCKER_BUILD_ARGS:-} -t "${OPTIMIZE_BASE_IMAGE_NAME}" \ |
| 38 |
--target snapshotter-base \ |
| 39 |
--build-arg=SNAPSHOTTER_BUILD_FLAGS="-race" \ |
| 40 |
"${REPO}" |
| 41 |
fi |
| 42 |
|
| 43 |
DOCKER_COMPOSE_YAML=$(mktemp) |
| 44 |
AUTH_DIR=$(mktemp -d) |
| 45 |
TMP_CONTEXT=$(mktemp -d) |
| 46 |
function cleanup { |
| 47 |
local ORG_EXIT_CODE="${1}" |
| 48 |
rm "${DOCKER_COMPOSE_YAML}" || true |
| 49 |
rm -rf "${AUTH_DIR}" || true |
| 50 |
rm -rf "${TMP_CONTEXT}" || true |
| 51 |
exit "${ORG_EXIT_CODE}" |
| 52 |
} |
| 53 |
trap 'cleanup "$?"' EXIT SIGHUP SIGINT SIGQUIT SIGTERM |
| 54 |
|
| 55 |
cat <<'EOF' > "${TMP_CONTEXT}/test.conflist" |
| 56 |
{ |
| 57 |
"cniVersion": "0.4.0", |
| 58 |
"name": "test", |
| 59 |
"plugins" : [{ |
| 60 |
"type": "bridge", |
| 61 |
"bridge": "test0", |
| 62 |
"isDefaultGateway": true, |
| 63 |
"forceAddress": false, |
| 64 |
"ipMasq": true, |
| 65 |
"hairpinMode": true, |
| 66 |
"ipam": { |
| 67 |
"type": "host-local", |
| 68 |
"subnet": "10.10.0.0/16" |
| 69 |
} |
| 70 |
}, |
| 71 |
{ |
| 72 |
"type": "loopback" |
| 73 |
}] |
| 74 |
} |
| 75 |
EOF |
| 76 |
|
| 77 |
cat <<EOF > "${TMP_CONTEXT}/Dockerfile" |
| 78 |
# Legacy builder that doesn't support TARGETARCH should set this explicitly using --build-arg. |
| 79 |
# If TARGETARCH isn't supported by the builder, the default value is "amd64". |
| 80 |
|
| 81 |
FROM ${OPTIMIZE_BASE_IMAGE_NAME} |
| 82 |
ARG TARGETARCH |
| 83 |
|
| 84 |
RUN apt-get update -y && \ |
| 85 |
apt-get --no-install-recommends install -y jq iptables zstd && \ |
| 86 |
go install github.com/google/go-containerregistry/cmd/[email protected] && \ |
| 87 |
mkdir -p /opt/tmp/cni/bin /etc/tmp/cni/net.d && \ |
| 88 |
curl -Ls https://github.com/containernetworking/plugins/releases/download/v${CNI_PLUGINS_VERSION}/cni-plugins-linux-\${TARGETARCH:-amd64}-v${CNI_PLUGINS_VERSION}.tgz | tar xzv -C /opt/tmp/cni/bin && \ |
| 89 |
curl -sSL https://github.com/containerd/nerdctl/releases/download/v${NERDCTL_VERSION}/nerdctl-full-${NERDCTL_VERSION}-linux-\${TARGETARCH:-amd64}.tar.gz | tar -C /usr/local -zx bin/buildkitd bin/buildctl |
| 90 |
|
| 91 |
# Installs CNI-related files to irregular paths (/opt/tmp/cni/bin and /etc/tmp/cni/net.d) for test. |
| 92 |
# see entrypoint.sh for more details. |
| 93 |
|
| 94 |
COPY ./test.conflist /etc/tmp/cni/net.d/test.conflist |
| 95 |
|
| 96 |
EOF |
| 97 |
docker build -t "${OPTIMIZE_TEST_IMAGE_NAME}" ${DOCKER_BUILD_ARGS:-} "${TMP_CONTEXT}" |
| 98 |
|
| 99 |
echo "Preparing creds..." |
| 100 |
prepare_creds "${AUTH_DIR}" "${REGISTRY_HOST}" "${DUMMYUSER}" "${DUMMYPASS}" |
| 101 |
|
| 102 |
echo "Testing..." |
| 103 |
function test_optimize { |
| 104 |
local OPTIMIZE_COMMAND="${1}" |
| 105 |
local NO_OPTIMIZE_COMMAND="${2}" |
| 106 |
local GETTOCDIGEST_COMMAND="${3}" |
| 107 |
local DECOMPRESS_COMMAND="${4}" |
| 108 |
local INVISIBLE_TOC="${5}" |
| 109 |
cat <<EOF > "${DOCKER_COMPOSE_YAML}" |
| 110 |
version: "3.3" |
| 111 |
services: |
| 112 |
testenv_opt: |
| 113 |
image: ${OPTIMIZE_TEST_IMAGE_NAME} |
| 114 |
container_name: testenv_opt |
| 115 |
privileged: true |
| 116 |
working_dir: ${REPO_PATH} |
| 117 |
entrypoint: ./script/optimize/optimize/entrypoint.sh |
| 118 |
environment: |
| 119 |
- NO_PROXY=127.0.0.1,localhost,${REGISTRY_HOST}:443 |
| 120 |
- OPTIMIZE_COMMAND=${OPTIMIZE_COMMAND} |
| 121 |
- NO_OPTIMIZE_COMMAND=${NO_OPTIMIZE_COMMAND} |
| 122 |
- GETTOCDIGEST_COMMAND=${GETTOCDIGEST_COMMAND} |
| 123 |
- DECOMPRESS_COMMAND=${DECOMPRESS_COMMAND} |
| 124 |
- INVISIBLE_TOC=${INVISIBLE_TOC} |
| 125 |
tmpfs: |
| 126 |
- /tmp:exec,mode=777 |
| 127 |
volumes: |
| 128 |
- "${REPO}:${REPO_PATH}:ro" |
| 129 |
- ${AUTH_DIR}:/auth:ro |
| 130 |
- "optimize-containerd-data:/var/lib/containerd" |
| 131 |
- "optimize-containerd-stargz-grpc-data:/var/lib/containerd-stargz-grpc" |
| 132 |
- "optimize-buildkit-data:/var/lib/buildkit" |
| 133 |
registry: |
| 134 |
image: registry:2 |
| 135 |
container_name: ${REGISTRY_HOST} |
| 136 |
environment: |
| 137 |
- REGISTRY_AUTH=htpasswd |
| 138 |
- REGISTRY_AUTH_HTPASSWD_REALM="Registry Realm" |
| 139 |
- REGISTRY_AUTH_HTPASSWD_PATH=/auth/auth/htpasswd |
| 140 |
- REGISTRY_HTTP_TLS_CERTIFICATE=/auth/certs/domain.crt |
| 141 |
- REGISTRY_HTTP_TLS_KEY=/auth/certs/domain.key |
| 142 |
- REGISTRY_HTTP_ADDR=${REGISTRY_HOST}:443 |
| 143 |
volumes: |
| 144 |
- ${AUTH_DIR}:/auth:ro |
| 145 |
volumes: |
| 146 |
optimize-containerd-data: |
| 147 |
optimize-containerd-stargz-grpc-data: |
| 148 |
optimize-buildkit-data: |
| 149 |
|
| 150 |
EOF |
| 151 |
local FAIL= |
| 152 |
if ! ( cd "${CONTEXT}" && \ |
| 153 |
docker-compose -f "${DOCKER_COMPOSE_YAML}" build ${DOCKER_BUILD_ARGS:-} testenv_opt && \ |
| 154 |
docker-compose -f "${DOCKER_COMPOSE_YAML}" up --abort-on-container-exit ) ; then |
| 155 |
FAIL=true |
| 156 |
fi |
| 157 |
docker-compose -f "${DOCKER_COMPOSE_YAML}" down -v |
| 158 |
if [ "${FAIL}" == "true" ] ; then |
| 159 |
exit 1 |
| 160 |
fi |
| 161 |
} |
| 162 |
|
| 163 |
test_optimize "image optimize --oci --zstdchunked" \ |
| 164 |
"image optimize --no-optimize --oci --zstdchunked" \ |
| 165 |
"image get-toc-digest --zstdchunked" \ |
| 166 |
"zstd -d" \ |
| 167 |
"true" |
| 168 |
|
| 169 |
test_optimize "image optimize --oci" \ |
| 170 |
"image optimize --no-optimize --oci" \ |
| 171 |
"image get-toc-digest" \ |
| 172 |
"gunzip" \ |
| 173 |
"false" |
| 174 |
|
| 175 |
exit 0 |