fix: sort mount volumes by specificity to prevent parent mounts hiding children
When git repo mounts and regular file mounts have overlapping target paths, broader parent mounts hide deeper child mounts because Docker Compose applies volumes in array order. - Add sort_volumes_by_specificity() to docker.py: - Sorts by target path depth (parent paths first, child paths last) - Logs warnings for duplicate targets - Handles :bind and :ro suffixes correctly - Integrate into manifest flow (compile_compose): - Sorts manifest mounts + EXTRA_VOLUMES before writing compose - Integrate into legacy flow (_modify_compose_file): - Sorts after appending extra_volumes to existing template volumes - Add 6 unit tests covering parent/child ordering, stable sort, type suffixes, empty list, single volume, and duplicate warnings. Quality gates: pytest (214 passed, 6 pre-existing), tsc --noEmit (clean)
This commit is contained in:
@@ -2,7 +2,13 @@
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from src.services.docker import get_container_id, get_container_name
|
||||
import logging
|
||||
|
||||
from src.services.docker import (
|
||||
get_container_id,
|
||||
get_container_name,
|
||||
sort_volumes_by_specificity,
|
||||
)
|
||||
|
||||
|
||||
class TestGetContainerId:
|
||||
@@ -50,3 +56,57 @@ class TestGetContainerName:
|
||||
result = get_container_name("missing")
|
||||
|
||||
assert result is None
|
||||
|
||||
|
||||
class TestSortVolumesBySpecificity:
|
||||
"""Tests for sort_volumes_by_specificity."""
|
||||
|
||||
def test_parent_before_child(self) -> None:
|
||||
"""A repo mount to /workspace/x should come before a file mount to /workspace/x/y/config.json."""
|
||||
volumes = [
|
||||
"/repo/x/y/config.json:/workspace/x/y/config.json",
|
||||
"/repo/x:/workspace/x",
|
||||
]
|
||||
result = sort_volumes_by_specificity(volumes)
|
||||
assert result[0] == "/repo/x:/workspace/x"
|
||||
assert result[1] == "/repo/x/y/config.json:/workspace/x/y/config.json"
|
||||
|
||||
def test_stable_sort_for_equal_depth(self) -> None:
|
||||
"""Mounts at the same depth preserve input order."""
|
||||
volumes = [
|
||||
"/a:/workspace/a",
|
||||
"/b:/workspace/b",
|
||||
"/c:/workspace/c",
|
||||
]
|
||||
result = sort_volumes_by_specificity(volumes)
|
||||
assert result == volumes
|
||||
|
||||
def test_with_type_suffix(self) -> None:
|
||||
"""Volume strings with :bind or :ro suffixes are parsed correctly."""
|
||||
volumes = [
|
||||
"/repo/x/y/config.json:/workspace/x/y/config.json:bind",
|
||||
"/repo/x:/workspace/x:bind",
|
||||
]
|
||||
result = sort_volumes_by_specificity(volumes)
|
||||
assert result[0] == "/repo/x:/workspace/x:bind"
|
||||
assert result[1] == "/repo/x/y/config.json:/workspace/x/y/config.json:bind"
|
||||
|
||||
def test_empty_list(self) -> None:
|
||||
"""Empty list returns empty list."""
|
||||
assert sort_volumes_by_specificity([]) == []
|
||||
|
||||
def test_single_volume(self) -> None:
|
||||
"""Single volume returns unchanged."""
|
||||
volumes = ["/repo:/workspace"]
|
||||
assert sort_volumes_by_specificity(volumes) == volumes
|
||||
|
||||
def test_duplicate_target_warning(self, caplog) -> None:
|
||||
"""Duplicate targets trigger a warning."""
|
||||
with caplog.at_level(logging.WARNING, logger="src.services.docker"):
|
||||
volumes = [
|
||||
"/a:/workspace/x",
|
||||
"/b:/workspace/x",
|
||||
]
|
||||
sort_volumes_by_specificity(volumes)
|
||||
assert "Duplicate mount targets detected" in caplog.text
|
||||
assert "/workspace/x" in caplog.text
|
||||
|
||||
Reference in New Issue
Block a user