#!/bin/bash set -euo pipefail root=$(cd "$(dirname "$0")/../.." && pwd) work=$(mktemp -d "${TMPDIR:-/tmp}/mirror-tests.XXXXXX") trap 'rm -rf "$work"' EXIT mkdir -p "$work/project/Scripts" "$work/bin" "$work/tmp" cp "$root/Scripts/mirror.sh" "$work/project/Scripts/" cat > "$work/project/Scripts/product.env" <<'PRODUCT' PRODUCT_GITHUB_REPO=fixture/destination PRODUCT_KNOT_HOST=knot.example.invalid PRODUCT_TANGLED_REPO=fixture.example/source PRODUCT_DEFAULT_BRANCH=main PRODUCT_RELEASE_WORKFLOW=release.yml PRODUCT cat > "$work/log.sh" <<'LOG' log_args() { local value separator='' printf '[' for value in "$@"; do value=${value//\\/\\\\} value=${value//\"/\\\"} printf '%s"%s"' "$separator" "$value" separator=', ' done printf ']\n' } LOG cat > "$work/bin/git" <<'GIT' #!/bin/bash set -euo pipefail source "$TEST_ROOT/log.sh" log_args "$@" >> "$TEST_ROOT/git.log" args=("$@") while [[ ${args[0]} == --git-dir=* || ${args[0]} == -c ]]; do if [[ ${args[0]} == --git-dir=* ]]; then args=("${args[@]:1}"); else args=("${args[@]:2}"); fi done command=${args[0]} args=("${args[@]:1}") case=${TEST_CASE:-} a=$(printf '1%.0s' {1..40}); b=$(printf '2%.0s' {1..40}) c=$(printf '3%.0s' {1..40}); d=$(printf '4%.0s' {1..40}) source_url=https://knot.example.invalid/fixture.example/source destination=https://github.com/fixture/destination.git for name in GIT_TRACE GIT_TRACE_CURL GIT_CURL_VERBOSE GIT_SSL_NO_VERIFY GIT_CONFIG_COUNT GIT_SSH HTTPS_PROXY; do [[ -z ${!name:-} ]] done [[ $GIT_CONFIG_GLOBAL == /dev/null && $GIT_CONFIG_SYSTEM == /dev/null && $GIT_TERMINAL_PROMPT == 0 ]] [[ -z ${RELEASE_DISPATCH_TOKEN:-} && -z ${dispatch_token:-} ]] expect() { local value index=0 [[ ${#args[@]} == $# ]] for value in "$@"; do [[ ${args[$index]} == "$value" ]] index=$((index + 1)) done } authentication() { local token=$MIRROR_TOKEN helper=$GIT_ASKPASS if grep -Fq -- "$token" "$helper"; then exit 1; fi [[ $("$helper" "Username for 'https://github.com':") == x-access-token ]] [[ $("$helper" "Password for 'https://x-access-token@github.com':") == "$token" ]] [[ $("$helper" "Username for 'https://github.com': ") == x-access-token ]] [[ $("$helper" "Password for 'https://x-access-token@github.com': ") == "$token" ]] if "$helper" "Password for 'https://elsewhere.invalid':" > /dev/null 2>&1; then exit 1; fi [[ -z ${GH_TOKEN:-} ]] } case "$command" in init) [[ ${args[0]} == --bare && ${args[1]} == --template= && ${#args[@]} == 3 ]] mkdir "${args[2]}" ;; check-ref-format) [[ ${args[0]} == refs/* ]] ;; fetch) if [[ ${args[1]} == "$source_url" ]]; then expect --no-tags "$source_url" refs/heads/main:refs/source/main 'refs/tags/v*:refs/source/tags/v*' [[ -z ${MIRROR_TOKEN:-} && -z ${GIT_ASKPASS:-} ]] if [[ $case == fetch-failure ]]; then exit 70; fi else authentication expect --no-tags "$destination" refs/heads/main:refs/destination/main fi ;; ls-remote) authentication expect --refs "$destination" refs/heads/main 'refs/tags/v*' if [[ ! -e $TEST_ROOT/pushed || $case == readback-failure ]]; then sha=$a; else sha=$b; fi printf '%s\trefs/heads/main\n' "$sha" if [[ $case == conflict || $case == off-main-conflict ]]; then printf '%s\trefs/tags/v1.2.3\n' "$a" elif [[ $case == existing-tag ]] || [[ -e $TEST_ROOT/pushed && $case != off-main && $case != non-v && $case != noncommit && $case != deleted-source-tag && $case != no-tags ]]; then printf '%s\trefs/tags/v1.2.3\n' "$c" fi if [[ $case == bulk-tags && -e $TEST_ROOT/pushed ]]; then printf '%s\trefs/tags/v1.2.4\n%s\trefs/tags/v1.2.5\n' "$c" "$c" fi # Unrelated refs are malformed so validation must ignore them first. if [[ $case == junk ]]; then printf 'junk\trefs/heads/broken\n'; fi printf '%s\trefs/heads/github-only\n%s\trefs/tags/vGithubOnly\n%s\trefs/tags/not-a-release\n' "$d" "$d" "$d" ;; rev-parse) ref=${args[${#args[@]} - 1]} if [[ $ref == 'refs/source/main^{commit}' ]]; then printf '%s\n' "$b" elif [[ $ref == 'refs/destination/main^{commit}' ]]; then printf '%s\n' "$a" elif [[ $ref == *'^{commit}' ]]; then if [[ $case == noncommit ]]; then exit 1; fi if [[ $case == off-main || $case == off-main-conflict ]]; then printf '%s\n' "$d"; else printf '%s\n' "$b"; fi else printf '%s\n' "$c" fi ;; merge-base) [[ ${args[0]} == --is-ancestor ]] if [[ ${args[1]} == "$a" && ${args[2]} == "$b" ]]; then if [[ $case == divergence ]]; then exit 1; fi else [[ ${args[2]} == "$b" ]] if [[ ${args[1]} == "$d" ]]; then exit 1; fi fi ;; for-each-ref) expect '--format=%(objectname)%09%(refname)' refs/source/tags/ case "$case" in off-main|off-main-conflict) printf '%s\trefs/source/tags/v1.2.3\n' "$d" ;; noncommit) printf '%s\trefs/source/tags/vBlob\n' "$d" ;; non-v|deleted-source-tag|no-tags) ;; bulk-tags) for tag in v1.2.3 v1.2.4 v1.2.5 v1.2.6; do printf '%s\trefs/source/tags/%s\n' "$c" "$tag"; done ;; *) printf '%s\trefs/source/tags/v1.2.3\n' "$c" ;; esac ;; push) authentication [[ ${args[0]} == --atomic && ${args[1]} == "$destination" && ${args[2]} == "$b:refs/heads/main" ]] for refspec in "${args[@]:2}"; do [[ $refspec != :* && $refspec != +* && $refspec != -* && $refspec != *'*'* ]] if [[ $case == bulk-tags ]]; then [[ ${refspec#*:} == refs/heads/main || ${refspec#*:} == refs/tags/v1.2.[345] ]] else [[ ${refspec#*:} == refs/heads/main || ${refspec#*:} == refs/tags/v1.2.3 ]] fi done if [[ $case == bulk-tags ]]; then [[ ${#args[@]} == 6 ]]; fi if [[ $case == push-race ]]; then exit 71; fi touch "$TEST_ROOT/pushed" ;; *) printf 'Unexpected Git command: %s\n' "$command" >&2; exit 1 ;; esac GIT cat > "$work/bin/gh" <<'GH' #!/bin/bash set -euo pipefail source "$TEST_ROOT/log.sh" log_args "$@" >> "$TEST_ROOT/gh.log" echo 'Mirror must not dispatch a second release for the tag push.' >&2 exit 1 GH chmod +x "$work/bin/"* export PATH="$work/bin:$PATH" TMPDIR="$work/tmp" TEST_ROOT="$work" export GITHUB_ACTIONS=true GITHUB_REPOSITORY=fixture/destination GITHUB_REF=refs/heads/main GITHUB_EVENT_NAME=schedule export MIRROR_TOKEN=fixture-secret-never-log RELEASE_DISPATCH_TOKEN=fixture-dispatch-never-log count=0 check() { "$@" || { printf 'Mirror check failed at line %s: %s\n' "${BASH_LINENO[0]}" "$*" >&2; exit 1; }; count=$((count + 1)); } run() { rm -f "$work/pushed" : > "$work/git.log"; : > "$work/gh.log" "$@" > "$work/output" 2>&1 } fails() { if run "$@"; then return 1; fi; } no_push() { ! grep -q '"push"' "$work/git.log"; } no_dispatch() { [[ ! -s $work/gh.log ]]; } private() { ! grep -Fq -e "$MIRROR_TOKEN" -e "$RELEASE_DISPATCH_TOKEN" "$work/git.log" "$work/gh.log" "$work/output"; } clean() { [[ -z $(ls -A "$work/tmp") ]]; } script="$work/project/Scripts/mirror.sh" check run env -u MIRROR_TOKEN bash "$script" --dry-run check test ! -s "$work/git.log" check grep -q 'No network calls' "$work/output" for override in MIRROR_TOKEN= GITHUB_ACTIONS=false GITHUB_REF=refs/heads/topic GITHUB_EVENT_NAME=push GITHUB_REPOSITORY=fixture/fork; do check fails env "$override" bash "$script" check test ! -s "$work/git.log" done check fails env MIRROR_TOKEN=$'two\nlines' bash "$script" check run bash "$script" check grep -q '"push", "--atomic"' "$work/git.log" check no_dispatch check grep -q 'No refs were deleted' "$work/output" check private check clean for case in divergence conflict fetch-failure push-race readback-failure; do check fails env TEST_CASE="$case" bash "$script" check clean check private if [[ $case == divergence || $case == conflict || $case == fetch-failure ]]; then check no_push; fi check no_dispatch done check fails env TEST_CASE=divergence bash "$script" check grep -q 'GitHub main has diverged' "$work/output" check fails env TEST_CASE=conflict bash "$script" check grep -q 'Release tag conflict' "$work/output" for case in off-main off-main-conflict non-v noncommit deleted-source-tag existing-tag no-tags; do check run env TEST_CASE="$case" bash "$script" check no_dispatch check private check clean check grep -q '0 new release tags' "$work/output" done check run env TEST_CASE=junk GITHUB_EVENT_NAME=workflow_dispatch GIT_TRACE=1 GIT_TRACE_CURL=1 GIT_CURL_VERBOSE=1 \ GIT_SSL_NO_VERIFY=1 GIT_CONFIG_COUNT=100 GIT_SSH=unsafe HTTPS_PROXY=unsafe bash -x "$script" check no_dispatch check private check clean check run env TEST_CASE=existing-tag bash "$script" check no_dispatch no_destructive_push() { local line while IFS= read -r line; do if [[ $line == *'"push"'* && $line =~ \"(:|\+|--force|--prune|--delete|--mirror) ]]; then printf 'A destructive push was attempted\n' >&2 return 1 fi done < "$work/git.log" } check no_destructive_push check run env TEST_CASE=bulk-tags bash "$script" check grep -q '3 new release tags' "$work/output" check no_dispatch check private check clean check test ! -e "$root/.tangled/workflows/mirror.yml" printf 'Mirror checks passed: %s (fake Git and GitHub only).\n' "$count"