Skip to content

Commit b1d6e01

Browse files
committed
Rename cachefrom -> cache_from
Fix cache_from integration test Fix image ID detection in ImageCollection.build Signed-off-by: Joffrey F <[email protected]>
1 parent 40290fc commit b1d6e01

File tree

3 files changed

+41
-25
lines changed

3 files changed

+41
-25
lines changed

docker/api/build.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import json
12
import logging
23
import os
34
import re
4-
import json
55

6+
from .. import auth
67
from .. import constants
78
from .. import errors
8-
from .. import auth
99
from .. import utils
1010

1111

@@ -18,7 +18,7 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
1818
custom_context=False, encoding=None, pull=False,
1919
forcerm=False, dockerfile=None, container_limits=None,
2020
decode=False, buildargs=None, gzip=False, shmsize=None,
21-
labels=None, cachefrom=None):
21+
labels=None, cache_from=None):
2222
"""
2323
Similar to the ``docker build`` command. Either ``path`` or ``fileobj``
2424
needs to be set. ``path`` can be a local path (to a directory
@@ -92,7 +92,8 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
9292
shmsize (int): Size of `/dev/shm` in bytes. The size must be
9393
greater than 0. If omitted the system uses 64MB.
9494
labels (dict): A dictionary of labels to set on the image.
95-
cachefrom (list): A list of images used for build cache resolution.
95+
cache_from (list): A list of images used for build cache
96+
resolution.
9697
9798
Returns:
9899
A generator for the build output.
@@ -189,12 +190,12 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
189190
'labels was only introduced in API version 1.23'
190191
)
191192

192-
if cachefrom:
193+
if cache_from:
193194
if utils.version_gte(self._version, '1.25'):
194-
params.update({'cachefrom': json.dumps(cachefrom)})
195+
params.update({'cachefrom': json.dumps(cache_from)})
195196
else:
196197
raise errors.InvalidVersion(
197-
'cachefrom was only introduced in API version 1.25'
198+
'cache_from was only introduced in API version 1.25'
198199
)
199200

200201
if context is not None:

docker/models/images.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ def build(self, **kwargs):
141141
``"0-3"``, ``"0,1"``
142142
decode (bool): If set to ``True``, the returned stream will be
143143
decoded into dicts on the fly. Default ``False``.
144-
cachefrom (list): A list of images used for build cache resolution.
144+
cache_from (list): A list of images used for build cache
145+
resolution.
145146
146147
Returns:
147148
(:py:class:`Image`): The built image.
@@ -162,10 +163,10 @@ def build(self, **kwargs):
162163
return BuildError('Unknown')
163164
event = events[-1]
164165
if 'stream' in event:
165-
match = re.search(r'Successfully built ([0-9a-f]+)',
166+
match = re.search(r'(Successfully built |sha256:)([0-9a-f]+)',
166167
event.get('stream', ''))
167168
if match:
168-
image_id = match.group(1)
169+
image_id = match.group(2)
169170
return self.get(image_id)
170171

171172
raise BuildError(event.get('error') or event)

tests/integration/api_build_test.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
import shutil
44
import tempfile
55

6-
import pytest
7-
import six
8-
96
from docker import errors
107

11-
from ..helpers import requires_api_version
8+
import six
9+
1210
from .base import BaseAPIIntegrationTest
11+
from ..helpers import requires_api_version
1312

1413

1514
class BuildTest(BaseAPIIntegrationTest):
@@ -155,25 +154,40 @@ def test_build_labels(self):
155154
self.assertEqual(info['Config']['Labels'], labels)
156155

157156
@requires_api_version('1.25')
158-
@pytest.mark.xfail(reason='Bad test')
159-
def test_build_cachefrom(self):
157+
def test_build_with_cache_from(self):
160158
script = io.BytesIO('\n'.join([
161-
'FROM scratch',
162-
'CMD sh -c "echo \'Hello, World!\'"',
159+
'FROM busybox',
160+
'ENV FOO=bar',
161+
'RUN touch baz',
162+
'RUN touch bax',
163163
]).encode('ascii'))
164164

165-
cachefrom = ['build1']
165+
stream = self.client.build(fileobj=script, tag='build1')
166+
self.tmp_imgs.append('build1')
167+
for chunk in stream:
168+
pass
166169

167170
stream = self.client.build(
168-
fileobj=script, tag='cachefrom', cachefrom=cachefrom
171+
fileobj=script, tag='build2', cache_from=['build1'],
172+
decode=True
169173
)
170-
self.tmp_imgs.append('cachefrom')
174+
self.tmp_imgs.append('build2')
175+
counter = 0
171176
for chunk in stream:
172-
pass
177+
if 'Using cache' in chunk.get('stream', ''):
178+
counter += 1
179+
assert counter == 3
180+
self.client.remove_image('build2')
173181

174-
info = self.client.inspect_image('cachefrom')
175-
# FIXME: Config.CacheFrom is not a real thing
176-
self.assertEqual(info['Config']['CacheFrom'], cachefrom)
182+
counter = 0
183+
stream = self.client.build(
184+
fileobj=script, tag='build2', cache_from=['nosuchtag'],
185+
decode=True
186+
)
187+
for chunk in stream:
188+
if 'Using cache' in chunk.get('stream', ''):
189+
counter += 1
190+
assert counter == 0
177191

178192
def test_build_stderr_data(self):
179193
control_chars = ['\x1b[91m', '\x1b[0m']

0 commit comments

Comments
 (0)