From 7c2ad79c30f67656daae206a5caaa828b445f33f Mon Sep 17 00:00:00 2001 From: ghanshyam Date: Tue, 9 Jun 2015 17:58:40 +0900 Subject: [PATCH] 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 --- tempest_lib/common/rest_client.py | 5 ++++- tempest_lib/tests/test_rest_client.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) 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'])