132 lines
5.9 KiB
Python
132 lines
5.9 KiB
Python
import json
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from validate_release import validate_manifest
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
REPOSITORIES = {
|
|
"backend": ["registry.invalid/akyldash/backend"],
|
|
"opensearch": ["registry.invalid/akyldash/opensearch"],
|
|
}
|
|
|
|
|
|
class ReleaseManifestTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.policy = {"schema_version": 1, "repositories": REPOSITORIES}
|
|
self.manifest = {
|
|
"schema_version": 1,
|
|
"source_ref": "refs/heads/main",
|
|
"commit": "a" * 40,
|
|
"images": {
|
|
"backend": "registry.invalid/akyldash/backend@sha256:" + "b" * 64,
|
|
"opensearch": "registry.invalid/akyldash/opensearch@sha256:" + "c" * 64,
|
|
},
|
|
}
|
|
|
|
def test_accepts_both_policy_pinned_images(self):
|
|
self.assertEqual(validate_manifest(self.manifest, self.policy), self.manifest)
|
|
|
|
def test_rejects_untrusted_ref(self):
|
|
self.manifest["source_ref"] = "refs/pull/12/head"
|
|
with self.assertRaisesRegex(ValueError, "source_ref"):
|
|
validate_manifest(self.manifest, self.policy)
|
|
|
|
def test_rejects_tag_instead_of_digest(self):
|
|
self.manifest["images"]["backend"] = "registry.invalid/akyldash/backend:latest"
|
|
with self.assertRaisesRegex(ValueError, "sha256"):
|
|
validate_manifest(self.manifest, self.policy)
|
|
|
|
def test_rejects_empty_image_reference(self):
|
|
self.manifest["images"]["backend"] = ""
|
|
with self.assertRaisesRegex(ValueError, "sha256"):
|
|
validate_manifest(self.manifest, self.policy)
|
|
|
|
def test_rejects_invalid_commit(self):
|
|
self.manifest["commit"] = "not-a-commit"
|
|
with self.assertRaisesRegex(ValueError, "commit"):
|
|
validate_manifest(self.manifest, self.policy)
|
|
|
|
def test_rejects_repository_outside_policy(self):
|
|
self.manifest["images"]["backend"] = "registry.invalid/other/backend@sha256:" + "b" * 64
|
|
with self.assertRaisesRegex(ValueError, "not allowed"):
|
|
validate_manifest(self.manifest, self.policy)
|
|
|
|
def test_rejects_missing_or_extra_images(self):
|
|
self.manifest["images"].pop("opensearch")
|
|
with self.assertRaisesRegex(ValueError, "exactly backend and opensearch"):
|
|
validate_manifest(self.manifest, self.policy)
|
|
self.manifest["images"]["opensearch"] = "registry.invalid/akyldash/opensearch@sha256:" + "c" * 64
|
|
self.manifest["images"]["extra"] = "registry.invalid/akyldash/extra@sha256:" + "d" * 64
|
|
with self.assertRaisesRegex(ValueError, "exactly backend and opensearch"):
|
|
validate_manifest(self.manifest, self.policy)
|
|
|
|
def test_rejects_duplicate_json_keys(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
manifest_path = Path(directory) / "manifest.json"
|
|
policy_path = Path(directory) / "policy.json"
|
|
manifest_path.write_text(
|
|
'{"schema_version":1,"source_ref":"refs/heads/main","commit":"' + "a" * 40
|
|
+ '","images":{"backend":"first","backend":"second","opensearch":"third"}}',
|
|
encoding="utf-8",
|
|
)
|
|
policy_path.write_text(json.dumps(self.policy), encoding="utf-8")
|
|
command = [
|
|
"python3", str(ROOT / "infra/deploy/validate_release.py"), str(manifest_path),
|
|
"--policy", str(policy_path),
|
|
]
|
|
result = subprocess.run(command, text=True, capture_output=True)
|
|
self.assertEqual(result.returncode, 1)
|
|
self.assertIn("duplicate JSON key: backend", result.stderr)
|
|
|
|
def test_rejects_malformed_json(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
manifest_path = Path(directory) / "manifest.json"
|
|
policy_path = Path(directory) / "policy.json"
|
|
manifest_path.write_text('{"schema_version":', encoding="utf-8")
|
|
policy_path.write_text(json.dumps(self.policy), encoding="utf-8")
|
|
result = subprocess.run(
|
|
["python3", str(ROOT / "infra/deploy/validate_release.py"), str(manifest_path), "--policy", str(policy_path)],
|
|
text=True,
|
|
capture_output=True,
|
|
)
|
|
self.assertEqual(result.returncode, 1)
|
|
self.assertIn("cannot read valid JSON", result.stderr)
|
|
|
|
@unittest.skipUnless(shutil.which("docker"), "Docker CLI is unavailable")
|
|
def test_compose_override_renders_digest_images_and_preserves_build(self):
|
|
backend = "registry.invalid/akyldash/backend@sha256:" + "b" * 64
|
|
opensearch = "registry.invalid/akyldash/opensearch@sha256:" + "c" * 64
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
temp = Path(directory)
|
|
secret = temp / "review_secret"
|
|
secret.touch()
|
|
override = temp / "release-override.json"
|
|
override.write_text(json.dumps({"services": {
|
|
"backend": {"image": backend},
|
|
"opensearch": {"image": opensearch},
|
|
}}), encoding="utf-8")
|
|
command = [
|
|
"docker", "compose", "--env-file", str(ROOT / "deploy/production/.env.example"),
|
|
"-f", str(ROOT / "deploy/production/compose.yaml"), "-f", str(override),
|
|
"config", "--format", "json",
|
|
]
|
|
result = subprocess.run(
|
|
command, text=True, capture_output=True,
|
|
env={**os.environ, "REVIEW_SECRET_FILE": str(secret)},
|
|
)
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
services = json.loads(result.stdout)["services"]
|
|
self.assertEqual(services["backend"]["image"], backend)
|
|
self.assertEqual(services["opensearch"]["image"], opensearch)
|
|
self.assertIn("build", services["backend"])
|
|
self.assertIn("build", services["opensearch"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|