Files
ianitor/scripts/smoke-docker.sh
T

94 lines
5.6 KiB
Bash

#!/usr/bin/env bash
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# Docker end to end (T4): deploy/docker/compose.yaml with the image, a throw-away Domi root,
# a branch build in another container, Fabrica's client (scripts/preview.sh) with a
# previews-only token, the preview through edge (Pingora) from the host, close. Ports 28080 and
# 28443 on 127.0.0.1; everything is removed at the end (containers, volumes, the network).
#
# scripts/smoke-docker.sh <image> <ianitor-control binary for the offline root> <work dir>
set -euo pipefail
# Git Bash on Windows rewrites /paths in arguments and the environment of Windows programs
# (docker.exe); the paths here are the container's.
export MSYS_NO_PATHCONV=1 MSYS2_ENV_CONV_EXCL='*'
IMAGE=$1 CTL=$2 WORK=$3
REPO=$(cd "$(dirname "$0")/.." && pwd)
P=ianitor-smoke
export COMPOSE_PROJECT_NAME=$P IANITOR_IMAGE=$IMAGE IANITOR_HTTP_PORT=28080 IANITOR_HTTPS_PORT=28443
export IANITOR_ADMIN_HOST=ianitor.smoke.test IANITOR_TLS=domi IANITOR_DOMI_ROOT=/var/lib/ianitor/domi-root.crt
export IANITOR_PREVIEW_DOMAIN=pv.smoke.test IANITOR_PREVIEW_UPSTREAMS=app:8080
DC=(docker compose -f "$REPO/deploy/docker/compose.yaml")
step() { echo "== $*"; }
ok() { echo " ok: $*"; }
fail() { echo "FAILED: $*" >&2; exit 1; }
cleanup() {
"${DC[@]}" down -v --remove-orphans >/dev/null 2>&1 || true
docker rm -f $P-app >/dev/null 2>&1 || true
docker volume rm ${P}_state >/dev/null 2>&1 || true
}
trap cleanup EXIT
[ ! -e "$WORK" ] || fail "$WORK exists; give a new folder"
mkdir -p "$WORK"
# In edge's network namespace: the loopback API (curl and jq from a throw-away Alpine).
inside() { docker run --rm -i --network container:$P-edge-1 alpine:3.20 sh -c "apk add -q curl jq >/dev/null && $1"; }
step "a throw-away Domi root (offline step)"
printf 'smoke-test-offline-passphrase' > "$WORK/pass"
"$CTL" ca init --out "$WORK/root" --passphrase-file "$WORK/pass" >/dev/null 2>&1 || fail "ca init"
docker volume create ${P}_state >/dev/null
docker run --rm -i -v ${P}_state:/v alpine:3.20 sh -c 'cat > /v/domi-root.crt && chown 10001:10001 /v /v/domi-root.crt' < "$WORK/root/domi-root.crt"
ok "root in the state volume"
step "docker compose up"
"${DC[@]}" up -d >/dev/null 2>&1 || fail "compose up"
for i in $(seq 60); do inside "curl -s -o /dev/null http://127.0.0.1:7878/" 2>/dev/null && break; sleep 2; done
ADMIN=$("${DC[@]}" exec -T control ianitor-control token create --name smoke --config /var/lib/ianitor/control.toml 2>/dev/null | tail -n1)
FABRICA=$("${DC[@]}" exec -T control ianitor-control token create --name fabrica --scope previews --config /var/lib/ianitor/control.toml 2>/dev/null | tail -n1)
[ -n "$ADMIN" ] && [ -n "$FABRICA" ] || fail "tokens"
ok "control up, tokens made"
step "the intermediate, signed offline"
rid=$(inside "curl -s -X POST -H 'Authorization: Bearer $ADMIN' http://127.0.0.1:7878/api/v1/certs/intermediate/renewal | jq -r .request_id")
inside "curl -s -H 'Authorization: Bearer $ADMIN' http://127.0.0.1:7878/api/v1/pki-requests/$rid/csr" > "$WORK/int.csr"
"$CTL" ca sign --root "$WORK/root" --csr "$WORK/int.csr" --out "$WORK/int.crt" --yes --passphrase-file "$WORK/pass" >/dev/null 2>&1 || fail "ca sign"
"${DC[@]}" exec -T control sh -c 'cat > /tmp/int.crt && ianitor-control pki install /tmp/int.crt --config /var/lib/ianitor/control.toml' < "$WORK/int.crt" >/dev/null 2>&1 || fail "pki install"
"${DC[@]}" restart control >/dev/null 2>&1
for i in $(seq 60); do
inside "curl -s -H 'Authorization: Bearer $ADMIN' http://127.0.0.1:7878/api/v1/hosts" 2>/dev/null | grep -q ianitor.smoke.test && break
sleep 2
done
ok "intermediate installed; admin host added"
step "a branch build in its own container"
docker run -d --name $P-app --network ${P}_default --network-alias app python:3.12-alpine \
sh -c 'mkdir /s && echo "hello from fix-42" > /s/index.html && python -m http.server 8080 --directory /s' >/dev/null
sleep 3
step "Fabrica opens the preview (scripts/preview.sh, previews-only token)"
url=$(inside "IANITOR_URL=http://127.0.0.1:7878 IANITOR_PREVIEW_TOKEN=$FABRICA sh -s -- open shop fix-42 http://app:8080 3600" < "$REPO/scripts/preview.sh")
[ "$url" = "https://fix-42--shop.pv.smoke.test/" ] || fail "preview URL: $url"
ok "preview URL $url"
body=$(curl -sS --cacert "$WORK/root/domi-root.crt" --resolve fix-42--shop.pv.smoke.test:28443:127.0.0.1 https://fix-42--shop.pv.smoke.test:28443/ || true)
[ "$body" = "hello from fix-42" ] || fail "the preview through edge: $body"
ok "the preview answers through edge (Pingora) from the host, with a Domi certificate"
code=$(curl -s -o /dev/null -w '%{http_code}' --cacert "$WORK/root/domi-root.crt" --resolve ianitor.smoke.test:28443:127.0.0.1 https://ianitor.smoke.test:28443/api/v1/team || true)
[ "$code" = 401 ] || fail "admin host: $code"
ok "the admin host reaches control and needs a sign-in (401)"
code=$(inside "curl -s -o /dev/null -w '%{http_code}' -H 'Authorization: Bearer $FABRICA' http://127.0.0.1:7878/api/v1/hosts")
[ "$code" = 403 ] || fail "preview token on hosts: $code"
ok "the preview token cannot read the hosts (403)"
step "closed on merge"
inside "IANITOR_URL=http://127.0.0.1:7878 IANITOR_PREVIEW_TOKEN=$FABRICA sh -s -- close shop fix-42" < "$REPO/scripts/preview.sh" >/dev/null
for i in $(seq 45); do
code=$(curl -s -o /dev/null -w '%{http_code}' --cacert "$WORK/root/domi-root.crt" --resolve fix-42--shop.pv.smoke.test:28443:127.0.0.1 https://fix-42--shop.pv.smoke.test:28443/ || true)
[ "$code" != 200 ] && break
sleep 2
done
[ "$code" != 200 ] || fail "still served after close"
ok "gone after close ($code)"
echo "SMOKE PASSED"