test: remove anonymous volumes with short-lived test containers

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Tobias KaupatandClaude Sonnet 5.5 committed 2026-10-19 16:13:44 +02:00
1 parent a74b210615
commit 8ff93f4709
3 files changed
+29 -8

No files matched your search

+18
View File
@@ -135,3 +135,21 @@ def start_container(
def remove_quietly(container: Container) -> None:
with contextlib.suppress(docker.errors.NotFound):
container.raw.remove(force=True, v=True)
def run_once(client: docker.DockerClient, image: str, **kwargs: Any) -> str:
"""Run a short-lived container and return its output.
`containers.run(remove=True)` keeps the image's anonymous volumes (VOLUME /data),
so run detached and remove the container together with its volumes.
"""
container = client.containers.run(image, detach=True, **kwargs)
try:
result = container.wait()
output = container.logs(stdout=True, stderr=False).decode()
if result["StatusCode"] != 0:
raise CommandError(f"container exited {result['StatusCode']}:\n{output}")
return output
finally:
with contextlib.suppress(docker.errors.NotFound):
container.remove(force=True, v=True)