From 1a66543461c610ff0b6c1dcf117ed965edd5c249 Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Tue, 12 Jan 2016 16:25:15 +0000 Subject: [PATCH] Make the default network name '{project name}_default' Signed-off-by: Aanand Prasad --- compose/project.py | 14 +++++++++----- tests/acceptance/cli_test.py | 4 ++-- tests/integration/project_test.py | 8 ++++++-- tests/unit/project_test.py | 2 +- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/compose/project.py b/compose/project.py index d41b1f52a..71e353ecb 100644 --- a/compose/project.py +++ b/compose/project.py @@ -185,7 +185,7 @@ class Project(object): net = service_dict.pop('net', None) if not net: if self.use_networking: - return Net(self.name) + return Net(self.default_network_name) return Net(None) net_name = get_service_name_from_net(net) @@ -383,7 +383,7 @@ class Project(object): def get_network(self): try: - return self.client.inspect_network(self.name) + return self.client.inspect_network(self.default_network_name) except NotFound: return None @@ -396,9 +396,9 @@ class Project(object): log.info( 'Creating network "{}" with {}' - .format(self.name, driver_name) + .format(self.default_network_name, driver_name) ) - self.client.create_network(self.name, driver=self.network_driver) + self.client.create_network(self.default_network_name, driver=self.network_driver) def remove_network(self): network = self.get_network() @@ -406,7 +406,11 @@ class Project(object): self.client.remove_network(network['Id']) def uses_default_network(self): - return any(service.net.mode == self.name for service in self.services) + return any(service.net.mode == self.default_network_name for service in self.services) + + @property + def default_network_name(self): + return '{}_default'.format(self.name) def _inject_deps(self, acc, service): dep_names = service.get_dependency_names() diff --git a/tests/acceptance/cli_test.py b/tests/acceptance/cli_test.py index 7348edb04..46ed4237d 100644 --- a/tests/acceptance/cli_test.py +++ b/tests/acceptance/cli_test.py @@ -355,7 +355,7 @@ class CLITestCase(DockerClientTestCase): services = self.project.get_services() - networks = self.client.networks(names=[self.project.name]) + networks = self.client.networks(names=[self.project.default_network_name]) for n in networks: self.addCleanup(self.client.remove_network, n['Id']) self.assertEqual(len(networks), 1) @@ -649,7 +649,7 @@ class CLITestCase(DockerClientTestCase): self.dispatch(['--x-networking', '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.name]) + networks = self.client.networks(names=[self.project.default_network_name]) for n in networks: self.addCleanup(self.client.remove_network, n['Id']) self.assertEqual(len(networks), 1) diff --git a/tests/integration/project_test.py b/tests/integration/project_test.py index 1a342c8a4..a3e0f33a0 100644 --- a/tests/integration/project_test.py +++ b/tests/integration/project_test.py @@ -108,10 +108,14 @@ class ProjectTest(DockerClientTestCase): assert project.get_network() is None def test_get_network(self): - network_name = 'network_does_exist' - project = Project(network_name, [], self.client) + project_name = 'network_does_exist' + network_name = '{}_default'.format(project_name) + + project = Project(project_name, [], self.client) self.client.create_network(network_name) self.addCleanup(self.client.remove_network, network_name) + + assert isinstance(project.get_network(), dict) assert project.get_network()['Name'] == network_name def test_net_from_service(self): diff --git a/tests/unit/project_test.py b/tests/unit/project_test.py index c8590a1f9..63953376b 100644 --- a/tests/unit/project_test.py +++ b/tests/unit/project_test.py @@ -346,7 +346,7 @@ class ProjectTest(unittest.TestCase): self.assertEqual(service.net.mode, 'container:' + container_name) def test_uses_default_network_true(self): - web = Service('web', project='test', image="alpine", net=Net('test')) + web = Service('web', project='test', image="alpine", net=Net('test_default')) db = Service('web', project='test', image="alpine", net=Net('other')) project = Project('test', [web, db], None) assert project.uses_default_network()