Browse Source

Use an anonymous intermediate container so that when recreating containers, suffixes always start from 1

Aanand Prasad 11 years ago
parent
commit
ea4753c49a
2 changed files with 19 additions and 4 deletions
  1. 13 2
      fig/service.py
  2. 6 2
      tests/service_test.py

+ 13 - 2
fig/service.py

@@ -97,10 +97,21 @@ class Service(object):
         if container.is_running:
             container.stop(timeout=1)
 
+        intermediate_container = Container.create(
+            self.client,
+            image='ubuntu',
+            command='echo',
+            volumes_from=container.id,
+        )
+        intermediate_container.start()
+        intermediate_container.wait()
+        container.remove()
+
         options = dict(override_options)
-        options['volumes_from'] = container.id
+        options['volumes_from'] = intermediate_container.id
+        new_container = self.create_container(**options)
 
-        return (container, self.create_container(**options))
+        return (intermediate_container, new_container)
 
     def start_container(self, container=None, **override_options):
         if container is None:

+ 6 - 2
tests/service_test.py

@@ -112,20 +112,24 @@ class ServiceTest(DockerClientTestCase):
         service = self.create_service('db', environment={'FOO': '1'}, volumes=['/var/db'])
         old_container = service.create_container()
         self.assertEqual(old_container.dictionary['Config']['Env'], ['FOO=1'])
+        self.assertEqual(old_container.name, 'figtest_db_1')
         service.start_container(old_container)
         volume_path = old_container.inspect()['Volumes']['/var/db']
 
+        num_containers_before = len(self.client.containers(all=True))
+
         service.options['environment']['FOO'] = '2'
         (old, new) = service.recreate_containers()
-        self.assertEqual(old, [old_container])
+        self.assertEqual(len(old), 1)
         self.assertEqual(len(new), 1)
 
         new_container = new[0]
         self.assertEqual(new_container.dictionary['Config']['Env'], ['FOO=2'])
+        self.assertEqual(new_container.name, 'figtest_db_1')
         service.start_container(new_container)
         self.assertEqual(new_container.inspect()['Volumes']['/var/db'], volume_path)
 
-        self.assertEqual(len(service.containers(stopped=True)), 2)
+        self.assertEqual(len(self.client.containers(all=True)), num_containers_before + 1)
         self.assertNotEqual(old_container.id, new_container.id)
 
     def test_start_container_passes_through_options(self):