Open netkey opened 2 years ago
Alert type not added to README.md and elastalert.rst
The description of the alerter setting added to ruletypes.rst has not been added.
No test code has been added.
testcode example
from elastalert.alerts import DingTalkAlerter
from elastalert.util import EAException
from requests import RequestException
def test_dingtalk():
rule = {
'name': 'Test DingTalk Rule',
'type': 'any',
'dingtalk_webhook': 'http://xxxxxxxxxx/xxxxxxxx',
'dingtalk_msgtype': 'text',
'alert': []
}
rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = DingTalkAlerter(rule)
match = {
'@timestamp': '2021-01-01T00:00:00',
'somefield': 'foobarbaz'
}
with mock.patch('requests.post') as mock_post_request:
alert.alert([match])
expected_data = {
'msgtype': 'text',
'text': {'content': 'Test DingTalk Rule\n\n@timestamp: 2021-01-01T00:00:00\nsomefield: foobarbaz\n'},
'at': {'isAtAll': False}
}
mock_post_request.assert_called_once_with(
'http://xxxxxxxxxx/xxxxxxxx',
data=mock.ANY,
headers={
'Content-Type': 'application/json',
'Accept': 'application/json;charset=utf-8'
},
)
actual_data = json.loads(mock_post_request.call_args_list[0][1]['data'])
assert expected_data == actual_data
def test_dingtalk_ea_exception():
with pytest.raises(EAException) as ea:
rule = {
'name': 'Test DingTalk Rule',
'type': 'any',
'dingtalk_webhook': 'http://xxxxxxxxxx/xxxxxxxx',
'dingtalk_msgtype': 'text',
'alert': []
}
rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = DingTalkAlerter(rule)
match = {
'@timestamp': '2021-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 request to Dingtalk: ' in str(ea)
def test_dingtalk_getinfo():
rule = {
'name': 'Test DingTalk Rule',
'type': 'any',
'dingtalk_webhook': 'http://xxxxxxxxxx/xxxxxxxx',
'dingtalk_msgtype': 'text',
'alert': [],
'alert_subject': 'Test DingTalk'
}
rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = DingTalkAlerter(rule)
expected_data = {
'type': 'dingtalk',
"dingtalk_webhook": 'http://xxxxxxxxxx/xxxxxxxx'
}
actual_data = alert.get_info()
assert expected_data == actual_data
@pytest.mark.parametrize('dingtalk_webhook, dingtalk_msgtype, expected_data', [
('x', '', 'Missing required option(s): dingtalk_webhook, dingtalk_msgtype'),
('', 'x', 'Missing required option(s): dingtalk_webhook, dingtalk_msgtype'),
])
def test_dingtalk_required_error(dingtalk_webhook, dingtalk_msgtype, expected_data):
try:
rule = {
'name': 'Test DingTalk Rule',
'type': 'any',
'alert': [],
'alert_subject': 'Test DingTalk'
}
if dingtalk_webhook:
rule['dingtalk_webhook'] = dingtalk_webhook
if dingtalk_msgtype:
rule['dingtalk_msgtype'] = dingtalk_msgtype
rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = DingTalkAlerter(rule)
actual_data = alert.get_info()
assert expected_data == actual_data
except Exception as ea:
assert expected_data in str(ea)
lint error
./elastalert/alerts.py:2188:1: E302 expected 2 blank lines, found 1
./elastalert/alerts.py:2214:26: E231 missing whitespace after ':'
./elastalert/alerts.py:2219:25: E128 continuation line under-indented for visual indent
./elastalert/alerts.py:2220:25: E128 continuation line under-indented for visual indent
./elastalert/alerts.py:2232:1: E302 expected 2 blank lines, found 1
./elastalert/alerts.py:2253:1: W191 indentation contains tabs
./elastalert/alerts.py:2253:1: E101 indentation contains mixed spaces and tabs
./elastalert/alerts.py:2254:1: W191 indentation contains tabs
./elastalert/alerts.py:2254:1: E101 indentation contains mixed spaces and tabs
./elastalert/alerts.py:2255:1: W191 indentation contains tabs
./elastalert/alerts.py:2255:1: E101 indentation contains mixed spaces and tabs
./elastalert/alerts.py:2257:1: W191 indentation contains tabs
./elastalert/alerts.py:2257:1: E101 indentation contains mixed spaces and tabs
./elastalert/alerts.py:2261:25: E128 continuation line under-indented for visual indent
./elastalert/alerts.py:2262:25: E128 continuation line under-indented for visual indent
./elastalert/loaders.py:82:37: W291 trailing whitespace
add dingtalk and aiops alert