From 869abbbed472324de8d868c6c20f17b2eb632841 Mon Sep 17 00:00:00 2001 From: Jeffrey Zhang Date: Tue, 31 Jan 2017 14:36:55 +0800 Subject: [PATCH] Create a new container when restart a container when necessary Recreate and start a new container when container parameter is changed in COPY_ALWAYS strategy. Change-Id: I1e45c0dc34a93a18b664109823d6fd5b88c331a2 Closes-Bug: #1659799 --- ansible/library/kolla_docker.py | 7 +++++-- tests/test_kolla_docker.py | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/ansible/library/kolla_docker.py b/ansible/library/kolla_docker.py index 860ff50770..0104e980ea 100644 --- a/ansible/library/kolla_docker.py +++ b/ansible/library/kolla_docker.py @@ -574,10 +574,13 @@ class DockerWorker(object): if not container: self.start_container() - elif container and config_strategy == 'COPY_ONCE': + return + # If config_strategy is COPY_ONCE or container's parameters are + # changed, try to start a new one. + if config_strategy == 'COPY_ONCE' or self.check_container_differs(): self.remove_container() self.start_container() - elif container and config_strategy == 'COPY_ALWAYS': + elif config_strategy == 'COPY_ALWAYS': self.restart_container() def start_container(self): diff --git a/tests/test_kolla_docker.py b/tests/test_kolla_docker.py index e3fc51525f..7faa23f2ff 100644 --- a/tests/test_kolla_docker.py +++ b/tests/test_kolla_docker.py @@ -385,11 +385,26 @@ class TestContainer(base.BaseTestCase): self.dw.check_container = mock.Mock( return_value=self.fake_data['containers'][0]) self.dw.restart_container = mock.Mock() + self.dw.check_container_differs = mock.Mock(return_value=False) self.dw.recreate_or_restart_container() self.dw.restart_container.assert_called_once_with() + def test_recreate_or_restart_container_container_copy_always_differs(self): + self.dw = get_DockerWorker({ + 'environment': dict(KOLLA_CONFIG_STRATEGY='COPY_ALWAYS')}) + self.dw.check_container = mock.Mock( + return_value=self.fake_data['containers'][0]) + self.dw.start_container = mock.Mock() + self.dw.remove_container = mock.Mock() + self.dw.check_container_differs = mock.Mock(return_value=True) + + self.dw.recreate_or_restart_container() + + self.dw.remove_container.assert_called_once_with() + self.dw.start_container.assert_called_once_with() + def test_recreate_or_restart_container_container_copy_once(self): self.dw = get_DockerWorker({ 'environment': dict(KOLLA_CONFIG_STRATEGY='COPY_ONCE')})