From bdc6b47e1f8a0d9aafef7cb3a0b261b7f0f8bf4f Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Wed, 15 Jan 2014 13:06:25 +0000 Subject: [PATCH] service.recreate_containers() no longer removes the old containers We need to keep them around until the new ones have been started. --- fig/service.py | 11 +++++------ tests/service_test.py | 22 +++++++++++++++------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/fig/service.py b/fig/service.py index d16ab9566..c6396d8c7 100644 --- a/fig/service.py +++ b/fig/service.py @@ -76,21 +76,20 @@ class Service(object): def recreate_containers(self, **override_options): """ If a container for this service doesn't exist, create one. If there are - any, stop, remove and recreate them. + any, stop them and create new ones. Does not remove the old containers. """ - containers = self.containers(stopped=True) - if len(containers) == 0: + old_containers = self.containers(stopped=True) + if len(old_containers) == 0: return [self.create_container(**override_options)] else: new_containers = [] - for old_container in containers: + for old_container in old_containers: if old_container.is_running: old_container.stop() options = dict(override_options) options['volumes_from'] = old_container.id new_containers.append(self.create_container(**options)) - old_container.remove() - return new_containers + return (old_containers, new_containers) def start_container(self, container=None, **override_options): if container is None: diff --git a/tests/service_test.py b/tests/service_test.py index c59b4ebb1..ee2e90933 100644 --- a/tests/service_test.py +++ b/tests/service_test.py @@ -109,16 +109,24 @@ class ServiceTest(DockerClientTestCase): self.assertIn('/var/db', container.inspect()['Volumes']) def test_recreate_containers(self): - service = self.create_service('db', environment={'FOO': '1'}) - container = service.create_container() - self.assertEqual(container.dictionary['Config']['Env'], ['FOO=1']) + 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']) + service.start_container(old_container) + volume_path = old_container.inspect()['Volumes']['/var/db'] service.options['environment']['FOO'] = '2' - new_container = service.recreate_containers()[0] - self.assertEqual(new_container.dictionary['Config']['Env'], ['FOO=2']) + (old, new) = service.recreate_containers() + self.assertEqual(old, [old_container]) + self.assertEqual(len(new), 1) - self.assertEqual(len(service.containers(stopped=True)), 1) - self.assertNotEqual(container.id, new_container.id) + new_container = new[0] + self.assertEqual(new_container.dictionary['Config']['Env'], ['FOO=2']) + service.start_container(new_container) + self.assertEqual(new_container.inspect()['Volumes']['/var/db'], volume_path) + + self.assertEqual(len(service.containers(stopped=True)), 2) + self.assertNotEqual(old_container.id, new_container.id) def test_start_container_passes_through_options(self): db = self.create_service('db')