|
1 | 1 | import logging |
2 | 2 |
|
3 | 3 | import pytest |
4 | | -from ocp_resources.prometheus_rule import PrometheusRule |
5 | 4 |
|
6 | 5 | from tests.utils import validate_runbook_url_exists |
7 | 6 | from utilities.constants import CNV_PROMETHEUS_RULES, QUARANTINED |
8 | 7 |
|
9 | 8 | LOGGER = logging.getLogger(__name__) |
10 | 9 |
|
11 | 10 |
|
12 | | -def get_downstream_runbook_url(alert_name): |
13 | | - return f"https://github.com/openshift/runbooks/blob/master/alerts/openshift-virtualization-operator/{alert_name}.md" |
14 | | - |
15 | | - |
16 | | -def get_upstream_runbook_url(alert_name): |
17 | | - return f"https://github.com/kubevirt/monitoring/blob/main/docs/runbooks/{alert_name}.md" |
18 | | - |
19 | | - |
20 | | -@pytest.fixture(scope="module") |
21 | | -def cnv_prometheus_rules_names(hco_namespace): |
22 | | - return [prometheus_rule.name for prometheus_rule in PrometheusRule.get(namespace=hco_namespace.name)] |
23 | | - |
24 | | - |
25 | | -@pytest.mark.polarion("CNV-10081") |
26 | | -def test_no_new_prometheus_rules(cnv_prometheus_rules_names): |
27 | | - """ |
28 | | - Since validations for runbook url of all cnv alerts are done via polarion parameterization of prometheusrules, |
29 | | - this test has been added to catch any new cnv prometheusrules that is not part of cnv_prometheus_rules_matrix |
30 | | - """ |
31 | | - assert sorted(CNV_PROMETHEUS_RULES) == sorted(cnv_prometheus_rules_names), ( |
32 | | - f"New cnv prometheusrule found: {set(cnv_prometheus_rules_names) - set(CNV_PROMETHEUS_RULES)}" |
33 | | - ) |
34 | | - |
35 | | - |
36 | | -@pytest.fixture() |
37 | | -def cnv_prometheus_rules_unique_alert_names_runbook(cnv_alerts_from_prometheus_rule): |
38 | | - alert_runbook_dict = {} |
39 | | - for alert in cnv_alerts_from_prometheus_rule: |
40 | | - alert_runbook_dict.setdefault(alert["alert"], set()).add(alert["annotations"]["runbook_url"]) |
41 | | - alerts_with_multiple_runbooks = { |
42 | | - alert_name: runbook_urls for alert_name, runbook_urls in alert_runbook_dict.items() if len(runbook_urls) > 1 |
43 | | - } |
44 | | - assert not alerts_with_multiple_runbooks, ( |
45 | | - f"Alerts with multiple different runbook URLs found: {alerts_with_multiple_runbooks}" |
46 | | - ) |
47 | | - return alert_runbook_dict |
48 | | - |
49 | | - |
50 | | -@pytest.mark.polarion("CNV-10083") |
51 | | -def test_runbook_upstream_urls(cnv_prometheus_rules_unique_alert_names_runbook): |
52 | | - url_not_reachable = {} |
53 | | - for alert_name in cnv_prometheus_rules_unique_alert_names_runbook.keys(): |
54 | | - url_not_reachable[alert_name] = validate_runbook_url_exists(url=get_upstream_runbook_url(alert_name=alert_name)) |
55 | | - not_reachable_url = list( |
56 | | - filter( |
57 | | - lambda _alert_name: url_not_reachable[_alert_name] is not None, |
58 | | - url_not_reachable, |
| 11 | +class TestRunbookUrlsAndPrometheusRules: |
| 12 | + @pytest.mark.polarion("CNV-10081") |
| 13 | + def test_no_new_prometheus_rules(self, cnv_prometheus_rules_names): |
| 14 | + """ |
| 15 | + Since validations for runbook url of all cnv alerts are done via polarion parameterization of prometheusrules, |
| 16 | + this test has been added to catch any new cnv prometheusrules that is not part of cnv_prometheus_rules_matrix |
| 17 | + """ |
| 18 | + assert sorted(CNV_PROMETHEUS_RULES) == sorted(cnv_prometheus_rules_names), ( |
| 19 | + f"New cnv prometheusrule found: {set(cnv_prometheus_rules_names) - set(CNV_PROMETHEUS_RULES)}" |
59 | 20 | ) |
60 | | - ) |
61 | | - if not_reachable_url: |
62 | | - LOGGER.error(f"Upstream runbook url not reachable for following CNV alerts: {not_reachable_url}") |
63 | | - raise AssertionError("CNV alerts with unreachable runbook urls found.") |
64 | | - |
65 | 21 |
|
66 | | -@pytest.mark.xfail( |
67 | | - reason=f"{QUARANTINED}: New alerts runbooks added to upstream and not merged yet for downstream, CNV-67890", |
68 | | - run=False, |
69 | | -) |
70 | | -@pytest.mark.polarion("CNV-10084") |
71 | | -def test_runbook_downstream_urls(cnv_prometheus_rules_unique_alert_names_runbook): |
72 | | - error_messages = [] |
73 | | - alerts_without_runbook = {} |
74 | | - for alert_name, runbook_url in cnv_prometheus_rules_unique_alert_names_runbook.items(): |
75 | | - runbook_url = next(iter(runbook_url)) |
76 | | - expected_url = get_downstream_runbook_url(alert_name=alert_name) |
77 | | - if not runbook_url or runbook_url != expected_url: |
78 | | - LOGGER.error(f"For alert: {alert_name}, expected url: {expected_url}, actual url: {runbook_url}") |
79 | | - alerts_without_runbook[alert_name] = alert_name |
80 | | - error = validate_runbook_url_exists(url=expected_url) |
81 | | - if error: |
82 | | - error_messages.append(error) |
83 | | - if alerts_without_runbook: |
84 | | - LOGGER.error(f"Runbook url missing for following CNV alerts: {alerts_without_runbook}") |
85 | | - raise AssertionError("CNV alerts with missing runbook url found.") |
86 | | - |
87 | | - if error_messages: |
88 | | - message = f"Downstream runbook url validation failed for the followings: {error_messages}" |
89 | | - LOGGER.error(message) |
90 | | - raise AssertionError(message) |
| 22 | + @pytest.mark.xfail( |
| 23 | + reason=f"{QUARANTINED}: New alerts runbooks added to upstream and not merged yet for downstream, CNV-67890", |
| 24 | + run=False, |
| 25 | + ) |
| 26 | + @pytest.mark.polarion("CNV-10084") |
| 27 | + def test_runbook_downstream_urls(self, cnv_alerts_runbook_urls_from_prometheus_rule): |
| 28 | + error_messages = {} |
| 29 | + alerts_without_runbook = [] |
| 30 | + |
| 31 | + for alert_name, runbook_url in cnv_alerts_runbook_urls_from_prometheus_rule.items(): |
| 32 | + if not runbook_url: |
| 33 | + LOGGER.error(f"For alert: {alert_name} Url not found") |
| 34 | + alerts_without_runbook.append(alert_name) |
| 35 | + error = validate_runbook_url_exists(url=runbook_url) |
| 36 | + if error: |
| 37 | + LOGGER.error(f"Alert {alert_name} url {runbook_url} is not valid") |
| 38 | + error_messages[alert_name] = runbook_url |
| 39 | + if alerts_without_runbook: |
| 40 | + LOGGER.error(f"Runbook url missing for following CNV alerts: {alerts_without_runbook}") |
| 41 | + raise AssertionError("CNV alerts with missing runbook url found.") |
| 42 | + |
| 43 | + if error_messages: |
| 44 | + message = f"Downstream runbook url validation failed for the followings: {error_messages}" |
| 45 | + LOGGER.error(message) |
| 46 | + raise AssertionError(message) |
0 commit comments