Skip to content

Commit b623793

Browse files
minor improvements to test_images integration test
Avoid using external images in test_build and test_build_cache, check both build caching both enabled and disabled.
1 parent 63f91a7 commit b623793

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

podman/tests/integration/test_images.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import tarfile
2121
import types
2222
import unittest
23+
import random
2324

2425
import podman.tests.integration.base as base
2526
from podman import PodmanClient
@@ -139,24 +140,40 @@ def test_corrupt_load(self):
139140
self.assertIn("payload does not match", e.exception.explanation)
140141

141142
def test_build(self):
142-
buffer = io.StringIO("""FROM quay.io/libpod/alpine_labels:latest""")
143-
144-
image, stream = self.client.images.build(fileobj=buffer)
143+
buffer = io.StringIO("""FROM scratch""")
144+
image, _ = self.client.images.build(fileobj=buffer)
145145
self.assertIsNotNone(image)
146146
self.assertIsNotNone(image.id)
147147

148148
def test_build_cache(self):
149-
"""Check that building twice the same image uses caching"""
150-
buffer = io.StringIO("""FROM quay.io/libpod/alpine_labels:latest\nLABEL test=value""")
149+
"""Check build caching when enabled
150+
151+
Build twice with caching enabled (default), then again with nocache
152+
"""
153+
def look_for_cache(stream) -> bool:
154+
# Search for a line with contents "-> Using cache <image id>"
155+
uses_cache = False
156+
for line in stream:
157+
parsed = json.loads(line)['stream']
158+
if "Using cache" in parsed:
159+
uses_cache = True
160+
break
161+
return uses_cache
162+
163+
label = str(random.getrandbits(32))
164+
buffer = io.StringIO(f"""FROM scratch\nLABEL test={label}""")
151165
image, _ = self.client.images.build(fileobj=buffer)
152166
buffer.seek(0)
153-
_, stream = self.client.images.build(fileobj=buffer)
154-
for line in stream:
155-
# Search for a line with contents "-> Using cache <image id>"
156-
parsed = json.loads(line)['stream']
157-
if "Using cache" in parsed:
158-
break
159-
self.assertEqual(parsed.split()[3], image.id)
167+
cached_image, stream = self.client.images.build(fileobj=buffer)
168+
self.assertTrue(look_for_cache(stream))
169+
self.assertEqual(cached_image.id, image.id,
170+
msg="Building twice with cache does not produce the same image id")
171+
# Build again with disabled cache
172+
buffer.seek(0)
173+
uncached_image, stream = self.client.images.build(fileobj=buffer, nocache=True)
174+
self.assertFalse(look_for_cache(stream))
175+
self.assertNotEqual(uncached_image.id, image.id,
176+
msg="Building twice without cache produces the same image id")
160177

161178
def test_build_with_context(self):
162179
context = io.BytesIO()

0 commit comments

Comments
 (0)