From c382f036c35634bf2e0f9721880e3f6501248cc3 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Mon, 15 Apr 2024 11:01:30 +0100 Subject: [PATCH] tests: Ensure API schemas are valid Validate the validation by validating our schemas against the JSON Schema meta schema. This is an important first step in getting us of JSON Schema Draft 4 and onto Draft 2019-09, which OpenAPI is a superset of. Change-Id: I3b5a05aa0aa058e92c6927c9e3bee3cdd4477f8f Signed-off-by: Stephen Finucane --- .../unit/api/openstack/compute/test_schemas.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/nova/tests/unit/api/openstack/compute/test_schemas.py b/nova/tests/unit/api/openstack/compute/test_schemas.py index 7cf34c4451..a3d59a55ec 100644 --- a/nova/tests/unit/api/openstack/compute/test_schemas.py +++ b/nova/tests/unit/api/openstack/compute/test_schemas.py @@ -10,7 +10,10 @@ # License for the specific language governing permissions and limitations # under the License. +import jsonschema.exceptions + from nova.api.openstack import compute +from nova.api.validation import validators from nova import test @@ -19,15 +22,22 @@ class SchemaTest(test.NoDBTestCase): def setUp(self): super().setUp() self.router = compute.APIRouterV21() + self.meta_schema = validators._SchemaValidator.validator_org def test_schemas(self): missing_schemas = set() + invalid_schemas = set() def _validate_func(func, method): if method in ("POST", "PUT", "PATCH"): # request body validation if not hasattr(func, '_request_schema'): missing_schemas.add(func.__qualname__) + else: + try: + self.meta_schema.check_schema(func._request_schema) + except jsonschema.exceptions.SchemaError: + invalid_schemas.add(func.__qualname__) for route in self.router.map.matchlist: if 'controller' not in route.defaults: @@ -104,3 +114,9 @@ class SchemaTest(test.NoDBTestCase): f"Found API resources without schemas: " f"{sorted(missing_schemas)}" ) + + if invalid_schemas: + raise test.TestingException( + f"Found API resources with invalid schemas: " + f"{sorted(invalid_schemas)}" + )