Merge pull request #41988 from redmatter/fix-dockerng-network-matching

Fix dockerng.network_* name matching
This commit is contained in:
Nicole Thomas 2017-07-27 15:25:06 -06:00 committed by GitHub
commit 6b45debf28
2 changed files with 71 additions and 4 deletions

View File

@ -2242,8 +2242,20 @@ def network_present(name, driver=None, containers=None):
# map containers to container's Ids.
containers = [__salt__['dockerng.inspect_container'](c)['Id'] for c in containers]
networks = __salt__['dockerng.networks'](names=[name])
log.trace(
'dockerng.network_present: current networks: {0}'.format(networks)
)
# networks will contain all Docker networks which partially match 'name'.
# We need to loop through to find the matching network, if there is one.
network = None
if networks:
network = networks[0] # we expect network's name to be unique
for network_iter in networks:
if network_iter['Name'] == name:
network = network_iter
break
if network is not None:
if all(c in network['Containers'] for c in containers):
ret['result'] = True
ret['comment'] = 'Network \'{0}\' already exists.'.format(name)
@ -2302,7 +2314,20 @@ def network_absent(name, driver=None):
'comment': ''}
networks = __salt__['dockerng.networks'](names=[name])
if not networks:
log.trace(
'dockerng.network_present: current networks: {0}'.format(networks)
)
# networks will contain all Docker networks which partially match 'name'.
# We need to loop through to find the matching network, if there is one.
network = None
if networks:
for network_iter in networks:
if network_iter['Name'] == name:
network = network_iter
break
if network is None:
ret['result'] = True
ret['comment'] = 'Network \'{0}\' already absent'.format(name)
return ret

View File

@ -698,10 +698,18 @@ class DockerngTestCase(TestCase):
dockerng_create_network = Mock(return_value='created')
dockerng_connect_container_to_network = Mock(return_value='connected')
dockerng_inspect_container = Mock(return_value={'Id': 'abcd'})
# Get dockerng.networks to return a network with a name which is a superset of the name of
# the network which is to be created, despite this network existing we should still expect
# that the new network will be created.
# Regression test for #41982.
dockerng_networks = Mock(return_value=[{
'Name': 'network_foobar',
'Containers': {'container': {}}
}])
__salt__ = {'dockerng.create_network': dockerng_create_network,
'dockerng.inspect_container': dockerng_inspect_container,
'dockerng.connect_container_to_network': dockerng_connect_container_to_network,
'dockerng.networks': Mock(return_value=[]),
'dockerng.networks': dockerng_networks,
}
with patch.dict(dockerng_state.__dict__,
{'__salt__': __salt__}):
@ -724,9 +732,13 @@ class DockerngTestCase(TestCase):
'''
dockerng_remove_network = Mock(return_value='removed')
dockerng_disconnect_container_from_network = Mock(return_value='disconnected')
dockerng_networks = Mock(return_value=[{
'Name': 'network_foo',
'Containers': {'container': {}}
}])
__salt__ = {'dockerng.remove_network': dockerng_remove_network,
'dockerng.disconnect_container_from_network': dockerng_disconnect_container_from_network,
'dockerng.networks': Mock(return_value=[{'Containers': {'container': {}}}]),
'dockerng.networks': dockerng_networks,
}
with patch.dict(dockerng_state.__dict__,
{'__salt__': __salt__}):
@ -742,6 +754,36 @@ class DockerngTestCase(TestCase):
'removed': 'removed'},
'result': True})
def test_network_absent_with_matching_network(self):
'''
Test dockerng.network_absent when the specified network does not exist,
but another network with a name which is a superset of the specified
name does exist. In this case we expect there to be no attempt to remove
any network.
Regression test for #41982.
'''
dockerng_remove_network = Mock(return_value='removed')
dockerng_disconnect_container_from_network = Mock(return_value='disconnected')
dockerng_networks = Mock(return_value=[{
'Name': 'network_foobar',
'Containers': {'container': {}}
}])
__salt__ = {'dockerng.remove_network': dockerng_remove_network,
'dockerng.disconnect_container_from_network': dockerng_disconnect_container_from_network,
'dockerng.networks': dockerng_networks,
}
with patch.dict(dockerng_state.__dict__,
{'__salt__': __salt__}):
ret = dockerng_state.network_absent(
'network_foo',
)
dockerng_disconnect_container_from_network.assert_not_called()
dockerng_remove_network.assert_not_called()
self.assertEqual(ret, {'name': 'network_foo',
'comment': 'Network \'network_foo\' already absent',
'changes': {},
'result': True})
def test_volume_present(self):
'''
Test dockerng.volume_present