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/test/validate-regulators-config_tests.cpp b/phosphor-regulators/test/validate-regulators-config_tests.cpp
index e62a88c..42038d1 100644
--- a/phosphor-regulators/test/validate-regulators-config_tests.cpp
+++ b/phosphor-regulators/test/validate-regulators-config_tests.cpp
@@ -2914,17 +2914,17 @@
expectCommandLineSyntax("Error: Schema file is required.",
outputMessageHelp, command, 1);
}
+ // Invalid: -c specified more than once
+ {
+ command = validateTool + schema + schemaFile + "-c -c " + fileName;
+ expectCommandLineSyntax(outputMessageHelp, outputMessage, command, 2);
+ }
// Invalid: -s specified more than once
{
command =
validateTool + "-s -s " + schemaFile + configuration + fileName;
expectCommandLineSyntax(outputMessageHelp, outputMessage, command, 2);
}
- // Invalid: -c specified more than once
- {
- command = validateTool + schema + schemaFile + "-c -c " + fileName;
- expectCommandLineSyntax(outputMessageHelp, outputMessage, command, 2);
- }
// Invalid: No file name specified after -c
{
command = validateTool + schema + schemaFile + configuration;
@@ -2939,15 +2939,28 @@
{
command = validateTool + schema + schemaFile + configuration +
"../notExistFile";
- expectCommandLineSyntax(
- "Traceback (most recent call last):", outputMessage, command, 1);
+ expectCommandLineSyntax("Error: Configuration file does not exist.",
+ outputMessageHelp, command, 1);
}
// Invalid: File specified after -s does not exist
{
command = validateTool + schema + "../notExistFile " + configuration +
fileName;
+ expectCommandLineSyntax("Error: Schema file does not exist.",
+ outputMessageHelp, command, 1);
+ }
+ // Invalid: File specified after -c is not right data format
+ {
+ TemporaryFile wrongFormatFile;
+ std::string wrongFormatFileName = wrongFormatFile.getPath().string();
+ std::ofstream out(wrongFormatFileName);
+ out << "foo";
+ out.close();
+ command = validateTool + schema + schemaFile + configuration +
+ wrongFormatFileName;
expectCommandLineSyntax(
- "Traceback (most recent call last):", outputMessage, command, 1);
+ "Error: Configuration file is not in the JSON format.",
+ outputMessageHelp, command, 1);
}
// Invalid: File specified after -s is not right data format
{
@@ -2958,8 +2971,8 @@
out.close();
command = validateTool + schema + wrongFormatFileName + configuration +
fileName;
- expectCommandLineSyntax(
- "Traceback (most recent call last):", outputMessage, command, 1);
+ expectCommandLineSyntax("Error: Schema file is not in the JSON format.",
+ outputMessageHelp, command, 1);
}
// Invalid: File specified after -c is not readable
{
@@ -2969,8 +2982,8 @@
command = validateTool + schema + schemaFile + configuration +
notReadableFileName;
chmod(notReadableFileName.c_str(), 0222);
- expectCommandLineSyntax(
- "Traceback (most recent call last):", outputMessage, command, 1);
+ expectCommandLineSyntax("Error: Configuration file is not readable.",
+ outputMessageHelp, command, 1);
}
// Invalid: File specified after -s is not readable
{
@@ -2980,8 +2993,8 @@
command = validateTool + schema + notReadableFileName + configuration +
fileName;
chmod(notReadableFileName.c_str(), 0222);
- expectCommandLineSyntax(
- "Traceback (most recent call last):", outputMessage, command, 1);
+ expectCommandLineSyntax("Error: Schema file is not readable.",
+ outputMessageHelp, command, 1);
}
// Invalid: Unexpected parameter specified (like -g)
{