From cdcf622018f6ea59c1fb9fe5e9cc3a31526cc3f3 Mon Sep 17 00:00:00 2001 From: Michal Nasiadka Date: Thu, 23 Mar 2023 13:39:05 +0100 Subject: [PATCH] Fix restart_container when restart_policy is no Closes-Bug: #2012654 Change-Id: I9735b4409a48d80851cbc26a9edbf370af1d45bf --- ansible/module_utils/kolla_docker_worker.py | 17 ++++++++++------- tests/kolla_docker_tests/test_docker_worker.py | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/ansible/module_utils/kolla_docker_worker.py b/ansible/module_utils/kolla_docker_worker.py index 6d6408101f..72400ea202 100644 --- a/ansible/module_utils/kolla_docker_worker.py +++ b/ansible/module_utils/kolla_docker_worker.py @@ -469,13 +469,16 @@ class DockerWorker(ContainerWorker): msg="No such container: {}".format(name)) else: self.changed = True - self.systemd.create_unit_file() - - if not self.systemd.restart(): - self.module.fail_json( - changed=True, - msg="Container timed out", - **self.check_container()) + if self.params.get('restart_policy') != 'no': + self.systemd.create_unit_file() + if not self.systemd.restart(): + self.module.fail_json( + changed=True, + msg="Container timed out", + **self.check_container()) + else: + self.dc.stop(name, timeout=graceful_timeout) + self.dc.start(name) def create_volume(self): if not self.check_volume(): diff --git a/tests/kolla_docker_tests/test_docker_worker.py b/tests/kolla_docker_tests/test_docker_worker.py index a7fa775f50..2c5efd716a 100644 --- a/tests/kolla_docker_tests/test_docker_worker.py +++ b/tests/kolla_docker_tests/test_docker_worker.py @@ -617,6 +617,24 @@ class TestContainer(base.BaseTestCase): self.dw.dc.containers.assert_called_once_with(all=True) self.dw.systemd.restart.assert_called_once_with() + def test_restart_container_no_systemd(self): + self.dw = get_DockerWorker({'name': 'my_container', + 'action': 'stop_container', + 'restart_policy': 'no'}) + self.dw.dc.containers.return_value = self.fake_data['containers'] + self.fake_data['container_inspect'].update( + self.fake_data['containers'][0]) + self.dw.dc.inspect_container.return_value = ( + self.fake_data['container_inspect']) + self.dw.restart_container() + + self.assertTrue(self.dw.changed) + self.dw.dc.containers.assert_called_once_with(all=True) + self.dw.dc.stop.assert_called_once_with( + 'my_container', timeout=10) + self.dw.dc.start.assert_called_once_with('my_container') + self.dw.module.fail_json.assert_not_called() + def test_restart_container_not_exists(self): self.dw = get_DockerWorker({'name': 'fake-container', 'action': 'restart_container'})