From a11d82b2cdb0817136a4d04f2233ffabbe943ec6 Mon Sep 17 00:00:00 2001 From: Matt Belle Date: Thu, 31 Oct 2024 15:07:08 -0400 Subject: [PATCH 1/2] Container.labels now returns an empty dict instead of None. Signed-off-by: Matt Belle --- podman/domain/containers.py | 10 +++++--- podman/tests/integration/test_containers.py | 27 +++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/podman/domain/containers.py b/podman/domain/containers.py index d2ef2e3e..f7fdd340 100644 --- a/podman/domain/containers.py +++ b/podman/domain/containers.py @@ -42,11 +42,15 @@ def image(self): @property def labels(self): """dict[str, str]: Returns labels associated with container.""" + labels = None with suppress(KeyError): + # Container created from ``list()`` operation if "Labels" in self.attrs: - return self.attrs["Labels"] - return self.attrs["Config"]["Labels"] - return {} + labels = self.attrs["Labels"] + # Container created from ``get()`` operation + else: + labels = self.attrs["Config"].get("Labels", {}) + return labels or {} @property def status(self): diff --git a/podman/tests/integration/test_containers.py b/podman/tests/integration/test_containers.py index 0fe0363f..a69f982b 100644 --- a/podman/tests/integration/test_containers.py +++ b/podman/tests/integration/test_containers.py @@ -56,6 +56,8 @@ def test_container_crud(self): with self.subTest("Inspect Container"): actual = self.client.containers.get(container.id) + + labeled_container.remove(v=True) self.assertIsInstance(actual, Container) self.assertEqual(actual.id, container.id) @@ -189,6 +191,31 @@ def test_container_rm_anonymous_volume(self): volume_list = self.client.volumes.list() self.assertEqual(len(volume_list), len(existing_volumes)) + def test_container_labels(self): + labels = {'label1': 'value1', 'label2': 'value2'} + labeled_container = self.client.containers.create(self.alpine_image, labels=labels) + unlabeled_container = self.client.containers.create( + self.alpine_image, + ) + + # inspect and list have 2 different schemas so we need to verify that we can + # successfully retrieve the labels on both + try: + # inspect schema + self.assertEqual(labeled_container.labels, labels) + self.assertEqual(unlabeled_container.labels, {}) + + # list schema + for container in self.client.containers.list(all=True): + if container.id == labeled_container.id: + self.assertEqual(container.labels, labels) + elif container.id == unlabeled_container.id: + self.assertEqual(container.labels, {}) + + finally: + labeled_container.remove(v=True) + unlabeled_container.remove(v=True) + if __name__ == '__main__': unittest.main() From 06d60c3ead22e80b4e8f01c24786b8ae4dd53376 Mon Sep 17 00:00:00 2001 From: Matt Belle Date: Thu, 31 Oct 2024 15:11:05 -0400 Subject: [PATCH 2/2] Removed accidental modification. Signed-off-by: Matt Belle --- podman/tests/integration/test_containers.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/podman/tests/integration/test_containers.py b/podman/tests/integration/test_containers.py index a69f982b..82b82b91 100644 --- a/podman/tests/integration/test_containers.py +++ b/podman/tests/integration/test_containers.py @@ -56,8 +56,6 @@ def test_container_crud(self): with self.subTest("Inspect Container"): actual = self.client.containers.get(container.id) - - labeled_container.remove(v=True) self.assertIsInstance(actual, Container) self.assertEqual(actual.id, container.id)