Skip to content

Commit 3dd9b4f

Browse files
committed
feat: add platform parameter to image push method and corresponding test
1 parent db7f8b8 commit 3dd9b4f

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

docker/api/image.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ def pull(self, repository, tag=None, stream=False, auth_config=None,
434434
return self._result(response)
435435

436436
def push(self, repository, tag=None, stream=False, auth_config=None,
437-
decode=False):
437+
decode=False, platform=None):
438438
"""
439439
Push an image or a repository to the registry. Similar to the ``docker
440440
push`` command.
@@ -448,6 +448,7 @@ def push(self, repository, tag=None, stream=False, auth_config=None,
448448
``username`` and ``password`` keys to be valid.
449449
decode (bool): Decode the JSON data from the server into dicts.
450450
Only applies with ``stream=True``
451+
platform (str): JSON-encoded OCI platform to select the platform-variant to push. If not provided, all available variants will attempt to be pushed.
451452
452453
Returns:
453454
(generator or str): The output from the server.
@@ -488,6 +489,13 @@ def push(self, repository, tag=None, stream=False, auth_config=None,
488489
log.debug('Sending supplied auth config')
489490
headers['X-Registry-Auth'] = auth.encode_header(auth_config)
490491

492+
if platform is not None:
493+
if utils.version_lt(self._version, '1.46'):
494+
raise errors.InvalidVersion(
495+
'platform was only introduced in API version 1.46'
496+
)
497+
params['platform'] = platform
498+
491499
response = self._post_json(
492500
u, None, headers=headers, stream=stream, params=params
493501
)

tests/unit/api_image_test.py

+23
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import docker
66
from docker import auth
77

8+
from ..helpers import requires_api_version
89
from . import fake_api
910
from .api_test import (
1011
DEFAULT_TIMEOUT_SECONDS,
@@ -271,6 +272,28 @@ def test_push_image_with_auth(self):
271272
timeout=DEFAULT_TIMEOUT_SECONDS
272273
)
273274

275+
@requires_api_version('1.46')
276+
def test_push_image_with_platform(self):
277+
with mock.patch('docker.auth.resolve_authconfig',
278+
fake_resolve_authconfig):
279+
self.client.push(
280+
fake_api.FAKE_IMAGE_NAME,
281+
platform=fake_api.FAKE_PLATFORM
282+
)
283+
284+
fake_request.assert_called_with(
285+
'POST',
286+
f"{url_prefix}images/test_image/push",
287+
params={
288+
'tag': None,
289+
'platform': fake_api.FAKE_PLATFORM
290+
},
291+
data='{}',
292+
headers={'Content-Type': 'application/json'},
293+
stream=False,
294+
timeout=DEFAULT_TIMEOUT_SECONDS
295+
)
296+
274297
def test_push_image_stream(self):
275298
with mock.patch('docker.auth.resolve_authconfig',
276299
fake_resolve_authconfig):

tests/unit/fake_api.py

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
FAKE_SECRET_NAME = 'super_secret'
2222
FAKE_CONFIG_ID = 'sekvs771242jfdjnvfuds8232'
2323
FAKE_CONFIG_NAME = 'super_config'
24+
FAKE_PLATFORM = "{'os': 'linux','architecture': 'arm','variant': 'v5'}"
2425

2526
# Each method is prefixed with HTTP method (get, post...)
2627
# for clarity and readability

0 commit comments

Comments
 (0)