"""Regression tests for the guard, using Git-visible isolated YAML fixtures only.""" import importlib.util import json from pathlib import Path import subprocess import tempfile import unittest SCRIPT = Path(__file__).with_name("check-postgres-shm.py") SPEC = importlib.util.spec_from_file_location("postgres_shm", SCRIPT) CHECK = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(CHECK) class PostgresShmTests(unittest.TestCase): def setUp(self): self.temp = tempfile.TemporaryDirectory(prefix=".shm-test-", dir=SCRIPT.parent.parent) self.addCleanup(self.temp.cleanup) self.root = Path(self.temp.name) subprocess.run(["git", "init", "-q", str(self.root)], check=True) def write(self, path, content): target = self.root / path target.parent.mkdir(parents=True, exist_ok=True) target.write_text(content) def compose(self, size="1g", image="postgres:17", extra=""): shm = f" shm_size: {size}\n" if size is not None else "" self.write("compose.yml", f"services:\n db:\n image: {image}\n{shm}{extra}") def cli(self): result = subprocess.run( ["python3", str(SCRIPT), "--root", str(self.root), "--json"], capture_output=True, text=True, ) return result.returncode, json.loads(result.stdout) def test_missing_or_inadequate_size_fails_cli(self): for size in (None, "64m", "512m", "1073741823", "null", "true", "0", "-1g", "${PG_SHM:-1g}", "!reset 1g", "nonsense"): with self.subTest(size=size): self.compose(size) code, report = self.cli() self.assertEqual(code, 1) self.assertIn("requires explicit literal shm_size >= 1g", report["errors"][0]) def test_adequate_docker_units_and_byte_counts_pass(self): for size in ("1g", "1024M", "1048576k", "1073741824", "'1GB'", "2g"): with self.subTest(size=size): self.compose(size) code, report = self.cli() self.assertEqual(code, 0) self.assertEqual(report["inventory"][0]["status"], "safe") def test_postgres_variants_profiles_and_new_nested_files_are_checked(self): self.compose() for index, image in enumerate(("timescale/timescaledb@sha256:fixture", "timescale/timescaledb-ha:pg17", "postgis/postgis:17", "bitnami/postgresql:17", "pgvector/pgvector:pg17", "${DB_IMAGE:-postgres:17}")): self.write(f"stacks/{index}/anything.yaml", f"services:\n other:\n" f" image: {image}\n profiles: [optional]\n") report = CHECK.audit(self.root) self.assertEqual(len(report["errors"]), 6) def test_yaml_anchors_and_exporter_client(self): self.write("compose.yml", """x-db: &db image: postgres:17 shm_size: 1g services: database: <<: *db postgres-exporter: image: quay.io/prometheuscommunity/postgres-exporter:latest environment: DATA_SOURCE_NAME: postgresql://db/app backend: image: app:local environment: DATABASE_URL: postgresql://db/app """) report = CHECK.audit(self.root) self.assertEqual(report["errors"], []) self.assertEqual(sum(row["postgres"] for row in report["inventory"]), 1) def test_overlay_cannot_lower_or_reset_size(self): self.compose() for size in ("64m", "null", "!reset null", "${SHM_SIZE}"): with self.subTest(size=size): self.write("compose.override.yml", f"services:\n db:\n shm_size: {size}\n") self.assertEqual(len(CHECK.audit(self.root)["errors"]), 1) self.write("compose.override.yml", "services:\n db:\n profiles: [infra-owned]\n") report = CHECK.audit(self.root) self.assertEqual(report["errors"], []) self.assertIn("unknown", [row["status"] for row in report["inventory"]]) def test_custom_server_command_environment_and_marker(self): self.compose() self.write("custom.yml", """services: command: image: company/database:local command: [postgres, -c, max_parallel_workers_per_gather=0] environment: image: company/database:local environment: [PGDATA=/data] marked: image: company/database:local x-postgres: true """) self.assertEqual(len(CHECK.audit(self.root)["errors"]), 3) def test_ipc_and_dev_shm_mounts_cannot_bypass_size(self): for extra in (" ipc: host\n", " ipc: service:other\n", " ipc: none\n", " volumes: ['/host/shm:/dev/shm']\n", " volumes: [{type: tmpfs, target: /dev/shm}]\n", " tmpfs: ['/dev/shm:size=64m']\n"): with self.subTest(extra=extra): self.compose(extra=extra) self.assertTrue(CHECK.audit(self.root)["errors"]) def test_actions_service_options_are_enforced(self): for options, expected in (("--shm-size=1g", 0), ("--shm-size 1024m", 0), ("--health-interval 5s", 1), ("--shm-size=64m", 1), ("--shm-size=1g --ipc=host", 1), ("--shm-size=1g --tmpfs=/dev/shm:size=64m", 1)): with self.subTest(options=options): self.write(".github/workflows/test.yml", f"""jobs: tests: services: pg: image: timescale/timescaledb:pg17 options: {options} """) self.assertEqual(len(CHECK.audit(self.root)["errors"]), expected) def test_invalid_yaml_and_unsupported_inheritance_fail_closed(self): self.compose() for content in ("services: [broken", "include: [elsewhere.yml]", "services: {db: {extends: other}}"): self.write("other.yml", content) result = subprocess.run(["python3", str(SCRIPT), "--root", str(self.root)], capture_output=True, text=True) self.assertEqual(result.returncode, 1) self.assertIn("ERROR", result.stderr) def test_extends_inherits_shm_and_rejects_unsafe_overrides(self): self.compose() for override in ("", " shm_size: 64m\n", " shm_size: !reset 1g\n", " ipc: host\n"): with self.subTest(override=override): self.write("dev/compose.yml", """ services: local-db: extends: {file: ../compose.yml, service: db} """ + override) rows = CHECK.audit(self.root)["inventory"] derived = next(row for row in rows if row["service"] == "local-db") self.assertTrue(derived["postgres"]) self.assertEqual(derived["status"], "at risk" if override else "safe") def test_extends_keeps_inherited_shm_mount_and_ipc(self): self.compose(extra=" ipc: host\n volumes: ['/tmp/manual:/dev/shm']\n") self.write("derived.yml", """ services: intermediate: extends: {file: compose.yml, service: db} inherited: extends: {service: intermediate} volumes: [fixture:/var/lib/postgresql/data] replaced: extends: {service: intermediate} ipc: private volumes: !override [fixture:/var/lib/postgresql/data] """) rows = {row["service"]: row for row in CHECK.audit(self.root)["inventory"]} self.assertIn("/dev/shm mount masks", rows["inherited"]["reason"]) self.assertIn("IPC bypasses", rows["inherited"]["reason"]) self.assertEqual(rows["replaced"]["status"], "safe") def test_nested_extends_resolves_paths_from_each_declaring_file(self): self.write("shared/policy.yml", "services: {db: {image: 'postgres:17', shm_size: 1g}}") self.write("stacks/layer.yml", "services: {middle: {extends: {file: ../shared/policy.yml, service: db}}}") self.write("compose.yml", "services: {local: {extends: {file: stacks/layer.yml, service: middle}}}") report = CHECK.audit(self.root) self.assertEqual(report["errors"], []) self.assertTrue(all(row["postgres"] and row["status"] == "safe" for row in report["inventory"])) def test_extends_rejects_cycles_missing_refs_and_ignored_files(self): self.compose() self.write(".gitignore", "ignored.yml\n") self.write("ignored.yml", "services: {db: {image: 'postgres:17', shm_size: 1g}}") for reference in ("{service: child}", "{service: absent}", "{file: ignored.yml, service: db}", "{file: ../compose.yml, service: db}"): with self.subTest(reference=reference): self.write("derived.yml", f"services: {{child: {{extends: {reference}}}}}") with self.assertRaisesRegex(ValueError, "circular extends|missing Git-visible"): CHECK.audit(self.root) def test_empty_inventory_is_an_error(self): self.assertIn("no PostgreSQL", CHECK.audit(self.root)["errors"][0]) if __name__ == "__main__": unittest.main()