Merge "Add validation for capsule template"
This commit is contained in:
commit
9f98a16566
@ -21,9 +21,11 @@ from zun.api.controllers import base
|
||||
from zun.api.controllers import link
|
||||
from zun.api.controllers.v1 import collection
|
||||
from zun.api.controllers.v1.schemas import capsules as schema
|
||||
from zun.api.controllers.v1.schemas import parameter_types
|
||||
from zun.api.controllers.v1.views import capsules_view as view
|
||||
from zun.api import utils as api_utils
|
||||
from zun.api import validation
|
||||
from zun.api.validation import validators
|
||||
from zun.common import consts
|
||||
from zun.common import exception
|
||||
from zun.common.i18n import _
|
||||
@ -62,6 +64,10 @@ def check_capsule_template(tpl):
|
||||
except Exception as e:
|
||||
raise exception.FailedParseStringToJson(e)
|
||||
|
||||
validator = validators.SchemaValidator(
|
||||
parameter_types.capsule_template)
|
||||
validator.validate(tpl_json)
|
||||
|
||||
kind_field = tpl_json.get('kind')
|
||||
if kind_field not in ['capsule', 'Capsule']:
|
||||
raise exception.InvalidCapsuleTemplate("kind fields need to be "
|
||||
|
@ -27,21 +27,22 @@ class TestCheckCapsuleTemplate(api_base.FunctionalTest):
|
||||
with self.assertRaisesRegex(
|
||||
exception.InvalidCapsuleTemplate, "kind fields need to "
|
||||
"be set as capsule or Capsule"):
|
||||
params = ({"kind": "test", "spec": {"containers": []}})
|
||||
params = ({"kind": "test", "metadata": {},
|
||||
"spec": {"containers": []}})
|
||||
capsules.check_capsule_template(params)
|
||||
|
||||
with self.assertRaisesRegex(
|
||||
exception.InvalidCapsuleTemplate, "No Spec found"):
|
||||
params = ({"kind": "capsule"})
|
||||
params = ({"kind": "capsule", "metadata": {}})
|
||||
capsules.check_capsule_template(params)
|
||||
|
||||
with self.assertRaisesRegex(
|
||||
exception.InvalidCapsuleTemplate,
|
||||
"No valid containers field"):
|
||||
params = ({"kind": "capsule", "spec": {}})
|
||||
params = ({"kind": "capsule", "metadata": {}, "spec": {}})
|
||||
capsules.check_capsule_template(params)
|
||||
|
||||
params = ({"kind": "capsule", "spec": {
|
||||
params = ({"kind": "capsule", "metadata": {}, "spec": {
|
||||
"containers": [{"image": "test1"}], "restartPolicy": "Always",
|
||||
}})
|
||||
spec_content, tpl_json = capsules.check_capsule_template(params)
|
||||
@ -49,23 +50,25 @@ class TestCheckCapsuleTemplate(api_base.FunctionalTest):
|
||||
|
||||
def test_check_capsule_template_unicode(self):
|
||||
with self.assertRaisesRegex(
|
||||
exception.InvalidCapsuleTemplate, "kind fields need to "
|
||||
"be set as capsule or Capsule"):
|
||||
params = (u'{"kind": "test", "spec": {"containers": []}}')
|
||||
exception.SchemaValidationError,
|
||||
"Invalid input for field 'kind'"):
|
||||
params = (u'{"kind": "test", "metadata": {}, '
|
||||
'"spec": {"containers": []}}')
|
||||
capsules.check_capsule_template(params)
|
||||
|
||||
with self.assertRaisesRegex(
|
||||
exception.InvalidCapsuleTemplate, "No Spec found"):
|
||||
params = (u'{"kind": "capsule"}')
|
||||
exception.SchemaValidationError,
|
||||
"'spec' is a required property"):
|
||||
params = (u'{"kind": "capsule", "metadata": {}}')
|
||||
capsules.check_capsule_template(params)
|
||||
|
||||
with self.assertRaisesRegex(
|
||||
exception.InvalidCapsuleTemplate,
|
||||
"No valid containers field"):
|
||||
params = (u'{"kind": "capsule", "spec": {}}')
|
||||
exception.SchemaValidationError,
|
||||
"Invalid input for field 'spec'"):
|
||||
params = (u'{"kind": "capsule", "spec": {}, "metadata": {}}')
|
||||
capsules.check_capsule_template(params)
|
||||
|
||||
params = (u'{"kind": "capsule", "spec": {'
|
||||
params = (u'{"kind": "capsule", "metadata": {}, "spec": {'
|
||||
u'"containers": [{"image": "test1"}],'
|
||||
u'"restartPolicy": "Always"}}')
|
||||
spec_content, tpl_json = capsules.check_capsule_template(params)
|
||||
|
Loading…
x
Reference in New Issue
Block a user