|
20 | 20 | import tarfile |
21 | 21 | import types |
22 | 22 | import unittest |
| 23 | +import random |
23 | 24 |
|
24 | 25 | import podman.tests.integration.base as base |
25 | 26 | from podman import PodmanClient |
@@ -139,24 +140,40 @@ def test_corrupt_load(self): |
139 | 140 | self.assertIn("payload does not match", e.exception.explanation) |
140 | 141 |
|
141 | 142 | 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) |
145 | 145 | self.assertIsNotNone(image) |
146 | 146 | self.assertIsNotNone(image.id) |
147 | 147 |
|
148 | 148 | 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}""") |
151 | 165 | image, _ = self.client.images.build(fileobj=buffer) |
152 | 166 | 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") |
160 | 177 |
|
161 | 178 | def test_build_with_context(self): |
162 | 179 | context = io.BytesIO() |
|
0 commit comments