Merge pull request #32015 from ticosax/fix-port-comparison-udp

[dockerng] Fix ports exposition when protocol is passed.
This commit is contained in:
Nicole Thomas 2016-03-21 09:22:19 -06:00
commit d117db3efb
2 changed files with 85 additions and 2 deletions

View file

@ -87,6 +87,30 @@ def _format_comments(comments):
return ret
def _map_port_from_yaml_to_docker(port):
'''
docker-py interface is not very nice:
While for ``port_bindings`` they support:
.. code-block:: python
'8888/tcp'
For ``ports``, it has to be transformed into:
.. code-block:: python
(8888, 'tcp')
'''
if isinstance(port, six.string_types):
port, sep, protocol = port.partition('/')
if protocol:
return int(port), protocol
return int(port)
return port
def _prep_input(kwargs):
'''
Repack (if necessary) data that should be in a dict but is easier to
@ -197,7 +221,7 @@ def _compare(actual, create_kwargs, defaults_from_image):
for port_def in data:
if isinstance(port_def, six.integer_types):
port_def = str(port_def)
if isinstance(port_def, tuple):
if isinstance(port_def, (tuple, list)):
desired_ports.append('{0}/{1}'.format(*port_def))
elif '/' not in port_def:
desired_ports.append('{0}/tcp'.format(port_def))
@ -1534,7 +1558,8 @@ def running(name,
if create_kwargs.get('port_bindings') is not None:
# Be smart and try to provide `ports` argument derived from
# the "port_bindings" configuration.
auto_ports = list(create_kwargs['port_bindings'])
auto_ports = [_map_port_from_yaml_to_docker(port)
for port in create_kwargs['port_bindings']]
actual_ports = create_kwargs.setdefault('ports', [])
actual_ports.extend([p for p in auto_ports if
p not in actual_ports])

View file

@ -200,6 +200,64 @@ class DockerngTestCase(TestCase):
client_timeout=60)
dockerng_start.assert_called_with('cont')
def test_running_with_udp_bindings(self):
'''
Check that `ports` contains ports defined from `port_bindings` with
protocol declaration passed as tuple. As stated by docker-py
documentation
https://docker-py.readthedocs.org/en/latest/port-bindings/
In sls:
.. code-block:: yaml
container:
dockerng.running:
- port_bindings:
- '9090:9797/udp'
is equivalent of:
.. code-block:: yaml
container:
dockerng.running:
- ports:
- 9797/udp
- port_bindings:
- '9090:9797/udp'
'''
dockerng_create = Mock()
dockerng_start = Mock()
dockerng_inspect_image = Mock(return_value={
'Id': 'abcd',
'Config': {'ExposedPorts': {}}
})
__salt__ = {'dockerng.list_containers': MagicMock(),
'dockerng.list_tags': MagicMock(),
'dockerng.pull': MagicMock(),
'dockerng.state': MagicMock(),
'dockerng.inspect_image': dockerng_inspect_image,
'dockerng.create': dockerng_create,
'dockerng.start': dockerng_start,
}
with patch.dict(dockerng_state.__dict__,
{'__salt__': __salt__}):
dockerng_state.running(
'cont',
image='image:latest',
port_bindings=['9090:9797/udp'])
dockerng_create.assert_called_with(
'image:latest',
validate_input=False,
name='cont',
ports=[(9797, 'udp')],
port_bindings={'9797/udp': [9090]},
validate_ip_addrs=False,
client_timeout=60)
dockerng_start.assert_called_with('cont')
def test_running_compare_images_by_id(self):
'''
Make sure the container is running