| 1 |
|
| 2 |
|
| 3 |
import os |
| 4 |
import shutil |
| 5 |
import subprocess |
| 6 |
import unittest |
| 7 |
from typing import Any |
| 8 |
from unittest import mock |
| 9 |
|
| 10 |
from parameterized import parameterized |
| 11 |
|
| 12 |
from podman_compose import PodmanCompose |
| 13 |
from podman_compose import PodmanComposeError |
| 14 |
from podman_compose import container_to_args |
| 15 |
|
| 16 |
|
| 17 |
def create_compose_mock(project_name: str = "test_project_name") -> PodmanCompose: |
| 18 |
compose = mock.Mock() |
| 19 |
compose.project_name = project_name |
| 20 |
compose.dirname = "test_dirname" |
| 21 |
compose.container_names_by_service.get = mock.Mock(return_value=None) |
| 22 |
compose.prefer_volume_over_mount = False |
| 23 |
compose.default_net = None |
| 24 |
compose.networks = {} |
| 25 |
compose.x_podman = {} |
| 26 |
compose.join_name_parts = mock.Mock(side_effect=lambda *args: '_'.join(args)) |
| 27 |
compose.format_name = mock.Mock(side_effect=lambda *args: '_'.join([project_name, *args])) |
| 28 |
|
| 29 |
async def podman_output(*args: Any, **kwargs: Any) -> None: |
| 30 |
pass |
| 31 |
|
| 32 |
compose.podman.output = mock.Mock(side_effect=podman_output) |
| 33 |
return compose |
| 34 |
|
| 35 |
|
| 36 |
def get_minimal_container() -> dict[str, Any]: |
| 37 |
return { |
| 38 |
"name": "project_name_service_name1", |
| 39 |
"service_name": "service_name", |
| 40 |
"image": "busybox", |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
def get_test_file_path(rel_path: str) -> str: |
| 45 |
repo_root = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) |
| 46 |
return os.path.realpath(os.path.join(repo_root, rel_path)) |
| 47 |
|
| 48 |
|
| 49 |
class TestContainerToArgs(unittest.IsolatedAsyncioTestCase): |
| 50 |
async def test_external_network_missing_raises_user_friendly_error(self) -> None: |
| 51 |
c = create_compose_mock() |
| 52 |
c.default_net = "external_net" |
| 53 |
c.networks = { |
| 54 |
"external_net": { |
| 55 |
"external": True, |
| 56 |
"name": "missing-external-network", |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
async def podman_output(*args: Any, **kwargs: Any) -> None: |
| 61 |
if args[2] == ["exists", "missing-external-network"]: |
| 62 |
raise subprocess.CalledProcessError(1, "podman network exists") |
| 63 |
|
| 64 |
setattr(c.podman, "output", mock.Mock(side_effect=podman_output)) |
| 65 |
cnt = get_minimal_container() |
| 66 |
|
| 67 |
with self.assertRaisesRegex( |
| 68 |
PodmanComposeError, |
| 69 |
r"External network \[missing-external-network\] does not exist\.", |
| 70 |
): |
| 71 |
await container_to_args(c, cnt) |
| 72 |
|
| 73 |
async def test_minimal(self) -> None: |
| 74 |
c = create_compose_mock() |
| 75 |
|
| 76 |
cnt = get_minimal_container() |
| 77 |
|
| 78 |
args = await container_to_args(c, cnt) |
| 79 |
self.assertEqual( |
| 80 |
args, |
| 81 |
[ |
| 82 |
"--name=project_name_service_name1", |
| 83 |
"-d", |
| 84 |
"--network=bridge:alias=service_name", |
| 85 |
"busybox", |
| 86 |
], |
| 87 |
) |
| 88 |
|
| 89 |
async def test_runtime(self) -> None: |
| 90 |
c = create_compose_mock() |
| 91 |
|
| 92 |
cnt = get_minimal_container() |
| 93 |
cnt["runtime"] = "runsc" |
| 94 |
|
| 95 |
args = await container_to_args(c, cnt) |
| 96 |
self.assertEqual( |
| 97 |
args, |
| 98 |
[ |
| 99 |
"--name=project_name_service_name1", |
| 100 |
"-d", |
| 101 |
"--network=bridge:alias=service_name", |
| 102 |
"--runtime", |
| 103 |
"runsc", |
| 104 |
"busybox", |
| 105 |
], |
| 106 |
) |
| 107 |
|
| 108 |
async def test_sysctl_list(self) -> None: |
| 109 |
c = create_compose_mock() |
| 110 |
|
| 111 |
cnt = get_minimal_container() |
| 112 |
cnt["sysctls"] = [ |
| 113 |
"net.core.somaxconn=1024", |
| 114 |
"net.ipv4.tcp_syncookies=0", |
| 115 |
] |
| 116 |
|
| 117 |
args = await container_to_args(c, cnt) |
| 118 |
self.assertEqual( |
| 119 |
args, |
| 120 |
[ |
| 121 |
"--name=project_name_service_name1", |
| 122 |
"-d", |
| 123 |
"--network=bridge:alias=service_name", |
| 124 |
"--sysctl", |
| 125 |
"net.core.somaxconn=1024", |
| 126 |
"--sysctl", |
| 127 |
"net.ipv4.tcp_syncookies=0", |
| 128 |
"busybox", |
| 129 |
], |
| 130 |
) |
| 131 |
|
| 132 |
async def test_sysctl_map(self) -> None: |
| 133 |
c = create_compose_mock() |
| 134 |
|
| 135 |
cnt = get_minimal_container() |
| 136 |
cnt["sysctls"] = { |
| 137 |
"net.core.somaxconn": 1024, |
| 138 |
"net.ipv4.tcp_syncookies": 0, |
| 139 |
} |
| 140 |
|
| 141 |
args = await container_to_args(c, cnt) |
| 142 |
self.assertEqual( |
| 143 |
args, |
| 144 |
[ |
| 145 |
"--name=project_name_service_name1", |
| 146 |
"-d", |
| 147 |
"--network=bridge:alias=service_name", |
| 148 |
"--sysctl", |
| 149 |
"net.core.somaxconn=1024", |
| 150 |
"--sysctl", |
| 151 |
"net.ipv4.tcp_syncookies=0", |
| 152 |
"busybox", |
| 153 |
], |
| 154 |
) |
| 155 |
|
| 156 |
async def test_sysctl_wrong_type(self) -> None: |
| 157 |
c = create_compose_mock() |
| 158 |
cnt = get_minimal_container() |
| 159 |
|
| 160 |
|
| 161 |
for wrong_type in [True, 0, 0.0, "wrong", ()]: |
| 162 |
with self.assertRaises(TypeError): |
| 163 |
cnt["sysctls"] = wrong_type |
| 164 |
await container_to_args(c, cnt) |
| 165 |
|
| 166 |
async def test_pid(self) -> None: |
| 167 |
c = create_compose_mock() |
| 168 |
cnt = get_minimal_container() |
| 169 |
|
| 170 |
cnt["pid"] = "host" |
| 171 |
|
| 172 |
args = await container_to_args(c, cnt) |
| 173 |
self.assertEqual( |
| 174 |
args, |
| 175 |
[ |
| 176 |
"--name=project_name_service_name1", |
| 177 |
"-d", |
| 178 |
"--network=bridge:alias=service_name", |
| 179 |
"--pid", |
| 180 |
"host", |
| 181 |
"busybox", |
| 182 |
], |
| 183 |
) |
| 184 |
|
| 185 |
async def test_http_proxy(self) -> None: |
| 186 |
c = create_compose_mock() |
| 187 |
|
| 188 |
cnt = get_minimal_container() |
| 189 |
cnt["http_proxy"] = False |
| 190 |
|
| 191 |
args = await container_to_args(c, cnt) |
| 192 |
self.assertEqual( |
| 193 |
args, |
| 194 |
[ |
| 195 |
"--name=project_name_service_name1", |
| 196 |
"-d", |
| 197 |
"--http-proxy=false", |
| 198 |
"--network=bridge:alias=service_name", |
| 199 |
"busybox", |
| 200 |
], |
| 201 |
) |
| 202 |
|
| 203 |
async def test_uidmaps_extension_old_path(self) -> None: |
| 204 |
c = create_compose_mock() |
| 205 |
|
| 206 |
cnt = get_minimal_container() |
| 207 |
cnt['x-podman'] = {'uidmaps': ['1000:1000:1']} |
| 208 |
|
| 209 |
with self.assertRaises(ValueError): |
| 210 |
await container_to_args(c, cnt) |
| 211 |
|
| 212 |
async def test_uidmaps_extension(self) -> None: |
| 213 |
c = create_compose_mock() |
| 214 |
|
| 215 |
cnt = get_minimal_container() |
| 216 |
cnt['x-podman.uidmaps'] = ['1000:1000:1', '1001:1001:2'] |
| 217 |
|
| 218 |
args = await container_to_args(c, cnt) |
| 219 |
self.assertEqual( |
| 220 |
args, |
| 221 |
[ |
| 222 |
"--name=project_name_service_name1", |
| 223 |
"-d", |
| 224 |
"--network=bridge:alias=service_name", |
| 225 |
'--uidmap', |
| 226 |
'1000:1000:1', |
| 227 |
'--uidmap', |
| 228 |
'1001:1001:2', |
| 229 |
"busybox", |
| 230 |
], |
| 231 |
) |
| 232 |
|
| 233 |
async def test_gidmaps_extension(self) -> None: |
| 234 |
c = create_compose_mock() |
| 235 |
|
| 236 |
cnt = get_minimal_container() |
| 237 |
cnt['x-podman.gidmaps'] = ['1000:1000:1', '1001:1001:2'] |
| 238 |
|
| 239 |
args = await container_to_args(c, cnt) |
| 240 |
self.assertEqual( |
| 241 |
args, |
| 242 |
[ |
| 243 |
"--name=project_name_service_name1", |
| 244 |
"-d", |
| 245 |
"--network=bridge:alias=service_name", |
| 246 |
'--gidmap', |
| 247 |
'1000:1000:1', |
| 248 |
'--gidmap', |
| 249 |
'1001:1001:2', |
| 250 |
"busybox", |
| 251 |
], |
| 252 |
) |
| 253 |
|
| 254 |
async def test_rootfs_extension(self) -> None: |
| 255 |
c = create_compose_mock() |
| 256 |
|
| 257 |
cnt = get_minimal_container() |
| 258 |
del cnt["image"] |
| 259 |
cnt["x-podman.rootfs"] = "/path/to/rootfs" |
| 260 |
|
| 261 |
args = await container_to_args(c, cnt) |
| 262 |
self.assertEqual( |
| 263 |
args, |
| 264 |
[ |
| 265 |
"--name=project_name_service_name1", |
| 266 |
"-d", |
| 267 |
"--network=bridge:alias=service_name", |
| 268 |
"--rootfs", |
| 269 |
"/path/to/rootfs", |
| 270 |
], |
| 271 |
) |
| 272 |
|
| 273 |
async def test_no_hosts_extension(self) -> None: |
| 274 |
c = create_compose_mock() |
| 275 |
|
| 276 |
cnt = get_minimal_container() |
| 277 |
cnt["x-podman.no_hosts"] = True |
| 278 |
|
| 279 |
args = await container_to_args(c, cnt) |
| 280 |
self.assertEqual( |
| 281 |
args, |
| 282 |
[ |
| 283 |
"--name=project_name_service_name1", |
| 284 |
"-d", |
| 285 |
"--network=bridge:alias=service_name", |
| 286 |
"--no-hosts", |
| 287 |
"busybox", |
| 288 |
], |
| 289 |
) |
| 290 |
|
| 291 |
@parameterized.expand([ |
| 292 |
|
| 293 |
("use_env_var_from_default_env_file_short_syntax", ["ZZVAR1"], "ZZVAR1=TEST1"), |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
("use_env_var_from_default_env_file_long_syntax", ["ZZVAR1=TEST1"], "ZZVAR1=TEST1"), |
| 299 |
|
| 300 |
( |
| 301 |
"use_env_var_from_default_env_file_override_value", |
| 302 |
["ZZVAR1=NEW_TEST1"], |
| 303 |
"ZZVAR1=NEW_TEST1", |
| 304 |
), |
| 305 |
]) |
| 306 |
async def test_env_file(self, test_name: str, cnt_env: list, expected_var: str) -> None: |
| 307 |
c = create_compose_mock() |
| 308 |
|
| 309 |
c.environ = {"ZZVAR1": "TEST1", "ZZVAR2": "TEST2"} |
| 310 |
|
| 311 |
cnt = get_minimal_container() |
| 312 |
cnt["environment"] = cnt_env |
| 313 |
|
| 314 |
args = await container_to_args(c, cnt) |
| 315 |
self.assertEqual( |
| 316 |
args, |
| 317 |
[ |
| 318 |
"--name=project_name_service_name1", |
| 319 |
"-d", |
| 320 |
"-e", |
| 321 |
f"{expected_var}", |
| 322 |
"--network=bridge:alias=service_name", |
| 323 |
"busybox", |
| 324 |
], |
| 325 |
) |
| 326 |
|
| 327 |
async def test_env_file_str(self) -> None: |
| 328 |
c = create_compose_mock() |
| 329 |
|
| 330 |
cnt = get_minimal_container() |
| 331 |
env_file = get_test_file_path('tests/integration/env_file_tests/env-files/project-1.env') |
| 332 |
cnt['env_file'] = env_file |
| 333 |
|
| 334 |
args = await container_to_args(c, cnt) |
| 335 |
self.assertEqual( |
| 336 |
args, |
| 337 |
[ |
| 338 |
"--name=project_name_service_name1", |
| 339 |
"-d", |
| 340 |
"-e", |
| 341 |
"ZZVAR1=podman-rocks-123", |
| 342 |
"-e", |
| 343 |
"ZZVAR2=podman-rocks-124", |
| 344 |
"-e", |
| 345 |
"ZZVAR3=podman-rocks-125", |
| 346 |
"--network=bridge:alias=service_name", |
| 347 |
"busybox", |
| 348 |
], |
| 349 |
) |
| 350 |
|
| 351 |
async def test_env_file_str_not_exists(self) -> None: |
| 352 |
c = create_compose_mock() |
| 353 |
|
| 354 |
cnt = get_minimal_container() |
| 355 |
cnt['env_file'] = 'notexists' |
| 356 |
|
| 357 |
with self.assertRaises(ValueError): |
| 358 |
await container_to_args(c, cnt) |
| 359 |
|
| 360 |
async def test_env_file_str_array_one_path(self) -> None: |
| 361 |
c = create_compose_mock() |
| 362 |
|
| 363 |
cnt = get_minimal_container() |
| 364 |
env_file = get_test_file_path('tests/integration/env_file_tests/env-files/project-1.env') |
| 365 |
cnt['env_file'] = [env_file] |
| 366 |
|
| 367 |
args = await container_to_args(c, cnt) |
| 368 |
self.assertEqual( |
| 369 |
args, |
| 370 |
[ |
| 371 |
"--name=project_name_service_name1", |
| 372 |
"-d", |
| 373 |
"-e", |
| 374 |
"ZZVAR1=podman-rocks-123", |
| 375 |
"-e", |
| 376 |
"ZZVAR2=podman-rocks-124", |
| 377 |
"-e", |
| 378 |
"ZZVAR3=podman-rocks-125", |
| 379 |
"--network=bridge:alias=service_name", |
| 380 |
"busybox", |
| 381 |
], |
| 382 |
) |
| 383 |
|
| 384 |
async def test_env_file_str_array_two_paths(self) -> None: |
| 385 |
c = create_compose_mock() |
| 386 |
|
| 387 |
cnt = get_minimal_container() |
| 388 |
env_file = get_test_file_path('tests/integration/env_file_tests/env-files/project-1.env') |
| 389 |
env_file_2 = get_test_file_path('tests/integration/env_file_tests/env-files/project-2.env') |
| 390 |
cnt['env_file'] = [env_file, env_file_2] |
| 391 |
|
| 392 |
args = await container_to_args(c, cnt) |
| 393 |
self.assertEqual( |
| 394 |
args, |
| 395 |
[ |
| 396 |
"--name=project_name_service_name1", |
| 397 |
"-d", |
| 398 |
"-e", |
| 399 |
"ZZVAR1=podman-rocks-123", |
| 400 |
"-e", |
| 401 |
"ZZVAR2=podman-rocks-124", |
| 402 |
"-e", |
| 403 |
"ZZVAR3=podman-rocks-125", |
| 404 |
"-e", |
| 405 |
"ZZVAR1=podman-rocks-223", |
| 406 |
"-e", |
| 407 |
"ZZVAR2=podman-rocks-224", |
| 408 |
"--network=bridge:alias=service_name", |
| 409 |
"busybox", |
| 410 |
], |
| 411 |
) |
| 412 |
|
| 413 |
async def test_env_file_obj_required(self) -> None: |
| 414 |
c = create_compose_mock() |
| 415 |
|
| 416 |
cnt = get_minimal_container() |
| 417 |
env_file = get_test_file_path('tests/integration/env_file_tests/env-files/project-1.env') |
| 418 |
cnt['env_file'] = {'path': env_file, 'required': True} |
| 419 |
|
| 420 |
args = await container_to_args(c, cnt) |
| 421 |
self.assertEqual( |
| 422 |
args, |
| 423 |
[ |
| 424 |
"--name=project_name_service_name1", |
| 425 |
"-d", |
| 426 |
"-e", |
| 427 |
"ZZVAR1=podman-rocks-123", |
| 428 |
"-e", |
| 429 |
"ZZVAR2=podman-rocks-124", |
| 430 |
"-e", |
| 431 |
"ZZVAR3=podman-rocks-125", |
| 432 |
"--network=bridge:alias=service_name", |
| 433 |
"busybox", |
| 434 |
], |
| 435 |
) |
| 436 |
|
| 437 |
async def test_env_file_obj_required_non_existent_path(self) -> None: |
| 438 |
c = create_compose_mock() |
| 439 |
|
| 440 |
cnt = get_minimal_container() |
| 441 |
cnt['env_file'] = {'path': 'not-exists', 'required': True} |
| 442 |
|
| 443 |
with self.assertRaises(ValueError): |
| 444 |
await container_to_args(c, cnt) |
| 445 |
|
| 446 |
async def test_env_file_obj_optional(self) -> None: |
| 447 |
c = create_compose_mock() |
| 448 |
|
| 449 |
cnt = get_minimal_container() |
| 450 |
cnt['env_file'] = {'path': 'not-exists', 'required': False} |
| 451 |
|
| 452 |
args = await container_to_args(c, cnt) |
| 453 |
self.assertEqual( |
| 454 |
args, |
| 455 |
[ |
| 456 |
"--name=project_name_service_name1", |
| 457 |
"-d", |
| 458 |
"--network=bridge:alias=service_name", |
| 459 |
"busybox", |
| 460 |
], |
| 461 |
) |
| 462 |
|
| 463 |
async def test_gpu_count_all(self) -> None: |
| 464 |
c = create_compose_mock() |
| 465 |
|
| 466 |
cnt = get_minimal_container() |
| 467 |
cnt["command"] = ["nvidia-smi"] |
| 468 |
cnt["deploy"] = {"resources": {"reservations": {"devices": [{}]}}} |
| 469 |
|
| 470 |
cnt["deploy"]["resources"]["reservations"]["devices"][0] = { |
| 471 |
"driver": "nvidia", |
| 472 |
"count": "all", |
| 473 |
"capabilities": ["gpu"], |
| 474 |
} |
| 475 |
|
| 476 |
args = await container_to_args(c, cnt) |
| 477 |
self.assertEqual( |
| 478 |
args, |
| 479 |
[ |
| 480 |
"--name=project_name_service_name1", |
| 481 |
"-d", |
| 482 |
"--network=bridge:alias=service_name", |
| 483 |
"--device", |
| 484 |
"nvidia.com/gpu=all", |
| 485 |
"--security-opt=label=disable", |
| 486 |
"busybox", |
| 487 |
"nvidia-smi", |
| 488 |
], |
| 489 |
) |
| 490 |
|
| 491 |
async def test_gpu_count_specific(self) -> None: |
| 492 |
c = create_compose_mock() |
| 493 |
|
| 494 |
cnt = get_minimal_container() |
| 495 |
cnt["command"] = ["nvidia-smi"] |
| 496 |
cnt["deploy"] = { |
| 497 |
"resources": { |
| 498 |
"reservations": { |
| 499 |
"devices": [ |
| 500 |
{ |
| 501 |
"driver": "nvidia", |
| 502 |
"count": 2, |
| 503 |
"capabilities": ["gpu"], |
| 504 |
} |
| 505 |
] |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
args = await container_to_args(c, cnt) |
| 511 |
self.assertEqual( |
| 512 |
args, |
| 513 |
[ |
| 514 |
"--name=project_name_service_name1", |
| 515 |
"-d", |
| 516 |
"--network=bridge:alias=service_name", |
| 517 |
"--device", |
| 518 |
"nvidia.com/gpu=0", |
| 519 |
"--device", |
| 520 |
"nvidia.com/gpu=1", |
| 521 |
"--security-opt=label=disable", |
| 522 |
"busybox", |
| 523 |
"nvidia-smi", |
| 524 |
], |
| 525 |
) |
| 526 |
|
| 527 |
async def test_gpu_device_ids_all(self) -> None: |
| 528 |
c = create_compose_mock() |
| 529 |
|
| 530 |
cnt = get_minimal_container() |
| 531 |
cnt["command"] = ["nvidia-smi"] |
| 532 |
cnt["deploy"] = { |
| 533 |
"resources": { |
| 534 |
"reservations": { |
| 535 |
"devices": [ |
| 536 |
{ |
| 537 |
"driver": "nvidia", |
| 538 |
"device_ids": "all", |
| 539 |
"capabilities": ["gpu"], |
| 540 |
} |
| 541 |
] |
| 542 |
} |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
args = await container_to_args(c, cnt) |
| 547 |
self.assertEqual( |
| 548 |
args, |
| 549 |
[ |
| 550 |
"--name=project_name_service_name1", |
| 551 |
"-d", |
| 552 |
"--network=bridge:alias=service_name", |
| 553 |
"--device", |
| 554 |
"nvidia.com/gpu=all", |
| 555 |
"--security-opt=label=disable", |
| 556 |
"busybox", |
| 557 |
"nvidia-smi", |
| 558 |
], |
| 559 |
) |
| 560 |
|
| 561 |
async def test_gpu_device_ids_specific(self) -> None: |
| 562 |
c = create_compose_mock() |
| 563 |
|
| 564 |
cnt = get_minimal_container() |
| 565 |
cnt["command"] = ["nvidia-smi"] |
| 566 |
cnt["deploy"] = { |
| 567 |
"resources": { |
| 568 |
"reservations": { |
| 569 |
"devices": [ |
| 570 |
{ |
| 571 |
"driver": "nvidia", |
| 572 |
"device_ids": [1, 3], |
| 573 |
"capabilities": ["gpu"], |
| 574 |
} |
| 575 |
] |
| 576 |
} |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
args = await container_to_args(c, cnt) |
| 581 |
self.assertEqual( |
| 582 |
args, |
| 583 |
[ |
| 584 |
"--name=project_name_service_name1", |
| 585 |
"-d", |
| 586 |
"--network=bridge:alias=service_name", |
| 587 |
"--device", |
| 588 |
"nvidia.com/gpu=1", |
| 589 |
"--device", |
| 590 |
"nvidia.com/gpu=3", |
| 591 |
"--security-opt=label=disable", |
| 592 |
"busybox", |
| 593 |
"nvidia-smi", |
| 594 |
], |
| 595 |
) |
| 596 |
|
| 597 |
@parameterized.expand([ |
| 598 |
( |
| 599 |
False, |
| 600 |
"z", |
| 601 |
[ |
| 602 |
"--mount", |
| 603 |
f"type=bind,source={get_test_file_path('test_dirname/foo')},destination=/mnt,z", |
| 604 |
], |
| 605 |
), |
| 606 |
( |
| 607 |
False, |
| 608 |
"Z", |
| 609 |
[ |
| 610 |
"--mount", |
| 611 |
f"type=bind,source={get_test_file_path('test_dirname/foo')},destination=/mnt,Z", |
| 612 |
], |
| 613 |
), |
| 614 |
(True, "z", ["-v", f"{get_test_file_path('test_dirname/foo')}:/mnt:z"]), |
| 615 |
(True, "Z", ["-v", f"{get_test_file_path('test_dirname/foo')}:/mnt:Z"]), |
| 616 |
]) |
| 617 |
async def test_selinux_volume( |
| 618 |
self, prefer_volume: bool, selinux_type: str, expected_additional_args: list |
| 619 |
) -> None: |
| 620 |
c = create_compose_mock() |
| 621 |
c.prefer_volume_over_mount = prefer_volume |
| 622 |
|
| 623 |
cnt = get_minimal_container() |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
cnt["_service"] = cnt["service_name"] |
| 628 |
|
| 629 |
cnt["volumes"] = [ |
| 630 |
{ |
| 631 |
"type": "bind", |
| 632 |
"source": "./foo", |
| 633 |
"target": "/mnt", |
| 634 |
"bind": { |
| 635 |
"selinux": selinux_type, |
| 636 |
}, |
| 637 |
} |
| 638 |
] |
| 639 |
|
| 640 |
args = await container_to_args(c, cnt) |
| 641 |
self.assertEqual( |
| 642 |
args, |
| 643 |
[ |
| 644 |
"--name=project_name_service_name1", |
| 645 |
"-d", |
| 646 |
*expected_additional_args, |
| 647 |
"--network=bridge:alias=service_name", |
| 648 |
"busybox", |
| 649 |
], |
| 650 |
) |
| 651 |
|
| 652 |
async def test_volumes_glob_mount_source(self) -> None: |
| 653 |
c = create_compose_mock() |
| 654 |
cnt = get_minimal_container() |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
cnt["_service"] = cnt["service_name"] |
| 659 |
|
| 660 |
cnt["volumes"] = [ |
| 661 |
{ |
| 662 |
"type": "glob", |
| 663 |
"source": f"{get_test_file_path('test_dirname/foo')}/*.ext", |
| 664 |
"target": "/mnt", |
| 665 |
} |
| 666 |
] |
| 667 |
expected_additional_args = [ |
| 668 |
"--mount", |
| 669 |
( |
| 670 |
"type=glob,source=" |
| 671 |
f"{get_test_file_path('test_dirname/foo') + '/*.ext'}," |
| 672 |
"destination=/mnt" |
| 673 |
), |
| 674 |
] |
| 675 |
args = await container_to_args(c, cnt) |
| 676 |
self.assertEqual( |
| 677 |
args, |
| 678 |
[ |
| 679 |
"--name=project_name_service_name1", |
| 680 |
"-d", |
| 681 |
*expected_additional_args, |
| 682 |
"--network=bridge:alias=service_name", |
| 683 |
"busybox", |
| 684 |
], |
| 685 |
) |
| 686 |
|
| 687 |
@parameterized.expand([ |
| 688 |
( |
| 689 |
"absolute_path", |
| 690 |
get_test_file_path('test_dirname/foo'), |
| 691 |
[ |
| 692 |
"--mount", |
| 693 |
f"type=bind,source={get_test_file_path('test_dirname/foo')},destination=/mnt", |
| 694 |
], |
| 695 |
), |
| 696 |
( |
| 697 |
"relative_path", |
| 698 |
'./foo', |
| 699 |
[ |
| 700 |
"--mount", |
| 701 |
f"type=bind,source={get_test_file_path('test_dirname/foo')},destination=/mnt", |
| 702 |
], |
| 703 |
), |
| 704 |
( |
| 705 |
"home_dir", |
| 706 |
'~/test_dirname/foo', |
| 707 |
[ |
| 708 |
"--mount", |
| 709 |
f"type=bind,source={os.path.expanduser('~/test_dirname/foo')},destination=/mnt", |
| 710 |
], |
| 711 |
), |
| 712 |
]) |
| 713 |
async def test_volumes_bind_mount_source( |
| 714 |
self, test_name: str, mount_source: str, expected_additional_args: list |
| 715 |
) -> None: |
| 716 |
c = create_compose_mock() |
| 717 |
cnt = get_minimal_container() |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
cnt["_service"] = cnt["service_name"] |
| 722 |
|
| 723 |
cnt["volumes"] = [ |
| 724 |
{ |
| 725 |
"type": "bind", |
| 726 |
"source": f"{mount_source}", |
| 727 |
"target": "/mnt", |
| 728 |
} |
| 729 |
] |
| 730 |
|
| 731 |
args = await container_to_args(c, cnt) |
| 732 |
self.assertEqual( |
| 733 |
args, |
| 734 |
[ |
| 735 |
"--name=project_name_service_name1", |
| 736 |
"-d", |
| 737 |
*expected_additional_args, |
| 738 |
"--network=bridge:alias=service_name", |
| 739 |
"busybox", |
| 740 |
], |
| 741 |
) |
| 742 |
|
| 743 |
@parameterized.expand([ |
| 744 |
( |
| 745 |
"without_subpath", |
| 746 |
{}, |
| 747 |
"type=image,source=example:latest,destination=/mnt/example", |
| 748 |
), |
| 749 |
( |
| 750 |
"with_subpath", |
| 751 |
{"image": {"subpath": "path/to/image/folder"}}, |
| 752 |
"type=image,source=example:latest,destination=/mnt/example,subpath=path/to/image/folder", |
| 753 |
), |
| 754 |
]) |
| 755 |
async def test_volumes_image_mount( |
| 756 |
self, test_name: str, image_opts: dict, expected_mount_arg: str |
| 757 |
) -> None: |
| 758 |
c = create_compose_mock() |
| 759 |
cnt = get_minimal_container() |
| 760 |
cnt["_service"] = cnt["service_name"] |
| 761 |
|
| 762 |
cnt["volumes"] = [ |
| 763 |
{ |
| 764 |
"type": "image", |
| 765 |
"source": "example:latest", |
| 766 |
"target": "/mnt/example", |
| 767 |
**image_opts, |
| 768 |
}, |
| 769 |
] |
| 770 |
|
| 771 |
args = await container_to_args(c, cnt) |
| 772 |
self.assertEqual( |
| 773 |
args, |
| 774 |
[ |
| 775 |
"--name=project_name_service_name1", |
| 776 |
"-d", |
| 777 |
"--mount", |
| 778 |
expected_mount_arg, |
| 779 |
"--network=bridge:alias=service_name", |
| 780 |
"busybox", |
| 781 |
], |
| 782 |
) |
| 783 |
|
| 784 |
@parameterized.expand([ |
| 785 |
( |
| 786 |
"without_subpath", |
| 787 |
{}, |
| 788 |
"type=volume,source=volname,destination=/mnt/example", |
| 789 |
), |
| 790 |
( |
| 791 |
"with_subpath", |
| 792 |
{"volume": {"subpath": "path/to/image/folder"}}, |
| 793 |
"type=volume,source=volname,destination=/mnt/example,subpath=path/to/image/folder", |
| 794 |
), |
| 795 |
]) |
| 796 |
async def test_volumes_mount( |
| 797 |
self, test_name: str, volume_opts: dict, expected_mount_arg: str |
| 798 |
) -> None: |
| 799 |
c = create_compose_mock() |
| 800 |
c.vols = {"volname": {"name": "volname"}} |
| 801 |
|
| 802 |
cnt = get_minimal_container() |
| 803 |
cnt["_service"] = cnt["service_name"] |
| 804 |
|
| 805 |
cnt["volumes"] = [ |
| 806 |
{ |
| 807 |
"type": "volume", |
| 808 |
"source": "volname", |
| 809 |
"target": "/mnt/example", |
| 810 |
**volume_opts, |
| 811 |
}, |
| 812 |
] |
| 813 |
|
| 814 |
args = await container_to_args(c, cnt) |
| 815 |
self.assertEqual( |
| 816 |
args, |
| 817 |
[ |
| 818 |
"--name=project_name_service_name1", |
| 819 |
"-d", |
| 820 |
"--mount", |
| 821 |
expected_mount_arg, |
| 822 |
"--network=bridge:alias=service_name", |
| 823 |
"busybox", |
| 824 |
], |
| 825 |
) |
| 826 |
|
| 827 |
@parameterized.expand([ |
| 828 |
( |
| 829 |
"create_host_path_set_to_true", |
| 830 |
{"bind": {"create_host_path": True}}, |
| 831 |
), |
| 832 |
( |
| 833 |
"create_host_path_default_true", |
| 834 |
{}, |
| 835 |
), |
| 836 |
]) |
| 837 |
async def test_volumes_bind_mount_create_source_dir(self, test_name: str, bind: dict) -> None: |
| 838 |
|
| 839 |
c = create_compose_mock() |
| 840 |
c.prefer_volume_over_mount = True |
| 841 |
cnt = get_minimal_container() |
| 842 |
|
| 843 |
cnt["_service"] = cnt["service_name"] |
| 844 |
|
| 845 |
volume_info = { |
| 846 |
"type": "bind", |
| 847 |
"source": "./not_exists/foo", |
| 848 |
"target": "/mnt", |
| 849 |
} |
| 850 |
volume_info.update(bind) |
| 851 |
cnt["volumes"] = [ |
| 852 |
volume_info, |
| 853 |
] |
| 854 |
|
| 855 |
args = await container_to_args(c, cnt) |
| 856 |
|
| 857 |
self.assertEqual( |
| 858 |
args, |
| 859 |
[ |
| 860 |
"--name=project_name_service_name1", |
| 861 |
"-d", |
| 862 |
"-v", |
| 863 |
f"{get_test_file_path('./test_dirname/not_exists/foo')}:/mnt", |
| 864 |
"--network=bridge:alias=service_name", |
| 865 |
"busybox", |
| 866 |
], |
| 867 |
) |
| 868 |
dir_path = get_test_file_path('./test_dirname/not_exists/foo') |
| 869 |
shutil.rmtree(dir_path) |
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
async def test_volumes_bind_mount_source_does_not_exist(self) -> None: |
| 874 |
c = create_compose_mock() |
| 875 |
c.prefer_volume_over_mount = True |
| 876 |
cnt = get_minimal_container() |
| 877 |
|
| 878 |
cnt["_service"] = cnt["service_name"] |
| 879 |
|
| 880 |
cnt["volumes"] = [ |
| 881 |
{ |
| 882 |
"type": "bind", |
| 883 |
"source": "./not_exists/foo", |
| 884 |
"target": "/mnt", |
| 885 |
"bind": {"create_host_path": False}, |
| 886 |
} |
| 887 |
] |
| 888 |
|
| 889 |
with self.assertRaises(ValueError): |
| 890 |
await container_to_args(c, cnt) |
| 891 |
|
| 892 |
@parameterized.expand([ |
| 893 |
("not_compat", False, "test_project_name", "test_project_name_network1"), |
| 894 |
("compat_no_dash", True, "test_project_name", "test_project_name_network1"), |
| 895 |
("compat_dash", True, "test_project-name", "test_projectname_network1"), |
| 896 |
]) |
| 897 |
async def test_network_default_name( |
| 898 |
self, name: str, is_compat: bool, project_name: str, expected_network_name: str |
| 899 |
) -> None: |
| 900 |
c = create_compose_mock(project_name) |
| 901 |
c.x_podman = {PodmanCompose.XPodmanSettingKey.DEFAULT_NET_NAME_COMPAT: is_compat} |
| 902 |
c.networks = {'network1': {}} |
| 903 |
|
| 904 |
cnt = get_minimal_container() |
| 905 |
cnt['networks'] = ['network1'] |
| 906 |
|
| 907 |
args = await container_to_args(c, cnt) |
| 908 |
self.assertEqual( |
| 909 |
args, |
| 910 |
[ |
| 911 |
"--name=project_name_service_name1", |
| 912 |
"-d", |
| 913 |
f"--network={expected_network_name}:alias=service_name", |
| 914 |
"busybox", |
| 915 |
], |
| 916 |
) |
| 917 |
|
| 918 |
async def test_device(self) -> None: |
| 919 |
c = create_compose_mock() |
| 920 |
cnt = get_minimal_container() |
| 921 |
|
| 922 |
cnt['devices'] = ['/dev/ttyS0'] |
| 923 |
cnt['device_cgroup_rules'] = ['c 100:200 rwm'] |
| 924 |
|
| 925 |
args = await container_to_args(c, cnt) |
| 926 |
self.assertEqual( |
| 927 |
args, |
| 928 |
[ |
| 929 |
"--name=project_name_service_name1", |
| 930 |
"-d", |
| 931 |
"--device", |
| 932 |
"/dev/ttyS0", |
| 933 |
"--device-cgroup-rule", |
| 934 |
"c 100:200 rwm", |
| 935 |
"--network=bridge:alias=service_name", |
| 936 |
"busybox", |
| 937 |
], |
| 938 |
) |
| 939 |
|
| 940 |
async def test_cpuset(self) -> None: |
| 941 |
c = create_compose_mock() |
| 942 |
cnt = get_minimal_container() |
| 943 |
cnt["cpuset"] = "0-1" |
| 944 |
|
| 945 |
args = await container_to_args(c, cnt) |
| 946 |
self.assertEqual( |
| 947 |
args, |
| 948 |
[ |
| 949 |
"--name=project_name_service_name1", |
| 950 |
"-d", |
| 951 |
"--network=bridge:alias=service_name", |
| 952 |
"--cpuset-cpus", |
| 953 |
"0-1", |
| 954 |
"busybox", |
| 955 |
], |
| 956 |
) |
| 957 |
|
| 958 |
async def test_pids_limit_container_level(self) -> None: |
| 959 |
c = create_compose_mock() |
| 960 |
cnt = get_minimal_container() |
| 961 |
cnt["pids_limit"] = 100 |
| 962 |
|
| 963 |
args = await container_to_args(c, cnt) |
| 964 |
self.assertEqual( |
| 965 |
args, |
| 966 |
[ |
| 967 |
"--name=project_name_service_name1", |
| 968 |
"-d", |
| 969 |
"--network=bridge:alias=service_name", |
| 970 |
"--pids-limit", |
| 971 |
"100", |
| 972 |
"busybox", |
| 973 |
], |
| 974 |
) |
| 975 |
|
| 976 |
async def test_pids_limit_deploy_section(self) -> None: |
| 977 |
c = create_compose_mock() |
| 978 |
cnt = get_minimal_container() |
| 979 |
cnt["deploy"] = {"resources": {"limits": {"pids": 100}}} |
| 980 |
|
| 981 |
args = await container_to_args(c, cnt) |
| 982 |
self.assertEqual( |
| 983 |
args, |
| 984 |
[ |
| 985 |
"--name=project_name_service_name1", |
| 986 |
"-d", |
| 987 |
"--network=bridge:alias=service_name", |
| 988 |
"--pids-limit", |
| 989 |
"100", |
| 990 |
"busybox", |
| 991 |
], |
| 992 |
) |
| 993 |
|
| 994 |
async def test_pids_limit_both_same(self) -> None: |
| 995 |
c = create_compose_mock() |
| 996 |
cnt = get_minimal_container() |
| 997 |
cnt["pids_limit"] = 100 |
| 998 |
cnt["deploy"] = {"resources": {"limits": {"pids": 100}}} |
| 999 |
|
| 1000 |
args = await container_to_args(c, cnt) |
| 1001 |
self.assertEqual( |
| 1002 |
args, |
| 1003 |
[ |
| 1004 |
"--name=project_name_service_name1", |
| 1005 |
"-d", |
| 1006 |
"--network=bridge:alias=service_name", |
| 1007 |
"--pids-limit", |
| 1008 |
"100", |
| 1009 |
"busybox", |
| 1010 |
], |
| 1011 |
) |
| 1012 |
|
| 1013 |
async def test_pids_limit_both_different(self) -> None: |
| 1014 |
c = create_compose_mock() |
| 1015 |
cnt = get_minimal_container() |
| 1016 |
cnt["pids_limit"] = 100 |
| 1017 |
cnt["deploy"] = {"resources": {"limits": {"pids": 200}}} |
| 1018 |
|
| 1019 |
with self.assertRaises(ValueError): |
| 1020 |
await container_to_args(c, cnt) |
| 1021 |
|
| 1022 |
async def test_healthcheck_string(self) -> None: |
| 1023 |
c = create_compose_mock() |
| 1024 |
cnt = get_minimal_container() |
| 1025 |
cnt["healthcheck"] = { |
| 1026 |
"test": "cmd arg1 arg2", |
| 1027 |
} |
| 1028 |
|
| 1029 |
args = await container_to_args(c, cnt) |
| 1030 |
self.assertEqual( |
| 1031 |
args, |
| 1032 |
[ |
| 1033 |
"--name=project_name_service_name1", |
| 1034 |
"-d", |
| 1035 |
"--network=bridge:alias=service_name", |
| 1036 |
"--health-cmd", |
| 1037 |
'["CMD-SHELL", "cmd arg1 arg2"]', |
| 1038 |
"busybox", |
| 1039 |
], |
| 1040 |
) |
| 1041 |
|
| 1042 |
async def test_healthcheck_cmd_args(self) -> None: |
| 1043 |
c = create_compose_mock() |
| 1044 |
cnt = get_minimal_container() |
| 1045 |
cnt["healthcheck"] = { |
| 1046 |
"test": ["CMD", "cmd", "arg1", "arg2"], |
| 1047 |
} |
| 1048 |
|
| 1049 |
args = await container_to_args(c, cnt) |
| 1050 |
self.assertEqual( |
| 1051 |
args, |
| 1052 |
[ |
| 1053 |
"--name=project_name_service_name1", |
| 1054 |
"-d", |
| 1055 |
"--network=bridge:alias=service_name", |
| 1056 |
"--health-cmd", |
| 1057 |
'["cmd", "arg1", "arg2"]', |
| 1058 |
"busybox", |
| 1059 |
], |
| 1060 |
) |
| 1061 |
|
| 1062 |
async def test_healthcheck_cmd_shell(self) -> None: |
| 1063 |
c = create_compose_mock() |
| 1064 |
cnt = get_minimal_container() |
| 1065 |
cnt["healthcheck"] = { |
| 1066 |
"test": ["CMD-SHELL", "cmd arg1 arg2"], |
| 1067 |
} |
| 1068 |
|
| 1069 |
args = await container_to_args(c, cnt) |
| 1070 |
self.assertEqual( |
| 1071 |
args, |
| 1072 |
[ |
| 1073 |
"--name=project_name_service_name1", |
| 1074 |
"-d", |
| 1075 |
"--network=bridge:alias=service_name", |
| 1076 |
"--health-cmd", |
| 1077 |
'["cmd arg1 arg2"]', |
| 1078 |
"busybox", |
| 1079 |
], |
| 1080 |
) |
| 1081 |
|
| 1082 |
async def test_healthcheck_disable(self) -> None: |
| 1083 |
c = create_compose_mock() |
| 1084 |
cnt = get_minimal_container() |
| 1085 |
cnt["healthcheck"] = { |
| 1086 |
"test": "cmd arg1 arg2", |
| 1087 |
"disable": "true", |
| 1088 |
} |
| 1089 |
|
| 1090 |
args = await container_to_args(c, cnt) |
| 1091 |
self.assertEqual( |
| 1092 |
args, |
| 1093 |
[ |
| 1094 |
"--name=project_name_service_name1", |
| 1095 |
"-d", |
| 1096 |
"--network=bridge:alias=service_name", |
| 1097 |
"--no-healthcheck", |
| 1098 |
"busybox", |
| 1099 |
], |
| 1100 |
) |
| 1101 |
|
| 1102 |
async def test_healthcheck_cmd_shell_error(self) -> None: |
| 1103 |
c = create_compose_mock() |
| 1104 |
cnt = get_minimal_container() |
| 1105 |
cnt["healthcheck"] = { |
| 1106 |
"test": ["CMD-SHELL", "cmd arg1", "arg2"], |
| 1107 |
} |
| 1108 |
|
| 1109 |
with self.assertRaisesRegex(ValueError, "'CMD-SHELL' takes a single string after it"): |
| 1110 |
await container_to_args(c, cnt) |
| 1111 |
|
| 1112 |
async def test_unknown_healthcheck_test_type(self) -> None: |
| 1113 |
c = create_compose_mock() |
| 1114 |
cnt = get_minimal_container() |
| 1115 |
cnt["healthcheck"] = { |
| 1116 |
"test": ["TEST", "arg1", "arg2"], |
| 1117 |
} |
| 1118 |
with self.assertRaises(ValueError) as error_msg: |
| 1119 |
await container_to_args(c, cnt) |
| 1120 |
|
| 1121 |
expected = "unknown healthcheck test type [TEST], expecting NONE, CMD or CMD-SHELL." |
| 1122 |
self.assertEqual(expected, str(error_msg.exception)) |
| 1123 |
|
| 1124 |
async def test_healthcheck_test_not_string_or_list(self) -> None: |
| 1125 |
c = create_compose_mock() |
| 1126 |
cnt = get_minimal_container() |
| 1127 |
cnt["healthcheck"] = { |
| 1128 |
"test": 2, |
| 1129 |
} |
| 1130 |
|
| 1131 |
with self.assertRaisesRegex(ValueError, "'healthcheck.test' either a string or a list"): |
| 1132 |
await container_to_args(c, cnt) |
| 1133 |
|
| 1134 |
async def test_healthcheck_not_a_dict_error(self) -> None: |
| 1135 |
c = create_compose_mock() |
| 1136 |
cnt = get_minimal_container() |
| 1137 |
cnt["healthcheck"] = [] |
| 1138 |
|
| 1139 |
with self.assertRaisesRegex(ValueError, "'healthcheck' must be a key-value mapping"): |
| 1140 |
await container_to_args(c, cnt) |
| 1141 |
|
| 1142 |
async def test_healthcheck_test_is_missing(self) -> None: |
| 1143 |
c = create_compose_mock() |
| 1144 |
cnt = get_minimal_container() |
| 1145 |
cnt["healthcheck"] = { |
| 1146 |
"interval": "1m", |
| 1147 |
"retries": "3", |
| 1148 |
} |
| 1149 |
|
| 1150 |
args = await container_to_args(c, cnt) |
| 1151 |
self.assertEqual( |
| 1152 |
args, |
| 1153 |
[ |
| 1154 |
"--name=project_name_service_name1", |
| 1155 |
"-d", |
| 1156 |
"--network=bridge:alias=service_name", |
| 1157 |
'--health-interval', |
| 1158 |
'1m', |
| 1159 |
'--health-retries', |
| 1160 |
'3', |
| 1161 |
"busybox", |
| 1162 |
], |
| 1163 |
) |
| 1164 |
|
| 1165 |
async def test_healthcheck_options(self) -> None: |
| 1166 |
c = create_compose_mock() |
| 1167 |
cnt = get_minimal_container() |
| 1168 |
cnt["healthcheck"] = { |
| 1169 |
"test": ["CMD", "cmd", "arg1", "arg2"], |
| 1170 |
"interval": "1m", |
| 1171 |
"timeout": "10s", |
| 1172 |
"retries": "3", |
| 1173 |
"start_period": "5s", |
| 1174 |
"start_interval": "6s", |
| 1175 |
} |
| 1176 |
|
| 1177 |
args = await container_to_args(c, cnt) |
| 1178 |
self.assertEqual( |
| 1179 |
args, |
| 1180 |
[ |
| 1181 |
"--name=project_name_service_name1", |
| 1182 |
"-d", |
| 1183 |
"--network=bridge:alias=service_name", |
| 1184 |
"--health-cmd", |
| 1185 |
'["cmd", "arg1", "arg2"]', |
| 1186 |
'--health-interval', |
| 1187 |
'1m', |
| 1188 |
'--health-timeout', |
| 1189 |
'10s', |
| 1190 |
'--health-start-period', |
| 1191 |
'5s', |
| 1192 |
'--health-startup-interval', |
| 1193 |
'6s', |
| 1194 |
'--health-retries', |
| 1195 |
'3', |
| 1196 |
"busybox", |
| 1197 |
], |
| 1198 |
) |