Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/skills/flavors.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ commentary -- it is what a flavor moves to when it is switched off
or recipe may carry its own copy of the flavor list or image names -- that drift
is what `config/flavors.json` and `scripts/flavors.py` exist to stop. Recipes
that need an image name ask for it at the source -- `build-ghcr`, `gen-sbom`,
and `secureboot` each run `python3 scripts/flavors.py image <flavor>` -- so
`secureboot`, and `scripts/check-reproducible-build.sh` each run
`python3 scripts/flavors.py image <flavor>` -- so
build, promote, and release always agree on published image names. `just
image_name` is the same query wrapped for callers outside the Justfile; no
recipe goes through it.
Expand Down
30 changes: 22 additions & 8 deletions scripts/check-reproducible-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,27 @@ REPO_ORGANIZATION="${REPO_ORGANIZATION:-projectbluefin}"
# Hold the source commit epoch constant across both uncached builds.
SOURCE_DATE_EPOCH="${SOURCE_DATE_EPOCH:-$(git log -1 --format=%ct -- Containerfile packages system_files scripts config .gitmodules)}"

case "${FLAVOR}" in
main | nvidia | gaming | nvidia-gaming) ;;
*)
echo "unknown flavor: ${FLAVOR} (expected main, nvidia, gaming or nvidia-gaming)" >&2
exit 2
;;
esac
# scripts/flavors.py owns the flavor roster and the published image name for
# each flavor, as it does for build-ghcr. Building under any other IMAGE_NAME
# would bake a different image-ref into image-info.json than CI ships.
# Gate on the active roster, not just the image map: a retired flavor still has
# an image name but is no longer shipped. flavors.py errors (missing python3,
# malformed config/flavors.json) pass through on stderr rather than being
# reported as an unknown flavor.
FLAVORS_PY="$(dirname "$0")/flavors.py"
if ! ROSTER="$(python3 "${FLAVORS_PY}" list)"; then
echo "cannot read the flavor roster from ${FLAVORS_PY}" >&2
exit 2
fi
if ! python3 -c 'import json, sys; sys.exit(sys.argv[2] not in json.loads(sys.argv[1]))' \
"${ROSTER}" "${FLAVOR}"; then
echo "unknown flavor: ${FLAVOR} (expected one of: ${ROSTER})" >&2
exit 2
fi
if ! IMAGE_NAME="$(python3 "${FLAVORS_PY}" image "${FLAVOR}")"; then
echo "cannot resolve the image name for ${FLAVOR} from ${FLAVORS_PY}" >&2
exit 2
fi

