Add options for containers to Service

This commit is contained in:
Ben Firshman 2013-12-10 20:51:55 +00:00
parent b59436742b
commit bf2505d15d
3 changed files with 13 additions and 9 deletions

View File

@ -2,15 +2,14 @@ import re
class Service(object):
def __init__(self, name, client=None, image=None, command=None, links=None):
def __init__(self, name, client=None, links=[], **options):
if not re.match('^[a-zA-Z0-9_]+$', name):
raise ValueError('Invalid name: %s' % name)
self.name = name
self.client = client
self.image = image
self.command = command
self.links = links or []
self.options = options
@property
def containers(self):
@ -33,7 +32,7 @@ class Service(object):
def start_container(self):
number = self.next_container_number()
name = make_name(self.name, number)
container = self.client.create_container(self.image, self.command, name=name)
container = self.client.create_container(name=name, **self.options)
self.client.start(
container['Id'],
links=self._get_links(),

View File

@ -17,9 +17,9 @@ class ServiceCollectionTest(DockerClientTestCase):
])
self.assertEqual(len(collection), 2)
self.assertEqual(collection.get('web').name, 'web')
self.assertEqual(collection.get('web').image, 'ubuntu')
self.assertEqual(collection.get('web').options['image'], 'ubuntu')
self.assertEqual(collection.get('db').name, 'db')
self.assertEqual(collection.get('db').image, 'ubuntu')
self.assertEqual(collection.get('db').options['image'], 'ubuntu')
def test_from_dict_sorts_in_dependency_order(self):
collection = ServiceCollection.from_dicts(None, [

View File

@ -59,11 +59,16 @@ class NameTestCase(DockerClientTestCase):
service.stop()
self.assertEqual(len(service.containers), 0)
def test_links_are_created_when_starting(self):
def test_start_container_passes_through_options(self):
db = self.create_service('db', environment={'FOO': 'BAR'})
db.start_container()
self.assertEqual(db.inspect()[0]['Config']['Env'], ['FOO=BAR'])
def test_start_container_creates_links(self):
db = self.create_service('db')
web = self.create_service('web', links=[db])
db.start()
web.start()
db.start_container()
web.start_container()
self.assertIn('/web_1/db_1', db.containers[0]['Names'])
db.stop()
web.stop()