diff --git a/.gitea/workflows/main.yml b/.gitea/workflows/main.yml index 2339789..5952fcc 100644 --- a/.gitea/workflows/main.yml +++ b/.gitea/workflows/main.yml @@ -24,6 +24,8 @@ jobs: run: python3 ci/test-bench-history.py - name: Release evidence gate checks run: python3 ci/test-conformance.py + - name: Release publication checks + run: python3 ci/test-release.py - name: Quality run: cargo clippy --all-targets --locked -- -D warnings - name: Build diff --git a/README.md b/README.md index 359b7f6..f94d3a5 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ returned nonce. From this repository, run it with the SDK server. It is a simulator check and sends work to the named socket. The first BM1366 mining session sequences fan, temperature, PSU, reset, work submission, and nonce polling through the HAL. It rejects stale work IDs, -malformed reports, and hashes below the caller's pool target. A sensor fault +malformed reports, and hashes above the caller's pool target. A sensor fault requests PSU shutdown. A transport fault closes the HAL socket; the backend must end its power lease on disconnect and have an independent hardware watchdog. No production board backend meets that gate yet. The caller must @@ -87,19 +87,25 @@ policy or API changes; Z changes for compatible fixes and packaging updates. ## Local checks ```sh +export CARGO_TARGET_DIR=/var/tmp/alkane-os-target cargo fmt --check cargo clippy --all-targets -- -D warnings cargo test -python3 ci/check-conformance.py +cargo build --release --locked +GITHUB_RUN_ID=123 python3 ci/check-conformance.py ``` -The last command currently fails because simulator and device evidence are -still pending. It cross-checks the evidence matrix against the built binary's +Use an executable local `CARGO_TARGET_DIR` when the workspace mount strips +executable bits. Replace `123` with the numeric ID of the CI run that +generated its evidence. The last command currently fails because simulator +and device evidence are still pending. It cross-checks the matrix against the built binary's targets and requires per-cell reports from the current CI run, bound to the source commit and binary digest. Referenced artifacts are opened and hashed; -captured shares must satisfy a difficulty-one-or-harder target and carry a -matching pool ACCEPT. It is a release gate, not a unit test. The simulator +SHA-256 captured shares must satisfy a difficulty-one-or-harder target and +carry a matching pool ACCEPT response. Scrypt and Equihash verification is +pending. It is a release gate, not a unit test. The simulator runner that produces this evidence has not yet been implemented. +The required artifact contract is in [ci/CONFORMANCE.md](ci/CONFORMANCE.md). The badges record measured host-side checks for this initial core. They do not imply that any board image or mining target has passed conformance. diff --git a/ci/CONFORMANCE.md b/ci/CONFORMANCE.md new file mode 100644 index 0000000..1d09766 --- /dev/null +++ b/ci/CONFORMANCE.md @@ -0,0 +1,41 @@ +# Release conformance evidence + +`check-conformance.py` is a release gate for all 64 same-vendor cells. It +currently fails because the simulator and bench evidence producer is not yet +implemented. A green Rust build, host HAL replay probe, or rootfs archive does +not fill any cell. + +Each CI run must put a JSON report at +`ci/evidence///.json`. The report binds its +`run_id`, `source_commit`, `os_binary_sha256`, `board`, and `chip` to the +current workflow. It records `stock_firmware_modified: false`, +`simulator_booted_stock: true`, and a `stock_result` of `mines` or `rejects`. +For a rejection, `stock_first_rejection` names the first genuine firmware +gate. Reports for stock mining also reference `stock_pool_accept_capture`. + +The report references files under `ci/evidence/` with objects containing +`path` and `sha256`: `sdk_image`, `stock_image`, `simulator_trace`, +`simulator_model`, `control_capture`, `chip_capture`, +`alkane_share_accept_capture`, and `bench_validation`. +The checker opens and hashes each file. It loads the trace and capture JSON, +checks the current run/image/cell identities, verifies SHA-256d nonces against +a pool target at difficulty 1 or higher, and requires a matching +`mining.submit` nonce, ntime, and pool ACCEPT response ID. The trace binds +the capture digests. +Scrypt and Equihash share verifiers are pending, so those cells fail closed. +Bench calibration names the physical control-board and chip devices, their +capture digests, and the simulator model digest. It can be reused across OS +commits until the model or a physical capture changes. +The fixed digests and artifact paths also appear in `conformance.json` so +source review can track what each cell claims. + +The simulator runner must generate these reports by booting pristine stock +firmware and Alkane OS in a machine model, with nonce data replayed from +real-silicon captures. A rejected stock pairing must show its first rejection +in the simulator trace while Alkane OS reaches pool ACCEPT. Bench devices +provide independent observations for model validation, not CI dependencies. +The checker verifies its input artifacts and reported nonce. It cannot prove +that a self-asserted pool response came from a real pool or turn a report into +physical evidence. The capture producer must preserve raw request/response +bytes and independent provenance. Capture provenance and faithful machine +models remain release blockers until the runner and bench validation are built. diff --git a/ci/check-conformance.py b/ci/check-conformance.py index 02a43b8..fa2f7ae 100644 --- a/ci/check-conformance.py +++ b/ci/check-conformance.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# SPDX-License-Identifier: MIT """Require the binary target matrix and traceable per-cell release evidence.""" import argparse @@ -29,6 +30,11 @@ REQUIRED_CHIPS = { "KF1950", "ID-1968", "ID-1980", "ID-1990", )}, } +REQUIRED_ALGORITHMS = { + **{chip: "Sha256" for chip in REQUIRED_CHIPS if chip not in + ("BM1489", "BM1491", "BM1746")}, + "BM1489": "Scrypt", "BM1491": "Scrypt", "BM1746": "Equihash", +} DIGEST = re.compile(r"[0-9a-f]{64}\Z") COMMIT = re.compile(r"[0-9a-f]{40}\Z") DIFF_ONE_TARGET = 0xffff << (8 * (0x1d - 3)) @@ -66,7 +72,9 @@ def artifact(root: pathlib.Path, value: object) -> pathlib.Path: return path -def verify_accept(capture: dict) -> None: +def verify_accept(capture: dict, algorithm: str) -> None: + if algorithm != "Sha256": + raise ValueError(f"{algorithm} share verifier is not implemented") if capture.get("nonce_source") != "real_silicon_capture": raise ValueError("share was not sourced from captured silicon") header_hex = capture.get("header80") @@ -82,25 +90,34 @@ def verify_accept(capture: dict) -> None: raise ValueError("invalid accepted share header") from error if int.from_bytes(header[76:80], "little") != nonce: raise ValueError("nonce differs from submitted header") + submission = capture.get("submission") + response = capture.get("pool_response") + params = submission.get("params") if isinstance(submission, dict) else None + if (not isinstance(params, list) or len(params) not in (5, 6) + or not all(isinstance(value, str) for value in params) + or not params[0] or not params[1] + or not re.fullmatch(r"[0-9a-f]{0,64}", params[2].lower()) + or params[3].lower() != f"{int.from_bytes(header[68:72], 'little'):08x}" + or params[4].lower() != f"{nonce:08x}" + or (len(params) == 6 and not re.fullmatch(r"[0-9a-f]{8}", params[5].lower())) + or not isinstance(response, dict) + or submission.get("method") != "mining.submit" + or type(submission.get("id")) is not int + or response.get("id") != submission["id"] + or response.get("result") is not True + or response.get("error") is not None): + raise ValueError("no matching pool ACCEPT for submitted nonce") target = int.from_bytes(bytes.fromhex(target_hex), "little") if not 0 < target <= DIFF_ONE_TARGET: raise ValueError("pool target is below difficulty one") hash_bytes = hashlib.sha256(hashlib.sha256(header).digest()).digest() if int.from_bytes(hash_bytes, "little") > target: raise ValueError("captured nonce misses the pool target") - submission = capture.get("submission") - response = capture.get("pool_response") - if (not isinstance(submission, dict) or not isinstance(response, dict) - or submission.get("method") != "mining.submit" - or type(submission.get("id")) is not int - or response.get("id") != submission["id"] - or response.get("result") is not True - or response.get("error") is not None): - raise ValueError("no matching pool ACCEPT response") def verify_cell(row: dict, root: pathlib.Path, run_id: str, - source_commit: str, binary_digest: str) -> None: + source_commit: str, binary_digest: str, + algorithm: str) -> None: board, chip = row["board"], row["chip"] report_path = (root / run_id / board / f"{chip}.json").resolve() if not report_path.is_relative_to(root.resolve()): @@ -108,7 +125,7 @@ def verify_cell(row: dict, root: pathlib.Path, run_id: str, report = load_report(report_path) identity = {"schema": 1, "run_id": run_id, "source_commit": source_commit, "os_binary_sha256": binary_digest, "board": board, "chip": chip, - "stock_firmware_modified": False} + "stock_firmware_modified": False, "algorithm": algorithm} if any(report.get(key) != value for key, value in identity.items()): raise ValueError("evidence belongs to a different run, source, binary, or cell") if report.get("stock_result") != row.get("stock_result"): @@ -119,47 +136,82 @@ def verify_cell(row: dict, root: pathlib.Path, run_id: str, raise ValueError("stock rejection gate is missing") if report.get("simulator_booted_stock") is not True: raise ValueError("unmodified stock boot is unverified") + paths = {} for field, digest_field in (("sdk_image", "sdk_image_sha256"), ("stock_image", "stock_image_sha256"), ("simulator_trace", "simulator_trace_sha256")): - artifact(root, report.get(field)) + paths[field] = artifact(root, report.get(field)) if report[field]["sha256"] != row.get(digest_field): raise ValueError(f"{field} digest differs from manifest") + artifact(root, report.get("simulator_model")) + model_digest = report["simulator_model"]["sha256"] + for field in ("control_capture", "chip_capture"): + artifact(root, report.get(field)) + trace = load_report(paths["simulator_trace"]) + trace_identity = {"run_id": run_id, "source_commit": source_commit, + "board": board, "chip": chip, + "sdk_image_sha256": row["sdk_image_sha256"], + "stock_image_sha256": row["stock_image_sha256"], + "stock_firmware_modified": False, + "stock_result": row["stock_result"], + "algorithm": algorithm, + "simulator_model_sha256": model_digest} + if any(trace.get(key) != value for key, value in trace_identity.items()): + raise ValueError("simulator trace does not bind stock image and current run") + if report["stock_result"] == "rejects": + if trace.get("first_rejection") != row["stock_first_rejection"]: + raise ValueError("stock rejection gate is absent from simulator trace") + else: + stock_capture = load_report(artifact(root, report.get("stock_pool_accept_capture"))) + if trace.get("stock_pool_accept_capture_sha256") != report["stock_pool_accept_capture"]["sha256"]: + raise ValueError("stock pool acceptance is not linked to simulator trace") + if (stock_capture.get("board") != board or stock_capture.get("chip") != chip + or stock_capture.get("firmware") != "stock"): + raise ValueError("stock pool acceptance belongs to another firmware or cell") + verify_accept(stock_capture, algorithm) capture = load_report(artifact(root, report.get("alkane_share_accept_capture"))) if report["alkane_share_accept_capture"]["path"] != row.get("alkane_share_accept_capture"): raise ValueError("accepted share artifact differs from manifest") - if capture.get("board") != board or capture.get("chip") != chip: + if trace.get("alkane_share_accept_capture_sha256") != report["alkane_share_accept_capture"]["sha256"]: + raise ValueError("Alkane pool acceptance is not linked to simulator trace") + if (capture.get("board") != board or capture.get("chip") != chip + or capture.get("firmware") != "alkane"): raise ValueError("accepted share belongs to another cell") - verify_accept(capture) + verify_accept(capture, algorithm) bench = load_report(artifact(root, report.get("bench_validation"))) if report["bench_validation"]["path"] != row.get("bench_validation"): raise ValueError("bench artifact differs from manifest") if (bench.get("board") != board or bench.get("chip") != chip or bench.get("validated") is not True - or not isinstance(bench.get("device_id"), str) - or not bench["device_id"]): + or bench.get("simulator_model_sha256") != model_digest + or any(bench.get(f"{field}_sha256") != report[field]["sha256"] + for field in ("control_capture", "chip_capture")) + or any(not isinstance(bench.get(f"{name}_device_id"), str) + or not bench[f"{name}_device_id"] + for name in ("control", "chip"))): raise ValueError("bench validation is missing or belongs to another cell") -def binary_targets(path: pathlib.Path) -> set[tuple[str, str]]: +def binary_targets(path: pathlib.Path) -> dict[tuple[str, str], str]: output = subprocess.run( [str(path), "--list-targets"], check=True, capture_output=True, text=True, timeout=15, ).stdout.splitlines() if not output or output[0] != "control_board,hash_chip,algorithm": raise ValueError("Alkane OS binary has no target matrix header") - targets = [] + targets = {} for line in output[1:]: parts = line.split(",") if len(parts) != 3 or not all(parts): raise ValueError(f"invalid binary target row: {line!r}") - targets.append((parts[0], parts[1])) - if len(targets) != len(set(targets)): - raise ValueError("duplicate binary target") - return set(targets) + cell = (parts[0], parts[1]) + if cell in targets: + raise ValueError("duplicate binary target") + targets[cell] = parts[2] + return targets -def validate(manifest: dict, built_targets: set[tuple[str, str]], +def validate(manifest: dict, built_targets: dict[tuple[str, str], str], evidence_root: pathlib.Path = pathlib.Path("ci/evidence"), run_id: str = "", source_commit: str = "", binary_digest: str = "") -> list[str]: @@ -182,8 +234,11 @@ def validate(manifest: dict, built_targets: set[tuple[str, str]], actual = {(row["board"], row["chip"]) for row in rows} if len(expected) < 64 or actual != expected or len(rows) != len(expected): errors.append(f"manifest matrix mismatch: {len(actual)} present, {len(expected)} expected") - if built_targets != expected: + if set(built_targets) != expected: errors.append(f"binary matrix mismatch: {len(built_targets)} built, {len(expected)} expected") + for (board, chip), algorithm in built_targets.items(): + if chip in REQUIRED_ALGORITHMS and algorithm != REQUIRED_ALGORITHMS[chip]: + errors.append(f"binary algorithm mismatch: {board} x {chip}") if (not run_id.isdecimal() or not COMMIT.fullmatch(source_commit) or not DIGEST.fullmatch(binary_digest)): errors.append("current run, source commit, or binary digest is unbound") @@ -205,7 +260,8 @@ def validate(manifest: dict, built_targets: set[tuple[str, str]], errors.append(f"{cell}: {', '.join(missing)}") elif run_id.isdecimal() and COMMIT.fullmatch(source_commit) and DIGEST.fullmatch(binary_digest): try: - verify_cell(row, evidence_root, run_id, source_commit, binary_digest) + verify_cell(row, evidence_root, run_id, source_commit, binary_digest, + built_targets.get((row["board"], row["chip"]), "unknown")) except (OSError, ValueError, KeyError, TypeError, json.JSONDecodeError) as error: errors.append(f"{cell}: evidence invalid: {error}") return errors diff --git a/ci/release.py b/ci/release.py index 5e324cd..343537d 100644 --- a/ci/release.py +++ b/ci/release.py @@ -1,7 +1,9 @@ #!/usr/bin/env python3 -"""Create one Gitea release and attach files using the runner's scoped token.""" +# SPDX-License-Identifier: MIT +"""Publish a complete Gitea release after all assets are attached to a draft.""" import json +import hashlib import os import pathlib import sys @@ -10,45 +12,129 @@ import urllib.parse import urllib.request -def request(url, payload, content_type): - headers = { - "Authorization": "token " + os.environ["GITEA_TOKEN"], - "Content-Type": content_type, +def request(url, method="GET", payload=None, content_type=None): + headers = {"Authorization": "token " + os.environ["GITEA_TOKEN"]} + if content_type: + headers["Content-Type"] = content_type + operation = urllib.request.Request(url, data=payload, headers=headers, method=method) + with urllib.request.urlopen(operation, timeout=60) as response: + body = response.read() + return json.loads(body) if body else None + + +def json_request(url, method, value): + return request(url, method, json.dumps(value).encode(), "application/json") + + +def files_to_publish(names): + paths = [pathlib.Path(name) for name in names] + if not paths or len({path.name for path in paths}) != len(paths): + raise ValueError("release assets must have distinct basenames") + for path in paths: + if not path.is_file() or path.stat().st_size == 0: + raise ValueError(f"release asset missing or empty: {path}") + return paths + + +def sha256_file(path): + digest = hashlib.sha256() + with path.open("rb") as source: + for chunk in iter(lambda: source.read(1_048_576), b""): + digest.update(chunk) + return digest.hexdigest() + + +class NoRedirect(urllib.request.HTTPRedirectHandler): + def redirect_request(self, request, fp, code, message, headers, new_url): + return None + + +def asset_digest(asset, api): + url = asset["browser_download_url"] + remote = urllib.parse.urlsplit(url) + expected = urllib.parse.urlsplit(api) + if remote.scheme != "https" or remote.netloc != expected.netloc: + raise ValueError("asset download is outside the trusted Gitea host") + operation = urllib.request.Request( + url, headers={"Authorization": "token " + os.environ["GITEA_TOKEN"]}) + digest = hashlib.sha256() + with urllib.request.build_opener(NoRedirect).open(operation, timeout=60) as response: + for chunk in iter(lambda: response.read(1_048_576), b""): + digest.update(chunk) + return digest.hexdigest() + + +def get_release(url): + try: + return request(url) + except urllib.error.HTTPError as error: + if error.code == 404: + return None + raise + + +def publish(tag, prerelease, paths, api, repo, sha): + base = f"{api}/repos/{repo}/releases" + tag_url = f"{base}/tags/{urllib.parse.quote(tag, safe='')}" + metadata = { + "tag_name": tag, + "target_commitish": sha, + "name": tag, + "body": f"Automated Alkane OS build from {sha}. Conformance results are in the repository.", + "prerelease": prerelease, } - req = urllib.request.Request(url, data=payload, headers=headers, method="POST") - with urllib.request.urlopen(req, timeout=60) as response: - return json.load(response) + release = get_release(tag_url) + if release is None: + release = json_request(base, "POST", {**metadata, "draft": True}) + elif release.get("target_commitish") != sha: + raise ValueError("release tag already belongs to another source revision") + release_url = f"{base}/{release['id']}" + assets_url = f"{release_url}/assets" + existing = request(assets_url) + wanted = {path.name: path.stat().st_size for path in paths} + digests = {path.name: sha256_file(path) for path in paths} + present = {asset["name"]: asset for asset in existing} + if not release.get("draft") and all( + (asset := present.get(name)) is not None and asset["size"] == size + and asset_digest(asset, api) == digests[name] + for name, size in wanted.items() + ): + print(f"Release {tag} is already complete") + return + if not release.get("draft"): + release = json_request(release_url, "PATCH", {**metadata, "draft": True}) + if not release.get("draft"): + raise ValueError("incomplete release could not be returned to draft") + for asset in existing: + if asset["name"] in wanted: + request(f"{assets_url}/{asset['id']}", "DELETE") + for path in paths: + query = urllib.parse.urlencode({"name": path.name}) + request(f"{assets_url}?{query}", "POST", path.read_bytes(), + "application/octet-stream") + uploaded = request(assets_url) + actual = {asset["name"]: asset for asset in uploaded} + if any((asset := actual.get(name)) is None or asset["size"] != size + or asset_digest(asset, api) != digests[name] + for name, size in wanted.items()): + raise ValueError("draft release asset verification failed") + release = json_request(release_url, "PATCH", {**metadata, "draft": False}) + if release.get("draft"): + raise ValueError("release remained a draft after publishing") + print(f"Published {tag} with {len(paths)} assets") def main(): if len(sys.argv) < 4: sys.exit("usage: release.py TAG PRERELEASE FILE [FILE ...]") - tag, prerelease, *files = sys.argv[1:] - api = os.environ.get("GITHUB_API_URL", "https://gitea.bluefla.re/api/v1").rstrip("/") - repo = os.environ["GITHUB_REPOSITORY"] - sha = os.environ["GITHUB_SHA"] - body = f"Automated Alkane OS build from {sha}. Conformance results are in the repository." - metadata = { - "tag_name": tag, - "target_commitish": sha, - "name": tag, - "body": body, - "prerelease": prerelease.lower() == "true", - } + tag, prerelease, *names = sys.argv[1:] try: - release = request(f"{api}/repos/{repo}/releases", json.dumps(metadata).encode(), "application/json") - except urllib.error.HTTPError as exc: - sys.exit(f"release creation failed: HTTP {exc.code}") - release_id = release["id"] - for name in files: - path = pathlib.Path(name) - query = urllib.parse.urlencode({"name": path.name}) - try: - request(f"{api}/repos/{repo}/releases/{release_id}/assets?{query}", - path.read_bytes(), "application/octet-stream") - except urllib.error.HTTPError as exc: - sys.exit(f"asset upload failed for {path.name}: HTTP {exc.code}") - print(f"Created {tag} with {len(files)} assets") + paths = files_to_publish(names) + api = os.environ.get("GITHUB_API_URL", "https://gitea.bluefla.re/api/v1").rstrip("/") + publish(tag, prerelease.lower() == "true", paths, api, + os.environ["GITHUB_REPOSITORY"], os.environ["GITHUB_SHA"]) + except (ValueError, OSError, urllib.error.HTTPError) as error: + sys.exit(f"release publication failed: {error}") if __name__ == "__main__": diff --git a/ci/test-conformance.py b/ci/test-conformance.py index e8022b7..4c38808 100644 --- a/ci/test-conformance.py +++ b/ci/test-conformance.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# SPDX-License-Identifier: MIT """Regression checks for the fail-closed release evidence gate.""" import copy @@ -14,7 +15,8 @@ spec = importlib.util.spec_from_file_location("check_conformance", SCRIPT) gate = importlib.util.module_from_spec(spec) spec.loader.exec_module(gate) MANIFEST = json.loads(Path(__file__).with_name("conformance.json").read_text()) -TARGETS = {(row["board"], row["chip"]) for row in MANIFEST["targets"]} +TARGETS = {(row["board"], row["chip"]): gate.REQUIRED_ALGORITHMS[row["chip"]] + for row in MANIFEST["targets"]} class ConformanceGateTest(unittest.TestCase): @@ -46,16 +48,27 @@ class ConformanceGateTest(unittest.TestCase): "board": row["board"], "chip": row["chip"], "stock_firmware_modified": False})) with self.assertRaisesRegex(ValueError, "different run, source"): - gate.verify_cell(row, root, "123", "a" * 40, "b" * 64) + gate.verify_cell(row, root, "123", "a" * 40, "b" * 64, + TARGETS[(row["board"], row["chip"])]) def test_share_accept_requires_real_difficulty_one_work(self): capture = {"nonce_source": "real_silicon_capture", "header80": "00" * 80, "pool_target_le": "ff" * 32, "nonce": 0, - "submission": {"id": 4, "method": "mining.submit"}, + "submission": {"id": 4, "method": "mining.submit", + "params": ["worker", "job", "", "00000000", "00000000"]}, "pool_response": {"id": 4, "result": True, "error": None}} with self.assertRaisesRegex(ValueError, "below difficulty one"): - gate.verify_accept(capture) + gate.verify_accept(capture, "Sha256") + capture["submission"]["params"][4] = "00000001" + with self.assertRaisesRegex(ValueError, "submitted nonce"): + gate.verify_accept(capture, "Sha256") + + def test_non_sha256_cells_need_algorithm_specific_verifiers(self): + with self.assertRaisesRegex(ValueError, "Scrypt share verifier"): + gate.verify_accept({}, "Scrypt") + with self.assertRaisesRegex(ValueError, "Equihash share verifier"): + gate.verify_accept({}, "Equihash") if __name__ == "__main__": diff --git a/ci/test-release.py b/ci/test-release.py new file mode 100644 index 0000000..ffe0cba --- /dev/null +++ b/ci/test-release.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: MIT +"""Check draft release recovery without making network requests.""" + +import importlib.util +import hashlib +from pathlib import Path +from tempfile import TemporaryDirectory +import unittest +from unittest.mock import patch + + +SCRIPT = Path(__file__).with_name("release.py") +spec = importlib.util.spec_from_file_location("release", SCRIPT) +release = importlib.util.module_from_spec(spec) +spec.loader.exec_module(release) + + +class FakeGitea: + def __init__(self): + self.release = None + self.assets = [] + self.blobs = {} + self.fail_second_upload = True + self.uploads = 0 + + def request(self, url, method="GET", payload=None, content_type=None): + if "/assets" in url: + if method == "GET": + return list(self.assets) + if method == "DELETE": + asset_id = int(url.rsplit("/", 1)[1]) + self.assets = [item for item in self.assets if item["id"] != asset_id] + self.blobs.pop(asset_id, None) + return None + self.uploads += 1 + if self.fail_second_upload and self.uploads == 2: + raise OSError("injected upload failure") + name = release.urllib.parse.parse_qs(url.split("?", 1)[1])["name"][0] + item = {"id": self.uploads, "name": name, "size": len(payload), + "browser_download_url": f"https://api/download/{self.uploads}"} + self.assets.append(item) + self.blobs[self.uploads] = payload + return item + if method == "GET": + return self.release + if method == "POST": + assert self.release is None + self.release = {**release.json.loads(payload), "id": 7} + return dict(self.release) + if method == "PATCH": + self.release.update(release.json.loads(payload)) + return dict(self.release) + raise AssertionError((method, url)) + + +class ReleaseTest(unittest.TestCase): + def test_retry_after_partial_upload_keeps_release_draft_until_complete(self): + with TemporaryDirectory() as directory: + paths = [Path(directory) / "source.tar.gz", Path(directory) / "bench.txt"] + for path in paths: + path.write_bytes(path.name.encode()) + fake = FakeGitea() + def digest(asset, _api): + return hashlib.sha256(fake.blobs[asset["id"]]).hexdigest() + + with patch.object(release, "request", fake.request), \ + patch.object(release, "asset_digest", digest): + with self.assertRaisesRegex(OSError, "injected upload failure"): + release.publish("v1.2.3", False, paths, "https://api", "org/repo", "a" * 40) + self.assertTrue(fake.release["draft"]) + self.assertEqual(len(fake.assets), 1) + fake.fail_second_upload = False + release.publish("v1.2.3", False, paths, "https://api", "org/repo", "a" * 40) + self.assertFalse(fake.release["draft"]) + self.assertEqual({item["name"] for item in fake.assets}, + {path.name for path in paths}) + fake.assets[0]["size"] = len(fake.blobs[fake.assets[0]["id"]]) + fake.blobs[fake.assets[0]["id"]] = b"x" * fake.assets[0]["size"] + release.publish("v1.2.3", False, paths, "https://api", "org/repo", "a" * 40) + self.assertFalse(fake.release["draft"]) + self.assertEqual(digest(fake.assets[0], "https://api"), + release.sha256_file(paths[0])) + + def test_preflight_rejects_missing_and_duplicate_assets(self): + with TemporaryDirectory() as directory: + path = Path(directory) / "one" + with self.assertRaisesRegex(ValueError, "missing or empty"): + release.files_to_publish([str(path)]) + path.write_bytes(b"x") + with self.assertRaisesRegex(ValueError, "distinct"): + release.files_to_publish([str(path), str(path)]) + + +if __name__ == "__main__": + unittest.main()