76 lines
3.4 KiB
Bash
76 lines
3.4 KiB
Bash
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
SCRIPT=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)/deploy.sh
|
|
TEST_TMP=$(cd -- "${TMPDIR:-/tmp}" && pwd -P)
|
|
TEST_ROOT=$(mktemp -d "$TEST_TMP/sw-joint-deploy-test.XXXXXX")
|
|
cleanup() {
|
|
[[ "$TEST_ROOT" == "$TEST_TMP/sw-joint-deploy-test."* ]] || exit 1
|
|
rm -rf -- "$TEST_ROOT"
|
|
}
|
|
trap cleanup EXIT
|
|
mkdir "$TEST_ROOT/bin"
|
|
cat > "$TEST_ROOT/bin/docker" <<'DOCKER'
|
|
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
printf '%s\n' "$*" >> "$TEST_STATE/calls"
|
|
if [[ "$TEST_MODE" == isolated-config ]]; then
|
|
[[ "${DOCKER_CONFIG:-}" == "$SW_DEPLOY_BASE/harbor-docker" || "$1" == build ]] || exit 1
|
|
else
|
|
[[ "${DOCKER_CONFIG:-}" == "$TEST_ROOT/default-config" ]] || exit 1
|
|
fi
|
|
component=frontend
|
|
image="${!#}"
|
|
if [[ "$image" == *sw2-backend:* ]]; then component=backend; fi
|
|
case "$1" in
|
|
build) exit 0 ;;
|
|
push|pull)
|
|
[[ "$TEST_MODE" != "$component-$1-fail" ]] || exit 1
|
|
echo 'registry progress output' ;;
|
|
inspect)
|
|
[[ "$TEST_MODE" != "$component-inspect-fail" ]] || exit 1
|
|
case "$TEST_MODE" in
|
|
"$component-digest-missing") exit 0 ;;
|
|
"$component-digest-wrong-repo") printf 'example.com/wrong@sha256:%064d\n' 1 ;;
|
|
"$component-digest-malformed") printf '%s@sha256:invalid\n' "${image%:*}" ;;
|
|
*)
|
|
# An unrelated digest may precede the correct registry digest.
|
|
printf 'example.com/old@sha256:%064d\n' 1
|
|
printf '%s@sha256:%064d\n' "${image%:*}" 1 ;;
|
|
esac ;;
|
|
run) [[ "$*" == *'@sha256:'* && "$*" == *'--rm'* ]] ;;
|
|
*) echo "Unexpected Docker command: $*" >&2; exit 1 ;;
|
|
esac
|
|
DOCKER
|
|
printf '#!/usr/bin/env bash\nexit 0\n' > "$TEST_ROOT/bin/curl"
|
|
chmod +x "$TEST_ROOT/bin/"*
|
|
export TEST_ROOT PATH="$TEST_ROOT/bin:$PATH"
|
|
|
|
for scenario in success isolated-config \
|
|
backend-push-fail backend-pull-fail backend-inspect-fail backend-digest-missing backend-digest-wrong-repo backend-digest-malformed \
|
|
frontend-push-fail frontend-pull-fail frontend-inspect-fail frontend-digest-missing frontend-digest-wrong-repo frontend-digest-malformed; do
|
|
export TEST_MODE="$scenario" TEST_STATE="$TEST_ROOT/$scenario"
|
|
export SW_DEPLOY_BASE="$TEST_STATE/base" DOCKER_CONFIG="$TEST_ROOT/default-config"
|
|
mkdir -p "$SW_DEPLOY_BASE/releases/new/backend" "$SW_DEPLOY_BASE/releases/new/frontend"
|
|
touch "$SW_DEPLOY_BASE/releases/new/backend/Dockerfile" "$SW_DEPLOY_BASE/releases/new/frontend/Dockerfile"
|
|
if [[ "$scenario" == isolated-config ]]; then
|
|
mkdir "$SW_DEPLOY_BASE/harbor-docker"
|
|
printf '{}\n' > "$SW_DEPLOY_BASE/harbor-docker/config.json"
|
|
fi
|
|
result=0
|
|
bash "$SCRIPT" new --build-only > "$TEST_STATE/output" 2>&1 || result=$?
|
|
case "$scenario" in
|
|
success|isolated-config)
|
|
[[ "$result" == 0 ]] || { cat "$TEST_STATE/output" >&2; exit 1; }
|
|
for repository in harbor.fuhua91.com/game/sw2-backend harbor.fuhua91.com/game/sw2; do
|
|
grep -qFx "push $repository:new" "$TEST_STATE/calls"
|
|
grep -qFx "pull $repository:new" "$TEST_STATE/calls"
|
|
grep -qE "^run .* $repository@sha256:[a-f0-9]{64} " "$TEST_STATE/calls"
|
|
done ;;
|
|
*)
|
|
[[ "$result" != 0 ]]
|
|
! grep -qE '^(run|stop|rename|rm) ' "$TEST_STATE/calls" ;;
|
|
esac
|
|
! grep -qE 'sw2/(backend|frontend):' "$TEST_STATE/calls"
|
|
printf 'passed: %s\n' "$scenario"
|
|
done
|