From cac836b0f5da256a0ee9c68552956ecc4dcad93b Mon Sep 17 00:00:00 2001 From: Otto Liljalaakso Date: Fri, 8 Sep 2023 14:06:32 +0300 Subject: [PATCH] Support additional_contexts Signed-off-by: Otto Liljalaakso --- podman_compose.py | 8 ++++ pytests/test_normalize_service.py | 8 ++++ tests/additional_contexts/README.md | 14 ++++++ .../data_for_dict/data.txt | 1 + .../data_for_list/data.txt | 1 + tests/additional_contexts/project/Dockerfile | 3 ++ .../project/docker-compose.yml | 12 +++++ ...test_podman_compose_additional_contexts.py | 44 +++++++++++++++++++ 8 files changed, 91 insertions(+) create mode 100644 tests/additional_contexts/README.md create mode 100644 tests/additional_contexts/data_for_dict/data.txt create mode 100644 tests/additional_contexts/data_for_list/data.txt create mode 100644 tests/additional_contexts/project/Dockerfile create mode 100644 tests/additional_contexts/project/docker-compose.yml create mode 100644 tests/test_podman_compose_additional_contexts.py diff --git a/podman_compose.py b/podman_compose.py index aa4aa0a..c9b7fd6 100755 --- a/podman_compose.py +++ b/podman_compose.py @@ -1464,6 +1464,12 @@ def normalize_service(service, sub_dir=""): if not context: context = "." service["build"]["context"] = context + if "build" in service and "additional_contexts" in service["build"]: + if is_dict(build["additional_contexts"]): + new_additional_contexts = [] + for k, v in build["additional_contexts"].items(): + new_additional_contexts.append(f"{k}={v}") + build["additional_contexts"] = new_additional_contexts for key in ("command", "entrypoint"): if key in service: if is_str(service[key]): @@ -2330,6 +2336,8 @@ async def build_one(compose, args, cnt): build_args.extend(get_secret_args(compose, cnt, secret, podman_is_building=True)) for tag in build_desc.get("tags", []): build_args.extend(["-t", tag]) + for additional_ctx in build_desc.get("additional_contexts", {}): + build_args.extend([f"--build-context={additional_ctx}"]) if "target" in build_desc: build_args.extend(["--target", build_desc["target"]]) container_to_ulimit_build_args(cnt, build_args) diff --git a/pytests/test_normalize_service.py b/pytests/test_normalize_service.py index c9b0e94..925af4e 100644 --- a/pytests/test_normalize_service.py +++ b/pytests/test_normalize_service.py @@ -20,6 +20,14 @@ class TestNormalizeService(unittest.TestCase): {"build": {"context": "./dir-1", "dockerfile": "dockerfile-1"}}, {"build": {"context": "./dir-1", "dockerfile": "dockerfile-1"}}, ), + ( + {"build": {"additional_contexts": ["ctx=../ctx", "ctx2=../ctx2"]}}, + {"build": {"additional_contexts": ["ctx=../ctx", "ctx2=../ctx2"]}}, + ), + ( + {"build": {"additional_contexts": {"ctx": "../ctx", "ctx2": "../ctx2"}}}, + {"build": {"additional_contexts": ["ctx=../ctx", "ctx2=../ctx2"]}}, + ), ]) def test_simple(self, input, expected): self.assertEqual(normalize_service(input), expected) diff --git a/tests/additional_contexts/README.md b/tests/additional_contexts/README.md new file mode 100644 index 0000000..a64510c --- /dev/null +++ b/tests/additional_contexts/README.md @@ -0,0 +1,14 @@ +# Test podman-compose with build.additional_contexts + +``` +podman-compose build +podman-compose up +podman-compose down +``` + +expected output would be + +``` +[dict] | Data for dict +[list] | Data for list +``` diff --git a/tests/additional_contexts/data_for_dict/data.txt b/tests/additional_contexts/data_for_dict/data.txt new file mode 100644 index 0000000..068b08d --- /dev/null +++ b/tests/additional_contexts/data_for_dict/data.txt @@ -0,0 +1 @@ +Data for dict diff --git a/tests/additional_contexts/data_for_list/data.txt b/tests/additional_contexts/data_for_list/data.txt new file mode 100644 index 0000000..a233ee4 --- /dev/null +++ b/tests/additional_contexts/data_for_list/data.txt @@ -0,0 +1 @@ +Data for list diff --git a/tests/additional_contexts/project/Dockerfile b/tests/additional_contexts/project/Dockerfile new file mode 100644 index 0000000..7e75b5d --- /dev/null +++ b/tests/additional_contexts/project/Dockerfile @@ -0,0 +1,3 @@ +FROM busybox +COPY --from=data data.txt /data/data.txt +CMD ["busybox", "cat", "/data/data.txt"] diff --git a/tests/additional_contexts/project/docker-compose.yml b/tests/additional_contexts/project/docker-compose.yml new file mode 100644 index 0000000..1282723 --- /dev/null +++ b/tests/additional_contexts/project/docker-compose.yml @@ -0,0 +1,12 @@ +version: "3.7" +services: + dict: + build: + context: . + additional_contexts: + data: ../data_for_dict + list: + build: + context: . + additional_contexts: + - data=../data_for_list diff --git a/tests/test_podman_compose_additional_contexts.py b/tests/test_podman_compose_additional_contexts.py new file mode 100644 index 0000000..324f53f --- /dev/null +++ b/tests/test_podman_compose_additional_contexts.py @@ -0,0 +1,44 @@ +# SPDX-License-Identifier: GPL-2.0 + + +"""Test how additional contexts are passed to podman.""" + +import os +import subprocess +import unittest + +from .test_podman_compose import podman_compose_path +from .test_podman_compose import test_path + + +def compose_yaml_path(): + """ "Returns the path to the compose file used for this test module""" + return os.path.join(test_path(), "additional_contexts", "project") + + +class TestComposeBuildAdditionalContexts(unittest.TestCase): + def test_build_additional_context(self): + """podman build should receive additional contexts as --build-context + + See additional_context/project/docker-compose.yaml for context paths + """ + cmd = ( + "coverage", + "run", + podman_compose_path(), + "--dry-run", + "--verbose", + "-f", + os.path.join(compose_yaml_path(), "docker-compose.yml"), + "build", + ) + p = subprocess.run( + cmd, + stdout=subprocess.PIPE, + check=False, + stderr=subprocess.STDOUT, + text=True, + ) + self.assertEqual(p.returncode, 0) + self.assertIn("--build-context=data=../data_for_dict", p.stdout) + self.assertIn("--build-context=data=../data_for_list", p.stdout)