|
| 1 | +import importlib.util |
| 2 | +from pathlib import Path |
| 3 | +import sys |
| 4 | +from types import SimpleNamespace |
| 5 | +import unittest |
| 6 | +from unittest.mock import patch |
| 7 | + |
| 8 | + |
| 9 | +MODULE_PATH = Path(__file__).resolve().parent / "system-test-runner.py" |
| 10 | +SPEC = importlib.util.spec_from_file_location("system_test_runner", MODULE_PATH) |
| 11 | +SYSTEM_TEST_RUNNER = importlib.util.module_from_spec(SPEC) |
| 12 | +sys.modules.setdefault("requests", SimpleNamespace(head=None)) |
| 13 | +sys.modules[SPEC.name] = SYSTEM_TEST_RUNNER |
| 14 | +assert SPEC.loader is not None |
| 15 | +SPEC.loader.exec_module(SYSTEM_TEST_RUNNER) |
| 16 | + |
| 17 | + |
| 18 | +class SystemTestRunnerTests(unittest.TestCase): |
| 19 | + def create_runner(self): |
| 20 | + with patch.object(SYSTEM_TEST_RUNNER.SystemTestRunner, "read_pid_file", return_value=None): |
| 21 | + return SYSTEM_TEST_RUNNER.SystemTestRunner() |
| 22 | + |
| 23 | + def test_wait_for_spring_requires_actuator_health(self): |
| 24 | + runner = self.create_runner() |
| 25 | + |
| 26 | + def fake_head(url, auth, timeout): |
| 27 | + self.assertEqual(url, "http://localhost:8080/actuator/health") |
| 28 | + return SimpleNamespace(status_code=503) |
| 29 | + |
| 30 | + with patch.object(SYSTEM_TEST_RUNNER.requests, "head", side_effect=fake_head), patch.object( |
| 31 | + SYSTEM_TEST_RUNNER.time, "sleep" |
| 32 | + ): |
| 33 | + self.assertFalse(runner.wait_for_spring(max_attempts=1)) |
| 34 | + |
| 35 | + def test_get_spring_status_only_reports_http_ready_for_actuator_health(self): |
| 36 | + runner = self.create_runner() |
| 37 | + runner.spring_server.pid = 123 |
| 38 | + |
| 39 | + def fake_head(url, auth, timeout): |
| 40 | + self.assertEqual(url, "http://localhost:8080/actuator/health") |
| 41 | + return SimpleNamespace(status_code=404) |
| 42 | + |
| 43 | + with patch.object(SYSTEM_TEST_RUNNER.requests, "head", side_effect=fake_head), patch.object( |
| 44 | + runner, "is_process_running", return_value=True |
| 45 | + ): |
| 46 | + status = runner.get_spring_status() |
| 47 | + |
| 48 | + self.assertTrue(status["process_running"]) |
| 49 | + self.assertFalse(status["http_ready"]) |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == "__main__": |
| 53 | + unittest.main() |
0 commit comments