|
8 | 8 | from typing import IO, List, Optional
|
9 | 9 |
|
10 | 10 | from docker import errors
|
| 11 | +from docker.errors import BuildError |
11 | 12 | from docker.models.containers import Container
|
12 | 13 | from docker.models.images import Image
|
13 | 14 | from docker.models.volumes import Volume
|
@@ -282,3 +283,48 @@ def wait_and_start():
|
282 | 283 | finally:
|
283 | 284 | ctr.stop()
|
284 | 285 | ctr.remove(force=True)
|
| 286 | + |
| 287 | + def test_build_pull_true(self): |
| 288 | + dockerfile = ( |
| 289 | + b"FROM quay.io/libpod/alpine:latest\n" |
| 290 | + ) |
| 291 | + img: Image |
| 292 | + img, logs = self.docker.images.build(fileobj=io.BytesIO(dockerfile), quiet=False, pull=True) |
| 293 | + has_tried_pull = False |
| 294 | + for e in logs: |
| 295 | + if "stream" in e and "trying to pull" in e["stream"].lower(): |
| 296 | + has_tried_pull = True |
| 297 | + self.assertTrue(has_tried_pull, "the build process has not tried to pull the base image") |
| 298 | + |
| 299 | + def test_build_pull_one(self): |
| 300 | + dockerfile = ( |
| 301 | + b"FROM quay.io/libpod/alpine:latest\n" |
| 302 | + ) |
| 303 | + img: Image |
| 304 | + img, logs = self.docker.images.build(fileobj=io.BytesIO(dockerfile), quiet=False, pull=1) |
| 305 | + has_tried_pull = False |
| 306 | + for e in logs: |
| 307 | + if "stream" in e and "trying to pull" in e["stream"].lower(): |
| 308 | + has_tried_pull = True |
| 309 | + self.assertTrue(has_tried_pull, "the build process has not tried to pull the base image") |
| 310 | + |
| 311 | + def test_build_pull_false(self): |
| 312 | + dockerfile = ( |
| 313 | + b"FROM quay.io/libpod/alpine:latest\n" |
| 314 | + ) |
| 315 | + img, logs = self.docker.images.build(fileobj=io.BytesIO(dockerfile), quiet=False, pull=False) |
| 316 | + has_tried_pull = False |
| 317 | + for e in logs: |
| 318 | + if "stream" in e and "trying to pull" in e["stream"].lower(): |
| 319 | + has_tried_pull = True |
| 320 | + self.assertFalse(has_tried_pull, "the build process has tried tried to pull the base image") |
| 321 | + |
| 322 | + def test_build_pull_never(self): |
| 323 | + try: |
| 324 | + dockerfile = ( |
| 325 | + b"FROM quay.io/libpod/does-not-exist:latest\n" |
| 326 | + ) |
| 327 | + _, _ = self.docker.images.build(fileobj=io.BytesIO(dockerfile), quiet=False, pull="never") |
| 328 | + self.fail("this line should not have been reached") |
| 329 | + except BuildError as e: |
| 330 | + self.assertTrue("image not known" in e.msg, "the exception should have been caused by missing base image") |
0 commit comments