Allow container name property from sparse list

When listing containers with `sparse=True` the container's name property
does not work because there is no 'Name' attribue on the object returned
by the Docker API. Instead the name is in the 'Names' attribute.

Signed-off-by: Stuart Thomson <stuartwthomson@live.co.uk>
This commit is contained in:
Stuart Thomson 2020-08-04 19:19:38 +01:00
parent b4beaaac8c
commit 290ee89a8c
2 changed files with 4 additions and 1 deletions

View File

@ -28,6 +28,8 @@ class Container(Model):
"""
if self.attrs.get('Name') is not None:
return self.attrs['Name'].lstrip('/')
if self.attrs.get('Names') is not None:
return self.attrs['Names'][0].lstrip('/')
@property
def image(self):

View File

@ -210,7 +210,7 @@ class ContainerCollectionTest(BaseIntegrationTest):
def test_list_sparse(self):
client = docker.from_env(version=TEST_API_VERSION)
container_id = client.containers.run(
"alpine", "sleep 300", detach=True).id
"alpine", "sleep 300", detach=True, name='a_container').id
self.tmp_containers.append(container_id)
containers = [c for c in client.containers.list(sparse=True) if c.id ==
container_id]
@ -220,6 +220,7 @@ class ContainerCollectionTest(BaseIntegrationTest):
assert container.attrs['Image'] == 'alpine'
assert container.status == 'running'
assert container.image == client.images.get('alpine')
assert container.name == 'a_container'
with pytest.raises(docker.errors.DockerException):
container.labels