Repository navigation
feat(run-docker-container): run a docker_image artifact without building #574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| """``agent-env run`` on a bundle whose task starts its container from an image folder, ``artifacts/<name>/``, named as | ||
| ``image_artifact_id``: the bundle writes the folder as its build context, the first run builds the image once where it | ||
| starts the container, as an agent's image is built where it's installed, a rerun builds nothing, and the grader in the | ||
| container writes the reward its score is. Needs a Docker daemon.""" | ||
|
|
||
| import json | ||
| import logging | ||
|
|
||
| import pytest | ||
| from click.testing import CliRunner | ||
|
|
||
| from agent_env.artifact.store import reset_artifact_store | ||
| from agent_env.cli import cli | ||
| from agent_env.config import configure, reset_config | ||
| from agent_env.providers.sandbox_providers import host_images | ||
|
|
||
| pytestmark = [pytest.mark.integration, pytest.mark.int_test_slow] | ||
|
|
||
| TASK = json.dumps([ | ||
| {"id": "box", "type": "deploy_sandbox", "sandbox_name": "box", "sandbox_mode": "vm", "sandbox_type": "local"}, | ||
| {"id": "task", "type": "run_docker_container", "sandbox_name": "box", "container_name": "task", | ||
| "image_artifact_id": "task-image", "command_override": "sleep infinity", "depends_on": ["box"]}, | ||
| {"id": "grade", "type": "run_container_unit_tests_verifier", "sandbox_name": "box", "container_name": "task", | ||
| "verifier_id": "tests", "reward_path": "/logs/verifier/reward.txt", "depends_on": ["task"], | ||
| "command": "mkdir -p /logs/verifier && grep -qx built /built && echo 1 > /logs/verifier/reward.txt"}, | ||
| {"id": "down", "type": "teardown_sandboxes", "sandbox_names": ["box"], "depends_on": ["grade"]}, | ||
| ]) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def state(monkeypatch, tmp_path): | ||
| """Local stores under this test's folder. Whatever ran, no sandbox work folder may be left behind.""" | ||
| sandboxes = tmp_path / "sandboxes" | ||
| monkeypatch.chdir(tmp_path) | ||
| monkeypatch.setenv("XDG_STATE_HOME", str(tmp_path / "state")) | ||
| monkeypatch.setenv("AGENT_ENV_DOCUMENT_STORE", "local") | ||
| monkeypatch.setenv("AGENT_ENV_OBJECT_STORE", "local") | ||
| monkeypatch.setenv("AGENT_ENV_LOCAL_SANDBOX_DIR", str(sandboxes)) | ||
| configure() | ||
| reset_artifact_store() | ||
| logging.disable(logging.CRITICAL) # pytest's live logging would take CliRunner's stdout | ||
| try: | ||
| yield tmp_path | ||
| assert not sandboxes.exists() or not any(sandboxes.iterdir()), "a run left a sandbox work folder" | ||
| finally: | ||
| logging.disable(logging.NOTSET) | ||
| reset_artifact_store() | ||
| reset_config() | ||
|
|
||
|
|
||
| def test_a_task_container_from_an_image_folder_is_built_by_the_first_run_alone(state, monkeypatch): | ||
| root = state / "bundle" | ||
| (root / "artifacts/task-image").mkdir(parents=True) | ||
| (root / "artifacts/task-image/Dockerfile").write_text( | ||
| "FROM mirror.gcr.io/library/debian:bookworm-slim\nRUN echo built > /built\n") | ||
| (root / "tasks").mkdir() | ||
| (root / "tasks/grade.json").write_text(TASK) | ||
| builds, build = [], host_images._build | ||
| monkeypatch.setattr(host_images, "_build", lambda *args: builds.append(args[2]) or build(*args)) | ||
|
|
||
| first = CliRunner().invoke(cli, ["run", str(root)]) | ||
|
|
||
| assert first.exit_code == 0 and "tasks/grade.json v1: passed" in first.output, first.output | ||
| assert len(builds) == 1, (first.output, builds) | ||
|
|
||
| again = CliRunner().invoke(cli, ["run", str(root)]) | ||
|
|
||
| assert again.exit_code == 0 and "tasks/grade.json v1: passed" in again.output, again.output | ||
| assert "building with docker" not in again.output and len(builds) == 1, (again.output, builds) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test leaves its Docker image behind. Its temporary bundle gets a new image name on each test run, and sandbox teardown deliberately keeps that image. Add a
finallyblock that removes it after the reuse assertions, including when an assertion fails, so repeated test runs do not fill Docker storage.Prompt To Fix With AI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. This PR is merged, so the cleanup (removing the image in a
finally) goes into the next PR.