Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/agent_env/env/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,12 @@ def _image(label: str, artifact_id: str, description: str, dockerfile: Path, ima
def _build_and_put(label: str, artifact_id: str, description: str, dockerfile: Path, image_name: str, *,
platform: str | None, say: Callable[[str], None]) -> DockerImageArtifact:
"""Build ``dockerfile``'s image for ``platform`` from the folder it's in, and write it with that folder as its build
context, which a sandbox that can't run its tar.gz builds it from."""
context, which a sandbox that can't run its tar.gz builds it from. The build holds this machine's lock for its tag,
as a host image's does: a run with a state root of its own builds the same tag with the same Docker, and two builds
of one tag at once fail with "image ... already exists"."""
say(f"Building {label} image from {dockerfile}...")
build_image(dockerfile, dockerfile.parent, image_name, platform=platform)
with holding_locks([f"image-build:{image_name}"]):
build_image(dockerfile, dockerfile.parent, image_name, platform=platform)
say(f"Creating {label} DockerImageArtifact...")
artifact = DockerImageArtifact.put(id=artifact_id, description=description, image_name=image_name,
build_context_path=str(dockerfile.parent), dockerfile_path=str(dockerfile))
Expand Down
28 changes: 28 additions & 0 deletions tst/unit/env/bootstrap_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
stores only, once each however many runs ask; pulled from the images published for their sources when every one of a
kind's is, else built for this host's platform. One built here switches to them once they're published."""

import contextlib
import re
from pathlib import Path

Expand Down Expand Up @@ -69,6 +70,33 @@ def keep(id, *, description, image_name, build_context_path=None, dockerfile_pat
assert kept == [(dockerfile.parent, str(dockerfile)) for dockerfile in bootstrap._DOCKERFILES[kind]]


@_INFRA_PUTS
def test_each_infra_image_is_built_holding_this_machines_lock_for_its_tag(local_stores, monkeypatch, kind, put):
"""Every agent-env process here builds with the one Docker, a run with a state root of its own too, as each test
worker's is, and two builds of one tag at once fail with "image ... already exists"."""
held, built = [], []

@contextlib.contextmanager
def holding(ids, **kwargs):
held.extend(ids)
try:
yield
finally:
del held[-len(ids):]

def keep(id, *, description, image_name, build_context_path=None, dockerfile_path=None, platform=None):
return get_artifact_store().put_document(DockerImageArtifact(
id=id, description=description, image_name=image_name, tar_gz_s3_url="file:///x.tar.gz"))

monkeypatch.setattr(bootstrap, "holding_locks", holding)
monkeypatch.setattr(bootstrap, "build_image", lambda dockerfile, context, tag, **kwargs: built.append((tag, [*held])))
monkeypatch.setattr(bootstrap.DockerImageArtifact, "put", keep)

put("x", platform=None)

assert built and all(f"image-build:{tag}" in locks for tag, locks in built), built


@pytest.fixture
def published(monkeypatch):
"""The kinds whose images the registry serves, all under ``_DIGEST``: those added to the set returned."""
Expand Down
Loading