265 lines
6.6 KiB
Python
265 lines
6.6 KiB
Python
import copy
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
from pathlib import Path
|
|
|
|
from core.inspection_cli import (
|
|
inventory_report,
|
|
provisioning_plan,
|
|
capabilities,
|
|
)
|
|
|
|
from core.configuration import load_config
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
|
|
|
|
class InspectionCLITests(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
self.temp = tempfile.TemporaryDirectory()
|
|
self.addCleanup(self.temp.cleanup)
|
|
|
|
self.root = Path(self.temp.name)
|
|
self.app = self.root / "app"
|
|
|
|
self.app.mkdir()
|
|
(self.app / "releases").mkdir()
|
|
|
|
for release in ("1", "2", "3"):
|
|
(self.app / "releases" / release).mkdir()
|
|
|
|
(self.app / "current").symlink_to("releases/3")
|
|
|
|
self.config = load_config(
|
|
ROOT / "config/site.example.json"
|
|
)
|
|
|
|
def cli(self, *args):
|
|
return subprocess.run(
|
|
[
|
|
sys.executable,
|
|
str(ROOT / "bin/absdev-deploy"),
|
|
*args,
|
|
],
|
|
cwd=ROOT,
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=10,
|
|
)
|
|
|
|
def test_inventory(self):
|
|
result = inventory_report(self.app)
|
|
|
|
self.assertEqual(result["inventory"], ["1", "2", "3"])
|
|
self.assertEqual(result["current"], "3")
|
|
|
|
def test_failed_release_inventory(self):
|
|
(
|
|
self.app / "releases/1/BAD_RELEASE"
|
|
).touch()
|
|
|
|
self.assertEqual(
|
|
inventory_report(self.app)["failed"],
|
|
["1"],
|
|
)
|
|
|
|
def test_failed_current_rejected(self):
|
|
(
|
|
self.app / "releases/3/BAD_RELEASE"
|
|
).touch()
|
|
|
|
with self.assertRaises(RuntimeError):
|
|
inventory_report(self.app)
|
|
|
|
def test_inventory_is_read_only(self):
|
|
before = sorted(
|
|
str(path.relative_to(self.app))
|
|
for path in self.app.rglob("*")
|
|
)
|
|
|
|
inventory_report(self.app)
|
|
|
|
after = sorted(
|
|
str(path.relative_to(self.app))
|
|
for path in self.app.rglob("*")
|
|
)
|
|
|
|
self.assertEqual(before, after)
|
|
|
|
def test_inventory_does_not_authorise_execution(self):
|
|
self.assertFalse(
|
|
inventory_report(self.app)["execution_permitted"]
|
|
)
|
|
|
|
def test_provisioning_plan(self):
|
|
result = provisioning_plan(self.config)
|
|
|
|
self.assertEqual(result["status"], "PLAN_ONLY")
|
|
|
|
self.assertEqual(
|
|
result["site"],
|
|
self.config["site"]["identifier"],
|
|
)
|
|
|
|
def test_provisioning_does_not_mutate_config(self):
|
|
original = copy.deepcopy(self.config)
|
|
|
|
provisioning_plan(self.config)
|
|
|
|
self.assertEqual(self.config, original)
|
|
|
|
def test_provisioning_deterministic(self):
|
|
self.assertEqual(
|
|
provisioning_plan(self.config),
|
|
provisioning_plan(self.config),
|
|
)
|
|
|
|
def test_provisioning_execution_disabled(self):
|
|
result = provisioning_plan(self.config)
|
|
|
|
self.assertFalse(result["execution_permitted"])
|
|
self.assertFalse(result["database_creation_permitted"])
|
|
|
|
def test_provisioning_database_driver(self):
|
|
result = provisioning_plan(self.config)
|
|
|
|
self.assertEqual(
|
|
result["database"]["driver"],
|
|
self.config["database"]["driver"],
|
|
)
|
|
|
|
def test_capabilities(self):
|
|
result = capabilities()
|
|
|
|
self.assertFalse(result["production_ready"])
|
|
self.assertFalse(result["deployment_execution_enabled"])
|
|
|
|
def test_cli_capabilities(self):
|
|
result = self.cli("capabilities")
|
|
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
self.assertEqual(json.loads(result.stdout), capabilities())
|
|
|
|
def test_cli_inventory(self):
|
|
result = self.cli(
|
|
"inventory",
|
|
"--app",
|
|
str(self.app),
|
|
)
|
|
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
|
|
self.assertEqual(
|
|
json.loads(result.stdout)["current"],
|
|
"3",
|
|
)
|
|
|
|
def test_cli_provisioning(self):
|
|
result = self.cli(
|
|
"provision-plan",
|
|
"--config",
|
|
str(ROOT / "config/site.example.json"),
|
|
)
|
|
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
|
|
self.assertEqual(
|
|
json.loads(result.stdout)["status"],
|
|
"PLAN_ONLY",
|
|
)
|
|
|
|
def test_cli_missing_config(self):
|
|
result = self.cli(
|
|
"provision-plan",
|
|
"--config",
|
|
str(self.root / "missing.json"),
|
|
)
|
|
|
|
self.assertNotEqual(result.returncode, 0)
|
|
|
|
def test_cli_retention_plan(self):
|
|
policy = self.root / "retention.json"
|
|
|
|
policy.write_text(json.dumps({
|
|
"inventory": ["1", "2", "3", "4"],
|
|
"current": "4",
|
|
"keep": 2,
|
|
"protected": [],
|
|
"failed": [],
|
|
"unresolved": [],
|
|
}))
|
|
|
|
result = self.cli(
|
|
"retention-plan",
|
|
"--policy",
|
|
str(policy),
|
|
)
|
|
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
|
|
report = json.loads(result.stdout)
|
|
|
|
self.assertEqual(report["archive_candidates"], ["1", "2"])
|
|
self.assertFalse(report["execution_permitted"])
|
|
|
|
def test_cli_retention_invalid_json(self):
|
|
policy = self.root / "retention.json"
|
|
policy.write_text("{invalid")
|
|
|
|
result = self.cli(
|
|
"retention-plan",
|
|
"--policy",
|
|
str(policy),
|
|
)
|
|
|
|
self.assertNotEqual(result.returncode, 0)
|
|
|
|
def test_cli_duplicate_json_fields(self):
|
|
policy = self.root / "retention.json"
|
|
policy.write_text('{"current":"1","current":"2"}')
|
|
|
|
result = self.cli(
|
|
"retention-plan",
|
|
"--policy",
|
|
str(policy),
|
|
)
|
|
|
|
self.assertNotEqual(result.returncode, 0)
|
|
|
|
def test_original_validation_preserved(self):
|
|
result = self.cli(
|
|
"validate",
|
|
"--config",
|
|
str(ROOT / "config/site.example.json"),
|
|
)
|
|
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
|
|
def test_cli_help(self):
|
|
result = self.cli("--help")
|
|
|
|
self.assertEqual(result.returncode, 0)
|
|
self.assertIn("validate", result.stdout)
|
|
self.assertIn("inventory", result.stdout)
|
|
self.assertIn("provision-plan", result.stdout)
|
|
|
|
def test_unknown_command_rejected(self):
|
|
result = self.cli("unknown-command")
|
|
|
|
self.assertNotEqual(result.returncode, 0)
|
|
|
|
def test_live_deployment_command_disabled(self):
|
|
result = self.cli("deploy")
|
|
|
|
self.assertNotEqual(result.returncode, 0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|