Merge pull request #128 from orchardup/better-error-message-when-service-is-not-dict

Improve error when service is not a dict
This commit is contained in:
Aanand Prasad 2014-03-03 16:40:59 +00:00
commit a68b4e6dde
3 changed files with 31 additions and 4 deletions

View File

@ -8,7 +8,7 @@ import signal
from inspect import getdoc from inspect import getdoc
from .. import __version__ from .. import __version__
from ..project import NoSuchService, DependencyError from ..project import NoSuchService, ConfigurationError
from ..service import CannotBeScaledError from ..service import CannotBeScaledError
from .command import Command from .command import Command
from .formatter import Formatter from .formatter import Formatter
@ -40,7 +40,7 @@ def main():
except KeyboardInterrupt: except KeyboardInterrupt:
log.error("\nAborting.") log.error("\nAborting.")
sys.exit(1) sys.exit(1)
except (UserError, NoSuchService, DependencyError) as e: except (UserError, NoSuchService, ConfigurationError) as e:
log.error(e.msg) log.error(e.msg)
sys.exit(1) sys.exit(1)
except NoSuchCommand as e: except NoSuchCommand as e:

View File

@ -67,6 +67,8 @@ class Project(object):
def from_config(cls, name, config, client): def from_config(cls, name, config, client):
dicts = [] dicts = []
for service_name, service in list(config.items()): for service_name, service in list(config.items()):
if not isinstance(service, dict):
raise ConfigurationError('Service "%s" doesn\'t have any configuration options. All top level keys in your fig.yml must map to a dictionary of configuration options.')
service['name'] = service_name service['name'] = service_name
dicts.append(service) dicts.append(service)
return cls.from_dicts(name, dicts, client) return cls.from_dicts(name, dicts, client)
@ -156,9 +158,13 @@ class NoSuchService(Exception):
return self.msg return self.msg
class DependencyError(Exception): class ConfigurationError(Exception):
def __init__(self, msg): def __init__(self, msg):
self.msg = msg self.msg = msg
def __str__(self): def __str__(self):
return self.msg return self.msg
class DependencyError(ConfigurationError):
pass

View File

@ -1,5 +1,5 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from fig.project import Project from fig.project import Project, ConfigurationError
from .testcases import DockerClientTestCase from .testcases import DockerClientTestCase
@ -37,6 +37,27 @@ class ProjectTest(DockerClientTestCase):
self.assertEqual(project.services[0].name, 'db') self.assertEqual(project.services[0].name, 'db')
self.assertEqual(project.services[1].name, 'web') self.assertEqual(project.services[1].name, 'web')
def test_from_config(self):
project = Project.from_config('figtest', {
'web': {
'image': 'ubuntu',
},
'db': {
'image': 'ubuntu',
},
}, self.client)
self.assertEqual(len(project.services), 2)
self.assertEqual(project.get_service('web').name, 'web')
self.assertEqual(project.get_service('web').options['image'], 'ubuntu')
self.assertEqual(project.get_service('db').name, 'db')
self.assertEqual(project.get_service('db').options['image'], 'ubuntu')
def test_from_config_throws_error_when_not_dict(self):
with self.assertRaises(ConfigurationError):
project = Project.from_config('figtest', {
'web': 'ubuntu',
}, self.client)
def test_get_service(self): def test_get_service(self):
web = self.create_service('web') web = self.create_service('web')
project = Project('test', [web], self.client) project = Project('test', [web], self.client)