# Match build-ghcr's cache route, resolving it once for both builds. Otherwise
# kernel flavors compile from the pristine base rather than test the image CI
Expand Down Expand Up @@ -95,7 +109,7 @@ build() {
--no-cache \
"${epoch_args[@]}" \
"${base_args[@]}" \
--build-arg IMAGE_NAME=utah \
--build-arg IMAGE_NAME="${IMAGE_NAME}" \
--build-arg IMAGE_ID=utah \
--build-arg IMAGE_FLAVOR="${FLAVOR}" \
--build-arg IMAGE_VENDOR="${REPO_ORGANIZATION}" \
Expand Down
60 changes: 60 additions & 0 deletions tests/test_flavor_contract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""The desktop contract must accept every flavor scripts/flavors.py builds.

contracts/bluefin-desktop.toml checks image-info.json inside a built image, so
its image-flavor and image-ref patterns restate the flavor roster and the
flavor-to-image-name map that config/flavors.json and scripts/flavors.py own.
Adding or renaming a flavor there without updating the contract would only
fail after a full image build. This pins the two together host-side.
"""

from __future__ import annotations

import json
import re
import subprocess
import tomllib
import unittest
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
FLAVORS = ROOT / "scripts" / "flavors.py"
CONTRACT = ROOT / "contracts" / "bluefin-desktop.toml"


def flavors_py(*args: str) -> str:
return subprocess.run(
["python3", str(FLAVORS), *args],
check=True, capture_output=True, text=True).stdout.strip()


class DesktopContractFlavorTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
contract = tomllib.loads(CONTRACT.read_text())
cls.image_info = contract["branding"]["image_info"]
cls.patterns = contract["branding"]["image_info_patterns"]
cls.flavors = json.loads(flavors_py("list"))

def test_the_roster_is_not_empty(self):
self.assertTrue(self.flavors)

def test_every_built_flavor_matches_the_image_flavor_pattern(self):
pattern = re.compile(self.patterns["image-flavor"])
for flavor in self.flavors:
with self.subTest(flavor=flavor):
self.assertRegex(flavor, pattern)

def test_every_built_image_matches_the_image_ref_pattern(self):
# configure-branding.sh writes
# ostree-image-signed:docker://ghcr.io/<vendor>/<IMAGE_NAME>.
pattern = re.compile(self.patterns["image-ref"])
vendor = self.image_info["image-vendor"]
for flavor in self.flavors:
image = flavors_py("image", flavor)
ref = f"ostree-image-signed:docker://ghcr.io/{vendor}/{image}"
with self.subTest(flavor=flavor, ref=ref):
self.assertRegex(ref, pattern)


if __name__ == "__main__":
unittest.main()
53 changes: 51 additions & 2 deletions tests/test_reproducible_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from __future__ import annotations

import json
import os
import subprocess
import tempfile
Expand Down Expand Up @@ -71,7 +72,7 @@ def setUp(self):
self.cosign.chmod(0o755)

def run_check(self, first: str, second: str, flavor: str = "main", verify_status: int = 0,
podman_version: str = "5.6.0"):
podman_version: str = "5.6.0", script: Path = SCRIPT):
layers_a = self.tmp / "a"
layers_b = self.tmp / "b"
layers_a.write_text(first)
Expand All @@ -88,7 +89,7 @@ def run_check(self, first: str, second: str, flavor: str = "main", verify_status
"PODMAN_VERSION": podman_version,
})
return subprocess.run(
["bash", str(SCRIPT), flavor],
["bash", str(script), flavor],
cwd=ROOT, env=env, capture_output=True, text=True)

def builds(self) -> list[str]:
Expand Down Expand Up @@ -154,6 +155,26 @@ def test_the_flavor_reaches_both_builds(self):
for invocation in self.builds():
self.assertIn("--build-arg IMAGE_FLAVOR=nvidia-gaming", invocation)

def test_each_flavor_builds_under_its_published_image_name(self):
# build-ghcr takes IMAGE_NAME from scripts/flavors.py; the probe must
# too, or a kernel flavor bakes the main image's image-ref into
# image-info.json and the two builds compare an image CI never ships.
roster = subprocess.run(
["python3", str(ROOT / "scripts" / "flavors.py"), "list"],
check=True, capture_output=True, text=True).stdout
for flavor in json.loads(roster):
with self.subTest(flavor=flavor):
self.build_log.write_text("")
expected = subprocess.run(
["python3", str(ROOT / "scripts" / "flavors.py"), "image", flavor],
check=True, capture_output=True, text=True).stdout.strip()
result = self.run_check(IDENTICAL, IDENTICAL, flavor=flavor)
self.assertEqual(result.returncode, 0, result.stderr)
builds = self.builds()
self.assertEqual(len(builds), 2)
for invocation in builds:
self.assertIn(f"--build-arg IMAGE_NAME={expected} ", invocation)

def test_kernel_flavors_build_only_the_verified_immutable_cache(self):
result = self.run_check(IDENTICAL, IDENTICAL, flavor="nvidia")
self.assertEqual(result.returncode, 0, result.stderr)
Expand All @@ -171,6 +192,34 @@ def test_an_unknown_flavor_is_rejected_before_building(self):
self.assertIn("unknown flavor", result.stderr)
self.assertEqual(self.builds(), [], "nothing should have been built")

def _run_with_config(self, config: str, flavor: str):
# Run a copy of the script against its own config/flavors.json, so the
# roster can be narrowed without touching the repository's.
tree = self.tmp / "tree"
(tree / "scripts").mkdir(parents=True)
(tree / "config").mkdir()
for name in ("check-reproducible-build.sh", "flavors.py", "podman-epoch-args.sh"):
target = tree / "scripts" / name
target.write_text((ROOT / "scripts" / name).read_text())
target.chmod(0o755)
(tree / "config" / "flavors.json").write_text(config)
return self.run_check(IDENTICAL, IDENTICAL, flavor=flavor,
script=tree / "scripts" / "check-reproducible-build.sh")

def test_a_retired_flavor_is_rejected_before_building(self):
result = self._run_with_config(
json.dumps({"flavors": ["main"], "retired": {"nvidia": "switched off"}}), "nvidia")
self.assertEqual(result.returncode, 2)
self.assertIn("unknown flavor: nvidia", result.stderr)
self.assertEqual(self.builds(), [], "nothing should have been built")

def test_a_broken_roster_is_not_reported_as_an_unknown_flavor(self):
result = self._run_with_config("{not json", "main")
self.assertEqual(result.returncode, 2)
self.assertNotIn("unknown flavor", result.stderr)
self.assertIn("cannot read the flavor roster", result.stderr)
self.assertEqual(self.builds(), [], "nothing should have been built")


if __name__ == "__main__":
unittest.main()
Loading