From 8d5cb518d66115515ef185e28562fd5e9a60de2b Mon Sep 17 00:00:00 2001 From: Roman Krček Date: Fri, 14 Feb 2025 14:12:44 +0000 Subject: Add ability to get stopped container's facts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit kolla_container_facts could only get running containers, this patch adds the ability to specify, that user wants even stopped containers. Also updates module's docs and corrects few typos. Change-Id: I9ace3c1d802bc00ce692b3ca755384c6b62bd72d Signed-off-by: Roman Krček --- tests/test_kolla_container_facts.py | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) (limited to 'tests/test_kolla_container_facts.py') diff --git a/tests/test_kolla_container_facts.py b/tests/test_kolla_container_facts.py index e58b058c5..b6a3d8000 100644 --- a/tests/test_kolla_container_facts.py +++ b/tests/test_kolla_container_facts.py @@ -109,7 +109,7 @@ def contruct_volume(vol_dict: dict) -> mock.Mock: return volume -def get_containers(override=None): +def get_containers(override=None, all: bool = False): if override: cont_dicts = override else: @@ -117,9 +117,11 @@ def get_containers(override=None): containers = [] for c in cont_dicts: - # Only running containers should be returned by the container APIs - if c['State']['Status'] == 'running': - containers.append(construct_container(c)) + # With the option "all", only running containers are returned + # by the container API + if not all and c['State']['Status'] != 'running': + continue + containers.append(construct_container(c)) return containers @@ -152,8 +154,9 @@ class TestContainerFacts(base.BaseTestCase): self.assertDictEqual( self.fake_data['containers'][0], self.dfw.result['containers']['my_container']) + self.dfw.client.containers.list.assert_called_once_with(all=False) - def test_get_container_multi(self): + def test_get_containers_multi(self): self.dfw = get_DockerFactsWorker( {'name': ['my_container', 'exited_container'], 'action': 'get_containers'}) @@ -165,8 +168,9 @@ class TestContainerFacts(base.BaseTestCase): self.assertIn('my_container', self.dfw.result['containers']) self.assertNotIn('my_container', self.dfw.result) self.assertNotIn('exited_container', self.dfw.result['containers']) + self.dfw.client.containers.list.assert_called_once_with(all=False) - def test_get_container_all(self): + def test_get_containers_all_running(self): self.dfw = get_DockerFactsWorker({'name': [], 'action': 'get_containers'}) running_containers = get_containers(self.fake_data['containers']) @@ -177,6 +181,21 @@ class TestContainerFacts(base.BaseTestCase): self.assertIn('my_container', self.dfw.result['containers']) self.assertNotIn('my_container', self.dfw.result) self.assertNotIn('exited_container', self.dfw.result['containers']) + self.dfw.client.containers.list.assert_called_once_with(all=False) + + def test_get_containers_all_including_stopped(self): + self.dfw = get_DockerFactsWorker({'name': [], + 'action': 'get_containers', + 'args': { + 'get_all_containers': True}}) + all_containers = get_containers(self.fake_data['containers'], all=True) + self.dfw.client.containers.list.return_value = all_containers + self.dfw.get_containers() + + self.assertFalse(self.dfw.result['changed']) + self.assertIn('my_container', self.dfw.result['containers']) + self.assertIn('exited_container', self.dfw.result['containers']) + self.dfw.client.containers.list.assert_called_once_with(all=True) def test_get_containers_env(self): fake_env = dict(KOLLA_BASE_DISTRO='ubuntu', -- cgit v1.2.3