From de2f58d818565dd3de28dee269dfc17c367306ef Mon Sep 17 00:00:00 2001 From: Matt Bogosian Date: Sun, 17 May 2015 09:25:38 -0700 Subject: [PATCH] Fix #602. Raise ValueError on empty argument to inspect_{container,image}() methods. --- docker/client.py | 2 ++ docker/utils/decorators.py | 2 ++ tests/test.py | 12 ++++++++++++ 3 files changed, 16 insertions(+) diff --git a/docker/client.py b/docker/client.py index 085dff41..63f80383 100644 --- a/docker/client.py +++ b/docker/client.py @@ -747,6 +747,8 @@ class Client(requests.Session): @check_resource def inspect_image(self, image): + if isinstance(image, dict): + image = image.get('Id') return self._result( self._get(self._url("/images/{0}/json".format(image))), True diff --git a/docker/utils/decorators.py b/docker/utils/decorators.py index 1ed0b6a0..aa4f0c3c 100644 --- a/docker/utils/decorators.py +++ b/docker/utils/decorators.py @@ -12,5 +12,7 @@ def check_resource(f): raise errors.NullResource( 'image or container param is undefined' ) + if not resource_id: + raise ValueError('image or container param is empty') return f(self, resource_id, *args, **kwargs) return wrapped diff --git a/tests/test.py b/tests/test.py index 9b90017c..98bb03ff 100644 --- a/tests/test.py +++ b/tests/test.py @@ -1782,6 +1782,12 @@ class DockerClientTest(Cleanup, base.BaseTestCase): except Exception as e: self.fail('Command should not raise exception: {0}'.format(e)) + try: + self.client.inspect_container('') + except ValueError as e: + self.assertEqual(e.args[0], + 'image or container param is empty') + fake_request.assert_called_with( url_prefix + 'containers/3cc2351ab11b/json', timeout=DEFAULT_TIMEOUT_SECONDS @@ -1953,6 +1959,12 @@ class DockerClientTest(Cleanup, base.BaseTestCase): except Exception as e: self.fail('Command should not raise exception: {0}'.format(e)) + try: + self.client.inspect_image('') + except ValueError as e: + self.assertEqual(e.args[0], + 'image or container param is empty') + fake_request.assert_called_with( url_prefix + 'images/test_image/json', timeout=DEFAULT_TIMEOUT_SECONDS