UTI: Export results
[csit.git] / resources / libraries / python / model / validate.py
1 # Copyright (c) 2021 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """Module for validating JSON instances against schemas.
15
16 Short module currently, as we validate only testcase info outputs.
17 Structure will probably change when we start validation mode file types.
18 """
19
20 import json
21 import jsonschema
22
23
24 def _get_validator(schema_path):
25     """Contruct validator with format checking enabled.
26
27     Load json schema from disk.
28     Perform validation against meta-schema before returning.
29
30     :param schema_path: Local filesystem path to .json file storing the schema.
31     :type schema_path: str
32     :returns: Instantiated validator class instance.
33     :rtype: jsonschema.validators.Validator
34     :raises RuntimeError: If the schema is not valid according its meta-schema.
35     """
36     with open(schema_path, u"rt", encoding="utf-8") as file_in:
37         schema = json.load(file_in)
38     validator_class = jsonschema.validators.validator_for(schema)
39     validator_class.check_schema(schema)
40     fmt_checker = jsonschema.FormatChecker()
41     validator = validator_class(schema, format_checker=fmt_checker)
42     return validator
43
44
45 def get_validators():
46     """Return mapping from file types to validator instances.
47
48     Uses hardcoded file types and paths to schemas on disk.
49
50     :returns: Validators, currently just for tc_info_output.
51     :rtype: Mapping[str, jsonschema.validators.Validator]
52     :raises RuntimeError: If schemas are not readable or not valid.
53     """
54     relative_path = u"docs/model/current/schema/test_case.info.schema.json"
55     # Robot is always started when CWD is CSIT_DIR.
56     validator = _get_validator(relative_path)
57     return dict(tc_info=validator)
58
59
60 def validate(file_path, validator):
61     """Load data from disk, use validator to validate it.
62
63     :param file_path: Local filesystem path including the file name to load.
64     :param validator: Validator instance to use for validation.
65     :type file_path: str
66     :type validator: jsonschema.validators.Validator
67     :raises RuntimeError: If schema validation fails.
68     """
69     with open(file_path, u"rt", encoding="utf-8") as file_in:
70         instance = json.load(file_in)
71     error = jsonschema.exceptions.best_match(validator.iter_errors(instance))
72     if error is not None:
73         raise error