2019-12-19 16:19:41 +01:00
|
|
|
import glob
|
|
|
|
import json
|
2022-02-11 22:22:59 +01:00
|
|
|
import os
|
2019-12-19 16:19:41 +01:00
|
|
|
import pytest
|
|
|
|
import yaml
|
2022-02-11 22:22:59 +01:00
|
|
|
from jsonschema import validate, RefResolver, Draft4Validator
|
2019-12-19 17:08:51 +01:00
|
|
|
from jsonschema.exceptions import ValidationError
|
2019-12-19 16:19:41 +01:00
|
|
|
|
|
|
|
|
2022-02-11 22:22:59 +01:00
|
|
|
SCHEMAS = (
|
|
|
|
('device-types', 'devicetype.json'),
|
|
|
|
('module-types', 'moduletype.json'),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-12-19 16:19:41 +01:00
|
|
|
def _get_definition_files():
|
|
|
|
"""
|
2022-02-11 22:22:59 +01:00
|
|
|
Return a list of all definition files within the specified path.
|
2019-12-19 16:19:41 +01:00
|
|
|
"""
|
2022-02-11 22:22:59 +01:00
|
|
|
ret = []
|
|
|
|
|
|
|
|
for path, schema in SCHEMAS:
|
|
|
|
|
|
|
|
# Initialize the schema
|
|
|
|
with open(f"schema/{schema}") as schema_file:
|
|
|
|
schema = json.loads(schema_file.read())
|
2019-12-19 16:19:41 +01:00
|
|
|
|
2022-02-11 22:22:59 +01:00
|
|
|
# Validate that the schema exists
|
|
|
|
assert schema, f"Schema definition for {path} is empty!"
|
2019-12-19 16:19:41 +01:00
|
|
|
|
2022-02-11 22:22:59 +01:00
|
|
|
# Map each definition file to its schema
|
|
|
|
for f in glob.glob(f"{path}/*/*", recursive=True):
|
|
|
|
ret.append((f, schema))
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
definition_files = _get_definition_files()
|
2019-12-19 16:19:41 +01:00
|
|
|
|
|
|
|
|
2019-12-19 17:14:27 +01:00
|
|
|
def test_environment():
|
|
|
|
"""
|
|
|
|
Run basic sanity checks on the environment to ensure tests are running correctly.
|
|
|
|
"""
|
|
|
|
# Validate that definition files exist
|
2022-02-11 22:22:59 +01:00
|
|
|
assert definition_files, "No definition files found!"
|
2019-12-19 17:14:27 +01:00
|
|
|
|
|
|
|
|
2022-02-11 22:22:59 +01:00
|
|
|
@pytest.mark.parametrize(('file_path', 'schema'), definition_files)
|
|
|
|
def test_definitions(file_path, schema):
|
2019-12-19 16:19:41 +01:00
|
|
|
"""
|
2022-02-11 22:22:59 +01:00
|
|
|
Validate each definition file using the provided JSON schema.
|
2019-12-19 16:19:41 +01:00
|
|
|
"""
|
2019-12-26 21:36:28 +01:00
|
|
|
# Check file extension
|
|
|
|
assert file_path.split('.')[-1] in ('yaml', 'yml'), f"Invalid file extension: {file_path}"
|
|
|
|
|
2019-12-19 16:19:41 +01:00
|
|
|
# Read file
|
|
|
|
with open(file_path) as definition_file:
|
2019-12-30 16:11:25 +01:00
|
|
|
content = definition_file.read()
|
|
|
|
|
2022-02-11 22:22:59 +01:00
|
|
|
# Check for trailing newline
|
|
|
|
assert content.endswith('\n'), "Missing trailing newline"
|
2019-12-30 16:11:25 +01:00
|
|
|
|
2022-02-11 22:22:59 +01:00
|
|
|
# Load YAML data from file
|
|
|
|
definition = yaml.load(content, Loader=yaml.SafeLoader)
|
2019-12-19 16:19:41 +01:00
|
|
|
|
2019-12-19 17:08:51 +01:00
|
|
|
try:
|
2022-02-11 22:22:59 +01:00
|
|
|
resolver = RefResolver(f'file://{os.getcwd()}/schema/devicetype.json', schema)
|
|
|
|
Draft4Validator(schema, resolver=resolver).validate(definition)
|
2019-12-19 17:08:51 +01:00
|
|
|
except ValidationError as e:
|
2020-11-24 19:25:06 +01:00
|
|
|
pytest.fail(f"{file_path} failed validation: {e}", False)
|