Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | """ |
| 4 | A tool for validating entity manager configurations. |
| 5 | """ |
| 6 | import argparse |
| 7 | import json |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 8 | import os |
| 9 | import sys |
| 10 | |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 11 | import jsonschema.validators |
| 12 | |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 13 | DEFAULT_SCHEMA_FILENAME = "global.json" |
| 14 | |
| 15 | |
| 16 | def main(): |
| 17 | parser = argparse.ArgumentParser( |
| 18 | description="Entity manager configuration validator", |
| 19 | ) |
| 20 | parser.add_argument( |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 21 | "-s", |
| 22 | "--schema", |
| 23 | help=( |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 24 | "Use the specified schema file instead of the default " |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 25 | "(__file__/../../schemas/global.json)" |
| 26 | ), |
| 27 | ) |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 28 | parser.add_argument( |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 29 | "-c", |
| 30 | "--config", |
| 31 | action="append", |
| 32 | help=( |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 33 | "Validate the specified configuration files (can be " |
| 34 | "specified more than once) instead of the default " |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 35 | "(__file__/../../configurations/**.json)" |
| 36 | ), |
| 37 | ) |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 38 | parser.add_argument( |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 39 | "-e", |
| 40 | "--expected-fails", |
| 41 | help=( |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 42 | "A file with a list of configurations to ignore should " |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 43 | "they fail to validate" |
| 44 | ), |
| 45 | ) |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 46 | parser.add_argument( |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 47 | "-k", |
| 48 | "--continue", |
| 49 | action="store_true", |
| 50 | help="keep validating after a failure", |
| 51 | ) |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 52 | parser.add_argument( |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 53 | "-v", "--verbose", action="store_true", help="be noisy" |
| 54 | ) |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 55 | args = parser.parse_args() |
| 56 | |
| 57 | schema_file = args.schema |
| 58 | if schema_file is None: |
| 59 | try: |
| 60 | source_dir = os.path.realpath(__file__).split(os.sep)[:-2] |
| 61 | schema_file = os.sep + os.path.join( |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 62 | *source_dir, "schemas", DEFAULT_SCHEMA_FILENAME |
| 63 | ) |
Patrick Williams | cad2d1f | 2022-12-04 14:38:16 -0600 | [diff] [blame] | 64 | except Exception: |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 65 | sys.stderr.write( |
| 66 | "Could not guess location of {}\n".format( |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 67 | DEFAULT_SCHEMA_FILENAME |
| 68 | ) |
| 69 | ) |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 70 | sys.exit(2) |
| 71 | |
| 72 | schema = {} |
| 73 | try: |
| 74 | with open(schema_file) as fd: |
| 75 | schema = json.load(fd) |
Patrick Williams | cad2d1f | 2022-12-04 14:38:16 -0600 | [diff] [blame] | 76 | except FileNotFoundError: |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 77 | sys.stderr.write( |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 78 | "Could not read schema file '{}'\n".format(schema_file) |
| 79 | ) |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 80 | sys.exit(2) |
| 81 | |
| 82 | config_files = args.config or [] |
| 83 | if len(config_files) == 0: |
| 84 | try: |
| 85 | source_dir = os.path.realpath(__file__).split(os.sep)[:-2] |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 86 | configs_dir = os.sep + os.path.join(*source_dir, "configurations") |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 87 | data = os.walk(configs_dir) |
| 88 | for root, _, files in data: |
| 89 | for f in files: |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 90 | if f.endswith(".json"): |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 91 | config_files.append(os.path.join(root, f)) |
Patrick Williams | cad2d1f | 2022-12-04 14:38:16 -0600 | [diff] [blame] | 92 | except Exception: |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 93 | sys.stderr.write("Could not guess location of configurations\n") |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 94 | sys.exit(2) |
| 95 | |
| 96 | configs = [] |
| 97 | for config_file in config_files: |
| 98 | try: |
| 99 | with open(config_file) as fd: |
| 100 | configs.append(json.load(fd)) |
Patrick Williams | cad2d1f | 2022-12-04 14:38:16 -0600 | [diff] [blame] | 101 | except FileNotFoundError: |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 102 | sys.stderr.write( |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 103 | "Could not parse config file '{}'\n".format(config_file) |
| 104 | ) |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 105 | sys.exit(2) |
| 106 | |
| 107 | expected_fails = [] |
| 108 | if args.expected_fails: |
| 109 | try: |
| 110 | with open(args.expected_fails) as fd: |
| 111 | for line in fd: |
| 112 | expected_fails.append(line.strip()) |
Patrick Williams | cad2d1f | 2022-12-04 14:38:16 -0600 | [diff] [blame] | 113 | except Exception: |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 114 | sys.stderr.write( |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 115 | "Could not read expected fails file '{}'\n".format( |
| 116 | args.expected_fails |
| 117 | ) |
| 118 | ) |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 119 | sys.exit(2) |
| 120 | |
| 121 | base_uri = "file://{}/".format( |
Patrick Williams | fa8ee87 | 2022-12-07 07:00:42 -0600 | [diff] [blame] | 122 | os.path.split(os.path.realpath(schema_file))[0] |
| 123 | ) |
Brad Bishop | c04b3f4 | 2020-05-01 08:17:59 -0400 | [diff] [blame] | 124 | resolver = jsonschema.RefResolver(base_uri, schema) |
| 125 | validator = jsonschema.Draft7Validator(schema, resolver=resolver) |
| 126 | |
| 127 | results = { |
| 128 | "invalid": [], |
| 129 | "unexpected_pass": [], |
| 130 | } |
| 131 | for config_file, config in zip(config_files, configs): |
| 132 | name = os.path.split(config_file)[1] |
| 133 | expect_fail = name in expected_fails |
| 134 | try: |
| 135 | validator.validate(config) |
| 136 | if expect_fail: |
| 137 | results["unexpected_pass"].append(name) |
| 138 | if not getattr(args, "continue"): |
| 139 | break |
| 140 | except jsonschema.exceptions.ValidationError as e: |
| 141 | if not expect_fail: |
| 142 | results["invalid"].append(name) |
| 143 | if args.verbose: |
| 144 | print(e) |
| 145 | if expect_fail or getattr(args, "continue"): |
| 146 | continue |
| 147 | break |
| 148 | |
| 149 | exit_status = 0 |
| 150 | if len(results["invalid"]) + len(results["unexpected_pass"]): |
| 151 | exit_status = 1 |
| 152 | unexpected_pass_suffix = " **" |
| 153 | show_suffix_explanation = False |
| 154 | print("results:") |
| 155 | for f in config_files: |
| 156 | if any([x in f for x in results["unexpected_pass"]]): |
| 157 | show_suffix_explanation = True |
| 158 | print(" '{}' passed!{}".format(f, unexpected_pass_suffix)) |
| 159 | if any([x in f for x in results["invalid"]]): |
| 160 | print(" '{}' failed!".format(f)) |
| 161 | |
| 162 | if show_suffix_explanation: |
| 163 | print("\n** configuration expected to fail") |
| 164 | |
| 165 | sys.exit(exit_status) |
| 166 | |
| 167 | |
| 168 | if __name__ == "__main__": |
| 169 | main() |