File: test_can_merge_build.py

package info (click to toggle)
podman-compose 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,996 kB
  • sloc: python: 10,946; sh: 107; javascript: 48; makefile: 13
file content (179 lines) | stat: -rw-r--r-- 7,559 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# SPDX-License-Identifier: GPL-2.0
from __future__ import annotations

import argparse
import copy
import os
import unittest

import yaml
from parameterized import parameterized

from podman_compose import PodmanCompose


class TestCanMergeBuild(unittest.TestCase):
    @parameterized.expand([
        ({}, {}, {}),
        ({}, {"test": "test"}, {"test": "test"}),
        ({"test": "test"}, {}, {"test": "test"}),
        ({"test": "test-1"}, {"test": "test-2"}, {"test": "test-2"}),
        ({}, {"build": "."}, {"build": {"context": "."}}),
        ({"build": "."}, {}, {"build": {"context": "."}}),
        ({"build": "./dir-1"}, {"build": "./dir-2"}, {"build": {"context": "./dir-2"}}),
        ({}, {"build": {"context": "./dir-1"}}, {"build": {"context": "./dir-1"}}),
        ({"build": {"context": "./dir-1"}}, {}, {"build": {"context": "./dir-1"}}),
        (
            {"build": {"context": "./dir-1"}},
            {"build": {"context": "./dir-2"}},
            {"build": {"context": "./dir-2"}},
        ),
        (
            {},
            {"build": {"dockerfile": "dockerfile-1"}},
            {"build": {"dockerfile": "dockerfile-1"}},
        ),
        (
            {"build": {"dockerfile": "dockerfile-1"}},
            {},
            {"build": {"dockerfile": "dockerfile-1"}},
        ),
        (
            {"build": {"dockerfile": "./dockerfile-1"}},
            {"build": {"dockerfile": "./dockerfile-2"}},
            {"build": {"dockerfile": "./dockerfile-2"}},
        ),
        (
            {"build": {"dockerfile": "./dockerfile-1"}},
            {"build": {"context": "./dir-2"}},
            {"build": {"dockerfile": "./dockerfile-1", "context": "./dir-2"}},
        ),
        (
            {"build": {"dockerfile": "./dockerfile-1", "context": "./dir-1"}},
            {"build": {"dockerfile": "./dockerfile-2", "context": "./dir-2"}},
            {"build": {"dockerfile": "./dockerfile-2", "context": "./dir-2"}},
        ),
        (
            {"build": {"dockerfile": "./dockerfile-1"}},
            {"build": {"dockerfile": "./dockerfile-2", "args": ["ENV1=1"]}},
            {"build": {"dockerfile": "./dockerfile-2", "args": ["ENV1=1"]}},
        ),
        (
            {"build": {"dockerfile": "./dockerfile-2", "args": ["ENV1=1"]}},
            {"build": {"dockerfile": "./dockerfile-1"}},
            {"build": {"dockerfile": "./dockerfile-1", "args": ["ENV1=1"]}},
        ),
        (
            {"build": {"dockerfile": "./dockerfile-2", "args": ["ENV1=1"]}},
            {"build": {"dockerfile": "./dockerfile-1", "args": ["ENV2=2"]}},
            {"build": {"dockerfile": "./dockerfile-1", "args": ["ENV1=1", "ENV2=2"]}},
        ),
        (
            {"build": {"dockerfile": "./dockerfile-1", "args": {"ENV1": "1"}}},
            {"build": {"dockerfile": "./dockerfile-2", "args": {"ENV2": "2"}}},
            {"build": {"dockerfile": "./dockerfile-2", "args": ["ENV1=1", "ENV2=2"]}},
        ),
        (
            {"build": {"dockerfile": "./dockerfile-1", "args": ["ENV1=1"]}},
            {"build": {"dockerfile": "./dockerfile-2", "args": {"ENV2": "2"}}},
            {"build": {"dockerfile": "./dockerfile-2", "args": ["ENV1=1", "ENV2=2"]}},
        ),
    ])
    def test_parse_compose_file_when_multiple_composes(self, input, override, expected):
        compose_test_1 = {"services": {"test-service": input}}
        compose_test_2 = {"services": {"test-service": override}}
        dump_yaml(compose_test_1, "test-compose-1.yaml")
        dump_yaml(compose_test_2, "test-compose-2.yaml")

        podman_compose = PodmanCompose()
        set_args(podman_compose, ["test-compose-1.yaml", "test-compose-2.yaml"])

        podman_compose._parse_compose_file()  # pylint: disable=protected-access

        actual_compose = {}
        if podman_compose.services:
            podman_compose.services["test-service"].pop("_deps")
            actual_compose = podman_compose.services["test-service"]
        self.assertEqual(actual_compose, expected)

    # $$$ is a placeholder for either command or entrypoint
    @parameterized.expand([
        ({}, {"$$$": []}, {"$$$": []}),
        ({"$$$": []}, {}, {"$$$": []}),
        ({"$$$": []}, {"$$$": "sh-2"}, {"$$$": ["sh-2"]}),
        ({"$$$": "sh-2"}, {"$$$": []}, {"$$$": []}),
        ({}, {"$$$": "sh"}, {"$$$": ["sh"]}),
        ({"$$$": "sh"}, {}, {"$$$": ["sh"]}),
        ({"$$$": "sh-1"}, {"$$$": "sh-2"}, {"$$$": ["sh-2"]}),
        ({"$$$": ["sh-1"]}, {"$$$": "sh-2"}, {"$$$": ["sh-2"]}),
        ({"$$$": "sh-1"}, {"$$$": ["sh-2"]}, {"$$$": ["sh-2"]}),
        ({"$$$": "sh-1"}, {"$$$": ["sh-2", "sh-3"]}, {"$$$": ["sh-2", "sh-3"]}),
        ({"$$$": ["sh-1"]}, {"$$$": ["sh-2", "sh-3"]}, {"$$$": ["sh-2", "sh-3"]}),
        ({"$$$": ["sh-1", "sh-2"]}, {"$$$": ["sh-3", "sh-4"]}, {"$$$": ["sh-3", "sh-4"]}),
        ({}, {"$$$": ["sh-3", "sh      4"]}, {"$$$": ["sh-3", "sh      4"]}),
        ({"$$$": "sleep infinity"}, {"$$$": "sh"}, {"$$$": ["sh"]}),
        ({"$$$": "sh"}, {"$$$": "sleep infinity"}, {"$$$": ["sleep", "infinity"]}),
        (
            {},
            {"$$$": "bash -c 'sleep infinity'"},
            {"$$$": ["bash", "-c", "sleep infinity"]},
        ),
    ])
    def test_parse_compose_file_when_multiple_composes_keys_command_entrypoint(
        self, base_template, override_template, expected_template
    ):
        for key in ['command', 'entrypoint']:
            base, override, expected = template_to_expression(
                base_template, override_template, expected_template, key
            )
            compose_test_1 = {"services": {"test-service": base}}
            compose_test_2 = {"services": {"test-service": override}}
            dump_yaml(compose_test_1, "test-compose-1.yaml")
            dump_yaml(compose_test_2, "test-compose-2.yaml")

            podman_compose = PodmanCompose()
            set_args(podman_compose, ["test-compose-1.yaml", "test-compose-2.yaml"])

            podman_compose._parse_compose_file()  # pylint: disable=protected-access

            actual = {}
            if podman_compose.services:
                podman_compose.services["test-service"].pop("_deps")
                actual = podman_compose.services["test-service"]
            self.assertEqual(actual, expected)


def set_args(podman_compose: PodmanCompose, file_names: list[str]) -> None:
    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 = "1"
    podman_compose.global_args.pod_args = None
    podman_compose.global_args.no_normalize = True


def dump_yaml(compose: dict, name: str) -> None:
    with open(name, "w", encoding="utf-8") as outfile:
        yaml.safe_dump(compose, outfile, default_flow_style=False)


def template_to_expression(base, override, expected, key):
    base_copy = copy.deepcopy(base)
    override_copy = copy.deepcopy(override)
    expected_copy = copy.deepcopy(expected)

    expected_copy[key] = expected_copy.pop("$$$")
    if "$$$" in base:
        base_copy[key] = base_copy.pop("$$$")
    if "$$$" in override:
        override_copy[key] = override_copy.pop("$$$")
    return base_copy, override_copy, expected_copy


def test_clean_test_yamls() -> None:
    test_files = ["test-compose-1.yaml", "test-compose-2.yaml"]
    for file in test_files:
        if os.path.exists(file):
            os.remove(file)