Skip to content

Commit

Permalink
Improve 'add-checks' logic in plugins-test
Browse files Browse the repository at this point in the history
Calling such test with the '--add-checks' flag will now make the test
add to the static references only those checks that were not stored
previously.

Flag description has been improved too.

Related: CMK-14637
Change-Id: I4a99958618571854cbe0c406c39caea4789607ee
  • Loading branch information
MatteoStifano committed Oct 23, 2023
1 parent c1bc36c commit 93e59e7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
30 changes: 17 additions & 13 deletions tests/plugins_integration/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,8 @@ def _verify_check_result(
if result_data and canon_data == result_data:
return True, ""

if mode == CheckModes.UPDATE or (mode == CheckModes.ADD and not canon_data):
logger.info(
"[%s] Canon %s!",
check_id,
"updated" if mode == CheckModes.UPDATE else "added",
)
if mode == CheckModes.UPDATE:
logger.info("[%s] Canon updated!", check_id)
return True, ""

with open(
Expand Down Expand Up @@ -297,7 +293,6 @@ def process_check_output(
output_dir: Path,
) -> bool:
"""Process the check output and either dump or compare it."""
passed = True if config.mode == CheckModes.UPDATE else None
logger.info('> Processing agent host "%s"...', host_name)
diffs = {}

Expand All @@ -310,28 +305,36 @@ def process_check_output(
else:
check_canons = {}

passed = None
check_results = {
_: item.get("extensions") for _, item in sorted(get_check_results(site, host_name).items())
}
for check_id in check_results:
logger.debug('> Processing check id "%s"...', check_id)
check_canon = check_canons.get(check_id, {})
check_result = check_results.get(check_id, {})
if config.mode == CheckModes.ADD and not check_canons.get(check_id):
check_canons[check_id] = check_results[check_id]
logger.info("[%s] Canon added!", check_id)

logger.debug('> Verifying check id "%s"...', check_id)
check_success, diff = _verify_check_result(
check_id,
check_canon,
check_result,
check_canons.get(check_id, {}),
check_results[check_id],
output_dir,
config.mode,
)
if check_success:
if passed is None:
passed = True
continue
passed = False
if config.mode == CheckModes.UPDATE:
check_canons[check_id] = check_results[check_id]
logger.info("[%s] Canon updated!", check_id)
passed = True
else:
passed = False
diffs[check_id] = diff

if diffs:
os.makedirs(config.diff_dir, exist_ok=True) # type: ignore
with open(
Expand All @@ -340,13 +343,14 @@ def process_check_output(
encoding="utf-8",
) as json_file:
json.dump(diffs, json_file, indent=4)

if config.mode != CheckModes.DEFAULT:
with open(
f"{config.response_dir}/{host_name}.json",
mode="w",
encoding="utf-8",
) as json_file:
json.dump(check_results, json_file, indent=4)
json.dump(check_canons, json_file, indent=4)

return passed is True

Expand Down
6 changes: 4 additions & 2 deletions tests/plugins_integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ def pytest_addoption(parser):
"--update-checks",
action="store_true",
default=False,
help="Store updated check output as static references",
help="Store updated check output as static references: checks already stored as reference"
"are updated and new ones are added.",
)
parser.addoption(
"--add-checks",
action="store_true",
default=False,
help="Store added check output as static references",
help="Store added check output as static references: checks already stored as reference are"
"not touched. Only new checks are added.",
)
parser.addoption(
"--skip-cleanup",
Expand Down

0 comments on commit 93e59e7

Please sign in to comment.