Skip to content

Commit 122a914

Browse files
authored
Merge pull request containers#1062 from tdgroot/down_removes_network
podman-compose down removes networks
2 parents a3fb4b3 + db0aad9 commit 122a914

File tree

6 files changed

+74
-3
lines changed

6 files changed

+74
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
podman-compose down removes networks.

podman_compose.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,24 @@ async def format_out(stdout):
14521452
log.info("exit code: %s", exit_code)
14531453
return exit_code
14541454

1455+
async def network_ls(self):
1456+
output = (
1457+
await self.output(
1458+
[],
1459+
"network",
1460+
[
1461+
"ls",
1462+
"--noheading",
1463+
"--filter",
1464+
f"label=io.podman.compose.project={self.compose.project_name}",
1465+
"--format",
1466+
"{{.Name}}",
1467+
],
1468+
)
1469+
).decode()
1470+
networks = output.splitlines()
1471+
return networks
1472+
14551473
async def volume_ls(self):
14561474
output = (
14571475
await self.output(
@@ -2612,7 +2630,7 @@ def get_volume_names(compose, cnt):
26122630

26132631

26142632
@cmd_run(podman_compose, "down", "tear down entire stack")
2615-
async def compose_down(compose, args):
2633+
async def compose_down(compose: PodmanCompose, args):
26162634
excluded = get_excluded(compose, args)
26172635
podman_args = []
26182636
timeout_global = getattr(args, "timeout", None)
@@ -2678,6 +2696,8 @@ async def compose_down(compose, args):
26782696
return
26792697
for pod in compose.pods:
26802698
await compose.podman.run([], "pod", ["rm", pod["name"]])
2699+
for network in await compose.podman.network_ls():
2700+
await compose.podman.run([], "network", ["rm", network])
26812701

26822702

26832703
@cmd_run(podman_compose, "ps", "show status of containers")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
version: "3"
2+
networks:
3+
mystack:
4+
services:
5+
web1:
6+
image: busybox
7+
hostname: web1
8+
command: ["/bin/busybox", "httpd", "-f", "-h", "/var/www/html", "-p", "8001"]
9+
working_dir: /var/www/html
10+
ports:
11+
- 8001:8001
12+
volumes:
13+
- ./test1.txt:/var/www/html/index.txt:ro,z
14+
web2:
15+
image: busybox
16+
hostname: web2
17+
command: ["/bin/busybox", "httpd", "-f", "-h", "/var/www/html", "-p", "8001"]
18+
working_dir: /var/www/html
19+
ports:
20+
- 8002:8001
21+
volumes:
22+
- ./test2.txt:/var/www/html/index.txt:ro,z
23+

tests/integration/network/test1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test1

tests/integration/network/test2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test2

tests/integration/test_podman_compose_tests.py

+27-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def test_run(self):
4848
]
4949

5050
out, _ = self.run_subprocess_assert_returncode(run_cmd)
51-
self.assertIn(b'127.0.0.1\tlocalhost', out)
51+
self.assertIn(b"127.0.0.1\tlocalhost", out)
5252

5353
# Run it again to make sure we can run it twice. I saw an issue where a second run, with
5454
# the container left up, would fail
@@ -67,7 +67,7 @@ def test_run(self):
6767
]
6868

6969
out, _ = self.run_subprocess_assert_returncode(run_cmd)
70-
self.assertIn(b'127.0.0.1\tlocalhost', out)
70+
self.assertIn(b"127.0.0.1\tlocalhost", out)
7171

7272
# This leaves a container running. Not sure it's intended, but it matches docker-compose
7373
down_cmd = [
@@ -187,3 +187,28 @@ def test_down_with_orphans(self):
187187
],
188188
1,
189189
)
190+
191+
def test_down_with_network(self):
192+
try:
193+
self.run_subprocess_assert_returncode([
194+
"coverage",
195+
"run",
196+
podman_compose_path(),
197+
"-f",
198+
os.path.join(test_path(), "network", "docker-compose.yml"),
199+
"up",
200+
"-d",
201+
])
202+
output, _, _ = self.run_subprocess(["podman", "network", "ls"])
203+
self.assertIn("network_mystack", output.decode())
204+
finally:
205+
self.run_subprocess_assert_returncode([
206+
"coverage",
207+
"run",
208+
podman_compose_path(),
209+
"-f",
210+
os.path.join(test_path(), "network", "docker-compose.yml"),
211+
"down",
212+
])
213+
output, _, _ = self.run_subprocess(["podman", "network", "ls"])
214+
self.assertNotIn("network_mystack", output.decode())

0 commit comments

Comments
 (0)