-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_utils.py
415 lines (383 loc) · 13.7 KB
/
test_utils.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
"""Module to test the utils.py file."""
import pytest
from unittest.mock import patch, MagicMock
from kytos.core.interface import Interface
from kytos.lib.helpers import get_link_mock
from napps.amlight.sdntrace_cp import utils, settings
# pylint: disable=too-many-public-methods, duplicate-code, protected-access
class TestUtils():
"""Test utils.py functions."""
@patch("requests.get")
def test_get_stored_flows(self, get_mock):
"Test get_stored_flows"
response = MagicMock()
response.status_code = 200
response.json.return_value = {"result": "ok"}
get_mock.return_value = response
api_url = f'{settings.FLOW_MANAGER_URL}/stored_flows/?state=installed'
result = utils.get_stored_flows()
get_mock.assert_called_with(api_url, timeout=20)
assert result['result'] == "ok"
def test_convert_list_entries(self):
"""Verify convert entries with a list of one example"""
eth = {"dl_vlan": 100}
dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
switch = {"switch": dpid, "eth": eth}
entries = {"trace": switch}
result = utils.convert_list_entries([entries])
expected = [{
"dpid": "00:00:00:00:00:00:00:01",
"in_port": 1,
"dl_vlan": [100],
}]
assert result == expected
def test_convert_entries_vlan(self):
"""Verify convert entries with simple example with vlan."""
eth = {"dl_vlan": 100}
dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
switch = {"switch": dpid, "eth": eth}
entries = {"trace": switch}
result = utils.convert_entries(entries)
expected = {
"dpid": "00:00:00:00:00:00:00:01",
"in_port": 1,
"dl_vlan": [100],
}
assert result == expected
def test_prepare_json(self):
"""Verify prepare json with simple tracepath result."""
trace_result = [
{
"in": {
"dpid": "00:00:00:00:00:00:00:01",
"port": 1,
"time": "2022-06-01 01:01:01.100000",
"type": "starting",
}
},
{
"in": {
"dpid": "00:00:00:00:00:00:00:03",
"port": 3,
"time": "2022-06-01 01:01:01.100000",
"type": "intermediary",
"vlan": 100,
},
"out": {
"port": 1,
"vlan": 123,
},
}
]
result = utils.prepare_json(trace_result)
expected = {
"result": [
{
"dpid": "00:00:00:00:00:00:00:01",
"port": 1,
"time": "2022-06-01 01:01:01.100000",
"type": "starting",
},
{
"dpid": "00:00:00:00:00:00:00:03",
"port": 3,
"time": "2022-06-01 01:01:01.100000",
"type": "intermediary",
"vlan": 100,
"out": {"port": 1, "vlan": 123},
},
]
}
assert result == expected
def test_prepare_list_json(self):
"""Verify prepare list with a simple tracepath result."""
trace_result = [
{
"in": {
"dpid": "00:00:00:00:00:00:00:01",
"port": 1,
"time": "2022-06-01 01:01:01.100000",
"type": "starting",
}
},
{
"in": {
"dpid": "00:00:00:00:00:00:00:03",
"port": 3,
"time": "2022-06-01 01:01:01.100000",
"type": "intermediary",
"vlan": 100,
},
"out": {
"port": 1,
"vlan": 123,
},
}
]
result = utils._prepare_json(trace_result)
expected = [
{
"dpid": "00:00:00:00:00:00:00:01",
"port": 1,
"time": "2022-06-01 01:01:01.100000",
"type": "starting",
},
{
"dpid": "00:00:00:00:00:00:00:03",
"port": 3,
"time": "2022-06-01 01:01:01.100000",
"type": "intermediary",
"vlan": 100,
"out": {"port": 1, "vlan": 123},
},
]
assert result == expected
def test_prepare_json_empty(self):
"""Verify prepare json with empty result."""
trace_result = []
result = utils.prepare_json(trace_result)
assert result == {"result": []}
@pytest.mark.parametrize(
"endpoint1,endpoint2,result",
[
(
{"dpid": "00:00:00:00:00:00:00:01"},
{"dpid": "00:00:00:00:00:00:00:02"},
False
),
(
{
"dpid": "00:00:00:00:00:00:00:03",
"out_port": 2,
"out_vlan": 200,
},
{
"dpid": "00:00:00:00:00:00:00:03",
"in_port": 3,
"in_vlan": 100,
},
False
),
(
{
"dpid": "00:00:00:00:00:00:00:03",
"in_port": 3,
"in_vlan": 100,
},
{
"dpid": "00:00:00:00:00:00:00:03",
"in_port": 3,
"in_vlan": 100,
},
False
),
(
{
"dpid": "00:00:00:00:00:00:00:03",
"in_port": 3,
"in_vlan": 100,
},
{
"dpid": "00:00:00:00:00:00:00:03",
"out_port": 2,
"out_vlan": 200,
},
False
),
(
{
"dpid": "00:00:00:00:00:00:00:03",
"in_port": 3,
"out_port": 2,
"in_vlan": 100,
},
{
"dpid": "00:00:00:00:00:00:00:03",
"in_port": 2,
"out_port": 3,
"out_vlan": 200,
},
False
),
(
{
"dpid": "00:00:00:00:00:00:00:03",
"in_port": 3,
"out_port": 2,
"in_vlan": 100,
},
{
"dpid": "00:00:00:00:00:00:00:03",
"in_port": 2,
"out_port": 3,
},
False
),
(
{
"dpid": "00:00:00:00:00:00:00:03",
"in_port": 3,
"out_port": 2,
},
{
"dpid": "00:00:00:00:00:00:00:03",
"in_port": 2,
"out_port": 3,
"out_vlan": 200,
},
False
),
(
{
"dpid": "00:00:00:00:00:00:00:01",
"in_port": 3,
"out_port": 2,
"out_vlan": 200,
},
{
"dpid": "00:00:00:00:00:00:00:01",
"in_port": 2,
"out_port": 3,
"in_vlan": 100,
},
False
),
(
{
"dpid": "00:00:00:00:00:00:00:01",
"in_port": 3,
"out_port": 2,
"out_vlan": 200,
},
{
"dpid": "00:00:00:00:00:00:00:01",
"in_port": 2,
"out_port": 3,
},
False
),
(
{
"dpid": "00:00:00:00:00:00:00:01",
"in_port": 3,
"out_port": 2,
},
{
"dpid": "00:00:00:00:00:00:00:01",
"in_port": 2,
"out_port": 3,
"in_vlan": 100,
},
False
),
(
{
"dpid": "00:00:00:00:00:00:00:03",
"in_port": 3,
"in_vlan": 100,
},
{
"dpid": "00:00:00:00:00:00:00:03",
"out_port": 3,
"out_vlan": 100,
},
True
),
]
)
def test_compare_endpoints1(self, endpoint1, endpoint2, result):
"""Test for compare endpoinst for the internal conditional no.
1 - first: Test endpoint1 dpid != endpoint2 dpid
2 - second: Test endpoint1 without in_port
3 - second: Test endpoint2 without out_port
4 - second: Test endpoint1 in_port != endpoint2 out_port
5 - third: Test endpoint1 in_vlan != endpoint2 out_vlan
6 - first: Test endpoint1 with in_vlan and endpoint2 without out_vlan
7 - first: Test endpoint1 without in_vlan and endpoint2 with out_vlan
8 - fifth: Test endpoint1 out_vlan != endpoint2 in_vlan
9 - fifth: Test endpoint1 with out_vlan and endpoint2 without in_vlan
10 - fifth: Test endpoint1 without out_vlan and endpoint2 with in_vlan
11 - fifth: Test endpoint1 out_vlan != endpoint2 in_vlan
"""
assert utils._compare_endpoints(endpoint1, endpoint2) == result
def test_find_endpoint_b(self):
"""Test find endpoint with interface equals link endpoint B."""
port = 1
mock_interface = Interface("interface A", port, MagicMock())
mock_interface.address = "00:00:00:00:00:00:00:01"
mock_interface.link = get_link_mock(
"00:00:00:00:00:00:00:02", "00:00:00:00:00:00:00:01"
)
mock_switch = MagicMock()
mock_switch.get_interface_by_port_no.return_value = mock_interface
expected = {'endpoint': mock_interface.link.endpoint_a}
result = utils.find_endpoint(mock_switch, port)
assert result == expected
def test_find_endpoint_a(self):
"""Test find endpoint with interface equals link endpoint A."""
port = 1
mock_interface = Interface("interface A", port, MagicMock())
mock_interface.address = "00:00:00:00:00:00:00:01"
mock_interface.link = get_link_mock(
"00:00:00:00:00:00:00:01", "00:00:00:00:00:00:00:03"
)
mock_switch = MagicMock()
mock_switch.get_interface_by_port_no.return_value = mock_interface
expected = {'endpoint': mock_interface.link.endpoint_b}
result = utils.find_endpoint(mock_switch, port)
assert result == expected
def test_find_endpoint_link_none(self):
"""Test find endpoint without link."""
port = 1
mock_interface = Interface("interface A", port, MagicMock())
mock_interface.address = "00:00:00:00:00:00:00:01"
mock_switch = MagicMock()
mock_switch.get_interface_by_port_no.return_value = mock_interface
result = utils.find_endpoint(mock_switch, port)
assert 'endpoint' in result
assert result['endpoint'] is None
def test_convert_vlan(self):
"""Test convert_vlan function"""
value = 100
result = utils.convert_vlan(value)
assert result[0] == 100
value = "4096/4096"
result = utils.convert_vlan(value)
assert result[0] == 4096
assert result[1] == 4096
@pytest.mark.parametrize(
"value,field_flow,result",
[
(None, 0, True),
(None, 10, False),
(None, "4096/4096", False),
([10], 0, False),
([10], 10, True),
([10], "4096/4096", True),
([10], 11, False),
([3], "5/1", True),
([2], "5/1", False),
]
)
def test_match_field_dl_vlan(self, value, field_flow, result):
"""Test match_field_dl_vlan"""
assert utils.match_field_dl_vlan(value, field_flow) == result
@pytest.mark.parametrize(
"field,field_flow,result",
[
('192.168.20.21', '192.168.20.21', True),
('192.168.20.21', '192.168.20.21/10', True),
('192.168.20.21', '192.168.20.21/32', True),
('192.168.20.21', '192.168.20.21/255.255.255.255', True),
('192.168.20.30', '192.168.20.21', False),
('192.200.20.30', '192.168.20.21/10', False),
('2002:db8::8a3f:362:7897', '2002:db8::8a3f:362:7897', True),
('2002:db8::8a3f:362:7897', '2002:db8::8a3f:362:7897/10', True),
('2002:db8::8a3f:362:7897', '2002:db8::8a3f:362:7897/128', True),
('2002:db8::8a3f:362:7', '2002:db8::8a3f:362:7897', False),
('3002:db8::9a3f:362:7897', '2002:db8::8a3f:362:7897/10', False),
],
)
def test_match_field_ip(self, field, field_flow, result):
"""Test match_field_ip"""
assert utils.match_field_ip(field, field_flow) == result