fix(model): Fail on invalid schema 82/42682/2
authorPeter Mikus <[email protected]>
Mon, 7 Apr 2025 09:45:06 +0000 (11:45 +0200)
committerPeter Mikus <[email protected]>
Wed, 9 Apr 2025 07:09:47 +0000 (07:09 +0000)
Signed-off-by: Peter Mikus <[email protected]>
Change-Id: Id8ff76ae51aaa7233da01b06b8c25dccc537724f

resources/libraries/python/model/ExportJson.py
resources/libraries/python/model/validate.py

index 3f923d6..3658dc4 100644 (file)
@@ -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."""
index 85c4b99..d3e1bdf 100644 (file)
@@ -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