Skip to content

Commit

Permalink
Merge pull request #462 from MattBelle/Issue-461_NullLabel
Browse files Browse the repository at this point in the history
Container.labels now returns an empty dict instead of None.
  • Loading branch information
openshift-merge-bot[bot] authored Nov 6, 2024
2 parents 8cfc8c2 + 06d60c3 commit d576182
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
10 changes: 7 additions & 3 deletions podman/domain/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
25 changes: 25 additions & 0 deletions podman/tests/integration/test_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,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()

0 comments on commit d576182

Please sign in to comment.