118 lines
5.7 KiB
Python
118 lines
5.7 KiB
Python
import copy
|
|
import os
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
import json
|
|
|
|
import stack
|
|
|
|
|
|
class StackTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.before = {"postgres": {"id": "owned", "started_at": "before",
|
|
"running": True, "volumes": ["owned_data"]}}
|
|
|
|
def test_noop_restart_cannot_pass(self):
|
|
with self.assertRaises(stack.CheckFailure):
|
|
stack.validate_transition("restart", self.before, copy.deepcopy(self.before))
|
|
|
|
def test_changed_volume_cannot_pass_restart(self):
|
|
after = copy.deepcopy(self.before)
|
|
after["postgres"].update(started_at="after", volumes=["other_data"])
|
|
with self.assertRaises(stack.CheckFailure):
|
|
stack.validate_transition("restart", self.before, after)
|
|
|
|
def test_stopped_container_cannot_pass_restart(self):
|
|
after = copy.deepcopy(self.before)
|
|
after["postgres"].update(started_at="after", running=False)
|
|
with self.assertRaises(stack.CheckFailure):
|
|
stack.validate_transition("restart", self.before, after)
|
|
|
|
def test_actual_stop_and_start_preserve_the_same_volume(self):
|
|
stopped = copy.deepcopy(self.before)
|
|
stopped["postgres"]["running"] = False
|
|
stack.validate_transition("stop-db", self.before, stopped)
|
|
started = copy.deepcopy(self.before)
|
|
started["postgres"]["started_at"] = "after"
|
|
stack.validate_transition("start-db", stopped, started)
|
|
|
|
def test_cache_stop_and_restart_require_real_changes(self):
|
|
before = {"redis": self.before["postgres"]}
|
|
stopped = copy.deepcopy(before)
|
|
stopped["redis"]["running"] = False
|
|
stack.validate_transition("stop-cache", before, stopped)
|
|
started = copy.deepcopy(before)
|
|
started["redis"]["started_at"] = "after"
|
|
stack.validate_transition("start-cache", stopped, started)
|
|
stack.validate_transition("restart-cache", before, started)
|
|
with self.assertRaises(stack.CheckFailure):
|
|
stack.validate_transition("restart-cache", before, copy.deepcopy(before))
|
|
|
|
def test_two_api_replicas_must_both_restart_without_recreation(self):
|
|
before = {"api": [dict(self.before["postgres"], id="api-1", volumes=[]),
|
|
dict(self.before["postgres"], id="api-2", volumes=[])]}
|
|
after = copy.deepcopy(before)
|
|
for item in after["api"]:
|
|
item["started_at"] = "after"
|
|
stack.validate_transition("restart", before, after)
|
|
partial = copy.deepcopy(after)
|
|
partial["api"][1]["started_at"] = "before"
|
|
with self.assertRaises(stack.CheckFailure):
|
|
stack.validate_transition("restart", before, partial)
|
|
replaced = copy.deepcopy(after)
|
|
replaced["api"][1]["id"] = "new-api-2"
|
|
with self.assertRaises(stack.CheckFailure):
|
|
stack.validate_transition("restart", before, replaced)
|
|
|
|
def test_snapshot_collects_all_api_replicas_and_checks_project(self):
|
|
def fake_command(argv, **_):
|
|
if argv[-3:] == ["ps", "-aq", "api"]:
|
|
return "api-2\napi-1\n"
|
|
if argv[:2] == ["docker", "inspect"]:
|
|
identifier = argv[2]
|
|
return json.dumps([{"Id": identifier,
|
|
"Config": {"Labels": {"com.docker.compose.project": "isolated",
|
|
"com.docker.compose.service": "api"}},
|
|
"State": {"StartedAt": "time-" + identifier, "Running": True},
|
|
"Mounts": []}])
|
|
self.fail("unexpected Docker command")
|
|
with patch.object(stack, "command", side_effect=fake_command):
|
|
result = stack.snapshot(["docker", "compose"], {"COMPOSE_PROJECT_NAME": "isolated"}, ["api"])
|
|
self.assertEqual([item["id"] for item in result["api"]], ["api-1", "api-2"])
|
|
|
|
def test_lifecycle_evidence_records_actual_states_without_hook_stdout(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
path = Path(directory) / "lifecycle.jsonl"
|
|
after = copy.deepcopy(self.before)
|
|
after["postgres"]["started_at"] = "after"
|
|
stack.record_lifecycle(path, "restart", "isolated", self.before, after,
|
|
source="teacher-persistence")
|
|
proof = json.loads(path.read_text())
|
|
self.assertEqual(proof["source"], "teacher-persistence")
|
|
self.assertEqual(proof["project"], "isolated")
|
|
self.assertEqual(proof["after"]["postgres"]["started_at"], "after")
|
|
|
|
def test_skipped_or_empty_go_run_cannot_pass(self):
|
|
for output in ("", '{"Action":"skip","Test":"TestStorage"}',
|
|
'{"Action":"pass","Test":"TestStorage"}\n{"Action":"skip","Test":"TestStorage/subcase"}',
|
|
'{"Action":"pass","Test":"TestOne"}\n{"Action":"skip","Test":"TestStorage"}'):
|
|
with self.subTest(output=output), self.assertRaises(stack.CheckFailure):
|
|
stack.go_results(output)
|
|
|
|
def test_project_must_be_explicit_before_any_docker_action(self):
|
|
for project in ("", "bad project", "../other", "-option"):
|
|
with self.subTest(project=project), self.assertRaises(stack.CheckFailure):
|
|
stack.compose_base({"COMPOSE_PROJECT_NAME": project})
|
|
|
|
def test_compose_paths_with_spaces_remain_single_arguments(self):
|
|
with tempfile.TemporaryDirectory(prefix="compose files ") as directory:
|
|
path = str(Path(directory) / "override.yaml")
|
|
args = stack.compose_base({"COMPOSE_PROJECT_NAME": "lab2-test-paths", "COMPOSE_FILE": path})
|
|
self.assertEqual(args[-2:], ["-f", str(Path(path).resolve())])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main(verbosity=2)
|