Fix 'run' behaviour with networks

- Test that one-off containers join all networks
- Don't set any aliases

Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
This commit is contained in:
Aanand Prasad 2016-01-21 16:49:50 +00:00
parent 1e51b7e88b
commit a14906fd35
2 changed files with 54 additions and 12 deletions

View File

@ -430,10 +430,12 @@ class Service(object):
return container
def connect_container_to_networks(self, container):
one_off = (container.labels.get(LABEL_ONE_OFF) == "True")
for network in self.networks:
self.client.connect_container_to_network(
container.id, network,
aliases=[self.name],
aliases=self._get_aliases(one_off=one_off),
links=self._get_links(False),
)
@ -505,6 +507,12 @@ class Service(object):
numbers = [c.number for c in containers]
return 1 if not numbers else max(numbers) + 1
def _get_aliases(self, one_off):
if one_off:
return []
return [self.name]
def _get_links(self, link_to_self):
links = {}
@ -610,7 +618,8 @@ class Service(object):
override_options,
one_off=one_off)
container_options['networking_config'] = self._get_container_networking_config()
container_options['networking_config'] = self._get_container_networking_config(
one_off=one_off)
return container_options
@ -646,10 +655,10 @@ class Service(object):
cpu_quota=options.get('cpu_quota'),
)
def _get_container_networking_config(self):
def _get_container_networking_config(self, one_off=False):
return self.client.create_networking_config({
network_name: self.client.create_endpoint_config(
aliases=[self.name],
aliases=self._get_aliases(one_off=one_off),
links=self._get_links(False),
)
for network_name in self.networks

View File

@ -903,14 +903,47 @@ class CLITestCase(DockerClientTestCase):
self.assertEqual(container.name, name)
@v2_only()
def test_run_with_networking(self):
self.base_dir = 'tests/fixtures/v2-simple'
self.dispatch(['run', 'simple', 'true'], None)
service = self.project.get_service('simple')
container, = service.containers(stopped=True, one_off=True)
networks = self.client.networks(names=[self.project.default_network.full_name])
self.assertEqual(len(networks), 1)
self.assertEqual(container.human_readable_command, u'true')
def test_run_interactive_connects_to_network(self):
self.base_dir = 'tests/fixtures/networks'
self.dispatch(['up', '-d'])
self.dispatch(['run', 'app', 'nslookup', 'app'])
self.dispatch(['run', 'app', 'nslookup', 'db'])
containers = self.project.get_service('app').containers(
stopped=True, one_off=True)
assert len(containers) == 2
for container in containers:
networks = container.get('NetworkSettings.Networks')
assert sorted(list(networks)) == [
'{}_{}'.format(self.project.name, name)
for name in ['back', 'front']
]
for _, config in networks.items():
assert not config['Aliases']
@v2_only()
def test_run_detached_connects_to_network(self):
self.base_dir = 'tests/fixtures/networks'
self.dispatch(['up', '-d'])
self.dispatch(['run', '-d', 'app', 'top'])
container = self.project.get_service('app').containers(one_off=True)[0]
networks = container.get('NetworkSettings.Networks')
assert sorted(list(networks)) == [
'{}_{}'.format(self.project.name, name)
for name in ['back', 'front']
]
for _, config in networks.items():
assert not config['Aliases']
assert self.lookup(container, 'app')
assert self.lookup(container, 'db')
def test_run_handles_sigint(self):
proc = start_process(self.base_dir, ['run', '-T', 'simple', 'top'])