2024-03-08 11:19:12 +01:00
|
|
|
# SPDX-License-Identifier: GPL-2.0
|
2023-04-30 05:37:23 +02:00
|
|
|
# pylint: disable=protected-access
|
2024-03-09 17:06:54 +01:00
|
|
|
from __future__ import annotations
|
2023-04-30 05:37:23 +02:00
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
2024-03-08 10:00:34 +01:00
|
|
|
import unittest
|
2024-03-08 22:46:19 +01:00
|
|
|
|
|
|
|
import yaml
|
2024-03-08 10:00:34 +01:00
|
|
|
from parameterized import parameterized
|
2024-03-08 22:46:19 +01:00
|
|
|
|
|
|
|
from podman_compose import PodmanCompose
|
|
|
|
from podman_compose import normalize_final
|
|
|
|
from podman_compose import normalize_service_final
|
2023-04-30 05:37:23 +02:00
|
|
|
|
2023-05-01 08:41:29 +02:00
|
|
|
cwd = os.path.abspath(".")
|
2024-03-08 10:00:34 +01:00
|
|
|
|
|
|
|
|
2024-03-09 22:34:06 +01:00
|
|
|
class TestNormalizeFinalBuild(unittest.TestCase):
|
2024-03-08 10:00:34 +01:00
|
|
|
cases_simple_normalization = [
|
|
|
|
({"image": "test-image"}, {"image": "test-image"}),
|
|
|
|
(
|
|
|
|
{"build": "."},
|
|
|
|
{
|
|
|
|
"build": {"context": cwd, "dockerfile": "Dockerfile"},
|
2023-04-30 05:37:23 +02:00
|
|
|
},
|
2024-03-08 10:00:34 +01:00
|
|
|
),
|
|
|
|
(
|
|
|
|
{"build": "../relative"},
|
|
|
|
{
|
|
|
|
"build": {
|
|
|
|
"context": os.path.normpath(os.path.join(cwd, "../relative")),
|
|
|
|
"dockerfile": "Dockerfile",
|
|
|
|
},
|
2023-04-30 05:37:23 +02:00
|
|
|
},
|
2024-03-08 10:00:34 +01:00
|
|
|
),
|
|
|
|
(
|
|
|
|
{"build": "./relative"},
|
|
|
|
{
|
|
|
|
"build": {
|
|
|
|
"context": os.path.normpath(os.path.join(cwd, "./relative")),
|
|
|
|
"dockerfile": "Dockerfile",
|
|
|
|
},
|
2023-04-30 05:37:23 +02:00
|
|
|
},
|
2024-03-08 10:00:34 +01:00
|
|
|
),
|
|
|
|
(
|
|
|
|
{"build": "/workspace/absolute"},
|
|
|
|
{
|
|
|
|
"build": {
|
|
|
|
"context": "/workspace/absolute",
|
|
|
|
"dockerfile": "Dockerfile",
|
|
|
|
},
|
2023-04-30 05:37:23 +02:00
|
|
|
},
|
2024-03-08 10:00:34 +01:00
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
"build": {
|
|
|
|
"dockerfile": "Dockerfile",
|
|
|
|
},
|
2023-04-30 05:37:23 +02:00
|
|
|
},
|
2024-03-08 10:00:34 +01:00
|
|
|
{
|
|
|
|
"build": {
|
|
|
|
"context": cwd,
|
|
|
|
"dockerfile": "Dockerfile",
|
|
|
|
},
|
2023-04-30 05:37:23 +02:00
|
|
|
},
|
2024-03-08 10:00:34 +01:00
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
"build": {
|
|
|
|
"context": ".",
|
|
|
|
},
|
2023-04-30 05:37:23 +02:00
|
|
|
},
|
2024-03-08 10:00:34 +01:00
|
|
|
{
|
|
|
|
"build": {
|
|
|
|
"context": cwd,
|
|
|
|
"dockerfile": "Dockerfile",
|
|
|
|
},
|
2023-04-30 05:37:23 +02:00
|
|
|
},
|
2024-03-08 10:00:34 +01:00
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
"build": {"context": "../", "dockerfile": "test-dockerfile"},
|
2023-04-30 05:37:23 +02:00
|
|
|
},
|
2024-03-08 10:00:34 +01:00
|
|
|
{
|
|
|
|
"build": {
|
|
|
|
"context": os.path.normpath(os.path.join(cwd, "../")),
|
|
|
|
"dockerfile": "test-dockerfile",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
"build": {"context": ".", "dockerfile": "./dev/test-dockerfile"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"build": {
|
|
|
|
"context": cwd,
|
|
|
|
"dockerfile": "./dev/test-dockerfile",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
]
|
2023-04-30 05:37:23 +02:00
|
|
|
|
2024-03-08 10:00:34 +01:00
|
|
|
@parameterized.expand(cases_simple_normalization)
|
|
|
|
def test_normalize_service_final_returns_absolute_path_in_context(self, input, expected):
|
|
|
|
# Tests that [service.build] is normalized after merges
|
|
|
|
project_dir = cwd
|
|
|
|
self.assertEqual(normalize_service_final(input, project_dir), expected)
|
2023-04-30 05:37:23 +02:00
|
|
|
|
2024-03-08 10:00:34 +01:00
|
|
|
@parameterized.expand(cases_simple_normalization)
|
|
|
|
def test_normalize_returns_absolute_path_in_context(self, input, expected):
|
|
|
|
project_dir = cwd
|
|
|
|
compose_test = {"services": {"test-service": input}}
|
|
|
|
compose_expected = {"services": {"test-service": expected}}
|
|
|
|
self.assertEqual(normalize_final(compose_test, project_dir), compose_expected)
|
2023-04-30 05:37:23 +02:00
|
|
|
|
2024-03-08 10:00:34 +01:00
|
|
|
@parameterized.expand(cases_simple_normalization)
|
|
|
|
def test_parse_compose_file_when_single_compose(self, input, expected):
|
|
|
|
compose_test = {"services": {"test-service": input}}
|
2023-04-30 05:37:23 +02:00
|
|
|
dump_yaml(compose_test, "test-compose.yaml")
|
|
|
|
|
|
|
|
podman_compose = PodmanCompose()
|
2023-06-11 07:19:14 +02:00
|
|
|
set_args(podman_compose, ["test-compose.yaml"], no_normalize=None)
|
2023-04-30 05:37:23 +02:00
|
|
|
|
|
|
|
podman_compose._parse_compose_file()
|
|
|
|
|
|
|
|
actual_compose = {}
|
|
|
|
if podman_compose.services:
|
|
|
|
podman_compose.services["test-service"].pop("_deps")
|
|
|
|
actual_compose = podman_compose.services["test-service"]
|
2024-03-08 10:00:34 +01:00
|
|
|
self.assertEqual(actual_compose, expected)
|
2023-04-30 05:37:23 +02:00
|
|
|
|
2024-03-08 10:00:34 +01:00
|
|
|
@parameterized.expand([
|
|
|
|
(
|
|
|
|
{},
|
|
|
|
{"build": "."},
|
|
|
|
{"build": {"context": cwd, "dockerfile": "Dockerfile"}},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{"build": "."},
|
|
|
|
{},
|
|
|
|
{"build": {"context": cwd, "dockerfile": "Dockerfile"}},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{"build": "/workspace/absolute"},
|
|
|
|
{"build": "./relative"},
|
|
|
|
{
|
|
|
|
"build": {
|
|
|
|
"context": os.path.normpath(os.path.join(cwd, "./relative")),
|
|
|
|
"dockerfile": "Dockerfile",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{"build": "./relative"},
|
|
|
|
{"build": "/workspace/absolute"},
|
|
|
|
{"build": {"context": "/workspace/absolute", "dockerfile": "Dockerfile"}},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{"build": "./relative"},
|
|
|
|
{"build": "/workspace/absolute"},
|
|
|
|
{"build": {"context": "/workspace/absolute", "dockerfile": "Dockerfile"}},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{"build": {"dockerfile": "test-dockerfile"}},
|
|
|
|
{},
|
|
|
|
{"build": {"context": cwd, "dockerfile": "test-dockerfile"}},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{},
|
|
|
|
{"build": {"dockerfile": "test-dockerfile"}},
|
|
|
|
{"build": {"context": cwd, "dockerfile": "test-dockerfile"}},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{},
|
|
|
|
{"build": {"dockerfile": "test-dockerfile"}},
|
|
|
|
{"build": {"context": cwd, "dockerfile": "test-dockerfile"}},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{"build": {"dockerfile": "test-dockerfile-1"}},
|
|
|
|
{"build": {"dockerfile": "test-dockerfile-2"}},
|
|
|
|
{"build": {"context": cwd, "dockerfile": "test-dockerfile-2"}},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{"build": "/workspace/absolute"},
|
|
|
|
{"build": {"dockerfile": "test-dockerfile"}},
|
|
|
|
{"build": {"context": "/workspace/absolute", "dockerfile": "test-dockerfile"}},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{"build": {"dockerfile": "test-dockerfile"}},
|
|
|
|
{"build": "/workspace/absolute"},
|
|
|
|
{"build": {"context": "/workspace/absolute", "dockerfile": "test-dockerfile"}},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{"build": {"dockerfile": "./test-dockerfile-1"}},
|
|
|
|
{"build": {"dockerfile": "./test-dockerfile-2", "args": ["ENV1=1"]}},
|
|
|
|
{
|
|
|
|
"build": {
|
|
|
|
"context": cwd,
|
|
|
|
"dockerfile": "./test-dockerfile-2",
|
|
|
|
"args": ["ENV1=1"],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{"build": {"dockerfile": "./test-dockerfile-1", "args": ["ENV1=1"]}},
|
|
|
|
{"build": {"dockerfile": "./test-dockerfile-2"}},
|
|
|
|
{
|
|
|
|
"build": {
|
|
|
|
"context": cwd,
|
|
|
|
"dockerfile": "./test-dockerfile-2",
|
|
|
|
"args": ["ENV1=1"],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
{"build": {"dockerfile": "./test-dockerfile-1", "args": ["ENV1=1"]}},
|
|
|
|
{"build": {"dockerfile": "./test-dockerfile-2", "args": ["ENV2=2"]}},
|
|
|
|
{
|
|
|
|
"build": {
|
|
|
|
"context": cwd,
|
|
|
|
"dockerfile": "./test-dockerfile-2",
|
|
|
|
"args": ["ENV1=1", "ENV2=2"],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
])
|
|
|
|
def test_parse_when_multiple_composes(self, input, override, expected):
|
|
|
|
compose_test_1 = {"services": {"test-service": input}}
|
|
|
|
compose_test_2 = {"services": {"test-service": override}}
|
2023-04-30 05:37:23 +02:00
|
|
|
dump_yaml(compose_test_1, "test-compose-1.yaml")
|
|
|
|
dump_yaml(compose_test_2, "test-compose-2.yaml")
|
|
|
|
|
|
|
|
podman_compose = PodmanCompose()
|
2023-06-11 07:19:14 +02:00
|
|
|
set_args(
|
|
|
|
podman_compose,
|
|
|
|
["test-compose-1.yaml", "test-compose-2.yaml"],
|
|
|
|
no_normalize=None,
|
|
|
|
)
|
2023-04-30 05:37:23 +02:00
|
|
|
|
|
|
|
podman_compose._parse_compose_file()
|
|
|
|
|
|
|
|
actual_compose = {}
|
|
|
|
if podman_compose.services:
|
|
|
|
podman_compose.services["test-service"].pop("_deps")
|
|
|
|
actual_compose = podman_compose.services["test-service"]
|
2024-03-08 10:00:34 +01:00
|
|
|
self.assertEqual(actual_compose, expected)
|
2023-04-30 05:37:23 +02:00
|
|
|
|
|
|
|
|
2024-03-07 17:28:06 +01:00
|
|
|
def set_args(podman_compose: PodmanCompose, file_names: list[str], no_normalize: bool) -> None:
|
2023-04-30 05:37:23 +02:00
|
|
|
podman_compose.global_args = argparse.Namespace()
|
|
|
|
podman_compose.global_args.file = file_names
|
|
|
|
podman_compose.global_args.project_name = None
|
|
|
|
podman_compose.global_args.env_file = None
|
|
|
|
podman_compose.global_args.profile = []
|
|
|
|
podman_compose.global_args.in_pod = True
|
2023-06-11 07:19:14 +02:00
|
|
|
podman_compose.global_args.no_normalize = no_normalize
|
2023-04-30 05:37:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
def dump_yaml(compose: dict, name: str) -> None:
|
|
|
|
# Path(Path.cwd()/"subdirectory").mkdir(parents=True, exist_ok=True)
|
|
|
|
with open(name, "w", encoding="utf-8") as outfile:
|
|
|
|
yaml.safe_dump(compose, outfile, default_flow_style=False)
|
|
|
|
|
|
|
|
|
|
|
|
def test_clean_test_yamls() -> None:
|
|
|
|
test_files = ["test-compose-1.yaml", "test-compose-2.yaml", "test-compose.yaml"]
|
|
|
|
for file in test_files:
|
2023-05-01 08:41:29 +02:00
|
|
|
if os.path.exists(file):
|
2023-04-30 05:37:23 +02:00
|
|
|
os.remove(file)
|