Skip to content

Commit 342a39d

Browse files
authored
Merge pull request #1179 from knarfS/fix_port_cmd
Fix port command
2 parents d6b8476 + ae41ef0 commit 342a39d

File tree

6 files changed

+69
-100
lines changed

6 files changed

+69
-100
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fix the port command for dynamic host ports.

podman_compose.py

+5-23
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import getpass
1616
import glob
1717
import hashlib
18-
import itertools
1918
import json
2019
import logging
2120
import os
@@ -3216,30 +3215,13 @@ async def compose_config(compose, args):
32163215

32173216
@cmd_run(podman_compose, "port", "Prints the public port for a port binding.")
32183217
async def compose_port(compose, args):
3219-
# TODO - deal with pod index
32203218
compose.assert_services(args.service)
32213219
containers = compose.container_names_by_service[args.service]
3222-
container_ports = list(
3223-
itertools.chain(*(compose.container_by_name[c]["ports"] for c in containers))
3224-
)
3225-
3226-
def _published_target(port_string):
3227-
published, target = port_string.split(":")[-2:]
3228-
return int(published), int(target)
3229-
3230-
select_udp = args.protocol == "udp"
3231-
published, target = None, None
3232-
for p in container_ports:
3233-
is_udp = p[-4:] == "/udp"
3234-
3235-
if select_udp and is_udp:
3236-
published, target = _published_target(p[-4:])
3237-
if not select_udp and not is_udp:
3238-
published, target = _published_target(p)
3239-
3240-
if target == args.private_port:
3241-
print(published)
3242-
return
3220+
output = await compose.podman.output([], "inspect", [containers[args.index - 1]])
3221+
inspect_json = json.loads(output.decode("utf-8"))
3222+
private_port = str(args.private_port) + "/" + args.protocol
3223+
host_port = inspect_json[0]["NetworkSettings"]["Ports"][private_port][0]["HostPort"]
3224+
print(host_port)
32433225

32443226

32453227
@cmd_run(podman_compose, "pause", "Pause all running containers")
+27-32
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
11
version: "3"
22
services:
3-
web1:
4-
image: nopush/podman-compose-test
5-
hostname: web1
6-
command: ["dumb-init", "/bin/busybox", "httpd", "-f", "-h", "/var/www/html", "-p", "8001"]
7-
working_dir: /var/www/html
8-
ports:
9-
- 8001:8001
10-
volumes:
11-
- ./test1.txt:/var/www/html/index.txt:ro,z
12-
web2:
13-
image: nopush/podman-compose-test
14-
hostname: web2
15-
command: ["dumb-init", "/bin/busybox", "httpd", "-f", "-h", "/var/www/html", "-p", "8002"]
16-
working_dir: /var/www/html
17-
ports:
18-
- 8002:8002
19-
- target: 8003
20-
host_ip: 127.0.0.1
21-
published: 8003
22-
protocol: udp
23-
- target: 8004
24-
host_ip: 127.0.0.1
25-
published: 8004
26-
protocol: tcp
27-
- target: 8005
28-
published: 8005
29-
- target: 8006
30-
protocol: udp
31-
- target: 8007
32-
host_ip: 127.0.0.1
33-
volumes:
34-
- ./test2.txt:/var/www/html/index.txt:ro,z
3+
web1:
4+
image: nopush/podman-compose-test
5+
hostname: web1
6+
command: ["dumb-init", "sleep", "infinity"]
7+
ports:
8+
- 8000:8000
9+
- 8001
10+
web2:
11+
image: nopush/podman-compose-test
12+
hostname: web2
13+
command: ["dumb-init", "sleep", "infinity"]
14+
ports:
15+
- 8002:8002
16+
- target: 8003
17+
host_ip: 127.0.0.1
18+
published: 8003
19+
protocol: udp
20+
- target: 8004
21+
host_ip: 127.0.0.1
22+
published: 8004
23+
protocol: tcp
24+
- target: 8005
25+
published: 8005
26+
- target: 8006
27+
protocol: udp
28+
- target: 8007
29+
host_ip: 127.0.0.1
3530

