|
| 1 | +import pytest |
| 2 | + |
| 3 | +from tests.integration.helpers import ( |
| 4 | + BASE_CMDS, |
| 5 | + assert_headers_in_lines, |
| 6 | + delete_target_id, |
| 7 | + exec_test_command, |
| 8 | + get_random_text, |
| 9 | + retry_exec_test_command_with_delay, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +def test_channels_list(): |
| 14 | + res = exec_test_command( |
| 15 | + BASE_CMDS["alerts"] + ["channels-list", "--text", "--delimiter=,"] |
| 16 | + ) |
| 17 | + lines = res.splitlines() |
| 18 | + headers = [ |
| 19 | + "channel_type", |
| 20 | + "content.email.email_addresses", |
| 21 | + "id", |
| 22 | + "label", |
| 23 | + "type", |
| 24 | + "updated", |
| 25 | + ] |
| 26 | + assert_headers_in_lines(headers, lines) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def get_channel_id(): |
| 31 | + channel_ids = exec_test_command( |
| 32 | + BASE_CMDS["alerts"] |
| 33 | + + [ |
| 34 | + "channels-list", |
| 35 | + "--text", |
| 36 | + "--no-headers", |
| 37 | + "--delimiter", |
| 38 | + ",", |
| 39 | + "--format", |
| 40 | + "id", |
| 41 | + ] |
| 42 | + ).splitlines() |
| 43 | + first_id = channel_ids[0].split(",")[0] |
| 44 | + yield first_id |
| 45 | + |
| 46 | + |
| 47 | +def test_alerts_definition_create(get_channel_id, get_service_type): |
| 48 | + label = get_random_text(8) + "_alert" |
| 49 | + exec_test_command( |
| 50 | + BASE_CMDS["alerts"] |
| 51 | + + [ |
| 52 | + "definition-create", |
| 53 | + "--channel_ids", |
| 54 | + get_channel_id, |
| 55 | + "--label", |
| 56 | + label, |
| 57 | + "--rule_criteria.rules.metric", |
| 58 | + "cpu_usage", |
| 59 | + "--rule_criteria.rules.operator", |
| 60 | + "eq", |
| 61 | + "--rule_criteria.rules.threshold", |
| 62 | + "80", |
| 63 | + "--rule_criteria.rules.aggregate_function", |
| 64 | + "avg", |
| 65 | + "--severity", |
| 66 | + "1", |
| 67 | + "--trigger_conditions.criteria_condition", |
| 68 | + "ALL", |
| 69 | + "--trigger_conditions.evaluation_period_seconds", |
| 70 | + "300", |
| 71 | + "--trigger_conditions.polling_interval_seconds", |
| 72 | + "300", |
| 73 | + "--trigger_conditions.trigger_occurrences", |
| 74 | + "3", |
| 75 | + get_service_type, |
| 76 | + ] |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +def test_alerts_list(): |
| 81 | + res = exec_test_command( |
| 82 | + BASE_CMDS["alerts"] |
| 83 | + + ["definitions-list-all", "--text", "--delimiter=,"] |
| 84 | + ) |
| 85 | + lines = res.splitlines() |
| 86 | + headers = ["class", "created", "label", "severity", "service_type"] |
| 87 | + assert_headers_in_lines(headers, lines) |
| 88 | + |
| 89 | + |
| 90 | +@pytest.fixture |
| 91 | +def get_alert_id(): |
| 92 | + alert_id = exec_test_command( |
| 93 | + BASE_CMDS["alerts"] |
| 94 | + + [ |
| 95 | + "definitions-list-all", |
| 96 | + "--text", |
| 97 | + "--no-headers", |
| 98 | + "--delimiter", |
| 99 | + ",", |
| 100 | + "--format", |
| 101 | + "id", |
| 102 | + ] |
| 103 | + ).splitlines() |
| 104 | + first_id = alert_id[-1] |
| 105 | + yield first_id |
| 106 | + |
| 107 | + |
| 108 | +def test_alert_view(get_alert_id, get_service_type): |
| 109 | + alert_id = get_alert_id |
| 110 | + service_type = get_service_type |
| 111 | + res = exec_test_command( |
| 112 | + BASE_CMDS["alerts"] |
| 113 | + + [ |
| 114 | + "definition-view", |
| 115 | + service_type, |
| 116 | + alert_id, |
| 117 | + "--text", |
| 118 | + "--delimiter=,", |
| 119 | + ] |
| 120 | + ) |
| 121 | + lines = res.splitlines() |
| 122 | + |
| 123 | + headers = ["class", "created", "label", "severity", "service_type"] |
| 124 | + assert_headers_in_lines(headers, lines) |
| 125 | + |
| 126 | + |
| 127 | +def test_alert_update(get_alert_id, get_service_type): |
| 128 | + alert_id = get_alert_id |
| 129 | + service_type = get_service_type |
| 130 | + new_label = get_random_text(8) + "_updated" |
| 131 | + updated_label = retry_exec_test_command_with_delay( |
| 132 | + BASE_CMDS["alerts"] |
| 133 | + + [ |
| 134 | + "definition-update", |
| 135 | + service_type, |
| 136 | + alert_id, |
| 137 | + "--label", |
| 138 | + new_label, |
| 139 | + "--text", |
| 140 | + "--no-headers", |
| 141 | + "--format=label", |
| 142 | + ], |
| 143 | + delay=50, |
| 144 | + ) |
| 145 | + assert updated_label == new_label |
| 146 | + delete_target_id( |
| 147 | + target="alerts", |
| 148 | + delete_command="definition-delete", |
| 149 | + service_type=service_type, |
| 150 | + id=alert_id, |
| 151 | + use_retry=True, |
| 152 | + ) |
0 commit comments