-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_type_checks.py
More file actions
360 lines (318 loc) · 10.8 KB
/
test_type_checks.py
File metadata and controls
360 lines (318 loc) · 10.8 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
"""Check Type unit tests."""
import pytest
from jdiff import extract_data_from_json
from jdiff.check_types import (
CheckType,
ExactMatchType,
OperatorType,
ParameterMatchType,
RegexType,
ToleranceType,
)
from .utility import ASSERT_FAIL_MESSAGE, load_json_file, load_mocks
def test_child_class_raises_exception():
"""Tests that exception is raised for child class when abstract methods are not implemented."""
class CheckTypeChild(CheckType):
"""Test Class."""
with pytest.raises(TypeError) as error:
CheckTypeChild() # pylint: disable=E0110
assert "Can't instantiate abstract class CheckTypeChild" in str(error.value)
assert "_validate" in str(error.value)
assert "evaluate" in str(error.value)
def test_child_class_proper_implementation():
"""Test properly implemented child class can be instantiated."""
class CheckTypeChild(CheckType):
"""Test Class."""
@staticmethod
def _validate(*args):
return None
def evaluate(self, *args, **kwargs):
return {}, True
check = CheckTypeChild()
assert isinstance(check, CheckTypeChild) and check._validate() is None and check.evaluate() == ({}, True)
@pytest.mark.parametrize(
"check_type_str, expected_class",
[
("exact_match", ExactMatchType),
("tolerance", ToleranceType),
("parameter_match", ParameterMatchType),
("regex", RegexType),
("operator", OperatorType),
],
)
def test_check_init(check_type_str, expected_class):
"""Validate that the returned class is the expected one."""
assert isinstance(CheckType.create(check_type_str), expected_class)
exception_tests_init = [
("does_not_exist", NotImplementedError, ""),
]
@pytest.mark.parametrize("check_type_str, exception_type, expected_in_output", exception_tests_init)
def tests_exceptions_init(check_type_str, exception_type, expected_in_output):
"""Tests exceptions when check object is initialized."""
with pytest.raises(exception_type) as error:
CheckType.create(check_type_str)
assert expected_in_output in str(error.value)
exact_match_test_values_no_change = (
"exact_match",
{},
"api",
"result[0].vrfs.default.peerList[*].[$peerAddress$,establishedTransitions]",
({}, True),
)
exact_match_test_values_changed = (
"exact_match",
{},
"api",
"result[0].vrfs.default.peerList[*].[$peerAddress$,prefixesSent]",
(
{
"10.1.0.0": {"prefixesSent": {"new_value": 52, "old_value": 50}},
"7.7.7.7": {"prefixesSent": {"new_value": 52, "old_value": 50}},
},
False,
),
)
tolerance_test_values_no_change = (
"tolerance",
{"tolerance": 10},
"api",
"result[0].vrfs.default.peerList[*].[$peerAddress$,establishedTransitions]",
({}, True),
)
tolerance_test_values_within_threshold = (
"tolerance",
{"tolerance": 10},
"api",
"result[0].vrfs.default.peerList[*].[$peerAddress$,prefixesSent]",
({}, True),
)
tolerance_test_values_beyond_threshold = (
"tolerance",
{"tolerance": 10},
"api",
"result[0].vrfs.default.peerList[*].[$peerAddress$,prefixesReceived]",
(
{
"10.1.0.0": {"prefixesReceived": {"new_value": 120, "old_value": 100}},
"10.64.207.255": {"prefixesReceived": {"new_value": 60, "old_value": 100}},
},
False,
),
)
check_type_tests = [
exact_match_test_values_no_change,
exact_match_test_values_changed,
tolerance_test_values_no_change,
tolerance_test_values_within_threshold,
tolerance_test_values_beyond_threshold,
]
@pytest.mark.parametrize("check_type_str, evaluate_args, folder_name, path, expected_results", check_type_tests)
def test_check_type_results(check_type_str, evaluate_args, folder_name, path, expected_results):
"""Validate that CheckType.evaluate returns the expected_results."""
check = CheckType.create(check_type_str)
pre_data, post_data = load_mocks(folder_name)
pre_value = extract_data_from_json(pre_data, path)
post_value = extract_data_from_json(post_data, path)
actual_results = check.evaluate(pre_value, post_value, **evaluate_args)
assert actual_results == expected_results, ASSERT_FAIL_MESSAGE.format(
output=actual_results, expected_output=expected_results
)
napalm_bgp_neighbor_status = (
"napalm_get_bgp_neighbors",
"exact_match",
{},
"global.peers.$*$.[is_enabled,is_up]",
(
{
"7.7.7.7": {
"is_enabled": {"new_value": False, "old_value": True},
"is_up": {"new_value": False, "old_value": True},
}
},
False,
),
)
napalm_bgp_neighbor_prefixes_ipv4 = (
"napalm_get_bgp_neighbors",
"tolerance",
{"tolerance": 10},
"global.peers.$*$.*.ipv4.[accepted_prefixes,received_prefixes,sent_prefixes]",
({"10.1.0.0": {"accepted_prefixes": {"new_value": 900, "old_value": 1000}}}, False),
)
napalm_bgp_neighbor_prefixes_ipv6 = (
"napalm_get_bgp_neighbors",
"tolerance",
{"tolerance": 10},
"global.peers.$*$.*.ipv6.[accepted_prefixes,received_prefixes,sent_prefixes]",
({"10.64.207.255": {"received_prefixes": {"new_value": 1100, "old_value": 1000}}}, False),
)
napalm_get_lldp_neighbors_exact_raw = (
"napalm_get_lldp_neighbors",
"exact_match",
{},
None,
(
{
"Ethernet1": {
"port": {"new_value": "518", "old_value": "519"},
"missing": [{"hostname": "ios-xrv-unittest", "port": "Gi0/0/0/0"}],
},
"Ethernet3": {
"new": [
{"hostname": "ios-xrv-unittest", "port": "Gi0/0/0/0"},
{"hostname": "ios-xrv-unittest", "port": "Gi0/0/0/1"},
]
},
},
False,
),
)
tolerance_no_path = (
"tolerance",
"tolerance",
{"tolerance": 10},
"",
(
{
"interfaces": {
"Ethernet1": {"power_level": {"new_value": 4, "old_value": -4}},
"Ethernet3": {"power_level": {"new_value": 3, "old_value": -3}},
}
},
False,
),
)
tolerance_path = (
"tolerance",
"tolerance",
{"tolerance": 10},
"interfaces",
(
{
"Ethernet1": {"power_level": {"new_value": 4, "old_value": -4}},
"Ethernet3": {"power_level": {"new_value": 3, "old_value": -3}},
},
False,
),
)
tolerance_deep_path = (
"tolerance",
"tolerance",
{"tolerance": 10},
"interfaces.Ethernet1",
(
{"power_level": {"new_value": 4, "old_value": -4}},
False,
),
)
check_tests = [
napalm_bgp_neighbor_status,
napalm_bgp_neighbor_prefixes_ipv4,
napalm_bgp_neighbor_prefixes_ipv6,
napalm_get_lldp_neighbors_exact_raw,
tolerance_no_path,
tolerance_path,
tolerance_deep_path,
]
@pytest.mark.parametrize("folder_name, check_type_str, evaluate_args, path, expected_result", check_tests)
def test_checks(folder_name, check_type_str, evaluate_args, path, expected_result):
"""Validate multiple checks on the same data to catch corner cases."""
check = CheckType.create(check_type_str)
pre_data, post_data = load_mocks(folder_name)
pre_value = extract_data_from_json(pre_data, path)
post_value = extract_data_from_json(post_data, path)
actual_results = check.evaluate(pre_value, post_value, **evaluate_args)
assert actual_results == expected_result, ASSERT_FAIL_MESSAGE.format(
output=actual_results, expected_output=expected_result
)
parameter_match_api = (
"pre.json",
"parameter_match",
{"mode": "match", "params": {"localAsn": "65130.1100", "linkType": "external"}},
"result[0].vrfs.default.peerList[*].[$peerAddress$,localAsn,linkType]",
(
{
"7.7.7.7": {"localAsn": "65130.1010", "linkType": "the road to seniority"},
"10.1.0.0": {"localAsn": "65130.8888"},
},
False,
),
)
parameter_no_match_api = (
"pre.json",
"parameter_match",
{"mode": "no-match", "params": {"localAsn": "65130.1100", "linkType": "external"}},
"result[0].vrfs.default.peerList[*].[$peerAddress$,localAsn,linkType]",
(
{
"10.1.0.0": {"linkType": "external"},
"10.2.0.0": {"localAsn": "65130.1100", "linkType": "external"},
"10.64.207.255": {"localAsn": "65130.1100", "linkType": "external"},
},
False,
),
)
parameter_match_napalm_facts = (
"napalm_facts.json",
"parameter_match",
{"mode": "match", "params": {"os_version": "12.1X47-D20.7"}},
"*",
({}, True),
)
@pytest.mark.parametrize(
"filename, check_type_str, evaluate_args, path, expected_result",
[parameter_match_api, parameter_no_match_api, parameter_match_napalm_facts],
)
def test_param_match(filename, check_type_str, evaluate_args, path, expected_result):
"""Validate parameter_match check type."""
check = CheckType.create(check_type_str)
# There is not concept of "pre" and "post" in parameter_match.
data = load_json_file("parameter_match", filename)
value = extract_data_from_json(data, path)
# pylint:disable=too-many-function-args
actual_results = check.evaluate(evaluate_args["params"], value, evaluate_args["mode"])
assert actual_results == expected_result, ASSERT_FAIL_MESSAGE.format(
output=actual_results, expected_output=expected_result
)
regex_match_include = (
"pre.json",
"regex",
{"regex": ".*UNDERLAY.*", "mode": "match"},
"result[0].vrfs.default.peerList[*].[$peerAddress$,peerGroup]",
(
{
"7.7.7.7": {"peerGroup": "EVPN-OVERLAY-SPINE"},
},
False,
),
)
regex_match_exclude = (
"pre.json",
"regex",
{"regex": ".*UNDERLAY.*", "mode": "no-match"},
"result[0].vrfs.default.peerList[*].[$peerAddress$,peerGroup]",
(
{
"10.1.0.0": {"peerGroup": "IPv4-UNDERLAY-SPINE"},
"10.2.0.0": {"peerGroup": "IPv4-UNDERLAY-SPINE"},
"10.64.207.255": {"peerGroup": "IPv4-UNDERLAY-MLAG-PEER"},
},
False,
),
)
regex_match = [
regex_match_include,
regex_match_exclude,
]
@pytest.mark.parametrize("filename, check_type_str, evaluate_args, path, expected_result", regex_match)
def test_regex_match(filename, check_type_str, evaluate_args, path, expected_result):
"""Validate regex check type."""
check = CheckType.create(check_type_str)
# There is not concept of "pre" and "post" in parameter_match.
data = load_json_file("api", filename)
value = extract_data_from_json(data, path)
# pylint:disable=too-many-function-args
actual_results = check.evaluate(evaluate_args["regex"], value, evaluate_args["mode"])
assert actual_results == expected_result, ASSERT_FAIL_MESSAGE.format(
output=actual_results, expected_output=expected_result
)