From 5a3bdbf89bd700f950beb8d86e563cc6870e8fbe Mon Sep 17 00:00:00 2001 From: BugFest Date: Wed, 12 Apr 2023 19:22:14 +0200 Subject: [PATCH] Exit code managed at PodmanCompose.run() Signed-off-by: BugFest --- podman_compose.py | 12 ++++++++---- tests/build_fail/README.md | 22 ++++++++++++++++++++++ tests/build_fail/context/Dockerfile | 3 +++ tests/build_fail/context/Dockerfile-alt | 9 +++++++++ tests/build_fail/docker-compose.yml | 25 +++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 tests/build_fail/README.md create mode 100644 tests/build_fail/context/Dockerfile create mode 100644 tests/build_fail/context/Dockerfile-alt create mode 100644 tests/build_fail/docker-compose.yml diff --git a/podman_compose.py b/podman_compose.py index bafe828..383359f 100755 --- a/podman_compose.py +++ b/podman_compose.py @@ -1487,7 +1487,9 @@ class PodmanCompose: if compose_required: self._parse_compose_file() cmd = self.commands[cmd_name] - cmd(self, args) + retcode = cmd(self, args) + if isinstance(retcode, int): + sys.exit(retcode) def _parse_compose_file(self): args = self.global_args @@ -2109,14 +2111,16 @@ def compose_build(compose, args): cnt = compose.container_by_name[container_names_by_service[service][0]] p = build_one(compose, args, cnt) status = parse_return_code(p, status) + if status != 0: + return status else: for cnt in compose.containers: p = build_one(compose, args, cnt) status = parse_return_code(p, status) + if status != 0: + return status - # When calling the "build" command, exit with the last non-Ok exit code found - if args.command == "build" or status != 0: - sys.exit(status) + return status def create_pods(compose, args): # pylint: disable=unused-argument diff --git a/tests/build_fail/README.md b/tests/build_fail/README.md new file mode 100644 index 0000000..5d5b1ed --- /dev/null +++ b/tests/build_fail/README.md @@ -0,0 +1,22 @@ +# Test podman-compose with build (fail scenario) + +```shell +podman-compose build || echo $? +``` + +expected output would be something like + +``` +STEP 1/3: FROM busybox +STEP 2/3: RUN this_command_does_not_exist +/bin/sh: this_command_does_not_exist: not found +Error: building at STEP "RUN this_command_does_not_exist": while running runtime: exit status 127 + +exit code: 127 +``` + +Expected `podman-compose` exit code: +```shell +echo $? +127 +``` diff --git a/tests/build_fail/context/Dockerfile b/tests/build_fail/context/Dockerfile new file mode 100644 index 0000000..6bab556 --- /dev/null +++ b/tests/build_fail/context/Dockerfile @@ -0,0 +1,3 @@ +FROM busybox +RUN this_command_does_not_exist +CMD ["sh"] diff --git a/tests/build_fail/context/Dockerfile-alt b/tests/build_fail/context/Dockerfile-alt new file mode 100644 index 0000000..75b5852 --- /dev/null +++ b/tests/build_fail/context/Dockerfile-alt @@ -0,0 +1,9 @@ +FROM busybox +ARG buildno=1 +ARG httpd_port=80 +ARG other_variable=not_set +ENV httpd_port ${httpd_port} +ENV other_variable ${other_variable} +RUN mkdir -p /var/www/html/ && \ + echo "ALT buildno=$buildno port=$httpd_port `date -Iseconds`" > /var/www/html/index.txt +CMD httpd -f -p "$httpd_port" -h /var/www/html diff --git a/tests/build_fail/docker-compose.yml b/tests/build_fail/docker-compose.yml new file mode 100644 index 0000000..a1116ad --- /dev/null +++ b/tests/build_fail/docker-compose.yml @@ -0,0 +1,25 @@ +version: "3" +services: + web1: + build: ./context + image: my-busybox-httpd + ports: + - 8080:80 + web2: + build: + context: ./context + dockerfile: Dockerfile-alt + labels: + mykey: myval + args: + buildno: 2 + httpd_port: 8000 + image: my-busybox-httpd2 + ports: + - 8000:8000 + test_build_arg_argument: + build: + context: ./context + dockerfile: Dockerfile-alt + image: my-busybox-httpd2 + command: env