tests/integration/ports/test1.txt

-1
This file was deleted.

tests/integration/ports/test2.txt

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# SPDX-License-Identifier: GPL-2.0
22

33
"""
4-
test_podman_compose_up_down.py
4+
test_podman_compose_ports.py
55
6-
Tests the podman compose up and down commands used to create and remove services.
6+
Tests the podman compose port command used to show the host port.
77
"""
88

9-
# pylint: disable=redefined-outer-name
109
import os
1110
import unittest
1211

@@ -35,51 +34,45 @@ def test_up_with_ports(self):
3534
"-f",
3635
os.path.join(test_path(), "ports", "docker-compose.yml"),
3736
"down",
38-
"--volumes",
3937
]
4038

39+
port_cmd = [
40+
podman_compose_path(),
41+
"-f",
42+
os.path.join(test_path(), "ports", "docker-compose.yml"),
43+
"port",
44+
]
45+
46+
udp_arg = ["--protocol", "udp"]
47+
48+
tcp_arg = ["--protocol", "tcp"]
49+
4150
try:
4251
self.run_subprocess_assert_returncode(up_cmd)
4352

44-
finally:
45-
self.run_subprocess_assert_returncode(down_cmd)
53+
port = self.run_subprocess_assert_returncode(port_cmd + ["web1", "8000"])
54+
self.assertEqual(port[0].decode().strip(), "8000")
4655

47-
def test_down_with_orphans(self):
48-
container_id, _ = self.run_subprocess_assert_returncode([
49-
"podman",
50-
"run",
51-
"--rm",
52-
"-d",
53-
"nopush/podman-compose-test",
54-
"dumb-init",
55-
"/bin/busybox",
56-
"httpd",
57-
"-f",
58-
"-h",
59-
"/etc/",
60-
"-p",
61-
"8000",
62-
])
56+
port = self.run_subprocess_assert_returncode(port_cmd + ["web1", "8001"])
57+
self.assertNotEqual(port[0].decode().strip(), "8001")
6358

64-
down_cmd = [
65-
"coverage",
66-
"run",
67-
podman_compose_path(),
68-
"-f",
69-
os.path.join(test_path(), "ports", "docker-compose.yml"),
70-
"down",
71-
"--volumes",
72-
"--remove-orphans",
73-
]
59+
port = self.run_subprocess_assert_returncode(port_cmd + ["web2", "8002"])
60+
self.assertEqual(port[0].decode().strip(), "8002")
61+
62+
port = self.run_subprocess_assert_returncode(port_cmd + udp_arg + ["web2", "8003"])
63+
self.assertEqual(port[0].decode().strip(), "8003")
7464

75-
self.run_subprocess_assert_returncode(down_cmd)
76-
77-
self.run_subprocess_assert_returncode(
78-
[
79-
"podman",
80-
"container",
81-
"exists",
82-
container_id.decode("utf-8"),
83-
],
84-
1,
85-
)
65+
port = self.run_subprocess_assert_returncode(port_cmd + ["web2", "8004"])
66+
self.assertEqual(port[0].decode().strip(), "8004")
67+
68+
port = self.run_subprocess_assert_returncode(port_cmd + tcp_arg + ["web2", "8005"])
69+
self.assertEqual(port[0].decode().strip(), "8005")
70+
71+
port = self.run_subprocess_assert_returncode(port_cmd + udp_arg + ["web2", "8006"])
72+
self.assertNotEqual(port[0].decode().strip(), "8006")
73+
74+
port = self.run_subprocess_assert_returncode(port_cmd + ["web2", "8007"])
75+
self.assertNotEqual(port[0].decode().strip(), "8007")
76+
77+
finally:
78+
self.run_subprocess_assert_returncode(down_cmd)

0 commit comments

Comments
 (0)