Skip to content

Commit d310d95

Browse files
committed
Add test for container list with sparse=True
Signed-off-by: Joffrey F <[email protected]>
1 parent 726d7f3 commit d310d95

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

docker/models/containers.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
from ..api import APIClient
66
from ..constants import DEFAULT_DATA_CHUNK_SIZE
7-
from ..errors import (ContainerError, ImageNotFound,
8-
create_unexpected_kwargs_error)
7+
from ..errors import (
8+
ContainerError, DockerException, ImageNotFound,
9+
create_unexpected_kwargs_error
10+
)
911
from ..types import HostConfig
1012
from ..utils import version_gte
1113
from .images import Image
@@ -27,7 +29,7 @@ def image(self):
2729
"""
2830
The image of the container.
2931
"""
30-
image_id = self.attrs['Image']
32+
image_id = self.attrs.get('ImageID', self.attrs['Image'])
3133
if image_id is None:
3234
return None
3335
return self.client.images.get(image_id.split(':')[1])
@@ -37,15 +39,23 @@ def labels(self):
3739
"""
3840
The labels of a container as dictionary.
3941
"""
40-
result = self.attrs['Config'].get('Labels')
41-
return result or {}
42+
try:
43+
result = self.attrs['Config'].get('Labels')
44+
return result or {}
45+
except KeyError:
46+
raise DockerException(
47+
'Label data is not available for sparse objects. Call reload()'
48+
' to retrieve all information'
49+
)
4250

4351
@property
4452
def status(self):
4553
"""
4654
The status of the container. For example, ``running``, or ``exited``.
4755
"""
48-
return self.attrs['State']['Status']
56+
if isinstance(self.attrs['State'], dict):
57+
return self.attrs['State']['Status']
58+
return self.attrs['State']
4959

5060
def attach(self, **kwargs):
5161
"""
@@ -863,14 +873,16 @@ def list(self, all=False, before=None, filters=None, limit=-1, since=None,
863873
container. Give the container name or id.
864874
- `since` (str): Only containers created after a particular
865875
container. Give container name or id.
866-
sparse (bool): Do not inspect containers. Returns partial
867-
informations, but guaranteed not to block. Use reload() on
868-
each container to get the full list of attributes.
869876
870877
A comprehensive list can be found in the documentation for
871878
`docker ps
872879
<https://docs.docker.com/engine/reference/commandline/ps>`_.
873880
881+
sparse (bool): Do not inspect containers. Returns partial
882+
information, but guaranteed not to block. Use
883+
:py:meth:`Container.reload` on resulting objects to retrieve
884+
all attributes. Default: ``False``
885+
874886
Returns:
875887
(list of :py:class:`Container`)
876888

tests/integration/models_containers_test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,28 @@ def test_list(self):
159159

160160
container = containers[0]
161161
assert container.attrs['Config']['Image'] == 'alpine'
162+
assert container.status == 'running'
163+
assert container.image == client.images.get('alpine')
164+
165+
container.kill()
166+
container.remove()
167+
assert container_id not in [c.id for c in client.containers.list()]
168+
169+
def test_list_sparse(self):
170+
client = docker.from_env(version=TEST_API_VERSION)
171+
container_id = client.containers.run(
172+
"alpine", "sleep 300", detach=True).id
173+
self.tmp_containers.append(container_id)
174+
containers = [c for c in client.containers.list(sparse=True) if c.id ==
175+
container_id]
176+
assert len(containers) == 1
177+
178+
container = containers[0]
179+
assert container.attrs['Image'] == 'alpine'
180+
assert container.status == 'running'
181+
assert container.image == client.images.get('alpine')
182+
with pytest.raises(docker.errors.DockerException):
183+
container.labels
162184

163185
container.kill()
164186
container.remove()

0 commit comments

Comments
 (0)