diff --git a/setup.py b/setup.py index 4b61948..6c1440b 100644 --- a/setup.py +++ b/setup.py @@ -29,7 +29,7 @@ def stamp_api_version(): setup( name="science-synapse", - version="2.9.0", + version="2.10.0", description="Client library and CLI for the Synapse API", author="Science Team", author_email="team@science.xyz", diff --git a/synapse/cli/deploy.py b/synapse/cli/deploy.py index c2acfc3..4cb4def 100644 --- a/synapse/cli/deploy.py +++ b/synapse/cli/deploy.py @@ -71,13 +71,14 @@ def create_metadata(file_path, console): return metadata -def deploy_package(ip_address, deb_package_path): +def deploy_package(ip_address, deb_package_path, timeout=None): """Deploy a .deb package to the device via gRPC DeployApp streaming. Returns True when the package was streamed and all device responses were received without error; False on any failure (connection, gRPC, or I/O). Callers can use the return value to decide whether to continue with - subsequent packages. + subsequent packages. ``timeout`` bounds the whole DeployApp call in + seconds (upload and install); None waits indefinitely. """ package_filename = os.path.basename(deb_package_path) console.clear_live() @@ -120,7 +121,9 @@ def chunk_generator(): yield chunk try: - device_responses = device.rpc.DeployApp(chunk_generator()) + device_responses = device.rpc.DeployApp( + chunk_generator(), timeout=timeout + ) current_index = 0 # Process each response from the device diff --git a/synapse/cli/gateware.py b/synapse/cli/gateware.py index fb9491e..c21b5c0 100644 --- a/synapse/cli/gateware.py +++ b/synapse/cli/gateware.py @@ -9,7 +9,6 @@ from __future__ import annotations -import glob import json import os import re @@ -17,7 +16,7 @@ import sys import uuid from pathlib import Path -from typing import Mapping, Sequence +from typing import Iterable, Mapping, Sequence from rich.console import Console @@ -218,6 +217,80 @@ def resolve_gateware_project( ) +# Highest build-summary schema this synapse understands. The SDK releases +# independently, so a newer schema must fail loudly rather than be misread. +_MAX_SUMMARY_SCHEMA = 2 + + +def _scan_bitstreams(bitstreams_dir: str) -> dict[str, tuple[int, int]]: + """Return ``{name: (mtime_ns, size)}`` for the regular files in *bitstreams_dir*. + + Size rides with mtime because some filesystems store whole seconds, so a + same-second rewrite would otherwise look untouched. + """ + if not os.path.isdir(bitstreams_dir): + return {} + files = {} + with os.scandir(bitstreams_dir) as entries: + for entry in entries: + if entry.is_file(): + st = entry.stat() + files[entry.name] = (st.st_mtime_ns, st.st_size) + return files + + +def _load_summary(path: str) -> dict | None: + """Return the build summary at *path* as a dict, or None if unreadable.""" + try: + with open(path, "r", encoding="utf-8") as fp: + summary = json.load(fp) + except (OSError, json.JSONDecodeError): + return None + return summary if isinstance(summary, dict) else None + + +def _summary_schema(path: str) -> tuple[int, dict] | None: + """Return ``(schema_version, summary)`` for a build summary, or None if unreadable.""" + summary = _load_summary(path) + if summary is None: + return None + version = summary.get("schema_version", 1) + if type(version) is not int: + raise ValueError( + f"Bitstream summary {path} has a non-integer schema_version {version!r}" + ) + return version, summary + + +def _schema2_bitstream( + bitstreams_dir: str, + summary_name: str, + summary: dict, + after: Mapping[str, tuple[int, int]], + emitted: set[str], +) -> str: + """Return the image a schema-2 summary names, checked to be from this build.""" + name = summary.get("bitstream") + # The readers find a summary by its image's stem, so the two must pair up. + expected = summary_name.removesuffix(".summary.json") + ".bit" + if name != expected: + raise ValueError( + f"Bitstream summary {summary_name} has 'bitstream' {name!r}; " + f"expected {expected!r}, the image next to it" + ) + if name not in after: + raise FileNotFoundError( + f"Bitstream summary {summary_name} names {name}, which does not " + f"exist in {bitstreams_dir}" + ) + if name not in emitted: + raise ValueError( + f"Bitstream summary {summary_name} names {name}, which this build " + "did not write (it predates the build)" + ) + return os.path.join(bitstreams_dir, name) + + def run_gateware_build( peripheral_dir: str, image_tag: str, @@ -231,24 +304,37 @@ def run_gateware_build( root either way, so a per-profile project can still reference shared sources above it (``../axon_test_source_peripheral.sv``). - Returns the absolute path to the newest ``sdk_*_extracted.bit`` emitted - under ``//build/bitstreams/``. This is the - *extracted* bitstream variant the SDK writes alongside the raw - ``sdk_*.bit``; the extracted form is what gets flashed to the probe, and - it carries its own same-stem ``.summary.json`` (same schema as the raw - one). Selecting it specifically — rather than the broader ``sdk_*.bit`` - glob, which would also match the extracted file and pick by mtime — - keeps the choice deterministic. + Returns the absolute path to the image this build emitted under + ``//build/bitstreams/``. Build directories + persist between builds, so only files the build created or changed (by + mtime and size) are considered; older files are ignored whatever their + name. Among those: + + - A schema-2 ``.summary.json`` names its image, ``.bit``, + in ``bitstream``; the newest such summary wins. + - Otherwise (schema 1, no ``schema_version``, or no summary) the newest + native ``sdk_*.bit`` wins, else the newest ``sdk_*_extracted.bit`` + (older SDKs emit only the extracted image). + + Unreadable summaries are skipped here. The image's same-stem + ``.summary.json`` describes it. Raises: LicenseUnsetError: if ``LM_LICENSE_FILE`` is unset (propagated from :func:`build_license_docker_args`). subprocess.CalledProcessError: if the container's build exits non-zero. - FileNotFoundError: if the build succeeds but no bitstream is emitted. + ValueError: if a new summary has a schema newer than this synapse + supports or a malformed ``schema_version``, or a schema-2 summary + names an invalid or pre-existing image. + FileNotFoundError: if the build emitted no bitstream, or a schema-2 + summary names a missing image. """ license_args = build_license_docker_args(env) abs_peripheral_dir = os.path.abspath(peripheral_dir) + bitstreams_dir = os.path.join( + abs_peripheral_dir, *project_subdir.split("/"), "build", "bitstreams" + ) argv = [ "docker", "run", @@ -265,24 +351,49 @@ def run_gateware_build( "-lc", f"axon-peripheral-sdk build --project {project_subdir}", ] + before = _scan_bitstreams(bitstreams_dir) subprocess.run(argv, check=True) + after = _scan_bitstreams(bitstreams_dir) + + emitted = {name for name, ident in after.items() if before.get(name) != ident} + + def newest(names: Iterable[str]) -> list[str]: + return sorted(names, key=lambda name: after[name][0], reverse=True) + + schema2 = {} + for name in emitted: + if not (name.startswith("sdk_") and name.endswith(".summary.json")): + continue + parsed = _summary_schema(os.path.join(bitstreams_dir, name)) + if parsed is None: + continue + version, summary = parsed + if version > _MAX_SUMMARY_SCHEMA: + raise ValueError( + f"Bitstream summary {name} uses schema_version {version}, newer " + f"than this synapse supports ({_MAX_SUMMARY_SCHEMA}). Upgrade " + "science-synapse to package this build." + ) + if version == 2: + schema2[name] = summary - bit_glob = os.path.join( - abs_peripheral_dir, - *project_subdir.split("/"), - "build", - "bitstreams", - "sdk_*_extracted.bit", - ) - matches = glob.glob(bit_glob) - if not matches: - raise FileNotFoundError( - "axon-peripheral-sdk build completed but no sdk_*_extracted.bit was " - f"emitted under {project_subdir}/build/bitstreams/" + if schema2: + matches = newest(schema2) + chosen = _schema2_bitstream( + bitstreams_dir, matches[0], schema2[matches[0]], after, emitted ) + else: + bits = [n for n in emitted if n.startswith("sdk_") and n.endswith(".bit")] + matches = [n for n in bits if not n.endswith("_extracted.bit")] or bits + if not matches: + raise FileNotFoundError( + "axon-peripheral-sdk build completed but no sdk_*.bit was " + f"emitted by this build under {project_subdir}/build/bitstreams/ " + "(files left from earlier builds are ignored)" + ) + matches = newest(matches) + chosen = os.path.join(bitstreams_dir, matches[0]) - matches.sort(key=os.path.getmtime, reverse=True) - chosen = matches[0] if len(matches) > 1: console.print( f"[yellow]Multiple bitstreams matched; selected newest: {chosen}[/yellow]" @@ -340,7 +451,7 @@ def read_usb_pid(bit_path: str) -> int: if not isinstance(summary, dict): raise ValueError( f"Bitstream summary {path} is not a JSON object; " - "expected a top-level usb_pid hex string (e.g. \"0x000B\")." + 'expected a top-level usb_pid hex string (e.g. "0x000B").' ) raw = summary.get("usb_pid") @@ -357,13 +468,13 @@ def read_usb_pid(bit_path: str) -> int: except ValueError: raise ValueError( f"Bitstream summary {path} usb_pid {raw!r} is not a valid hex " - "string (expected e.g. \"0x000B\")" + 'string (expected e.g. "0x000B")' ) if not (0 < usb_pid <= 0xFFFF): raise ValueError( f"Bitstream summary {path} usb_pid {raw!r} ({usb_pid}) is out of " - "range; expected a hex string in 1..0xFFFF (e.g. \"0x000B\")" + 'range; expected a hex string in 1..0xFFFF (e.g. "0x000B")' ) return usb_pid @@ -371,7 +482,7 @@ def read_usb_pid(bit_path: str) -> int: def read_identifier(bit_path: str) -> str | None: """Return ``_`` from the bitstream's summary. - This is the custom bitstream's identity: the on-device file names, the + This is the custom bitstream's identity: the on-device manifest name, the manifest fragment's ``name``, the ``axon-gateware-*`` deb name, ``scifi-probe-updater update --name``, and the systemd instance all key on it, and deploying the same identifier again overrides the previous @@ -380,14 +491,8 @@ def read_identifier(bit_path: str) -> str | None: should stay within ``[A-Za-z0-9._-]`` — the identifier rides in ``scifi-probe-update-custom@:``. """ - path = summary_path_for(bit_path) - try: - with open(path, "r", encoding="utf-8") as fp: - summary = json.load(fp) - except (OSError, json.JSONDecodeError): - return None - - if not isinstance(summary, dict): + summary = _load_summary(summary_path_for(bit_path)) + if summary is None: return None target_profile = summary.get("target_profile") if not isinstance(target_profile, str) or not target_profile: @@ -407,14 +512,8 @@ def read_git_sha(bit_path: str) -> str | None: Best-effort: returns None when the summary or the field is missing/malformed. """ - path = summary_path_for(bit_path) - try: - with open(path, "r", encoding="utf-8") as fp: - summary = json.load(fp) - except (OSError, json.JSONDecodeError): - return None - - if not isinstance(summary, dict): + summary = _load_summary(summary_path_for(bit_path)) + if summary is None: return None project = summary.get("project") if not isinstance(project, dict): diff --git a/synapse/cli/peripherals.py b/synapse/cli/peripherals.py index ea36322..a1a8c37 100644 --- a/synapse/cli/peripherals.py +++ b/synapse/cli/peripherals.py @@ -295,9 +295,7 @@ def build_peripheral_so( # of profile doesn't depend on which one a given repo happens to take. # shlex.quote keeps a hostile profile name from breaking out of the # `bash -c` string; the value reaches us from --profile or the environment. - profile_flag = ( - f" -DAXON_TARGET_PROFILE={shlex.quote(profile)}" if profile else "" - ) + profile_flag = f" -DAXON_TARGET_PROFILE={shlex.quote(profile)}" if profile else "" build_cmd_str = ( "cd /home/workspace && " "if [ -f CMakePresets.json ]; then " @@ -456,9 +454,7 @@ def build_peripheral_deb( # 1. Stage the plugin .so at /usr/lib/scifi/plugins/.so if not os.path.exists(so_path): - console.print( - f"[bold red]Error:[/bold red] Plugin .so not found at {so_path}" - ) + console.print(f"[bold red]Error:[/bold red] Plugin .so not found at {so_path}") return False plugin_dst = os.path.join(staging_dir, "usr", "lib", "scifi", "plugins") os.makedirs(plugin_dst, exist_ok=True) @@ -505,9 +501,7 @@ def build_peripheral_deb( ) return False arch_suffix = detect_arch() - platform_opt = ( - "linux/arm64" if arch_suffix == "arm64" else "linux/amd64" - ) + platform_opt = "linux/arm64" if arch_suffix == "arm64" else "linux/amd64" console.print( f"[yellow]Extracting SDK runtime from Docker image [bold]{image_tag}[/bold]...[/yellow]" @@ -534,9 +528,7 @@ def build_peripheral_deb( return False sdk_files = [ - f - for f in os.listdir(sdk_dst) - if f.startswith("libscifi-peripheral-sdk.so") + f for f in os.listdir(sdk_dst) if f.startswith("libscifi-peripheral-sdk.so") ] if not sdk_files: console.print( @@ -635,9 +627,14 @@ def build_gateware_deb( The deb package is named ``axon-gateware-`` where the identifier is ``bitstream_name`` (falling back to the plugin name). On-device files land at: - /opt/scifi/bitstreams/custom/.bit + /opt/scifi/bitstreams/custom/artifacts// /opt/scifi/bitstreams/custom/.manifest.json + The .bit keeps its build basename because scifi-probe-updater's Lattice + DFU path reads the image's ``v`` version from it and refuses an + unversioned image; the per-identifier directory keeps images of + different identifiers from colliding. + The fragment carries ``{"name", "usb_pid", "artifact"}`` (plus ``"git_hash"`` when provided) with ``artifact`` relative to /opt/scifi/bitstreams (canonical-manifest convention). Redeploying the @@ -661,13 +658,15 @@ def build_gateware_deb( # /tmp eventually cleans itself. custom_dir = os.path.join(staging_dir, "opt", "scifi", "bitstreams", "custom") - os.makedirs(custom_dir, exist_ok=True) - shutil.copy2(bit_path, os.path.join(custom_dir, f"{identifier}.bit")) + artifact = f"artifacts/{identifier}/{os.path.basename(bit_path)}" + bit_dst = os.path.join(custom_dir, artifact) + os.makedirs(os.path.dirname(bit_dst), exist_ok=True) + shutil.copy2(bit_path, bit_dst) fragment: dict = { "name": identifier, "usb_pid": usb_pid, - "artifact": f"custom/{identifier}.bit", + "artifact": f"custom/{artifact}", } if git_hash: fragment["git_hash"] = git_hash @@ -788,7 +787,7 @@ def _run_gateware_half(peripheral_dir: str, project_subdir: str) -> Optional[str except LicenseUnsetError as exc: console.print(f"[bold red]Error:[/bold red] {exc}") return None - except (subprocess.CalledProcessError, FileNotFoundError) as exc: + except (subprocess.CalledProcessError, FileNotFoundError, ValueError) as exc: console.print(f"[bold red]Error:[/bold red] Gateware build failed: {exc}") return None @@ -840,7 +839,9 @@ def _build_debs( else None ) if resolved_profile: - console.print(f"[bold]Target profile:[/bold] [yellow]{resolved_profile}[/yellow]") + console.print( + f"[bold]Target profile:[/bold] [yellow]{resolved_profile}[/yellow]" + ) if do_gateware and clean: try: @@ -892,7 +893,9 @@ def _build_debs( git_hash=gateware.read_git_sha(bit_path), ): return None - deb = find_deb_package(dist_dir, f"{_gateware_package_name(identifier)}_{version}") + deb = find_deb_package( + dist_dir, f"{_gateware_package_name(identifier)}_{version}" + ) if deb is None: return None debs.append(deb) @@ -905,17 +908,17 @@ def _build_debs( # --------------------------------------------------------------------------- -def build_cmd(args) -> None: +def build_cmd(args) -> Optional[bool]: """Handle ``synapsectl peripherals build``.""" if not ensure_docker(): - return + return False peripheral_dir = os.path.abspath(args.peripheral_dir) manifest = validate_manifest(os.path.join(peripheral_dir, "manifest.json")) if not manifest: - return + return False console.print( f"[bold]Building peripheral plugin:[/bold] [yellow]{manifest['name']}[/yellow]" @@ -929,7 +932,7 @@ def build_cmd(args) -> None: profile=getattr(args, "profile", None), ) if debs is None: - return + return False package_lines = "\n".join(f"Package: [bold]{d}[/bold]" for d in debs) console.print( @@ -952,7 +955,7 @@ def build_cmd(args) -> None: # --------------------------------------------------------------------------- -def deploy_cmd(args) -> None: +def deploy_cmd(args) -> Optional[bool]: """Handle ``synapsectl peripherals deploy`` — gRPC-stream a .deb to the device. Reuses apps' DeployApp gRPC method; the scifi-server side accepts plugin @@ -974,18 +977,18 @@ def deploy_cmd(args) -> None: console.print( f"[bold red]Error:[/bold red] Provided package not found: {deb_packages[0]}" ) - return + return False console.print( f"[bold]Deploying pre-built plugin:[/bold] [yellow]{os.path.basename(deb_packages[0])}[/yellow]" ) else: if not ensure_docker(): - return + return False peripheral_dir = os.path.abspath(args.peripheral_dir) manifest = validate_manifest(os.path.join(peripheral_dir, "manifest.json")) if not manifest: - return + return False console.print( f"[bold]Deploying peripheral plugin:[/bold] [yellow]{manifest['name']}[/yellow]" @@ -995,7 +998,7 @@ def deploy_cmd(args) -> None: peripheral_dir, manifest, half, profile=getattr(args, "profile", None) ) if debs is None: - return + return False deb_packages = debs if not args.uri: @@ -1012,7 +1015,7 @@ def deploy_cmd(args) -> None: f"[bold red]Error:[/bold red] Deploy failed for {deb}; " "skipping any remaining packages." ) - return + return False # --------------------------------------------------------------------------- diff --git a/synapse/tests/cli/test_custom_gateware_packaging.py b/synapse/tests/cli/test_custom_gateware_packaging.py index 3dec7e5..53d6b7f 100644 --- a/synapse/tests/cli/test_custom_gateware_packaging.py +++ b/synapse/tests/cli/test_custom_gateware_packaging.py @@ -5,7 +5,9 @@ * ``gateware.summary_path_for`` / ``gateware.read_usb_pid`` * ``build.find_deb_package`` package_name filtering - * ``peripherals.build_gateware_deb`` staging layout + fpm invocation + * ``peripherals.build_gateware_deb`` staging layout + fpm invocation: the + image keeps its build file name under + ``custom/artifacts//`` and the fragment points at it The two-deb build/deploy command flows live in test_half_selectors.py. """ @@ -15,9 +17,13 @@ import importlib import json import os +from pathlib import Path import pytest +from synapse.tests.cli.conftest import fake_fpm_run +from synapse.tests.cli.test_gateware_runner import _new_ns, _summary, _write + @pytest.fixture() def gateware(): @@ -244,7 +250,9 @@ def test_read_identifier_missing_target_profile_returns_none(gateware, tmp_path) def test_read_identifier_missing_project_name_returns_none(gateware, tmp_path): bit = tmp_path / "sdk_x.bit" bit.write_text("bit") - _write_summary(bit, {"usb_pid": "0x000B", "target_profile": "via-devkit", "project": {}}) + _write_summary( + bit, {"usb_pid": "0x000B", "target_profile": "via-devkit", "project": {}} + ) assert gateware.read_identifier(str(bit)) is None @@ -291,7 +299,9 @@ def test_read_git_sha_missing_git_sha_returns_none(gateware, tmp_path): def test_read_git_sha_empty_string_returns_none(gateware, tmp_path): bit = tmp_path / "sdk_x.bit" bit.write_text("bit") - _write_summary(bit, {"usb_pid": "0x000B", "project": {"name": "gateware", "git_sha": ""}}) + _write_summary( + bit, {"usb_pid": "0x000B", "project": {"name": "gateware", "git_sha": ""}} + ) assert gateware.read_git_sha(str(bit)) is None @@ -336,10 +346,6 @@ def test_find_deb_package_version_anchored_prefix_skips_stale(buildmod, tmp_path # peripherals.build_gateware_deb # --------------------------------------------------------------------------- -# synapse/tests/cli/ is a package (has __init__.py), so import the helper -# package-qualified rather than relying on pytest's rootdir sys.path insert. -from synapse.tests.cli.conftest import fake_fpm_run - def _spy_mkdtemp(peripherals, monkeypatch, holder: list): real_mkdtemp = peripherals.tempfile.mkdtemp @@ -352,49 +358,81 @@ def spy(*args, **kwargs): monkeypatch.setattr(peripherals.tempfile, "mkdtemp", spy) -def test_build_gateware_deb_stages_bit_fragment_and_depends( - peripherals, tmp_path, monkeypatch -): - """With bitstream_name and git_hash: files/fragment keyed on identifier.""" - pd = tmp_path / "plugin" +BITSTREAMS = Path("opt", "scifi", "bitstreams") +VERSIONED_BIT = "sdk_nerv512u-devkit_gateware_v0.3.7_ab12cd34_11052026_1530.bit" + + +def _build(peripherals, monkeypatch, tmp_path, bit_name, *, payload, **kwargs): + """Run build_gateware_deb on a fresh plugin dir with fpm stubbed out. + + Returns (ok, staging root, recorded subprocess argv list). + """ + pd = tmp_path / f"plugin-{len(list(tmp_path.iterdir()))}" pd.mkdir() - bit = tmp_path / "sdk_x.bit" - bit.write_text("BITSTREAM") - manifest = {"name": "scifi-my-chip", "version": "0.2.0"} + bit = pd / "build" / bit_name + bit.parent.mkdir() + bit.write_bytes(payload) staging: list = [] _spy_mkdtemp(peripherals, monkeypatch, staging) calls: list = [] - dist_dir = os.path.join(str(pd), "dist") - monkeypatch.setattr(peripherals.subprocess, "run", fake_fpm_run(dist_dir, calls)) - + monkeypatch.setattr( + peripherals.subprocess, "run", fake_fpm_run(str(pd / "dist"), calls) + ) ok = peripherals.build_gateware_deb( - str(pd), manifest, bit_path=str(bit), usb_pid=4, - bitstream_name="via-devkit_gateware", git_hash="e6890a3", - version="0.2.0" + str(pd), + {"name": "scifi-my-chip", "version": "0.2.0"}, + bit_path=str(bit), + usb_pid=4, + version="0.2.0", + **kwargs, ) - assert ok is True assert len(staging) == 1 + return ok, Path(staging[0]), calls - bit_dst = os.path.join( - staging[0], "opt", "scifi", "bitstreams", "custom", "via-devkit_gateware.bit" - ) - frag_dst = os.path.join( - staging[0], "opt", "scifi", "bitstreams", "custom", - "via-devkit_gateware.manifest.json", + +def _staged_bits(root: Path) -> list: + return sorted((root / BITSTREAMS / "custom").rglob("*.bit")) + + +def _fragment(root: Path, identifier: str) -> dict: + path = root / BITSTREAMS / "custom" / f"{identifier}.manifest.json" + return json.loads(path.read_text(encoding="utf-8")) + + +def _fpm_name(calls: list) -> str: + fpm_call = next(c for c in calls if "fpm" in c) + return fpm_call[fpm_call.index("-n") + 1] + + +def test_build_gateware_deb_stages_bit_fragment_and_depends( + peripherals, tmp_path, monkeypatch +): + """With bitstream_name and git_hash: image under artifacts//.""" + payload = b"\x96\x01BITSTREAM\x00" + ok, root, calls = _build( + peripherals, + monkeypatch, + tmp_path, + "sdk_x.bit", + payload=payload, + bitstream_name="via-devkit_gateware", + git_hash="e6890a3", ) - assert os.path.exists(bit_dst), "bitstream staged under custom/ as .bit" - with open(frag_dst, "r", encoding="utf-8") as fh: - frag = json.load(fh) - assert frag == { + assert ok is True + + image = root / BITSTREAMS / "custom/artifacts/via-devkit_gateware/sdk_x.bit" + assert _staged_bits(root) == [image] + assert image.read_bytes() == payload + assert _fragment(root, "via-devkit_gateware") == { "name": "via-devkit_gateware", "usb_pid": 4, - "artifact": "custom/via-devkit_gateware.bit", + "artifact": "custom/artifacts/via-devkit_gateware/sdk_x.bit", "git_hash": "e6890a3", } fpm_call = next(c for c in calls if "fpm" in c) - assert fpm_call[fpm_call.index("-n") + 1] == "axon-gateware-via-devkit-gateware" + assert _fpm_name(calls) == "axon-gateware-via-devkit-gateware" assert fpm_call[fpm_call.index("--depends") + 1] == "axonprobe-bitstreams" # fpm input must be "opt" (not "."): postinstall.sh must NOT ship in the # payload, or the driver and gateware debs would dpkg-conflict on @@ -405,39 +443,100 @@ def test_build_gateware_deb_stages_bit_fragment_and_depends( def test_build_gateware_deb_omit_bitstream_name_falls_back_to_plugin_name( peripherals, tmp_path, monkeypatch ): - """Omitting bitstream_name: files/fragment keyed on plugin name, no git_hash key.""" - pd = tmp_path / "plugin" - pd.mkdir() - bit = tmp_path / "sdk_x.bit" - bit.write_text("BITSTREAM") - manifest = {"name": "scifi-my-chip", "version": "0.2.0"} + """Omitting bitstream_name: keyed on plugin name, no git_hash key.""" + ok, root, calls = _build( + peripherals, monkeypatch, tmp_path, "sdk_x.bit", payload=b"BITSTREAM" + ) + assert ok is True - staging: list = [] - _spy_mkdtemp(peripherals, monkeypatch, staging) - calls: list = [] - dist_dir = os.path.join(str(pd), "dist") - monkeypatch.setattr(peripherals.subprocess, "run", fake_fpm_run(dist_dir, calls)) + image = root / BITSTREAMS / "custom/artifacts/scifi-my-chip/sdk_x.bit" + assert _staged_bits(root) == [image] + assert _fragment(root, "scifi-my-chip") == { + "name": "scifi-my-chip", + "usb_pid": 4, + "artifact": "custom/artifacts/scifi-my-chip/sdk_x.bit", + } + assert _fpm_name(calls) == "axon-gateware-scifi-my-chip" - ok = peripherals.build_gateware_deb( - str(pd), manifest, bit_path=str(bit), usb_pid=4, version="0.2.0" + +@pytest.mark.parametrize( + "bit_name", + [VERSIONED_BIT, "sdk_nerv512u-devkit_gateware_extracted.bit"], + ids=["versioned", "extracted"], +) +def test_build_gateware_deb_keeps_build_file_name( + peripherals, tmp_path, monkeypatch, bit_name +): + """The build's own file name is kept verbatim, never renamed to .bit.""" + payload = bytes(range(256)) + ok, root, calls = _build( + peripherals, + monkeypatch, + tmp_path, + bit_name, + payload=payload, + bitstream_name="nerv512u-devkit_gateware", + git_hash="ab12cd34", ) assert ok is True - bit_dst = os.path.join( - staging[0], "opt", "scifi", "bitstreams", "custom", "scifi-my-chip.bit" - ) - frag_dst = os.path.join( - staging[0], "opt", "scifi", "bitstreams", "custom", - "scifi-my-chip.manifest.json", + custom = root / BITSTREAMS / "custom" + image = custom / "artifacts" / "nerv512u-devkit_gateware" / bit_name + assert _staged_bits(root) == [image] + assert image.read_bytes() == payload + assert not (custom / "nerv512u-devkit_gateware.bit").exists() + assert _fragment(root, "nerv512u-devkit_gateware") == { + "name": "nerv512u-devkit_gateware", + "usb_pid": 4, + "artifact": f"custom/artifacts/nerv512u-devkit_gateware/{bit_name}", + "git_hash": "ab12cd34", + } + assert _fpm_name(calls) == "axon-gateware-nerv512u-devkit-gateware" + + +@pytest.mark.parametrize("bitstream_name", ["via-devkit_gateware", None]) +def test_build_gateware_deb_fragment_artifact_resolves_to_staged_image( + peripherals, tmp_path, monkeypatch, bitstream_name +): + """Fragment ``artifact`` (relative to opt/scifi/bitstreams) is the image.""" + payload = b"\x69\x80IMAGE" + ok, root, _ = _build( + peripherals, + monkeypatch, + tmp_path, + VERSIONED_BIT, + payload=payload, + bitstream_name=bitstream_name, ) - assert os.path.exists(bit_dst), "fallback: bit staged as .bit" - with open(frag_dst, "r", encoding="utf-8") as fh: - frag = json.load(fh) - # 3-key fragment, NO git_hash key - assert frag == {"name": "scifi-my-chip", "usb_pid": 4, "artifact": "custom/scifi-my-chip.bit"} + assert ok is True - fpm_call = next(c for c in calls if "fpm" in c) - assert fpm_call[fpm_call.index("-n") + 1] == "axon-gateware-scifi-my-chip" + identifier = bitstream_name or "scifi-my-chip" + resolved = root / BITSTREAMS / _fragment(root, identifier)["artifact"] + assert _staged_bits(root) == [resolved] + assert resolved.read_bytes() == payload + + +def test_build_gateware_deb_distinct_identifiers_never_share_a_path( + peripherals, tmp_path, monkeypatch +): + """Same build file name, two identifiers: no on-device path collides.""" + payloads = {"alpha_gateware": b"ALPHA", "beta_gateware": b"BETA"} + staged = {} + for identifier, payload in payloads.items(): + ok, root, _ = _build( + peripherals, + monkeypatch, + tmp_path, + VERSIONED_BIT, + payload=payload, + bitstream_name=identifier, + ) + assert ok is True + staged[identifier] = { + p.relative_to(root) for p in (root / "opt").rglob("*") if p.is_file() + } + + assert staged["alpha_gateware"].isdisjoint(staged["beta_gateware"]) def test_build_gateware_deb_missing_bit_errors(peripherals, tmp_path, capsys): @@ -448,3 +547,71 @@ def test_build_gateware_deb_missing_bit_errors(peripherals, tmp_path, capsys): ) assert ok is False assert "not found" in capsys.readouterr().out.lower() + + +def test_selected_native_bytes_and_summary_become_custom_package( + gateware, peripherals, tmp_path, monkeypatch +): + pd = tmp_path / "plugin" + bitstreams = pd / "src" / "gateware" / "build" / "bitstreams" + native = bitstreams / "sdk_profile-alpha_gateware.bit" + native_payload = b"\x96\x01\x80\x7f\x00\xa5" + + def emit_sdk_outputs(): + _write(native, native_payload, _new_ns(1)) + _summary( + native, + _new_ns(1), + schema_version=1, + usb_pid="0x000B", + target_profile="profile-alpha", + project={"name": "gateware", "git_sha": "123abcd"}, + ) + extracted = bitstreams / "sdk_profile-alpha_gateware_extracted.bit" + _write(extracted, b"\x69\x80\x01\xfe\x00\xa5", _new_ns(2)) + _summary( + extracted, + _new_ns(2), + schema_version=1, + usb_pid="0x0004", + target_profile="wrong-profile", + project={"name": "decoy", "git_sha": "badbeef"}, + ) + + staging = [] + _spy_mkdtemp(peripherals, monkeypatch, staging) + fpm_run = fake_fpm_run(str(pd / "dist"), []) + + def fake_run(argv, *args, **kwargs): + # The SDK build writes its images; every other call goes to the fpm stub. + if any("axon-peripheral-sdk build" in str(element) for element in argv): + emit_sdk_outputs() + return fpm_run(argv, *args, **kwargs) + + monkeypatch.setattr(peripherals.subprocess, "run", fake_run) + + selected = gateware.run_gateware_build( + str(pd), "fake-gw:latest", env={"LM_LICENSE_FILE": "7788@licenseserver"} + ) + ok = peripherals.build_gateware_deb( + str(pd), + {"name": "scifi-plugin", "version": "0.2.0"}, + bit_path=selected, + usb_pid=gateware.read_usb_pid(selected), + bitstream_name=gateware.read_identifier(selected), + git_hash=gateware.read_git_sha(selected), + version="0.2.0", + ) + + assert ok is True + root = Path(staging[0]) + bit_name = Path(selected).name + image = root / BITSTREAMS / "custom/artifacts/profile-alpha_gateware" / bit_name + assert image.read_bytes() == native_payload + assert _staged_bits(root) == [image] + assert _fragment(root, "profile-alpha_gateware") == { + "name": "profile-alpha_gateware", + "usb_pid": 11, + "artifact": f"custom/artifacts/profile-alpha_gateware/{bit_name}", + "git_hash": "123abcd", + } diff --git a/synapse/tests/cli/test_deploy_timeout.py b/synapse/tests/cli/test_deploy_timeout.py new file mode 100644 index 0000000..11aace5 --- /dev/null +++ b/synapse/tests/cli/test_deploy_timeout.py @@ -0,0 +1,67 @@ +"""``deploy.deploy_package`` forwards its timeout as the DeployApp deadline.""" + +from __future__ import annotations + +import importlib +from unittest import mock + +import pytest + +from synapse.api.app_pb2 import AppDeployResponse +from synapse.api.status_pb2 import StatusCode + +_NO_DEADLINE = object() + + +class _RecordingRpc: + def __init__(self): + self.deploy_calls: list = [] + + def DeployApp(self, request_iterator, **kwargs): + # Drain the stream like a real server so the whole package is sent. + list(request_iterator) + self.deploy_calls.append(kwargs) + return iter([AppDeployResponse(status=StatusCode.kOk, message="ok")]) + + +@pytest.fixture() +def deploy(): + return importlib.import_module("synapse.cli.deploy") + + +@pytest.fixture() +def rpc(deploy, monkeypatch): + rpc = _RecordingRpc() + device = mock.MagicMock() + device.rpc = rpc + monkeypatch.setattr(deploy.syn, "Device", lambda *a, **k: device) + return rpc + + +@pytest.fixture() +def deb(tmp_path): + path = tmp_path / "axon-gateware-x_0.1.0_arm64.deb" + path.write_bytes(b"\x00DEB" * 1024) + return str(path) + + +def _deadline(rpc): + assert len(rpc.deploy_calls) == 1, "one DeployApp stream per package" + return rpc.deploy_calls[0].get("timeout", _NO_DEADLINE) + + +@pytest.mark.parametrize("seconds", [1, 30.5, 900]) +def test_deploy_package_passes_timeout_to_deploy_app(deploy, rpc, deb, seconds): + deploy.deploy_package("10.0.0.2", deb, timeout=seconds) + assert _deadline(rpc) == seconds + + +def test_deploy_package_without_timeout_sets_no_deadline(deploy, rpc, deb): + """Positional two-argument calls keep working and impose no deadline.""" + deploy.deploy_package("10.0.0.2", deb) + assert _deadline(rpc) in (None, _NO_DEADLINE) + + +def test_deploy_package_explicit_none_timeout_sets_no_deadline(deploy, rpc, deb): + deploy.deploy_package("10.0.0.2", deb, timeout=None) + assert _deadline(rpc) in (None, _NO_DEADLINE) diff --git a/synapse/tests/cli/test_gateware_profiles.py b/synapse/tests/cli/test_gateware_profiles.py index dfc2b64..805e835 100644 --- a/synapse/tests/cli/test_gateware_profiles.py +++ b/synapse/tests/cli/test_gateware_profiles.py @@ -19,9 +19,18 @@ from __future__ import annotations import importlib +import subprocess +from pathlib import Path import pytest +from synapse.tests.cli.test_gateware_runner import ( + _new_ns, + _project_args, + _summary, + _write, +) + @pytest.fixture() def gw(): @@ -163,38 +172,68 @@ def test_bare_gateware_dir_without_manifest_raises(gw, tmp_path): # --------------------------------------------------------------------------- -def test_build_command_and_bit_glob_follow_the_project_subdir( +def test_build_targets_each_profile_project_and_selects_its_native_image( gw, tmp_path, monkeypatch ): - """The SDK ``--project`` argument and the bitstream glob must agree; a - mismatch would build one profile and package the other's stale .bit.""" - project = "src/gateware/via-devkit" - bitstreams = tmp_path / project / "build" / "bitstreams" - bitstreams.mkdir(parents=True) - expected_bit = bitstreams / "sdk_via-devkit_gateware_extracted.bit" - expected_bit.write_bytes(b"\x00") - - # A decoy under the *other* profile proves the glob is scoped. - other = tmp_path / "src/gateware/nerv512u-devkit/build/bitstreams" - other.mkdir(parents=True) - (other / "sdk_nerv512u-devkit_gateware_extracted.bit").write_bytes(b"\x00") - - recorded = {} + """The SDK stub writes only into the project the command names, so a build + that ignores the selected profile finds nothing (or the wrong image).""" + profiles = { + "profile-alpha": {"usb_pid": "0x0004", "git_sha": "123abcd"}, + "profile-beta": {"usb_pid": "0x000B", "git_sha": "456def0"}, + } + _make_repo(tmp_path, profiles=profiles) + expected = {} + + def emit(project): + profile = Path(project).name + metadata = profiles[profile] + bitstreams = tmp_path / project / "build" / "bitstreams" + native = bitstreams / f"sdk_{profile}_gateware.bit" + payload = bytes([len(expected) + 1, 0x96, 0x80, 0x7F]) + _write(native, payload, _new_ns(1)) + _summary( + native, + _new_ns(1), + schema_version=1, + target_profile=profile, + usb_pid=metadata["usb_pid"], + project={"name": "gateware", "git_sha": metadata["git_sha"]}, + ) + extracted = bitstreams / f"sdk_{profile}_gateware_extracted.bit" + _write(extracted, b"wrong extracted bytes", _new_ns(9)) + _summary( + extracted, + _new_ns(9), + usb_pid="0xFFFF", + target_profile="wrong-profile", + project={"name": "decoy", "git_sha": "badbeef"}, + ) + expected[profile] = (native, payload) - def fake_run(argv, **kwargs): - recorded["argv"] = argv - import subprocess as sp + requested = [] - return sp.CompletedProcess(argv, 0) + def fake_run(argv, *args, **kwargs): + projects = _project_args(argv) + requested.append(projects) + if len(projects) == 1: + emit(projects[0]) + return subprocess.CompletedProcess(argv, 0) monkeypatch.setattr(gw.subprocess, "run", fake_run) - got = gw.run_gateware_build( - str(tmp_path), - "fake-gw:latest", - env={"LM_LICENSE_FILE": "7788@licenseserver"}, - project_subdir=project, - ) - - assert got == str(expected_bit) - assert f"axon-peripheral-sdk build --project {project}" in recorded["argv"][-1] + for profile, metadata in profiles.items(): + project = gw.resolve_gateware_project(str(tmp_path), profile, {}) + selected = gw.run_gateware_build( + str(tmp_path), + "fake-gw:latest", + env={"LM_LICENSE_FILE": "7788@licenseserver"}, + project_subdir=project, + ) + assert requested[-1] == [f"src/gateware/{profile}"] + native, payload = expected[profile] + assert Path(selected) == native + assert Path(selected).read_bytes() == payload + assert gw.summary_path_for(selected) == str(native.with_suffix(".summary.json")) + assert gw.read_usb_pid(selected) == int(metadata["usb_pid"], 16) + assert gw.read_identifier(selected) == f"{profile}_gateware" + assert gw.read_git_sha(selected) == metadata["git_sha"] diff --git a/synapse/tests/cli/test_gateware_runner.py b/synapse/tests/cli/test_gateware_runner.py index df08af7..7bfdbff 100644 --- a/synapse/tests/cli/test_gateware_runner.py +++ b/synapse/tests/cli/test_gateware_runner.py @@ -1,53 +1,64 @@ -"""AC-10 / AC-6: unit tests for run_gateware_build(). - -The runner lives in `synapse.cli.gateware` per the plan's File Structure -section. Signature: - - run_gateware_build(peripheral_dir: str, image_tag: str, - env: Mapping[str, str] = os.environ) -> str - -Behavior (per AC-6): - 1. Calls build_license_docker_args(env); LicenseUnsetError propagates. - 2. Issues `docker run --rm --user dev -v :/home/workspace - -w /home/workspace /bin/bash -lc - 'axon-peripheral-sdk build --project src/gateware'`. - 3. Non-zero exit -> raises subprocess.CalledProcessError. - 4. After success, globs - /src/gateware/build/bitstreams/sdk_*_extracted.bit - and returns the newest by mtime (warns on multi-match). The *extracted* - variant is what gets flashed; the raw sdk_*.bit is ignored. - 5. Empty glob -> FileNotFoundError with message mentioning - "sdk_*_extracted.bit". - -Sub-phase 4.4 (Tester): the xfail marker is removed — these tests now run -as live AC-6 acceptance gates and must fail until the Implementer lands -``run_gateware_build``. +"""Public SDK runner contract: return the image this build produced. + +Only files the build wrote or changed are candidates. A schema-2 summary names +the image outright; otherwise the newest native ``sdk_*.bit`` wins, falling +back to the newest ``sdk_*_extracted.bit``. """ from __future__ import annotations import importlib +import json import os +import shlex import subprocess import time +from pathlib import Path import pytest +IMAGE_TAG = "myplugin-gateware:latest-arm64" + + +# Earlier builds' files sit an hour in the past; this build's land whole +# seconds after "now", so ordering never depends on the wall clock moving. +def _stale_ns() -> int: + return time.time_ns() - 3600 * 10**9 + + +def _new_ns(order: int) -> int: + return time.time_ns() + order * 10**9 + + +def _write(path: Path, data: bytes, mtime_ns: int) -> Path: + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(data) + os.utime(path, ns=(mtime_ns, mtime_ns)) + return path + + +def _summary(bit_path: Path, mtime_ns: int, **fields) -> Path: + """Write *bit_path*'s same-stem ``.summary.json``.""" + path = bit_path.with_name(bit_path.name.removesuffix(".bit") + ".summary.json") + return _write(path, json.dumps(fields).encode(), mtime_ns) + + +def _project_args(argv) -> list[str]: + """Every ``--project`` value in *argv*, including inside ``bash -lc``.""" + values = [] + for element in argv: + tokens = shlex.split(str(element)) + values += [b for a, b in zip(tokens, tokens[1:]) if a == "--project"] + return values + @pytest.fixture() def gateware(): - """Lazy import — avoids module-collection failure before AC-5/AC-6 land.""" return importlib.import_module("synapse.cli.gateware") -# --------------------------------------------------------------------------- -# Fixtures -# --------------------------------------------------------------------------- - - @pytest.fixture() def peripheral_dir(tmp_path): - """Create a minimal peripheral dir with src/gateware/ + a license file.""" pd = tmp_path / "myplugin" (pd / "src" / "gateware").mkdir(parents=True) license_file = tmp_path / "license.dat" @@ -55,240 +66,415 @@ def peripheral_dir(tmp_path): return pd, license_file -def _bitstreams_dir(peripheral_dir): - bs = os.path.join(str(peripheral_dir), "src", "gateware", "build", "bitstreams") - os.makedirs(bs, exist_ok=True) - return bs +@pytest.fixture() +def bitstreams(peripheral_dir): + path = peripheral_dir[0] / "src" / "gateware" / "build" / "bitstreams" + path.mkdir(parents=True) + return path -# --------------------------------------------------------------------------- -# AC-10 case 10 / 13: docker-run argv shape -# --------------------------------------------------------------------------- +@pytest.fixture() +def build(gateware, peripheral_dir, bitstreams, monkeypatch): + """Run the build once; *emit* plays the SDK, writing into ``bitstreams``.""" + pd, license_file = peripheral_dir + + def run(emit) -> Path: + before = len(run.argvs) + def fake_run(argv, *args, **kwargs): + assert len(run.argvs) == before, "build invoked the SDK twice" + run.argvs.append(argv) + emit(bitstreams) + return subprocess.CompletedProcess(argv, 0, b"", b"") + + monkeypatch.setattr(gateware.subprocess, "run", fake_run) + selected = gateware.run_gateware_build( + str(pd), IMAGE_TAG, env={"LM_LICENSE_FILE": str(license_file)} + ) + assert len(run.argvs) == before + 1 + assert Path(selected).is_absolute() + return Path(selected) -def test_runner_builds_docker_run_argv_with_project_flag( + run.argvs = [] + return run + + +def test_runner_raises_when_license_unset_and_does_not_invoke_docker( gateware, peripheral_dir, monkeypatch ): - """Case 10/13: captured docker-run argv has the correct shape and ends - with the exact axon-peripheral-sdk invocation (`--project src/gateware`; - the SDK no longer accepts `--pdc`/`--impl`). - """ - pd, license_file = peripheral_dir - recorded: list[list[str]] = [] + pd, _ = peripheral_dir + called = [] def fake_run(argv, *args, **kwargs): - recorded.append(list(argv)) - # Drop a fake extracted .bit so the post-run glob succeeds. - bs = _bitstreams_dir(pd) - bit = os.path.join(bs, "sdk_topbuild_extracted.bit") - with open(bit, "w") as fp: - fp.write("bitstream") + called.append(argv) return subprocess.CompletedProcess(argv, 0, b"", b"") monkeypatch.setattr(gateware.subprocess, "run", fake_run) - result = gateware.run_gateware_build( - str(pd), - "myplugin-gateware:latest-arm64", - env={"LM_LICENSE_FILE": str(license_file)}, - ) + with pytest.raises(gateware.LicenseUnsetError): + gateware.run_gateware_build(str(pd), IMAGE_TAG, env={}) - assert len(recorded) == 1, "runner should issue exactly one docker run" - argv = recorded[0] + assert called == [] - # Sanity: docker run --rm - assert argv[0:3] == ["docker", "run", "--rm"] - # The image tag is the second-to-last block before the entrypoint. - assert "myplugin-gateware:latest-arm64" in argv +def test_runner_passes_default_project_to_sdk(build): + build(lambda bs: _write(bs / "sdk_gateware.bit", b"native", _new_ns(1))) - # Workdir is /home/workspace (case 13) - assert "-w" in argv - assert argv[argv.index("-w") + 1] == "/home/workspace" + assert [_project_args(argv) for argv in build.argvs] == [["src/gateware"]] - # Bind-mount the peripheral_dir abspath to /home/workspace. - abs_pd = os.path.abspath(str(pd)) - assert "-v" in argv - # There may be multiple -v (license + workspace); check that the - # workspace bind-mount is present. - v_indices = [i for i, tok in enumerate(argv) if tok == "-v"] - bind_targets = {argv[i + 1] for i in v_indices} - assert f"{abs_pd}:/home/workspace" in bind_targets - # The SDK command is the final shell -lc payload. - assert "/bin/bash" in argv - bash_idx = argv.index("/bin/bash") - assert argv[bash_idx + 1] == "-lc" - sdk_cmd = argv[bash_idx + 2] - assert sdk_cmd == "axon-peripheral-sdk build --project src/gateware" +# --------------------------------------------------------------------------- +# Legacy rule (summary schema_version missing or 1) +# --------------------------------------------------------------------------- - # And the returned path is the extracted .bit we dropped. - assert result.endswith("sdk_topbuild_extracted.bit") +legacy_schema = pytest.mark.parametrize( + "schema_field", + [{}, {"schema_version": 1}], + ids=["schema_version_missing", "schema_version_1"], +) -# --------------------------------------------------------------------------- -# AC-10 case 14: LM_LICENSE_FILE forwarded -# --------------------------------------------------------------------------- +def test_runner_returns_native_only_output(build, bitstreams): + selected = build( + lambda bs: _write(bs / "sdk_gateware.bit", b"\x96\x01\x80\x7f", _new_ns(1)) + ) + assert selected == bitstreams / "sdk_gateware.bit" + assert selected.read_bytes() == b"\x96\x01\x80\x7f" -def test_runner_forwards_floating_license_arg(gateware, peripheral_dir, monkeypatch): - """Case 14: port@host floating license -> -e LM_LICENSE_FILE= in argv.""" - pd, _ = peripheral_dir - recorded: list[list[str]] = [] - def fake_run(argv, *args, **kwargs): - recorded.append(list(argv)) - bs = _bitstreams_dir(pd) - with open(os.path.join(bs, "sdk_topbuild_extracted.bit"), "w") as fp: - fp.write("x") - return subprocess.CompletedProcess(argv, 0, b"", b"") +def test_runner_prefers_native_over_newer_extracted(build, bitstreams): + def emit(bs): + _write(bs / "sdk_gateware.bit", b"native", _new_ns(1)) + _write(bs / "sdk_gateware_extracted.bit", b"extracted", _new_ns(3)) - monkeypatch.setattr(gateware.subprocess, "run", fake_run) + selected = build(emit) + + assert selected == bitstreams / "sdk_gateware.bit" + assert selected.read_bytes() == b"native" + + +def test_runner_returns_newest_native_candidate(build, bitstreams): + def emit(bs): + _write(bs / "sdk_old.bit", b"old native", _new_ns(1)) + _write(bs / "sdk_new.bit", b"new native", _new_ns(2)) + _write(bs / "sdk_new_extracted.bit", b"extracted decoy", _new_ns(3)) + + selected = build(emit) - gateware.run_gateware_build( - str(pd), - "myplugin-gateware:latest-arm64", - env={"LM_LICENSE_FILE": "27000@licenseserver"}, + assert selected == bitstreams / "sdk_new.bit" + assert selected.read_bytes() == b"new native" + + +def test_runner_falls_back_to_extracted_when_no_native_image(build, bitstreams): + selected = build( + lambda bs: _write(bs / "sdk_gateware_extracted.bit", b"extracted", _new_ns(1)) ) - argv = recorded[0] - e_indices = [i for i, tok in enumerate(argv) if tok == "-e"] - e_pairs = [argv[i + 1] for i in e_indices] - assert "LM_LICENSE_FILE=27000@licenseserver" in e_pairs + assert selected == bitstreams / "sdk_gateware_extracted.bit" + assert selected.read_bytes() == b"extracted" - # And no bind-mount for the license (port@host mode is mount-free). - v_indices = [i for i, tok in enumerate(argv) if tok == "-v"] - bind_targets = [argv[i + 1] for i in v_indices] - assert not any("/opt/lattice/license.dat" in t for t in bind_targets) +def test_runner_falls_back_to_newest_extracted_candidate(build, bitstreams): + def emit(bs): + _write(bs / "sdk_new_extracted.bit", b"new extracted", _new_ns(2)) + _write(bs / "sdk_old_extracted.bit", b"old extracted", _new_ns(1)) -# --------------------------------------------------------------------------- -# AC-10 case 15: unset env -> error, no subprocess.run -# --------------------------------------------------------------------------- + selected = build(emit) + assert selected == bitstreams / "sdk_new_extracted.bit" + assert selected.read_bytes() == b"new extracted" -def test_runner_raises_when_license_unset_and_does_not_invoke_docker( - gateware, peripheral_dir, monkeypatch -): - """Case 15: LM_LICENSE_FILE unset -> LicenseUnsetError, subprocess.run unused.""" - pd, _ = peripheral_dir - called = [] - def fake_run(argv, *args, **kwargs): # pragma: no cover - must NOT be called - called.append(argv) - return subprocess.CompletedProcess(argv, 0, b"", b"") +def test_runner_ignores_newer_non_sdk_and_non_bit_files(build, bitstreams): + def emit(bs): + _write(bs / "sdk_gateware.bit", b"native", _new_ns(1)) + _write(bs / "gateware.bit", b"decoy", _new_ns(3)) + _write(bs / "sdk_gateware.mcs", b"decoy", _new_ns(3)) - monkeypatch.setattr(gateware.subprocess, "run", fake_run) + assert build(emit) == bitstreams / "sdk_gateware.bit" - with pytest.raises(gateware.LicenseUnsetError): - gateware.run_gateware_build( - str(pd), - "myplugin-gateware:latest-arm64", - env={}, + +@legacy_schema +def test_legacy_summary_ignores_bitstream_field_and_prefers_native( + build, bitstreams, schema_field +): + """Below schema 2 a ``bitstream`` key carries no meaning.""" + + def emit(bs): + _write(bs / "sdk_gateware.bit", b"native", _new_ns(1)) + _write(bs / "sdk_gateware_extracted.bit", b"extracted", _new_ns(2)) + _summary( + bs / "sdk_gateware.bit", + _new_ns(1), + bitstream="sdk_gateware_extracted.bit", + **schema_field, ) - assert called == [] + assert build(emit) == bitstreams / "sdk_gateware.bit" + + +@legacy_schema +def test_legacy_summary_falls_back_to_extracted(build, bitstreams, schema_field): + def emit(bs): + _write(bs / "sdk_gateware_extracted.bit", b"extracted", _new_ns(1)) + _summary(bs / "sdk_gateware_extracted.bit", _new_ns(1), **schema_field) + + assert build(emit) == bitstreams / "sdk_gateware_extracted.bit" # --------------------------------------------------------------------------- -# AC-10 case 11: bitstream glob returns newest of many +# Only this build's output counts # --------------------------------------------------------------------------- -def test_runner_returns_newest_bit_when_multiple_emitted( - gateware, peripheral_dir, monkeypatch +def test_runner_returns_extracted_image_when_stale_native_predates_build( + build, bitstreams ): - """Case 11: glob with two extracted .bit files of different mtimes -> newest wins.""" - pd, license_file = peripheral_dir - bs = _bitstreams_dir(pd) - older = os.path.join(bs, "sdk_old_extracted.bit") - newer = os.path.join(bs, "sdk_new_extracted.bit") - with open(older, "w") as fp: - fp.write("old") - time.sleep(0.05) - with open(newer, "w") as fp: - fp.write("new") - # Belt-and-suspenders: force mtimes so the test isn't flaky on - # coarse-granularity filesystems. - os.utime(older, (1_000_000, 1_000_000)) - os.utime(newer, (2_000_000, 2_000_000)) + """A native image left by an earlier build must not mask this build's + extracted-only output.""" + _write(bitstreams / "sdk_x.bit", b"earlier native", _stale_ns()) - def fake_run(argv, *args, **kwargs): - return subprocess.CompletedProcess(argv, 0, b"", b"") + selected = build( + lambda bs: _write(bs / "sdk_y_extracted.bit", b"this build", _new_ns(1)) + ) - monkeypatch.setattr(gateware.subprocess, "run", fake_run) + assert selected == bitstreams / "sdk_y_extracted.bit" + assert selected.read_bytes() == b"this build" + + +def test_runner_ignores_unchanged_preexisting_file_with_future_mtime(build, bitstreams): + """Unchanged means unchanged, even when the old file's mtime is ahead.""" + _write(bitstreams / "sdk_x.bit", b"earlier native", _new_ns(100)) - result = gateware.run_gateware_build( - str(pd), - "myplugin-gateware:latest-arm64", - env={"LM_LICENSE_FILE": str(license_file)}, + selected = build( + lambda bs: _write(bs / "sdk_y_extracted.bit", b"this build", _new_ns(1)) ) - assert os.path.abspath(result) == os.path.abspath(newer) + assert selected == bitstreams / "sdk_y_extracted.bit" + + +def test_runner_returns_preexisting_image_the_build_rewrote(build, bitstreams): + _write(bitstreams / "sdk_gateware.bit", b"earlier bytes", _stale_ns()) + + selected = build( + lambda bs: _write(bs / "sdk_gateware.bit", b"rebuilt bytes!", _new_ns(1)) + ) + + assert selected == bitstreams / "sdk_gateware.bit" + assert selected.read_bytes() == b"rebuilt bytes!" + + +def test_sequential_builds_each_return_their_own_native_image(build, bitstreams): + first = build(lambda bs: _write(bs / "sdk_a.bit", b"build A", _new_ns(5))) + # B's image is older on disk than A's: newness to *this* build is what + # matters, not mtime across builds. + second = build(lambda bs: _write(bs / "sdk_b.bit", b"build B", _new_ns(2))) + + assert first == bitstreams / "sdk_a.bit" + assert second == bitstreams / "sdk_b.bit" + assert second.read_bytes() == b"build B" + + +def test_runner_raises_when_only_non_candidate_files_emitted(build): + def emit(bs): + _write(bs / "gateware.bit", b"decoy", _new_ns(1)) + _write(bs / "sdk_gateware.mcs", b"decoy", _new_ns(1)) + + with pytest.raises(FileNotFoundError): + build(emit) + + +def test_runner_raises_when_build_emits_nothing(build): + with pytest.raises(FileNotFoundError): + build(lambda bs: None) + + +def test_runner_raises_when_build_emits_nothing_but_old_images_exist(build, bitstreams): + _write(bitstreams / "sdk_x.bit", b"earlier native", _stale_ns()) + _write(bitstreams / "sdk_x_extracted.bit", b"earlier extracted", _stale_ns()) + _summary(bitstreams / "sdk_x.bit", _stale_ns(), schema_version=1) + + with pytest.raises(FileNotFoundError): + build(lambda bs: None) # --------------------------------------------------------------------------- -# AC-10 case 12: no .bit -> clear FileNotFoundError naming the glob +# Schema 2: the summary names the image # --------------------------------------------------------------------------- -def test_runner_raises_with_glob_in_message_when_no_bit_emitted( - gateware, peripheral_dir, monkeypatch -): - """Case 12: docker run succeeds but no .bit lands -> FileNotFoundError - whose message names the expected glob pattern. - """ - pd, license_file = peripheral_dir - _bitstreams_dir(pd) # exists but empty +def test_schema2_summary_selects_named_image_over_newer_native(build, bitstreams): + def emit(bs): + _write(bs / "sdk_gw_extracted.bit", b"named image", _new_ns(1)) + _summary( + bs / "sdk_gw_extracted.bit", + _new_ns(1), + schema_version=2, + bitstream="sdk_gw_extracted.bit", + ) + _write(bs / "sdk_other.bit", b"newer native decoy", _new_ns(5)) - def fake_run(argv, *args, **kwargs): - return subprocess.CompletedProcess(argv, 0, b"", b"") + selected = build(emit) + + assert selected == bitstreams / "sdk_gw_extracted.bit" + assert selected.read_bytes() == b"named image" - monkeypatch.setattr(gateware.subprocess, "run", fake_run) - with pytest.raises(FileNotFoundError) as excinfo: - gateware.run_gateware_build( - str(pd), - "myplugin-gateware:latest-arm64", - env={"LM_LICENSE_FILE": str(license_file)}, +def _schema2_emitter(bitstream, extra=lambda bs: None, schema_version=2): + """Emit ``sdk_gw.bit`` plus its summary, whose ``bitstream`` is *bitstream*.""" + + def emit(bs): + _write(bs / "sdk_gw.bit", b"native", _new_ns(1)) + extra(bs) + _summary( + bs / "sdk_gw.bit", + _new_ns(1), + schema_version=schema_version, + bitstream=bitstream, ) - assert "sdk_*_extracted.bit" in str(excinfo.value) + return emit + + +def test_schema2_summary_selects_its_own_sibling_image(build, bitstreams): + def emit(bs): + _write(bs / "sdk_other.bit", b"newer native decoy", _new_ns(5)) + + selected = build(_schema2_emitter("sdk_gw.bit", extra=emit)) + + assert selected == bitstreams / "sdk_gw.bit" + assert selected.read_bytes() == b"native" + + +def test_schema2_rejects_summary_naming_a_different_new_image(build): + """Each summary may only name its own ``.bit``, even when the other + image is a valid file this build wrote.""" + emit = _schema2_emitter( + "sdk_other.bit", + extra=lambda bs: _write(bs / "sdk_other.bit", b"other", _new_ns(2)), + ) + + with pytest.raises(ValueError): + build(emit) + + +@pytest.mark.parametrize( + "bitstream", + # The backslash form is a legal POSIX file name, so only the rule rejects it. + ["sub/sdk_gw.bit", "sub\\sdk_gw.bit", "../sdk_gw.bit"], + ids=["forward_slash", "backslash", "parent_relative"], +) +def test_schema2_rejects_bitstream_that_is_not_a_basename(build, bitstream): + """Rejected even though a fresh image exists where the path points.""" + emit = _schema2_emitter( + bitstream, extra=lambda bs: _write(bs / bitstream, b"escape", _new_ns(2)) + ) + + with pytest.raises(ValueError): + build(emit) + + +def test_schema2_rejects_absolute_bitstream(build, bitstreams): + with pytest.raises(ValueError): + build(_schema2_emitter(str(bitstreams / "sdk_gw.bit"))) + + +def test_schema2_rejects_bitstream_without_bit_suffix(build): + emit = _schema2_emitter( + "sdk_gw.mcs", extra=lambda bs: _write(bs / "sdk_gw.mcs", b"mcs", _new_ns(1)) + ) + + with pytest.raises(ValueError): + build(emit) + + +def test_schema2_rejects_missing_sibling_image(build): + """A fresh fallback image must not paper over the summary's missing one.""" + + def emit(bs): + _write(bs / "sdk_other.bit", b"fallback native", _new_ns(1)) + _summary( + bs / "sdk_gw.bit", _new_ns(1), schema_version=2, bitstream="sdk_gw.bit" + ) + + with pytest.raises(FileNotFoundError): + build(emit) + + +def test_schema2_rejects_sibling_image_from_an_earlier_build(build, bitstreams): + _write(bitstreams / "sdk_old.bit", b"earlier native", _stale_ns()) + + def emit(bs): + _write(bs / "sdk_other.bit", b"fallback native", _new_ns(1)) + _summary( + bs / "sdk_old.bit", _new_ns(1), schema_version=2, bitstream="sdk_old.bit" + ) + + with pytest.raises((ValueError, FileNotFoundError)): + build(emit) + + +@pytest.mark.parametrize("schema_version", [3, 17]) +def test_newer_summary_schema_asks_for_upgrade(build, schema_version): + with pytest.raises(ValueError) as exc: + build(_schema2_emitter("sdk_gw.bit", schema_version=schema_version)) + + message = str(exc.value) + assert str(schema_version) in message + assert "upgrade" in message.lower() # --------------------------------------------------------------------------- -# Extracted-bitstream selection: the raw sdk_*.bit is ignored +# Summaries left by earlier builds carry no weight # --------------------------------------------------------------------------- -def test_runner_ignores_raw_bit_and_picks_extracted( - gateware, peripheral_dir, monkeypatch -): - """When both the raw sdk_*.bit and its sdk_*_extracted.bit sit side by - side, the runner must select the extracted variant — even when the raw - .bit is newer by mtime (the raw glob would otherwise win on mtime). - """ - pd, license_file = peripheral_dir - bs = _bitstreams_dir(pd) - raw = os.path.join(bs, "sdk_topbuild.bit") - extracted = os.path.join(bs, "sdk_topbuild_extracted.bit") - with open(raw, "w") as fp: - fp.write("raw") - with open(extracted, "w") as fp: - fp.write("extracted") - # Make the raw .bit strictly newer so a mtime-only selection would pick it. - os.utime(extracted, (1_000_000, 1_000_000)) - os.utime(raw, (2_000_000, 2_000_000)) +def test_stale_newer_schema_summary_is_ignored_next_to_fresh_image(build, bitstreams): + """A schema-3 summary from an earlier build must not block this build.""" + _summary( + bitstreams / "sdk_gw.bit", + _stale_ns(), + schema_version=3, + bitstream="sdk_gw.bit", + ) - def fake_run(argv, *args, **kwargs): - return subprocess.CompletedProcess(argv, 0, b"", b"") + selected = build(lambda bs: _write(bs / "sdk_gw.bit", b"fresh", _new_ns(1))) + + assert selected == bitstreams / "sdk_gw.bit" + assert selected.read_bytes() == b"fresh" - monkeypatch.setattr(gateware.subprocess, "run", fake_run) - result = gateware.run_gateware_build( - str(pd), - "myplugin-gateware:latest-arm64", - env={"LM_LICENSE_FILE": str(license_file)}, +def test_stale_schema2_summary_does_not_redirect_selection(build, bitstreams): + _write(bitstreams / "sdk_old.bit", b"earlier native", _stale_ns()) + _summary( + bitstreams / "sdk_old.bit", + _stale_ns(), + schema_version=2, + bitstream="sdk_old.bit", ) - assert os.path.abspath(result) == os.path.abspath(extracted) + selected = build( + lambda bs: _write(bs / "sdk_new_extracted.bit", b"fresh", _new_ns(1)) + ) + + assert selected == bitstreams / "sdk_new_extracted.bit" + + +# --------------------------------------------------------------------------- +# Schema 2 without a usable ``bitstream`` field +# --------------------------------------------------------------------------- + + +@pytest.mark.parametrize( + "bitstream_field", + [{}, {"bitstream": None}, {"bitstream": 7}, {"bitstream": ["sdk_gw.bit"]}], + ids=["missing", "null", "integer", "list"], +) +def test_schema2_summary_without_string_bitstream_raises(build, bitstream_field): + def emit(bs): + _write(bs / "sdk_gw.bit", b"native", _new_ns(1)) + _summary(bs / "sdk_gw.bit", _new_ns(1), schema_version=2, **bitstream_field) + + with pytest.raises(ValueError): + build(emit) diff --git a/synapse/tests/cli/test_half_selectors.py b/synapse/tests/cli/test_half_selectors.py index 9050ff9..7968056 100644 --- a/synapse/tests/cli/test_half_selectors.py +++ b/synapse/tests/cli/test_half_selectors.py @@ -803,10 +803,13 @@ def spy_mkdtemp(*args, **kwargs): f"driver deb must not carry the bitstream; got: {driver_files!r}" ) - assert any( - f.endswith(os.path.join("opt/scifi/bitstreams/custom", "via-devkit_gateware.bit")) - for f in gateware_files - ), f"gateware deb stages the bit under custom/; got: {gateware_files!r}" + image = os.path.join( + "opt/scifi/bitstreams/custom/artifacts/via-devkit_gateware", fake_bit.name + ) + assert [f for f in gateware_files if f.endswith(".bit")] == [image], ( + f"gateware deb stages only the built bit, under artifacts//; " + f"got: {gateware_files!r}" + ) assert any( f.endswith( os.path.join("opt/scifi/bitstreams/custom", "via-devkit_gateware.manifest.json") @@ -887,10 +890,12 @@ def spy_mkdtemp(*args, **kwargs): assert len(staging_dirs) == 1 files = _captured_staging_files(staging_dirs[0]) - assert any( - f.endswith(os.path.join("opt/scifi/bitstreams/custom", "via-devkit_gateware.bit")) - for f in files - ), f"gateware deb stages the bit; got: {files!r}" + image = os.path.join( + "opt/scifi/bitstreams/custom/artifacts/via-devkit_gateware", fake_bit.name + ) + assert [f for f in files if f.endswith(".bit")] == [image], ( + f"gateware deb stages only the built bit, under artifacts//; got: {files!r}" + ) with open( os.path.join( staging_dirs[0], @@ -903,7 +908,7 @@ def spy_mkdtemp(*args, **kwargs): assert frag == { "name": "via-devkit_gateware", "usb_pid": 4, - "artifact": "custom/via-devkit_gateware.bit", + "artifact": f"custom/artifacts/via-devkit_gateware/{fake_bit.name}", "git_hash": "e6890a3", } assert not any(f.endswith(".so") for f in files) diff --git a/synapse/tests/cli/test_peripherals_exit_codes.py b/synapse/tests/cli/test_peripherals_exit_codes.py new file mode 100644 index 0000000..bed8899 --- /dev/null +++ b/synapse/tests/cli/test_peripherals_exit_codes.py @@ -0,0 +1,318 @@ +"""Process exit status of ``synapsectl peripherals build`` / ``... deploy``. + +Each case runs the real ``synapsectl`` entry point on a command line and +asserts the integer it hands to ``sys.exit``. Only the edges that would touch +Docker, the gateware toolchain, fpm or a device are faked. +""" + +from __future__ import annotations + +import importlib.util +import itertools +import json +import os +import subprocess +import sys +from pathlib import Path + +import pytest + +import synapse.cli +from synapse.tests.cli.conftest import fake_fpm_run + +HALVES = ["driver", "gateware", "both"] +PLUGIN = "intan_rhd2132" +URI = "10.0.0.1" + +# conftest.py stubs synapse.cli.__main__, so load the real file under another name. +_spec = importlib.util.spec_from_file_location( + "synapse_cli_main_for_exit_codes", + Path(synapse.cli.__file__).with_name("__main__.py"), +) +cli_main = importlib.util.module_from_spec(_spec) +_spec.loader.exec_module(cli_main) + +with_and_without_uri = pytest.mark.parametrize( + "uri_args", [[], ["-u", URI]], ids=["no-uri", "with-uri"] +) + + +@pytest.fixture() +def peripherals(): + return importlib.import_module("synapse.cli.peripherals") + + +@pytest.fixture() +def synapsectl(monkeypatch): + """Return ``run(argv) -> exit status`` for the real ``synapsectl`` main.""" + + def run(argv): + monkeypatch.setattr(sys, "argv", ["synapsectl", *argv]) + return cli_main.main() + + return run + + +@pytest.fixture() +def plugin_dir(tmp_path): + """A single-project plugin repo with a valid manifest and gateware project.""" + pd = tmp_path / PLUGIN + (pd / "build" / "aarch64").mkdir(parents=True) + (pd / "manifest.json").write_text( + json.dumps( + { + "name": PLUGIN, + "version": "0.1.0", + "install": {"target": f"/usr/lib/scifi/plugins/{PLUGIN}.so"}, + } + ) + ) + # The driver half's runtime libs and .so, as a driver build leaves them. + for fname in ( + f"{PLUGIN}.so", + "libscifi-peripheral-sdk.so", + "libscifi-peripheral-sdk.so.0", + "libscifi-peripheral-sdk.so.0.1.0", + ): + (pd / "build" / "aarch64" / fname).write_text("fake") + gw = pd / "src" / "gateware" + gw.mkdir(parents=True) + (gw / "peripheral.yaml").write_text("radiant_version: '2024.2'\n") + return pd + + +def _emit_bitstream(peripheral_dir, *args, **kwargs): + """Stand-in for a gateware build that emits an image plus its summary.""" + out = os.path.join(str(peripheral_dir), "src", "gateware", "build", "bitstreams") + os.makedirs(out, exist_ok=True) + bit = os.path.join(out, f"sdk_{PLUGIN}.bit") + with open(bit, "w", encoding="utf-8") as fh: + fh.write("fake-bit") + with open( + os.path.join(out, f"sdk_{PLUGIN}.summary.json"), "w", encoding="utf-8" + ) as fh: + json.dump( + { + "usb_pid": "0x0004", + "target_profile": "via-devkit", + "project": {"name": "gateware", "git_sha": "e6890a3"}, + }, + fh, + ) + return bit + + +@pytest.fixture() +def toolchain(peripherals, plugin_dir, monkeypatch): + """Fake Docker, the driver/gateware builds, fpm and the device: all succeed. + + The real .deb packagers run; fpm is faked to drop the .deb into dist/. + Returns the list of ``(uri, deb)`` deploy calls. + """ + deploys: list = [] + + def deploy_package(uri, deb, *args, **kwargs): + deploys.append((uri, deb)) + return True + + monkeypatch.setattr(peripherals, "ensure_docker", lambda: True) + monkeypatch.setattr( + peripherals, + "build_docker_image", + lambda *a, **kw: {"driver": "fake-driver:arm64", "gateware": "fake-gw:arm64"}, + ) + monkeypatch.setattr(peripherals, "build_peripheral_so", lambda *a, **kw: True) + monkeypatch.setattr(peripherals.gateware, "run_gateware_build", _emit_bitstream) + monkeypatch.setattr( + peripherals.subprocess, + "run", + fake_fpm_run(os.path.join(str(plugin_dir), "dist"), []), + ) + monkeypatch.setattr(peripherals, "deploy_package", deploy_package) + return deploys + + +# --- Failure injectors: each breaks one precondition or one build step. ------ + + +def _docker_missing(peripherals, plugin_dir, monkeypatch): + monkeypatch.setattr(peripherals, "ensure_docker", lambda: False) + + +def _manifest_missing(peripherals, plugin_dir, monkeypatch): + (plugin_dir / "manifest.json").unlink() + + +def _manifest_not_json(peripherals, plugin_dir, monkeypatch): + (plugin_dir / "manifest.json").write_text("{not json") + + +def _manifest_without_name(peripherals, plugin_dir, monkeypatch): + (plugin_dir / "manifest.json").write_text(json.dumps({"version": "0.1.0"})) + + +def _driver_build_fails(peripherals, plugin_dir, monkeypatch): + monkeypatch.setattr(peripherals, "build_peripheral_so", lambda *a, **kw: False) + + +def _gateware_raises(error): + def inject(peripherals, plugin_dir, monkeypatch): + def fail(*args, **kwargs): + raise error + + monkeypatch.setattr(peripherals.gateware, "run_gateware_build", fail) + + return inject + + +def _fpm_fails(peripherals, plugin_dir, monkeypatch): + def run(argv, *args, **kwargs): + rc = 1 if "fpm" in argv else 0 + return subprocess.CompletedProcess(list(argv), rc, b"", b"fpm: error") + + monkeypatch.setattr(peripherals.subprocess, "run", run) + + +# (id, injector, halves the failure applies to) +FAILURES = [ + # Preconditions, checked before any half builds. + ("docker-missing", _docker_missing, HALVES), + ("manifest-missing", _manifest_missing, HALVES), + ("manifest-not-json", _manifest_not_json, HALVES), + ("manifest-without-name", _manifest_without_name, HALVES), + # A requested half fails to build or package. + ("driver-build-fails", _driver_build_fails, ["driver", "both"]), + ( + "gateware-build-fails", + _gateware_raises(subprocess.CalledProcessError(2, ["axon-peripheral-sdk"])), + ["gateware", "both"], + ), + ( + "gateware-emits-no-image", + _gateware_raises(FileNotFoundError("build emitted no bitstream")), + ["gateware", "both"], + ), + ("deb-packaging-fails", _fpm_fails, HALVES), +] +FAILURE_CASES = [ + pytest.param(inject, half, id=f"{case_id}-{half}") + for case_id, inject, halves in FAILURES + for half in halves +] + + +# --- peripherals build ------------------------------------------------------- + + +@pytest.mark.parametrize("inject, half", FAILURE_CASES) +def test_build_exits_nonzero_when_a_requested_half_cannot_be_built( + synapsectl, peripherals, plugin_dir, toolchain, monkeypatch, inject, half +): + inject(peripherals, plugin_dir, monkeypatch) + + assert synapsectl(["peripherals", "build", half, str(plugin_dir)]) != 0 + + +@pytest.mark.parametrize("half", HALVES) +def test_build_exits_zero_when_every_requested_half_builds( + synapsectl, plugin_dir, toolchain, half +): + assert synapsectl(["peripherals", "build", half, str(plugin_dir)]) == 0 + assert list((plugin_dir / "dist").glob("*.deb")), "a .deb must be produced" + + +# --- peripherals deploy ------------------------------------------------------ + + +@with_and_without_uri +def test_deploy_exits_nonzero_when_package_does_not_exist( + synapsectl, plugin_dir, toolchain, tmp_path, uri_args +): + missing = tmp_path / "absent_0.1.0_arm64.deb" + + status = synapsectl( + [ + *uri_args, + "peripherals", + "deploy", + "both", + str(plugin_dir), + "--package", + str(missing), + ] + ) + + assert status != 0 + assert toolchain == [], "nothing may be deployed" + + +@with_and_without_uri +@pytest.mark.parametrize("inject, half", FAILURE_CASES) +def test_deploy_exits_nonzero_when_the_build_fails( + synapsectl, peripherals, plugin_dir, toolchain, monkeypatch, inject, half, uri_args +): + inject(peripherals, plugin_dir, monkeypatch) + + status = synapsectl([*uri_args, "peripherals", "deploy", half, str(plugin_dir)]) + + assert status != 0 + assert toolchain == [], "nothing may be deployed after a failed build" + + +@pytest.mark.parametrize("half", HALVES) +def test_deploy_without_uri_exits_zero_after_building( + synapsectl, plugin_dir, toolchain, half +): + assert synapsectl(["peripherals", "deploy", half, str(plugin_dir)]) == 0 + assert list((plugin_dir / "dist").glob("*.deb")), "packages must be created" + assert toolchain == [], "no URI means nothing is deployed" + + +@pytest.mark.parametrize("half", HALVES) +def test_deploy_exits_zero_when_every_package_deploys( + synapsectl, plugin_dir, toolchain, half +): + assert synapsectl(["-u", URI, "peripherals", "deploy", half, str(plugin_dir)]) == 0 + expected = 2 if half == "both" else 1 + assert [uri for uri, _ in toolchain] == [URI] * expected + + +@pytest.mark.parametrize( + "half, failing_call", + [("driver", 0), ("gateware", 0), ("both", 0), ("both", 1)], + ids=["driver", "gateware", "both-first-package", "both-second-package"], +) +def test_deploy_exits_nonzero_when_any_package_fails_to_deploy( + synapsectl, peripherals, plugin_dir, toolchain, monkeypatch, half, failing_call +): + call_index = itertools.count() + + def deploy_package(uri, deb, *args, **kwargs): + return next(call_index) != failing_call + + monkeypatch.setattr(peripherals, "deploy_package", deploy_package) + + assert synapsectl(["-u", URI, "peripherals", "deploy", half, str(plugin_dir)]) != 0 + + +@with_and_without_uri +def test_deploy_existing_package_exits_zero( + synapsectl, plugin_dir, toolchain, tmp_path, uri_args +): + deb = tmp_path / "prebuilt_0.1.0_arm64.deb" + deb.write_text("fake-deb") + + status = synapsectl( + [ + *uri_args, + "peripherals", + "deploy", + "both", + str(plugin_dir), + "--package", + str(deb), + ] + ) + + assert status == 0 + assert toolchain == ([(URI, str(deb))] if uri_args else [])