Browse Source

Add options for containers to Service

Ben Firshman 12 years ago
parent
commit
bf2505d15d
3 changed files with 13 additions and 9 deletions
  1. 3 4
      plum/service.py
  2. 2 2
      plum/tests/service_collection_test.py
  3. 8 3
      plum/tests/service_test.py

+ 3 - 4
plum/service.py

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

+ 2 - 2
plum/tests/service_collection_test.py

@@ -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, [

+ 8 - 3
plum/tests/service_test.py

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