From db0aad97bdd8ad820b5e1b2ddee5f71847e30fec Mon Sep 17 00:00:00 2001 From: Timon de Groot Date: Thu, 17 Oct 2024 16:40:18 +0200 Subject: [PATCH] podman-compose down removes networks Fixes #490 Signed-off-by: Timon de Groot --- newsfragments/down-removes-network.feature | 1 + podman_compose.py | 22 +++++++++++++- tests/integration/network/docker-compose.yml | 23 +++++++++++++++ tests/integration/network/test1.txt | 1 + tests/integration/network/test2.txt | 1 + .../integration/test_podman_compose_tests.py | 29 +++++++++++++++++-- 6 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 newsfragments/down-removes-network.feature create mode 100644 tests/integration/network/docker-compose.yml create mode 100644 tests/integration/network/test1.txt create mode 100644 tests/integration/network/test2.txt diff --git a/newsfragments/down-removes-network.feature b/newsfragments/down-removes-network.feature new file mode 100644 index 0000000..17ac9f0 --- /dev/null +++ b/newsfragments/down-removes-network.feature @@ -0,0 +1 @@ +podman-compose down removes networks. diff --git a/podman_compose.py b/podman_compose.py index e768a22..db6dc58 100755 --- a/podman_compose.py +++ b/podman_compose.py @@ -1452,6 +1452,24 @@ class Podman: log.info("exit code: %s", exit_code) return exit_code + async def network_ls(self): + output = ( + await self.output( + [], + "network", + [ + "ls", + "--noheading", + "--filter", + f"label=io.podman.compose.project={self.compose.project_name}", + "--format", + "{{.Name}}", + ], + ) + ).decode() + networks = output.splitlines() + return networks + async def volume_ls(self): output = ( await self.output( @@ -2612,7 +2630,7 @@ def get_volume_names(compose, cnt): @cmd_run(podman_compose, "down", "tear down entire stack") -async def compose_down(compose, args): +async def compose_down(compose: PodmanCompose, args): excluded = get_excluded(compose, args) podman_args = [] timeout_global = getattr(args, "timeout", None) @@ -2678,6 +2696,8 @@ async def compose_down(compose, args): return for pod in compose.pods: await compose.podman.run([], "pod", ["rm", pod["name"]]) + for network in await compose.podman.network_ls(): + await compose.podman.run([], "network", ["rm", network]) @cmd_run(podman_compose, "ps", "show status of containers") diff --git a/tests/integration/network/docker-compose.yml b/tests/integration/network/docker-compose.yml new file mode 100644 index 0000000..aa24411 --- /dev/null +++ b/tests/integration/network/docker-compose.yml @@ -0,0 +1,23 @@ +version: "3" +networks: + mystack: +services: + web1: + image: busybox + hostname: web1 + command: ["/bin/busybox", "httpd", "-f", "-h", "/var/www/html", "-p", "8001"] + working_dir: /var/www/html + ports: + - 8001:8001 + volumes: + - ./test1.txt:/var/www/html/index.txt:ro,z + web2: + image: busybox + hostname: web2 + command: ["/bin/busybox", "httpd", "-f", "-h", "/var/www/html", "-p", "8001"] + working_dir: /var/www/html + ports: + - 8002:8001 + volumes: + - ./test2.txt:/var/www/html/index.txt:ro,z + diff --git a/tests/integration/network/test1.txt b/tests/integration/network/test1.txt new file mode 100644 index 0000000..a5bce3f --- /dev/null +++ b/tests/integration/network/test1.txt @@ -0,0 +1 @@ +test1 diff --git a/tests/integration/network/test2.txt b/tests/integration/network/test2.txt new file mode 100644 index 0000000..180cf83 --- /dev/null +++ b/tests/integration/network/test2.txt @@ -0,0 +1 @@ +test2 diff --git a/tests/integration/test_podman_compose_tests.py b/tests/integration/test_podman_compose_tests.py index 67df2cd..f5ac152 100644 --- a/tests/integration/test_podman_compose_tests.py +++ b/tests/integration/test_podman_compose_tests.py @@ -48,7 +48,7 @@ class TestPodmanCompose(unittest.TestCase, RunSubprocessMixin): ] out, _ = self.run_subprocess_assert_returncode(run_cmd) - self.assertIn(b'127.0.0.1\tlocalhost', out) + self.assertIn(b"127.0.0.1\tlocalhost", out) # Run it again to make sure we can run it twice. I saw an issue where a second run, with # the container left up, would fail @@ -67,7 +67,7 @@ class TestPodmanCompose(unittest.TestCase, RunSubprocessMixin): ] out, _ = self.run_subprocess_assert_returncode(run_cmd) - self.assertIn(b'127.0.0.1\tlocalhost', out) + self.assertIn(b"127.0.0.1\tlocalhost", out) # This leaves a container running. Not sure it's intended, but it matches docker-compose down_cmd = [ @@ -187,3 +187,28 @@ class TestPodmanCompose(unittest.TestCase, RunSubprocessMixin): ], 1, ) + + def test_down_with_network(self): + try: + self.run_subprocess_assert_returncode([ + "coverage", + "run", + podman_compose_path(), + "-f", + os.path.join(test_path(), "network", "docker-compose.yml"), + "up", + "-d", + ]) + output, _, _ = self.run_subprocess(["podman", "network", "ls"]) + self.assertIn("network_mystack", output.decode()) + finally: + self.run_subprocess_assert_returncode([ + "coverage", + "run", + podman_compose_path(), + "-f", + os.path.join(test_path(), "network", "docker-compose.yml"), + "down", + ]) + output, _, _ = self.run_subprocess(["podman", "network", "ls"]) + self.assertNotIn("network_mystack", output.decode())