Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NAS-130172 / 24.10 / Validate specified portals and notes #35

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions apps_validation/portals.py
Original file line number Diff line number Diff line change
@@ -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'],
Qubad786 marked this conversation as resolved.
Show resolved Hide resolved
},
'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()
10 changes: 9 additions & 1 deletion apps_validation/validate_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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+$')
Expand Down Expand Up @@ -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(
Expand All @@ -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'))
Expand Down
Loading