#!/usr/bin/env bash # Tests for cascade.sh against local bare repositories. A pre-receive hook # stands in for Gitea branch protection; tests/bin/curl stands in for the API. set -euo pipefail SCRIPT="$(cd "$(dirname "$0")/.." && pwd)/cascade.sh" TEST_BIN="$(cd "$(dirname "$0")" && pwd)/bin" IMAGE=code.test/fritzlab/base-dev PASS=0 FAIL=0 ROOT=$(mktemp -d) trap 'rm -rf "${ROOT}"' EXIT ok() { echo "PASS [$1]"; PASS=$((PASS + 1)); } bad() { echo "FAIL [$1]: $2"; echo " output: ${OUT}"; FAIL=$((FAIL + 1)); } # fresh_remote : a bare fritzlab/target on main. fresh_remote() { rm -rf "${ROOT}/remotes" "${ROOT}/seed" mkdir -p "${ROOT}/remotes/fritzlab" git init -q --bare -b main "${ROOT}/remotes/fritzlab/target.git" git init -q -b main "${ROOT}/seed" printf 'FROM %s:%s\nRUN true\n' "${IMAGE}" "$2" >"${ROOT}/seed/Dockerfile" git -C "${ROOT}/seed" add Dockerfile git -C "${ROOT}/seed" -c user.name=t -c user.email=t@test commit -q -m seed git -C "${ROOT}/seed" push -q "${ROOT}/remotes/fritzlab/target.git" main if [ "$1" = 1 ]; then cat >"${ROOT}/remotes/fritzlab/target.git/hooks/pre-receive" <<'HOOK' #!/usr/bin/env bash while read -r _ _ ref; do if [ "$ref" = refs/heads/main ]; then echo "error: Not allowed to push to protected branch main" >&2 exit 1 fi done HOOK chmod +x "${ROOT}/remotes/fritzlab/target.git/hooks/pre-receive" fi : >"${ROOT}/curl.log" } # cascade [CURL_STATUS]: run the action script; sets OUT and RC. cascade() { RC=0 OUT=$(cd "${ROOT}" && env PATH="${TEST_BIN}:$PATH" CURL_LOG="${ROOT}/curl.log" \ CURL_STATUS="${2:-201}" TARGET_REPO=fritzlab/target IMAGE="${IMAGE}" TAG_INPUT="$1" \ FILE=Dockerfile TOKEN=test-token HOST=code.test MESSAGE= RUN_NUMBER=99 \ GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0="url.file://${ROOT}/remotes/.insteadOf" \ GIT_CONFIG_VALUE_0="https://ci-bot:test-token@code.test/" \ bash "${SCRIPT}" 2>&1) || RC=$? } remote_from() { git --git-dir="${ROOT}/remotes/fritzlab/target.git" show "$1:Dockerfile" 2>/dev/null | head -1 } fresh_remote 0 20 cascade 22 if [ "$RC" -eq 0 ] && [ "$(remote_from main)" = "FROM ${IMAGE}:22" ] && [ ! -s "${ROOT}/curl.log" ]; then ok "open main takes the bump directly" else bad "open main takes the bump directly" "rc=${RC} main=$(remote_from main)" fi fresh_remote 1 20 cascade 22 if [ "$RC" -eq 0 ] && [ "$(remote_from main)" = "FROM ${IMAGE}:20" ] \ && [ "$(remote_from cascade/base-dev-22)" = "FROM ${IMAGE}:22" ] \ && grep -q '^POST https://code.test/api/v1/repos/fritzlab/target/pulls ' "${ROOT}/curl.log" \ && grep -q '"head":"cascade/base-dev-22"' <(tr -d ' \n' <"${ROOT}/curl.log") \ && grep -q '"title":"bumpbase-devto#22"' <(tr -d ' \n' <"${ROOT}/curl.log") \ && grep -q 'proposed base-dev:22' <<<"${OUT}"; then ok "protected main gets a pull request" else bad "protected main gets a pull request" "rc=${RC} main=$(remote_from main) log=$(cat "${ROOT}/curl.log")" fi cascade 22 409 if [ "$RC" -eq 0 ] && grep -q 'cascade/base-dev-22 already exists' <<<"${OUT}" \ && grep -q 'already open' <<<"${OUT}"; then ok "a re-run reuses the open pull request" else bad "a re-run reuses the open pull request" "rc=${RC}" fi fresh_remote 1 20 cascade 22 403 if [ "$RC" -eq 1 ] && grep -q 'answered HTTP 403' <<<"${OUT}"; then ok "a refused pull request fails the step" else bad "a refused pull request fails the step" "rc=${RC}" fi fresh_remote 1 22 cascade 22 if [ "$RC" -eq 0 ] && grep -q 'already on base-dev:22, skipping' <<<"${OUT}" && [ ! -s "${ROOT}/curl.log" ]; then ok "a target already on the tag is left alone" else bad "a target already on the tag is left alone" "rc=${RC}" fi echo "${PASS} passed, ${FAIL} failed" [ "${FAIL}" -eq 0 ]