| 1 |
|
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import argparse |
| 6 |
import os |
| 7 |
import unittest |
| 8 |
|
| 9 |
import yaml |
| 10 |
from parameterized import parameterized |
| 11 |
|
| 12 |
from podman_compose import PodmanCompose |
| 13 |
from podman_compose import normalize_final |
| 14 |
from podman_compose import normalize_service_final |
| 15 |
|
| 16 |
cwd = os.path.abspath(".") |
| 17 |
|
| 18 |
|
| 19 |
class TestNormalizeFinalBuild(unittest.TestCase): |
| 20 |
cases_simple_normalization = [ |
| 21 |
({"image": "test-image"}, {"image": "test-image"}), |
| 22 |
( |
| 23 |
{"build": "."}, |
| 24 |
{ |
| 25 |
"build": {"context": cwd}, |
| 26 |
}, |
| 27 |
), |
| 28 |
( |
| 29 |
{"build": "../relative"}, |
| 30 |
{ |
| 31 |
"build": { |
| 32 |
"context": os.path.normpath(os.path.join(cwd, "../relative")), |
| 33 |
}, |
| 34 |
}, |
| 35 |
), |
| 36 |
( |
| 37 |
{"build": "./relative"}, |
| 38 |
{ |
| 39 |
"build": { |
| 40 |
"context": os.path.normpath(os.path.join(cwd, "./relative")), |
| 41 |
}, |
| 42 |
}, |
| 43 |
), |
| 44 |
( |
| 45 |
{"build": "/workspace/absolute"}, |
| 46 |
{ |
| 47 |
"build": { |
| 48 |
"context": "/workspace/absolute", |
| 49 |
}, |
| 50 |
}, |
| 51 |
), |
| 52 |
( |
| 53 |
{ |
| 54 |
"build": { |
| 55 |
"dockerfile": "Dockerfile", |
| 56 |
}, |
| 57 |
}, |
| 58 |
{ |
| 59 |
"build": { |
| 60 |
"context": cwd, |
| 61 |
"dockerfile": "Dockerfile", |
| 62 |
}, |
| 63 |
}, |
| 64 |
), |
| 65 |
( |
| 66 |
{ |
| 67 |
"build": { |
| 68 |
"context": ".", |
| 69 |
}, |
| 70 |
}, |
| 71 |
{ |
| 72 |
"build": { |
| 73 |
"context": cwd, |
| 74 |
}, |
| 75 |
}, |
| 76 |
), |
| 77 |
( |
| 78 |
{ |
| 79 |
"build": {"context": "../", "dockerfile": "test-dockerfile"}, |
| 80 |
}, |
| 81 |
{ |
| 82 |
"build": { |
| 83 |
"context": os.path.normpath(os.path.join(cwd, "../")), |
| 84 |
"dockerfile": "test-dockerfile", |
| 85 |
}, |
| 86 |
}, |
| 87 |
), |
| 88 |
( |
| 89 |
{ |
| 90 |
"build": {"context": ".", "dockerfile": "./dev/test-dockerfile"}, |
| 91 |
}, |
| 92 |
{ |
| 93 |
"build": { |
| 94 |
"context": cwd, |
| 95 |
"dockerfile": "./dev/test-dockerfile", |
| 96 |
}, |
| 97 |
}, |
| 98 |
), |
| 99 |
] |
| 100 |
|
| 101 |
@parameterized.expand(cases_simple_normalization) |
| 102 |
def test_normalize_service_final_returns_absolute_path_in_context(self, input, expected): |
| 103 |
|
| 104 |
project_dir = cwd |
| 105 |
self.assertEqual(normalize_service_final(input, project_dir), expected) |
| 106 |
|
| 107 |
@parameterized.expand(cases_simple_normalization) |
| 108 |
def test_normalize_returns_absolute_path_in_context(self, input, expected): |
| 109 |
project_dir = cwd |
| 110 |
compose_test = {"services": {"test-service": input}} |
| 111 |
compose_expected = {"services": {"test-service": expected}} |
| 112 |
self.assertEqual(normalize_final(compose_test, project_dir), compose_expected) |
| 113 |
|
| 114 |
@parameterized.expand(cases_simple_normalization) |
| 115 |
def test_parse_compose_file_when_single_compose(self, input, expected): |
| 116 |
compose_test = {"services": {"test-service": input}} |
| 117 |
dump_yaml(compose_test, "test-compose.yaml") |
| 118 |
|
| 119 |
podman_compose = PodmanCompose() |
| 120 |
set_args(podman_compose, ["test-compose.yaml"], no_normalize=None) |
| 121 |
|
| 122 |
podman_compose._parse_compose_file() |
| 123 |
|
| 124 |
actual_compose = {} |
| 125 |
if podman_compose.services: |
| 126 |
actual_compose = podman_compose.original_configuration( |
| 127 |
podman_compose.services["test-service"] |
| 128 |
) |
| 129 |
self.assertEqual(actual_compose, expected) |
| 130 |
|
| 131 |
@parameterized.expand([ |
| 132 |
( |
| 133 |
{}, |
| 134 |
{"build": "."}, |
| 135 |
{"build": {"context": cwd}}, |
| 136 |
), |
| 137 |
( |
| 138 |
{"build": "."}, |
| 139 |
{}, |
| 140 |
{"build": {"context": cwd}}, |
| 141 |
), |
| 142 |
( |
| 143 |
{"build": "/workspace/absolute"}, |
| 144 |
{"build": "./relative"}, |
| 145 |
{ |
| 146 |
"build": { |
| 147 |
"context": os.path.normpath(os.path.join(cwd, "./relative")), |
| 148 |
} |
| 149 |
}, |
| 150 |
), |
| 151 |
( |
| 152 |
{"build": "./relative"}, |
| 153 |
{"build": "/workspace/absolute"}, |
| 154 |
{"build": {"context": "/workspace/absolute"}}, |
| 155 |
), |
| 156 |
( |
| 157 |
{"build": "./relative"}, |
| 158 |
{"build": "/workspace/absolute"}, |
| 159 |
{"build": {"context": "/workspace/absolute"}}, |
| 160 |
), |
| 161 |
( |
| 162 |
{"build": {"dockerfile": "test-dockerfile"}}, |
| 163 |
{}, |
| 164 |
{"build": {"context": cwd, "dockerfile": "test-dockerfile"}}, |
| 165 |
), |
| 166 |
( |
| 167 |
{}, |
| 168 |
{"build": {"dockerfile": "test-dockerfile"}}, |
| 169 |
{"build": {"context": cwd, "dockerfile": "test-dockerfile"}}, |
| 170 |
), |
| 171 |
( |
| 172 |
{}, |
| 173 |
{"build": {"dockerfile": "test-dockerfile"}}, |
| 174 |
{"build": {"context": cwd, "dockerfile": "test-dockerfile"}}, |
| 175 |
), |
| 176 |
( |
| 177 |
{"build": {"dockerfile": "test-dockerfile-1"}}, |
| 178 |
{"build": {"dockerfile": "test-dockerfile-2"}}, |
| 179 |
{"build": {"context": cwd, "dockerfile": "test-dockerfile-2"}}, |
| 180 |
), |
| 181 |
( |
| 182 |
{"build": "/workspace/absolute"}, |
| 183 |
{"build": {"dockerfile": "test-dockerfile"}}, |
| 184 |
{"build": {"context": "/workspace/absolute", "dockerfile": "test-dockerfile"}}, |
| 185 |
), |
| 186 |
( |
| 187 |
{"build": {"dockerfile": "test-dockerfile"}}, |
| 188 |
{"build": "/workspace/absolute"}, |
| 189 |
{"build": {"context": "/workspace/absolute", "dockerfile": "test-dockerfile"}}, |
| 190 |
), |
| 191 |
( |
| 192 |
{"build": {"dockerfile": "./test-dockerfile-1"}}, |
| 193 |
{"build": {"dockerfile": "./test-dockerfile-2", "args": ["ENV1=1"]}}, |
| 194 |
{ |
| 195 |
"build": { |
| 196 |
"context": cwd, |
| 197 |
"dockerfile": "./test-dockerfile-2", |
| 198 |
"args": ["ENV1=1"], |
| 199 |
} |
| 200 |
}, |
| 201 |
), |
| 202 |
( |
| 203 |
{"build": {"dockerfile": "./test-dockerfile-1", "args": ["ENV1=1"]}}, |
| 204 |
{"build": {"dockerfile": "./test-dockerfile-2"}}, |
| 205 |
{ |
| 206 |
"build": { |
| 207 |
"context": cwd, |
| 208 |
"dockerfile": "./test-dockerfile-2", |
| 209 |
"args": ["ENV1=1"], |
| 210 |
} |
| 211 |
}, |
| 212 |
), |
| 213 |
( |
| 214 |
{"build": {"dockerfile": "./test-dockerfile-1", "args": ["ENV1=1"]}}, |
| 215 |
{"build": {"dockerfile": "./test-dockerfile-2", "args": ["ENV2=2"]}}, |
| 216 |
{ |
| 217 |
"build": { |
| 218 |
"context": cwd, |
| 219 |
"dockerfile": "./test-dockerfile-2", |
| 220 |
"args": ["ENV1=1", "ENV2=2"], |
| 221 |
} |
| 222 |
}, |
| 223 |
), |
| 224 |
]) |
| 225 |
def test_parse_when_multiple_composes(self, input, override, expected): |
| 226 |
compose_test_1 = {"services": {"test-service": input}} |
| 227 |
compose_test_2 = {"services": {"test-service": override}} |
| 228 |
dump_yaml(compose_test_1, "test-compose-1.yaml") |
| 229 |
dump_yaml(compose_test_2, "test-compose-2.yaml") |
| 230 |
|
| 231 |
podman_compose = PodmanCompose() |
| 232 |
set_args( |
| 233 |
podman_compose, |
| 234 |
["test-compose-1.yaml", "test-compose-2.yaml"], |
| 235 |
no_normalize=None, |
| 236 |
) |
| 237 |
|
| 238 |
podman_compose._parse_compose_file() |
| 239 |
|
| 240 |
actual_compose = {} |
| 241 |
if podman_compose.services: |
| 242 |
actual_compose = podman_compose.original_configuration( |
| 243 |
podman_compose.services["test-service"] |
| 244 |
) |
| 245 |
self.assertEqual(actual_compose, expected) |
| 246 |
|
| 247 |
|
| 248 |
def set_args(podman_compose: PodmanCompose, file_names: list[str], no_normalize: bool) -> None: |
| 249 |
podman_compose.global_args = argparse.Namespace() |
| 250 |
podman_compose.global_args.file = file_names |
| 251 |
podman_compose.global_args.project_name = None |
| 252 |
podman_compose.global_args.env_file = None |
| 253 |
podman_compose.global_args.profile = [] |
| 254 |
podman_compose.global_args.in_pod = "1" |
| 255 |
podman_compose.global_args.pod_args = None |
| 256 |
podman_compose.global_args.no_normalize = no_normalize |
| 257 |
|
| 258 |
|
| 259 |
def dump_yaml(compose: dict, name: str) -> None: |
| 260 |
|
| 261 |
with open(name, "w", encoding="utf-8") as outfile: |
| 262 |
yaml.safe_dump(compose, outfile, default_flow_style=False) |
| 263 |
|
| 264 |
|
| 265 |
def test_clean_test_yamls() -> None: |
| 266 |
test_files = ["test-compose-1.yaml", "test-compose-2.yaml", "test-compose.yaml"] |
| 267 |
for file in test_files: |
| 268 |
if os.path.exists(file): |
| 269 |
os.remove(file) |