Skip to content

Commit aa9276a

Browse files
committed
Cover unexpected Docker build errors
1 parent 06a0cdb commit aa9276a

1 file changed

Lines changed: 60 additions & 1 deletion

File tree

tests/mock_vws/test_docker.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import uuid
55
from collections.abc import Iterable, Iterator
66
from http import HTTPStatus
7-
from typing import TYPE_CHECKING
7+
from pathlib import Path
8+
from typing import TYPE_CHECKING, cast
89

910
import docker
1011
import pytest
@@ -274,3 +275,61 @@ def test_build_and_run(
274275
matching_targets = cloud_reco_client.query(image=high_quality_image)
275276

276277
assert matching_targets[0].target_id == target_id
278+
279+
280+
def test_build_and_run_raises_full_log_for_unexpected_build_error(
281+
*,
282+
monkeypatch: pytest.MonkeyPatch,
283+
tmp_path: Path,
284+
) -> None:
285+
"""An unexpected Docker build error includes the full build log."""
286+
287+
class Images:
288+
"""Mock Docker images API."""
289+
290+
def build(self, **_: object) -> tuple[object, object]:
291+
"""Raise an unexpected Docker build error."""
292+
build_log = [
293+
{"stream": "first build log line"},
294+
{"stream": "second build log line"},
295+
{"aux": "ignored"},
296+
]
297+
raise BuildError(
298+
reason="unexpected build failure",
299+
build_log=iter(build_log),
300+
)
301+
302+
class Client:
303+
"""Mock Docker client."""
304+
305+
images = Images()
306+
307+
class Config:
308+
"""Mock pytest config."""
309+
310+
rootpath = tmp_path
311+
312+
class Request:
313+
"""Mock pytest request."""
314+
315+
config = Config()
316+
317+
class CustomBridgeNetwork:
318+
"""Mock Docker network."""
319+
320+
name = "custom-bridge-network"
321+
322+
monkeypatch.setattr(target=docker, name="from_env", value=Client)
323+
324+
with pytest.raises(
325+
expected_exception=AssertionError,
326+
match="first build log line\nsecond build log line",
327+
):
328+
test_build_and_run(
329+
high_quality_image=io.BytesIO(),
330+
custom_bridge_network=cast(
331+
"Network",
332+
CustomBridgeNetwork(),
333+
),
334+
request=cast("pytest.FixtureRequest", Request()),
335+
)

0 commit comments

Comments
 (0)