93 lines
4.2 KiB
Python
93 lines
4.2 KiB
Python
import importlib.util
|
|
import tarfile
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
import yaml
|
|
|
|
spec = importlib.util.spec_from_file_location("build_slice", Path(__file__).parents[1] / "build-slice.py")
|
|
bs = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(bs)
|
|
|
|
|
|
def put(root: Path, rel: str, text: str):
|
|
p = root / rel
|
|
p.parent.mkdir(parents=True, exist_ok=True)
|
|
p.write_text(text)
|
|
|
|
|
|
@pytest.fixture
|
|
def repo(tmp_path):
|
|
put(tmp_path, "clusters/a/kustomization.yaml", "resources:\n - ./flux-system\n - ./layers.yaml\n")
|
|
put(tmp_path, "clusters/a/flux-system/kustomization.yaml", "resources:\n - gotk-sync.yaml\n")
|
|
put(tmp_path, "clusters/a/flux-system/gotk-sync.yaml",
|
|
"apiVersion: kustomize.toolkit.fluxcd.io/v1\nkind: Kustomization\nspec:\n path: ./clusters/a\n")
|
|
put(tmp_path, "clusters/a/layers.yaml",
|
|
"apiVersion: kustomize.toolkit.fluxcd.io/v1\nkind: Kustomization\nspec:\n path: ./layers/a/00-vault\n")
|
|
put(tmp_path, "layers/a/00-vault/kustomization.yaml", "resources:\n - ../../../infrastructure/vault/contour-template\n")
|
|
put(tmp_path, "infrastructure/vault/contour-template/kustomization.yaml",
|
|
"resources:\n - ../base\npatches:\n - path: vault.yaml\n")
|
|
put(tmp_path, "infrastructure/vault/contour-template/vault.yaml", "kind: HelmRelease\n")
|
|
put(tmp_path, "infrastructure/vault/base/kustomization.yaml", "resources:\n - helmrelease.yaml\n")
|
|
put(tmp_path, "infrastructure/vault/base/helmrelease.yaml", "kind: HelmRelease\n")
|
|
put(tmp_path, "infrastructure/vault/base/unused.yaml", "kind: ConfigMap\n")
|
|
put(tmp_path, "infrastructure/vault/uralkal/kustomization.yaml", "resources:\n - ../base\n")
|
|
put(tmp_path, "clusters/b/kustomization.yaml", "resources:\n - ../../infrastructure/vault/uralkal\n")
|
|
return tmp_path
|
|
|
|
|
|
def test_slice_follows_flux_paths_and_skips_foreign(repo):
|
|
files, entries = bs.collect(repo, "clusters/a")
|
|
names = {str(f) for f in files}
|
|
assert "infrastructure/vault/base/helmrelease.yaml" in names
|
|
assert "infrastructure/vault/contour-template/vault.yaml" in names
|
|
assert "layers/a/00-vault/kustomization.yaml" in names
|
|
assert not any("uralkal" in n or n.startswith("clusters/b") for n in names)
|
|
assert "infrastructure/vault/base/unused.yaml" not in names
|
|
assert entries == ["clusters/a", "layers/a/00-vault"]
|
|
|
|
|
|
def test_remote_reference_stops_build(repo):
|
|
put(repo, "clusters/a/kustomization.yaml", "resources:\n - https://github.com/x/y//z\n")
|
|
with pytest.raises(bs.SliceError, match="remote reference"):
|
|
bs.collect(repo, "clusters/a")
|
|
|
|
|
|
def test_dangling_reference_stops_build(repo):
|
|
put(repo, "clusters/a/kustomization.yaml", "resources:\n - ./missing.yaml\n")
|
|
with pytest.raises(bs.SliceError, match="does not exist"):
|
|
bs.collect(repo, "clusters/a")
|
|
|
|
|
|
def test_tar_is_byte_identical_across_runs(repo, tmp_path_factory):
|
|
files, _ = bs.collect(repo, "clusters/a")
|
|
out1 = tmp_path_factory.mktemp("o") / "s.tar"
|
|
out2 = tmp_path_factory.mktemp("o") / "s.tar"
|
|
bs.write_tar(repo, files, out1)
|
|
(repo / "clusters/a/layers.yaml").touch()
|
|
bs.write_tar(repo, files, out2)
|
|
assert out1.read_bytes() == out2.read_bytes()
|
|
with tarfile.open(out1) as t:
|
|
assert all(m.mtime == 0 for m in t.getmembers())
|
|
|
|
|
|
|
|
pspec = importlib.util.spec_from_file_location("publish_slice", Path(__file__).parents[1] / "publish-slice.py")
|
|
ps = importlib.util.module_from_spec(pspec)
|
|
pspec.loader.exec_module(ps)
|
|
|
|
|
|
def test_every_cluster_with_kustomization_is_a_contour(tmp_path):
|
|
put(tmp_path, "clusters/stand/kustomization.yaml", "resources: []\n")
|
|
put(tmp_path, "clusters/vad/kustomization.yaml", "resources: []\n")
|
|
put(tmp_path, "clusters/contour/apps.yaml", "kind: Kustomization\n")
|
|
assert ps.list_contours(tmp_path) == ["stand", "vad"]
|
|
|
|
|
|
def test_child_pipeline_has_one_job_per_contour():
|
|
doc = yaml.safe_load(ps.child_pipeline(["stand", "vad"], "3.9.68", "abcdef1234"))
|
|
jobs = {k: v for k, v in doc.items() if k != "stages"}
|
|
assert sorted(jobs) == ["slice stand", "slice vad"]
|
|
assert "--contour vad --release 3.9.68 --commit abcdef1234" in jobs["slice vad"]["script"][1]
|
|
assert all(ord(ch) < 128 for ch in ps.child_pipeline(["stand"], "1.0.0", "a" * 40))
|