Make the default network name '{project name}_default'

Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
This commit is contained in:
Aanand Prasad 2016-01-12 16:25:15 +00:00
parent b786b47bc8
commit 1a66543461
4 changed files with 18 additions and 10 deletions

View File

@ -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()

View File

@ -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)

View File

@ -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):

View File

@ -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()