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, |
161
|
|
|
None, in_vlan) |
162
|
|
|
|
163
|
|
|
expected_flow_mod = { |
164
|
|
|
'match': {'in_port': interface_a.port_number, 'dl_vlan': in_vlan}, |
165
|
|
|
'cookie': evc.get_cookie(), |
166
|
|
|
'actions': [ |
167
|
|
|
{'action_type': 'pop_vlan'}, |
168
|
|
|
{'action_type': 'output', |
169
|
|
|
'port': interface_z.port_number |
170
|
|
|
} |
171
|
|
|
] |
172
|
|
|
} |
173
|
|
|
self.assertEqual(expected_flow_mod, flow_mod) |
174
|
|
|
|
175
|
|
|
def test_prepare_push_flow(self): |
176
|
|
|
"""Test prepare push flow method.""" |
177
|
|
|
attributes = { |
178
|
|
|
"controller": get_controller_mock(), |
179
|
|
|
"name": "custom_name", |
180
|
|
|
"uni_a": get_uni_mocked(interface_port=1, is_valid=True), |
181
|
|
|
"uni_z": get_uni_mocked(interface_port=2, is_valid=True), |
182
|
|
|
} |
183
|
|
|
evc = EVC(**attributes) |
184
|
|
|
interface_a = evc.uni_a.interface |
185
|
|
|
interface_z = evc.uni_z.interface |
186
|
|
|
out_vlan_a = 20 |
187
|
|
|
|
188
|
|
|
for in_vlan_a in (10, None): |
189
|
|
|
with self.subTest(in_vlan_a=in_vlan_a): |
190
|
|
|
# pylint: disable=protected-access |
191
|
|
|
flow_mod = evc._prepare_push_flow(interface_a, interface_z, |
192
|
|
|
in_vlan_a, out_vlan_a) |
193
|
|
|
|
194
|
|
|
expected_flow_mod = { |
195
|
|
|
'match': {'in_port': interface_a.port_number}, |
196
|
|
|
'cookie': evc.get_cookie(), |
197
|
|
|
'actions': [ |
198
|
|
|
{'action_type': 'push_vlan', 'tag_type': 's'}, |
199
|
|
|
{'action_type': 'set_vlan', 'vlan_id': out_vlan_a}, |
200
|
|
|
{ |
201
|
|
|
'action_type': 'output', |
202
|
|
|
'port': interface_z.port_number |
203
|
|
|
} |
204
|
|
|
] |
205
|
|
|
} |
206
|
|
|
if in_vlan_a: |
207
|
|
|
expected_flow_mod['match']['dl_vlan'] = in_vlan_a |
208
|
|
|
expected_flow_mod['actions'].insert(0, { |
209
|
|
|
'action_type': 'pop_vlan' |
210
|
|
|
}) |
211
|
|
|
self.assertEqual(expected_flow_mod, flow_mod) |
212
|
|
|
|
213
|
|
|
@staticmethod |
214
|
|
|
@patch('napps.kytos.mef_eline.models.EVC._send_flow_mods') |
215
|
|
|
def test_install_uni_flows(send_flow_mods_mock): |
216
|
|
|
"""Test install uni flows method. |
217
|
|
|
|
218
|
|
|
This test will verify the flows send to the send_flow_mods method. |
219
|
|
|
""" |
220
|
|
|
uni_a = get_uni_mocked(interface_port=2, tag_value=82, |
221
|
|
|
switch_id="switch_uni_a", is_valid=True) |
222
|
|
|
uni_z = get_uni_mocked(interface_port=3, tag_value=83, |
223
|
|
|
switch_id="switch_uni_z", is_valid=True) |
224
|
|
|
|
225
|
|
|
attributes = { |
226
|
|
|
"controller": get_controller_mock(), |
227
|
|
|
"name": "custom_name", |
228
|
|
|
"uni_a": uni_a, |
229
|
|
|
"uni_z": uni_z, |
230
|
|
|
"primary_links": [ |
231
|
|
|
get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, |
232
|
|
|
metadata={"s_vlan": 5}), |
233
|
|
|
get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, |
234
|
|
|
metadata={"s_vlan": 6}) |
235
|
|
|
] |
236
|
|
|
} |
237
|
|
|
evc = EVC(**attributes) |
238
|
|
|
|
239
|
|
|
# pylint: disable=protected-access |
240
|
|
|
evc._install_uni_flows(attributes['primary_links']) |
241
|
|
|
|
242
|
|
|
expected_flow_mod_a = [ |
243
|
|
|
{'match': {'in_port': uni_a.interface.port_number, |
244
|
|
|
'dl_vlan': uni_a.user_tag.value}, |
245
|
|
|
'cookie': evc.get_cookie(), |
246
|
|
|
'actions': [ |
247
|
|
|
{'action_type': 'pop_vlan'}, |
248
|
|
|
{'action_type': 'push_vlan', 'tag_type': 's'}, |
249
|
|
|
{'action_type': 'set_vlan', |
250
|
|
|
'vlan_id': evc.primary_links[0].get_metadata('s_vlan').value}, |
251
|
|
|
{'action_type': 'output', |
252
|
|
|
'port': evc.primary_links[0].endpoint_a.port_number} |
253
|
|
|
]}, |
254
|
|
|
{'match': { |
255
|
|
|
'in_port': evc.primary_links[0].endpoint_a.port_number, |
256
|
|
|
'dl_vlan': evc.primary_links[0].get_metadata('s_vlan').value |
257
|
|
|
}, |
258
|
|
|
'cookie': evc.get_cookie(), |
259
|
|
|
'actions': [ |
260
|
|
|
{'action_type': 'pop_vlan'}, |
261
|
|
|
{'action_type': 'push_vlan', 'tag_type': 'c'}, |
262
|
|
|
{'action_type': 'set_vlan', 'vlan_id': uni_a.user_tag.value}, |
263
|
|
|
{'action_type': 'output', 'port': uni_a.interface.port_number} |
264
|
|
|
] |
265
|
|
|
} |
266
|
|
|
] |
267
|
|
|
|
268
|
|
|
send_flow_mods_mock.assert_any_call(uni_a.interface.switch, |
269
|
|
|
expected_flow_mod_a) |
270
|
|
|
|
271
|
|
|
expected_flow_mod_z = [ |
272
|
|
|
{'match': {'in_port': uni_z.interface.port_number, |
273
|
|
|
'dl_vlan': uni_z.user_tag.value}, |
274
|
|
|
'cookie': evc.get_cookie(), |
275
|
|
|
'actions': [ |
276
|
|
|
{'action_type': 'pop_vlan'}, |
277
|
|
|
{'action_type': 'push_vlan', 'tag_type': 's'}, |
278
|
|
|
{'action_type': 'set_vlan', |
279
|
|
|
'vlan_id': evc.primary_links[-1].get_metadata('s_vlan').value |
280
|
|
|
}, |
281
|
|
|
{'action_type': 'output', |
282
|
|
|
'port': evc.primary_links[-1].endpoint_b.port_number} |
283
|
|
|
] |
284
|
|
|
}, |
285
|
|
|
{'match': { |
286
|
|
|
'in_port': evc.primary_links[-1].endpoint_b.port_number, |
287
|
|
|
'dl_vlan': evc.primary_links[-1].get_metadata('s_vlan').value |
288
|
|
|
}, |
289
|
|
|
'cookie': evc.get_cookie(), |
290
|
|
|
'actions': [ |
291
|
|
|
{'action_type': 'pop_vlan'}, |
292
|
|
|
{'action_type': 'push_vlan', 'tag_type': 'c'}, |
293
|
|
|
{'action_type': 'set_vlan', 'vlan_id': uni_z.user_tag.value}, |
294
|
|
|
{'action_type': 'output', 'port': uni_z.interface.port_number} |
295
|
|
|
] |
296
|
|
|
} |
297
|
|
|
] |
298
|
|
|
|
299
|
|
|
send_flow_mods_mock.assert_any_call(uni_z.interface.switch, |
300
|
|
|
expected_flow_mod_z) |
301
|
|
|
|
302
|
|
|
@staticmethod |
303
|
|
|
@patch('napps.kytos.mef_eline.models.EVC._send_flow_mods') |
304
|
|
|
def test_install_nni_flows(send_flow_mods_mock): |
305
|
|
|
"""Test install nni flows method. |
306
|
|
|
|
307
|
|
|
This test will verify the flows send to the send_flow_mods method. |
308
|
|
|
""" |
309
|
|
|
uni_a = get_uni_mocked(interface_port=2, tag_value=82, |
310
|
|
|
switch_id="switch_uni_a", is_valid=True) |
311
|
|
|
uni_z = get_uni_mocked(interface_port=3, tag_value=83, |
312
|
|
|
switch_id="switch_uni_z", is_valid=True) |
313
|
|
|
|
314
|
|
|
attributes = { |
315
|
|
|
"controller": get_controller_mock(), |
316
|
|
|
"name": "custom_name", |
317
|
|
|
"uni_a": uni_a, |
318
|
|
|
"uni_z": uni_z, |
319
|
|
|
"primary_links": [ |
320
|
|
|
get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, |
321
|
|
|
metadata={"s_vlan": 5}), |
322
|
|
|
get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, |
323
|
|
|
metadata={"s_vlan": 6}) |
324
|
|
|
] |
325
|
|
|
} |
326
|
|
|
evc = EVC(**attributes) |
327
|
|
|
|
328
|
|
|
# pylint: disable=protected-access |
329
|
|
|
evc._install_nni_flows(attributes['primary_links']) |
330
|
|
|
|
331
|
|
|
in_vlan = evc.primary_links[0].get_metadata('s_vlan').value |
332
|
|
|
out_vlan = evc.primary_links[-1].get_metadata('s_vlan').value |
333
|
|
|
|
334
|
|
|
in_port = evc.primary_links[0].endpoint_b.port_number |
335
|
|
|
out_port = evc.primary_links[-1].endpoint_a.port_number |
336
|
|
|
|
337
|
|
|
expected_flow_mods = [ |
338
|
|
|
{ |
339
|
|
|
'match': {'in_port': in_port, 'dl_vlan': in_vlan}, |
340
|
|
|
'cookie': evc.get_cookie(), |
341
|
|
|
'actions': [ |
342
|
|
|
{'action_type': 'set_vlan', 'vlan_id': out_vlan}, |
343
|
|
|
{'action_type': 'output', 'port': out_port} |
344
|
|
|
] |
345
|
|
|
}, |
346
|
|
|
{ |
347
|
|
|
'match': {'in_port': out_port, 'dl_vlan': out_vlan}, |
348
|
|
|
'cookie': evc.get_cookie(), |
349
|
|
|
'actions': [ |
350
|
|
|
{'action_type': 'set_vlan', 'vlan_id': in_vlan}, |
351
|
|
|
{'action_type': 'output', 'port': in_port} |
352
|
|
|
] |
353
|
|
|
} |
354
|
|
|
] |
355
|
|
|
|
356
|
|
|
switch = evc.primary_links[0].endpoint_b.switch |
357
|
|
|
send_flow_mods_mock.assert_called_once_with(switch, expected_flow_mods) |
358
|
|
|
|
359
|
|
|
@patch('requests.post') |
360
|
|
|
@patch('napps.kytos.mef_eline.storehouse.StoreHouse.save_evc') |
361
|
|
|
@patch('napps.kytos.mef_eline.models.log') |
362
|
|
|
@patch('napps.kytos.mef_eline.models.Path.choose_vlans') |
363
|
|
|
@patch('napps.kytos.mef_eline.models.EVC._install_nni_flows') |
364
|
|
|
@patch('napps.kytos.mef_eline.models.EVC._install_uni_flows') |
365
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.activate') |
366
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.should_deploy') |
367
|
|
|
def test_deploy_successfully(self, *args): |
368
|
|
|
"""Test if all methods to deploy are called.""" |
369
|
|
|
# pylint: disable=too-many-locals |
370
|
|
|
(should_deploy_mock, activate_mock, |
371
|
|
|
install_uni_flows_mock, install_nni_flows, chose_vlans_mock, |
372
|
|
|
log_mock, _, _) = args |
373
|
|
|
|
374
|
|
|
should_deploy_mock.return_value = True |
375
|
|
|
uni_a = get_uni_mocked(interface_port=2, tag_value=82, |
376
|
|
|
switch_id="switch_uni_a", is_valid=True) |
377
|
|
|
uni_z = get_uni_mocked(interface_port=3, tag_value=83, |
378
|
|
|
switch_id="switch_uni_z", is_valid=True) |
379
|
|
|
|
380
|
|
|
primary_links = [ |
381
|
|
|
get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, |
382
|
|
|
metadata={"s_vlan": 5}), |
383
|
|
|
get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, |
384
|
|
|
metadata={"s_vlan": 6}) |
385
|
|
|
] |
386
|
|
|
|
387
|
|
|
attributes = { |
388
|
|
|
"controller": get_controller_mock(), |
389
|
|
|
"name": "custom_name", |
390
|
|
|
"uni_a": uni_a, |
391
|
|
|
"uni_z": uni_z, |
392
|
|
|
"primary_links": primary_links, |
393
|
|
|
"queue_id": 5 |
394
|
|
|
} |
395
|
|
|
# Setup path to deploy |
396
|
|
|
path = Path() |
397
|
|
|
path.append(primary_links[0]) |
398
|
|
|
path.append(primary_links[1]) |
399
|
|
|
|
400
|
|
|
evc = EVC(**attributes) |
401
|
|
|
|
402
|
|
|
deployed = evc.deploy_to_path(path) |
403
|
|
|
|
404
|
|
|
self.assertEqual(should_deploy_mock.call_count, 1) |
405
|
|
|
self.assertEqual(activate_mock.call_count, 1) |
406
|
|
|
self.assertEqual(install_uni_flows_mock.call_count, 1) |
407
|
|
|
self.assertEqual(install_nni_flows.call_count, 1) |
408
|
|
|
self.assertEqual(chose_vlans_mock.call_count, 1) |
409
|
|
|
log_mock.info.assert_called_with(f"{evc} was deployed.") |
410
|
|
|
self.assertTrue(deployed) |
411
|
|
|
|
412
|
|
|
@patch('requests.post') |
413
|
|
|
@patch('napps.kytos.mef_eline.models.log') |
414
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.discover_new_paths', |
415
|
|
|
return_value=[]) |
416
|
|
|
@patch('napps.kytos.mef_eline.models.Path.choose_vlans') |
417
|
|
|
@patch('napps.kytos.mef_eline.models.EVC._install_nni_flows') |
418
|
|
|
@patch('napps.kytos.mef_eline.models.EVC._install_uni_flows') |
419
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.activate') |
420
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.should_deploy', |
421
|
|
|
return_value=False) |
422
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.sync') |
423
|
|
|
def test_deploy_fail(self, *args): |
424
|
|
|
"""Test if all methods is ignored when the should_deploy is false.""" |
425
|
|
|
# pylint: disable=too-many-locals |
426
|
|
|
(sync_mock, should_deploy_mock, activate_mock, install_uni_flows_mock, |
427
|
|
|
install_nni_flows, choose_vlans_mock, |
428
|
|
|
discover_new_paths, log_mock, _) = args |
429
|
|
|
|
430
|
|
|
uni_a = get_uni_mocked(interface_port=2, tag_value=82, |
431
|
|
|
switch_id="switch_uni_a", |
432
|
|
|
switch_dpid="switch_dpid_uni_a", |
433
|
|
|
is_valid=True) |
434
|
|
|
uni_z = get_uni_mocked(interface_port=3, tag_value=83, |
435
|
|
|
switch_id="switch_uni_z", |
436
|
|
|
switch_dpid="switch_dpid_uni_a", |
437
|
|
|
is_valid=True) |
438
|
|
|
|
439
|
|
|
attributes = { |
440
|
|
|
"controller": get_controller_mock(), |
441
|
|
|
"name": "custom_name", |
442
|
|
|
"uni_a": uni_a, |
443
|
|
|
"uni_z": uni_z, |
444
|
|
|
"primary_links": [ |
445
|
|
|
get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, |
446
|
|
|
metadata={"s_vlan": 5}), |
447
|
|
|
get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, |
448
|
|
|
metadata={"s_vlan": 6}) |
449
|
|
|
] |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
evc = EVC(**attributes) |
453
|
|
|
deployed = evc.deploy_to_path() |
454
|
|
|
|
455
|
|
|
self.assertEqual(discover_new_paths.call_count, 1) |
456
|
|
|
self.assertEqual(should_deploy_mock.call_count, 1) |
457
|
|
|
self.assertEqual(activate_mock.call_count, 0) |
458
|
|
|
self.assertEqual(install_uni_flows_mock.call_count, 0) |
459
|
|
|
self.assertEqual(install_nni_flows.call_count, 0) |
460
|
|
|
self.assertEqual(choose_vlans_mock.call_count, 0) |
461
|
|
|
self.assertEqual(log_mock.info.call_count, 0) |
462
|
|
|
self.assertEqual(sync_mock.call_count, 1) |
463
|
|
|
self.assertFalse(deployed) |
464
|
|
|
|
465
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.deploy_to_path') |
466
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.discover_new_paths') |
467
|
|
|
def test_deploy_to_backup_path1(self, discover_new_paths_mocked, |
468
|
|
|
deploy_to_path_mocked): |
469
|
|
|
"""Test deployment when dynamic_backup_path is False in same switch""" |
470
|
|
|
uni_a = get_uni_mocked(interface_port=2, tag_value=82, |
471
|
|
|
is_valid=True) |
472
|
|
|
uni_z = get_uni_mocked(interface_port=3, tag_value=83, |
473
|
|
|
is_valid=True) |
474
|
|
|
|
475
|
|
|
switch = Mock(spec=Switch) |
476
|
|
|
uni_a.interface.switch = switch |
477
|
|
|
uni_z.interface.switch = switch |
478
|
|
|
|
479
|
|
|
attributes = { |
480
|
|
|
"controller": get_controller_mock(), |
481
|
|
|
"name": "custom_name", |
482
|
|
|
"uni_a": uni_a, |
483
|
|
|
"uni_z": uni_z, |
484
|
|
|
"enabled": True, |
485
|
|
|
"dynamic_backup_path": False |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
evc = EVC(**attributes) |
489
|
|
|
discover_new_paths_mocked.return_value = [] |
490
|
|
|
deploy_to_path_mocked.return_value = True |
491
|
|
|
|
492
|
|
|
# storehouse initialization mock |
493
|
|
|
evc._storehouse.box = Mock() # pylint: disable=protected-access |
494
|
|
|
evc._storehouse.box.data = {} # pylint: disable=protected-access |
495
|
|
|
|
496
|
|
|
deployed = evc.deploy_to_backup_path() |
497
|
|
|
|
498
|
|
|
deploy_to_path_mocked.assert_called_once_with() |
499
|
|
|
self.assertEqual(deployed, True) |
500
|
|
|
|
501
|
|
|
@patch('requests.post') |
502
|
|
|
@patch('napps.kytos.mef_eline.storehouse.StoreHouse.save_evc') |
503
|
|
|
@patch('napps.kytos.mef_eline.models.log') |
504
|
|
|
@patch('napps.kytos.mef_eline.models.Path.choose_vlans') |
505
|
|
|
@patch('napps.kytos.mef_eline.models.EVC._install_nni_flows') |
506
|
|
|
@patch('napps.kytos.mef_eline.models.EVC._install_uni_flows') |
507
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.activate') |
508
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.should_deploy') |
509
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.discover_new_paths') |
510
|
|
|
def test_deploy_without_path_case1(self, *args): |
511
|
|
|
"""Test if not path is found a dynamic path is used.""" |
512
|
|
|
# pylint: disable=too-many-locals |
513
|
|
|
(discover_new_paths_mocked, should_deploy_mock, activate_mock, |
514
|
|
|
install_uni_flows_mock, install_nni_flows, chose_vlans_mock, |
515
|
|
|
log_mock, _, _) = args |
516
|
|
|
|
517
|
|
|
should_deploy_mock.return_value = False |
518
|
|
|
uni_a = get_uni_mocked(interface_port=2, tag_value=82, |
519
|
|
|
switch_id="switch_uni_a", is_valid=True) |
520
|
|
|
uni_z = get_uni_mocked(interface_port=3, tag_value=83, |
521
|
|
|
switch_id="switch_uni_z", is_valid=True) |
522
|
|
|
|
523
|
|
|
attributes = { |
524
|
|
|
"controller": get_controller_mock(), |
525
|
|
|
"name": "custom_name", |
526
|
|
|
"uni_a": uni_a, |
527
|
|
|
"uni_z": uni_z, |
528
|
|
|
"enabled": True, |
529
|
|
|
"dynamic_backup_path": False |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
dynamic_backup_path = Path([ |
533
|
|
|
get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, |
534
|
|
|
metadata={"s_vlan": 5}), |
535
|
|
|
get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, |
536
|
|
|
metadata={"s_vlan": 6}) |
537
|
|
|
]) |
538
|
|
|
|
539
|
|
|
evc = EVC(**attributes) |
540
|
|
|
discover_new_paths_mocked.return_value = [dynamic_backup_path] |
541
|
|
|
|
542
|
|
|
deployed = evc.deploy_to_path() |
543
|
|
|
|
544
|
|
|
self.assertEqual(should_deploy_mock.call_count, 1) |
545
|
|
|
self.assertEqual(discover_new_paths_mocked.call_count, 1) |
546
|
|
|
self.assertEqual(activate_mock.call_count, 1) |
547
|
|
|
self.assertEqual(install_uni_flows_mock.call_count, 1) |
548
|
|
|
self.assertEqual(install_nni_flows.call_count, 1) |
549
|
|
|
self.assertEqual(chose_vlans_mock.call_count, 1) |
550
|
|
|
log_mock.info.assert_called_with(f"{evc} was deployed.") |
551
|
|
|
self.assertTrue(deployed) |
552
|
|
|
|
553
|
|
|
@patch('napps.kytos.mef_eline.models.EVC._send_flow_mods') |
554
|
|
|
def test_remove_current_flows(self, send_flow_mods_mocked): |
555
|
|
|
"""Test remove current flows.""" |
556
|
|
|
uni_a = get_uni_mocked(interface_port=2, tag_value=82, |
557
|
|
|
switch_id="switch_uni_a", is_valid=True) |
558
|
|
|
uni_z = get_uni_mocked(interface_port=3, tag_value=83, |
559
|
|
|
switch_id="switch_uni_z", is_valid=True) |
560
|
|
|
|
561
|
|
|
switch_a = Switch('00:00:00:00:00:01') |
562
|
|
|
switch_b = Switch('00:00:00:00:00:02') |
563
|
|
|
switch_c = Switch('00:00:00:00:00:03') |
564
|
|
|
|
565
|
|
|
attributes = { |
566
|
|
|
"controller": get_controller_mock(), |
567
|
|
|
"name": "custom_name", |
568
|
|
|
"uni_a": uni_a, |
569
|
|
|
"uni_z": uni_z, |
570
|
|
|
"active": True, |
571
|
|
|
"enabled": True, |
572
|
|
|
"primary_links": [ |
573
|
|
|
get_link_mocked(switch_a=switch_a, |
574
|
|
|
switch_b=switch_b, |
575
|
|
|
endpoint_a_port=9, endpoint_b_port=10, |
576
|
|
|
metadata={"s_vlan": 5}), |
577
|
|
|
get_link_mocked(switch_a=switch_b, |
578
|
|
|
switch_b=switch_c, |
579
|
|
|
endpoint_a_port=11, endpoint_b_port=12, |
580
|
|
|
metadata={"s_vlan": 6}) |
581
|
|
|
] |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
evc = EVC(**attributes) |
585
|
|
|
|
586
|
|
|
# storehouse initialization mock |
587
|
|
|
evc._storehouse.box = Mock() # pylint: disable=protected-access |
588
|
|
|
evc._storehouse.box.data = {} # pylint: disable=protected-access |
589
|
|
|
|
590
|
|
|
evc.current_path = evc.primary_links |
591
|
|
|
evc.remove_current_flows() |
592
|
|
|
|
593
|
|
|
self.assertEqual(send_flow_mods_mocked.call_count, 5) |
594
|
|
|
self.assertFalse(evc.is_active()) |
595
|
|
|
flows = [{'cookie': evc.get_cookie(), |
596
|
|
|
'cookie_mask': 18446744073709551615}] |
597
|
|
|
switch_1 = evc.primary_links[0].endpoint_a.switch |
598
|
|
|
switch_2 = evc.primary_links[0].endpoint_b.switch |
599
|
|
|
send_flow_mods_mocked.assert_any_call(switch_1, flows, 'delete') |
600
|
|
|
send_flow_mods_mocked.assert_any_call(switch_2, flows, 'delete') |
601
|
|
|
|