|
1
|
|
|
"""Module to test the utls.py file.""" |
|
2
|
1 |
|
from unittest.mock import MagicMock, Mock |
|
3
|
1 |
|
import pytest |
|
4
|
|
|
|
|
5
|
1 |
|
from kytos.core.common import EntityStatus |
|
6
|
1 |
|
from napps.kytos.mef_eline.exceptions import DisabledSwitch |
|
7
|
1 |
|
from napps.kytos.mef_eline.utils import (check_disabled_component, |
|
8
|
|
|
compare_endpoint_trace, |
|
9
|
|
|
compare_uni_out_trace, |
|
10
|
|
|
get_vlan_tags_and_masks, map_dl_vlan, |
|
11
|
|
|
merge_flow_dicts, prepare_delete_flow, |
|
12
|
|
|
_does_uni_affect_evc) |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
# pylint: disable=too-many-public-methods, too-many-lines |
|
16
|
1 |
|
class TestUtils: |
|
17
|
|
|
"""Test utility functions.""" |
|
18
|
|
|
|
|
19
|
1 |
|
@pytest.mark.parametrize( |
|
20
|
|
|
"switch,expected", |
|
21
|
|
|
[ |
|
22
|
|
|
( |
|
23
|
|
|
MagicMock(dpid="1234"), |
|
24
|
|
|
True |
|
25
|
|
|
), |
|
26
|
|
|
( |
|
27
|
|
|
MagicMock(dpid="2345"), |
|
28
|
|
|
False |
|
29
|
|
|
) |
|
30
|
|
|
] |
|
31
|
|
|
) |
|
32
|
1 |
|
def test_compare_endpoint_trace(self, switch, expected): |
|
33
|
|
|
"""Test method compare_endpoint_trace""" |
|
34
|
1 |
|
trace = {"dpid": "1234", "port": 2, "vlan": 123} |
|
35
|
|
|
|
|
36
|
1 |
|
endpoint = MagicMock() |
|
37
|
1 |
|
endpoint.port_number = 2 |
|
38
|
1 |
|
vlan = 123 |
|
39
|
1 |
|
endpoint.switch = switch |
|
40
|
1 |
|
assert compare_endpoint_trace(endpoint, vlan, trace) == expected |
|
41
|
1 |
|
assert compare_endpoint_trace(endpoint, None, trace) == expected |
|
42
|
|
|
|
|
43
|
1 |
|
def test_compare_uni_out_trace(self): |
|
44
|
|
|
"""Test compare_uni_out_trace method.""" |
|
45
|
|
|
# case1: trace without 'out' info, should return True |
|
46
|
1 |
|
interface = MagicMock() |
|
47
|
1 |
|
assert compare_uni_out_trace(None, interface, {}) |
|
48
|
|
|
|
|
49
|
|
|
# case2: trace with valid port and VLAN, should return True |
|
50
|
1 |
|
interface.port_number = 1 |
|
51
|
1 |
|
tag_value = 123 |
|
52
|
1 |
|
trace = {"out": {"port": 1, "vlan": 123}} |
|
53
|
1 |
|
assert compare_uni_out_trace(tag_value, interface, trace) |
|
54
|
|
|
|
|
55
|
|
|
# case3: UNI has VLAN but trace dont have, should return False |
|
56
|
1 |
|
trace = {"out": {"port": 1}} |
|
57
|
1 |
|
assert compare_uni_out_trace(tag_value, interface, trace) is False |
|
58
|
|
|
|
|
59
|
|
|
# case4: UNI and trace dont have VLAN should return True |
|
60
|
1 |
|
assert compare_uni_out_trace(None, interface, trace) |
|
61
|
|
|
|
|
62
|
|
|
# case5: UNI dont have VLAN but trace has, should return False |
|
63
|
1 |
|
trace = {"out": {"port": 1, "vlan": 123}} |
|
64
|
1 |
|
assert compare_uni_out_trace(None, interface, trace) is False |
|
65
|
|
|
|
|
66
|
1 |
|
def test_map_dl_vlan(self): |
|
67
|
|
|
"""Test map_dl_vlan""" |
|
68
|
1 |
|
cases = {0: None, "untagged": None, "any": 1, "4096/4096": 1, 10: 10} |
|
69
|
1 |
|
for value, mapped in cases.items(): |
|
70
|
1 |
|
result = map_dl_vlan(value) |
|
71
|
1 |
|
assert result == mapped |
|
72
|
|
|
|
|
73
|
1 |
|
@pytest.mark.parametrize( |
|
74
|
|
|
"vlan_range,expected", |
|
75
|
|
|
[ |
|
76
|
|
|
( |
|
77
|
|
|
[[101, 200]], |
|
78
|
|
|
[ |
|
79
|
|
|
101, |
|
80
|
|
|
"102/4094", |
|
81
|
|
|
"104/4088", |
|
82
|
|
|
"112/4080", |
|
83
|
|
|
"128/4032", |
|
84
|
|
|
"192/4088", |
|
85
|
|
|
200, |
|
86
|
|
|
] |
|
87
|
|
|
), |
|
88
|
|
|
( |
|
89
|
|
|
[[101, 90]], |
|
90
|
|
|
[] |
|
91
|
|
|
), |
|
92
|
|
|
( |
|
93
|
|
|
[[34, 34]], |
|
94
|
|
|
[34] |
|
95
|
|
|
), |
|
96
|
|
|
( |
|
97
|
|
|
[ |
|
98
|
|
|
[34, 34], |
|
99
|
|
|
[128, 128], |
|
100
|
|
|
[130, 135] |
|
101
|
|
|
], |
|
102
|
|
|
[ |
|
103
|
|
|
34, |
|
104
|
|
|
128, |
|
105
|
|
|
"130/4094", |
|
106
|
|
|
"132/4092" |
|
107
|
|
|
] |
|
108
|
|
|
) |
|
109
|
|
|
] |
|
110
|
|
|
) |
|
111
|
1 |
|
def test_get_vlan_tags_and_masks(self, vlan_range, expected): |
|
112
|
|
|
"""Test get_vlan_tags_and_masks""" |
|
113
|
1 |
|
assert get_vlan_tags_and_masks(vlan_range) == expected |
|
114
|
|
|
|
|
115
|
1 |
|
def test_check_disabled_component(self): |
|
116
|
|
|
"""Test check disabled component""" |
|
117
|
1 |
|
uni_a = MagicMock() |
|
118
|
1 |
|
switch = MagicMock() |
|
119
|
1 |
|
switch.status = EntityStatus.DISABLED |
|
120
|
1 |
|
uni_a.interface.switch = switch |
|
121
|
|
|
|
|
122
|
1 |
|
uni_z = MagicMock() |
|
123
|
1 |
|
uni_z.interface.switch = switch |
|
124
|
|
|
|
|
125
|
|
|
# Switch disabled |
|
126
|
1 |
|
with pytest.raises(DisabledSwitch): |
|
127
|
1 |
|
check_disabled_component(uni_a, uni_z) |
|
128
|
|
|
|
|
129
|
|
|
# Uni_a interface disabled |
|
130
|
1 |
|
switch.status = EntityStatus.UP |
|
131
|
1 |
|
uni_a.interface.status = EntityStatus.DISABLED |
|
132
|
1 |
|
with pytest.raises(DisabledSwitch): |
|
133
|
1 |
|
check_disabled_component(uni_a, uni_z) |
|
134
|
|
|
|
|
135
|
|
|
# Uni_z interface disabled |
|
136
|
1 |
|
uni_a.interface.status = EntityStatus.UP |
|
137
|
1 |
|
uni_z.interface.status = EntityStatus.DISABLED |
|
138
|
1 |
|
with pytest.raises(DisabledSwitch): |
|
139
|
1 |
|
check_disabled_component(uni_a, uni_z) |
|
140
|
|
|
|
|
141
|
|
|
# There is no disabled component |
|
142
|
1 |
|
uni_z.interface.status = EntityStatus.UP |
|
143
|
1 |
|
check_disabled_component(uni_a, uni_z) |
|
144
|
|
|
|
|
145
|
1 |
|
@pytest.mark.parametrize( |
|
146
|
|
|
"src1,src2,src3,expected", |
|
147
|
|
|
[ |
|
148
|
|
|
( |
|
149
|
|
|
{"dpida": [10, 11, 12]}, |
|
150
|
|
|
{"dpida": [11, 20, 21]}, |
|
151
|
|
|
{"dpidb": [30, 31, 32]}, |
|
152
|
|
|
{"dpida": [10, 11, 12, 11, 20, 21], "dpidb": [30, 31, 32]}, |
|
153
|
|
|
), |
|
154
|
|
|
( |
|
155
|
|
|
{"dpida": [10, 11, 12]}, |
|
156
|
|
|
{"dpida": [11, 20, 21], "dpidb": [40, 41, 42]}, |
|
157
|
|
|
{"dpidb": [30, 31, 32]}, |
|
158
|
|
|
{"dpida": [10, 11, 12, 11, 20, 21], |
|
159
|
|
|
"dpidb": [40, 41, 42, 30, 31, 32]}, |
|
160
|
|
|
), |
|
161
|
|
|
] |
|
162
|
|
|
) |
|
163
|
1 |
|
def test_merge_flow_dicts(self, src1, src2, src3, expected) -> None: |
|
164
|
|
|
"""test merge flow dicts.""" |
|
165
|
1 |
|
assert merge_flow_dicts({}, src1, src2, src3) == expected |
|
166
|
|
|
|
|
167
|
1 |
|
def test_prepare_delete_flow(self): |
|
168
|
|
|
"""Test prepare_delete_flow""" |
|
169
|
1 |
|
cookie_mask = int(0xffffffffffffffff) |
|
170
|
1 |
|
flow_mod = {'00:01': [ |
|
171
|
|
|
{ |
|
172
|
|
|
'match': {'in_port': 1, 'dl_vlan': 22}, |
|
173
|
|
|
'cookie': 12275899796742638400, |
|
174
|
|
|
'actions': [{'action_type': 'pop_vlan'}], |
|
175
|
|
|
'owner': 'mef_eline', |
|
176
|
|
|
'table_group': 'evpl', |
|
177
|
|
|
'table_id': 0, |
|
178
|
|
|
'priority': 20000 |
|
179
|
|
|
}, |
|
180
|
|
|
{ |
|
181
|
|
|
'match': {'in_port': 3, 'dl_vlan': 1}, |
|
182
|
|
|
'cookie': 12275899796742638400, |
|
183
|
|
|
'actions': [{'action_type': 'pop_vlan'}], |
|
184
|
|
|
'owner': 'mef_eline', |
|
185
|
|
|
'table_group': 'evpl', |
|
186
|
|
|
'table_id': 0, |
|
187
|
|
|
'priority': 20000 |
|
188
|
|
|
} |
|
189
|
|
|
]} |
|
190
|
1 |
|
actual_flows = prepare_delete_flow(flow_mod) |
|
191
|
1 |
|
assert '00:01' in actual_flows |
|
192
|
1 |
|
for i in range(len(actual_flows['00:01'])): |
|
193
|
1 |
|
assert (actual_flows['00:01'][i]['cookie'] == |
|
194
|
|
|
flow_mod["00:01"][i]['cookie']) |
|
195
|
1 |
|
assert (actual_flows['00:01'][i]['match'] == |
|
196
|
|
|
flow_mod["00:01"][i]['match']) |
|
197
|
1 |
|
assert actual_flows['00:01'][i]['cookie_mask'] == cookie_mask |
|
198
|
|
|
|
|
199
|
|
|
# pylint: disable=too-many-arguments |
|
200
|
1 |
|
@pytest.mark.parametrize( |
|
201
|
|
|
"intf_a_status, intf_z_status, is_active, is_uni, event, expected", |
|
202
|
|
|
[ |
|
203
|
|
|
# link_DOWN |
|
204
|
|
|
( |
|
205
|
|
|
EntityStatus.DOWN, EntityStatus.DOWN, |
|
206
|
|
|
True, True, 'down', True |
|
207
|
|
|
), |
|
208
|
|
|
( |
|
209
|
|
|
EntityStatus.UP, EntityStatus.UP, |
|
210
|
|
|
True, True, 'down', False |
|
211
|
|
|
), |
|
212
|
|
|
( |
|
213
|
|
|
EntityStatus.DOWN, EntityStatus.UP, |
|
214
|
|
|
False, True, 'down', False |
|
215
|
|
|
), |
|
216
|
|
|
( |
|
217
|
|
|
EntityStatus.UP, EntityStatus.UP, |
|
218
|
|
|
False, True, 'down', False |
|
219
|
|
|
), |
|
220
|
|
|
( # Not UNI |
|
221
|
|
|
EntityStatus.DOWN, EntityStatus.DOWN, |
|
222
|
|
|
True, False, 'down', False |
|
223
|
|
|
), |
|
224
|
|
|
# link_up |
|
225
|
|
|
( |
|
226
|
|
|
EntityStatus.DOWN, EntityStatus.DOWN, |
|
227
|
|
|
True, True, 'up', False |
|
228
|
|
|
), |
|
229
|
|
|
( |
|
230
|
|
|
EntityStatus.UP, EntityStatus.UP, |
|
231
|
|
|
True, True, 'up', False |
|
232
|
|
|
), |
|
233
|
|
|
( |
|
234
|
|
|
EntityStatus.DOWN, EntityStatus.UP, |
|
235
|
|
|
False, True, 'up', False |
|
236
|
|
|
), |
|
237
|
|
|
( |
|
238
|
|
|
EntityStatus.UP, EntityStatus.UP, |
|
239
|
|
|
False, True, 'up', True |
|
240
|
|
|
), |
|
241
|
|
|
( # Not UNI |
|
242
|
|
|
EntityStatus.UP, EntityStatus.UP, |
|
243
|
|
|
False, False, 'up', False |
|
244
|
|
|
), |
|
245
|
|
|
] |
|
246
|
|
|
) |
|
247
|
1 |
|
def test_does_uni_affect_evc( |
|
248
|
|
|
self, |
|
249
|
|
|
intf_a_status, |
|
250
|
|
|
intf_z_status, |
|
251
|
|
|
is_active, |
|
252
|
|
|
is_uni, |
|
253
|
|
|
event, |
|
254
|
|
|
expected |
|
255
|
|
|
): |
|
256
|
|
|
"""Test _does_uni_affect_evc when interface.""" |
|
257
|
1 |
|
evc = Mock() |
|
258
|
1 |
|
evc.uni_a.interface.status = intf_a_status |
|
259
|
1 |
|
evc.uni_z.interface.status = intf_z_status |
|
260
|
1 |
|
if is_uni: |
|
261
|
1 |
|
interface = evc.uni_a.interface |
|
262
|
|
|
else: |
|
263
|
1 |
|
interface = Mock() |
|
264
|
1 |
|
evc.is_active.return_value = is_active |
|
265
|
|
|
assert _does_uni_affect_evc(evc, interface, event) is expected |
|
266
|
|
|
|