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'])