Files
planet/scripts/harness/test_backend_runtime_assets.py
2026-09-23 21:20:12 +08:00

55 lines
2.5 KiB
Python

"""Verify production COPY rules and .dockerignore without installing an image stack."""
import json
import os
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[2]
POLICY = "config/earth-boundary-pov-policy.china-v1.json"
@unittest.skipUnless(
os.environ.get("PLANET_HARNESS_DOCKER_SMOKE") == "1",
"Docker packaging checks are opt-in",
)
class BackendRuntimeAssetsTests(unittest.TestCase):
def test_production_copy_rules_include_the_policy_and_exclude_secrets(self):
copy_rules = [
line for line in (ROOT / "backend/Dockerfile").read_text().splitlines()
if line.startswith(("COPY backend ", "COPY config/", "COPY scripts/", "COPY resources/", "COPY VERSION "))
]
with tempfile.TemporaryDirectory(prefix="planet-runtime-assets-") as directory:
temporary = Path(directory)
dockerfile = temporary / "Dockerfile"
dockerfile.write_text("FROM scratch\n" + "\n".join(copy_rules) + "\n")
exported = temporary / "exported"
result = subprocess.run(
["docker", "build", "--file", str(dockerfile), "--output",
f"type=local,dest={exported}", str(ROOT)],
capture_output=True, text=True, timeout=120,
)
self.assertEqual(result.returncode, 0, result.stderr)
deployed_policy = exported / "app" / POLICY
self.assertTrue(deployed_policy.is_file(), "Backend startup requires the shared policy")
self.assertEqual(deployed_policy.read_bytes(), (ROOT / POLICY).read_bytes())
self.assertEqual(json.loads(deployed_policy.read_text())["profile"], "china-pov-v1")
self.assertTrue((exported / "app/backend/app/services/compute_center_verified_sites.json").is_file())
self.assertFalse((exported / "app/backend/.env").exists())
self.assertFalse((exported / "app/config/earth-boundary-sources.local.json").exists())
self.assertTrue((exported / "app/resources/earth/boundary-pov-geometry.china-v1.json").is_file())
for builder in ("build_earth_boundary_china_pov_geojson.py", "build_earth_boundary_pmtiles.py"):
probe = subprocess.run(
[sys.executable, str(exported / "app/scripts" / builder), "--help"],
capture_output=True, text=True, timeout=10,
)
self.assertEqual(probe.returncode, 0, probe.stderr)
if __name__ == "__main__":
unittest.main()