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 <stephenfin@redhat.com>
This commit is contained in:
parent
7e78623ac5
commit
c382f036c3
@ -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)}"
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user