def test_slack_uses_list_of_custom_slack_channel(): rule = { 'name': 'Test Rule', 'type': 'any', 'slack_webhook_url': ['http://please.dontgohere.slack'], 'slack_channel_override': ['#test-alert', '#test-alert2'], 'alert': [] } rules_loader = FileRulesLoader({}) rules_loader.load_modules(rule) alert = SlackAlerter(rule) match = { '@timestamp': '2016-01-01T00:00:00', 'somefield': 'foobarbaz' } with mock.patch('requests.post') as mock_post_request: alert.alert([match]) expected_data1 = { 'username': '******', 'channel': '#test-alert', 'icon_emoji': ':ghost:', 'attachments': [ { 'color': 'danger', 'title': rule['name'], 'text': BasicMatchString(rule, match).__str__(), 'mrkdwn_in': ['text', 'pretext'], 'fields': [] } ], 'text': '', 'parse': 'none' } expected_data2 = { 'username': '******', 'channel': '#test-alert2', 'icon_emoji': ':ghost:', 'attachments': [ { 'color': 'danger', 'title': rule['name'], 'text': BasicMatchString(rule, match).__str__(), 'mrkdwn_in': ['text', 'pretext'], 'fields': [] } ], 'text': '', 'parse': 'none' } mock_post_request.assert_called_with( rule['slack_webhook_url'][0], data=mock.ANY, headers={'content-type': 'application/json'}, proxies=None, verify=True, timeout=10 ) assert expected_data1 == json.loads(mock_post_request.call_args_list[0][1]['data']) assert expected_data2 == json.loads(mock_post_request.call_args_list[1][1]['data'])
def test_slack_attach_jira_url_when_generated(): rule = { 'name': 'Test Rule', 'type': 'any', 'slack_attach_jira_ticket_url': True, 'slack_jira_ticket_title': 'My Title', 'slack_jira_ticket_color': '#aabbcc', 'slack_webhook_url': 'http://please.dontgohere.slack', 'alert': [] } rules_loader = FileRulesLoader({}) rules_loader.load_modules(rule) alert = SlackAlerter(rule) alert.pipeline = {'jira_ticket': 'foo_ticket', 'jira_server': 'https://myjiraserver'} match = { '@timestamp': '2016-01-01T00:00:00', } with mock.patch('requests.post') as mock_post_request: alert.alert([match]) expected_data = { 'username': '******', 'parse': 'none', 'text': '', 'attachments': [ { 'color': 'danger', 'title': 'Test Rule', 'text': BasicMatchString(rule, match).__str__(), 'mrkdwn_in': ['text', 'pretext'], 'fields': [] }, { 'color': '#aabbcc', 'title': 'My Title', 'title_link': 'https://myjiraserver/browse/foo_ticket' } ], 'icon_emoji': ':ghost:', 'channel': '' } mock_post_request.assert_called_once_with( rule['slack_webhook_url'], data=mock.ANY, headers={'content-type': 'application/json'}, proxies=None, verify=True, timeout=10 ) actual_data = json.loads(mock_post_request.call_args_list[0][1]['data']) assert expected_data == actual_data
def test_slack_get_aggregation_summary_text__maximum_width(): rule = { 'name': 'Test Rule', 'type': 'any', 'slack_webhook_url': 'http://please.dontgohere.slack', 'slack_username_override': 'elastalert', 'slack_msg_pretext': 'pretext value', 'alert_subject': 'Cool subject', 'alert': [] } rules_loader = FileRulesLoader({}) rules_loader.load_modules(rule) alert = SlackAlerter(rule) assert 75 == alert.get_aggregation_summary_text__maximum_width()
def test_slack_ca_certs(ca_certs, ignore_ssl_errors, excpet_verify): rule = { 'name': 'Test Rule', 'type': 'any', 'slack_webhook_url': 'http://please.dontgohere.slack', 'alert_subject': 'Cool subject', 'alert': [] } if ca_certs: rule['slack_ca_certs'] = ca_certs if ignore_ssl_errors: rule['slack_ignore_ssl_errors'] = ignore_ssl_errors rules_loader = FileRulesLoader({}) rules_loader.load_modules(rule) alert = SlackAlerter(rule) match = { '@timestamp': '2017-01-01T00:00:00', 'somefield': 'foobarbaz' } with mock.patch('requests.post') as mock_post_request: alert.alert([match]) expected_data = { 'username': '******', 'channel': '', 'icon_emoji': ':ghost:', 'attachments': [ { 'color': 'danger', 'title': rule['alert_subject'], 'text': BasicMatchString(rule, match).__str__(), 'mrkdwn_in': ['text', 'pretext'], 'fields': [] } ], 'text': '', 'parse': 'none' } mock_post_request.assert_called_once_with( rule['slack_webhook_url'], data=mock.ANY, headers={'content-type': 'application/json'}, proxies=None, verify=excpet_verify, timeout=10 ) assert expected_data == json.loads(mock_post_request.call_args_list[0][1]['data'])
def test_slack_uses_custom_title(caplog): caplog.set_level(logging.INFO) rule = { 'name': 'Test Rule', 'type': 'any', 'slack_webhook_url': 'http://please.dontgohere.slack', 'alert_subject': 'Cool subject', 'alert': [] } rules_loader = FileRulesLoader({}) rules_loader.load_modules(rule) alert = SlackAlerter(rule) match = { '@timestamp': '2016-01-01T00:00:00', 'somefield': 'foobarbaz' } with mock.patch('requests.post') as mock_post_request: alert.alert([match]) expected_data = { 'username': '******', 'channel': '', 'icon_emoji': ':ghost:', 'attachments': [ { 'color': 'danger', 'title': rule['alert_subject'], 'text': BasicMatchString(rule, match).__str__(), 'mrkdwn_in': ['text', 'pretext'], 'fields': [] } ], 'text': '', 'parse': 'none' } mock_post_request.assert_called_once_with( rule['slack_webhook_url'], data=mock.ANY, headers={'content-type': 'application/json'}, proxies=None, verify=True, timeout=10 ) assert expected_data == json.loads(mock_post_request.call_args_list[0][1]['data']) assert ('elastalert', logging.INFO, "Alert 'Test Rule' sent to Slack") == caplog.record_tuples[0]
def test_slack_getinfo(): rule = { 'name': 'Test Rule', 'type': 'any', 'slack_webhook_url': 'http://please.dontgohere.slack', 'alert_subject': 'Cool subject', 'alert': [] } rules_loader = FileRulesLoader({}) rules_loader.load_modules(rule) alert = SlackAlerter(rule) expected_data = { 'type': 'slack', 'slack_username_override': 'elastalert' } actual_data = alert.get_info() assert expected_data == actual_data
def test_slack_required_error(slack_webhook_url, expected_data): try: rule = { 'name': 'Test Rule', 'type': 'any', 'alert_subject': 'Cool subject', 'alert': [] } if slack_webhook_url: rule['slack_webhook_url'] = slack_webhook_url rules_loader = FileRulesLoader({}) rules_loader.load_modules(rule) alert = SlackAlerter(rule) actual_data = alert.get_info() assert expected_data == actual_data except Exception as ea: assert expected_data in str(ea)
def test_slack_ea_exception(): with pytest.raises(EAException) as ea: rule = { 'name': 'Test Rule', 'type': 'any', 'slack_webhook_url': 'http://please.dontgohere.slack', 'slack_username_override': 'elastalert', 'slack_msg_pretext': 'pretext value', 'alert_subject': 'Cool subject', 'alert': [] } rules_loader = FileRulesLoader({}) rules_loader.load_modules(rule) alert = SlackAlerter(rule) match = { '@timestamp': '2016-01-01T00:00:00', 'somefield': 'foobarbaz' } mock_run = mock.MagicMock(side_effect=RequestException) with mock.patch('requests.post', mock_run), pytest.raises(RequestException): alert.alert([match]) assert 'Error posting to slack: ' in str(ea)