diff options
| author | Joffrey F <joffrey@docker.com> | 2018-03-14 16:54:29 -0700 |
|---|---|---|
| committer | Joffrey F <joffrey@docker.com> | 2018-03-14 16:54:29 -0700 |
| commit | 78600d52ab7dbfae2ce554f80c5d0fedd71a6f99 (patch) | |
| tree | 9b4952b48e9da154f4137ac7aae77125f38fda26 | |
| parent | 69f248ccdd24b8e648c28ce8b1a0cf375ea51afb (diff) | |
| parent | 90c0dbe5f8df7ef6d8fd0ccc581fd64cc2ecd1ab (diff) | |
| download | docker-py-78600d52ab7dbfae2ce554f80c5d0fedd71a6f99.tar.gz | |
Merge branch 'mnottale-container-list-sparse'
| -rw-r--r-- | docker/models/containers.py | 35 | ||||
| -rw-r--r-- | tests/integration/models_containers_test.py | 22 |
2 files changed, 49 insertions, 8 deletions
diff --git a/docker/models/containers.py b/docker/models/containers.py index 895080c..1e06ed6 100644 --- a/docker/models/containers.py +++ b/docker/models/containers.py @@ -4,8 +4,10 @@ from collections import namedtuple from ..api import APIClient from ..constants import DEFAULT_DATA_CHUNK_SIZE -from ..errors import (ContainerError, ImageNotFound, - create_unexpected_kwargs_error) +from ..errors import ( + ContainerError, DockerException, ImageNotFound, + create_unexpected_kwargs_error +) from ..types import HostConfig from ..utils import version_gte from .images import Image @@ -27,7 +29,7 @@ class Container(Model): """ The image of the container. """ - image_id = self.attrs['Image'] + image_id = self.attrs.get('ImageID', self.attrs['Image']) if image_id is None: return None return self.client.images.get(image_id.split(':')[1]) @@ -37,15 +39,23 @@ class Container(Model): """ The labels of a container as dictionary. """ - result = self.attrs['Config'].get('Labels') - return result or {} + try: + result = self.attrs['Config'].get('Labels') + return result or {} + except KeyError: + raise DockerException( + 'Label data is not available for sparse objects. Call reload()' + ' to retrieve all information' + ) @property def status(self): """ The status of the container. For example, ``running``, or ``exited``. """ - return self.attrs['State']['Status'] + if isinstance(self.attrs['State'], dict): + return self.attrs['State']['Status'] + return self.attrs['State'] def attach(self, **kwargs): """ @@ -833,7 +843,8 @@ class ContainerCollection(Collection): resp = self.client.api.inspect_container(container_id) return self.prepare_model(resp) - def list(self, all=False, before=None, filters=None, limit=-1, since=None): + def list(self, all=False, before=None, filters=None, limit=-1, since=None, + sparse=False): """ List containers. Similar to the ``docker ps`` command. @@ -867,6 +878,11 @@ class ContainerCollection(Collection): `docker ps <https://docs.docker.com/engine/reference/commandline/ps>`_. + sparse (bool): Do not inspect containers. Returns partial + information, but guaranteed not to block. Use + :py:meth:`Container.reload` on resulting objects to retrieve + all attributes. Default: ``False`` + Returns: (list of :py:class:`Container`) @@ -877,7 +893,10 @@ class ContainerCollection(Collection): resp = self.client.api.containers(all=all, before=before, filters=filters, limit=limit, since=since) - return [self.get(r['Id']) for r in resp] + if sparse: + return [self.prepare_model(r) for r in resp] + else: + return [self.get(r['Id']) for r in resp] def prune(self, filters=None): return self.client.api.prune_containers(filters=filters) diff --git a/tests/integration/models_containers_test.py b/tests/integration/models_containers_test.py index fac4de2..38aae4d 100644 --- a/tests/integration/models_containers_test.py +++ b/tests/integration/models_containers_test.py @@ -159,6 +159,28 @@ class ContainerCollectionTest(BaseIntegrationTest): container = containers[0] assert container.attrs['Config']['Image'] == 'alpine' + assert container.status == 'running' + assert container.image == client.images.get('alpine') + + container.kill() + container.remove() + assert container_id not in [c.id for c in client.containers.list()] + + def test_list_sparse(self): + client = docker.from_env(version=TEST_API_VERSION) + container_id = client.containers.run( + "alpine", "sleep 300", detach=True).id + self.tmp_containers.append(container_id) + containers = [c for c in client.containers.list(sparse=True) if c.id == + container_id] + assert len(containers) == 1 + + container = containers[0] + assert container.attrs['Image'] == 'alpine' + assert container.status == 'running' + assert container.image == client.images.get('alpine') + with pytest.raises(docker.errors.DockerException): + container.labels container.kill() container.remove() |
