From efccf15a55057efbe88c574531df7c315ca97d3e Mon Sep 17 00:00:00 2001 From: Robin Syl Date: Mon, 11 Mar 2024 23:26:10 +0100 Subject: [PATCH] Unit test for healthcheck block Signed-off-by: Robin Syl --- tests/healthcheck/docker-compose.yml | 11 +++++++ tests/test_podman_compose_up_down.py | 45 ++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 tests/healthcheck/docker-compose.yml diff --git a/tests/healthcheck/docker-compose.yml b/tests/healthcheck/docker-compose.yml new file mode 100644 index 0000000..5c12b3c --- /dev/null +++ b/tests/healthcheck/docker-compose.yml @@ -0,0 +1,11 @@ +version: "3" +services: + healthcheck: + image: nopush/podman-compose-test + healthcheck: + test: [ "CMD-SHELL", "curl -f http://localhost || exit 1" ] + interval: 1m + timeout: 10s + retries: 3 + start_period: 10s + start_interval: 5s diff --git a/tests/test_podman_compose_up_down.py b/tests/test_podman_compose_up_down.py index 93b92fd..d91cb3a 100644 --- a/tests/test_podman_compose_up_down.py +++ b/tests/test_podman_compose_up_down.py @@ -7,6 +7,7 @@ """ # pylint: disable=redefined-outer-name +import json import os import unittest @@ -89,3 +90,47 @@ def test_up(self, profiles, expected_services): actual_services[service] = service in actual_output self.assertEqual(expected_services, actual_services) + + def test_healthcheck(self): + up_cmd = [ + "coverage", + "run", + podman_compose_path(), + "-f", + os.path.join(test_path(), "healthcheck", "docker-compose.yml"), + "up", + "-d", + ] + self.run_subprocess_assert_returncode(up_cmd) + + command_container_id = [ + "podman", + "ps", + "-a", + "--filter", + "label=io.podman.compose.project=healthcheck", + "--format", + '"{{.ID}}"', + ] + out, _ = self.run_subprocess_assert_returncode(command_container_id) + self.assertNotEqual(out, b"") + container_id = out.decode("utf-8").strip().replace('"', "") + + command_inspect = ["podman", "container", "inspect", container_id] + + out, _ = self.run_subprocess_assert_returncode(command_inspect) + out_string = out.decode("utf-8") + inspect = json.loads(out_string) + healthcheck_obj = inspect[0]["Config"]["Healthcheck"] + expected = { + "Test": ["CMD-SHELL", "/bin/sh -c 'curl -f http://localhost || exit 1'"], + "StartPeriod": 10000000000, + "Interval": 60000000000, + "Timeout": 10000000000, + "Retries": 3, + } + self.assertEqual(healthcheck_obj, expected) + + # StartInterval is not available in the config object + create_obj = inspect[0]["Config"]["CreateCommand"] + self.assertIn("--health-startup-interval", create_obj)