From 0c12db06ec8bafd7a640e09bc0e374038c062b8c Mon Sep 17 00:00:00 2001 From: d11wtq Date: Sat, 7 Jun 2014 13:03:53 +0000 Subject: [PATCH] Move 'auto_start' option default to Service and add unit tests Signed-off-by: Chris Corbyn --- fig/project.py | 11 +++-------- fig/service.py | 6 ++++-- tests/unit/project_test.py | 2 +- tests/unit/service_test.py | 4 ++++ 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/fig/project.py b/fig/project.py index 1f1571f08..605e4c077 100644 --- a/fig/project.py +++ b/fig/project.py @@ -65,12 +65,7 @@ class Project(object): del service_dict['links'] - auto_start = True - if 'auto_start' in service_dict: - auto_start = service_dict.get('auto_start', True) - del service_dict['auto_start'] - - project.services.append(Service(auto_start=auto_start, client=client, project=name, links=links, **service_dict)) + project.services.append(Service(client=client, project=name, links=links, **service_dict)) return project @classmethod @@ -94,7 +89,7 @@ class Project(object): raise NoSuchService(name) - def get_services(self, service_names=None, auto_start=True): + def get_services(self, service_names=None): """ Returns a list of this project's services filtered by the provided list of names, or all services if @@ -106,7 +101,7 @@ class Project(object): do not exist. """ if service_names is None or len(service_names) == 0: - return filter(lambda srv: srv.auto_start == auto_start, self.services) + return filter(lambda s: s.options['auto_start'], self.services) else: unsorted = [self.get_service(name) for name in service_names] return [s for s in self.services if s in unsorted] diff --git a/fig/service.py b/fig/service.py index a954dc4a3..7df3c46b9 100644 --- a/fig/service.py +++ b/fig/service.py @@ -37,7 +37,7 @@ class ConfigError(ValueError): class Service(object): - def __init__(self, name, auto_start=True, client=None, project='default', links=[], **options): + def __init__(self, name, client=None, project='default', links=[], **options): if not re.match('^[a-zA-Z0-9]+$', name): raise ConfigError('Invalid name: %s' % name) if not re.match('^[a-zA-Z0-9]+$', project): @@ -45,6 +45,9 @@ class Service(object): if 'image' in options and 'build' in options: raise ConfigError('Service %s has both an image and build path specified. A service can either be built to image or use an existing image, not both.' % name) + if 'auto_start' not in options: + options['auto_start'] = True + supported_options = DOCKER_CONFIG_KEYS + ['auto_start', 'build', 'expose'] for k in options: @@ -55,7 +58,6 @@ class Service(object): raise ConfigError(msg) self.name = name - self.auto_start = auto_start self.client = client self.project = project self.links = links or [] diff --git a/tests/unit/project_test.py b/tests/unit/project_test.py index 4a2ad1422..f5bacc654 100644 --- a/tests/unit/project_test.py +++ b/tests/unit/project_test.py @@ -13,7 +13,7 @@ class ProjectTest(unittest.TestCase): { 'name': 'db', 'image': 'ubuntu' - } + }, ], None) self.assertEqual(len(project.services), 2) self.assertEqual(project.get_service('web').name, 'web') diff --git a/tests/unit/service_test.py b/tests/unit/service_test.py index 490cb60d6..330e04114 100644 --- a/tests/unit/service_test.py +++ b/tests/unit/service_test.py @@ -20,6 +20,10 @@ class ServiceTest(unittest.TestCase): Service('a') Service('foo') + def test_auto_start_defaults_true(self): + service = Service(name='foo', project='bar') + self.assertEqual(service.options['auto_start'], True) + def test_project_validation(self): self.assertRaises(ConfigError, lambda: Service(name='foo', project='_')) Service(name='foo', project='bar')