|
| 1 | +import json |
| 2 | +import logging |
| 3 | +from unittest import mock |
| 4 | + |
| 5 | +import pytest |
| 6 | +from requests import RequestException |
| 7 | + |
| 8 | +from elastalert.alerters.flashduty import FlashdutyAlerter |
| 9 | +from elastalert.loaders import FileRulesLoader |
| 10 | +from elastalert.util import EAException |
| 11 | + |
| 12 | + |
| 13 | +def test_flashduty_alert(caplog): |
| 14 | + caplog.set_level(logging.INFO) |
| 15 | + rule = { |
| 16 | + 'name': 'Test Flashduty Rule', |
| 17 | + 'type': 'any', |
| 18 | + 'flashduty_integration_key': 'xxx', |
| 19 | + 'flashduty_title': 'Test Alert', |
| 20 | + 'flashduty_description': 'Test Description', |
| 21 | + 'flashduty_event_status': 'Info', |
| 22 | + 'flashduty_alert_key': 'test-alert', |
| 23 | + 'flashduty_check': 'test-check', |
| 24 | + 'flashduty_service': 'test-service', |
| 25 | + 'flashduty_cluster': 'test-cluster', |
| 26 | + 'flashduty_resource': 'test-resource', |
| 27 | + 'flashduty_metric': 'test-metric', |
| 28 | + 'flashduty_group': 'test-group', |
| 29 | + 'flashduty_env': 'test-env', |
| 30 | + 'flashduty_app': 'test-app', |
| 31 | + 'alert': [], |
| 32 | + } |
| 33 | + rules_loader = FileRulesLoader({}) |
| 34 | + rules_loader.load_modules(rule) |
| 35 | + alert = FlashdutyAlerter(rule) |
| 36 | + match = { |
| 37 | + '@timestamp': '2025-03-20T00:00:00', |
| 38 | + 'somefield': 'foobar' |
| 39 | + } |
| 40 | + |
| 41 | + with mock.patch('requests.post') as mock_post_request: |
| 42 | + alert.alert([match]) |
| 43 | + |
| 44 | + expected_data = { |
| 45 | + 'title': 'Test Alert', |
| 46 | + 'description': 'Test Description', |
| 47 | + 'event_status': 'Info', |
| 48 | + 'alert_key': 'test-alert', |
| 49 | + 'labels': { |
| 50 | + 'check': 'test-check', |
| 51 | + 'service': 'test-service', |
| 52 | + 'cluster': 'test-cluster', |
| 53 | + 'resource': 'test-resource', |
| 54 | + 'metric': 'test-metric', |
| 55 | + 'group': 'test-group', |
| 56 | + 'env': 'test-env', |
| 57 | + 'app': 'test-app', |
| 58 | + 'information': 'Test Flashduty Rule\n\n@timestamp: 2025-03-20T00:00:00\nsomefield: foobar\n' |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + mock_post_request.assert_called_once_with( |
| 63 | + 'https://api.flashcat.cloud/event/push/alert/standard?integration_key=' + rule['flashduty_integration_key'], |
| 64 | + data=mock.ANY, |
| 65 | + headers={'Content-Type': 'application/json'} |
| 66 | + ) |
| 67 | + |
| 68 | + actual_data = json.loads(mock_post_request.call_args_list[0][1]['data']) |
| 69 | + assert expected_data == actual_data |
| 70 | + assert ('elastalert', logging.INFO, 'Trigger sent to flashduty') == caplog.record_tuples[0] |
| 71 | + |
| 72 | + |
| 73 | +def test_flashduty_ea_exception(): |
| 74 | + rule = { |
| 75 | + 'name': 'Test Flashduty Rule', |
| 76 | + 'type': 'any', |
| 77 | + 'flashduty_integration_key': 'xxx', |
| 78 | + 'alert': [], |
| 79 | + } |
| 80 | + rules_loader = FileRulesLoader({}) |
| 81 | + rules_loader.load_modules(rule) |
| 82 | + alert = FlashdutyAlerter(rule) |
| 83 | + match = { |
| 84 | + '@timestamp': '2025-03-20T00:00:00', |
| 85 | + 'somefield': 'foobar' |
| 86 | + } |
| 87 | + mock_run = mock.MagicMock(side_effect=RequestException) |
| 88 | + with mock.patch('requests.post', mock_run): |
| 89 | + with pytest.raises(EAException) as ea: |
| 90 | + alert.alert([match]) |
| 91 | + assert 'Error posting to flashduty: ' in str(ea) |
| 92 | + |
| 93 | + |
| 94 | +def test_flashduty_getinfo(): |
| 95 | + rule = { |
| 96 | + 'name': 'Test Flashduty Rule', |
| 97 | + 'type': 'any', |
| 98 | + 'flashduty_integration_key': 'xxx', |
| 99 | + 'flashduty_title': 'Test Alert', |
| 100 | + 'flashduty_description': 'Test Description', |
| 101 | + 'flashduty_event_status': 'Info', |
| 102 | + 'flashduty_alert_key': 'test-alert', |
| 103 | + 'flashduty_check': 'test-check', |
| 104 | + 'flashduty_service': 'test-service', |
| 105 | + 'flashduty_cluster': 'test-cluster', |
| 106 | + 'flashduty_resource': 'test-resource', |
| 107 | + 'flashduty_metric': 'test-metric', |
| 108 | + 'flashduty_group': 'test-group', |
| 109 | + 'flashduty_env': 'test-env', |
| 110 | + 'flashduty_app': 'test-app', |
| 111 | + 'alert': [], |
| 112 | + } |
| 113 | + rules_loader = FileRulesLoader({}) |
| 114 | + rules_loader.load_modules(rule) |
| 115 | + alert = FlashdutyAlerter(rule) |
| 116 | + |
| 117 | + expected_data = { |
| 118 | + 'type': 'flashduty', |
| 119 | + 'flashduty_integration_key': 'xxx', |
| 120 | + 'flashduty_title': 'Test Alert', |
| 121 | + 'flashduty_description': 'Test Description', |
| 122 | + 'flashduty_event_status': 'Info', |
| 123 | + 'flashduty_alert_key': 'test-alert', |
| 124 | + 'flashduty_check': 'test-check', |
| 125 | + 'flashduty_service': 'test-service', |
| 126 | + 'flashduty_cluster': 'test-cluster', |
| 127 | + 'flashduty_resource': 'test-resource', |
| 128 | + 'flashduty_metric': 'test-metric', |
| 129 | + 'flashduty_group': 'test-group', |
| 130 | + 'flashduty_env': 'test-env', |
| 131 | + 'flashduty_app': 'test-app' |
| 132 | + } |
| 133 | + actual_data = alert.get_info() |
| 134 | + assert expected_data == actual_data |
| 135 | + |
| 136 | + |
| 137 | +def test_flashduty_required_error(): |
| 138 | + try: |
| 139 | + rule = { |
| 140 | + 'name': 'Test Flashduty Rule', |
| 141 | + 'type': 'any', |
| 142 | + 'alert': [], |
| 143 | + } |
| 144 | + rules_loader = FileRulesLoader({}) |
| 145 | + rules_loader.load_modules(rule) |
| 146 | + FlashdutyAlerter(rule) |
| 147 | + except Exception as ea: |
| 148 | + assert 'Missing required option(s): flashduty_integration_key' in str(ea) |
0 commit comments