Total Complexity | 43 |
Total Lines | 642 |
Duplicated Lines | 18.85 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like build.tests.test_models often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | """Module to test the schedule.py file.""" |
||
2 | from unittest import TestCase |
||
3 | from unittest.mock import Mock, patch |
||
4 | |||
5 | from kytos.core.interface import UNI, Interface |
||
6 | from kytos.core.switch import Switch |
||
7 | from kytos.core.common import EntityStatus |
||
8 | |||
9 | from napps.kytos.mef_eline.models import EVC, Path |
||
10 | from napps.kytos.mef_eline.settings import MANAGER_URL |
||
11 | from .helpers import get_link_mocked, get_uni_mocked |
||
12 | |||
13 | |||
14 | class TestPath(TestCase): |
||
15 | """"Class to test path methods.""" |
||
16 | |||
17 | def test_status_case_1(self): |
||
18 | """Test if empty link is DISABLED.""" |
||
19 | current_path = Path() |
||
20 | self.assertEqual(current_path.status, EntityStatus.DISABLED) |
||
21 | |||
22 | def test_status_case_2(self): |
||
23 | """Test if link status is DOWN.""" |
||
24 | links = [ |
||
25 | get_link_mocked(status=EntityStatus.DOWN), |
||
26 | get_link_mocked(status=EntityStatus.UP) |
||
27 | ] |
||
28 | current_path = Path(links) |
||
29 | self.assertEqual(current_path.status, EntityStatus.DOWN) |
||
30 | |||
31 | def test_status_case_3(self): |
||
32 | """Test if link status is DISABLED.""" |
||
33 | links = [ |
||
34 | get_link_mocked(status=EntityStatus.DISABLED), |
||
35 | get_link_mocked(status=EntityStatus.UP) |
||
36 | ] |
||
37 | current_path = Path(links) |
||
38 | self.assertEqual(current_path.status, EntityStatus.DISABLED) |
||
39 | |||
40 | def test_status_case_4(self): |
||
41 | """Test if link status is UP.""" |
||
42 | links = [ |
||
43 | get_link_mocked(status=EntityStatus.UP), |
||
44 | get_link_mocked(status=EntityStatus.UP) |
||
45 | ] |
||
46 | current_path = Path(links) |
||
47 | self.assertEqual(current_path.status, EntityStatus.UP) |
||
48 | |||
49 | def test_compare_same_paths(self): |
||
50 | """Test compare paths with same links.""" |
||
51 | links = [ |
||
52 | get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, |
||
53 | metadata={"s_vlan": 5}), |
||
54 | get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, |
||
55 | metadata={"s_vlan": 6}) |
||
56 | ] |
||
57 | |||
58 | path_1 = Path(links) |
||
59 | path_2 = Path(links) |
||
60 | self.assertEqual(path_1, path_2) |
||
61 | |||
62 | def test_compare_different_paths(self): |
||
63 | """Test compare paths with different links.""" |
||
64 | links_1 = [ |
||
65 | get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, |
||
66 | metadata={"s_vlan": 5}), |
||
67 | get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, |
||
68 | metadata={"s_vlan": 6}) |
||
69 | ] |
||
70 | links_2 = [ |
||
71 | get_link_mocked(endpoint_a_port=12, endpoint_b_port=11, |
||
72 | metadata={"s_vlan": 5}), |
||
73 | get_link_mocked(endpoint_a_port=14, endpoint_b_port=16, |
||
74 | metadata={"s_vlan": 11}) |
||
75 | ] |
||
76 | |||
77 | path_1 = Path(links_1) |
||
78 | path_2 = Path(links_2) |
||
79 | self.assertNotEqual(path_1, path_2) |
||
80 | |||
81 | def test_as_dict(self): |
||
82 | """Test path as dict.""" |
||
83 | links = [ |
||
84 | get_link_mocked(link_dict={"id": 3}), |
||
85 | get_link_mocked(link_dict={"id": 2}) |
||
86 | ] |
||
87 | |||
88 | current_path = Path(links) |
||
89 | expected_dict = [{"id": 3}, {"id": 2}] |
||
90 | self.assertEqual(expected_dict, current_path.as_dict()) |
||
91 | |||
92 | |||
93 | class TestEVC(TestCase): # pylint: disable=too-many-public-methods |
||
94 | """Tests to verify EVC class.""" |
||
95 | |||
96 | def test_attributes_empty(self): |
||
97 | """Test if the EVC raises an error with name is required.""" |
||
98 | attributes = {} |
||
99 | error_message = "name is required." |
||
100 | with self.assertRaises(ValueError) as handle_error: |
||
101 | EVC(**attributes) |
||
102 | self.assertEqual(str(handle_error.exception), error_message) |
||
103 | |||
104 | def test_without_uni_a(self): |
||
105 | """Test if the EVC raises and error with UNI A is required.""" |
||
106 | attributes = {"name": "circuit_name"} |
||
107 | error_message = "uni_a is required." |
||
108 | with self.assertRaises(ValueError) as handle_error: |
||
109 | EVC(**attributes) |
||
110 | self.assertEqual(str(handle_error.exception), error_message) |
||
111 | |||
112 | def test_with_invalid_uni_a(self): |
||
113 | """Test if the EVC raises and error with invalid UNI A.""" |
||
114 | attributes = { |
||
115 | "name": "circuit_name", |
||
116 | "uni_a": get_uni_mocked(tag_value=82) |
||
117 | } |
||
118 | error_message = "VLAN tag 82 is not available in uni_a" |
||
119 | with self.assertRaises(ValueError) as handle_error: |
||
120 | EVC(**attributes) |
||
121 | self.assertEqual(str(handle_error.exception), error_message) |
||
122 | |||
123 | def test_without_uni_z(self): |
||
124 | """Test if the EVC raises and error with UNI Z is required.""" |
||
125 | attributes = { |
||
126 | "name": "circuit_name", |
||
127 | "uni_a": get_uni_mocked(is_valid=True) |
||
128 | } |
||
129 | error_message = "uni_z is required." |
||
130 | with self.assertRaises(ValueError) as handle_error: |
||
131 | EVC(**attributes) |
||
132 | self.assertEqual(str(handle_error.exception), error_message) |
||
133 | |||
134 | def test_with_invalid_uni_z(self): |
||
135 | """Test if the EVC raises and error with UNI Z is required.""" |
||
136 | attributes = { |
||
137 | "name": "circuit_name", |
||
138 | "uni_a": get_uni_mocked(is_valid=True), |
||
139 | "uni_z": get_uni_mocked(tag_value=83) |
||
140 | } |
||
141 | error_message = "VLAN tag 83 is not available in uni_z" |
||
142 | with self.assertRaises(ValueError) as handle_error: |
||
143 | EVC(**attributes) |
||
144 | self.assertEqual(str(handle_error.exception), error_message) |
||
145 | |||
146 | View Code Duplication | def test_update_name(self): |
|
|
|||
147 | """Test if raises and error when trying to update the name.""" |
||
148 | attributes = { |
||
149 | "name": "circuit_name", |
||
150 | "uni_a": get_uni_mocked(is_valid=True), |
||
151 | "uni_z": get_uni_mocked(is_valid=True) |
||
152 | } |
||
153 | update_dict = { |
||
154 | "name": "circuit_name_2" |
||
155 | } |
||
156 | error_message = "name can't be be updated." |
||
157 | with self.assertRaises(ValueError) as handle_error: |
||
158 | evc = EVC(**attributes) |
||
159 | evc.update(**update_dict) |
||
160 | self.assertEqual(str(handle_error.exception), error_message) |
||
161 | |||
162 | View Code Duplication | def test_update_uni_a(self): |
|
163 | """Test if raises and error when trying to update the uni_a.""" |
||
164 | attributes = { |
||
165 | "name": "circuit_name", |
||
166 | "uni_a": get_uni_mocked(is_valid=True), |
||
167 | "uni_z": get_uni_mocked(is_valid=True) |
||
168 | } |
||
169 | update_dict = { |
||
170 | "uni_a": get_uni_mocked(is_valid=True) |
||
171 | } |
||
172 | error_message = "uni_a can't be be updated." |
||
173 | with self.assertRaises(ValueError) as handle_error: |
||
174 | evc = EVC(**attributes) |
||
175 | evc.update(**update_dict) |
||
176 | self.assertEqual(str(handle_error.exception), error_message) |
||
177 | |||
178 | View Code Duplication | def test_update_uni_z(self): |
|
179 | """Test if raises and error when trying to update the uni_z.""" |
||
180 | attributes = { |
||
181 | "name": "circuit_name", |
||
182 | "uni_a": get_uni_mocked(is_valid=True), |
||
183 | "uni_z": get_uni_mocked(is_valid=True) |
||
184 | } |
||
185 | update_dict = { |
||
186 | "uni_z": get_uni_mocked(is_valid=True) |
||
187 | } |
||
188 | error_message = "uni_z can't be be updated." |
||
189 | with self.assertRaises(ValueError) as handle_error: |
||
190 | evc = EVC(**attributes) |
||
191 | evc.update(**update_dict) |
||
192 | self.assertEqual(str(handle_error.exception), error_message) |
||
193 | |||
194 | def test_circuit_representation(self): |
||
195 | """Test the method __repr__.""" |
||
196 | attributes = { |
||
197 | "name": "circuit_name", |
||
198 | "uni_a": get_uni_mocked(is_valid=True), |
||
199 | "uni_z": get_uni_mocked(is_valid=True) |
||
200 | } |
||
201 | evc = EVC(**attributes) |
||
202 | expected_value = f'EVC({evc.id}, {evc.name})' |
||
203 | self.assertEqual(str(evc), expected_value) |
||
204 | |||
205 | def test_comparison_method(self): |
||
206 | """Test the method __eq__.""" |
||
207 | attributes = { |
||
208 | "name": "circuit_name", |
||
209 | "uni_a": get_uni_mocked(is_valid=True), |
||
210 | "uni_z": get_uni_mocked(is_valid=True) |
||
211 | } |
||
212 | evc1 = EVC(**attributes) |
||
213 | evc2 = EVC(**attributes) |
||
214 | |||
215 | attributes = { |
||
216 | "name": "circuit_name_2", |
||
217 | "uni_a": get_uni_mocked(is_valid=True), |
||
218 | "uni_z": get_uni_mocked(is_valid=True) |
||
219 | } |
||
220 | evc3 = EVC(**attributes) |
||
221 | evc4 = EVC(**attributes) |
||
222 | |||
223 | self.assertEqual(evc1 == evc2, True) |
||
224 | self.assertEqual(evc1 == evc3, False) |
||
225 | self.assertEqual(evc2 == evc3, False) |
||
226 | self.assertEqual(evc3 == evc4, True) |
||
227 | |||
228 | def test_as_dict(self): |
||
229 | """Test the method as_dict.""" |
||
230 | attributes = { |
||
231 | "id": "custom_id", |
||
232 | "name": "custom_name", |
||
233 | "uni_a": get_uni_mocked(is_valid=True), |
||
234 | "uni_z": get_uni_mocked(is_valid=True), |
||
235 | "start_date": '2018-08-21T18:44:54', |
||
236 | "end_date": '2018-08-21T18:44:55', |
||
237 | 'primary_links': [], |
||
238 | 'request_time': '2018-08-21T19:10:41', |
||
239 | 'creation_time': '2018-08-21T18:44:54', |
||
240 | 'owner': "my_name", |
||
241 | 'circuit_scheduler': [], |
||
242 | 'enabled': True, |
||
243 | 'priority': 2 |
||
244 | } |
||
245 | evc = EVC(**attributes) |
||
246 | |||
247 | expected_dict = { |
||
248 | 'id': 'custom_id', |
||
249 | 'name': 'custom_name', |
||
250 | 'uni_a': attributes['uni_a'].as_dict(), |
||
251 | 'uni_z': attributes['uni_z'].as_dict(), |
||
252 | 'start_date': '2018-08-21T18:44:54', |
||
253 | 'end_date': '2018-08-21T18:44:55', |
||
254 | 'bandwidth': 0, |
||
255 | 'primary_links': [], |
||
256 | 'backup_links': [], |
||
257 | 'current_path': [], |
||
258 | 'primary_path': [], |
||
259 | 'backup_path': [], |
||
260 | 'dynamic_backup_path': False, |
||
261 | '_requested': { |
||
262 | "id": "custom_id", |
||
263 | "name": "custom_name", |
||
264 | "uni_a": attributes['uni_a'].as_dict(), |
||
265 | "uni_z": attributes['uni_z'].as_dict(), |
||
266 | "start_date": '2018-08-21T18:44:54', |
||
267 | "end_date": '2018-08-21T18:44:55', |
||
268 | 'primary_links': [], |
||
269 | 'request_time': '2018-08-21T19:10:41', |
||
270 | 'creation_time': '2018-08-21T18:44:54', |
||
271 | 'owner': "my_name", |
||
272 | 'circuit_scheduler': [], |
||
273 | 'enabled': True, |
||
274 | 'priority': 2 |
||
275 | }, |
||
276 | 'request_time': '2018-08-21T19:10:41', |
||
277 | 'creation_time': '2018-08-21T18:44:54', |
||
278 | 'owner': 'my_name', |
||
279 | 'circuit_scheduler': [], |
||
280 | 'active': False, |
||
281 | 'enabled': True, |
||
282 | 'priority': 2 |
||
283 | } |
||
284 | actual_dict = evc.as_dict() |
||
285 | for name, value in expected_dict.items(): |
||
286 | actual = actual_dict.get(name) |
||
287 | if name == '_requested': |
||
288 | for requested_name, requested_value in value.items(): |
||
289 | if isinstance(requested_value, UNI): |
||
290 | value[requested_name] = requested_value.as_dict() |
||
291 | self.assertEqual(value, actual) |
||
292 | |||
293 | def test_primary_links_zipped(self): |
||
294 | """Test primary links zipped method.""" |
||
295 | pass |
||
296 | |||
297 | @staticmethod |
||
298 | @patch('napps.kytos.mef_eline.models.log') |
||
299 | def test_should_deploy_case1(log_mock): |
||
300 | """Test should deploy method without primary links.""" |
||
301 | log_mock.debug.return_value = True |
||
302 | attributes = { |
||
303 | "name": "custom_name", |
||
304 | "uni_a": get_uni_mocked(is_valid=True), |
||
305 | "uni_z": get_uni_mocked(is_valid=True) |
||
306 | } |
||
307 | |||
308 | evc = EVC(**attributes) |
||
309 | evc.should_deploy() |
||
310 | log_mock.debug.assert_called_with('Path is empty.') |
||
311 | |||
312 | @patch('napps.kytos.mef_eline.models.log') |
||
313 | def test_should_deploy_case2(self, log_mock): |
||
314 | """Test should deploy method with disable circuit.""" |
||
315 | log_mock.debug.return_value = True |
||
316 | attributes = { |
||
317 | "name": "custom_name", |
||
318 | "uni_a": get_uni_mocked(is_valid=True), |
||
319 | "uni_z": get_uni_mocked(is_valid=True), |
||
320 | "primary_links": [get_link_mocked(), get_link_mocked()] |
||
321 | } |
||
322 | evc = EVC(**attributes) |
||
323 | |||
324 | self.assertFalse(evc.should_deploy(attributes['primary_links'])) |
||
325 | log_mock.debug.assert_called_with(f'{evc} is disabled.') |
||
326 | |||
327 | @patch('napps.kytos.mef_eline.models.log') |
||
328 | def test_should_deploy_case3(self, log_mock): |
||
329 | """Test should deploy method with enabled and not active circuit.""" |
||
330 | log_mock.debug.return_value = True |
||
331 | attributes = { |
||
332 | "name": "custom_name", |
||
333 | "uni_a": get_uni_mocked(is_valid=True), |
||
334 | "uni_z": get_uni_mocked(is_valid=True), |
||
335 | "primary_links": [get_link_mocked(), get_link_mocked()], |
||
336 | "enabled": True |
||
337 | } |
||
338 | evc = EVC(**attributes) |
||
339 | self.assertTrue(evc.should_deploy(attributes['primary_links'])) |
||
340 | log_mock.debug.assert_called_with(f'{evc} will be deployed.') |
||
341 | |||
342 | @patch('napps.kytos.mef_eline.models.log') |
||
343 | def test_should_deploy_case4(self, log_mock): |
||
344 | """Test should deploy method with enabled and active circuit.""" |
||
345 | log_mock.debug.return_value = True |
||
346 | attributes = { |
||
347 | "name": "custom_name", |
||
348 | "uni_a": get_uni_mocked(is_valid=True), |
||
349 | "uni_z": get_uni_mocked(is_valid=True), |
||
350 | "primary_links": [get_link_mocked(), get_link_mocked()], |
||
351 | "enabled": True, |
||
352 | "active": True |
||
353 | } |
||
354 | evc = EVC(**attributes) |
||
355 | self.assertFalse(evc.should_deploy(attributes['primary_links'])) |
||
356 | |||
357 | @patch('napps.kytos.mef_eline.models.requests') |
||
358 | def test_send_flow_mods(self, requests_mock): |
||
359 | """Test if you are sending flow_mods.""" |
||
360 | flow_mods = {"id": 20} |
||
361 | switch = Mock(spec=Switch, id=1) |
||
362 | EVC.send_flow_mods(switch, flow_mods) |
||
363 | expected_endpoint = f"{MANAGER_URL}/flows/{switch.id}" |
||
364 | expected_data = {"flows": flow_mods} |
||
365 | self.assertEqual(requests_mock.post.call_count, 1) |
||
366 | requests_mock.post.assert_called_once_with(expected_endpoint, |
||
367 | json=expected_data) |
||
368 | |||
369 | def test_prepare_flow_mod(self): |
||
370 | """Test prepare flow_mod method.""" |
||
371 | interface_a = Interface('eth0', 1, Mock(spec=Switch)) |
||
372 | interface_z = Interface('eth1', 3, Mock(spec=Switch)) |
||
373 | flow_mod = EVC.prepare_flow_mod(interface_a, interface_z) |
||
374 | expected_flow_mod = { |
||
375 | 'match': {'in_port': interface_a.port_number}, |
||
376 | 'actions': [ |
||
377 | {'action_type': 'output', |
||
378 | 'port': interface_z.port_number} |
||
379 | ] |
||
380 | } |
||
381 | self.assertEqual(expected_flow_mod, flow_mod) |
||
382 | |||
383 | def test_prepare_pop_flow(self): |
||
384 | """Test prepare pop flow method.""" |
||
385 | attributes = { |
||
386 | "name": "custom_name", |
||
387 | "uni_a": get_uni_mocked(interface_port=1, is_valid=True), |
||
388 | "uni_z": get_uni_mocked(interface_port=2, is_valid=True), |
||
389 | } |
||
390 | evc = EVC(**attributes) |
||
391 | interface_a = evc.uni_a.interface |
||
392 | interface_z = evc.uni_z.interface |
||
393 | in_vlan = 10 |
||
394 | flow_mod = evc.prepare_pop_flow(interface_a, interface_z, in_vlan) |
||
395 | expected_flow_mod = { |
||
396 | 'match': {'in_port': interface_a.port_number, 'dl_vlan': in_vlan}, |
||
397 | 'actions': [ |
||
398 | {'action_type': 'pop_vlan'}, |
||
399 | {'action_type': 'output', |
||
400 | 'port': interface_z.port_number |
||
401 | } |
||
402 | ] |
||
403 | } |
||
404 | self.assertEqual(expected_flow_mod, flow_mod) |
||
405 | |||
406 | def test_prepare_push_flow(self): |
||
407 | """Test prepare push flow method.""" |
||
408 | attributes = { |
||
409 | "name": "custom_name", |
||
410 | "uni_a": get_uni_mocked(interface_port=1, is_valid=True), |
||
411 | "uni_z": get_uni_mocked(interface_port=2, is_valid=True), |
||
412 | } |
||
413 | evc = EVC(**attributes) |
||
414 | interface_a = evc.uni_a.interface |
||
415 | interface_z = evc.uni_z.interface |
||
416 | in_vlan_a = 10 |
||
417 | out_vlan_a = 20 |
||
418 | in_vlan_z = 3 |
||
419 | flow_mod = evc.prepare_push_flow(interface_a, interface_z, |
||
420 | in_vlan_a, out_vlan_a, in_vlan_z) |
||
421 | expected_flow_mod = { |
||
422 | 'match': {'in_port': interface_a.port_number, |
||
423 | 'dl_vlan': in_vlan_a |
||
424 | }, |
||
425 | 'actions': [ |
||
426 | {'action_type': 'set_vlan', 'vlan_id': in_vlan_z}, |
||
427 | {'action_type': 'push_vlan', 'tag_type': 's'}, |
||
428 | {'action_type': 'set_vlan', 'vlan_id': out_vlan_a}, |
||
429 | {'action_type': 'output', |
||
430 | 'port': interface_z.port_number |
||
431 | } |
||
432 | ] |
||
433 | } |
||
434 | self.assertEqual(expected_flow_mod, flow_mod) |
||
435 | |||
436 | @staticmethod |
||
437 | @patch('napps.kytos.mef_eline.models.EVC.send_flow_mods') |
||
438 | def test_install_uni_flows(send_flow_mods_mock): |
||
439 | """Test install uni flows method. |
||
440 | |||
441 | This test will verify the flows send to the send_flows_mods method. |
||
442 | """ |
||
443 | uni_a = get_uni_mocked(interface_port=2, tag_value=82, |
||
444 | switch_id="switch_uni_a", is_valid=True) |
||
445 | uni_z = get_uni_mocked(interface_port=3, tag_value=83, |
||
446 | switch_id="switch_uni_z", is_valid=True) |
||
447 | |||
448 | attributes = { |
||
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 | evc = EVC(**attributes) |
||
460 | evc.install_uni_flows(attributes['primary_links']) |
||
461 | |||
462 | expected_flow_mod_a = [ |
||
463 | {'match': {'in_port': uni_a.interface.port_number, |
||
464 | 'dl_vlan': uni_a.user_tag.value}, |
||
465 | 'actions': [ |
||
466 | {'action_type': 'set_vlan', 'vlan_id': uni_z.user_tag.value}, |
||
467 | {'action_type': 'push_vlan', 'tag_type': 's'}, |
||
468 | {'action_type': 'set_vlan', |
||
469 | 'vlan_id': evc.primary_links[0].get_metadata('s_vlan').value}, |
||
470 | {'action_type': 'output', |
||
471 | 'port': evc.primary_links[0].endpoint_a.port_number} |
||
472 | ]}, |
||
473 | {'match': { |
||
474 | 'in_port': evc.primary_links[0].endpoint_a.port_number, |
||
475 | 'dl_vlan': evc.primary_links[0].get_metadata('s_vlan').value |
||
476 | }, |
||
477 | 'actions': [ |
||
478 | {'action_type': 'pop_vlan'}, |
||
479 | {'action_type': 'output', 'port': uni_a.interface.port_number} |
||
480 | ] |
||
481 | } |
||
482 | ] |
||
483 | send_flow_mods_mock.assert_any_call(uni_a.interface.switch, |
||
484 | expected_flow_mod_a) |
||
485 | |||
486 | expected_flow_mod_z = [ |
||
487 | {'match': {'in_port': uni_z.interface.port_number, |
||
488 | 'dl_vlan': uni_z.user_tag.value}, |
||
489 | 'actions': [ |
||
490 | {'action_type': 'set_vlan', 'vlan_id': uni_a.user_tag.value}, |
||
491 | {'action_type': 'push_vlan', 'tag_type': 's'}, |
||
492 | {'action_type': 'set_vlan', |
||
493 | 'vlan_id': evc.primary_links[-1].get_metadata('s_vlan').value |
||
494 | }, |
||
495 | {'action_type': 'output', |
||
496 | 'port': evc.primary_links[-1].endpoint_b.port_number} |
||
497 | ] |
||
498 | }, |
||
499 | {'match': { |
||
500 | 'in_port': evc.primary_links[-1].endpoint_b.port_number, |
||
501 | 'dl_vlan': evc.primary_links[-1].get_metadata('s_vlan').value |
||
502 | }, |
||
503 | 'actions': [ |
||
504 | {'action_type': 'pop_vlan'}, |
||
505 | {'action_type': 'output', 'port': uni_z.interface.port_number} |
||
506 | ] |
||
507 | } |
||
508 | ] |
||
509 | |||
510 | send_flow_mods_mock.assert_any_call(uni_z.interface.switch, |
||
511 | expected_flow_mod_z) |
||
512 | |||
513 | @staticmethod |
||
514 | @patch('napps.kytos.mef_eline.models.EVC.send_flow_mods') |
||
515 | def test_install_nni_flows(send_flow_mods_mock): |
||
516 | """Test install nni flows method. |
||
517 | |||
518 | This test will verify the flows send to the send_flows_mods method. |
||
519 | """ |
||
520 | uni_a = get_uni_mocked(interface_port=2, tag_value=82, |
||
521 | switch_id="switch_uni_a", is_valid=True) |
||
522 | uni_z = get_uni_mocked(interface_port=3, tag_value=83, |
||
523 | switch_id="switch_uni_z", is_valid=True) |
||
524 | |||
525 | attributes = { |
||
526 | "name": "custom_name", |
||
527 | "uni_a": uni_a, |
||
528 | "uni_z": uni_z, |
||
529 | "primary_links": [ |
||
530 | get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, |
||
531 | metadata={"s_vlan": 5}), |
||
532 | get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, |
||
533 | metadata={"s_vlan": 6}) |
||
534 | ] |
||
535 | } |
||
536 | evc = EVC(**attributes) |
||
537 | evc.install_nni_flows(attributes['primary_links']) |
||
538 | |||
539 | in_vlan = evc.primary_links[0].get_metadata('s_vlan').value |
||
540 | out_vlan = evc.primary_links[-1].get_metadata('s_vlan').value |
||
541 | |||
542 | in_port = evc.primary_links[0].endpoint_b.port_number |
||
543 | out_port = evc.primary_links[-1].endpoint_a.port_number |
||
544 | |||
545 | expected_flow_mods = [ |
||
546 | {'match': { |
||
547 | 'in_port': in_port, |
||
548 | 'dl_vlan': in_vlan}, |
||
549 | 'actions': [ |
||
550 | {'action_type': 'set_vlan', 'vlan_id': out_vlan}, |
||
551 | {'action_type': 'output', 'port': out_port} |
||
552 | ] |
||
553 | }, |
||
554 | {'match': {'in_port': out_port, 'dl_vlan': out_vlan}, |
||
555 | 'actions': [ |
||
556 | {'action_type': 'set_vlan', 'vlan_id': in_vlan}, |
||
557 | {'action_type': 'output', 'port': in_port} |
||
558 | ] |
||
559 | } |
||
560 | ] |
||
561 | |||
562 | switch = evc.primary_links[0].endpoint_b.switch |
||
563 | send_flow_mods_mock.assert_called_once_with(switch, expected_flow_mods) |
||
564 | |||
565 | View Code Duplication | @patch('napps.kytos.mef_eline.models.log') |
|
566 | @patch('napps.kytos.mef_eline.models.EVC.choose_vlans') |
||
567 | @patch('napps.kytos.mef_eline.models.EVC.install_nni_flows') |
||
568 | @patch('napps.kytos.mef_eline.models.EVC.install_uni_flows') |
||
569 | @patch('napps.kytos.mef_eline.models.EVC.activate') |
||
570 | @patch('napps.kytos.mef_eline.models.EVC.should_deploy') |
||
571 | def test_deploy_successfully(self, *args): |
||
572 | """Test if all methods to deploy are called.""" |
||
573 | (should_deploy_mock, activate_mock, install_uni_flows_mock, |
||
574 | install_nni_flows, chose_vlans_mock, log_mock) = args |
||
575 | |||
576 | should_deploy_mock.return_value = True |
||
577 | uni_a = get_uni_mocked(interface_port=2, tag_value=82, |
||
578 | switch_id="switch_uni_a", is_valid=True) |
||
579 | uni_z = get_uni_mocked(interface_port=3, tag_value=83, |
||
580 | switch_id="switch_uni_z", is_valid=True) |
||
581 | |||
582 | attributes = { |
||
583 | "name": "custom_name", |
||
584 | "uni_a": uni_a, |
||
585 | "uni_z": uni_z, |
||
586 | "primary_links": [ |
||
587 | get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, |
||
588 | metadata={"s_vlan": 5}), |
||
589 | get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, |
||
590 | metadata={"s_vlan": 6}) |
||
591 | ] |
||
592 | } |
||
593 | |||
594 | evc = EVC(**attributes) |
||
595 | evc.deploy() |
||
596 | |||
597 | self.assertEqual(should_deploy_mock.call_count, 1) |
||
598 | self.assertEqual(activate_mock.call_count, 1) |
||
599 | self.assertEqual(install_uni_flows_mock.call_count, 1) |
||
600 | self.assertEqual(install_nni_flows.call_count, 1) |
||
601 | self.assertEqual(chose_vlans_mock.call_count, 1) |
||
602 | log_mock.info.assert_called_once_with(f"{evc} was deployed.") |
||
603 | |||
604 | View Code Duplication | @patch('napps.kytos.mef_eline.models.log') |
|
605 | @patch('napps.kytos.mef_eline.models.EVC.choose_vlans') |
||
606 | @patch('napps.kytos.mef_eline.models.EVC.install_nni_flows') |
||
607 | @patch('napps.kytos.mef_eline.models.EVC.install_uni_flows') |
||
608 | @patch('napps.kytos.mef_eline.models.EVC.activate') |
||
609 | @patch('napps.kytos.mef_eline.models.EVC.should_deploy') |
||
610 | def test_deploy_fail(self, *args): |
||
611 | """Test if all methods is ignored when the should_deploy is false.""" |
||
612 | (should_deploy_mock, activate_mock, install_uni_flows_mock, |
||
613 | install_nni_flows, chose_vlans_mock, log_mock) = args |
||
614 | |||
615 | should_deploy_mock.return_value = False |
||
616 | uni_a = get_uni_mocked(interface_port=2, tag_value=82, |
||
617 | switch_id="switch_uni_a", is_valid=True) |
||
618 | uni_z = get_uni_mocked(interface_port=3, tag_value=83, |
||
619 | switch_id="switch_uni_z", is_valid=True) |
||
620 | |||
621 | attributes = { |
||
622 | "name": "custom_name", |
||
623 | "uni_a": uni_a, |
||
624 | "uni_z": uni_z, |
||
625 | "primary_links": [ |
||
626 | get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, |
||
627 | metadata={"s_vlan": 5}), |
||
628 | get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, |
||
629 | metadata={"s_vlan": 6}) |
||
630 | ] |
||
631 | } |
||
632 | |||
633 | evc = EVC(**attributes) |
||
634 | evc.deploy() |
||
635 | |||
636 | self.assertEqual(should_deploy_mock.call_count, 1) |
||
637 | self.assertEqual(activate_mock.call_count, 0) |
||
638 | self.assertEqual(install_uni_flows_mock.call_count, 0) |
||
639 | self.assertEqual(install_nni_flows.call_count, 0) |
||
640 | self.assertEqual(chose_vlans_mock.call_count, 0) |
||
641 | self.assertEqual(log_mock.info.call_count, 0) |
||
642 |