| 1 |
|
| 2 |
import unittest |
| 3 |
from typing import Any |
| 4 |
from typing import Union |
| 5 |
|
| 6 |
from parameterized import parameterized |
| 7 |
|
| 8 |
from podman_compose import normalize_service |
| 9 |
|
| 10 |
|
| 11 |
class TestNormalizeService(unittest.TestCase): |
| 12 |
@parameterized.expand([ |
| 13 |
({"test": "test"}, {"test": "test"}), |
| 14 |
({"build": "."}, {"build": {"context": "."}}), |
| 15 |
({"build": "./dir-1"}, {"build": {"context": "./dir-1"}}), |
| 16 |
({"build": {"context": "./dir-1"}}, {"build": {"context": "./dir-1"}}), |
| 17 |
( |
| 18 |
{"build": {"dockerfile": "dockerfile-1"}}, |
| 19 |
{"build": {"dockerfile": "dockerfile-1"}}, |
| 20 |
), |
| 21 |
( |
| 22 |
{"build": {"context": "./dir-1", "dockerfile": "dockerfile-1"}}, |
| 23 |
{"build": {"context": "./dir-1", "dockerfile": "dockerfile-1"}}, |
| 24 |
), |
| 25 |
( |
| 26 |
{"build": {"additional_contexts": ["ctx=../ctx", "ctx2=../ctx2"]}}, |
| 27 |
{"build": {"additional_contexts": ["ctx=../ctx", "ctx2=../ctx2"]}}, |
| 28 |
), |
| 29 |
( |
| 30 |
{"build": {"additional_contexts": {"ctx": "../ctx", "ctx2": "../ctx2"}}}, |
| 31 |
{"build": {"additional_contexts": ["ctx=../ctx", "ctx2=../ctx2"]}}, |
| 32 |
), |
| 33 |
]) |
| 34 |
def test_simple(self, input: dict[str, Any], expected: dict[str, Any]) -> None: |
| 35 |
self.assertEqual(normalize_service(input), expected) |
| 36 |
|
| 37 |
@parameterized.expand([ |
| 38 |
({"test": "test"}, {"test": "test"}), |
| 39 |
({"build": "."}, {"build": {"context": "./sub_dir/."}}), |
| 40 |
({"build": "./dir-1"}, {"build": {"context": "./sub_dir/dir-1"}}), |
| 41 |
({"build": {"context": "./dir-1"}}, {"build": {"context": "./sub_dir/dir-1"}}), |
| 42 |
( |
| 43 |
{"build": {"dockerfile": "dockerfile-1"}}, |
| 44 |
{"build": {"context": "./sub_dir", "dockerfile": "dockerfile-1"}}, |
| 45 |
), |
| 46 |
( |
| 47 |
{"build": {"context": "./dir-1", "dockerfile": "dockerfile-1"}}, |
| 48 |
{"build": {"context": "./sub_dir/dir-1", "dockerfile": "dockerfile-1"}}, |
| 49 |
), |
| 50 |
( |
| 51 |
{"volumes": ["./nested/relative:/mnt", "../dir-in-parent:/mnt", "..:/mnt", ".:/mnt"]}, |
| 52 |
{ |
| 53 |
"volumes": [ |
| 54 |
"./sub_dir/./nested/relative:/mnt", |
| 55 |
"./sub_dir/../dir-in-parent:/mnt", |
| 56 |
"./sub_dir/..:/mnt", |
| 57 |
"./sub_dir/.:/mnt", |
| 58 |
] |
| 59 |
}, |
| 60 |
), |
| 61 |
( |
| 62 |
{ |
| 63 |
"volumes": [ |
| 64 |
{ |
| 65 |
"type": "bind", |
| 66 |
"source": "./nested/relative", |
| 67 |
"target": "/mnt", |
| 68 |
} |
| 69 |
] |
| 70 |
}, |
| 71 |
{ |
| 72 |
"volumes": [ |
| 73 |
{ |
| 74 |
"type": "bind", |
| 75 |
"source": "./sub_dir/./nested/relative", |
| 76 |
"target": "/mnt", |
| 77 |
} |
| 78 |
] |
| 79 |
}, |
| 80 |
), |
| 81 |
]) |
| 82 |
def test_normalize_service_with_sub_dir( |
| 83 |
self, input: dict[str, Any], expected: dict[str, Any] |
| 84 |
) -> None: |
| 85 |
self.assertEqual(normalize_service(input, sub_dir="./sub_dir"), expected) |
| 86 |
|
| 87 |
@parameterized.expand([ |
| 88 |
([], []), |
| 89 |
(["sh"], ["sh"]), |
| 90 |
(["sh", "-c", "date"], ["sh", "-c", "date"]), |
| 91 |
("sh", ["sh"]), |
| 92 |
("sleep infinity", ["sleep", "infinity"]), |
| 93 |
( |
| 94 |
"bash -c 'sleep infinity'", |
| 95 |
["bash", "-c", "sleep infinity"], |
| 96 |
), |
| 97 |
]) |
| 98 |
def test_command_like(self, input: Union[list[str], str], expected: list[str]) -> None: |
| 99 |
for key in ['command', 'entrypoint']: |
| 100 |
input_service = {} |
| 101 |
input_service[key] = input |
| 102 |
|
| 103 |
expected_service = {} |
| 104 |
expected_service[key] = expected |
| 105 |
self.assertEqual(normalize_service(input_service), expected_service) |