Skip to content

Commit 3bce173

Browse files
authored
Fixed return None on missing metric for filter_metric_by_component (#3645)
##### Short description: `test_cdi_json_patch_metrics` uses the function `filter_metric_by_component` which returns None instead of 0 if the metric isn't found. The None value is used in later addition in `wait_for_metrics_value_update`, causing a type error ##### More details: having trouble reproducing issue on local cluster, so trying to future-proof with a warning and new return statement ##### What this PR does / why we need it: part of detecting early 4.99 failures for 4.22.0 ##### Which issue(s) this PR fixes: ##### Special notes for reviewer: ##### jira-ticket: https://issues.redhat.com/browse/CNV-78331 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Tests** * Improved metric handling in test utilities: missing or malformed metrics now log a warning and return a safe default numeric value. * Sampling logic tightened to ignore empty or falsy samples before performing comparisons to prevent false positives. * Timeout failures are now logged and re-raised so test harnesses reliably detect and handle timing issues. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent f9619a4 commit 3bce173

File tree

1 file changed

+10
-3
lines changed
  • tests/install_upgrade_operators/json_patch

1 file changed

+10
-3
lines changed

tests/install_upgrade_operators/json_patch/utils.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,15 @@ def get_metrics_value_with_annotation(prometheus, query_string, component_name):
6363
def filter_metric_by_component(metrics, metric_name, component_name):
6464
annotation_name = get_annotation_name_for_component(component_name=component_name)
6565
for metric in metrics:
66-
if metric["metric"]["annotation_name"] == annotation_name and metric["metric"]["__name__"] == metric_name:
67-
return int(metric["value"][1])
66+
if (
67+
metric.get("metric", {}).get("annotation_name") == annotation_name
68+
and metric.get("metric", {}).get("__name__") == metric_name
69+
):
70+
metric_value = metric.get("value", [])
71+
if len(metric_value) > 1:
72+
return int(metric_value[1])
73+
LOGGER.warning(f"No results found when filtering for {metric_name} and annotation {annotation_name} in {metrics}.")
74+
return 0
6875

6976

7077
def wait_for_metrics_value_update(prometheus, component_name, query_string, previous_value):
@@ -78,7 +85,7 @@ def wait_for_metrics_value_update(prometheus, component_name, query_string, prev
7885
)
7986
try:
8087
for sample in samples:
81-
if sample == previous_value + 1:
88+
if sample and sample == previous_value + 1:
8289
return sample
8390
except TimeoutExpiredError:
8491
LOGGER.error(f"Query string: {query_string} for component: {component_name}, previous value: {previous_value}.")

0 commit comments

Comments
 (0)