| 1 |
|
| 2 |
|
| 3 |
import json |
| 4 |
import os |
| 5 |
import unittest |
| 6 |
from datetime import timedelta |
| 7 |
from io import BytesIO |
| 8 |
from typing import Final |
| 9 |
|
| 10 |
from packaging import version |
| 11 |
|
| 12 |
from tests.integration.test_utils import EnsureHealthcheckRun |
| 13 |
from tests.integration.test_utils import ExecutionTime |
| 14 |
from tests.integration.test_utils import RunSubprocessMixin |
| 15 |
from tests.integration.test_utils import get_podman_version |
| 16 |
from tests.integration.test_utils import podman_compose_path |
| 17 |
from tests.integration.test_utils import test_path |
| 18 |
|
| 19 |
EXECUTION_TIMEOUT: Final[float] = 30 |
| 20 |
|
| 21 |
|
| 22 |
def compose_yaml_path() -> str: |
| 23 |
return os.path.join(test_path(), "wait", "docker-compose.yml") |
| 24 |
|
| 25 |
|
| 26 |
@unittest.skipIf( |
| 27 |
get_podman_version() < version.parse("4.6.0"), "Wait feature not supported below 4.6.0." |
| 28 |
) |
| 29 |
class TestComposeWait(unittest.TestCase, RunSubprocessMixin): |
| 30 |
def setUp(self) -> None: |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
self.run_subprocess_assert_returncode([ |
| 35 |
podman_compose_path(), |
| 36 |
"-f", |
| 37 |
compose_yaml_path(), |
| 38 |
"build", |
| 39 |
]) |
| 40 |
|
| 41 |
def _get_health_status(self, container_name: str) -> str: |
| 42 |
output, _ = self.run_subprocess_assert_returncode([ |
| 43 |
"podman", |
| 44 |
"inspect", |
| 45 |
container_name, |
| 46 |
]) |
| 47 |
inspect = json.load(BytesIO(output)) |
| 48 |
|
| 49 |
self.assertEqual(len(inspect), 1) |
| 50 |
self.assertIn("State", inspect[0]) |
| 51 |
self.assertIn("Health", inspect[0]["State"]) |
| 52 |
self.assertIn("Status", inspect[0]["State"]["Health"]) |
| 53 |
|
| 54 |
return inspect[0]["State"]["Health"]["Status"] |
| 55 |
|
| 56 |
def _is_running(self, container_name: str) -> bool: |
| 57 |
output, _ = self.run_subprocess_assert_returncode([ |
| 58 |
"podman", |
| 59 |
"inspect", |
| 60 |
container_name, |
| 61 |
]) |
| 62 |
inspect = json.load(BytesIO(output)) |
| 63 |
|
| 64 |
self.assertEqual(len(inspect), 1) |
| 65 |
self.assertIn("State", inspect[0]) |
| 66 |
self.assertIn("Running", inspect[0]["State"]) |
| 67 |
return inspect[0]["State"]["Running"] |
| 68 |
|
| 69 |
def _compose_down(self) -> None: |
| 70 |
self.run_subprocess_assert_returncode([ |
| 71 |
podman_compose_path(), |
| 72 |
"-f", |
| 73 |
compose_yaml_path(), |
| 74 |
"down", |
| 75 |
"-t", |
| 76 |
"0", |
| 77 |
]) |
| 78 |
|
| 79 |
def test_without_wait(self) -> None: |
| 80 |
try: |
| 81 |
with EnsureHealthcheckRun( |
| 82 |
runner=self, test_case=self, container_name="wait_app_health_1" |
| 83 |
): |
| 84 |
|
| 85 |
|
| 86 |
with ExecutionTime(max_execution_time=timedelta(seconds=10)): |
| 87 |
self.run_subprocess_assert_returncode([ |
| 88 |
podman_compose_path(), |
| 89 |
"-f", |
| 90 |
compose_yaml_path(), |
| 91 |
"up", |
| 92 |
"-d", |
| 93 |
]) |
| 94 |
|
| 95 |
output, _ = self.run_subprocess_assert_returncode([ |
| 96 |
podman_compose_path(), |
| 97 |
"-f", |
| 98 |
compose_yaml_path(), |
| 99 |
"ps", |
| 100 |
]) |
| 101 |
self.assertIn(b"wait_app_health_1", output) |
| 102 |
self.assertIn(b"wait_app_1", output) |
| 103 |
|
| 104 |
self.assertTrue(self._is_running("wait_app_1")) |
| 105 |
|
| 106 |
health = self._get_health_status("wait_app_health_1") |
| 107 |
self.assertEqual(health, "starting") |
| 108 |
finally: |
| 109 |
self._compose_down() |
| 110 |
|
| 111 |
def test_wait(self) -> None: |
| 112 |
try: |
| 113 |
with EnsureHealthcheckRun( |
| 114 |
runner=self, test_case=self, container_name="wait_app_health_1" |
| 115 |
): |
| 116 |
|
| 117 |
|
| 118 |
with ExecutionTime(min_execution_time=timedelta(seconds=10)): |
| 119 |
self.run_subprocess_assert_returncode( |
| 120 |
[ |
| 121 |
podman_compose_path(), |
| 122 |
"-f", |
| 123 |
compose_yaml_path(), |
| 124 |
"up", |
| 125 |
"-d", |
| 126 |
"--wait", |
| 127 |
], |
| 128 |
timeout=EXECUTION_TIMEOUT, |
| 129 |
) |
| 130 |
|
| 131 |
output, _ = self.run_subprocess_assert_returncode([ |
| 132 |
podman_compose_path(), |
| 133 |
"-f", |
| 134 |
compose_yaml_path(), |
| 135 |
"ps", |
| 136 |
]) |
| 137 |
self.assertIn(b"wait_app_health_1", output) |
| 138 |
self.assertIn(b"wait_app_1", output) |
| 139 |
|
| 140 |
self.assertTrue(self._is_running("wait_app_1")) |
| 141 |
|
| 142 |
health = self._get_health_status("wait_app_health_1") |
| 143 |
self.assertEqual(health, "healthy") |
| 144 |
finally: |
| 145 |
self._compose_down() |
| 146 |
|
| 147 |
def test_wait_with_timeout(self) -> None: |
| 148 |
try: |
| 149 |
with EnsureHealthcheckRun( |
| 150 |
runner=self, test_case=self, container_name="wait_app_health_1" |
| 151 |
): |
| 152 |
|
| 153 |
with ExecutionTime( |
| 154 |
min_execution_time=timedelta(seconds=5), |
| 155 |
max_execution_time=timedelta(seconds=10), |
| 156 |
): |
| 157 |
self.run_subprocess_assert_returncode( |
| 158 |
[ |
| 159 |
podman_compose_path(), |
| 160 |
"-f", |
| 161 |
compose_yaml_path(), |
| 162 |
"up", |
| 163 |
"-d", |
| 164 |
"--wait", |
| 165 |
"--wait-timeout", |
| 166 |
"5", |
| 167 |
], |
| 168 |
timeout=EXECUTION_TIMEOUT, |
| 169 |
) |
| 170 |
|
| 171 |
output, _ = self.run_subprocess_assert_returncode([ |
| 172 |
podman_compose_path(), |
| 173 |
"-f", |
| 174 |
compose_yaml_path(), |
| 175 |
"ps", |
| 176 |
]) |
| 177 |
self.assertIn(b"wait_app_health_1", output) |
| 178 |
self.assertIn(b"wait_app_1", output) |
| 179 |
|
| 180 |
self.assertTrue(self._is_running("wait_app_1")) |
| 181 |
|
| 182 |
health = self._get_health_status("wait_app_health_1") |
| 183 |
self.assertEqual(health, "starting") |
| 184 |
finally: |
| 185 |
self._compose_down() |
| 186 |
|
| 187 |
def test_wait_with_start(self) -> None: |
| 188 |
try: |
| 189 |
with EnsureHealthcheckRun( |
| 190 |
runner=self, test_case=self, container_name="wait_app_health_1" |
| 191 |
): |
| 192 |
|
| 193 |
|
| 194 |
with ExecutionTime(max_execution_time=timedelta(seconds=10)): |
| 195 |
|
| 196 |
|
| 197 |
self.run_subprocess_assert_returncode([ |
| 198 |
podman_compose_path(), |
| 199 |
"-f", |
| 200 |
compose_yaml_path(), |
| 201 |
"up", |
| 202 |
"-d", |
| 203 |
]) |
| 204 |
self.run_subprocess_assert_returncode([ |
| 205 |
podman_compose_path(), |
| 206 |
"-f", |
| 207 |
compose_yaml_path(), |
| 208 |
"kill", |
| 209 |
"--all", |
| 210 |
]) |
| 211 |
|
| 212 |
output, _ = self.run_subprocess_assert_returncode([ |
| 213 |
podman_compose_path(), |
| 214 |
"-f", |
| 215 |
compose_yaml_path(), |
| 216 |
"ps", |
| 217 |
]) |
| 218 |
self.assertIn(b"wait_app_health_1", output) |
| 219 |
self.assertIn(b"wait_app_1", output) |
| 220 |
|
| 221 |
self.assertFalse(self._is_running("wait_app_health_1")) |
| 222 |
self.assertFalse(self._is_running("wait_app_1")) |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
with ExecutionTime(min_execution_time=timedelta(seconds=10)): |
| 227 |
self.run_subprocess_assert_returncode( |
| 228 |
[ |
| 229 |
podman_compose_path(), |
| 230 |
"-f", |
| 231 |
compose_yaml_path(), |
| 232 |
"start", |
| 233 |
"--wait", |
| 234 |
], |
| 235 |
timeout=EXECUTION_TIMEOUT, |
| 236 |
) |
| 237 |
|
| 238 |
self.assertTrue(self._is_running("wait_app_1")) |
| 239 |
|
| 240 |
health = self._get_health_status("wait_app_health_1") |
| 241 |
self.assertEqual(health, "healthy") |
| 242 |
finally: |
| 243 |
self._compose_down() |