From: Peter Mikus Date: Mon, 7 Apr 2025 09:45:06 +0000 (+0200) Subject: fix(model): Fail on invalid schema X-Git-Url: https://gerrit.fd.io/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F82%2F42682%2F2;p=csit.git fix(model): Fail on invalid schema Signed-off-by: Peter Mikus Change-Id: Id8ff76ae51aaa7233da01b06b8c25dccc537724f --- diff --git a/resources/libraries/python/model/ExportJson.py b/resources/libraries/python/model/ExportJson.py index 3f923d6d0e..3658dc46f9 100644 --- a/resources/libraries/python/model/ExportJson.py +++ b/resources/libraries/python/model/ExportJson.py @@ -86,7 +86,7 @@ class ExportJson(): return test_type def export_pending_data(self): - """Write the accumulated data to disk. + """Write the data to disk, raise error if invalid. Create missing directories. Reset both file path and data to avoid writing multiple times. @@ -97,23 +97,27 @@ class ExportJson(): If no file path is set, do not write anything, as that is the failsafe behavior when caller from unexpected place. - Aso do not write anything when EXPORT_JSON constant is false. + Also do not write anything when EXPORT_JSON constant is false. - Regardless of whether data was written, it is cleared. + :raises: ValidationError if data export does not conform to schema. """ + error = None if not Constants.EXPORT_JSON or not self.file_path: self.data = None self.file_path = None return new_file_path = write_output(self.file_path, self.data) - # Data is going to be cleared (as a sign that export succeeded), - # so this is the last chance to detect if it was for a test case. - is_testcase = "result" in self.data + if "result" in self.data: + error = validate(new_file_path, self.validators["tc_info"]) + if error: + # Mark as failed and re-export. + self.data["passed"] = False + self.data["message"] = str(error) + write_output(self.file_path, self.data) self.data = None - # Validation for output goes here when ready. self.file_path = None - if is_testcase: - validate(new_file_path, self.validators["tc_info"]) + if error: + raise error def warn_on_bad_export(self): """If bad state is detected, log a warning and clean up state.""" diff --git a/resources/libraries/python/model/validate.py b/resources/libraries/python/model/validate.py index 85c4b993c9..d3e1bdf706 100644 --- a/resources/libraries/python/model/validate.py +++ b/resources/libraries/python/model/validate.py @@ -1,4 +1,4 @@ -# Copyright (c) 2023 Cisco and/or its affiliates. +# Copyright (c) 2025 Cisco and/or its affiliates. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at: @@ -52,11 +52,12 @@ def validate(file_path, validator): :param validator: Validator instance to use for validation. :type file_path: str :type validator: jsonschema.validators.Validator - :raises ValidationError: If schema validation fails. + :returns: None if valid, error if invalid. + :rtype: Optional[ValidationError] """ with open(file_path, "rt", encoding="utf-8") as file_in: instance = json.load(file_in) error = jsonschema.exceptions.best_match(validator.iter_errors(instance)) if error is not None: print(json.dumps(instance, indent=4)) - raise error + return error