forked from mtusi2026/mtusi2026lab2
Завершить PostgreSQL, Redis-сессии, миграции, таймауты и диагностику. Зафиксировать собственные тесты и доказательства финального прогона: 43 PASS, 0 FAIL, 0 SKIP; 768 HTTP-запросов без ошибок. Добавить официальный отчёт с запуском, архитектурой, SQL-планами и ограничениями. PR отложен до завершения уровня 5.
72 lines
3.2 KiB
Python
72 lines
3.2 KiB
Python
import copy
|
|
import os
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
|
|
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_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)
|