Fix restart_container when restart_policy is no

Closes-Bug: #2012654

Change-Id: I9735b4409a48d80851cbc26a9edbf370af1d45bf
This commit is contained in:
Michal Nasiadka 2023-03-23 13:39:05 +01:00
parent 2845861ef9
commit cdcf622018
2 changed files with 28 additions and 7 deletions

View File

@ -469,13 +469,16 @@ class DockerWorker(ContainerWorker):
msg="No such container: {}".format(name))
else:
self.changed = True
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():

View File

@ -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'})