Skip to content

Commit

Permalink
Merge pull request #447 from krrhodes/patch-1
Browse files Browse the repository at this point in the history
Accept integer ports in containers_create.create
  • Loading branch information
openshift-merge-bot[bot] authored Nov 5, 2024
2 parents 7a67491 + 85b3220 commit 8cfc8c2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
5 changes: 4 additions & 1 deletion podman/domain/containers_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def create(
pids_limit (int): Tune a container's pids limit. Set -1 for unlimited.
platform (str): Platform in the format os[/arch[/variant]]. Only used if the method
needs to pull the requested image.
ports (Dict[str, Union[int, Tuple[str, int], List[int], Dict[str, Union[int, Tuple[str, int], List[int]]]]]): Ports to bind inside the container.
ports (Dict[Union[int, str], Union[int, Tuple[str, int], List[int], Dict[str, Union[int, Tuple[str, int], List[int]]]]]): Ports to bind inside the container.
The keys of the dictionary are the ports to bind inside the container, either as an
integer or a string in the form port/protocol, where the protocol is either
Expand Down Expand Up @@ -622,6 +622,9 @@ def parse_host_port(_container_port, _protocol, _host):
return result

for container, host in args.pop("ports", {}).items():
if isinstance(container, int):
container = str(container)

if "/" in container:
container_port, protocol = container.split("/")
else:
Expand Down
10 changes: 10 additions & 0 deletions podman/tests/integration/test_container_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ def test_container_ports(self):
'1223/tcp': [{'HostIp': '', 'HostPort': '1235'}],
},
},
{
'input': {
2244: 3344,
},
'expected_output': {
'2244/tcp': [
{'HostIp': '', 'HostPort': '3344'},
],
},
},
]

for port_test in port_tests:
Expand Down
8 changes: 7 additions & 1 deletion podman/tests/integration/test_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def test_container_crud(self):

with self.subTest("Create from Alpine Image"):
container = self.client.containers.create(
self.alpine_image, command=["echo", random_string], ports={'2222/tcp': 3333}
self.alpine_image,
command=["echo", random_string],
ports={'2222/tcp': 3333, 2244: 3344},
)
self.assertIsInstance(container, Container)
self.assertGreater(len(container.attrs), 0)
Expand All @@ -63,6 +65,10 @@ def test_container_crud(self):
self.assertEqual(
"3333", container.attrs["NetworkSettings"]["Ports"]["2222/tcp"][0]["HostPort"]
)
self.assertIn("2244/tcp", container.attrs["NetworkSettings"]["Ports"])
self.assertEqual(
"3344", container.attrs["NetworkSettings"]["Ports"]["2244/tcp"][0]["HostPort"]
)

file_contents = b"This is an integration test for archive."
file_buffer = io.BytesIO(file_contents)
Expand Down
46 changes: 45 additions & 1 deletion podman/tests/unit/test_containersmanager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import unittest

try:
Expand All @@ -7,7 +8,7 @@
# Python < 3.10
from collections import Iterator

from unittest.mock import DEFAULT, patch
from unittest.mock import ANY, DEFAULT, patch, MagicMock

import requests_mock

Expand Down Expand Up @@ -213,6 +214,49 @@ def test_create_404(self, mock):
with self.assertRaises(ImageNotFound):
self.client.containers.create("fedora", "/usr/bin/ls", cpu_count=9999)

@requests_mock.Mocker()
def test_create_parse_host_port(self, mock):
mock_response = MagicMock()
mock_response.json = lambda: {
"Id": "87e1325c82424e49a00abdd4de08009eb76c7de8d228426a9b8af9318ced5ecd",
"Size": 1024,
}
self.client.containers.client.post = MagicMock(return_value=mock_response)
mock.get(
tests.LIBPOD_URL
+ "/containers/87e1325c82424e49a00abdd4de08009eb76c7de8d228426a9b8af9318ced5ecd/json",
json=FIRST_CONTAINER,
)

port_str = {'2233': 3333}
port_str_protocol = {'2244/tcp': 3344}
port_int = {2255: 3355}
ports = {**port_str, **port_str_protocol, **port_int}
self.client.containers.create("fedora", "/usr/bin/ls", ports=ports)

self.client.containers.client.post.assert_called()
expected_ports = [
{
'container_port': 2233,
'host_port': 3333,
'protocol': 'tcp',
},
{
'container_port': 2244,
'host_port': 3344,
'protocol': 'tcp',
},
{
'container_port': 2255,
'host_port': 3355,
'protocol': 'tcp',
},
]
actual_ports = json.loads(self.client.containers.client.post.call_args[1]['data'])[
'portmappings'
]
self.assertEqual(expected_ports, actual_ports)

def test_create_unsupported_key(self):
with self.assertRaises(TypeError) as e:
self.client.containers.create("fedora", "/usr/bin/ls", blkio_weight=100.0)
Expand Down

0 comments on commit 8cfc8c2

Please sign in to comment.