Adds a 'labels' property to the container model

The Docker API seems to respond with a 'null' value for the 'Labels'
attribute from containers that were created with older Docker versions.
An empty dictionary is returned in this case.

Signed-off-by: Frank Sachsenheim <funkyfuture@riseup.net>
This commit is contained in:
Frank Sachsenheim 2017-03-04 00:22:19 +01:00
parent 6529fa599c
commit 1cd56b9f0c
4 changed files with 15 additions and 1 deletions

View File

@ -18,6 +18,14 @@ class Container(Model):
if self.attrs.get('Name') is not None:
return self.attrs['Name'].lstrip('/')
@property
def labels(self):
"""
The labels of a container as dictionary.
"""
result = self.attrs['Config'].get('Labels')
return result or {}
@property
def status(self):
"""

View File

@ -25,6 +25,7 @@ Container objects
.. autoattribute:: short_id
.. autoattribute:: name
.. autoattribute:: status
.. autoattribute:: labels
.. py:attribute:: attrs
The raw representation of this object from the server.

View File

@ -134,7 +134,7 @@ def get_fake_inspect_container(tty=False):
status_code = 200
response = {
'Id': FAKE_CONTAINER_ID,
'Config': {'Privileged': True, 'Tty': tty},
'Config': {'Labels': {'foo': 'bar'}, 'Privileged': True, 'Tty': tty},
'ID': FAKE_CONTAINER_ID,
'Image': 'busybox:latest',
'Name': 'foobar',

View File

@ -390,6 +390,11 @@ class ContainerTest(unittest.TestCase):
container.kill(signal=5)
client.api.kill.assert_called_with(FAKE_CONTAINER_ID, signal=5)
def test_labels(self):
client = make_fake_client()
container = client.containers.get(FAKE_CONTAINER_ID)
assert container.labels == {'foo': 'bar'}
def test_logs(self):
client = make_fake_client()
container = client.containers.get(FAKE_CONTAINER_ID)