Browse Source

Use split_port for ports format check

Rather than implement the logic a second time, use docker-py
split_port function to test if the ports is valid.

Signed-off-by: Mazz Mosley <[email protected]>
Mazz Mosley 10 years ago
parent
commit
df74b131ff
3 changed files with 12 additions and 29 deletions
  1. 4 9
      compose/config/schema.json
  2. 6 18
      compose/config/validation.py
  3. 2 2
      tests/unit/config_test.py

+ 4 - 9
compose/config/schema.json

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

+ 6 - 18
compose/config/validation.py

@@ -1,5 +1,6 @@
 import os
 
+from docker.utils.ports import split_port
 import json
 from jsonschema import Draft4Validator, FormatChecker, ValidationError
 
@@ -26,26 +27,13 @@ DOCKER_CONFIG_HINTS = {
 VALID_NAME_CHARS = '[a-zA-Z0-9\._\-]'
 
 
[email protected]_checks(format="ports", raises=ValidationError("Ports is incorrectly formatted."))
[email protected]_checks(format="ports", raises=ValidationError("Invalid port formatting, it should be '[[remote_ip:]remote_port:]port[/protocol]'"))
 def format_ports(instance):
-    def _is_valid(port):
-        if ':' in port or '/' in port:
-            return True
-        try:
-            int(port)
-            return True
-        except ValueError:
-            return False
+    try:
+        split_port(instance)
+    except ValueError:
         return False
-
-    if isinstance(instance, list):
-        for port in instance:
-            if not _is_valid(port):
-                return False
-        return True
-    elif isinstance(instance, str):
-        return _is_valid(instance)
-    return False
+    return True
 
 
 def get_unsupported_config_msg(service_name, error_key):

+ 2 - 2
tests/unit/config_test.py

@@ -76,7 +76,7 @@ class ConfigTest(unittest.TestCase):
 
     def test_config_invalid_ports_format_validation(self):
         with self.assertRaises(ConfigurationError):
-            for invalid_ports in [{"1": "8000"}, "whatport"]:
+            for invalid_ports in [{"1": "8000"}, "whatport", "625", "8000:8050"]:
                 config.load(
                     config.ConfigDetails(
                         {'web': {'image': 'busybox', 'ports': invalid_ports}},
@@ -86,7 +86,7 @@ class ConfigTest(unittest.TestCase):
                 )
 
     def test_config_valid_ports_format_validation(self):
-        valid_ports = [["8000", "9000"], "625", "8000:8050", ["8000/8050"]]
+        valid_ports = [["8000", "9000"], ["8000/8050"], ["8000"]]
         for ports in valid_ports:
             config.load(
                 config.ConfigDetails(