소스 검색

Fixes #2096: Only show multiple port clash warning if multiple containers
are about to be started.

Signed-off-by: Danyal Prout <[email protected]>

Danyal Prout 9 년 전
부모
커밋
339ebc0483
2개의 변경된 파일21개의 추가작업 그리고 1개의 파일을 삭제
  1. 1 1
      compose/service.py
  2. 20 0
      tests/unit/service_test.py

+ 1 - 1
compose/service.py

@@ -179,7 +179,7 @@ class Service(object):
                      'Remove the custom name to scale the service.'
                      % (self.name, self.custom_container_name))
 
-        if self.specifies_host_port():
+        if self.specifies_host_port() and desired_num > 1:
             log.warn('The "%s" service specifies a port on the host. If multiple containers '
                      'for this service are created on a single host, the port will clash.'
                      % self.name)

+ 20 - 0
tests/unit/service_test.py

@@ -642,6 +642,26 @@ class ServiceTest(unittest.TestCase):
         service = Service('foo', project='testing')
         assert service.image_name == 'testing_foo'
 
+    @mock.patch('compose.service.log', autospec=True)
+    def test_only_log_warning_when_host_ports_clash(self, mock_log):
+        self.mock_client.inspect_image.return_value = {'Id': 'abcd'}
+        name = 'foo'
+        service = Service(
+            name,
+            client=self.mock_client,
+            ports=["8080:80"])
+
+        service.scale(0)
+        self.assertFalse(mock_log.warn.called)
+
+        service.scale(1)
+        self.assertFalse(mock_log.warn.called)
+
+        service.scale(2)
+        mock_log.warn.assert_called_once_with(
+            'The "{}" service specifies a port on the host. If multiple containers '
+            'for this service are created on a single host, the port will clash.'.format(name))
+
 
 def sort_by_name(dictionary_list):
     return sorted(dictionary_list, key=lambda k: k['name'])