فهرست منبع

Fix extra warnings on masked volumes.

Signed-off-by: Daniel Nephin <[email protected]>
Daniel Nephin 10 سال پیش
والد
کامیت
e1308a8329
2فایلهای تغییر یافته به همراه39 افزوده شده و 1 حذف شده
  1. 4 1
      compose/service.py
  2. 35 0
      tests/unit/service_test.py

+ 4 - 1
compose/service.py

@@ -963,7 +963,10 @@ def warn_on_masked_volume(volumes_option, container_volumes, service):
         for volume in container_volumes)
 
     for volume in volumes_option:
-        if container_volumes.get(volume.internal) != volume.external:
+        if (
+            volume.internal in container_volumes and
+            container_volumes.get(volume.internal) != volume.external
+        ):
             log.warn((
                 "Service \"{service}\" is using volume \"{volume}\" from the "
                 "previous container. Host mapping \"{host_path}\" has no effect. "

+ 35 - 0
tests/unit/service_test.py

@@ -26,6 +26,8 @@ from compose.service import parse_volume_spec
 from compose.service import Service
 from compose.service import ServiceNet
 from compose.service import VolumeFromSpec
+from compose.service import VolumeSpec
+from compose.service import warn_on_masked_volume
 
 
 class ServiceTest(unittest.TestCase):
@@ -750,6 +752,39 @@ class ServiceVolumesTest(unittest.TestCase):
             ['/mnt/sda1/host/path:/data:rw'],
         )
 
+    def test_warn_on_masked_volume_no_warning_when_no_container_volumes(self):
+        volumes_option = [VolumeSpec('/home/user', '/path', 'rw')]
+        container_volumes = []
+        service = 'service_name'
+
+        with mock.patch('compose.service.log') as mock_log:
+            warn_on_masked_volume(volumes_option, container_volumes, service)
+
+        assert not mock_log.warn.called
+
+    def test_warn_on_masked_volume_when_masked(self):
+        volumes_option = [VolumeSpec('/home/user', '/path', 'rw')]
+        container_volumes = [
+            VolumeSpec('/var/lib/docker/path', '/path', 'rw'),
+            VolumeSpec('/var/lib/docker/path', '/other', 'rw'),
+        ]
+        service = 'service_name'
+
+        with mock.patch('compose.service.log') as mock_log:
+            warn_on_masked_volume(volumes_option, container_volumes, service)
+
+        mock_log.warn.called_once_with(mock.ANY)
+
+    def test_warn_on_masked_no_warning_with_same_path(self):
+        volumes_option = [VolumeSpec('/home/user', '/path', 'rw')]
+        container_volumes = [VolumeSpec('/home/user', '/path', 'rw')]
+        service = 'service_name'
+
+        with mock.patch('compose.service.log') as mock_log:
+            warn_on_masked_volume(volumes_option, container_volumes, service)
+
+        assert not mock_log.warn.called
+
     def test_create_with_special_volume_mode(self):
         self.mock_client.inspect_image.return_value = {'Id': 'imageid'}