Skip to content

Commit

Permalink
Merge pull request #35 from truenas/mrehan/fix-portals
Browse files Browse the repository at this point in the history
NAS-130172 / 24.10 / Validate specified portals and notes
  • Loading branch information
Qubad786 authored Jul 24, 2024
2 parents 38d9b8a + 7f9547d commit 378e3e7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
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'],
},
'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

0 comments on commit 378e3e7

Please sign in to comment.