| 1 |
|
| 2 |
from __future__ import annotations |
| 3 |
|
| 4 |
import argparse |
| 5 |
import copy |
| 6 |
import os |
| 7 |
import unittest |
| 8 |
|
| 9 |
import yaml |
| 10 |
from parameterized import parameterized |
| 11 |
|
| 12 |
from podman_compose import PodmanCompose |
| 13 |
|
| 14 |
|
| 15 |
class TestCanMergeBuild(unittest.TestCase): |
| 16 |
@parameterized.expand([ |
| 17 |
({}, {}, {}), |
| 18 |
({}, {"test": "test"}, {"test": "test"}), |
| 19 |
({"test": "test"}, {}, {"test": "test"}), |
| 20 |
({"test": "test-1"}, {"test": "test-2"}, {"test": "test-2"}), |
| 21 |
({}, {"build": "."}, {"build": {"context": "."}}), |
| 22 |
({"build": "."}, {}, {"build": {"context": "."}}), |
| 23 |
({"build": "./dir-1"}, {"build": "./dir-2"}, {"build": {"context": "./dir-2"}}), |
| 24 |
({}, {"build": {"context": "./dir-1"}}, {"build": {"context": "./dir-1"}}), |
| 25 |
({"build": {"context": "./dir-1"}}, {}, {"build": {"context": "./dir-1"}}), |
| 26 |
( |
| 27 |
{"build": {"context": "./dir-1"}}, |
| 28 |
{"build": {"context": "./dir-2"}}, |
| 29 |
{"build": {"context": "./dir-2"}}, |
| 30 |
), |
| 31 |
( |
| 32 |
{}, |
| 33 |
{"build": {"dockerfile": "dockerfile-1"}}, |
| 34 |
{"build": {"dockerfile": "dockerfile-1"}}, |
| 35 |
), |
| 36 |
( |
| 37 |
{"build": {"dockerfile": "dockerfile-1"}}, |
| 38 |
{}, |
| 39 |
{"build": {"dockerfile": "dockerfile-1"}}, |
| 40 |
), |
| 41 |
( |
| 42 |
{"build": {"dockerfile": "./dockerfile-1"}}, |
| 43 |
{"build": {"dockerfile": "./dockerfile-2"}}, |
| 44 |
{"build": {"dockerfile": "./dockerfile-2"}}, |
| 45 |
), |
| 46 |
( |
| 47 |
{"build": {"dockerfile": "./dockerfile-1"}}, |
| 48 |
{"build": {"context": "./dir-2"}}, |
| 49 |
{"build": {"dockerfile": "./dockerfile-1", "context": "./dir-2"}}, |
| 50 |
), |
| 51 |
( |
| 52 |
{"build": {"dockerfile": "./dockerfile-1", "context": "./dir-1"}}, |
| 53 |
{"build": {"dockerfile": "./dockerfile-2", "context": "./dir-2"}}, |
| 54 |
{"build": {"dockerfile": "./dockerfile-2", "context": "./dir-2"}}, |
| 55 |
), |
| 56 |
( |
| 57 |
{"build": {"dockerfile": "./dockerfile-1"}}, |
| 58 |
{"build": {"dockerfile": "./dockerfile-2", "args": ["ENV1=1"]}}, |
| 59 |
{"build": {"dockerfile": "./dockerfile-2", "args": ["ENV1=1"]}}, |
| 60 |
), |
| 61 |
( |
| 62 |
{"build": {"dockerfile": "./dockerfile-2", "args": ["ENV1=1"]}}, |
| 63 |
{"build": {"dockerfile": "./dockerfile-1"}}, |
| 64 |
{"build": {"dockerfile": "./dockerfile-1", "args": ["ENV1=1"]}}, |
| 65 |
), |
| 66 |
( |
| 67 |
{"build": {"dockerfile": "./dockerfile-2", "args": ["ENV1=1"]}}, |
| 68 |
{"build": {"dockerfile": "./dockerfile-1", "args": ["ENV2=2"]}}, |
| 69 |
{"build": {"dockerfile": "./dockerfile-1", "args": ["ENV1=1", "ENV2=2"]}}, |
| 70 |
), |
| 71 |
( |
| 72 |
{"build": {"dockerfile": "./dockerfile-1", "args": {"ENV1": "1"}}}, |
| 73 |
{"build": {"dockerfile": "./dockerfile-2", "args": {"ENV2": "2"}}}, |
| 74 |
{"build": {"dockerfile": "./dockerfile-2", "args": ["ENV1=1", "ENV2=2"]}}, |
| 75 |
), |
| 76 |
( |
| 77 |
{"build": {"dockerfile": "./dockerfile-1", "args": ["ENV1=1"]}}, |
| 78 |
{"build": {"dockerfile": "./dockerfile-2", "args": {"ENV2": "2"}}}, |
| 79 |
{"build": {"dockerfile": "./dockerfile-2", "args": ["ENV1=1", "ENV2=2"]}}, |
| 80 |
), |
| 81 |
]) |
| 82 |
def test_parse_compose_file_when_multiple_composes(self, input, override, expected): |
| 83 |
compose_test_1 = {"services": {"test-service": input}} |
| 84 |
compose_test_2 = {"services": {"test-service": override}} |
| 85 |
dump_yaml(compose_test_1, "test-compose-1.yaml") |
| 86 |
dump_yaml(compose_test_2, "test-compose-2.yaml") |
| 87 |
|
| 88 |
podman_compose = PodmanCompose() |
| 89 |
set_args(podman_compose, ["test-compose-1.yaml", "test-compose-2.yaml"]) |
| 90 |
|
| 91 |
podman_compose._parse_compose_file() |
| 92 |
|
| 93 |
actual_compose = {} |
| 94 |
if podman_compose.services: |
| 95 |
actual_compose = podman_compose.original_configuration( |
| 96 |
podman_compose.services["test-service"] |
| 97 |
) |
| 98 |
self.assertEqual(actual_compose, expected) |
| 99 |
|
| 100 |
def test_parse_with_map_merge_into_none(self): |
| 101 |
compose_test_1 = {"volumes": {"vol_a": None}} |
| 102 |
compose_test_2 = { |
| 103 |
"volumes": { |
| 104 |
"vol_a": { |
| 105 |
"driver_opts": {"type": "none", "device": "/dev/some", "o": "bind"}, |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
expected = {"driver_opts": {"device": "/dev/some", "o": "bind", "type": "none"}} |
| 110 |
dump_yaml(compose_test_1, "test-compose-1.yaml") |
| 111 |
dump_yaml(compose_test_2, "test-compose-2.yaml") |
| 112 |
|
| 113 |
podman_compose = PodmanCompose() |
| 114 |
set_args(podman_compose, ["test-compose-1.yaml", "test-compose-2.yaml"]) |
| 115 |
|
| 116 |
podman_compose._parse_compose_file() |
| 117 |
|
| 118 |
actual_compose = podman_compose.vols["vol_a"] |
| 119 |
self.assertEqual(actual_compose, expected) |
| 120 |
|
| 121 |
|
| 122 |
@parameterized.expand([ |
| 123 |
({}, {"$$$": []}, {"$$$": []}), |
| 124 |
({"$$$": []}, {}, {"$$$": []}), |
| 125 |
({"$$$": []}, {"$$$": "sh-2"}, {"$$$": ["sh-2"]}), |
| 126 |
({"$$$": "sh-2"}, {"$$$": []}, {"$$$": []}), |
| 127 |
({}, {"$$$": "sh"}, {"$$$": ["sh"]}), |
| 128 |
({"$$$": "sh"}, {}, {"$$$": ["sh"]}), |
| 129 |
({"$$$": "sh-1"}, {"$$$": "sh-2"}, {"$$$": ["sh-2"]}), |
| 130 |
({"$$$": ["sh-1"]}, {"$$$": "sh-2"}, {"$$$": ["sh-2"]}), |
| 131 |
({"$$$": "sh-1"}, {"$$$": ["sh-2"]}, {"$$$": ["sh-2"]}), |
| 132 |
({"$$$": "sh-1"}, {"$$$": ["sh-2", "sh-3"]}, {"$$$": ["sh-2", "sh-3"]}), |
| 133 |
({"$$$": ["sh-1"]}, {"$$$": ["sh-2", "sh-3"]}, {"$$$": ["sh-2", "sh-3"]}), |
| 134 |
({"$$$": ["sh-1", "sh-2"]}, {"$$$": ["sh-3", "sh-4"]}, {"$$$": ["sh-3", "sh-4"]}), |
| 135 |
({}, {"$$$": ["sh-3", "sh 4"]}, {"$$$": ["sh-3", "sh 4"]}), |
| 136 |
({"$$$": "sleep infinity"}, {"$$$": "sh"}, {"$$$": ["sh"]}), |
| 137 |
({"$$$": "sh"}, {"$$$": "sleep infinity"}, {"$$$": ["sleep", "infinity"]}), |
| 138 |
( |
| 139 |
{}, |
| 140 |
{"$$$": "bash -c 'sleep infinity'"}, |
| 141 |
{"$$$": ["bash", "-c", "sleep infinity"]}, |
| 142 |
), |
| 143 |
]) |
| 144 |
def test_parse_compose_file_when_multiple_composes_keys_command_entrypoint( |
| 145 |
self, base_template, override_template, expected_template |
| 146 |
): |
| 147 |
for key in ['command', 'entrypoint']: |
| 148 |
base, override, expected = template_to_expression( |
| 149 |
base_template, override_template, expected_template, key |
| 150 |
) |
| 151 |
compose_test_1 = {"services": {"test-service": base}} |
| 152 |
compose_test_2 = {"services": {"test-service": override}} |
| 153 |
dump_yaml(compose_test_1, "test-compose-1.yaml") |
| 154 |
dump_yaml(compose_test_2, "test-compose-2.yaml") |
| 155 |
|
| 156 |
podman_compose = PodmanCompose() |
| 157 |
set_args(podman_compose, ["test-compose-1.yaml", "test-compose-2.yaml"]) |
| 158 |
|
| 159 |
podman_compose._parse_compose_file() |
| 160 |
|
| 161 |
actual = {} |
| 162 |
if podman_compose.services: |
| 163 |
actual = podman_compose.original_configuration( |
| 164 |
podman_compose.services["test-service"] |
| 165 |
) |
| 166 |
self.assertEqual(actual, expected) |
| 167 |
|
| 168 |
|
| 169 |
def set_args(podman_compose: PodmanCompose, file_names: list[str]) -> None: |
| 170 |
podman_compose.global_args = argparse.Namespace() |
| 171 |
podman_compose.global_args.file = file_names |
| 172 |
podman_compose.global_args.project_name = None |
| 173 |
podman_compose.global_args.env_file = None |
| 174 |
podman_compose.global_args.profile = [] |
| 175 |
podman_compose.global_args.in_pod = "1" |
| 176 |
podman_compose.global_args.pod_args = None |
| 177 |
podman_compose.global_args.no_normalize = True |
| 178 |
|
| 179 |
|
| 180 |
def dump_yaml(compose: dict, name: str) -> None: |
| 181 |
with open(name, "w", encoding="utf-8") as outfile: |
| 182 |
yaml.safe_dump(compose, outfile, default_flow_style=False) |
| 183 |
|
| 184 |
|
| 185 |
def template_to_expression(base, override, expected, key): |
| 186 |
base_copy = copy.deepcopy(base) |
| 187 |
override_copy = copy.deepcopy(override) |
| 188 |
expected_copy = copy.deepcopy(expected) |
| 189 |
|
| 190 |
expected_copy[key] = expected_copy.pop("$$$") |
| 191 |
if "$$$" in base: |
| 192 |
base_copy[key] = base_copy.pop("$$$") |
| 193 |
if "$$$" in override: |
| 194 |
override_copy[key] = override_copy.pop("$$$") |
| 195 |
return base_copy, override_copy, expected_copy |
| 196 |
|
| 197 |
|
| 198 |
def test_clean_test_yamls() -> None: |
| 199 |
test_files = ["test-compose-1.yaml", "test-compose-2.yaml"] |
| 200 |
for file in test_files: |
| 201 |
if os.path.exists(file): |
| 202 |
os.remove(file) |