regulators: Improve config file validator
Improve error checking in config file validator.
Add checking if config file does not exist, config file is not readable,
and config file is not a JSON file.
Add checking if schema file does not exist, schema file is not readable,
and schema file is not a JSON file.
Tested:
Run local CI with -Dlong-tests=enabled to enable tests for
validate-regulators-config.py.
1/7 phosphor-regulators-tests OK 32.49s
Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: I5a84949e89be004a20a04c00d16b49286cb42f37
diff --git a/phosphor-regulators/tools/validate-regulators-config.py b/phosphor-regulators/tools/validate-regulators-config.py
index 7b1cd6f..125bd0c 100755
--- a/phosphor-regulators/tools/validate-regulators-config.py
+++ b/phosphor-regulators/tools/validate-regulators-config.py
@@ -2,8 +2,9 @@
import argparse
import json
-import sys
import jsonschema
+import os
+import sys
r"""
Validates the phosphor-regulators configuration file. Checks it against a JSON
@@ -283,6 +284,14 @@
return config_json
+def validate_JSON_format(file):
+ with open(file) as json_data:
+ try:
+ return json.load(json_data)
+ except ValueError as err:
+ return False
+ return True
+
if __name__ == '__main__':
parser = argparse.ArgumentParser(
@@ -299,9 +308,27 @@
if not args.schema_file:
parser.print_help()
sys.exit("Error: Schema file is required.")
+ if not os.path.exists(args.schema_file):
+ parser.print_help()
+ sys.exit("Error: Schema file does not exist.")
+ if not os.access(args.schema_file, os.R_OK):
+ parser.print_help()
+ sys.exit("Error: Schema file is not readable.")
+ if not validate_JSON_format(args.schema_file):
+ parser.print_help()
+ sys.exit("Error: Schema file is not in the JSON format.")
if not args.configuration_file:
parser.print_help()
sys.exit("Error: Configuration file is required.")
+ if not os.path.exists(args.configuration_file):
+ parser.print_help()
+ sys.exit("Error: Configuration file does not exist.")
+ if not os.access(args.configuration_file, os.R_OK):
+ parser.print_help()
+ sys.exit("Error: Configuration file is not readable.")
+ if not validate_JSON_format(args.configuration_file):
+ parser.print_help()
+ sys.exit("Error: Configuration file is not in the JSON format.")
config_json = validate_schema(args.configuration_file, args.schema_file)