Repository navigation
fix(store): one local registry container per port, never removed #43
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,95 @@ | ||
| """LocalRegistryImageStore's lazy registry bring-up, with docker and the /v2/ ping faked.""" | ||
|
|
||
| import subprocess | ||
|
|
||
| import pytest | ||
|
|
||
| from agent_env.store.image_store import local_registry_image_store | ||
| from agent_env.store.image_store.local_registry_image_store import LocalRegistryImageStore | ||
|
|
||
|
|
||
| class _Docker: | ||
| """Answers the docker commands bring-up issues and records each one.""" | ||
|
|
||
| def __init__(self, *, run_rc=0, existing_port=None, start_rc=0): | ||
| self.run_rc, self.existing_port, self.start_rc = run_rc, existing_port, start_rc | ||
| self.calls: list[list[str]] = [] | ||
|
|
||
| def __call__(self, argv, **kwargs): | ||
| self.calls.append(argv) | ||
| command = argv[1] | ||
| if command == "run": | ||
| return subprocess.CompletedProcess(argv, self.run_rc, "", "Conflict" if self.run_rc else "") | ||
| if command == "inspect": | ||
| found = self.existing_port is not None | ||
| return subprocess.CompletedProcess(argv, 0 if found else 1, str(self.existing_port or ""), "") | ||
| if command == "start": | ||
| return subprocess.CompletedProcess(argv, self.start_rc, "", "boom" if self.start_rc else "") | ||
| raise AssertionError(f"unexpected docker call: {argv}") | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def docker(monkeypatch): | ||
| def install(**kwargs): | ||
| fake = _Docker(**kwargs) | ||
| monkeypatch.setattr(local_registry_image_store.subprocess, "run", fake) | ||
| return fake | ||
| return install | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def listening(monkeypatch): | ||
| """The registry answers only after bring-up has run a docker command.""" | ||
| def install(fake): | ||
| monkeypatch.setattr(LocalRegistryImageStore, "_registry_listening", staticmethod(lambda port: bool(fake.calls))) | ||
| monkeypatch.setattr(local_registry_image_store.time, "sleep", lambda seconds: None) | ||
| return install | ||
|
|
||
|
|
||
| def test_the_default_port_runs_agentenv_registry(docker, listening): | ||
| fake = docker() | ||
| listening(fake) | ||
| LocalRegistryImageStore("localhost:5000").ensure_repository("r") | ||
| assert fake.calls == [["docker", "run", "-d", "--restart", "unless-stopped", "--name", "agentenv-registry", | ||
| "-p", "127.0.0.1:5000:5000", "registry:2"]] | ||
|
|
||
|
|
||
| def test_another_port_runs_its_own_container(docker, listening): | ||
| fake = docker() | ||
| listening(fake) | ||
| LocalRegistryImageStore("localhost:5001").ensure_repository("r") | ||
| assert fake.calls == [["docker", "run", "-d", "--restart", "unless-stopped", "--name", "agentenv-registry-5001", | ||
| "-p", "127.0.0.1:5001:5000", "registry:2"]] | ||
|
|
||
|
|
||
| def test_a_serving_registry_is_left_alone(docker, monkeypatch): | ||
| fake = docker() | ||
| monkeypatch.setattr(LocalRegistryImageStore, "_registry_listening", staticmethod(lambda port: True)) | ||
| LocalRegistryImageStore("localhost:5000").ensure_repository("r") | ||
| assert fake.calls == [] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("port, name", [(5000, "agentenv-registry"), (5001, "agentenv-registry-5001")]) | ||
| def test_a_stopped_container_on_this_port_is_started(docker, listening, port, name): | ||
| fake = docker(run_rc=125, existing_port=port) | ||
| listening(fake) | ||
| LocalRegistryImageStore(f"localhost:{port}").ensure_repository("r") | ||
| assert [call[1] for call in fake.calls] == ["run", "inspect", "start"] | ||
| assert fake.calls[1][-1] == name | ||
| assert fake.calls[-1] == ["docker", "start", name] | ||
|
|
||
|
|
||
| def test_an_older_agentenv_registry_on_another_port_is_refused_not_removed(docker, monkeypatch): | ||
| fake = docker(run_rc=125, existing_port=5001) | ||
| monkeypatch.setattr(LocalRegistryImageStore, "_registry_listening", staticmethod(lambda port: False)) | ||
| with pytest.raises(RuntimeError, match="agentenv-registry is using port 5001, not 5000"): | ||
| LocalRegistryImageStore("localhost:5000").ensure_repository("r") | ||
| assert [call[1] for call in fake.calls] == ["run", "inspect"] | ||
|
|
||
|
|
||
| def test_a_failed_run_with_no_container_surfaces_dockers_error(docker, monkeypatch): | ||
| fake = docker(run_rc=125) | ||
| monkeypatch.setattr(LocalRegistryImageStore, "_registry_listening", staticmethod(lambda port: False)) | ||
| with pytest.raises(RuntimeError, match="could not start the local registry: Conflict"): | ||
| LocalRegistryImageStore("localhost:5000").ensure_repository("r") | ||
| assert all(call[1] != "rm" for call in fake.calls) |
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.
Uh oh!
There was an error while loading. Please reload this page.