| 1 |
|
| 2 |
|
| 3 |
import json |
| 4 |
import os |
| 5 |
import unittest |
| 6 |
from typing import Any |
| 7 |
|
| 8 |
from packaging import version |
| 9 |
from parameterized import parameterized |
| 10 |
|
| 11 |
from tests.integration.test_utils import RunSubprocessMixin |
| 12 |
from tests.integration.test_utils import get_podman_version |
| 13 |
from tests.integration.test_utils import podman_compose_path |
| 14 |
from tests.integration.test_utils import test_path |
| 15 |
|
| 16 |
|
| 17 |
def compose_yaml_path(scenario: str) -> str: |
| 18 |
return os.path.join( |
| 19 |
os.path.join(test_path(), "compose_up_behavior"), f"docker-compose_{scenario}.yaml" |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
class TestComposeUpBehavior(unittest.TestCase, RunSubprocessMixin): |
| 24 |
def get_existing_containers(self, scenario: str) -> dict[str, Any]: |
| 25 |
out, _ = self.run_subprocess_assert_returncode( |
| 26 |
[ |
| 27 |
podman_compose_path(), |
| 28 |
"-f", |
| 29 |
compose_yaml_path(scenario), |
| 30 |
"ps", |
| 31 |
"--format", |
| 32 |
'json', |
| 33 |
], |
| 34 |
) |
| 35 |
containers = json.loads(out) |
| 36 |
return { |
| 37 |
c.get("Names")[0]: { |
| 38 |
"name": c.get("Names")[0], |
| 39 |
"id": c.get("Id"), |
| 40 |
"service_name": c.get("Labels", {}).get("io.podman.compose.service", ""), |
| 41 |
"config_hash": c.get("Labels", {}).get("io.podman.compose.config-hash", ""), |
| 42 |
"exited": c.get("Exited"), |
| 43 |
} |
| 44 |
for c in containers |
| 45 |
} |
| 46 |
|
| 47 |
@parameterized.expand([ |
| 48 |
( |
| 49 |
"service_change_app", |
| 50 |
"service_change_base", |
| 51 |
["up"], |
| 52 |
{"app"}, |
| 53 |
), |
| 54 |
( |
| 55 |
"service_change_app", |
| 56 |
"service_change_base", |
| 57 |
["up", "app"], |
| 58 |
{"app"}, |
| 59 |
), |
| 60 |
( |
| 61 |
"service_change_app", |
| 62 |
"service_change_base", |
| 63 |
["up", "db"], |
| 64 |
set(), |
| 65 |
), |
| 66 |
( |
| 67 |
"service_change_db", |
| 68 |
"service_change_base", |
| 69 |
["up"], |
| 70 |
{"db", "app"}, |
| 71 |
), |
| 72 |
( |
| 73 |
"service_change_db", |
| 74 |
"service_change_base", |
| 75 |
["up", "app"], |
| 76 |
{"db", "app"}, |
| 77 |
), |
| 78 |
( |
| 79 |
"service_change_db", |
| 80 |
"service_change_base", |
| 81 |
["up", "db"], |
| 82 |
{"db", "app"}, |
| 83 |
), |
| 84 |
]) |
| 85 |
def test_recreate_on_config_changed( |
| 86 |
self, |
| 87 |
change_to: str, |
| 88 |
running_scenario: str, |
| 89 |
command_args: list[str], |
| 90 |
expect_recreated_services: set[str], |
| 91 |
) -> None: |
| 92 |
try: |
| 93 |
self.run_subprocess_assert_returncode( |
| 94 |
[podman_compose_path(), "-f", compose_yaml_path(running_scenario), "up", "-d"], |
| 95 |
) |
| 96 |
|
| 97 |
original_containers = self.get_existing_containers(running_scenario) |
| 98 |
|
| 99 |
out, err = self.run_subprocess_assert_returncode( |
| 100 |
[ |
| 101 |
podman_compose_path(), |
| 102 |
"--verbose", |
| 103 |
"-f", |
| 104 |
compose_yaml_path(change_to), |
| 105 |
*command_args, |
| 106 |
"-d", |
| 107 |
], |
| 108 |
) |
| 109 |
|
| 110 |
new_containers = self.get_existing_containers(change_to) |
| 111 |
recreated_services = { |
| 112 |
c.get("service_name") |
| 113 |
for c in original_containers.values() |
| 114 |
if new_containers.get(c.get("name"), {}).get("id") != c.get("id") |
| 115 |
} |
| 116 |
|
| 117 |
self.assertEqual( |
| 118 |
recreated_services, |
| 119 |
expect_recreated_services, |
| 120 |
msg=f"Expected services to be recreated: {expect_recreated_services}, " |
| 121 |
f"but got: {recreated_services}, containers: " |
| 122 |
f"[{original_containers}, {new_containers}]", |
| 123 |
) |
| 124 |
self.assertTrue( |
| 125 |
all([c.get("exited") is False for c in new_containers.values()]), |
| 126 |
msg="Not all containers are running after up command", |
| 127 |
) |
| 128 |
|
| 129 |
finally: |
| 130 |
self.run_subprocess_assert_returncode([ |
| 131 |
podman_compose_path(), |
| 132 |
"-f", |
| 133 |
compose_yaml_path(change_to), |
| 134 |
"down", |
| 135 |
"-t", |
| 136 |
"0", |
| 137 |
]) |
| 138 |
|
| 139 |
@unittest.skipIf( |
| 140 |
get_podman_version() < version.parse("5.6.0"), |
| 141 |
"The image pull policy feature was only added as of Podman 5.6.0.", |
| 142 |
) |
| 143 |
def test_pull_only_if_image_missing(self) -> None: |
| 144 |
"""Verify image is pulled because default pull policy is --missing""" |
| 145 |
|
| 146 |
compose_file = compose_yaml_path("default_pull_policy") |
| 147 |
image = "docker.io/library/alpine:latest" |
| 148 |
|
| 149 |
try: |
| 150 |
self.run_subprocess_assert_returncode([ |
| 151 |
podman_compose_path(), |
| 152 |
"-f", |
| 153 |
compose_file, |
| 154 |
"up", |
| 155 |
"-d", |
| 156 |
]) |
| 157 |
|
| 158 |
|
| 159 |
self.run_subprocess_assert_returncode([ |
| 160 |
"podman", |
| 161 |
"rmi", |
| 162 |
"-f", |
| 163 |
image, |
| 164 |
]) |
| 165 |
|
| 166 |
|
| 167 |
_, error = self.run_subprocess_assert_returncode([ |
| 168 |
podman_compose_path(), |
| 169 |
"--verbose", |
| 170 |
"-f", |
| 171 |
compose_file, |
| 172 |
"up", |
| 173 |
"-d", |
| 174 |
]) |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
self.assertIn(b"podman pull --policy missing alpine:latest", error) |
| 180 |
|
| 181 |
output, _ = self.run_subprocess_assert_returncode([ |
| 182 |
podman_compose_path(), |
| 183 |
"-f", |
| 184 |
compose_file, |
| 185 |
"ps", |
| 186 |
]) |
| 187 |
self.assertIn(b"compose_up_behavior_test_1", output) |
| 188 |
finally: |
| 189 |
self.run_subprocess_assert_returncode([ |
| 190 |
podman_compose_path(), |
| 191 |
"-f", |
| 192 |
compose_file, |
| 193 |
"down", |
| 194 |
"-t", |
| 195 |
"0", |
| 196 |
]) |
| 197 |
self.run_subprocess_assert_returncode([ |
| 198 |
"podman", |
| 199 |
"rmi", |
| 200 |
"-f", |
| 201 |
image, |
| 202 |
]) |
| 203 |
|
| 204 |
@unittest.skipIf( |
| 205 |
get_podman_version() < version.parse("5.6.0"), |
| 206 |
"The image pull policy feature was only added as of Podman 5.6.0.", |
| 207 |
) |
| 208 |
def test_pull_default_policy_overrides_lower_priority_policy(self) -> None: |
| 209 |
"""Verify pull policy flag with higher priority overrides the default pull |
| 210 |
policy --missing""" |
| 211 |
|
| 212 |
compose_file = compose_yaml_path("override_missing") |
| 213 |
image = "docker.io/library/alpine:latest" |
| 214 |
|
| 215 |
try: |
| 216 |
self.run_subprocess_assert_returncode([ |
| 217 |
podman_compose_path(), |
| 218 |
"-f", |
| 219 |
compose_file, |
| 220 |
"up", |
| 221 |
"-d", |
| 222 |
]) |
| 223 |
|
| 224 |
|
| 225 |
self.run_subprocess_assert_returncode([ |
| 226 |
"podman", |
| 227 |
"rmi", |
| 228 |
"-f", |
| 229 |
image, |
| 230 |
]) |
| 231 |
|
| 232 |
|
| 233 |
_, error = self.run_subprocess_assert_returncode([ |
| 234 |
podman_compose_path(), |
| 235 |
"--verbose", |
| 236 |
"-f", |
| 237 |
compose_file, |
| 238 |
"up", |
| 239 |
"-d", |
| 240 |
]) |
| 241 |
|
| 242 |
|
| 243 |
self.assertIn(b"podman pull --policy always alpine:latest", error) |
| 244 |
finally: |
| 245 |
self.run_subprocess_assert_returncode([ |
| 246 |
podman_compose_path(), |
| 247 |
"-f", |
| 248 |
compose_file, |
| 249 |
"down", |
| 250 |
"-t", |
| 251 |
"0", |
| 252 |
]) |
| 253 |
self.run_subprocess_assert_returncode([ |
| 254 |
"podman", |
| 255 |
"rmi", |
| 256 |
"-f", |
| 257 |
image, |
| 258 |
]) |
| 259 |
|
| 260 |
@unittest.skipIf( |
| 261 |
get_podman_version() < version.parse("5.6.0"), |
| 262 |
"The image pull policy feature was only added as of Podman 5.6.0.", |
| 263 |
) |
| 264 |
def test_localhost_image_not_pulled(self) -> None: |
| 265 |
"""Verify existing localhost/ images are used locally without pulling""" |
| 266 |
|
| 267 |
compose_file = compose_yaml_path("non_existent_localhost_image") |
| 268 |
image = "localhost/test-image:1" |
| 269 |
|
| 270 |
try: |
| 271 |
|
| 272 |
|
| 273 |
self.run_subprocess_assert_returncode(["podman", "pull", "alpine:latest"]) |
| 274 |
self.run_subprocess_assert_returncode(["podman", "tag", "alpine:latest", image]) |
| 275 |
|
| 276 |
output, _ = self.run_subprocess_assert_returncode([ |
| 277 |
podman_compose_path(), |
| 278 |
"-f", |
| 279 |
compose_file, |
| 280 |
"up", |
| 281 |
"-d", |
| 282 |
]) |
| 283 |
|
| 284 |
|
| 285 |
self.run_subprocess_assert_returncode([ |
| 286 |
"podman", |
| 287 |
"rmi", |
| 288 |
"-f", |
| 289 |
image, |
| 290 |
]) |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
_, error = self.run_subprocess_assert_returncode([ |
| 296 |
podman_compose_path(), |
| 297 |
"--verbose", |
| 298 |
"-f", |
| 299 |
compose_file, |
| 300 |
"up", |
| 301 |
"-d", |
| 302 |
]) |
| 303 |
self.assertNotIn(b"Trying to pull", error) |
| 304 |
|
| 305 |
|
| 306 |
output, _ = self.run_subprocess_assert_returncode([ |
| 307 |
"podman", |
| 308 |
"ps", |
| 309 |
"-a", |
| 310 |
"--filter", |
| 311 |
f"ancestor={image}", |
| 312 |
"--format", |
| 313 |
"{{.Image}}", |
| 314 |
]) |
| 315 |
self.assertIn(b"localhost/test-image:1", output) |
| 316 |
finally: |
| 317 |
self.run_subprocess_assert_returncode([ |
| 318 |
podman_compose_path(), |
| 319 |
"-f", |
| 320 |
compose_file, |
| 321 |
"down", |
| 322 |
]) |
| 323 |
self.run_subprocess_assert_returncode([ |
| 324 |
"podman", |
| 325 |
"rmi", |
| 326 |
"-f", |
| 327 |
image, |
| 328 |
]) |
| 329 |
|
| 330 |
@unittest.skipIf( |
| 331 |
get_podman_version() < version.parse("5.6.0"), |
| 332 |
"The image pull policy feature was only added as of Podman 5.6.0.", |
| 333 |
) |
| 334 |
def test_localhost_image_built_if_does_not_exist(self) -> None: |
| 335 |
"""Verify non-existent localhost/ images are built instead of pulling""" |
| 336 |
|
| 337 |
compose_file = compose_yaml_path("build_localhost_image") |
| 338 |
image = "localhost/not-exists" |
| 339 |
|
| 340 |
|
| 341 |
self.run_subprocess_assert_returncode( |
| 342 |
[ |
| 343 |
"podman", |
| 344 |
"image", |
| 345 |
"exists", |
| 346 |
image, |
| 347 |
], |
| 348 |
1, |
| 349 |
) |
| 350 |
|
| 351 |
try: |
| 352 |
self.run_subprocess_assert_returncode([ |
| 353 |
podman_compose_path(), |
| 354 |
"-f", |
| 355 |
compose_file, |
| 356 |
"up", |
| 357 |
"-d", |
| 358 |
]) |
| 359 |
|
| 360 |
|
| 361 |
self.run_subprocess_assert_returncode([ |
| 362 |
"podman", |
| 363 |
"rmi", |
| 364 |
"-f", |
| 365 |
image, |
| 366 |
]) |
| 367 |
|
| 368 |
|
| 369 |
output, _ = self.run_subprocess_assert_returncode([ |
| 370 |
podman_compose_path(), |
| 371 |
"--verbose", |
| 372 |
"-f", |
| 373 |
compose_file, |
| 374 |
"up", |
| 375 |
"-d", |
| 376 |
]) |
| 377 |
self.assertNotIn(b"Trying to pull", output) |
| 378 |
|
| 379 |
|
| 380 |
output, _ = self.run_subprocess_assert_returncode([ |
| 381 |
"podman", |
| 382 |
"images", |
| 383 |
"--filter", |
| 384 |
f"reference={image}", |
| 385 |
"--format", |
| 386 |
"{{.Repository}}:{{.Tag}}", |
| 387 |
]) |
| 388 |
self.assertIn(image.encode(), output) |
| 389 |
|
| 390 |
|
| 391 |
output, _ = self.run_subprocess_assert_returncode([ |
| 392 |
"podman", |
| 393 |
"ps", |
| 394 |
"-a", |
| 395 |
"--filter", |
| 396 |
f"ancestor={image}", |
| 397 |
"--format", |
| 398 |
"{{.Image}}", |
| 399 |
]) |
| 400 |
self.assertIn(image.encode(), output) |
| 401 |
finally: |
| 402 |
self.run_subprocess_assert_returncode([ |
| 403 |
podman_compose_path(), |
| 404 |
"-f", |
| 405 |
compose_file, |
| 406 |
"down", |
| 407 |
]) |
| 408 |
self.run_subprocess_assert_returncode( |
| 409 |
["podman", "rmi", "-f", image], |
| 410 |
) |