import copy import json import subprocess import sys import tempfile import unittest from pathlib import Path from core.configuration import load_config from core.preflight import PreflightError, site_preflight from core.inspection_cli import provisioning_plan ROOT = Path(__file__).resolve().parents[2] class PreflightTests(unittest.TestCase): def setUp(self): self.temp = tempfile.TemporaryDirectory() self.addCleanup(self.temp.cleanup) self.root = Path(self.temp.name) self.config = load_config( ROOT / "config/site.example.json" ) self.other = copy.deepcopy(self.config) self.other["site"].update({ "identifier": "other-absdev-net", "domain": "other.absdev.net", "root": "/opt/laravel-sites/other-absdev-net", "origin_port": 8198, }) self.other["docker"]["project"] = ( "site-other-absdev-net" ) self.other["database"]["name"] = "other_db" self.other["database"]["username"] = "other_app" def preflight(self, registry=None): return site_preflight( self.config, [] if registry is None else registry, ) def write_json(self, name, data): path = self.root / name path.write_text(json.dumps(data)) return path def cli(self, *args): return subprocess.run( [ sys.executable, str(ROOT / "bin/absdev-deploy"), *map(str, args), ], cwd=ROOT, capture_output=True, text=True, timeout=10, ) def test_valid_preflight(self): result = self.preflight() self.assertEqual( result["status"], "STATIC_PREFLIGHT_PASSED", ) def test_valid_existing_registry(self): result = self.preflight([self.other]) self.assertEqual(result["site_count"], 2) def test_duplicate_site_rejected(self): with self.assertRaises(PreflightError): self.preflight([self.config]) def test_port_conflict_rejected(self): self.other["site"]["origin_port"] = ( self.config["site"]["origin_port"] ) with self.assertRaises(PreflightError): self.preflight([self.other]) def test_domain_conflict_rejected(self): self.other["site"]["domain"] = ( self.config["site"]["domain"] ) with self.assertRaises(PreflightError): self.preflight([self.other]) def test_database_conflict_rejected(self): self.other["database"]["name"] = ( self.config["database"]["name"] ) with self.assertRaises(PreflightError): self.preflight([self.other]) def test_username_conflict_rejected(self): self.other["database"]["username"] = ( self.config["database"]["username"] ) with self.assertRaises(PreflightError): self.preflight([self.other]) def test_untrusted_site_root_rejected(self): self.config["site"]["root"] = "/etc" with self.assertRaises(PreflightError): self.preflight() def test_invalid_registry_rejected(self): with self.assertRaises(PreflightError): self.preflight("invalid") def test_deterministic_preflight(self): self.assertEqual( self.preflight(), self.preflight(), ) def test_input_not_mutated(self): original = copy.deepcopy(self.config) self.preflight([self.other]) self.assertEqual(self.config, original) def test_runtime_not_verified(self): self.assertFalse( self.preflight()["runtime_verified"] ) def test_execution_disabled(self): self.assertFalse( self.preflight()["execution_permitted"] ) def test_registry_completeness_not_assumed(self): self.assertFalse( self.preflight()["registry_completeness_verified"] ) def test_provision_plan_enforces_site_policy(self): self.config["database"]["username"] = "root" with self.assertRaises(RuntimeError): provisioning_plan(self.config) def test_provision_plan_reports_static_validation(self): result = provisioning_plan(self.config) self.assertTrue(result["static_policy_verified"]) def test_cli_site_policy(self): config = self.write_json( "site.json", self.config, ) result = self.cli( "site-policy", "--config", config, ) self.assertEqual(result.returncode, 0, result.stderr) self.assertEqual( json.loads(result.stdout)["status"], "VALID", ) def test_cli_registry_validation(self): registry = self.write_json( "registry.json", [self.config, self.other], ) result = self.cli( "registry-validate", "--registry", registry, ) self.assertEqual(result.returncode, 0, result.stderr) self.assertEqual( json.loads(result.stdout)["site_count"], 2, ) def test_cli_registry_conflict(self): registry = self.write_json( "registry.json", [self.config, self.config], ) result = self.cli( "registry-validate", "--registry", registry, ) self.assertNotEqual(result.returncode, 0) def test_cli_preflight(self): config = self.write_json("site.json", self.config) registry = self.write_json("registry.json", [self.other]) result = self.cli( "preflight", "--config", config, "--registry", registry, ) self.assertEqual(result.returncode, 0, result.stderr) report = json.loads(result.stdout) self.assertEqual( report["status"], "STATIC_PREFLIGHT_PASSED", ) self.assertFalse(report["execution_permitted"]) def test_cli_preflight_requires_registry(self): config = self.write_json("site.json", self.config) result = self.cli( "preflight", "--config", config, ) self.assertNotEqual(result.returncode, 0) def test_cli_missing_registry_rejected(self): result = self.cli( "registry-validate", "--registry", self.root / "missing.json", ) self.assertNotEqual(result.returncode, 0) def test_cli_capabilities_updated(self): result = self.cli("capabilities") self.assertEqual(result.returncode, 0) report = json.loads(result.stdout) for command in ( "site-policy", "registry-validate", "preflight", ): self.assertIn(command, report["commands"]) def test_cli_provisioning_rejects_unsafe_site(self): self.config["site"]["root"] = "/etc" config = self.write_json( "site.json", self.config, ) result = self.cli( "provision-plan", "--config", config, ) self.assertNotEqual(result.returncode, 0) def test_mutating_commands_remain_unavailable(self): for command in ( "deploy", "migrate", "rollback", "restore", "provision", "delete", ): with self.subTest(command=command): self.assertNotEqual( self.cli(command).returncode, 0, ) if __name__ == "__main__": unittest.main()