Browse Source

Support links in v2 files

Signed-off-by: Aanand Prasad <[email protected]>
Aanand Prasad 9 years ago
parent
commit
ee63075a34

+ 1 - 0
compose/config/service_schema_v2.json

@@ -88,6 +88,7 @@
         "image": {"type": "string"},
         "ipc": {"type": "string"},
         "labels": {"$ref": "#/definitions/list_or_dict"},
+        "links": {"type": "array", "items": {"type": "string"}, "uniqueItems": true},
 
         "logging": {
             "type": "object",

+ 1 - 2
compose/project.py

@@ -78,12 +78,11 @@ class Project(object):
             if use_networking:
                 networks = get_networks(service_dict, all_networks)
                 net = Net(networks[0]) if networks else Net("none")
-                links = []
             else:
                 networks = []
                 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)
 
             project.services.append(

+ 7 - 6
compose/service.py

@@ -426,10 +426,11 @@ class Service(object):
 
     def connect_container_to_networks(self, container):
         for network in self.networks:
-            log.debug('Connecting "{}" to "{}"'.format(container.name, network))
             self.client.connect_container_to_network(
                 container.id, network,
-                aliases=[self.name])
+                aliases=[self.name],
+                links=self._get_links(False),
+            )
 
     def remove_duplicate_containers(self, timeout=DEFAULT_TIMEOUT):
         for c in self.duplicate_containers():
@@ -500,9 +501,6 @@ class Service(object):
         return 1 if not numbers else max(numbers) + 1
 
     def _get_links(self, link_to_self):
-        if self.use_networking:
-            return []
-
         links = {}
 
         for service, link_name in self.links:
@@ -645,7 +643,10 @@ class Service(object):
 
     def _get_container_networking_config(self):
         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
             if network_name not in ['host', 'bridge']
         })

+ 7 - 12
tests/acceptance/cli_test.py

@@ -461,6 +461,10 @@ class CLITestCase(DockerClientTestCase):
         app_container = self.project.get_service('app').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
         assert sorted(back_network['Containers']) == sorted([db_container.id, app_container.id])
 
@@ -474,6 +478,9 @@ class CLITestCase(DockerClientTestCase):
         # app can see db
         assert self.lookup(app_container, "db")
 
+        # app has aliased db to "database"
+        assert self.lookup(app_container, "database")
+
     @v2_only()
     def test_up_missing_network(self):
         self.base_dir = 'tests/fixtures/networks'
@@ -566,18 +573,6 @@ class CLITestCase(DockerClientTestCase):
             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):
         self.base_dir = 'tests/fixtures/links-composefile'
         self.dispatch(['up', '-d', 'web'], None)

+ 2 - 0
tests/fixtures/networks/docker-compose.yml

@@ -9,6 +9,8 @@ services:
     image: busybox
     command: top
     networks: ["front", "back"]
+    links:
+      - "db:database"
   db:
     image: busybox
     command: top

+ 0 - 8
tests/unit/service_test.py

@@ -536,14 +536,6 @@ class ServiceTest(unittest.TestCase):
             ports=["127.0.0.1:1000-2000:2000-3000"])
         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):
         image_name = 'example/web:latest'
         service = Service('foo', image=image_name)