1
0
Эх сурвалжийг харах

Merge pull request #3299 from dannyprout/2096-only-show-port-clash-warning-when-starting-multiple-containers

Only show port clash warning if multiple containers are about to be started
Aanand Prasad 9 жил өмнө
parent
commit
e2cb7b0237

+ 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'])