浏览代码

Allow integer value for ports

While it was intended as a positive to be stricter in validation
it would in fact break backwards compatibility, which we do not
want to be doing.

Consider re-visiting this later and include a deprecation warning if
we want to be stricter.

Signed-off-by: Mazz Mosley <[email protected]>
Mazz Mosley 10 年之前
父节点
当前提交
b4872de213
共有 3 个文件被更改,包括 27 次插入7 次删除
  1. 16 4
      compose/config/schema.json
  2. 7 0
      compose/config/validation.py
  3. 4 3
      tests/unit/config_test.py

+ 16 - 4
compose/config/schema.json

@@ -75,10 +75,22 @@
         "pid": {"type": "string"},
 
         "ports": {
-          "type": "array",
-          "items": {"type": "string"},
-          "uniqueItems": true,
-          "format": "ports"
+          "oneOf": [
+            {
+              "type": "array",
+              "items": {"type": "string"},
+              "uniqueItems": true,
+              "format": "ports"
+            },
+            {
+              "type": "string",
+              "format": "ports"
+            },
+            {
+              "type": "number",
+              "format": "ports"
+            }
+          ]
         },
 
         "privileged": {"type": "string"},

+ 7 - 0
compose/config/validation.py

@@ -84,6 +84,13 @@ def process_errors(errors):
                     required.append("Service '{}' has neither an image nor a build path specified. Exactly one must be provided.".format(service_name))
                 else:
                     required.append(error.message)
+            elif error.validator == 'oneOf':
+                config_key = error.path[1]
+                valid_types = [context.validator_value for context in error.context]
+                valid_type_msg = " or ".join(valid_types)
+                type_errors.append("Service '{}' configuration key '{}' contains an invalid type, it should be either {}".format(
+                    service_name, config_key, valid_type_msg)
+                )
             elif error.validator == 'type':
                 msg = "a"
                 if error.validator_value == "array":

+ 4 - 3
tests/unit/config_test.py

@@ -75,8 +75,9 @@ class ConfigTest(unittest.TestCase):
             )
 
     def test_config_invalid_ports_format_validation(self):
-        with self.assertRaises(ConfigurationError):
-            for invalid_ports in [{"1": "8000"}, "whatport", "625", "8000:8050"]:
+        expected_error_msg = "Service 'web' configuration key 'ports' contains an invalid type"
+        with self.assertRaisesRegexp(ConfigurationError, expected_error_msg):
+            for invalid_ports in [{"1": "8000"}, False, 0]:
                 config.load(
                     config.ConfigDetails(
                         {'web': {'image': 'busybox', 'ports': invalid_ports}},
@@ -86,7 +87,7 @@ class ConfigTest(unittest.TestCase):
                 )
 
     def test_config_valid_ports_format_validation(self):
-        valid_ports = [["8000", "9000"], ["8000/8050"], ["8000"]]
+        valid_ports = [["8000", "9000"], ["8000/8050"], ["8000"], "8000", 8000]
         for ports in valid_ports:
             config.load(
                 config.ConfigDetails(