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
21 changes: 21 additions & 0 deletions tests/test_docker_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,27 @@ def test_stop_all_scoped_to_run_label(monkeypatch):
mine.stop.assert_called_once()


def test_stop_prunes_only_this_runs_containers(monkeypatch):
# Other projects share the Docker host: never prune their stopped containers.
monkeypatch.setattr(docker_helper.time, "sleep", lambda s: None)
helper = _helper()
helper.server.containers.list.return_value = []
helper.stop()
helper.server.containers.prune.assert_called_once_with(
filters={"label": "ssgberk.run=abc12345-0000"})


def test_clean_removes_only_ssgberk_test_images():
helper = _helper()
ours = types.SimpleNamespace(id="i1", tags=["ssgberk/test.hugo:latest"])
theirs = types.SimpleNamespace(id="i2", tags=["postgres:16"])
untagged = types.SimpleNamespace(id="i3", tags=[])
helper.server.images.list.return_value = [ours, theirs, untagged]
helper.clean()
helper.server.images.remove.assert_called_once_with("i1", force=True)
helper.server.images.prune.assert_not_called()


def test_stop_all_tolerates_not_found(monkeypatch):
monkeypatch.setattr(docker_helper.time, "sleep", lambda s: None)
helper = _helper()
Expand Down
10 changes: 6 additions & 4 deletions toolset/utils/docker_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ def __build(self, base_url, path, build_log_file, log_prefix, dockerfile,

def clean(self):
'''
Cleans all the docker images from the system
Removes the ssgberk/test.* generator images. Other images (and the dangling
images of other projects on a shared Docker host) are left alone; removing a
tagged image already deletes its unused layers.
'''

self.server.images.prune()
for image in self.server.images.list():
if len(image.tags) > 0:
if DockerHelper.is_ssgberk_test_image(image.tags[0]):
self.server.images.remove(image.id, force=True)
self.server.images.prune()

def build(self, test, build_log_dir=os.devnull):
'''
Expand Down Expand Up @@ -315,7 +315,9 @@ def stop(self, containers=None):
else:
DockerHelper.__stop_all(self.server, self.benchmarker.config.run_id)

self.server.containers.prune()
# Only this run's stopped containers: the Docker host is shared with other projects.
self.server.containers.prune(
filters={"label": "%s=%s" % (RUN_LABEL, self.benchmarker.config.run_id)})

def server_container_exists(self, container_id_or_name):
'''
Expand Down
Loading