|
1
|
|
|
"""Method to thest EVCDeploy class.""" |
|
2
|
|
|
import sys |
|
3
|
|
|
from unittest import TestCase |
|
4
|
|
|
from unittest.mock import Mock, patch |
|
5
|
|
|
|
|
6
|
|
|
from kytos.core.interface import Interface |
|
7
|
|
|
from kytos.core.switch import Switch |
|
8
|
|
|
|
|
9
|
|
|
# pylint: disable=wrong-import-position |
|
10
|
|
|
sys.path.insert(0, '/var/lib/kytos/napps/..') |
|
11
|
|
|
# pylint: enable=wrong-import-position |
|
12
|
|
|
|
|
13
|
|
|
from napps.kytos.mef_eline.models import EVC, Path # NOQA |
|
14
|
|
|
from napps.kytos.mef_eline.settings import MANAGER_URL # NOQA |
|
15
|
|
|
from tests.helpers import get_link_mocked,\ |
|
16
|
|
|
get_uni_mocked, get_controller_mock # NOQA |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
class TestEVC(TestCase): # pylint: disable=too-many-public-methods |
|
20
|
|
|
"""Tests to verify EVC class.""" |
|
21
|
|
|
|
|
22
|
|
|
def test_primary_links_zipped(self): |
|
23
|
|
|
"""Test primary links zipped method.""" |
|
24
|
|
|
|
|
25
|
|
|
@staticmethod |
|
26
|
|
|
@patch('napps.kytos.mef_eline.models.log') |
|
27
|
|
|
def test_should_deploy_case1(log_mock): |
|
28
|
|
|
"""Test should deploy method without primary links.""" |
|
29
|
|
|
log_mock.debug.return_value = True |
|
30
|
|
|
attributes = { |
|
31
|
|
|
"controller": get_controller_mock(), |
|
32
|
|
|
"name": "custom_name", |
|
33
|
|
|
"uni_a": get_uni_mocked(is_valid=True), |
|
34
|
|
|
"uni_z": get_uni_mocked(is_valid=True) |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
evc = EVC(**attributes) |
|
38
|
|
|
evc.should_deploy() |
|
39
|
|
|
log_mock.debug.assert_called_with('Path is empty.') |
|
40
|
|
|
|
|
41
|
|
|
@patch('napps.kytos.mef_eline.models.log') |
|
42
|
|
|
def test_should_deploy_case2(self, log_mock): |
|
43
|
|
|
"""Test should deploy method with disable circuit.""" |
|
44
|
|
|
log_mock.debug.return_value = True |
|
45
|
|
|
attributes = { |
|
46
|
|
|
"controller": get_controller_mock(), |
|
47
|
|
|
"name": "custom_name", |
|
48
|
|
|
"uni_a": get_uni_mocked(is_valid=True), |
|
49
|
|
|
"uni_z": get_uni_mocked(is_valid=True), |
|
50
|
|
|
"primary_links": [get_link_mocked(), get_link_mocked()] |
|
51
|
|
|
} |
|
52
|
|
|
evc = EVC(**attributes) |
|
53
|
|
|
|
|
54
|
|
|
self.assertFalse(evc.should_deploy(attributes['primary_links'])) |
|
55
|
|
|
log_mock.debug.assert_called_with(f'{evc} is disabled.') |
|
56
|
|
|
|
|
57
|
|
|
@patch('napps.kytos.mef_eline.models.log') |
|
58
|
|
|
def test_should_deploy_case3(self, log_mock): |
|
59
|
|
|
"""Test should deploy method with enabled and not active circuit.""" |
|
60
|
|
|
log_mock.debug.return_value = True |
|
61
|
|
|
attributes = { |
|
62
|
|
|
"controller": get_controller_mock(), |
|
63
|
|
|
"name": "custom_name", |
|
64
|
|
|
"uni_a": get_uni_mocked(is_valid=True), |
|
65
|
|
|
"uni_z": get_uni_mocked(is_valid=True), |
|
66
|
|
|
"primary_links": [get_link_mocked(), get_link_mocked()], |
|
67
|
|
|
"enabled": True |
|
68
|
|
|
} |
|
69
|
|
|
evc = EVC(**attributes) |
|
70
|
|
|
self.assertTrue(evc.should_deploy(attributes['primary_links'])) |
|
71
|
|
|
log_mock.debug.assert_called_with(f'{evc} will be deployed.') |
|
72
|
|
|
|
|
73
|
|
|
@patch('napps.kytos.mef_eline.models.log') |
|
74
|
|
|
def test_should_deploy_case4(self, log_mock): |
|
75
|
|
|
"""Test should deploy method with enabled and active circuit.""" |
|
76
|
|
|
log_mock.debug.return_value = True |
|
77
|
|
|
attributes = { |
|
78
|
|
|
"controller": get_controller_mock(), |
|
79
|
|
|
"name": "custom_name", |
|
80
|
|
|
"uni_a": get_uni_mocked(is_valid=True), |
|
81
|
|
|
"uni_z": get_uni_mocked(is_valid=True), |
|
82
|
|
|
"primary_links": [get_link_mocked(), get_link_mocked()], |
|
83
|
|
|
"enabled": True, |
|
84
|
|
|
"active": True |
|
85
|
|
|
} |
|
86
|
|
|
evc = EVC(**attributes) |
|
87
|
|
|
self.assertFalse(evc.should_deploy(attributes['primary_links'])) |
|
88
|
|
|
|
|
89
|
|
|
@patch('napps.kytos.mef_eline.models.requests') |
|
90
|
|
|
def test_send_flow_mods_case1(self, requests_mock): |
|
91
|
|
|
"""Test if you are sending flow_mods.""" |
|
92
|
|
|
flow_mods = {"id": 20} |
|
93
|
|
|
switch = Mock(spec=Switch, id=1) |
|
94
|
|
|
|
|
95
|
|
|
# pylint: disable=protected-access |
|
96
|
|
|
EVC._send_flow_mods(switch, flow_mods) |
|
97
|
|
|
|
|
98
|
|
|
expected_endpoint = f"{MANAGER_URL}/flows/{switch.id}" |
|
99
|
|
|
expected_data = {"flows": flow_mods} |
|
100
|
|
|
self.assertEqual(requests_mock.post.call_count, 1) |
|
101
|
|
|
requests_mock.post.assert_called_once_with(expected_endpoint, |
|
102
|
|
|
json=expected_data) |
|
103
|
|
|
|
|
104
|
|
|
@patch('napps.kytos.mef_eline.models.requests') |
|
105
|
|
|
def test_send_flow_mods_case2(self, requests_mock): |
|
106
|
|
|
"""Test if you are sending flow_mods.""" |
|
107
|
|
|
flow_mods = {"id": 20} |
|
108
|
|
|
switch = Mock(spec=Switch, id=1) |
|
109
|
|
|
|
|
110
|
|
|
# pylint: disable=protected-access |
|
111
|
|
|
EVC._send_flow_mods(switch, flow_mods, command='delete') |
|
112
|
|
|
|
|
113
|
|
|
expected_endpoint = f"{MANAGER_URL}/delete/{switch.id}" |
|
114
|
|
|
expected_data = {"flows": flow_mods} |
|
115
|
|
|
self.assertEqual(requests_mock.post.call_count, 1) |
|
116
|
|
|
requests_mock.post.assert_called_once_with(expected_endpoint, |
|
117
|
|
|
json=expected_data) |
|
118
|
|
|
|
|
119
|
|
|
def test_prepare_flow_mod(self): |
|
120
|
|
|
"""Test prepare flow_mod method.""" |
|
121
|
|
|
interface_a = Interface('eth0', 1, Mock(spec=Switch)) |
|
122
|
|
|
interface_z = Interface('eth1', 3, Mock(spec=Switch)) |
|
123
|
|
|
attributes = { |
|
124
|
|
|
"controller": get_controller_mock(), |
|
125
|
|
|
"name": "custom_name", |
|
126
|
|
|
"uni_a": get_uni_mocked(is_valid=True), |
|
127
|
|
|
"uni_z": get_uni_mocked(is_valid=True), |
|
128
|
|
|
"primary_links": [get_link_mocked(), get_link_mocked()], |
|
129
|
|
|
"enabled": True, |
|
130
|
|
|
"active": True |
|
131
|
|
|
} |
|
132
|
|
|
evc = EVC(**attributes) |
|
133
|
|
|
|
|
134
|
|
|
# pylint: disable=protected-access |
|
135
|
|
|
flow_mod = evc._prepare_flow_mod(interface_a, interface_z) |
|
136
|
|
|
expected_flow_mod = { |
|
137
|
|
|
'match': {'in_port': interface_a.port_number}, |
|
138
|
|
|
'cookie': evc.get_cookie(), |
|
139
|
|
|
'actions': [ |
|
140
|
|
|
{'action_type': 'output', |
|
141
|
|
|
'port': interface_z.port_number} |
|
142
|
|
|
] |
|
143
|
|
|
} |
|
144
|
|
|
self.assertEqual(expected_flow_mod, flow_mod) |
|
145
|
|
|
|
|
146
|
|
|
def test_prepare_pop_flow(self): |
|
147
|
|
|
"""Test prepare pop flow method.""" |
|
148
|
|
|
attributes = { |
|
149
|
|
|
"controller": get_controller_mock(), |
|
150
|
|
|
"name": "custom_name", |
|
151
|
|
|
"uni_a": get_uni_mocked(interface_port=1, is_valid=True), |
|
152
|
|
|
"uni_z": get_uni_mocked(interface_port=2, is_valid=True), |
|
153
|
|
|
} |
|
154
|
|
|
evc = EVC(**attributes) |
|
155
|
|
|
interface_a = evc.uni_a.interface |
|
156
|
|
|
interface_z = evc.uni_z.interface |
|
157
|
|
|
in_vlan = 10 |
|
158
|
|
|
|
|
159
|
|
|
# pylint: disable=protected-access |
|
160
|
|
|
flow_mod = evc._prepare_pop_flow(interface_a, interface_z, in_vlan) |
|
161
|
|
|
|
|
162
|
|
|
expected_flow_mod = { |
|
163
|
|
|
'match': {'in_port': interface_a.port_number, 'dl_vlan': in_vlan}, |
|
164
|
|
|
'cookie': evc.get_cookie(), |
|
165
|
|
|
'actions': [ |
|
166
|
|
|
{'action_type': 'pop_vlan'}, |
|
167
|
|
|
{'action_type': 'output', |
|
168
|
|
|
'port': interface_z.port_number |
|
169
|
|
|
} |
|
170
|
|
|
] |
|
171
|
|
|
} |
|
172
|
|
|
self.assertEqual(expected_flow_mod, flow_mod) |
|
173
|
|
|
|
|
174
|
|
|
def test_prepare_push_flow(self): |
|
175
|
|
|
"""Test prepare push flow method.""" |
|
176
|
|
|
attributes = { |
|
177
|
|
|
"controller": get_controller_mock(), |
|
178
|
|
|
"name": "custom_name", |
|
179
|
|
|
"uni_a": get_uni_mocked(interface_port=1, is_valid=True), |
|
180
|
|
|
"uni_z": get_uni_mocked(interface_port=2, is_valid=True), |
|
181
|
|
|
} |
|
182
|
|
|
evc = EVC(**attributes) |
|
183
|
|
|
interface_a = evc.uni_a.interface |
|
184
|
|
|
interface_z = evc.uni_z.interface |
|
185
|
|
|
out_vlan_a = 20 |
|
186
|
|
|
|
|
187
|
|
|
for in_vlan_a in (10, None): |
|
188
|
|
|
for in_vlan_z in (3, None): |
|
189
|
|
|
with self.subTest(in_vlan_a=in_vlan_a, in_vlan_z=in_vlan_z): |
|
190
|
|
|
# pylint: disable=protected-access |
|
191
|
|
|
flow_mod = evc._prepare_push_flow(interface_a, interface_z, |
|
192
|
|
|
in_vlan_a, out_vlan_a, |
|
193
|
|
|
in_vlan_z) |
|
194
|
|
|
|
|
195
|
|
|
expected_flow_mod = { |
|
196
|
|
|
'match': {'in_port': interface_a.port_number}, |
|
197
|
|
|
'cookie': evc.get_cookie(), |
|
198
|
|
|
'actions': [ |
|
199
|
|
|
{'action_type': 'push_vlan', 'tag_type': 's'}, |
|
200
|
|
|
{'action_type': 'set_vlan', 'vlan_id': out_vlan_a}, |
|
201
|
|
|
{ |
|
202
|
|
|
'action_type': 'output', |
|
203
|
|
|
'port': interface_z.port_number |
|
204
|
|
|
} |
|
205
|
|
|
] |
|
206
|
|
|
} |
|
207
|
|
|
if in_vlan_a and in_vlan_z: |
|
208
|
|
|
expected_flow_mod['match']['dl_vlan'] = in_vlan_a |
|
209
|
|
|
expected_flow_mod['actions'].insert(0, { |
|
210
|
|
|
'action_type': 'set_vlan', 'vlan_id': in_vlan_z |
|
211
|
|
|
}) |
|
212
|
|
|
elif in_vlan_a: |
|
213
|
|
|
expected_flow_mod['match']['dl_vlan'] = in_vlan_a |
|
214
|
|
|
expected_flow_mod['actions'].insert(0, { |
|
215
|
|
|
'action_type': 'pop_vlan' |
|
216
|
|
|
}) |
|
217
|
|
|
elif in_vlan_z: |
|
218
|
|
|
expected_flow_mod['actions'].insert(0, { |
|
219
|
|
|
'action_type': 'set_vlan', 'vlan_id': in_vlan_z |
|
220
|
|
|
}) |
|
221
|
|
|
expected_flow_mod['actions'].insert(0, { |
|
222
|
|
|
'action_type': 'push_vlan', 'tag_type': 'c' |
|
223
|
|
|
}) |
|
224
|
|
|
self.assertEqual(expected_flow_mod, flow_mod) |
|
225
|
|
|
|
|
226
|
|
|
@staticmethod |
|
227
|
|
|
@patch('napps.kytos.mef_eline.models.EVC._send_flow_mods') |
|
228
|
|
|
def test_install_uni_flows(send_flow_mods_mock): |
|
229
|
|
|
"""Test install uni flows method. |
|
230
|
|
|
|
|
231
|
|
|
This test will verify the flows send to the send_flow_mods method. |
|
232
|
|
|
""" |
|
233
|
|
|
uni_a = get_uni_mocked(interface_port=2, tag_value=82, |
|
234
|
|
|
switch_id="switch_uni_a", is_valid=True) |
|
235
|
|
|
uni_z = get_uni_mocked(interface_port=3, tag_value=83, |
|
236
|
|
|
switch_id="switch_uni_z", is_valid=True) |
|
237
|
|
|
|
|
238
|
|
|
attributes = { |
|
239
|
|
|
"controller": get_controller_mock(), |
|
240
|
|
|
"name": "custom_name", |
|
241
|
|
|
"uni_a": uni_a, |
|
242
|
|
|
"uni_z": uni_z, |
|
243
|
|
|
"primary_links": [ |
|
244
|
|
|
get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, |
|
245
|
|
|
metadata={"s_vlan": 5}), |
|
246
|
|
|
get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, |
|
247
|
|
|
metadata={"s_vlan": 6}) |
|
248
|
|
|
] |
|
249
|
|
|
} |
|
250
|
|
|
evc = EVC(**attributes) |
|
251
|
|
|
|
|
252
|
|
|
# pylint: disable=protected-access |
|
253
|
|
|
evc._install_uni_flows(attributes['primary_links']) |
|
254
|
|
|
|
|
255
|
|
|
expected_flow_mod_a = [ |
|
256
|
|
|
{'match': {'in_port': uni_a.interface.port_number, |
|
257
|
|
|
'dl_vlan': uni_a.user_tag.value}, |
|
258
|
|
|
'cookie': evc.get_cookie(), |
|
259
|
|
|
'actions': [ |
|
260
|
|
|
{'action_type': 'set_vlan', 'vlan_id': uni_z.user_tag.value}, |
|
261
|
|
|
{'action_type': 'push_vlan', 'tag_type': 's'}, |
|
262
|
|
|
{'action_type': 'set_vlan', |
|
263
|
|
|
'vlan_id': evc.primary_links[0].get_metadata('s_vlan').value}, |
|
264
|
|
|
{'action_type': 'output', |
|
265
|
|
|
'port': evc.primary_links[0].endpoint_a.port_number} |
|
266
|
|
|
]}, |
|
267
|
|
|
{'match': { |
|
268
|
|
|
'in_port': evc.primary_links[0].endpoint_a.port_number, |
|
269
|
|
|
'dl_vlan': evc.primary_links[0].get_metadata('s_vlan').value |
|
270
|
|
|
}, |
|
271
|
|
|
'cookie': evc.get_cookie(), |
|
272
|
|
|
'actions': [ |
|
273
|
|
|
{'action_type': 'pop_vlan'}, |
|
274
|
|
|
{'action_type': 'output', 'port': uni_a.interface.port_number} |
|
275
|
|
|
] |
|
276
|
|
|
} |
|
277
|
|
|
] |
|
278
|
|
|
send_flow_mods_mock.assert_any_call(uni_a.interface.switch, |
|
279
|
|
|
expected_flow_mod_a) |
|
280
|
|
|
|
|
281
|
|
|
expected_flow_mod_z = [ |
|
282
|
|
|
{'match': {'in_port': uni_z.interface.port_number, |
|
283
|
|
|
'dl_vlan': uni_z.user_tag.value}, |
|
284
|
|
|
'cookie': evc.get_cookie(), |
|
285
|
|
|
'actions': [ |
|
286
|
|
|
{'action_type': 'set_vlan', 'vlan_id': uni_a.user_tag.value}, |
|
287
|
|
|
{'action_type': 'push_vlan', 'tag_type': 's'}, |
|
288
|
|
|
{'action_type': 'set_vlan', |
|
289
|
|
|
'vlan_id': evc.primary_links[-1].get_metadata('s_vlan').value |
|
290
|
|
|
}, |
|
291
|
|
|
{'action_type': 'output', |
|
292
|
|
|
'port': evc.primary_links[-1].endpoint_b.port_number} |
|
293
|
|
|
] |
|
294
|
|
|
}, |
|
295
|
|
|
{'match': { |
|
296
|
|
|
'in_port': evc.primary_links[-1].endpoint_b.port_number, |
|
297
|
|
|
'dl_vlan': evc.primary_links[-1].get_metadata('s_vlan').value |
|
298
|
|
|
}, |
|
299
|
|
|
'cookie': evc.get_cookie(), |
|
300
|
|
|
'actions': [ |
|
301
|
|
|
{'action_type': 'pop_vlan'}, |
|
302
|
|
|
{'action_type': 'output', 'port': uni_z.interface.port_number} |
|
303
|
|
|
] |
|
304
|
|
|
} |
|
305
|
|
|
] |
|
306
|
|
|
|
|
307
|
|
|
send_flow_mods_mock.assert_any_call(uni_z.interface.switch, |
|
308
|
|
|
expected_flow_mod_z) |
|
309
|
|
|
|
|
310
|
|
|
@staticmethod |
|
311
|
|
|
@patch('napps.kytos.mef_eline.models.EVC._send_flow_mods') |
|
312
|
|
|
def test_install_nni_flows(send_flow_mods_mock): |
|
313
|
|
|
"""Test install nni flows method. |
|
314
|
|
|
|
|
315
|
|
|
This test will verify the flows send to the send_flow_mods method. |
|
316
|
|
|
""" |
|
317
|
|
|
uni_a = get_uni_mocked(interface_port=2, tag_value=82, |
|
318
|
|
|
switch_id="switch_uni_a", is_valid=True) |
|
319
|
|
|
uni_z = get_uni_mocked(interface_port=3, tag_value=83, |
|
320
|
|
|
switch_id="switch_uni_z", is_valid=True) |
|
321
|
|
|
|
|
322
|
|
|
attributes = { |
|
323
|
|
|
"controller": get_controller_mock(), |
|
324
|
|
|
"name": "custom_name", |
|
325
|
|
|
"uni_a": uni_a, |
|
326
|
|
|
"uni_z": uni_z, |
|
327
|
|
|
"primary_links": [ |
|
328
|
|
|
get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, |
|
329
|
|
|
metadata={"s_vlan": 5}), |
|
330
|
|
|
get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, |
|
331
|
|
|
metadata={"s_vlan": 6}) |
|
332
|
|
|
] |
|
333
|
|
|
} |
|
334
|
|
|
evc = EVC(**attributes) |
|
335
|
|
|
|
|
336
|
|
|
# pylint: disable=protected-access |
|
337
|
|
|
evc._install_nni_flows(attributes['primary_links']) |
|
338
|
|
|
|
|
339
|
|
|
in_vlan = evc.primary_links[0].get_metadata('s_vlan').value |
|
340
|
|
|
out_vlan = evc.primary_links[-1].get_metadata('s_vlan').value |
|
341
|
|
|
|
|
342
|
|
|
in_port = evc.primary_links[0].endpoint_b.port_number |
|
343
|
|
|
out_port = evc.primary_links[-1].endpoint_a.port_number |
|
344
|
|
|
|
|
345
|
|
|
expected_flow_mods = [ |
|
346
|
|
|
{ |
|
347
|
|
|
'match': {'in_port': in_port, 'dl_vlan': in_vlan}, |
|
348
|
|
|
'cookie': evc.get_cookie(), |
|
349
|
|
|
'actions': [ |
|
350
|
|
|
{'action_type': 'set_vlan', 'vlan_id': out_vlan}, |
|
351
|
|
|
{'action_type': 'output', 'port': out_port} |
|
352
|
|
|
] |
|
353
|
|
|
}, |
|
354
|
|
|
{ |
|
355
|
|
|
'match': {'in_port': out_port, 'dl_vlan': out_vlan}, |
|
356
|
|
|
'cookie': evc.get_cookie(), |
|
357
|
|
|
'actions': [ |
|
358
|
|
|
{'action_type': 'set_vlan', 'vlan_id': in_vlan}, |
|
359
|
|
|
{'action_type': 'output', 'port': in_port} |
|
360
|
|
|
] |
|
361
|
|
|
} |
|
362
|
|
|
] |
|
363
|
|
|
|
|
364
|
|
|
switch = evc.primary_links[0].endpoint_b.switch |
|
365
|
|
|
send_flow_mods_mock.assert_called_once_with(switch, expected_flow_mods) |
|
366
|
|
|
|
|
367
|
|
|
@patch('napps.kytos.mef_eline.models.log') |
|
368
|
|
|
@patch('napps.kytos.mef_eline.models.Path.choose_vlans') |
|
369
|
|
|
@patch('napps.kytos.mef_eline.models.EVC._install_nni_flows') |
|
370
|
|
|
@patch('napps.kytos.mef_eline.models.EVC._install_uni_flows') |
|
371
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.activate') |
|
372
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.should_deploy') |
|
373
|
|
|
def test_deploy_successfully(self, *args): |
|
374
|
|
|
"""Test if all methods to deploy are called.""" |
|
375
|
|
|
# pylint: disable=too-many-locals |
|
376
|
|
|
(should_deploy_mock, activate_mock, |
|
377
|
|
|
install_uni_flows_mock, install_nni_flows, chose_vlans_mock, |
|
378
|
|
|
log_mock) = args |
|
379
|
|
|
|
|
380
|
|
|
should_deploy_mock.return_value = True |
|
381
|
|
|
uni_a = get_uni_mocked(interface_port=2, tag_value=82, |
|
382
|
|
|
switch_id="switch_uni_a", is_valid=True) |
|
383
|
|
|
uni_z = get_uni_mocked(interface_port=3, tag_value=83, |
|
384
|
|
|
switch_id="switch_uni_z", is_valid=True) |
|
385
|
|
|
|
|
386
|
|
|
primary_links = [ |
|
387
|
|
|
get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, |
|
388
|
|
|
metadata={"s_vlan": 5}), |
|
389
|
|
|
get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, |
|
390
|
|
|
metadata={"s_vlan": 6}) |
|
391
|
|
|
] |
|
392
|
|
|
|
|
393
|
|
|
attributes = { |
|
394
|
|
|
"controller": get_controller_mock(), |
|
395
|
|
|
"name": "custom_name", |
|
396
|
|
|
"uni_a": uni_a, |
|
397
|
|
|
"uni_z": uni_z, |
|
398
|
|
|
"primary_links": primary_links |
|
399
|
|
|
} |
|
400
|
|
|
# Setup path to deploy |
|
401
|
|
|
path = Path() |
|
402
|
|
|
path.append(primary_links[0]) |
|
403
|
|
|
path.append(primary_links[1]) |
|
404
|
|
|
|
|
405
|
|
|
evc = EVC(**attributes) |
|
406
|
|
|
|
|
407
|
|
|
# storehouse mock |
|
408
|
|
|
evc._storehouse.box = Mock() # pylint: disable=protected-access |
|
409
|
|
|
evc._storehouse.box.data = {} # pylint: disable=protected-access |
|
410
|
|
|
|
|
411
|
|
|
deployed = evc.deploy_to_path(path) |
|
412
|
|
|
|
|
413
|
|
|
self.assertEqual(should_deploy_mock.call_count, 1) |
|
414
|
|
|
self.assertEqual(activate_mock.call_count, 1) |
|
415
|
|
|
self.assertEqual(install_uni_flows_mock.call_count, 1) |
|
416
|
|
|
self.assertEqual(install_nni_flows.call_count, 1) |
|
417
|
|
|
self.assertEqual(chose_vlans_mock.call_count, 1) |
|
418
|
|
|
log_mock.info.assert_called_with(f"{evc} was deployed.") |
|
419
|
|
|
self.assertTrue(deployed) |
|
420
|
|
|
|
|
421
|
|
|
@patch('napps.kytos.mef_eline.models.log') |
|
422
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.discover_new_path', |
|
423
|
|
|
return_value=None) |
|
424
|
|
|
@patch('napps.kytos.mef_eline.models.Path.choose_vlans') |
|
425
|
|
|
@patch('napps.kytos.mef_eline.models.EVC._install_nni_flows') |
|
426
|
|
|
@patch('napps.kytos.mef_eline.models.EVC._install_uni_flows') |
|
427
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.activate') |
|
428
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.should_deploy', |
|
429
|
|
|
return_value=False) |
|
430
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.sync') |
|
431
|
|
|
def test_deploy_fail(self, *args): |
|
432
|
|
|
"""Test if all methods is ignored when the should_deploy is false.""" |
|
433
|
|
|
# pylint: disable=too-many-locals |
|
434
|
|
|
(sync_mock, should_deploy_mock, activate_mock, install_uni_flows_mock, |
|
435
|
|
|
install_nni_flows, chose_vlans_mock, |
|
436
|
|
|
discover_new_path, log_mock) = args |
|
437
|
|
|
|
|
438
|
|
|
uni_a = get_uni_mocked(interface_port=2, tag_value=82, |
|
439
|
|
|
switch_id="switch_uni_a", |
|
440
|
|
|
switch_dpid="switch_dpid_uni_a", |
|
441
|
|
|
is_valid=True) |
|
442
|
|
|
uni_z = get_uni_mocked(interface_port=3, tag_value=83, |
|
443
|
|
|
switch_id="switch_uni_z", |
|
444
|
|
|
switch_dpid="switch_dpid_uni_a", |
|
445
|
|
|
is_valid=True) |
|
446
|
|
|
|
|
447
|
|
|
attributes = { |
|
448
|
|
|
"controller": get_controller_mock(), |
|
449
|
|
|
"name": "custom_name", |
|
450
|
|
|
"uni_a": uni_a, |
|
451
|
|
|
"uni_z": uni_z, |
|
452
|
|
|
"primary_links": [ |
|
453
|
|
|
get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, |
|
454
|
|
|
metadata={"s_vlan": 5}), |
|
455
|
|
|
get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, |
|
456
|
|
|
metadata={"s_vlan": 6}) |
|
457
|
|
|
] |
|
458
|
|
|
} |
|
459
|
|
|
|
|
460
|
|
|
evc = EVC(**attributes) |
|
461
|
|
|
deployed = evc.deploy_to_path() |
|
462
|
|
|
|
|
463
|
|
|
self.assertEqual(discover_new_path.call_count, 1) |
|
464
|
|
|
self.assertEqual(should_deploy_mock.call_count, 1) |
|
465
|
|
|
self.assertEqual(activate_mock.call_count, 0) |
|
466
|
|
|
self.assertEqual(install_uni_flows_mock.call_count, 0) |
|
467
|
|
|
self.assertEqual(install_nni_flows.call_count, 0) |
|
468
|
|
|
self.assertEqual(chose_vlans_mock.call_count, 0) |
|
469
|
|
|
self.assertEqual(log_mock.info.call_count, 0) |
|
470
|
|
|
self.assertEqual(sync_mock.call_count, 1) |
|
471
|
|
|
self.assertFalse(deployed) |
|
472
|
|
|
|
|
473
|
|
|
@patch('napps.kytos.mef_eline.models.log') |
|
474
|
|
|
@patch('napps.kytos.mef_eline.models.Path.choose_vlans') |
|
475
|
|
|
@patch('napps.kytos.mef_eline.models.EVC._install_nni_flows') |
|
476
|
|
|
@patch('napps.kytos.mef_eline.models.EVC._install_uni_flows') |
|
477
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.activate') |
|
478
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.should_deploy') |
|
479
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.discover_new_path') |
|
480
|
|
|
def test_deploy_without_path_case1(self, *args): |
|
481
|
|
|
"""Test if not path is found a dynamic path is used.""" |
|
482
|
|
|
# pylint: disable=too-many-locals |
|
483
|
|
|
(discover_new_path_mocked, should_deploy_mock, activate_mock, |
|
484
|
|
|
install_uni_flows_mock, install_nni_flows, chose_vlans_mock, |
|
485
|
|
|
log_mock) = args |
|
486
|
|
|
|
|
487
|
|
|
should_deploy_mock.return_value = False |
|
488
|
|
|
uni_a = get_uni_mocked(interface_port=2, tag_value=82, |
|
489
|
|
|
switch_id="switch_uni_a", is_valid=True) |
|
490
|
|
|
uni_z = get_uni_mocked(interface_port=3, tag_value=83, |
|
491
|
|
|
switch_id="switch_uni_z", is_valid=True) |
|
492
|
|
|
|
|
493
|
|
|
attributes = { |
|
494
|
|
|
"controller": get_controller_mock(), |
|
495
|
|
|
"name": "custom_name", |
|
496
|
|
|
"uni_a": uni_a, |
|
497
|
|
|
"uni_z": uni_z, |
|
498
|
|
|
"enabled": True, |
|
499
|
|
|
"dynamic_backup_path": True |
|
500
|
|
|
} |
|
501
|
|
|
|
|
502
|
|
|
dynamic_backup_path = Path([ |
|
503
|
|
|
get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, |
|
504
|
|
|
metadata={"s_vlan": 5}), |
|
505
|
|
|
get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, |
|
506
|
|
|
metadata={"s_vlan": 6}) |
|
507
|
|
|
]) |
|
508
|
|
|
|
|
509
|
|
|
evc = EVC(**attributes) |
|
510
|
|
|
discover_new_path_mocked.return_value = dynamic_backup_path |
|
511
|
|
|
|
|
512
|
|
|
# storehouse initialization mock |
|
513
|
|
|
evc._storehouse.box = Mock() # pylint: disable=protected-access |
|
514
|
|
|
evc._storehouse.box.data = {} # pylint: disable=protected-access |
|
515
|
|
|
|
|
516
|
|
|
deployed = evc.deploy_to_path() |
|
517
|
|
|
|
|
518
|
|
|
self.assertEqual(should_deploy_mock.call_count, 1) |
|
519
|
|
|
self.assertEqual(discover_new_path_mocked.call_count, 1) |
|
520
|
|
|
self.assertEqual(activate_mock.call_count, 1) |
|
521
|
|
|
self.assertEqual(install_uni_flows_mock.call_count, 1) |
|
522
|
|
|
self.assertEqual(install_nni_flows.call_count, 1) |
|
523
|
|
|
self.assertEqual(chose_vlans_mock.call_count, 1) |
|
524
|
|
|
log_mock.info.assert_called_with(f"{evc} was deployed.") |
|
525
|
|
|
self.assertTrue(deployed) |
|
526
|
|
|
|
|
527
|
|
|
@patch('napps.kytos.mef_eline.models.EVC._send_flow_mods') |
|
528
|
|
|
def test_remove_current_flows(self, send_flow_mods_mocked): |
|
529
|
|
|
"""Test remove current flows.""" |
|
530
|
|
|
uni_a = get_uni_mocked(interface_port=2, tag_value=82, |
|
531
|
|
|
switch_id="switch_uni_a", is_valid=True) |
|
532
|
|
|
uni_z = get_uni_mocked(interface_port=3, tag_value=83, |
|
533
|
|
|
switch_id="switch_uni_z", is_valid=True) |
|
534
|
|
|
|
|
535
|
|
|
switch_a = Switch('00:00:00:00:00:01') |
|
536
|
|
|
switch_b = Switch('00:00:00:00:00:02') |
|
537
|
|
|
switch_c = Switch('00:00:00:00:00:03') |
|
538
|
|
|
|
|
539
|
|
|
attributes = { |
|
540
|
|
|
"controller": get_controller_mock(), |
|
541
|
|
|
"name": "custom_name", |
|
542
|
|
|
"uni_a": uni_a, |
|
543
|
|
|
"uni_z": uni_z, |
|
544
|
|
|
"active": True, |
|
545
|
|
|
"enabled": True, |
|
546
|
|
|
"primary_links": [ |
|
547
|
|
|
get_link_mocked(switch_a=switch_a, |
|
548
|
|
|
switch_b=switch_b, |
|
549
|
|
|
endpoint_a_port=9, endpoint_b_port=10, |
|
550
|
|
|
metadata={"s_vlan": 5}), |
|
551
|
|
|
get_link_mocked(switch_a=switch_b, |
|
552
|
|
|
switch_b=switch_c, |
|
553
|
|
|
endpoint_a_port=11, endpoint_b_port=12, |
|
554
|
|
|
metadata={"s_vlan": 6}) |
|
555
|
|
|
] |
|
556
|
|
|
} |
|
557
|
|
|
|
|
558
|
|
|
evc = EVC(**attributes) |
|
559
|
|
|
|
|
560
|
|
|
# storehouse initialization mock |
|
561
|
|
|
evc._storehouse.box = Mock() # pylint: disable=protected-access |
|
562
|
|
|
evc._storehouse.box.data = {} # pylint: disable=protected-access |
|
563
|
|
|
|
|
564
|
|
|
evc.current_path = evc.primary_links |
|
565
|
|
|
evc.remove_current_flows() |
|
566
|
|
|
|
|
567
|
|
|
self.assertEqual(send_flow_mods_mocked.call_count, 3) |
|
568
|
|
|
self.assertFalse(evc.is_active()) |
|
569
|
|
|
flows = [{'cookie': evc.get_cookie(), |
|
570
|
|
|
'cookie_mask': 18446744073709551615}] |
|
571
|
|
|
switch_1 = evc.primary_links[0].endpoint_a.switch |
|
572
|
|
|
switch_2 = evc.primary_links[0].endpoint_b.switch |
|
573
|
|
|
send_flow_mods_mocked.assert_any_call(switch_1, flows, 'delete') |
|
574
|
|
|
send_flow_mods_mocked.assert_any_call(switch_2, flows, 'delete') |
|
575
|
|
|
|