mirror of
https://github.com/docker/compose.git
synced 2025-07-20 20:24:30 +02:00
Support links in v2 files
Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
This commit is contained in:
parent
5a249bd9f5
commit
ee63075a34
@ -88,6 +88,7 @@
|
|||||||
"image": {"type": "string"},
|
"image": {"type": "string"},
|
||||||
"ipc": {"type": "string"},
|
"ipc": {"type": "string"},
|
||||||
"labels": {"$ref": "#/definitions/list_or_dict"},
|
"labels": {"$ref": "#/definitions/list_or_dict"},
|
||||||
|
"links": {"type": "array", "items": {"type": "string"}, "uniqueItems": true},
|
||||||
|
|
||||||
"logging": {
|
"logging": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
|
@ -78,12 +78,11 @@ class Project(object):
|
|||||||
if use_networking:
|
if use_networking:
|
||||||
networks = get_networks(service_dict, all_networks)
|
networks = get_networks(service_dict, all_networks)
|
||||||
net = Net(networks[0]) if networks else Net("none")
|
net = Net(networks[0]) if networks else Net("none")
|
||||||
links = []
|
|
||||||
else:
|
else:
|
||||||
networks = []
|
networks = []
|
||||||
net = project.get_net(service_dict)
|
net = project.get_net(service_dict)
|
||||||
links = project.get_links(service_dict)
|
|
||||||
|
|
||||||
|
links = project.get_links(service_dict)
|
||||||
volumes_from = get_volumes_from(project, service_dict)
|
volumes_from = get_volumes_from(project, service_dict)
|
||||||
|
|
||||||
project.services.append(
|
project.services.append(
|
||||||
|
@ -426,10 +426,11 @@ class Service(object):
|
|||||||
|
|
||||||
def connect_container_to_networks(self, container):
|
def connect_container_to_networks(self, container):
|
||||||
for network in self.networks:
|
for network in self.networks:
|
||||||
log.debug('Connecting "{}" to "{}"'.format(container.name, network))
|
|
||||||
self.client.connect_container_to_network(
|
self.client.connect_container_to_network(
|
||||||
container.id, network,
|
container.id, network,
|
||||||
aliases=[self.name])
|
aliases=[self.name],
|
||||||
|
links=self._get_links(False),
|
||||||
|
)
|
||||||
|
|
||||||
def remove_duplicate_containers(self, timeout=DEFAULT_TIMEOUT):
|
def remove_duplicate_containers(self, timeout=DEFAULT_TIMEOUT):
|
||||||
for c in self.duplicate_containers():
|
for c in self.duplicate_containers():
|
||||||
@ -500,9 +501,6 @@ class Service(object):
|
|||||||
return 1 if not numbers else max(numbers) + 1
|
return 1 if not numbers else max(numbers) + 1
|
||||||
|
|
||||||
def _get_links(self, link_to_self):
|
def _get_links(self, link_to_self):
|
||||||
if self.use_networking:
|
|
||||||
return []
|
|
||||||
|
|
||||||
links = {}
|
links = {}
|
||||||
|
|
||||||
for service, link_name in self.links:
|
for service, link_name in self.links:
|
||||||
@ -645,7 +643,10 @@ class Service(object):
|
|||||||
|
|
||||||
def _get_container_networking_config(self):
|
def _get_container_networking_config(self):
|
||||||
return self.client.create_networking_config({
|
return self.client.create_networking_config({
|
||||||
network_name: self.client.create_endpoint_config(aliases=[self.name])
|
network_name: self.client.create_endpoint_config(
|
||||||
|
aliases=[self.name],
|
||||||
|
links=self._get_links(False),
|
||||||
|
)
|
||||||
for network_name in self.networks
|
for network_name in self.networks
|
||||||
if network_name not in ['host', 'bridge']
|
if network_name not in ['host', 'bridge']
|
||||||
})
|
})
|
||||||
|
@ -461,6 +461,10 @@ class CLITestCase(DockerClientTestCase):
|
|||||||
app_container = self.project.get_service('app').containers()[0]
|
app_container = self.project.get_service('app').containers()[0]
|
||||||
db_container = self.project.get_service('db').containers()[0]
|
db_container = self.project.get_service('db').containers()[0]
|
||||||
|
|
||||||
|
for net_name in [front_name, back_name]:
|
||||||
|
links = app_container.get('NetworkSettings.Networks.{}.Links'.format(net_name))
|
||||||
|
assert '{}:database'.format(db_container.name) in links
|
||||||
|
|
||||||
# db and app joined the back network
|
# db and app joined the back network
|
||||||
assert sorted(back_network['Containers']) == sorted([db_container.id, app_container.id])
|
assert sorted(back_network['Containers']) == sorted([db_container.id, app_container.id])
|
||||||
|
|
||||||
@ -474,6 +478,9 @@ class CLITestCase(DockerClientTestCase):
|
|||||||
# app can see db
|
# app can see db
|
||||||
assert self.lookup(app_container, "db")
|
assert self.lookup(app_container, "db")
|
||||||
|
|
||||||
|
# app has aliased db to "database"
|
||||||
|
assert self.lookup(app_container, "database")
|
||||||
|
|
||||||
@v2_only()
|
@v2_only()
|
||||||
def test_up_missing_network(self):
|
def test_up_missing_network(self):
|
||||||
self.base_dir = 'tests/fixtures/networks'
|
self.base_dir = 'tests/fixtures/networks'
|
||||||
@ -566,18 +573,6 @@ class CLITestCase(DockerClientTestCase):
|
|||||||
for name in ['bar', 'foo']
|
for name in ['bar', 'foo']
|
||||||
]
|
]
|
||||||
|
|
||||||
@v2_only()
|
|
||||||
def test_up_with_links_is_invalid(self):
|
|
||||||
self.base_dir = 'tests/fixtures/v2-simple'
|
|
||||||
|
|
||||||
result = self.dispatch(
|
|
||||||
['-f', 'links-invalid.yml', 'up', '-d'],
|
|
||||||
returncode=1)
|
|
||||||
|
|
||||||
# TODO: fix validation error messages for v2 files
|
|
||||||
# assert "Unsupported config option for service 'simple': 'links'" in result.stderr
|
|
||||||
assert "Unsupported config option" in result.stderr
|
|
||||||
|
|
||||||
def test_up_with_links_v1(self):
|
def test_up_with_links_v1(self):
|
||||||
self.base_dir = 'tests/fixtures/links-composefile'
|
self.base_dir = 'tests/fixtures/links-composefile'
|
||||||
self.dispatch(['up', '-d', 'web'], None)
|
self.dispatch(['up', '-d', 'web'], None)
|
||||||
|
2
tests/fixtures/networks/docker-compose.yml
vendored
2
tests/fixtures/networks/docker-compose.yml
vendored
@ -9,6 +9,8 @@ services:
|
|||||||
image: busybox
|
image: busybox
|
||||||
command: top
|
command: top
|
||||||
networks: ["front", "back"]
|
networks: ["front", "back"]
|
||||||
|
links:
|
||||||
|
- "db:database"
|
||||||
db:
|
db:
|
||||||
image: busybox
|
image: busybox
|
||||||
command: top
|
command: top
|
||||||
|
@ -536,14 +536,6 @@ class ServiceTest(unittest.TestCase):
|
|||||||
ports=["127.0.0.1:1000-2000:2000-3000"])
|
ports=["127.0.0.1:1000-2000:2000-3000"])
|
||||||
self.assertEqual(service.specifies_host_port(), True)
|
self.assertEqual(service.specifies_host_port(), True)
|
||||||
|
|
||||||
def test_get_links_with_networking(self):
|
|
||||||
service = Service(
|
|
||||||
'foo',
|
|
||||||
image='foo',
|
|
||||||
links=[(Service('one'), 'one')],
|
|
||||||
use_networking=True)
|
|
||||||
self.assertEqual(service._get_links(link_to_self=True), [])
|
|
||||||
|
|
||||||
def test_image_name_from_config(self):
|
def test_image_name_from_config(self):
|
||||||
image_name = 'example/web:latest'
|
image_name = 'example/web:latest'
|
||||||
service = Service('foo', image=image_name)
|
service = Service('foo', image=image_name)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user