Debian Codegraph @

tests/integration/healthcheck/test_podman_compose_healthcheck.py

raw symbols SCIP
1 import json
2 import os
3 import time
4 import unittest
5 from datetime import timedelta
6 from io import BytesIO
7 from pathlib import Path
8
9 from tests.integration.test_utils import EnsureHealthcheckRun
10 from tests.integration.test_utils import ExecutionTime
11 from tests.integration.test_utils import RunSubprocessMixin
12 from tests.integration.test_utils import podman_compose_path
13 from tests.integration.test_utils import test_path
14
15
16 def compose_yaml_path() -> str:
17 return os.path.join(test_path(), "healthcheck", "docker-compose.yml")
18
19
20 class TestComposeHealthcheck(unittest.TestCase, RunSubprocessMixin):
21 def _is_running(self, container_name: str) -> bool:
22 output, _ = self.run_subprocess_assert_returncode([
23 "podman",
24 "inspect",
25 container_name,
26 ])
27 inspect = json.load(BytesIO(output))
28
29 self.assertEqual(len(inspect), 1)
30 self.assertIn("State", inspect[0])
31 self.assertIn("Running", inspect[0]["State"])
32 return inspect[0]["State"]["Running"]
33
34 def _get_health_status(self, container_name: str) -> str:
35 output, _ = self.run_subprocess_assert_returncode([
36 "podman",
37 "inspect",
38 container_name,
39 ])
40 inspect = json.load(BytesIO(output))
41
42 self.assertEqual(len(inspect), 1)
43 self.assertIn("State", inspect[0])
44 self.assertIn("Health", inspect[0]["State"])
45 self.assertIn("Status", inspect[0]["State"]["Health"])
46
47 return inspect[0]["State"]["Health"]["Status"]
48
49 def _create_file_for_health_test(self, file_path: str) -> Path:
50 path = Path(file_path)
51 path.parent.mkdir(parents=True, exist_ok=True)
52 path.touch(exist_ok=True)
53 return path
54
55 def _assert_status(self, expected: str, container_name: str = "healthcheck_app_1") -> str:
56 status = "unknown"
57 # Total test time is ~10s: grace period (~6s) + healthcheck duration (~4s)
58 # Healthcheck timing: interval 1s + timeout 1s per attempt, with 2 retries
59 for _ in range(20):
60 status = self._get_health_status(container_name)
61 if status == expected:
62 return status
63 time.sleep(0.5)
64 return status
65
66 # test if healthcheck is able to change health status: starting -> healthy -> unhealthy
67 def test_healthcheck(self) -> None:
68 try:
69 with EnsureHealthcheckRun(
70 runner=self, test_case=self, container_name="healthcheck_app_1"
71 ):
72 # the execution time of this command must be not more than 10 seconds
73 # otherwise this test case makes no sense
74 with ExecutionTime(max_execution_time=timedelta(seconds=10)):
75 self.run_subprocess_assert_returncode([
76 podman_compose_path(),
77 "-f",
78 compose_yaml_path(),
79 "up",
80 "-d",
81 ])
82
83 output, _ = self.run_subprocess_assert_returncode([
84 podman_compose_path(),
85 "-f",
86 compose_yaml_path(),
87 "ps",
88 ])
89 self.assertIn(b"healthcheck_app_1", output)
90 self.assertTrue(self._is_running("healthcheck_app_1"))
91
92 health = self._get_health_status("healthcheck_app_1")
93 self.assertEqual(health, "starting")
94
95 # condition for a container to become 'healthy'
96 file_path = os.path.join(test_path(), "healthcheck", "health-trigger", "healthy")
97 path = self._create_file_for_health_test(file_path)
98
99 health_status = self._assert_status("healthy")
100 self.assertEqual(health_status, "healthy")
101
102 # condition for a container to become 'unhealthy'
103 path.unlink()
104 health_status = self._assert_status("unhealthy")
105 self.assertEqual(health_status, "unhealthy")
106 finally:
107 self.run_subprocess_assert_returncode([
108 podman_compose_path(),
109 "-f",
110 compose_yaml_path(),
111 "down",
112 "-t",
113 "0",
114 ])