Skip to content

Commit 53e9792

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. Signed-off-by: Federico Rizzo <[email protected]>
1 parent 289f769 commit 53e9792

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
@@ -145,24 +146,40 @@ def test_corrupt_load(self):
145146
self.assertIn("payload does not match", e.exception.explanation)
146147

147148
def test_build(self):
148-
buffer = io.StringIO("""FROM quay.io/libpod/alpine_labels:latest""")
149-
150-
image, stream = self.client.images.build(fileobj=buffer)
149+
buffer = io.StringIO("""FROM scratch""")
150+
image, _ = self.client.images.build(fileobj=buffer)
151151
self.assertIsNotNone(image)
152152
self.assertIsNotNone(image.id)
153153

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

167184
def test_build_with_manifest(self):
168185
buffer = io.StringIO("""FROM quay.io/libpod/alpine_labels:latest""")

0 commit comments

Comments
 (0)