diff --git a/Justfile b/Justfile index a10585b5..4e7a8920 100644 --- a/Justfile +++ b/Justfile @@ -233,6 +233,22 @@ dogfood-check: dogfood-mdns: bash scripts/dogfood-mdns.sh dist/diskless +# Shared body for the single opt-in sysext export recipes. NAME is the element +# base (the element is oci/-sysext.bst) and also names the echo line; +# PREFIX is that element's sysext-image name, so *.raw.zst matches the +# built artifact. Every export-*-sysext recipe calls this instead of re-writing +# the checkout, copy, and SHA256SUMS steps by hand, so a new add-on cannot drift +# its glob the way the zfs glob did (#306). +[group('sysext')] +_export-sysext-element NAME PREFIX: + rm -rf dist/{{NAME}}-checkout + mkdir -p dist/sysext + just bst artifact checkout oci/{{NAME}}-sysext.bst --directory /src/dist/{{NAME}}-checkout + cp dist/{{NAME}}-checkout/{{PREFIX}}*.raw.zst dist/sysext/ + grep 'raw.zst$' dist/{{NAME}}-checkout/SHA256SUMS >> dist/sysext/SHA256SUMS + rm -rf dist/{{NAME}}-checkout + @echo "==> wrote {{NAME}} sysext:" && ls -lh dist/sysext/ + # Build the OpenZFS systemd-sysext (locked to one image version). [group('sysext')] build-zfs-sysext: gen-dev-keys @@ -241,13 +257,7 @@ build-zfs-sysext: gen-dev-keys # Export the OpenZFS sysext + SHA256SUMS to dist/sysext/. [group('sysext')] export-zfs-sysext: build-zfs-sysext - rm -rf dist/zfs-checkout - mkdir -p dist/sysext - just bst artifact checkout oci/zfs-sysext.bst --directory /src/dist/zfs-checkout - cp dist/zfs-checkout/zfs_*.raw.zst dist/sysext/ - grep 'raw.zst$' dist/zfs-checkout/SHA256SUMS >> dist/sysext/SHA256SUMS - rm -rf dist/zfs-checkout - @echo "==> wrote zfs sysext:" && ls -lh dist/sysext/ + just _export-sysext-element "zfs" "zfs_" # Build the homelab sysext (component manifests + bluefin-homelab-apply) and # its add-on sysexts (Argo Workflows, MCP server, KubeStellar). @@ -302,13 +312,7 @@ build-nvidia-sysext FLAVOUR="nvidia-open-595": gen-dev-keys # Export an NVIDIA driver sysext + SHA256SUMS to dist/sysext/. [group('sysext')] export-nvidia-sysext FLAVOUR="nvidia-open-595": (build-nvidia-sysext FLAVOUR) - rm -rf dist/{{FLAVOUR}}-checkout - mkdir -p dist/sysext - just bst artifact checkout oci/{{FLAVOUR}}-sysext.bst --directory /src/dist/{{FLAVOUR}}-checkout - cp dist/{{FLAVOUR}}-checkout/{{FLAVOUR}}_*.raw.zst dist/sysext/ - grep 'raw.zst$' dist/{{FLAVOUR}}-checkout/SHA256SUMS >> dist/sysext/SHA256SUMS - rm -rf dist/{{FLAVOUR}}-checkout - @echo "==> wrote {{FLAVOUR}} sysext:" && ls -lh dist/sysext/ + just _export-sysext-element "{{FLAVOUR}}" "{{FLAVOUR}}_" # Install dist/diskless/ in QEMU, merge its NVIDIA sysext and probe it (no GPU). [group('sysext')] @@ -347,13 +351,7 @@ build-nvidia-container-toolkit-sysext: # Export the NVIDIA Container Toolkit sysext + SHA256SUMS to dist/sysext/. [group('sysext')] export-nvidia-container-toolkit-sysext: build-nvidia-container-toolkit-sysext - rm -rf dist/nvidia-ctk-checkout - mkdir -p dist/sysext - just bst artifact checkout oci/nvidia-container-toolkit-sysext.bst --directory /src/dist/nvidia-ctk-checkout - cp dist/nvidia-ctk-checkout/nvidia-container-toolkit-*.raw.zst dist/sysext/ - grep 'raw.zst$' dist/nvidia-ctk-checkout/SHA256SUMS >> dist/sysext/SHA256SUMS - rm -rf dist/nvidia-ctk-checkout - @echo "==> wrote nvidia-container-toolkit sysext:" && ls -lh dist/sysext/ + just _export-sysext-element "nvidia-container-toolkit" "nvidia-container-toolkit-" # Build the Tailscale systemd-sysext (locked to one image version). [group('sysext')] @@ -363,13 +361,7 @@ build-tailscale-sysext: # Export the Tailscale sysext + SHA256SUMS to dist/sysext/. [group('sysext')] export-tailscale-sysext: build-tailscale-sysext - rm -rf dist/tailscale-checkout - mkdir -p dist/sysext - just bst artifact checkout oci/tailscale-sysext.bst --directory /src/dist/tailscale-checkout - cp dist/tailscale-checkout/tailscale_*.raw.zst dist/sysext/ - grep 'raw.zst$' dist/tailscale-checkout/SHA256SUMS >> dist/sysext/SHA256SUMS - rm -rf dist/tailscale-checkout - @echo "==> wrote tailscale sysext:" && ls -lh dist/sysext/ + just _export-sysext-element "tailscale" "tailscale_" # Build the NetBird systemd-sysext (locked to one image version). [group('sysext')] @@ -379,13 +371,7 @@ build-netbird-sysext: # Export the NetBird sysext + SHA256SUMS to dist/sysext/. [group('sysext')] export-netbird-sysext: build-netbird-sysext - rm -rf dist/netbird-checkout - mkdir -p dist/sysext - just bst artifact checkout oci/netbird-sysext.bst --directory /src/dist/netbird-checkout - cp dist/netbird-checkout/netbird_*.raw.zst dist/sysext/ - grep 'raw.zst$' dist/netbird-checkout/SHA256SUMS >> dist/sysext/SHA256SUMS - rm -rf dist/netbird-checkout - @echo "==> wrote netbird sysext:" && ls -lh dist/sysext/ + just _export-sysext-element "netbird" "netbird_" # Set up KubeStellar kc-agent for the user in ONE command. [group('test')] diff --git a/tests/unit/test_nvidia_container_toolkit_sysext.py b/tests/unit/test_nvidia_container_toolkit_sysext.py index 8904f2e1..57fe949f 100644 --- a/tests/unit/test_nvidia_container_toolkit_sysext.py +++ b/tests/unit/test_nvidia_container_toolkit_sysext.py @@ -205,4 +205,4 @@ def test_justfile_validates_builds_and_exports_the_sysext() -> None: validate = next(line for line in justfile.splitlines() if "just bst show --deps all" in line) assert "oci/nvidia-container-toolkit-sysext.bst" in validate assert "just bst build oci/nvidia-container-toolkit-sysext.bst" in justfile - assert "just bst artifact checkout oci/nvidia-container-toolkit-sysext.bst" in justfile + assert 'just _export-sysext-element "nvidia-container-toolkit" "nvidia-container-toolkit-"' in justfile diff --git a/tests/unit/test_sysext_export_contract.py b/tests/unit/test_sysext_export_contract.py index 2ea4f857..2d7436b6 100644 --- a/tests/unit/test_sysext_export_contract.py +++ b/tests/unit/test_sysext_export_contract.py @@ -24,7 +24,11 @@ def test_export_sysext_does_not_emit_legacy_k3s_alias() -> None: body = recipe_body("export-sysext") assert body, "the export-sysext recipe is missing, so this check would pass vacuously" + + # k0s no longer delegates: its recipe inlines the checkout and copies + # oci/k0s-sysext.bst's artifact, so the export is preserved by the body. assert any("oci/k0s-sysext.bst" in line for line in body), "export-sysext no longer exports k0s" + assert not [line for line in body if "k3s" in line] @@ -35,21 +39,72 @@ def sysext_image_name(element: str) -> str: return match.group(1).replace("%{image-version}", "") -def test_export_recipes_copy_globs_match_the_element_image_names() -> None: - for recipe, element in ( - ("export-zfs-sysext", "zfs-sysext.bst"), - ("export-nvidia-sysext", "nvidia-open-595-sysext.bst"), - ): - body = recipe_body(recipe) - assert body, f"the {recipe} recipe is missing, so this check would pass vacuously" - - copies = [line for line in body if line.startswith("cp ") and ".raw.zst" in line] - assert copies, f"{recipe} never copies a .raw.zst artifact" - - prefix = sysext_image_name(element) - for line in copies: - glob = line.split()[1].rsplit("/", 1)[-1] - stem = glob.split("*", 1)[0].replace("{{FLAVOUR}}", "nvidia-open-595") - assert stem == prefix, ( - f"{recipe} copies {glob!r}, which never matches {prefix}.raw.zst" - ) +# The five single-element add-on exports delegate to the shared helper. +# k0s behavior changes remain separate; homelab exports several elements. +def _sysext_export_recipes() -> list[str]: + names = [] + for line in JUSTFILE.read_text(encoding="utf-8").splitlines(): + stripped = line.strip() + if not re.match(r"^export-", stripped) or ":" not in line: + continue + name = stripped.split(":", 1)[0].split()[0] + if (name.endswith("-sysext") + and name not in {"export-sysext", "export-homelab-sysext"}): + names.append(name) + return names + + +def _recipe_header(name: str) -> str: + lines = JUSTFILE.read_text(encoding="utf-8").splitlines() + header = re.compile(rf"^{re.escape(name)}(\s[^:]*)?:(?!=)") + for line in lines: + if header.match(line): + return line + return "" + + +def _flavour_of(header: str) -> str | None: + match = re.search(r'FLAVOUR="([^"]*)"', header) + return match.group(1) if match else None + + +def _resolve(expr: str, flavour: str | None) -> str: + return expr.replace("{{FLAVOUR}}", flavour) if flavour else expr + + +def _helper_call(body: list[str]) -> tuple[str, str] | None: + for line in body: + if not line.startswith("just _export-sysext-element"): + continue + args = re.findall(r'"([^"]*)"', line) + if len(args) == 2: + return args[0], args[1] + return None + + +def test_single_element_recipes_call_the_shared_helper() -> None: + recipes = _sysext_export_recipes() + assert recipes, "no export-*-sysext recipes found" + # The parser splits the FLAVOUR= assignment off the header, so the NVIDIA + # recipe is discovered rather than silently skipped (regression for #649). + assert "export-nvidia-sysext" in recipes, "NVIDIA export dropped from discovery" + + for name in recipes: + header = _recipe_header(name) + flavour = _flavour_of(header) + body = recipe_body(name) + call = _helper_call(body) + assert call, ( + f"{name} does not call _export-sysext-element; it re-inlines the " + "checkout, copy, and SHA256SUMS steps, so its glob is unguarded" + ) + + ename = f"{_resolve(call[0], flavour)}-sysext.bst" + element_path = ROOT / "elements" / "oci" / ename + assert element_path.exists(), f"{name} names element {ename}, which does not exist" + expected = re.sub(r"%\{[^}]+\}", "", sysext_image_name(ename)) + prefix = _resolve(call[1], flavour) + assert prefix == expected, ( + f"{name} copies with prefix {prefix!r}, but the element's sysext-image is " + f"{expected!r}, so the glob never matches the built artifact" + )