Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core - validate - report errors per file #8565

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions c7n/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,12 @@ def validate(options):

used_policy_names = set()
structure = StructureParser()
errors = []
all_errors = {}
found_deprecations = False
footnotes = deprecated.Footnotes()

for config_file in options.configs:
errors = []

config_file = os.path.expanduser(config_file)
if not os.path.exists(config_file):
Expand All @@ -221,7 +222,7 @@ def validate(options):
except PolicyValidationError as e:
log.error("Configuration invalid: {}".format(config_file))
log.error("%s" % e)
errors.append(e)
all_errors[config_file] = e
continue

load_resources(structure.get_resource_types(data))
Expand Down Expand Up @@ -270,6 +271,7 @@ def validate(options):
log.info("Configuration valid: {}".format(config_file))
continue

all_errors[config_file] = errors
log.error("Configuration invalid: {}".format(config_file))
for e in errors:
log.error("%s" % e)
Expand All @@ -279,7 +281,7 @@ def validate(options):
log.warning("deprecation footnotes:\n" + notes)
if options.check_deprecations == deprecated.STRICT:
sys.exit(1)
if errors:
if all_errors:
sys.exit(1)


Expand Down
13 changes: 13 additions & 0 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,23 @@ def test_failed_validation(self):
verbose=False,
check_deprecations="yes",
)
log_output = self.capture_logging("custodian.commands")
with self.assertRaises((SystemExit, ValueError)) as exit:
validate_yaml_policies(yaml_validate_options)
# if there is a bad policy in the batch being validated, there should be an exit 1
self.assertEqual(exit.exception.code, 1)

# ...and it should report accurate per-file validation status individually
validation_output = log_output.getvalue()
self.assertIn(
"Configuration invalid: tests/data/test_policies/ebs-BADVALIDATION.yml",
validation_output
)
self.assertIn(
"Configuration valid: tests/data/test_policies/ami-GOODVALIDATION.yml",
validation_output
)

yaml_validate_options.configs.remove(
"tests/data/test_policies/ebs-BADVALIDATION.yml"
)
Expand Down