diff --git a/apps_validation/portals.py b/apps_validation/portals.py new file mode 100644 index 0000000..4331b08 --- /dev/null +++ b/apps_validation/portals.py @@ -0,0 +1,51 @@ +from jsonschema import validate as json_schema_validate, ValidationError as JsonValidationError + +from apps_exceptions import ValidationErrors + + +IX_NOTES_KEY = 'x-notes' +IX_PORTAL_KEY = 'x-portals' +VALIDATION_SCHEMA = { + '$schema': 'https://json-schema.org/draft/2020-12/schema', + 'type': 'object', + 'properties': { + 'x-portals': { + 'type': 'array', + 'items': { + 'type': 'object', + 'properties': { + 'name': { + 'type': 'string', + 'minLength': 1, + }, + 'path': { + 'type': 'string', + 'pattern': '^/.*' + }, + 'scheme': { + 'type': 'string', + 'enum': ['http', 'https'], + }, + 'host': {'type': 'string'}, + 'port': {'type': 'integer'}, + }, + 'required': ['name', 'scheme', 'host', 'port'], + 'additionalProperties': False + }, + }, + 'x-notes': { + 'type': ['string', 'null'], + } + }, +} + + +def validate_portals_and_notes(schema, data): + verrors = ValidationErrors() + + try: + json_schema_validate(data, VALIDATION_SCHEMA) + except JsonValidationError as e: + verrors.add(schema, f'Failed to validate rendered portal config: {e}') + + verrors.check() diff --git a/apps_validation/validate_templates.py b/apps_validation/validate_templates.py index 622163e..850a938 100644 --- a/apps_validation/validate_templates.py +++ b/apps_validation/validate_templates.py @@ -8,6 +8,7 @@ from catalog_templating.render import render_templates from .names import get_test_values_dir_path, get_test_values_from_test_dir +from .portals import validate_portals_and_notes RE_APP_VERSION = re.compile(r'^v\d+_\d+_\d+$') @@ -56,6 +57,7 @@ def validate_template_rendering(app_path: str, schema: str, verrors: ValidationE verrors.check() + rendered_config = {} for test_values_file in test_values_files: try: rendered = render_templates( @@ -69,13 +71,19 @@ def validate_template_rendering(app_path: str, schema: str, verrors: ValidationE else: for file_name, rendered_template in rendered.items(): try: - yaml.safe_load(rendered_template) + rendered_config.update(yaml.safe_load(rendered_template)) except yaml.YAMLError as e: verrors.add( f'{schema}.{file_name}', f'Failed to verify rendered template is a valid yaml file using {test_values_file!r}: {e}' ) + if rendered_config: + try: + validate_portals_and_notes(schema, rendered_config) + except ValidationErrors as ve: + verrors.extend(ve) + def validate_library(app_path: str, schema: str, verrors: ValidationErrors) -> None: library_dir = pathlib.Path(os.path.join(app_path, 'templates/library'))