-
Notifications
You must be signed in to change notification settings - Fork 171
/
test_rules.py
164 lines (145 loc) · 5.22 KB
/
test_rules.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'''
Unit test definitions for all rules
'''
import mailer
import pytest
from alertaclient.models.alert import Alert
from mock import DEFAULT, MagicMock, patch
def test_rules_dont_exist():
'''
Test the rules file is read
'''
with patch('mailer.os') as system_os:
system_os.path.exists.return_value = False
# res = mailer.parse_group_rules('config_file')
system_os.path.exists.called_once_with('confile_file')
# assert res is None
def test_rules_parsing():
'''
Test the rules file is properly read
'''
with patch.multiple(mailer, os=DEFAULT, open=DEFAULT,
json=DEFAULT, validate_rules=DEFAULT) as mocks:
mocks['os'].path.exists.return_value = True
mocks['os'].walk().__iter__\
.return_value = [('/', None,
['cantopen.json', 'invalid.json', 'valid.json'])]
invalid_file = MagicMock()
valid_file = MagicMock()
mocks['open'].side_effect = [IOError, invalid_file, valid_file]
doc = [{'notify': {'fields': []}}]
mocks['json'].load.side_effect = [TypeError, doc]
mocks['validate_rules'].return_value = doc
res = mailer.parse_group_rules('config_file')
# Assert that we checked for folder existence
mocks['os'].path.exists.called_once_with('confile_file')
# Check that validation was called for valid file
mocks['validate_rules'].assert_called_once_with(doc)
assert mocks['validate_rules'].call_count == 1
# Assert that we tried to open all 3 files
assert mocks['open'].call_count == 3
# Assert that we tried to load 2 files only
assert mocks['json'].load.call_count == 2
# Assert that we have proper return value
assert res == doc
TESTDOCS = [
('', False),
('String', False),
({}, False),
([], True),
([
{'name': 'invalid_no_fields',
'contacts': []}
], False),
([
{'name': 'invalid_empty_fields',
'fields': [],
'contacts': []}
], False),
([
{'name': 'invalid_no_contacts',
'fields': [{'field': 'resource', 'regex': r'\d{4}'}]}
], False),
([
{'name': 'invalid_no_field_on_fields',
'fields': [{'regex': r'\d{4}'}],
'contacts': []}
], False),
([
{'name': 'invalid_fields_not_list',
'fields': {'regex': r'\d{4}'},
'contacts': []}
], False),
([
{'name': 'invalid_no_fields_regex',
'fields': [{'field': 'test'}],
'contacts': []}
], False),
([
{'name': 'invalid_no_fields_regex',
'fields': [{'field': 'tags', 'regex': 'atag'}],
'exclude': True,
'contacts': []}
], True),
]
@pytest.mark.parametrize('doc, is_valid', TESTDOCS)
def test_rules_validation(doc, is_valid):
'''
Test rule validation
'''
res = mailer.validate_rules(doc)
if is_valid:
assert res is not None and res == doc
else:
assert res is None or res == []
RULES_DATA = [
# ({'resource': 'server-1234', 'event': '5678'}, [], []),
({'resource': '1234', 'event': '5678'},
[{'name': 'Test1',
'fields': [{'field': 'resource', 'regex': r'(\w.*)?\d{4}'}],
'contacts': ['[email protected]']}],
['[email protected]'])
]
@pytest.mark.parametrize('alert_spec, input_rules, expected_contacts',
RULES_DATA)
def test_rules_evaluation(alert_spec, input_rules, expected_contacts):
'''
Test that rules are properly evaluated
'''
with patch.dict(mailer.OPTIONS, mailer.DEFAULT_OPTIONS):
mailer.OPTIONS['mail_to'] = []
mailer.OPTIONS['group_rules'] = input_rules
mail_sender = mailer.MailSender()
with patch.object(mail_sender, '_send_email_message') as _sem:
alert = Alert.parse(alert_spec)
_, emailed_contacts = mail_sender.send_email(alert)
assert _sem.call_count == 1
assert emailed_contacts == expected_contacts
def test_rule_matches_list():
'''
Test regex matching is working properly
for a list
'''
# Mock options to instantiate mailer
with patch.dict(mailer.OPTIONS, mailer.DEFAULT_OPTIONS):
mail_sender = mailer.MailSender()
with patch.object(mailer, 're') as regex:
regex.match.side_effect = [MagicMock(), None]
assert mail_sender._rule_matches('regex', ['item1']) is True
regex.match.assert_called_with('regex', 'item1')
assert mail_sender._rule_matches('regex', ['item2']) is False
regex.match.assert_called_with('regex', 'item2')
def test_rule_matches_string():
'''
Test regex matching is working properly
for a string
'''
# Mock options to instantiate mailer
with patch.dict(mailer.OPTIONS, mailer.DEFAULT_OPTIONS):
mail_sender = mailer.MailSender()
with patch.object(mailer, 're') as regex:
regex.search.side_effect = [MagicMock(), None]
assert mail_sender._rule_matches('regex', 'value1') is True
regex.search.assert_called_with('regex', 'value1')
assert mail_sender._rule_matches('regex', 'value2') is False
regex.search.assert_called_with('regex', 'value2')