forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_check_release_eligibility.py
More file actions
294 lines (262 loc) · 14.2 KB
/
Copy pathtest_check_release_eligibility.py
File metadata and controls
294 lines (262 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env python3
"""Fixtures for the automatic main-SHA release eligibility proof."""
from __future__ import annotations
import importlib.util
import shutil
import sys
import tempfile
import unittest
from pathlib import Path
SCRIPT_DIR = Path(__file__).resolve().parent
ROOT = SCRIPT_DIR.parents[1]
CHECKER_PATH = SCRIPT_DIR / "check_release_eligibility.py"
VERIFIER_PATH = SCRIPT_DIR / "verify_release_eligibility.py"
def load_module(name: str, path: Path):
spec = importlib.util.spec_from_file_location(name, path)
assert spec and spec.loader
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module
spec.loader.exec_module(module)
return module
CHECKER = load_module("check_release_eligibility", CHECKER_PATH)
VERIFIER = load_module("verify_release_eligibility", VERIFIER_PATH)
SHA = "a" * 40
BASE_SHA = "b" * 40
class ReleaseIdentityTests(unittest.TestCase):
def identity(self, **overrides: str):
values = {
"ref": "refs/heads/main",
"sha": SHA,
"before": BASE_SHA,
"after": SHA,
"checkout_sha": SHA,
}
values.update(overrides)
return VERIFIER.ReleaseIdentity(**values)
def test_accepts_exact_main_sha_identity(self) -> None:
VERIFIER.validate(self.identity())
def test_rejects_non_main_ref(self) -> None:
with self.assertRaisesRegex(VERIFIER.ReleaseEligibilityError, "refs/heads/main"):
VERIFIER.validate(self.identity(ref="refs/heads/release"))
def test_rejects_ambiguous_or_non_sha_release_identity(self) -> None:
for value in ("main", "a" * 7, "A" * 40, "0" * 40):
with self.subTest(value=value), self.assertRaisesRegex(VERIFIER.ReleaseEligibilityError, "release SHA"):
VERIFIER.validate(self.identity(sha=value))
def test_rejects_event_or_checkout_sha_mismatch(self) -> None:
with self.assertRaisesRegex(VERIFIER.ReleaseEligibilityError, "event after SHA"):
VERIFIER.validate(self.identity(after="c" * 40))
with self.assertRaisesRegex(VERIFIER.ReleaseEligibilityError, "checked-out SHA"):
VERIFIER.validate(self.identity(checkout_sha="c" * 40))
class WorkflowContractTests(unittest.TestCase):
def fixture_root(self) -> Path:
temp = Path(tempfile.mkdtemp())
workflow = temp / ".github/workflows/release-eligibility.yml"
action = temp / ".github/actions/release-eligibility/action.yml"
workflow.parent.mkdir(parents=True)
action.parent.mkdir(parents=True)
shutil.copy2(ROOT / ".github/workflows/release-eligibility.yml", workflow)
shutil.copy2(ROOT / ".github/actions/release-eligibility/action.yml", action)
self.addCleanup(shutil.rmtree, temp)
return temp
def test_current_workflow_and_action_are_valid(self) -> None:
self.assertEqual(CHECKER.validate(), [])
def test_non_main_push_trigger_is_rejected(self) -> None:
root = self.fixture_root()
workflow = root / ".github/workflows/release-eligibility.yml"
workflow.write_text(workflow.read_text(encoding="utf-8").replace("branches: [main]", "branches: [development]"), encoding="utf-8")
self.assertIn("release eligibility must trigger only on pushes to main", CHECKER.validate(root))
def test_path_filter_is_rejected(self) -> None:
root = self.fixture_root()
workflow = root / ".github/workflows/release-eligibility.yml"
workflow.write_text(
workflow.read_text(encoding="utf-8").replace("branches: [main]", "branches: [main]\n paths: ['backend/**']"),
encoding="utf-8",
)
self.assertIn("release eligibility must not path-filter or otherwise narrow main pushes", CHECKER.validate(root))
def test_manual_dispatch_is_rejected(self) -> None:
root = self.fixture_root()
workflow = root / ".github/workflows/release-eligibility.yml"
workflow.write_text(
workflow.read_text(encoding="utf-8").replace("on:\n", "on:\n workflow_dispatch:\n", 1),
encoding="utf-8",
)
self.assertIn("release eligibility must declare only the automatic push trigger", CHECKER.validate(root))
def test_quoted_extra_trigger_is_rejected(self) -> None:
root = self.fixture_root()
workflow = root / ".github/workflows/release-eligibility.yml"
workflow.write_text(
workflow.read_text(encoding="utf-8").replace("on:\n", "on:\n 'workflow_dispatch':\n", 1),
encoding="utf-8",
)
self.assertIn("release eligibility must declare only the automatic push trigger", CHECKER.validate(root))
def test_conditional_result_job_is_rejected(self) -> None:
root = self.fixture_root()
workflow = root / ".github/workflows/release-eligibility.yml"
workflow.write_text(
workflow.read_text(encoding="utf-8").replace(" runs-on: ubuntu-latest", " if: github.ref == 'refs/heads/main'\n runs-on: ubuntu-latest"),
encoding="utf-8",
)
self.assertIn("release eligibility result job must not be conditionally skipped or tolerated", CHECKER.validate(root))
def test_result_job_continue_on_error_is_rejected(self) -> None:
root = self.fixture_root()
workflow = root / ".github/workflows/release-eligibility.yml"
workflow.write_text(
workflow.read_text(encoding="utf-8").replace(" runs-on: ubuntu-latest", " continue-on-error: true\n runs-on: ubuntu-latest"),
encoding="utf-8",
)
self.assertIn("release eligibility result job must not be conditionally skipped or tolerated", CHECKER.validate(root))
def test_action_invocation_cannot_be_skipped_or_tolerated(self) -> None:
for field in ("if: github.ref == 'refs/heads/main'", "continue-on-error: true"):
with self.subTest(field=field):
root = self.fixture_root()
workflow = root / ".github/workflows/release-eligibility.yml"
workflow.write_text(
workflow.read_text(encoding="utf-8").replace(
" - name: Verify release eligibility\n uses:",
f" - name: Verify release eligibility\n {field}\n uses:",
),
encoding="utf-8",
)
self.assertIn(
"release eligibility action invocation must not be conditionally skipped or tolerated",
CHECKER.validate(root),
)
def test_least_privilege_permissions_are_enforced(self) -> None:
root = self.fixture_root()
workflow = root / ".github/workflows/release-eligibility.yml"
workflow.write_text(
workflow.read_text(encoding="utf-8").replace(" contents: read", " contents: read\n actions: write"),
encoding="utf-8",
)
self.assertIn("release eligibility must use only repository contents: read permissions", CHECKER.validate(root))
def test_ambiguous_workflow_sha_is_rejected(self) -> None:
root = self.fixture_root()
workflow = root / ".github/workflows/release-eligibility.yml"
workflow.write_text(workflow.read_text(encoding="utf-8").replace("sha: ${{ github.sha }}", "sha: main"), encoding="utf-8")
self.assertIn("release eligibility must pass sha as ${{ github.sha }}", CHECKER.validate(root))
def test_missing_checkout_sha_binding_is_rejected(self) -> None:
root = self.fixture_root()
action = root / ".github/actions/release-eligibility/action.yml"
action.write_text(
action.read_text(encoding="utf-8").replace("--checkout-sha \"$RELEASE_CHECKOUT_SHA\"", "--checkout-sha main"),
encoding="utf-8",
)
self.assertIn("release identity validator must receive the checkout SHA", CHECKER.validate(root))
def test_uv_setup_is_required_and_fail_closed_before_canonical_preflight(self) -> None:
cases = (
(
"missing setup",
" - name: Set up uv for canonical checks\n uses: astral-sh/setup-uv@ecd24dd710f2fb0dca1693a67af11fc4a5c5ec84\n with:\n enable-cache: true\n cache-dependency-glob: backend/openapi-requirements.txt\n\n",
"",
"release eligibility is missing its uv setup step",
),
(
"unpinned-or-wrong setup",
"uses: astral-sh/setup-uv@ecd24dd710f2fb0dca1693a67af11fc4a5c5ec84",
"uses: astral-sh/setup-uv@v7",
"release eligibility uv setup step must use the pinned setup action",
),
(
"conditional setup",
" - name: Set up uv for canonical checks\n uses:",
" - name: Set up uv for canonical checks\n if: github.ref == 'refs/heads/main'\n uses:",
"release eligibility uv setup step must not be conditionally skipped or tolerated",
),
(
"missing OpenAPI cache key",
"cache-dependency-glob: backend/openapi-requirements.txt",
"cache-dependency-glob: backend/pylock.toml",
"release eligibility uv setup must key the OpenAPI dependency cache",
),
(
"setup after preflight",
" - name: Set up uv for canonical checks\n uses: astral-sh/setup-uv@ecd24dd710f2fb0dca1693a67af11fc4a5c5ec84\n with:\n enable-cache: true\n cache-dependency-glob: backend/openapi-requirements.txt\n\n",
"",
"release eligibility must set up uv before the canonical preflight",
),
)
for name, old, new, expected in cases:
with self.subTest(name=name):
root = self.fixture_root()
action = root / ".github/actions/release-eligibility/action.yml"
text = action.read_text(encoding="utf-8")
if name == "setup after preflight":
self.assertIn(old, text)
uv_start = text.index(old)
preflight_start = text.index(" - name: Run canonical deterministic CI preflight")
text = (
text[:uv_start]
+ text[uv_start + len(old) : preflight_start]
+ text[preflight_start:]
+ old
)
else:
self.assertIn(old, text)
text = text.replace(old, new, 1)
action.write_text(text, encoding="utf-8")
self.assertIn(expected, CHECKER.validate(root))
def test_critical_action_steps_cannot_be_skipped_or_fail_open(self) -> None:
cases = (
(
"identity condition",
" - name: Verify exact main release identity\n shell:",
" - name: Verify exact main release identity\n if: github.ref == 'refs/heads/main'\n shell:",
"release eligibility identity validation step must not be conditionally skipped or tolerated",
),
(
"identity tolerance",
" - name: Verify exact main release identity\n shell:",
" - name: Verify exact main release identity\n continue-on-error: true\n shell:",
"release eligibility identity validation step must not be conditionally skipped or tolerated",
),
(
"identity fail open",
"git cat-file -e \"${RELEASE_SHA}^{commit}\"",
"git cat-file -e \"${RELEASE_SHA}^{commit}\" || true",
"release eligibility identity validation step must not contain a shell fail-open path",
),
(
"identity alternate fail open",
"git cat-file -e \"${RELEASE_SHA}^{commit}\"",
"git cat-file -e \"${RELEASE_SHA}^{commit}\" || :",
"release eligibility identity validation step must not contain a shell fail-open path",
),
(
"preflight condition",
" - name: Run canonical deterministic CI preflight\n shell:",
" - name: Run canonical deterministic CI preflight\n if: github.ref == 'refs/heads/main'\n shell:",
"release eligibility canonical preflight step must not be conditionally skipped or tolerated",
),
(
"preflight tolerance",
" - name: Run canonical deterministic CI preflight\n shell:",
" - name: Run canonical deterministic CI preflight\n continue-on-error: true\n shell:",
"release eligibility canonical preflight step must not be conditionally skipped or tolerated",
),
(
"preflight fail open",
"--skip-pr-body-checks",
"--skip-pr-body-checks || true",
"release eligibility canonical preflight step must not contain a shell fail-open path",
),
(
"preflight disables strict errors",
" set -euo pipefail\n python3 .github/scripts/run_checks.py",
" set -euo pipefail\n set +e\n python3 .github/scripts/run_checks.py",
"release eligibility canonical preflight step must not contain a shell fail-open path",
),
(
"missing ancestry",
" git merge-base --is-ancestor \"$RELEASE_BEFORE\" \"$RELEASE_SHA\"\n",
"",
"release eligibility must require the base SHA to be an ancestor",
),
)
for name, old, new, expected in cases:
with self.subTest(name=name):
root = self.fixture_root()
action = root / ".github/actions/release-eligibility/action.yml"
action.write_text(action.read_text(encoding="utf-8").replace(old, new, 1), encoding="utf-8")
self.assertIn(expected, CHECKER.validate(root))
if __name__ == "__main__":
unittest.main()