浏览代码

Fix build section without context.

Signed-off-by: Daniel Nephin <[email protected]>
Daniel Nephin 9 年之前
父节点
当前提交
532dffd688
共有 4 个文件被更改,包括 22 次插入4 次删除
  1. 3 0
      compose/config/config.py
  2. 6 1
      compose/config/service_schema_v2.0.json
  3. 2 3
      compose/config/validation.py
  4. 11 0
      tests/unit/config/config_test.py

+ 3 - 0
compose/config/config.py

@@ -874,6 +874,9 @@ def validate_paths(service_dict):
             build_path = build
             build_path = build
         elif isinstance(build, dict) and 'context' in build:
         elif isinstance(build, dict) and 'context' in build:
             build_path = build['context']
             build_path = build['context']
+        else:
+            # We have a build section but no context, so nothing to validate
+            return
 
 
         if (
         if (
             not is_url(build_path) and
             not is_url(build_path) and

+ 6 - 1
compose/config/service_schema_v2.0.json

@@ -196,7 +196,12 @@
       "anyOf": [
       "anyOf": [
           {"required": ["build"]},
           {"required": ["build"]},
           {"required": ["image"]}
           {"required": ["image"]}
-      ]
+      ],
+      "properties": {
+        "build": {
+          "required": ["context"]
+        }
+      }
     }
     }
   }
   }
 }
 }

+ 2 - 3
compose/config/validation.py

@@ -253,10 +253,9 @@ def handle_generic_service_error(error, path):
         msg_format = "{path} contains an invalid type, it should be {msg}"
         msg_format = "{path} contains an invalid type, it should be {msg}"
         error_msg = _parse_valid_types_from_validator(error.validator_value)
         error_msg = _parse_valid_types_from_validator(error.validator_value)
 
 
-    # TODO: no test case for this branch, there are no config options
-    # which exercise this branch
     elif error.validator == 'required':
     elif error.validator == 'required':
-        msg_format = "{path} is invalid, {msg}"
+        error_msg = ", ".join(error.validator_value)
+        msg_format = "{path} is invalid, {msg} is required."
 
 
     elif error.validator == 'dependencies':
     elif error.validator == 'dependencies':
         config_key = list(error.validator_value.keys())[0]
         config_key = list(error.validator_value.keys())[0]

+ 11 - 0
tests/unit/config/config_test.py

@@ -1169,6 +1169,17 @@ class ConfigTest(unittest.TestCase):
             config.load(config_details)
             config.load(config_details)
         assert "Service 'one' depends on service 'three'" in exc.exconly()
         assert "Service 'one' depends on service 'three'" in exc.exconly()
 
 
+    def test_load_dockerfile_without_context(self):
+        config_details = build_config_details({
+            'version': '2',
+            'services': {
+                'one': {'build': {'dockerfile': 'Dockerfile.foo'}},
+            },
+        })
+        with pytest.raises(ConfigurationError) as exc:
+            config.load(config_details)
+        assert 'one.build is invalid, context is required.' in exc.exconly()
+
 
 
 class NetworkModeTest(unittest.TestCase):
 class NetworkModeTest(unittest.TestCase):
     def test_network_mode_standard(self):
     def test_network_mode_standard(self):