返回 CodeWhale
require-full-ci-receipt.test.sh
根目录 / scripts / release / require-full-ci-receipt.test.sh
1 #!/usr/bin/env bash
2 set -euo pipefail
3
4 repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
5 script="${repo_root}/scripts/release/require-full-ci-receipt.sh"
6 tmp_dir="$(mktemp -d)"
7 trap 'rm -rf "${tmp_dir}"' EXIT
8
9 sha="0123456789abcdef0123456789abcdef01234567"
10
11 fake_gh="${tmp_dir}/gh"
12 cat > "${fake_gh}" <<'EOF'
13 #!/usr/bin/env bash
14 set -euo pipefail
15 [[ "$1" == "api" ]] || { echo "unexpected: $*" >&2; exit 9; }
16 path="$2"
17 filter="$4"
18 case "${path}" in
19 */workflows/ci.yml/runs\?*) fixture="${FIXTURE_DIR}/runs.json" ;;
20 */actions/runs/*/jobs\?*)
21 run_id="${path#*/actions/runs/}"
22 run_id="${run_id%%/*}"
23 fixture="${FIXTURE_DIR}/jobs-${run_id}.json"
24 ;;
25 *) echo "unexpected path: ${path}" >&2; exit 9 ;;
26 esac
27 [[ -f "${fixture}" ]] || { echo "API error" >&2; exit 1; }
28 jq -r "${filter}" "${fixture}"
29 EOF
30 chmod +x "${fake_gh}"
31 export FULL_CI_RECEIPT_GH="${fake_gh}"
32 export FIXTURE_DIR="${tmp_dir}"
33
34 expect_pass() {
35 local label="$1"
36 if ! "${script}" owner/Repo "${sha}" >"${tmp_dir}/out" 2>&1; then
37 echo "FAIL (${label}): expected a receipt" >&2
38 cat "${tmp_dir}/out" >&2
39 exit 1
40 fi
41 }
42 expect_fail() {
43 local label="$1"
44 if "${script}" owner/Repo "${2:-${sha}}" >"${tmp_dir}/out" 2>&1; then
45 echo "FAIL (${label}): expected refusal" >&2
46 cat "${tmp_dir}/out" >&2
47 exit 1
48 fi
49 }
50
51 run() {
52 local id="$1" event="${2:-workflow_dispatch}" conclusion="${3:-success}" head="${4:-${sha}}" owner="${5:-owner/repo}"
53 printf '{"id":%s,"head_sha":"%s","event":"%s","status":"completed","conclusion":"%s","path":".github/workflows/ci.yml","head_repository":{"full_name":"%s"},"html_url":"https://example.invalid/%s"}' \
54 "${id}" "${head}" "${event}" "${conclusion}" "${owner}" "${id}"
55 }
56 runs() {
57 local IFS=,
58 echo "{\"workflow_runs\":[$*]}" > "${tmp_dir}/runs.json"
59 }
60 full_jobs='{"name":"Change detection","conclusion":"success"},
61 {"name":"Lint","conclusion":"success"},
62 {"name":"Shared-process tests","conclusion":"skipped"},
63 {"name":"Scoped test (${{ matrix.os }})","conclusion":"skipped"},
64 {"name":"Sharded tests (ubuntu-latest) / Suite check","conclusion":"success"},
65 {"name":"Sharded tests (macos-latest) / Suite check","conclusion":"success"},
66 {"name":"Sharded tests (windows-latest) / Suite check","conclusion":"success"}'
67 jobs() {
68 local id="$1" body="$2" total="${3:-}"
69 local count
70 count="$(echo "[${body}]" | jq length)"
71 echo "{\"total_count\":${total:-${count}},\"jobs\":[${body}]}" > "${tmp_dir}/jobs-${id}.json"
72 }
73
74 # 1. No run at all.
75 runs
76 expect_fail "no runs"
77 grep -q "No completed, green, full ci.yml run" "${tmp_dir}/out"
78
79 # 2. A green full dispatch for the exact SHA is a receipt.
80 runs "$(run 21)"
81 jobs 21 "${full_jobs}"
82 expect_pass "full dispatch"
83 grep -q "Full CI receipt for ${sha}: https://example.invalid/21" "${tmp_dir}/out"
84
85 # 3. A green scoped dispatch (crate/filter set) never counts.
86 runs "$(run 22)"
87 jobs 22 '{"name":"Change detection","conclusion":"success"},
88 {"name":"Lint","conclusion":"skipped"},
89 {"name":"Sharded tests (ubuntu-latest)","conclusion":"skipped"},
90 {"name":"Sharded tests (macos-latest)","conclusion":"skipped"},
91 {"name":"Sharded tests (windows-latest)","conclusion":"skipped"},
92 {"name":"Scoped test (ubuntu-latest)","conclusion":"success"}'
93 expect_fail "scoped run"
94 grep -q "(scoped run)" "${tmp_dir}/out"
95
96 # 4. A scoped job that ran alongside green suite checks still disqualifies.
97 jobs 22 "${full_jobs},{\"name\":\"Scoped test (macos-latest)\",\"conclusion\":\"success\"}"
98 expect_fail "scoped job present"
99
100 # 5. A shared-process qualification run is not the three-OS suite.
101 runs "$(run 23)"
102 jobs 23 '{"name":"Change detection","conclusion":"success"},
103 {"name":"Lint","conclusion":"success"},
104 {"name":"Shared-process tests","conclusion":"success"},
105 {"name":"Sharded tests (ubuntu-latest)","conclusion":"skipped"},
106 {"name":"Sharded tests (macos-latest)","conclusion":"skipped"},
107 {"name":"Sharded tests (windows-latest)","conclusion":"skipped"}'
108 expect_fail "shared-process run"
109
110 # 6. A missing suite check (renamed job, skipped OS) is not a receipt.
111 runs "$(run 24)"
112 jobs 24 "$(echo "${full_jobs}" | grep -v 'macos-latest) / Suite check' | sed '$ s/,$//')"
113 expect_fail "missing macOS suite"
114 grep -q "full-suite job did not succeed" "${tmp_dir}/out"
115
116 # 7. A failed, cancelled or still-running job disqualifies even a green run.
117 runs "$(run 25)"
118 jobs 25 "${full_jobs},{\"name\":\"Docs\",\"conclusion\":\"failure\"}"
119 expect_fail "failed job"
120 jobs 25 "${full_jobs},{\"name\":\"Docs\",\"conclusion\":null}"
121 expect_fail "unfinished job"
122
123 # 8. A truncated job list is refused rather than trusted.
124 jobs 25 "${full_jobs}" 150
125 expect_fail "truncated jobs"
126 grep -q "incomplete job list" "${tmp_dir}/out"
127
128 # 9. Wrong event, conclusion, SHA or repository never counts, even with full jobs.
129 for spec in "26 pull_request" "27 workflow_dispatch failure" "28 workflow_dispatch success ffffffffffffffffffffffffffffffffffffffff" "29 workflow_dispatch success ${sha} fork/repo"; do
130 read -r id event conclusion head owner <<< "${spec}"
131 runs "$(run "${id}" "${event}" "${conclusion:-success}" "${head:-${sha}}" "${owner:-owner/repo}")"
132 jobs "${id}" "${full_jobs}"
133 expect_fail "run filter ${spec}"
134 done
135
136 # 10. An unreadable job list skips that run; a later full run still counts.
137 runs "$(run 30)" "$(run 21)"
138 expect_pass "unreadable first run"
139 grep -q "https://example.invalid/30 not counted" "${tmp_dir}/out"
140
141 # 11. An API failure listing runs is a refusal.
142 rm -f "${tmp_dir}/runs.json"
143 expect_fail "api failure"
144 grep -q "Actions API call failed" "${tmp_dir}/out"
145
146 # 12. Malformed input is rejected before any API call.
147 expect_fail "short sha" "abc123"
148 expect_fail "uppercase sha" "0123456789ABCDEF0123456789ABCDEF01234567"
149 if "${script}" "bad repo" "${sha}" >/dev/null 2>&1; then
150 echo "FAIL (bad repo): expected refusal" >&2
151 exit 1
152 fi
153
154 echo "require-full-ci-receipt tests passed"
155
155 lines BASH