mirror of
https://github.com/containers/podman-compose.git
synced 2025-06-01 16:06:46 +02:00
commit
342a39dcfe
1
newsfragments/1039-fix-port-command.bugfix
Normal file
1
newsfragments/1039-fix-port-command.bugfix
Normal file
@ -0,0 +1 @@
|
|||||||
|
- Fix the port command for dynamic host ports.
|
@ -15,7 +15,6 @@ import asyncio.subprocess
|
|||||||
import getpass
|
import getpass
|
||||||
import glob
|
import glob
|
||||||
import hashlib
|
import hashlib
|
||||||
import itertools
|
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
@ -3216,30 +3215,13 @@ async def compose_config(compose, args):
|
|||||||
|
|
||||||
@cmd_run(podman_compose, "port", "Prints the public port for a port binding.")
|
@cmd_run(podman_compose, "port", "Prints the public port for a port binding.")
|
||||||
async def compose_port(compose, args):
|
async def compose_port(compose, args):
|
||||||
# TODO - deal with pod index
|
|
||||||
compose.assert_services(args.service)
|
compose.assert_services(args.service)
|
||||||
containers = compose.container_names_by_service[args.service]
|
containers = compose.container_names_by_service[args.service]
|
||||||
container_ports = list(
|
output = await compose.podman.output([], "inspect", [containers[args.index - 1]])
|
||||||
itertools.chain(*(compose.container_by_name[c]["ports"] for c in containers))
|
inspect_json = json.loads(output.decode("utf-8"))
|
||||||
)
|
private_port = str(args.private_port) + "/" + args.protocol
|
||||||
|
host_port = inspect_json[0]["NetworkSettings"]["Ports"][private_port][0]["HostPort"]
|
||||||
def _published_target(port_string):
|
print(host_port)
|
||||||
published, target = port_string.split(":")[-2:]
|
|
||||||
return int(published), int(target)
|
|
||||||
|
|
||||||
select_udp = args.protocol == "udp"
|
|
||||||
published, target = None, None
|
|
||||||
for p in container_ports:
|
|
||||||
is_udp = p[-4:] == "/udp"
|
|
||||||
|
|
||||||
if select_udp and is_udp:
|
|
||||||
published, target = _published_target(p[-4:])
|
|
||||||
if not select_udp and not is_udp:
|
|
||||||
published, target = _published_target(p)
|
|
||||||
|
|
||||||
if target == args.private_port:
|
|
||||||
print(published)
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
@cmd_run(podman_compose, "pause", "Pause all running containers")
|
@cmd_run(podman_compose, "pause", "Pause all running containers")
|
||||||
|
@ -1,35 +1,30 @@
|
|||||||
version: "3"
|
version: "3"
|
||||||
services:
|
services:
|
||||||
web1:
|
web1:
|
||||||
image: nopush/podman-compose-test
|
image: nopush/podman-compose-test
|
||||||
hostname: web1
|
hostname: web1
|
||||||
command: ["dumb-init", "/bin/busybox", "httpd", "-f", "-h", "/var/www/html", "-p", "8001"]
|
command: ["dumb-init", "sleep", "infinity"]
|
||||||
working_dir: /var/www/html
|
ports:
|
||||||
ports:
|
- 8000:8000
|
||||||
- 8001:8001
|
- 8001
|
||||||
volumes:
|
web2:
|
||||||
- ./test1.txt:/var/www/html/index.txt:ro,z
|
image: nopush/podman-compose-test
|
||||||
web2:
|
hostname: web2
|
||||||
image: nopush/podman-compose-test
|
command: ["dumb-init", "sleep", "infinity"]
|
||||||
hostname: web2
|
ports:
|
||||||
command: ["dumb-init", "/bin/busybox", "httpd", "-f", "-h", "/var/www/html", "-p", "8002"]
|
- 8002:8002
|
||||||
working_dir: /var/www/html
|
- target: 8003
|
||||||
ports:
|
host_ip: 127.0.0.1
|
||||||
- 8002:8002
|
published: 8003
|
||||||
- target: 8003
|
protocol: udp
|
||||||
host_ip: 127.0.0.1
|
- target: 8004
|
||||||
published: 8003
|
host_ip: 127.0.0.1
|
||||||
protocol: udp
|
published: 8004
|
||||||
- target: 8004
|
protocol: tcp
|
||||||
host_ip: 127.0.0.1
|
- target: 8005
|
||||||
published: 8004
|
published: 8005
|
||||||
protocol: tcp
|
- target: 8006
|
||||||
- target: 8005
|
protocol: udp
|
||||||
published: 8005
|
- target: 8007
|
||||||
- target: 8006
|
host_ip: 127.0.0.1
|
||||||
protocol: udp
|
|
||||||
- target: 8007
|
|
||||||
host_ip: 127.0.0.1
|
|
||||||
volumes:
|
|
||||||
- ./test2.txt:/var/www/html/index.txt:ro,z
|
|
||||||
|
|
||||||
|
@ -1 +0,0 @@
|
|||||||
test1
|
|
@ -1 +0,0 @@
|
|||||||
test2
|
|
@ -1,12 +1,11 @@
|
|||||||
# SPDX-License-Identifier: GPL-2.0
|
# SPDX-License-Identifier: GPL-2.0
|
||||||
|
|
||||||
"""
|
"""
|
||||||
test_podman_compose_up_down.py
|
test_podman_compose_ports.py
|
||||||
|
|
||||||
Tests the podman compose up and down commands used to create and remove services.
|
Tests the podman compose port command used to show the host port.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# pylint: disable=redefined-outer-name
|
|
||||||
import os
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
@ -35,51 +34,45 @@ class TestPodmanCompose(unittest.TestCase, RunSubprocessMixin):
|
|||||||
"-f",
|
"-f",
|
||||||
os.path.join(test_path(), "ports", "docker-compose.yml"),
|
os.path.join(test_path(), "ports", "docker-compose.yml"),
|
||||||
"down",
|
"down",
|
||||||
"--volumes",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
port_cmd = [
|
||||||
|
podman_compose_path(),
|
||||||
|
"-f",
|
||||||
|
os.path.join(test_path(), "ports", "docker-compose.yml"),
|
||||||
|
"port",
|
||||||
|
]
|
||||||
|
|
||||||
|
udp_arg = ["--protocol", "udp"]
|
||||||
|
|
||||||
|
tcp_arg = ["--protocol", "tcp"]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.run_subprocess_assert_returncode(up_cmd)
|
self.run_subprocess_assert_returncode(up_cmd)
|
||||||
|
|
||||||
|
port = self.run_subprocess_assert_returncode(port_cmd + ["web1", "8000"])
|
||||||
|
self.assertEqual(port[0].decode().strip(), "8000")
|
||||||
|
|
||||||
|
port = self.run_subprocess_assert_returncode(port_cmd + ["web1", "8001"])
|
||||||
|
self.assertNotEqual(port[0].decode().strip(), "8001")
|
||||||
|
|
||||||
|
port = self.run_subprocess_assert_returncode(port_cmd + ["web2", "8002"])
|
||||||
|
self.assertEqual(port[0].decode().strip(), "8002")
|
||||||
|
|
||||||
|
port = self.run_subprocess_assert_returncode(port_cmd + udp_arg + ["web2", "8003"])
|
||||||
|
self.assertEqual(port[0].decode().strip(), "8003")
|
||||||
|
|
||||||
|
port = self.run_subprocess_assert_returncode(port_cmd + ["web2", "8004"])
|
||||||
|
self.assertEqual(port[0].decode().strip(), "8004")
|
||||||
|
|
||||||
|
port = self.run_subprocess_assert_returncode(port_cmd + tcp_arg + ["web2", "8005"])
|
||||||
|
self.assertEqual(port[0].decode().strip(), "8005")
|
||||||
|
|
||||||
|
port = self.run_subprocess_assert_returncode(port_cmd + udp_arg + ["web2", "8006"])
|
||||||
|
self.assertNotEqual(port[0].decode().strip(), "8006")
|
||||||
|
|
||||||
|
port = self.run_subprocess_assert_returncode(port_cmd + ["web2", "8007"])
|
||||||
|
self.assertNotEqual(port[0].decode().strip(), "8007")
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
self.run_subprocess_assert_returncode(down_cmd)
|
self.run_subprocess_assert_returncode(down_cmd)
|
||||||
|
|
||||||
def test_down_with_orphans(self):
|
|
||||||
container_id, _ = self.run_subprocess_assert_returncode([
|
|
||||||
"podman",
|
|
||||||
"run",
|
|
||||||
"--rm",
|
|
||||||
"-d",
|
|
||||||
"nopush/podman-compose-test",
|
|
||||||
"dumb-init",
|
|
||||||
"/bin/busybox",
|
|
||||||
"httpd",
|
|
||||||
"-f",
|
|
||||||
"-h",
|
|
||||||
"/etc/",
|
|
||||||
"-p",
|
|
||||||
"8000",
|
|
||||||
])
|
|
||||||
|
|
||||||
down_cmd = [
|
|
||||||
"coverage",
|
|
||||||
"run",
|
|
||||||
podman_compose_path(),
|
|
||||||
"-f",
|
|
||||||
os.path.join(test_path(), "ports", "docker-compose.yml"),
|
|
||||||
"down",
|
|
||||||
"--volumes",
|
|
||||||
"--remove-orphans",
|
|
||||||
]
|
|
||||||
|
|
||||||
self.run_subprocess_assert_returncode(down_cmd)
|
|
||||||
|
|
||||||
self.run_subprocess_assert_returncode(
|
|
||||||
[
|
|
||||||
"podman",
|
|
||||||
"container",
|
|
||||||
"exists",
|
|
||||||
container_id.decode("utf-8"),
|
|
||||||
],
|
|
||||||
1,
|
|
||||||
)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user