diff options
author | Aanand Prasad <aanand.prasad@gmail.com> | 2016-07-12 16:38:06 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-12 16:38:06 -0400 |
commit | 1f34b4896ad336d1a9b51029a0b837f904ba485c (patch) | |
tree | 69367e33409f2005709a7c585ff5ad8ffe5c79d1 | |
parent | 5e47026a9beefaccbb845df4ce00ae8a684f0de6 (diff) | |
parent | e8ea79dfdb7b722801113131bfe90e88c141dc09 (diff) | |
download | docker-py-1f34b4896ad336d1a9b51029a0b837f904ba485c.tar.gz |
Merge pull request #1119 from Mobelux/fix-build-with-auth
Pass X-Registry-Auth when building an image
-rw-r--r-- | docker/api/build.py | 6 | ||||
-rw-r--r-- | tests/unit/build_test.py | 61 |
2 files changed, 63 insertions, 4 deletions
diff --git a/docker/api/build.py b/docker/api/build.py index 971a50e..7403716 100644 --- a/docker/api/build.py +++ b/docker/api/build.py @@ -18,7 +18,8 @@ class BuildApiMixin(object): custom_context=False, encoding=None, pull=False, forcerm=False, dockerfile=None, container_limits=None, decode=False, buildargs=None, gzip=False): - remote = context = headers = None + remote = context = None + headers = {} container_limits = container_limits or {} if path is None and fileobj is None: raise TypeError("Either path or fileobj needs to be provided.") @@ -134,8 +135,7 @@ class BuildApiMixin(object): ', '.join(repr(k) for k in self._auth_configs.keys()) ) ) - if headers is None: - headers = {} + if utils.compare_version('1.19', self._version) >= 0: headers['X-Registry-Config'] = auth.encode_header( self._auth_configs diff --git a/tests/unit/build_test.py b/tests/unit/build_test.py index 414153e..b2705eb 100644 --- a/tests/unit/build_test.py +++ b/tests/unit/build_test.py @@ -2,8 +2,9 @@ import gzip import io import docker +from docker import auth -from .api_test import DockerClientTest +from .api_test import DockerClientTest, fake_request, url_prefix class BuildTest(DockerClientTest): @@ -83,8 +84,25 @@ class BuildTest(DockerClientTest): } } + expected_params = {'t': None, 'q': False, 'dockerfile': None, + 'rm': False, 'nocache': False, 'pull': False, + 'forcerm': False, + 'remote': 'https://github.com/docker-library/mongo'} + expected_headers = { + 'X-Registry-Config': auth.encode_header(self.client._auth_configs)} + self.client.build(path='https://github.com/docker-library/mongo') + fake_request.assert_called_with( + 'POST', + url_prefix + 'build', + stream=True, + data=None, + headers=expected_headers, + params=expected_params, + timeout=None + ) + def test_build_container_with_named_dockerfile(self): self.client.build('.', dockerfile='nameddockerfile') @@ -103,3 +121,44 @@ class BuildTest(DockerClientTest): 'foo': 'bar' }) ) + + def test_set_auth_headers_with_empty_dict_and_auth_configs(self): + self.client._auth_configs = { + 'https://example.com': { + 'user': 'example', + 'password': 'example', + 'email': 'example@example.com' + } + } + + headers = {} + expected_headers = { + 'X-Registry-Config': auth.encode_header(self.client._auth_configs)} + self.client._set_auth_headers(headers) + self.assertEqual(headers, expected_headers) + + def test_set_auth_headers_with_dict_and_auth_configs(self): + self.client._auth_configs = { + 'https://example.com': { + 'user': 'example', + 'password': 'example', + 'email': 'example@example.com' + } + } + + headers = {'foo': 'bar'} + expected_headers = { + 'foo': 'bar', + 'X-Registry-Config': auth.encode_header(self.client._auth_configs)} + + self.client._set_auth_headers(headers) + self.assertEqual(headers, expected_headers) + + def test_set_auth_headers_with_dict_and_no_auth_configs(self): + headers = {'foo': 'bar'} + expected_headers = { + 'foo': 'bar' + } + + self.client._set_auth_headers(headers) + self.assertEqual(headers, expected_headers) |