summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorghanshyam <ghanshyam.mann@nectechnologies.in>2015-06-09 17:58:40 +0900
committerghanshyam <ghanshyam.mann@nectechnologies.in>2015-06-09 17:58:40 +0900
commit7c2ad79c30f67656daae206a5caaa828b445f33f (patch)
tree753527d4c5a613d5455c466f8cd7fdc76c8bccaf
parentf46b6c99e05c2a4dba23f071edc2b1dad23e42dc (diff)
downloadtempest-lib-7c2ad79c30f67656daae206a5caaa828b445f33f.tar.gz
Explicitly add json schema validator for schema validation
JSON Schema use validator (draft3 or draft4) to validate the given schema against given instance. By default it use draft4 validator which is current one. If new validator will be released in future and that become default then, there might be some compatibility issues between draft4 and new version. For example from draft3 to draft4, 'ip-version' format has been changed to 'ipv4'. This commits explicitly pass the validator while doing the schema validation. Also adds unit tests to check the current version of validator. If any new validator will be available in future, then we can use that by doing proper modification in schema files if needed. Change-Id: Ie11e3ba60926f247a82670b7da37d7890d16280c
-rw-r--r--tempest_lib/common/rest_client.py5
-rw-r--r--tempest_lib/tests/test_rest_client.py22
2 files changed, 26 insertions, 1 deletions
diff --git a/tempest_lib/common/rest_client.py b/tempest_lib/common/rest_client.py
index 2206ca1..062cc8e 100644
--- a/tempest_lib/common/rest_client.py
+++ b/tempest_lib/common/rest_client.py
@@ -34,7 +34,8 @@ MAX_RECURSION_DEPTH = 2
# All the successful HTTP status codes from RFC 7231 & 4918
HTTP_SUCCESS = (200, 201, 202, 203, 204, 205, 206, 207)
-# JSON Schema format checker used for JSON Schema validation
+# JSON Schema validator and format checker used for JSON Schema validation
+JSONSCHEMA_VALIDATOR = jsonschema.Draft4Validator
FORMAT_CHECKER = jsonschema.draft4_format_checker
@@ -809,6 +810,7 @@ class RestClient(object):
if body_schema:
try:
jsonschema.validate(body, body_schema,
+ cls=JSONSCHEMA_VALIDATOR,
format_checker=FORMAT_CHECKER)
except jsonschema.ValidationError as ex:
msg = ("HTTP response body is invalid (%s)") % ex
@@ -823,6 +825,7 @@ class RestClient(object):
if header_schema:
try:
jsonschema.validate(resp, header_schema,
+ cls=JSONSCHEMA_VALIDATOR,
format_checker=FORMAT_CHECKER)
except jsonschema.ValidationError as ex:
msg = ("HTTP response header is invalid (%s)") % ex
diff --git a/tempest_lib/tests/test_rest_client.py b/tempest_lib/tests/test_rest_client.py
index 21b0eda..5deee3f 100644
--- a/tempest_lib/tests/test_rest_client.py
+++ b/tempest_lib/tests/test_rest_client.py
@@ -15,6 +15,7 @@
import json
import httplib2
+import jsonschema
from oslotest import mockpatch
import six
@@ -1027,3 +1028,24 @@ class TestRestClientJSONSchemaFormatValidation(TestJSONSchemaValidationBase):
}
body = {'foo': 'example@example.com'}
self._test_validate_pass(schema, body)
+
+
+class TestRestClientJSONSchemaValidatorVersion(TestJSONSchemaValidationBase):
+
+ schema = {
+ 'status_code': [200],
+ 'response_body': {
+ 'type': 'object',
+ 'properties': {
+ 'foo': {'type': 'string'}
+ }
+ }
+ }
+
+ def test_current_json_schema_validator_version(self):
+ with mockpatch.PatchObject(jsonschema.Draft4Validator,
+ "check_schema") as chk_schema:
+ body = {'foo': 'test'}
+ self._test_validate_pass(self.schema, body)
+ chk_schema.mock.assert_called_once_with(
+ self.schema['response_body'])