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 UNI, Interface |
7
|
|
|
from kytos.core.switch import Switch |
8
|
|
|
from kytos.core.common import EntityStatus |
9
|
|
|
|
10
|
|
|
# pylint: disable=wrong-import-position |
11
|
|
|
sys.path.insert(0, '/var/lib/kytos/napps/..') |
12
|
|
|
# pylint: enable=wrong-import-position |
13
|
|
|
|
14
|
|
|
from napps.kytos.mef_eline.models import EVC, Path # NOQA |
15
|
|
|
from napps.kytos.mef_eline.settings import MANAGER_URL # NOQA |
16
|
|
|
from napps.kytos.mef_eline.tests.helpers import get_link_mocked, get_uni_mocked # 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
|
|
|
pass |
25
|
|
|
|
26
|
|
|
@staticmethod |
27
|
|
|
@patch('napps.kytos.mef_eline.models.log') |
28
|
|
|
def test_should_deploy_case1(log_mock): |
29
|
|
|
"""Test should deploy method without primary links.""" |
30
|
|
|
log_mock.debug.return_value = True |
31
|
|
|
attributes = { |
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
|
|
|
"name": "custom_name", |
47
|
|
|
"uni_a": get_uni_mocked(is_valid=True), |
48
|
|
|
"uni_z": get_uni_mocked(is_valid=True), |
49
|
|
|
"primary_links": [get_link_mocked(), get_link_mocked()] |
50
|
|
|
} |
51
|
|
|
evc = EVC(**attributes) |
52
|
|
|
|
53
|
|
|
self.assertFalse(evc.should_deploy(attributes['primary_links'])) |
54
|
|
|
log_mock.debug.assert_called_with(f'{evc} is disabled.') |
55
|
|
|
|
56
|
|
|
@patch('napps.kytos.mef_eline.models.log') |
57
|
|
|
def test_should_deploy_case3(self, log_mock): |
58
|
|
|
"""Test should deploy method with enabled and not active circuit.""" |
59
|
|
|
log_mock.debug.return_value = True |
60
|
|
|
attributes = { |
61
|
|
|
"name": "custom_name", |
62
|
|
|
"uni_a": get_uni_mocked(is_valid=True), |
63
|
|
|
"uni_z": get_uni_mocked(is_valid=True), |
64
|
|
|
"primary_links": [get_link_mocked(), get_link_mocked()], |
65
|
|
|
"enabled": True |
66
|
|
|
} |
67
|
|
|
evc = EVC(**attributes) |
68
|
|
|
self.assertTrue(evc.should_deploy(attributes['primary_links'])) |
69
|
|
|
log_mock.debug.assert_called_with(f'{evc} will be deployed.') |
70
|
|
|
|
71
|
|
|
@patch('napps.kytos.mef_eline.models.log') |
72
|
|
|
def test_should_deploy_case4(self, log_mock): |
73
|
|
|
"""Test should deploy method with enabled and active circuit.""" |
74
|
|
|
log_mock.debug.return_value = True |
75
|
|
|
attributes = { |
76
|
|
|
"name": "custom_name", |
77
|
|
|
"uni_a": get_uni_mocked(is_valid=True), |
78
|
|
|
"uni_z": get_uni_mocked(is_valid=True), |
79
|
|
|
"primary_links": [get_link_mocked(), get_link_mocked()], |
80
|
|
|
"enabled": True, |
81
|
|
|
"active": True |
82
|
|
|
} |
83
|
|
|
evc = EVC(**attributes) |
84
|
|
|
self.assertFalse(evc.should_deploy(attributes['primary_links'])) |
85
|
|
|
|
86
|
|
|
@patch('napps.kytos.mef_eline.models.requests') |
87
|
|
|
def test_send_flow_mods(self, requests_mock): |
88
|
|
|
"""Test if you are sending flow_mods.""" |
89
|
|
|
flow_mods = {"id": 20} |
90
|
|
|
switch = Mock(spec=Switch, id=1) |
91
|
|
|
EVC.send_flow_mods(switch, flow_mods) |
92
|
|
|
expected_endpoint = f"{MANAGER_URL}/flows/{switch.id}" |
93
|
|
|
expected_data = {"flows": flow_mods} |
94
|
|
|
self.assertEqual(requests_mock.post.call_count, 1) |
95
|
|
|
requests_mock.post.assert_called_once_with(expected_endpoint, |
96
|
|
|
json=expected_data) |
97
|
|
|
|
98
|
|
|
def test_prepare_flow_mod(self): |
99
|
|
|
"""Test prepare flow_mod method.""" |
100
|
|
|
interface_a = Interface('eth0', 1, Mock(spec=Switch)) |
101
|
|
|
interface_z = Interface('eth1', 3, Mock(spec=Switch)) |
102
|
|
|
attributes = { |
103
|
|
|
"name": "custom_name", |
104
|
|
|
"uni_a": get_uni_mocked(is_valid=True), |
105
|
|
|
"uni_z": get_uni_mocked(is_valid=True), |
106
|
|
|
"primary_links": [get_link_mocked(), get_link_mocked()], |
107
|
|
|
"enabled": True, |
108
|
|
|
"active": True |
109
|
|
|
} |
110
|
|
|
evc = EVC(**attributes) |
111
|
|
|
flow_mod = evc.prepare_flow_mod(interface_a, interface_z) |
112
|
|
|
expected_flow_mod = { |
113
|
|
|
'match': {'in_port': interface_a.port_number}, |
114
|
|
|
'cookie': evc.get_cookie(), |
115
|
|
|
'actions': [ |
116
|
|
|
{'action_type': 'output', |
117
|
|
|
'port': interface_z.port_number} |
118
|
|
|
] |
119
|
|
|
} |
120
|
|
|
self.assertEqual(expected_flow_mod, flow_mod) |
121
|
|
|
|
122
|
|
|
def test_prepare_pop_flow(self): |
123
|
|
|
"""Test prepare pop flow method.""" |
124
|
|
|
attributes = { |
125
|
|
|
"name": "custom_name", |
126
|
|
|
"uni_a": get_uni_mocked(interface_port=1, is_valid=True), |
127
|
|
|
"uni_z": get_uni_mocked(interface_port=2, is_valid=True), |
128
|
|
|
} |
129
|
|
|
evc = EVC(**attributes) |
130
|
|
|
interface_a = evc.uni_a.interface |
131
|
|
|
interface_z = evc.uni_z.interface |
132
|
|
|
in_vlan = 10 |
133
|
|
|
flow_mod = evc.prepare_pop_flow(interface_a, interface_z, in_vlan) |
134
|
|
|
expected_flow_mod = { |
135
|
|
|
'match': {'in_port': interface_a.port_number, 'dl_vlan': in_vlan}, |
136
|
|
|
'cookie': evc.get_cookie(), |
137
|
|
|
'actions': [ |
138
|
|
|
{'action_type': 'pop_vlan'}, |
139
|
|
|
{'action_type': 'output', |
140
|
|
|
'port': interface_z.port_number |
141
|
|
|
} |
142
|
|
|
] |
143
|
|
|
} |
144
|
|
|
self.assertEqual(expected_flow_mod, flow_mod) |
145
|
|
|
|
146
|
|
|
def test_prepare_push_flow(self): |
147
|
|
|
"""Test prepare push flow method.""" |
148
|
|
|
attributes = { |
149
|
|
|
"name": "custom_name", |
150
|
|
|
"uni_a": get_uni_mocked(interface_port=1, is_valid=True), |
151
|
|
|
"uni_z": get_uni_mocked(interface_port=2, is_valid=True), |
152
|
|
|
} |
153
|
|
|
evc = EVC(**attributes) |
154
|
|
|
interface_a = evc.uni_a.interface |
155
|
|
|
interface_z = evc.uni_z.interface |
156
|
|
|
in_vlan_a = 10 |
157
|
|
|
out_vlan_a = 20 |
158
|
|
|
in_vlan_z = 3 |
159
|
|
|
flow_mod = evc.prepare_push_flow(interface_a, interface_z, |
160
|
|
|
in_vlan_a, out_vlan_a, in_vlan_z) |
161
|
|
|
expected_flow_mod = { |
162
|
|
|
'match': {'in_port': interface_a.port_number, |
163
|
|
|
'dl_vlan': in_vlan_a |
164
|
|
|
}, |
165
|
|
|
'cookie': evc.get_cookie(), |
166
|
|
|
'actions': [ |
167
|
|
|
{'action_type': 'set_vlan', 'vlan_id': in_vlan_z}, |
168
|
|
|
{'action_type': 'push_vlan', 'tag_type': 's'}, |
169
|
|
|
{'action_type': 'set_vlan', 'vlan_id': out_vlan_a}, |
170
|
|
|
{'action_type': 'output', |
171
|
|
|
'port': interface_z.port_number |
172
|
|
|
} |
173
|
|
|
] |
174
|
|
|
} |
175
|
|
|
self.assertEqual(expected_flow_mod, flow_mod) |
176
|
|
|
|
177
|
|
|
@staticmethod |
178
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.send_flow_mods') |
179
|
|
|
def test_install_uni_flows(send_flow_mods_mock): |
180
|
|
|
"""Test install uni flows method. |
181
|
|
|
|
182
|
|
|
This test will verify the flows send to the send_flows_mods method. |
183
|
|
|
""" |
184
|
|
|
uni_a = get_uni_mocked(interface_port=2, tag_value=82, |
185
|
|
|
switch_id="switch_uni_a", is_valid=True) |
186
|
|
|
uni_z = get_uni_mocked(interface_port=3, tag_value=83, |
187
|
|
|
switch_id="switch_uni_z", is_valid=True) |
188
|
|
|
|
189
|
|
|
attributes = { |
190
|
|
|
"name": "custom_name", |
191
|
|
|
"uni_a": uni_a, |
192
|
|
|
"uni_z": uni_z, |
193
|
|
|
"primary_links": [ |
194
|
|
|
get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, |
195
|
|
|
metadata={"s_vlan": 5}), |
196
|
|
|
get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, |
197
|
|
|
metadata={"s_vlan": 6}) |
198
|
|
|
] |
199
|
|
|
} |
200
|
|
|
evc = EVC(**attributes) |
201
|
|
|
evc.install_uni_flows(attributes['primary_links']) |
202
|
|
|
|
203
|
|
|
expected_flow_mod_a = [ |
204
|
|
|
{'match': {'in_port': uni_a.interface.port_number, |
205
|
|
|
'dl_vlan': uni_a.user_tag.value}, |
206
|
|
|
'cookie': evc.get_cookie(), |
207
|
|
|
'actions': [ |
208
|
|
|
{'action_type': 'set_vlan', 'vlan_id': uni_z.user_tag.value}, |
209
|
|
|
{'action_type': 'push_vlan', 'tag_type': 's'}, |
210
|
|
|
{'action_type': 'set_vlan', |
211
|
|
|
'vlan_id': evc.primary_links[0].get_metadata('s_vlan').value}, |
212
|
|
|
{'action_type': 'output', |
213
|
|
|
'port': evc.primary_links[0].endpoint_a.port_number} |
214
|
|
|
]}, |
215
|
|
|
{'match': { |
216
|
|
|
'in_port': evc.primary_links[0].endpoint_a.port_number, |
217
|
|
|
'dl_vlan': evc.primary_links[0].get_metadata('s_vlan').value |
218
|
|
|
}, |
219
|
|
|
'cookie': evc.get_cookie(), |
220
|
|
|
'actions': [ |
221
|
|
|
{'action_type': 'pop_vlan'}, |
222
|
|
|
{'action_type': 'output', 'port': uni_a.interface.port_number} |
223
|
|
|
] |
224
|
|
|
} |
225
|
|
|
] |
226
|
|
|
send_flow_mods_mock.assert_any_call(uni_a.interface.switch, |
227
|
|
|
expected_flow_mod_a) |
228
|
|
|
|
229
|
|
|
expected_flow_mod_z = [ |
230
|
|
|
{'match': {'in_port': uni_z.interface.port_number, |
231
|
|
|
'dl_vlan': uni_z.user_tag.value}, |
232
|
|
|
'cookie': evc.get_cookie(), |
233
|
|
|
'actions': [ |
234
|
|
|
{'action_type': 'set_vlan', 'vlan_id': uni_a.user_tag.value}, |
235
|
|
|
{'action_type': 'push_vlan', 'tag_type': 's'}, |
236
|
|
|
{'action_type': 'set_vlan', |
237
|
|
|
'vlan_id': evc.primary_links[-1].get_metadata('s_vlan').value |
238
|
|
|
}, |
239
|
|
|
{'action_type': 'output', |
240
|
|
|
'port': evc.primary_links[-1].endpoint_b.port_number} |
241
|
|
|
] |
242
|
|
|
}, |
243
|
|
|
{'match': { |
244
|
|
|
'in_port': evc.primary_links[-1].endpoint_b.port_number, |
245
|
|
|
'dl_vlan': evc.primary_links[-1].get_metadata('s_vlan').value |
246
|
|
|
}, |
247
|
|
|
'cookie': evc.get_cookie(), |
248
|
|
|
'actions': [ |
249
|
|
|
{'action_type': 'pop_vlan'}, |
250
|
|
|
{'action_type': 'output', 'port': uni_z.interface.port_number} |
251
|
|
|
] |
252
|
|
|
} |
253
|
|
|
] |
254
|
|
|
|
255
|
|
|
send_flow_mods_mock.assert_any_call(uni_z.interface.switch, |
256
|
|
|
expected_flow_mod_z) |
257
|
|
|
|
258
|
|
|
@staticmethod |
259
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.send_flow_mods') |
260
|
|
|
def test_install_nni_flows(send_flow_mods_mock): |
261
|
|
|
"""Test install nni flows method. |
262
|
|
|
|
263
|
|
|
This test will verify the flows send to the send_flows_mods method. |
264
|
|
|
""" |
265
|
|
|
uni_a = get_uni_mocked(interface_port=2, tag_value=82, |
266
|
|
|
switch_id="switch_uni_a", is_valid=True) |
267
|
|
|
uni_z = get_uni_mocked(interface_port=3, tag_value=83, |
268
|
|
|
switch_id="switch_uni_z", is_valid=True) |
269
|
|
|
|
270
|
|
|
attributes = { |
271
|
|
|
"name": "custom_name", |
272
|
|
|
"uni_a": uni_a, |
273
|
|
|
"uni_z": uni_z, |
274
|
|
|
"primary_links": [ |
275
|
|
|
get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, |
276
|
|
|
metadata={"s_vlan": 5}), |
277
|
|
|
get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, |
278
|
|
|
metadata={"s_vlan": 6}) |
279
|
|
|
] |
280
|
|
|
} |
281
|
|
|
evc = EVC(**attributes) |
282
|
|
|
evc.install_nni_flows(attributes['primary_links']) |
283
|
|
|
|
284
|
|
|
in_vlan = evc.primary_links[0].get_metadata('s_vlan').value |
285
|
|
|
out_vlan = evc.primary_links[-1].get_metadata('s_vlan').value |
286
|
|
|
|
287
|
|
|
in_port = evc.primary_links[0].endpoint_b.port_number |
288
|
|
|
out_port = evc.primary_links[-1].endpoint_a.port_number |
289
|
|
|
|
290
|
|
|
expected_flow_mods = [ |
291
|
|
|
{ |
292
|
|
|
'match': {'in_port': in_port, 'dl_vlan': in_vlan}, |
293
|
|
|
'cookie': evc.get_cookie(), |
294
|
|
|
'actions': [ |
295
|
|
|
{'action_type': 'set_vlan', 'vlan_id': out_vlan}, |
296
|
|
|
{'action_type': 'output', 'port': out_port} |
297
|
|
|
] |
298
|
|
|
}, |
299
|
|
|
{ |
300
|
|
|
'match': {'in_port': out_port, 'dl_vlan': out_vlan}, |
301
|
|
|
'cookie': evc.get_cookie(), |
302
|
|
|
'actions': [ |
303
|
|
|
{'action_type': 'set_vlan', 'vlan_id': in_vlan}, |
304
|
|
|
{'action_type': 'output', 'port': in_port} |
305
|
|
|
] |
306
|
|
|
} |
307
|
|
|
] |
308
|
|
|
|
309
|
|
|
switch = evc.primary_links[0].endpoint_b.switch |
310
|
|
|
send_flow_mods_mock.assert_called_once_with(switch, expected_flow_mods) |
311
|
|
|
|
312
|
|
View Code Duplication |
@patch('napps.kytos.mef_eline.models.log') |
|
|
|
|
313
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.choose_vlans') |
314
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.install_nni_flows') |
315
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.install_uni_flows') |
316
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.activate') |
317
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.should_deploy') |
318
|
|
|
def test_deploy_successfully(self, *args): |
319
|
|
|
"""Test if all methods to deploy are called.""" |
320
|
|
|
(should_deploy_mock, activate_mock, install_uni_flows_mock, |
321
|
|
|
install_nni_flows, chose_vlans_mock, log_mock) = args |
322
|
|
|
|
323
|
|
|
should_deploy_mock.return_value = True |
324
|
|
|
uni_a = get_uni_mocked(interface_port=2, tag_value=82, |
325
|
|
|
switch_id="switch_uni_a", is_valid=True) |
326
|
|
|
uni_z = get_uni_mocked(interface_port=3, tag_value=83, |
327
|
|
|
switch_id="switch_uni_z", is_valid=True) |
328
|
|
|
|
329
|
|
|
attributes = { |
330
|
|
|
"name": "custom_name", |
331
|
|
|
"uni_a": uni_a, |
332
|
|
|
"uni_z": uni_z, |
333
|
|
|
"primary_links": [ |
334
|
|
|
get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, |
335
|
|
|
metadata={"s_vlan": 5}), |
336
|
|
|
get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, |
337
|
|
|
metadata={"s_vlan": 6}) |
338
|
|
|
] |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
evc = EVC(**attributes) |
342
|
|
|
deployed = evc.deploy(attributes['primary_links']) |
343
|
|
|
|
344
|
|
|
self.assertEqual(should_deploy_mock.call_count, 1) |
345
|
|
|
self.assertEqual(activate_mock.call_count, 1) |
346
|
|
|
self.assertEqual(install_uni_flows_mock.call_count, 1) |
347
|
|
|
self.assertEqual(install_nni_flows.call_count, 1) |
348
|
|
|
self.assertEqual(chose_vlans_mock.call_count, 1) |
349
|
|
|
log_mock.info.assert_called_once_with(f"{evc} was deployed.") |
350
|
|
|
self.assertTrue(deployed) |
351
|
|
|
|
352
|
|
View Code Duplication |
@patch('napps.kytos.mef_eline.models.log') |
|
|
|
|
353
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.choose_vlans') |
354
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.install_nni_flows') |
355
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.install_uni_flows') |
356
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.activate') |
357
|
|
|
@patch('napps.kytos.mef_eline.models.EVC.should_deploy') |
358
|
|
|
def test_deploy_fail(self, *args): |
359
|
|
|
"""Test if all methods is ignored when the should_deploy is false.""" |
360
|
|
|
(should_deploy_mock, activate_mock, install_uni_flows_mock, |
361
|
|
|
install_nni_flows, chose_vlans_mock, log_mock) = args |
362
|
|
|
|
363
|
|
|
should_deploy_mock.return_value = False |
364
|
|
|
uni_a = get_uni_mocked(interface_port=2, tag_value=82, |
365
|
|
|
switch_id="switch_uni_a", is_valid=True) |
366
|
|
|
uni_z = get_uni_mocked(interface_port=3, tag_value=83, |
367
|
|
|
switch_id="switch_uni_z", is_valid=True) |
368
|
|
|
|
369
|
|
|
attributes = { |
370
|
|
|
"name": "custom_name", |
371
|
|
|
"uni_a": uni_a, |
372
|
|
|
"uni_z": uni_z, |
373
|
|
|
"primary_links": [ |
374
|
|
|
get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, |
375
|
|
|
metadata={"s_vlan": 5}), |
376
|
|
|
get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, |
377
|
|
|
metadata={"s_vlan": 6}) |
378
|
|
|
] |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
evc = EVC(**attributes) |
382
|
|
|
deployed = evc.deploy() |
383
|
|
|
|
384
|
|
|
self.assertEqual(should_deploy_mock.call_count, 1) |
385
|
|
|
self.assertEqual(activate_mock.call_count, 0) |
386
|
|
|
self.assertEqual(install_uni_flows_mock.call_count, 0) |
387
|
|
|
self.assertEqual(install_nni_flows.call_count, 0) |
388
|
|
|
self.assertEqual(chose_vlans_mock.call_count, 0) |
389
|
|
|
self.assertEqual(log_mock.info.call_count, 0) |
390
|
|
|
self.assertFalse(deployed) |
391
|
|
|
|