Skip to content

Commit

Permalink
podman-compose down removes networks
Browse files Browse the repository at this point in the history
Fixes #490
  • Loading branch information
tdgroot committed Oct 17, 2024
1 parent a3fb4b3 commit ad6251c
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
1 change: 1 addition & 0 deletions newsfragments/down-removes-network.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
podman-compose down removes networks.
22 changes: 21 additions & 1 deletion podman_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,24 @@ async def format_out(stdout):
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("utf-8")
networks = output.splitlines()
return networks

async def volume_ls(self):
output = (
await self.output(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
23 changes: 23 additions & 0 deletions tests/integration/network/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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

1 change: 1 addition & 0 deletions tests/integration/network/test1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test1
1 change: 1 addition & 0 deletions tests/integration/network/test2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test2
30 changes: 30 additions & 0 deletions tests/integration/test_podman_compose_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,33 @@ def test_down_with_orphans(self):
],
1,
)

def test_down_with_network(self):
up_cmd = [
"coverage",
"run",
podman_compose_path(),
"-f",
os.path.join(test_path(), "network", "docker-compose.yml"),
"up",
"-d",
]

down_cmd = [
"coverage",
"run",
podman_compose_path(),
"-f",
os.path.join(test_path(), "network", "docker-compose.yml"),
"down",
]

try:
self.run_subprocess_assert_returncode(up_cmd)
output, _, _ = self.run_subprocess(["podman", "network", "ls"])
self.assertIn("network_mystack", output.decode())
finally:
out, _, return_code = self.run_subprocess(down_cmd)
self.assertEqual(return_code, 0)
output, _, _ = self.run_subprocess(["podman", "network", "ls"])
self.assertNotIn("network_mystack", output.decode())

0 comments on commit ad6251c

Please sign in to comment.