Passed
Pull Request — master (#491)
by Aldo
03:57
created

TestEVC.test_primary_links_zipped()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nop 1
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
"""Method to thest EVCDeploy class."""
2 1
import sys
3 1
from unittest.mock import MagicMock, Mock, call, patch
4 1
import operator
5 1
import pytest
6 1
from kytos.lib.helpers import get_controller_mock
7
8 1
from kytos.core.common import EntityStatus
9 1
from kytos.core.exceptions import KytosNoTagAvailableError
10 1
from kytos.core.interface import Interface
11 1
from kytos.core.switch import Switch
12 1
from httpx import TimeoutException
13
# pylint: disable=wrong-import-position
14 1
sys.path.insert(0, "/var/lib/kytos/napps/..")
15
# pylint: enable=wrong-import-position
16
17 1
from napps.kytos.mef_eline.exceptions import (FlowModException,   # NOQA
18
                                              EVCPathNotInstalled)
19 1
from napps.kytos.mef_eline.models import EVC, EVCDeploy, Path  # NOQA
20 1
from napps.kytos.mef_eline.settings import (ANY_SB_PRIORITY,  # NOQA
21
                                            EPL_SB_PRIORITY, EVPL_SB_PRIORITY,
22
                                            MANAGER_URL,
23
                                            SDN_TRACE_CP_URL,
24
                                            UNTAGGED_SB_PRIORITY)
25 1
from napps.kytos.mef_eline.tests.helpers import (get_link_mocked,  # NOQA
26
                                                 get_uni_mocked)
27
28
29
# pylint: disable=too-many-public-methods, too-many-lines
30 1
class TestEVC():
31
    """Tests to verify EVC class."""
32
33 1
    def setup_method(self):
34
        """Setup method"""
35 1
        attributes = {
36
            "controller": get_controller_mock(),
37
            "name": "circuit_for_tests",
38
            "uni_a": get_uni_mocked(is_valid=True),
39
            "uni_z": get_uni_mocked(is_valid=True),
40
        }
41 1
        self.evc_deploy = EVCDeploy(**attributes)
42
43 1
    def test_primary_links_zipped_empty(self):
44
        """Test primary links zipped method."""
45 1
        assert not self.evc_deploy.links_zipped(None)
46
47 1
    @staticmethod
48 1
    @patch("napps.kytos.mef_eline.models.evc.log")
49 1
    def test_should_deploy_case1(log_mock):
50
        """Test should deploy method without primary links."""
51 1
        log_mock.debug.return_value = True
52 1
        attributes = {
53
            "controller": get_controller_mock(),
54
            "name": "custom_name",
55
            "uni_a": get_uni_mocked(is_valid=True),
56
            "uni_z": get_uni_mocked(is_valid=True),
57
        }
58
59 1
        evc = EVC(**attributes)
60 1
        evc.should_deploy()
61 1
        log_mock.debug.assert_called_with("Path is empty.")
62
63 1 View Code Duplication
    @patch("napps.kytos.mef_eline.models.evc.log")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
64 1
    def test_should_deploy_case2(self, log_mock):
65
        """Test should deploy method with disable circuit."""
66 1
        log_mock.debug.return_value = True
67 1
        attributes = {
68
            "controller": get_controller_mock(),
69
            "name": "custom_name",
70
            "uni_a": get_uni_mocked(is_valid=True),
71
            "uni_z": get_uni_mocked(is_valid=True),
72
            "primary_links": [get_link_mocked(), get_link_mocked()],
73
        }
74 1
        evc = EVC(**attributes)
75
76 1
        assert evc.should_deploy(attributes["primary_links"]) is False
77 1
        log_mock.debug.assert_called_with(f"{evc} is disabled.")
78
79 1 View Code Duplication
    @patch("napps.kytos.mef_eline.models.evc.log")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
80 1
    def test_should_deploy_case3(self, log_mock):
81
        """Test should deploy method with enabled and not active circuit."""
82 1
        log_mock.debug.return_value = True
83 1
        attributes = {
84
            "controller": get_controller_mock(),
85
            "name": "custom_name",
86
            "uni_a": get_uni_mocked(is_valid=True),
87
            "uni_z": get_uni_mocked(is_valid=True),
88
            "primary_links": [get_link_mocked(), get_link_mocked()],
89
            "enabled": True,
90
        }
91 1
        evc = EVC(**attributes)
92 1
        assert evc.should_deploy(attributes["primary_links"]) is True
93 1
        log_mock.debug.assert_called_with(f"{evc} will be deployed.")
94
95 1
    @patch("napps.kytos.mef_eline.models.evc.log")
96 1
    def test_should_deploy_case4(self, log_mock):
97
        """Test should deploy method with enabled and active circuit."""
98 1
        log_mock.debug.return_value = True
99 1
        attributes = {
100
            "controller": get_controller_mock(),
101
            "name": "custom_name",
102
            "uni_a": get_uni_mocked(is_valid=True),
103
            "uni_z": get_uni_mocked(is_valid=True),
104
            "primary_links": [get_link_mocked(), get_link_mocked()],
105
            "enabled": True,
106
            "active": True,
107
        }
108 1
        evc = EVC(**attributes)
109 1
        assert evc.should_deploy(attributes["primary_links"]) is False
110
111 1
    @patch("napps.kytos.mef_eline.models.evc.httpx")
112 1
    def test_send_flow_mods_case1(self, httpx_mock):
113
        """Test if you are sending flow_mods."""
114 1
        flow_mods = {"id": 20}
115 1
        switch = Mock(spec=Switch, id=1)
116
117 1
        response = MagicMock()
118 1
        response.status_code = 201
119 1
        response.is_server_error = False
120 1
        httpx_mock.post.return_value = response
121
122
        # pylint: disable=protected-access
123 1
        EVC._send_flow_mods(switch.id, flow_mods)
124
125 1
        expected_endpoint = f"{MANAGER_URL}/flows/{switch.id}"
126 1
        expected_data = {"flows": flow_mods, "force": False}
127 1
        assert httpx_mock.post.call_count == 1
128 1
        httpx_mock.post.assert_called_once_with(
129
            expected_endpoint, json=expected_data, timeout=30
130
        )
131
132 1
    @patch("napps.kytos.mef_eline.models.evc.httpx")
133 1
    def test_send_flow_mods_case2(self, httpx_mock):
134
        """Test if you are sending flow_mods."""
135 1
        flow_mods = {"id": 20}
136 1
        switch = Mock(spec=Switch, id=1)
137 1
        response = MagicMock()
138 1
        response.status_code = 201
139 1
        response.is_server_error = False
140 1
        httpx_mock.post.return_value = response
141
142
        # pylint: disable=protected-access
143 1
        EVC._send_flow_mods(switch.id, flow_mods, command='delete', force=True)
144
145 1
        expected_endpoint = f"{MANAGER_URL}/delete/{switch.id}"
146 1
        expected_data = {"flows": flow_mods, "force": True}
147 1
        assert httpx_mock.post.call_count == 1
148 1
        httpx_mock.post.assert_called_once_with(
149
            expected_endpoint, json=expected_data, timeout=30
150
        )
151
152 1
    @patch("time.sleep")
153 1
    @patch("napps.kytos.mef_eline.models.evc.httpx")
154 1
    def test_send_flow_mods_error(self, httpx_mock, _):
155
        """Test flow_manager call fails."""
156 1
        flow_mods = {"id": 20}
157 1
        switch = Mock(spec=Switch, id=1)
158 1
        response = MagicMock()
159 1
        response.status_code = 415
160 1
        httpx_mock.post.return_value = response
161
162
        # pylint: disable=protected-access
163 1
        with pytest.raises(FlowModException):
164 1
            EVC._send_flow_mods(
165
                switch.id,
166
                flow_mods,
167
                command='delete',
168
                force=True
169
            )
170 1
        assert httpx_mock.post.call_count == 3
171
172 1
    def test_prepare_flow_mod(self):
173
        """Test prepare flow_mod method."""
174 1
        interface_a = Interface("eth0", 1, Mock(spec=Switch))
175 1
        interface_z = Interface("eth1", 3, Mock(spec=Switch))
176 1
        attributes = {
177
            "table_group": {"epl": 0, "evpl": 0},
178
            "controller": get_controller_mock(),
179
            "name": "custom_name",
180
            "uni_a": get_uni_mocked(is_valid=True),
181
            "uni_z": get_uni_mocked(is_valid=True),
182
            "primary_links": [get_link_mocked(), get_link_mocked()],
183
            "enabled": True,
184
            "active": True,
185
        }
186 1
        evc = EVC(**attributes)
187
188
        # pylint: disable=protected-access
189 1
        flow_mod = evc._prepare_flow_mod(interface_a, interface_z)
190 1
        expected_flow_mod = {
191
            "match": {"in_port": interface_a.port_number},
192
            "cookie": evc.get_cookie(),
193
            "owner": "mef_eline",
194
            "actions": [
195
                {"action_type": "output", "port": interface_z.port_number}
196
            ],
197
            "priority": EVPL_SB_PRIORITY,
198
            "table_group": "evpl",
199
            "table_id": 0,
200
        }
201 1
        assert expected_flow_mod == flow_mod
202
203 1
        evc.sb_priority = 1234
204 1
        flow_mod = evc._prepare_flow_mod(interface_a, interface_z, 3)
205 1
        assert flow_mod["priority"] == 1234
206 1
        assert flow_mod["actions"][1]["action_type"] == "set_queue"
207 1
        assert flow_mod["actions"][1]["queue_id"] == 3
208
209 1
    def test_prepare_pop_flow(self):
210
        """Test prepare pop flow  method."""
211 1
        attributes = {
212
            "table_group": {"epl": 0, "evpl": 0},
213
            "controller": get_controller_mock(),
214
            "name": "custom_name",
215
            "uni_a": get_uni_mocked(interface_port=1, is_valid=True),
216
            "uni_z": get_uni_mocked(interface_port=2, is_valid=True),
217
        }
218 1
        evc = EVC(**attributes)
219 1
        interface_a = evc.uni_a.interface
220 1
        interface_z = evc.uni_z.interface
221 1
        in_vlan = 10
222
223
        # pylint: disable=protected-access
224 1
        flow_mod = evc._prepare_pop_flow(
225
            interface_a, interface_z, in_vlan
226
        )
227
228 1
        expected_flow_mod = {
229
            "match": {"in_port": interface_a.port_number,
230
                      "dl_vlan": in_vlan},
231
            "cookie": evc.get_cookie(),
232
            "owner": "mef_eline",
233
            "actions": [
234
                {"action_type": "pop_vlan"},
235
                {"action_type": "output", "port": interface_z.port_number},
236
            ],
237
            "priority": EVPL_SB_PRIORITY,
238
            "table_group": "evpl",
239
            "table_id": 0,
240
        }
241 1
        assert expected_flow_mod == flow_mod
242
243
    # pylint: disable=too-many-branches
244 1
    @pytest.mark.parametrize(
245
        "in_vlan_a,in_vlan_z",
246
        [
247
            (100, 50),
248
            (100, 100),
249
            (100, "4096/4096"),
250
            (100, 0),
251
            (100, None),
252
            ("4096/4096", 50),
253
            ("4096/4096", "4096/4096"),
254
            ("4096/4096", 0),
255
            ("4096/4096", None),
256
            (0, 50),
257
            (0, "4096/4096"),
258
            (0, 0),
259
            (0, None),
260
            (None, 50),
261
            (None, "4096/4096"),
262
            (None, 0),
263
            (None, None),
264
        ]
265
    )
266 1
    def test_prepare_push_flow(self, in_vlan_a, in_vlan_z):
267
        """Test prepare push flow method."""
268 1
        attributes = {
269
            "table_group": {"evpl": 3, "epl": 4},
270
            "controller": get_controller_mock(),
271
            "name": "custom_name",
272
            "uni_a": get_uni_mocked(interface_port=1, is_valid=True),
273
            "uni_z": get_uni_mocked(interface_port=2, is_valid=True),
274
        }
275 1
        evc = EVC(**attributes)
276 1
        interface_a = evc.uni_a.interface
277 1
        interface_z = evc.uni_z.interface
278 1
        out_vlan_a = 20
279
280
        # pylint: disable=protected-access
281 1
        flow_mod = evc._prepare_push_flow(interface_a, interface_z,
282
                                          in_vlan_a, out_vlan_a,
283
                                          in_vlan_z)
284 1
        expected_flow_mod = {
285
            'match': {'in_port': interface_a.port_number},
286
            'cookie': evc.get_cookie(),
287
            'owner': 'mef_eline',
288
            'table_id': 3,
289
            'table_group': 'evpl',
290
            'actions': [
291
                {'action_type': 'push_vlan', 'tag_type': 's'},
292
                {'action_type': 'set_vlan', 'vlan_id': out_vlan_a},
293
                {
294
                    'action_type': 'output',
295
                    'port': interface_z.port_number
296
                }
297
            ],
298
            "priority": EVPL_SB_PRIORITY,
299
        }
300 1
        expected_flow_mod["priority"] = evc.get_priority(in_vlan_a)
301 1
        if in_vlan_a is not None:
302 1
            expected_flow_mod['match']['dl_vlan'] = in_vlan_a
303 1
        if in_vlan_z not in evc.special_cases and in_vlan_a != in_vlan_z:
304 1
            new_action = {"action_type": "set_vlan",
305
                          "vlan_id": in_vlan_z}
306 1
            expected_flow_mod["actions"].insert(0, new_action)
307 1
        if in_vlan_a not in evc.special_cases:
308 1
            if in_vlan_z == 0:
309 1
                new_action = {"action_type": "pop_vlan"}
310 1
                expected_flow_mod["actions"].insert(0, new_action)
311 1
        elif in_vlan_a == "4096/4096":
312 1
            if in_vlan_z == 0:
313 1
                new_action = {"action_type": "pop_vlan"}
314 1
                expected_flow_mod["actions"].insert(0, new_action)
315 1
        elif not in_vlan_a:
316 1
            if in_vlan_a is None:
317 1
                expected_flow_mod["table_group"] = "epl"
318 1
                expected_flow_mod["table_id"] = 4
319 1
            if in_vlan_z not in evc.special_cases:
320 1
                new_action = {"action_type": "push_vlan",
321
                              "tag_type": "c"}
322 1
                expected_flow_mod["actions"].insert(0, new_action)
323 1
        assert expected_flow_mod == flow_mod
324
325 1
    @staticmethod
326 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._send_flow_mods")
327 1
    def test_install_uni_flows(send_flow_mods_mock):
328
        """Test install uni flows method.
329
330
        This test will verify the flows send to the send_flow_mods method.
331
        """
332 1
        evc = TestEVC.create_evc_inter_switch()
333
334
        # pylint: disable=protected-access
335 1
        evc._install_uni_flows()
336 1
        send_flow_mods_mock.assert_not_called()
337
338
        # pylint: disable=protected-access
339 1
        uni_flows = evc._install_uni_flows(evc.primary_links)
340 1
        assert uni_flows
341 1
        assert list(uni_flows.keys()) == [evc.uni_a.interface.switch.id,
342
                                          evc.uni_z.interface.switch.id]
343
344 1
        expected_flow_mod_a = [
345
            {
346
                "match": {
347
                    "in_port": evc.uni_a.interface.port_number,
348
                    "dl_vlan": evc.uni_a.user_tag.value,
349
                },
350
                "cookie": evc.get_cookie(),
351
                "owner": "mef_eline",
352
                "table_group": "evpl",
353
                "table_id": 0,
354
                "actions": [
355
                    {
356
                        "action_type": "set_vlan",
357
                        "vlan_id": evc.uni_z.user_tag.value
358
                    },
359
                    {"action_type": "push_vlan", "tag_type": "s"},
360
                    {
361
                        "action_type": "set_vlan",
362
                        "vlan_id": evc.primary_links[0]
363
                        .get_metadata("s_vlan")
364
                        .value,
365
                    },
366
                    {
367
                        "action_type": "output",
368
                        "port": evc.primary_links[0].endpoint_a.port_number,
369
                    },
370
                ],
371
                "priority": EVPL_SB_PRIORITY,
372
            },
373
            {
374
                "match": {
375
                    "in_port": evc.primary_links[0].endpoint_a.port_number,
376
                    "dl_vlan": evc.primary_links[0]
377
                    .get_metadata("s_vlan")
378
                    .value,
379
                },
380
                "cookie": evc.get_cookie(),
381
                "owner": "mef_eline",
382
                "table_group": "evpl",
383
                "table_id": 0,
384
                "actions": [
385
                    {"action_type": "pop_vlan"},
386
                    {
387
                        "action_type": "output",
388
                        "port": evc.uni_a.interface.port_number,
389
                    },
390
                ],
391
                "priority": EVPL_SB_PRIORITY,
392
            },
393
        ]
394
395 1
        send_flow_mods_mock.assert_any_call(
396
            evc.uni_a.interface.switch.id, expected_flow_mod_a
397
        )
398
399 1
        expected_flow_mod_z = [
400
            {
401
                "match": {
402
                    "in_port": evc.uni_z.interface.port_number,
403
                    "dl_vlan": evc.uni_z.user_tag.value,
404
                },
405
                "cookie": evc.get_cookie(),
406
                "owner": "mef_eline",
407
                "table_group": "evpl",
408
                "table_id": 0,
409
                "actions": [
410
                    {
411
                        "action_type": "set_vlan",
412
                        "vlan_id": evc.uni_a.user_tag.value
413
                    },
414
                    {"action_type": "push_vlan", "tag_type": "s"},
415
                    {
416
                        "action_type": "set_vlan",
417
                        "vlan_id": evc.primary_links[-1]
418
                        .get_metadata("s_vlan")
419
                        .value,
420
                    },
421
                    {
422
                        "action_type": "output",
423
                        "port": evc.primary_links[-1].endpoint_b.port_number,
424
                    },
425
                ],
426
                "priority": EVPL_SB_PRIORITY,
427
            },
428
            {
429
                "match": {
430
                    "in_port": evc.primary_links[-1].endpoint_b.port_number,
431
                    "dl_vlan": evc.primary_links[-1]
432
                    .get_metadata("s_vlan")
433
                    .value,
434
                },
435
                "cookie": evc.get_cookie(),
436
                "owner": "mef_eline",
437
                "table_group": "evpl",
438
                "table_id": 0,
439
                "actions": [
440
                    {"action_type": "pop_vlan"},
441
                    {
442
                        "action_type": "output",
443
                        "port": evc.uni_z.interface.port_number,
444
                    },
445
                ],
446
                "priority": EVPL_SB_PRIORITY,
447
            },
448
        ]
449
450 1
        send_flow_mods_mock.assert_any_call(
451
            evc.uni_z.interface.switch.id, expected_flow_mod_z
452
        )
453
454 1
    @staticmethod
455 1
    def create_evc_inter_switch(tag_value_a=82, tag_value_z=83):
456
        """Create inter-switch EVC with two links in the path"""
457 1
        uni_a = get_uni_mocked(
458
            interface_port=2,
459
            tag_value=tag_value_a,
460
            switch_id=1,
461
            switch_dpid=1,
462
            is_valid=True,
463
        )
464 1
        uni_z = get_uni_mocked(
465
            interface_port=3,
466
            tag_value=tag_value_z,
467
            switch_id=3,
468
            switch_dpid=3,
469
            is_valid=True,
470
        )
471
472 1
        attributes = {
473
            "controller": get_controller_mock(),
474
            "name": "custom_name",
475
            "id": "1",
476
            "uni_a": uni_a,
477
            "uni_z": uni_z,
478
            "primary_links": [
479
                get_link_mocked(
480
                    switch_a=Switch(1),
481
                    switch_b=Switch(2),
482
                    endpoint_a_port=9,
483
                    endpoint_b_port=10,
484
                    metadata={"s_vlan": 5},
485
                ),
486
                get_link_mocked(
487
                    switch_a=Switch(2),
488
                    switch_b=Switch(3),
489
                    endpoint_a_port=11,
490
                    endpoint_b_port=12,
491
                    metadata={"s_vlan": 6},
492
                ),
493
            ],
494
            "table_group": {"epl": 0, "evpl": 0}
495
        }
496 1
        return EVC(**attributes)
497
498 1
    @staticmethod
499 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._send_flow_mods")
500 1
    def test_install_nni_flows(send_flow_mods_mock):
501
        """Test install nni flows method.
502
503
        This test will verify the flows send to the send_flow_mods method.
504
        """
505 1
        evc = TestEVC.create_evc_inter_switch()
506
507
        # pylint: disable=protected-access
508 1
        nni_flows = evc._install_nni_flows(evc.primary_links)
509 1
        assert nni_flows
510 1
        dpid = evc.primary_links[0].endpoint_b.switch.id
511 1
        assert list(nni_flows.keys()) == [dpid]
512
513 1
        in_vlan = evc.primary_links[0].get_metadata("s_vlan").value
514 1
        out_vlan = evc.primary_links[-1].get_metadata("s_vlan").value
515
516 1
        in_port = evc.primary_links[0].endpoint_b.port_number
517 1
        out_port = evc.primary_links[-1].endpoint_a.port_number
518
519 1
        expected_flow_mods = [
520
            {
521
                "match": {"in_port": in_port, "dl_vlan": in_vlan},
522
                "cookie": evc.get_cookie(),
523
                "owner": "mef_eline",
524
                "table_group": "evpl",
525
                "table_id": 0,
526
                "actions": [
527
                    {"action_type": "set_vlan", "vlan_id": out_vlan},
528
                    {"action_type": "output", "port": out_port},
529
                ],
530
                "priority": EVPL_SB_PRIORITY
531
            },
532
            {
533
                "match": {"in_port": out_port, "dl_vlan": out_vlan},
534
                "cookie": evc.get_cookie(),
535
                "owner": "mef_eline",
536
                "table_group": "evpl",
537
                "table_id": 0,
538
                "actions": [
539
                    {"action_type": "set_vlan", "vlan_id": in_vlan},
540
                    {"action_type": "output", "port": in_port},
541
                ],
542
                "priority": EVPL_SB_PRIORITY,
543
            },
544
        ]
545
546 1
        dpid = evc.primary_links[0].endpoint_b.switch.id
547 1
        send_flow_mods_mock.assert_called_once_with(dpid, expected_flow_mods)
548
549 1
    @patch("napps.kytos.mef_eline.models.evc.EVCDeploy._send_flow_mods")
550 1
    @patch("napps.kytos.mef_eline.models.evc.EVCDeploy._prepare_nni_flows")
551 1
    def test_install_nni_flows_error(self, prepare_nni_mock, send_flow_mock):
552
        """Test _install_nni_flows with error"""
553 1
        prepare_nni_mock.return_value = {'1': 1}
554 1
        send_flow_mock.side_effect = FlowModException('err')
555 1
        with pytest.raises(EVCPathNotInstalled):
556 1
            self.evc_deploy._install_nni_flows()
557
558 1
    @patch("httpx.post")
559 1
    @patch("napps.kytos.mef_eline.controllers.ELineController.upsert_evc")
560 1
    @patch("napps.kytos.mef_eline.models.evc.log")
561 1
    @patch("napps.kytos.mef_eline.models.path.Path.choose_vlans")
562 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._install_nni_flows")
563 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._install_uni_flows")
564 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._install_direct_uni_flows")
565 1
    @patch("napps.kytos.mef_eline.models.evc.EVC.activate")
566 1
    @patch("napps.kytos.mef_eline.models.evc.EVC.should_deploy")
567 1
    def test_deploy_successfully(self, *args):
568
        """Test if all methods to deploy are called."""
569
        # pylint: disable=too-many-locals
570 1
        (
571
            should_deploy_mock,
572
            activate_mock,
573
            install_direct_uni_flows_mock,
574
            install_uni_flows_mock,
575
            install_nni_flows,
576
            chose_vlans_mock,
577
            log_mock,
578
            _,
579
            httpx_mock,
580
        ) = args
581
582 1
        response = MagicMock()
583 1
        response.status_code = 201
584 1
        response.is_server_error = False
585 1
        httpx_mock.return_value = response
586
587 1
        should_deploy_mock.return_value = True
588 1
        evc = self.create_evc_inter_switch()
589 1
        deployed = evc.deploy_to_path(evc.primary_links)
590
591 1
        assert should_deploy_mock.call_count == 1
592 1
        assert activate_mock.call_count == 1
593 1
        assert install_uni_flows_mock.call_count == 1
594 1
        assert install_nni_flows.call_count == 1
595 1
        assert chose_vlans_mock.call_count == 1
596 1
        log_mock.info.assert_called_with(f"{evc} was deployed.")
597 1
        assert deployed is True
598
599
        # intra switch EVC
600 1
        evc = self.create_evc_intra_switch()
601 1
        assert evc.deploy_to_path(evc.primary_links) is True
602 1
        assert install_direct_uni_flows_mock.call_count == 1
603 1
        assert activate_mock.call_count == 2
604 1
        assert log_mock.info.call_count == 2
605 1
        log_mock.info.assert_called_with(f"{evc} was deployed.")
606
607 1
    @patch("httpx.post")
608 1
    @patch("napps.kytos.mef_eline.models.evc.log")
609 1
    @patch("napps.kytos.mef_eline.models.evc.EVC.discover_new_paths")
610 1
    @patch("napps.kytos.mef_eline.models.path.Path.choose_vlans")
611 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._install_nni_flows")
612 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._install_uni_flows")
613 1
    @patch("napps.kytos.mef_eline.models.evc.EVC.activate")
614 1
    @patch("napps.kytos.mef_eline.models.evc.EVC.should_deploy")
615 1
    @patch("napps.kytos.mef_eline.models.EVC.sync")
616 1
    def test_deploy_fail(self, *args):
617
        """Test if all methods is ignored when the should_deploy is false."""
618
        # pylint: disable=too-many-locals
619 1
        (
620
            sync_mock,
621
            should_deploy_mock,
622
            activate_mock,
623
            install_uni_flows_mock,
624
            install_nni_flows,
625
            choose_vlans_mock,
626
            discover_new_paths_mock,
627
            log_mock,
628
            httpx_mock,
629
        ) = args
630
631 1
        response = MagicMock()
632 1
        response.status_code = 201
633 1
        httpx_mock.return_value = response
634
635 1
        evc = self.create_evc_inter_switch()
636 1
        should_deploy_mock.return_value = False
637 1
        discover_new_paths_mock.return_value = []
638 1
        deployed = evc.deploy_to_path()
639
640 1
        assert discover_new_paths_mock.call_count == 1
641 1
        assert should_deploy_mock.call_count == 1
642 1
        assert activate_mock.call_count == 0
643 1
        assert install_uni_flows_mock.call_count == 0
644 1
        assert install_nni_flows.call_count == 0
645 1
        assert choose_vlans_mock.call_count == 0
646 1
        assert log_mock.info.call_count == 0
647 1
        assert sync_mock.call_count == 0
648 1
        assert deployed is False
649
650
        # NoTagAvailable on static path
651 1
        should_deploy_mock.return_value = True
652 1
        choose_vlans_mock.side_effect = KytosNoTagAvailableError(MagicMock())
653 1
        assert evc.deploy_to_path(evc.primary_links) is False
654
655
        # NoTagAvailable on dynamic path
656 1
        should_deploy_mock.return_value = False
657 1
        discover_new_paths_mock.return_value = [Path(['a', 'b'])]
658 1
        choose_vlans_mock.side_effect = KytosNoTagAvailableError(MagicMock())
659 1
        assert evc.deploy_to_path(evc.primary_links) is False
660
661 1
    @patch("napps.kytos.mef_eline.models.evc.log")
662 1
    @patch(
663
        "napps.kytos.mef_eline.models.evc.EVC.discover_new_paths",
664
        return_value=[],
665
    )
666 1
    @patch("napps.kytos.mef_eline.models.path.Path.choose_vlans")
667 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._install_nni_flows")
668 1
    @patch("napps.kytos.mef_eline.models.evc.EVC.should_deploy")
669 1
    @patch("napps.kytos.mef_eline.models.evc.EVC.remove_current_flows")
670 1
    @patch("napps.kytos.mef_eline.models.evc.EVC.sync")
671 1
    def test_deploy_error(self, *args):
672
        """Test if all methods is ignored when the should_deploy is false."""
673
        # pylint: disable=too-many-locals
674 1
        (
675
            sync_mock,
676
            remove_current_flows,
677
            should_deploy_mock,
678
            install_nni_flows,
679
            choose_vlans_mock,
680
            discover_new_paths,
681
            log_mock,
682
        ) = args
683
684 1
        install_nni_flows.side_effect = EVCPathNotInstalled
685 1
        should_deploy_mock.return_value = True
686 1
        uni_a = get_uni_mocked(
687
            interface_port=2,
688
            tag_value=82,
689
            switch_id="switch_uni_a",
690
            is_valid=True,
691
        )
692 1
        uni_z = get_uni_mocked(
693
            interface_port=3,
694
            tag_value=83,
695
            switch_id="switch_uni_z",
696
            is_valid=True,
697
        )
698
699 1
        primary_links = [
700
            get_link_mocked(
701
                endpoint_a_port=9, endpoint_b_port=10, metadata={"s_vlan": 5}
702
            ),
703
            get_link_mocked(
704
                endpoint_a_port=11, endpoint_b_port=12, metadata={"s_vlan": 6}
705
            ),
706
        ]
707
708 1
        attributes = {
709
            "controller": get_controller_mock(),
710
            "name": "custom_name",
711
            "uni_a": uni_a,
712
            "uni_z": uni_z,
713
            "primary_links": primary_links,
714
            "queue_id": 5,
715
        }
716
        # Setup path to deploy
717 1
        path = Path()
718 1
        path.append(primary_links[0])
719 1
        path.append(primary_links[1])
720
721 1
        evc = EVC(**attributes)
722
723 1
        deployed = evc.deploy_to_path(path)
724
725 1
        assert discover_new_paths.call_count == 0
726 1
        assert should_deploy_mock.call_count == 1
727 1
        assert install_nni_flows.call_count == 1
728 1
        assert choose_vlans_mock.call_count == 1
729 1
        assert log_mock.error.call_count == 1
730 1
        assert sync_mock.call_count == 0
731 1
        assert remove_current_flows.call_count == 2
732 1
        assert deployed is False
733
734 1
    @patch("napps.kytos.mef_eline.models.evc.emit_event")
735 1
    @patch("napps.kytos.mef_eline.models.evc.EVC.get_failover_path_candidates")
736 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._install_nni_flows")
737 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._install_uni_flows")
738 1
    @patch("napps.kytos.mef_eline.models.evc.EVC.remove_path_flows")
739 1
    @patch("napps.kytos.mef_eline.models.EVC.sync")
740 1
    def test_setup_failover_path(self, *args):
741
        """Test setup_failover_path method."""
742 1
        (
743
            sync_mock,
744
            remove_path_flows_mock,
745
            install_uni_flows_mock,
746
            install_nni_flows_mock,
747
            get_failover_path_candidates_mock,
748
            emit_event_mock,
749
        ) = args
750
751
        # case1: early return intra switch
752 1
        evc1 = self.create_evc_intra_switch()
753
754 1
        assert evc1.setup_failover_path() is False
755 1
        assert sync_mock.call_count == 0
756
757
        # case2: early return not eligible for path failover
758 1
        evc2 = self.create_evc_inter_switch()
759 1
        evc2.is_eligible_for_failover_path = MagicMock(return_value=False)
760
761 1
        assert evc2.setup_failover_path() is False
762 1
        assert sync_mock.call_count == 0
763
764
        # case3: success failover_path setup
765 1
        evc2.is_eligible_for_failover_path = MagicMock(return_value=True)
766 1
        evc2.failover_path = ["link1", "link2"]
767 1
        path_mock = MagicMock()
768 1
        path_mock.__iter__.return_value = ["link3"]
769 1
        get_failover_path_candidates_mock.return_value = [None, path_mock]
770 1
        mock_choose = path_mock.choose_vlans
771
772 1
        assert evc2.setup_failover_path() is True
773 1
        remove_path_flows_mock.assert_called_with(["link1", "link2"])
774 1
        mock_choose.assert_called()
775 1
        install_nni_flows_mock.assert_called_with(path_mock)
776 1
        install_uni_flows_mock.assert_called_with(path_mock, skip_in=True)
777 1
        assert evc2.failover_path == path_mock
778 1
        assert sync_mock.call_count == 1
779 1
        assert emit_event_mock.call_count == 1
780 1
        assert emit_event_mock.call_args[0][1] == "failover_deployed"
781
782
        # case 4: failed to setup failover_path - No Tag available
783 1
        evc2.failover_path = []
784 1
        mock_choose.side_effect = KytosNoTagAvailableError(MagicMock())
785 1
        sync_mock.call_count = 0
786
787 1
        assert evc2.setup_failover_path() is False
788 1
        assert len(list(evc2.failover_path)) == 0
789 1
        assert sync_mock.call_count == 1
790
791
        # case 5: failed to setup failover_path - FlowMod exception
792 1
        evc2.failover_path = []
793 1
        mock_choose.side_effect = None
794 1
        install_nni_flows_mock.side_effect = EVCPathNotInstalled("error")
795 1
        sync_mock.call_count = 0
796
797 1
        assert evc2.setup_failover_path() is False
798 1
        assert len(list(evc2.failover_path)) == 0
799 1
        assert sync_mock.call_count == 1
800 1
        remove_path_flows_mock.assert_called_with(path_mock)
801
802 1
    @patch("napps.kytos.mef_eline.models.evc.EVC.deploy_to_path")
803 1
    @patch("napps.kytos.mef_eline.models.evc.EVC.discover_new_paths")
804 1
    def test_deploy_to_backup_path1(
805
        self, discover_new_paths_mocked, deploy_to_path_mocked
806
    ):
807
        """Test deployment when dynamic_backup_path is False in same switch"""
808 1
        uni_a = get_uni_mocked(interface_port=2, tag_value=82, is_valid=True)
809 1
        uni_z = get_uni_mocked(interface_port=3, tag_value=83, is_valid=True)
810
811 1
        switch = Mock(spec=Switch)
812 1
        uni_a.interface.switch = switch
813 1
        uni_z.interface.switch = switch
814
815 1
        attributes = {
816
            "controller": get_controller_mock(),
817
            "name": "custom_name",
818
            "uni_a": uni_a,
819
            "uni_z": uni_z,
820
            "enabled": True,
821
            "dynamic_backup_path": False,
822
        }
823
824 1
        evc = EVC(**attributes)
825 1
        discover_new_paths_mocked.return_value = []
826 1
        deploy_to_path_mocked.return_value = True
827
828 1
        deployed = evc.deploy_to_backup_path()
829
830 1
        deploy_to_path_mocked.assert_called_once_with()
831 1
        assert deployed is True
832
833 1
    @patch("httpx.post")
834 1
    @patch("napps.kytos.mef_eline.controllers.ELineController.upsert_evc")
835 1
    @patch("napps.kytos.mef_eline.models.evc.log")
836 1
    @patch("napps.kytos.mef_eline.models.path.Path.choose_vlans")
837 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._install_nni_flows")
838 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._install_uni_flows")
839 1
    @patch("napps.kytos.mef_eline.models.evc.EVC.activate")
840 1
    @patch("napps.kytos.mef_eline.models.evc.EVC.should_deploy")
841 1
    @patch("napps.kytos.mef_eline.models.evc.EVC.discover_new_paths")
842 1
    def test_deploy_without_path_case1(self, *args):
843
        """Test if not path is found a dynamic path is used."""
844
        # pylint: disable=too-many-locals
845 1
        (
846
            discover_new_paths_mocked,
847
            should_deploy_mock,
848
            activate_mock,
849
            install_uni_flows_mock,
850
            install_nni_flows,
851
            chose_vlans_mock,
852
            log_mock,
853
            _,
854
            httpx_mock,
855
        ) = args
856
857 1
        response = MagicMock()
858 1
        response.status_code = 201
859 1
        httpx_mock.return_value = response
860
861 1
        should_deploy_mock.return_value = False
862 1
        uni_a = get_uni_mocked(
863
            interface_port=2,
864
            tag_value=82,
865
            switch_id="switch_uni_a",
866
            is_valid=True,
867
        )
868 1
        uni_z = get_uni_mocked(
869
            interface_port=3,
870
            tag_value=83,
871
            switch_id="switch_uni_z",
872
            is_valid=True,
873
        )
874
875 1
        attributes = {
876
            "controller": get_controller_mock(),
877
            "name": "custom_name",
878
            "uni_a": uni_a,
879
            "uni_z": uni_z,
880
            "enabled": True,
881
            "dynamic_backup_path": False,
882
        }
883
884 1
        dynamic_backup_path = Path(
885
            [
886
                get_link_mocked(
887
                    endpoint_a_port=9,
888
                    endpoint_b_port=10,
889
                    metadata={"s_vlan": 5},
890
                ),
891
                get_link_mocked(
892
                    endpoint_a_port=11,
893
                    endpoint_b_port=12,
894
                    metadata={"s_vlan": 6},
895
                ),
896
            ]
897
        )
898
899 1
        evc = EVC(**attributes)
900 1
        discover_new_paths_mocked.return_value = [dynamic_backup_path]
901
902 1
        deployed = evc.deploy_to_path()
903
904 1
        assert should_deploy_mock.call_count == 1
905 1
        assert discover_new_paths_mocked.call_count == 1
906 1
        assert activate_mock.call_count == 1
907 1
        assert install_uni_flows_mock.call_count == 1
908 1
        assert install_nni_flows.call_count == 1
909 1
        assert chose_vlans_mock.call_count == 1
910 1
        log_mock.info.assert_called_with(f"{evc} was deployed.")
911 1
        assert deployed is True
912
913 1
    @patch("napps.kytos.mef_eline.models.evc.EVCDeploy.deploy_to_primary_path")
914 1
    @patch("napps.kytos.mef_eline.models.evc.EVCDeploy.deploy_to_backup_path")
915 1
    @patch("napps.kytos.mef_eline.models.evc.emit_event")
916 1
    def test_deploy(self, *args):
917
        """Test method deploy"""
918 1
        (emit_event_mock, deploy_primary_mock, deploy_backup_mock) = args
919
920
        # case 1: deploy to primary
921 1
        self.evc_deploy.archived = False
922 1
        deploy_primary_mock.return_value = True
923 1
        assert self.evc_deploy.deploy()
924 1
        assert emit_event_mock.call_count == 1
925
926
        # case 2: deploy to backup
927 1
        deploy_primary_mock.return_value = False
928 1
        deploy_backup_mock.return_value = True
929 1
        assert self.evc_deploy.deploy()
930 1
        assert emit_event_mock.call_count == 2
931
932
        # case 3: fail to deploy to primary and backup
933 1
        deploy_backup_mock.return_value = False
934 1
        assert self.evc_deploy.deploy() is False
935 1
        assert emit_event_mock.call_count == 2
936
937
        # case 4: archived
938 1
        self.evc_deploy.archived = True
939 1
        assert self.evc_deploy.deploy() is False
940 1
        assert emit_event_mock.call_count == 2
941
942 1
    @patch("napps.kytos.mef_eline.models.evc.EVCDeploy.remove_current_flows")
943 1
    @patch("napps.kytos.mef_eline.models.evc.EVCDeploy.sync")
944 1
    @patch("napps.kytos.mef_eline.models.evc.emit_event")
945 1
    def test_remove(self, *args):
946
        """Test method remove"""
947 1
        (emit_event_mock, sync_mock, remove_flows_mock) = args
948 1
        self.evc_deploy.remove()
949 1
        remove_flows_mock.assert_called()
950 1
        sync_mock.assert_called()
951 1
        emit_event_mock.assert_called()
952 1
        assert self.evc_deploy.is_enabled() is False
953
954 1
    @patch("napps.kytos.mef_eline.controllers.ELineController.upsert_evc")
955 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._send_flow_mods")
956 1
    @patch("napps.kytos.mef_eline.models.evc.log.error")
957 1
    def test_remove_current_flows(self, *args):
958
        """Test remove current flows."""
959
        # pylint: disable=too-many-locals
960 1
        (log_error_mock, send_flow_mods_mocked, _) = args
961 1
        uni_a = get_uni_mocked(
962
            interface_port=2,
963
            tag_value=82,
964
            switch_id="switch_uni_a",
965
            is_valid=True,
966
        )
967 1
        uni_z = get_uni_mocked(
968
            interface_port=3,
969
            tag_value=83,
970
            switch_id="switch_uni_z",
971
            is_valid=True,
972
        )
973
974 1
        switch_a = Switch("00:00:00:00:00:01")
975 1
        switch_b = Switch("00:00:00:00:00:02")
976 1
        switch_c = Switch("00:00:00:00:00:03")
977
978 1
        attributes = {
979
            "controller": get_controller_mock(),
980
            "name": "custom_name",
981
            "uni_a": uni_a,
982
            "uni_z": uni_z,
983
            "active": True,
984
            "enabled": True,
985
            "primary_links": [
986
                get_link_mocked(
987
                    switch_a=switch_a,
988
                    switch_b=switch_b,
989
                    endpoint_a_port=9,
990
                    endpoint_b_port=10,
991
                    metadata={"s_vlan": 5},
992
                ),
993
                get_link_mocked(
994
                    switch_a=switch_b,
995
                    switch_b=switch_c,
996
                    endpoint_a_port=11,
997
                    endpoint_b_port=12,
998
                    metadata={"s_vlan": 6},
999
                ),
1000
            ],
1001
        }
1002
1003 1
        evc = EVC(**attributes)
1004
1005 1
        evc.current_path = evc.primary_links
1006 1
        evc.remove_current_flows()
1007
1008 1
        assert send_flow_mods_mocked.call_count == 5
1009 1
        assert evc.is_active() is False
1010 1
        flows = [
1011
            {"cookie": evc.get_cookie(), "cookie_mask": 18446744073709551615}
1012
        ]
1013 1
        switch_1 = evc.primary_links[0].endpoint_a.switch
1014 1
        switch_2 = evc.primary_links[0].endpoint_b.switch
1015 1
        send_flow_mods_mocked.assert_any_call(switch_1.id, flows, 'delete',
1016
                                              force=True)
1017 1
        send_flow_mods_mocked.assert_any_call(switch_2.id, flows, 'delete',
1018
                                              force=True)
1019
1020 1
        send_flow_mods_mocked.side_effect = FlowModException("error")
1021 1
        evc.remove_current_flows()
1022 1
        log_error_mock.assert_called()
1023
1024 1
    @patch("napps.kytos.mef_eline.controllers.ELineController.upsert_evc")
1025 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._send_flow_mods")
1026 1
    @patch("napps.kytos.mef_eline.models.evc.log.error")
1027 1
    def test_remove_failover_flows_exclude_uni_switches(self, *args):
1028
        """Test remove failover flows excluding UNI switches."""
1029
        # pylint: disable=too-many-locals
1030 1
        (log_error_mock, send_flow_mods_mocked, mock_upsert) = args
1031 1
        uni_a = get_uni_mocked(
1032
            interface_port=2,
1033
            tag_value=82,
1034
            switch_id="00:00:00:00:00:00:00:01",
1035
            is_valid=True,
1036
        )
1037 1
        uni_z = get_uni_mocked(
1038
            interface_port=3,
1039
            tag_value=83,
1040
            switch_id="00:00:00:00:00:00:00:03",
1041
            is_valid=True,
1042
        )
1043
1044 1
        switch_a = Switch("00:00:00:00:00:00:00:01")
1045 1
        switch_b = Switch("00:00:00:00:00:00:00:02")
1046 1
        switch_c = Switch("00:00:00:00:00:00:00:03")
1047
1048 1
        attributes = {
1049
            "controller": get_controller_mock(),
1050
            "name": "custom_name",
1051
            "uni_a": uni_a,
1052
            "uni_z": uni_z,
1053
            "active": True,
1054
            "enabled": True,
1055
            "failover_path": [
1056
                get_link_mocked(
1057
                    switch_a=switch_a,
1058
                    switch_b=switch_b,
1059
                    endpoint_a_port=9,
1060
                    endpoint_b_port=10,
1061
                    metadata={"s_vlan": 5},
1062
                ),
1063
                get_link_mocked(
1064
                    switch_a=switch_b,
1065
                    switch_b=switch_c,
1066
                    endpoint_a_port=11,
1067
                    endpoint_b_port=12,
1068
                    metadata={"s_vlan": 6},
1069
                ),
1070
            ],
1071
        }
1072
1073 1
        evc = EVC(**attributes)
1074 1
        evc.remove_failover_flows(exclude_uni_switches=True, sync=True)
1075
1076 1
        assert send_flow_mods_mocked.call_count == 1
1077 1
        flows = [
1078
            {"cookie": evc.get_cookie(),
1079
             "cookie_mask": int(0xffffffffffffffff)}
1080
        ]
1081 1
        send_flow_mods_mocked.assert_any_call(switch_b.id, flows, 'delete',
1082
                                              force=True)
1083 1
        assert mock_upsert.call_count == 1
1084
1085 1
        send_flow_mods_mocked.side_effect = FlowModException("error")
1086 1
        evc.remove_current_flows()
1087 1
        log_error_mock.assert_called()
1088
1089 1
    @patch("napps.kytos.mef_eline.controllers.ELineController.upsert_evc")
1090 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._send_flow_mods")
1091 1
    def test_remove_failover_flows_include_all(self, *args):
1092
        """Test remove failover flows including UNI switches."""
1093
        # pylint: disable=too-many-locals
1094 1
        (send_flow_mods_mocked, mock_upsert) = args
1095 1
        uni_a = get_uni_mocked(
1096
            interface_port=2,
1097
            tag_value=82,
1098
            switch_id="00:00:00:00:00:00:00:01",
1099
            is_valid=True,
1100
        )
1101 1
        uni_z = get_uni_mocked(
1102
            interface_port=3,
1103
            tag_value=83,
1104
            switch_id="00:00:00:00:00:00:00:03",
1105
            is_valid=True,
1106
        )
1107
1108 1
        switch_a = Switch("00:00:00:00:00:00:00:01")
1109 1
        switch_b = Switch("00:00:00:00:00:00:00:02")
1110 1
        switch_c = Switch("00:00:00:00:00:00:00:03")
1111
1112 1
        attributes = {
1113
            "controller": get_controller_mock(),
1114
            "name": "custom_name",
1115
            "uni_a": uni_a,
1116
            "uni_z": uni_z,
1117
            "active": True,
1118
            "enabled": True,
1119
            "failover_path": [
1120
                get_link_mocked(
1121
                    switch_a=switch_a,
1122
                    switch_b=switch_b,
1123
                    endpoint_a_port=9,
1124
                    endpoint_b_port=10,
1125
                    metadata={"s_vlan": 5},
1126
                ),
1127
                get_link_mocked(
1128
                    switch_a=switch_b,
1129
                    switch_b=switch_c,
1130
                    endpoint_a_port=11,
1131
                    endpoint_b_port=12,
1132
                    metadata={"s_vlan": 6},
1133
                ),
1134
            ],
1135
        }
1136
1137 1
        evc = EVC(**attributes)
1138 1
        evc.remove_failover_flows(exclude_uni_switches=False, sync=True)
1139
1140 1
        assert send_flow_mods_mocked.call_count == 3
1141 1
        flows = [
1142
            {"cookie": evc.get_cookie(),
1143
             "cookie_mask": int(0xffffffffffffffff)}
1144
        ]
1145 1
        send_flow_mods_mocked.assert_any_call(switch_a.id, flows, 'delete',
1146
                                              force=True)
1147 1
        send_flow_mods_mocked.assert_any_call(switch_b.id, flows, 'delete',
1148
                                              force=True)
1149 1
        send_flow_mods_mocked.assert_any_call(switch_c.id, flows, 'delete',
1150
                                              force=True)
1151 1
        assert mock_upsert.call_count == 1
1152
1153 1
    @staticmethod
1154 1
    def create_evc_intra_switch():
1155
        """Create intra-switch EVC."""
1156 1
        switch = Mock(spec=Switch)
1157 1
        switch.dpid = 2
1158 1
        switch.id = switch.dpid
1159 1
        interface_a = Interface("eth0", 1, switch)
1160 1
        interface_z = Interface("eth1", 3, switch)
1161 1
        uni_a = get_uni_mocked(
1162
            tag_value=82,
1163
            is_valid=True,
1164
        )
1165 1
        uni_z = get_uni_mocked(
1166
            tag_value=84,
1167
            is_valid=True,
1168
        )
1169 1
        uni_a.interface = interface_a
1170 1
        uni_z.interface = interface_z
1171 1
        attributes = {
1172
            "table_group": {"epl": 0, "evpl": 0},
1173
            "controller": get_controller_mock(),
1174
            "name": "custom_name",
1175
            "id": "1",
1176
            "uni_a": uni_a,
1177
            "uni_z": uni_z,
1178
            "enabled": True,
1179
        }
1180 1
        return EVC(**attributes)
1181
1182
    # pylint: disable=too-many-branches
1183 1
    @pytest.mark.parametrize(
1184
        "uni_a,uni_z",
1185
        [
1186
            (100, 50),
1187
            (100, "4096/4096"),
1188
            (100, 0),
1189
            (100, None),
1190
            ("4096/4096", 50),
1191
            ("4096/4096", "4096/4096"),
1192
            ("4096/4096", 0),
1193
            ("4096/4096", None),
1194
            (0, 50),
1195
            (0, "4096/4096"),
1196
            (0, 0),
1197
            (0, None),
1198
            (None, 50),
1199
            (None, "4096/4096"),
1200
            (None, 0),
1201
            (None, None),
1202
        ]
1203
    )
1204 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._send_flow_mods")
1205 1
    def test_deploy_direct_uni_flows(self, send_flow_mods_mock, uni_a, uni_z):
1206
        """Test _install_direct_uni_flows"""
1207 1
        evc = TestEVC.create_evc_intra_switch()
1208 1
        expected_dpid = evc.uni_a.interface.switch.id
1209
1210 1
        expected_flows = [
1211
            {
1212
                "match": {"in_port": 1},
1213
                "cookie": evc.get_cookie(),
1214
                "owner": "mef_eline",
1215
                "table_id": 0,
1216
                "table_group": "epl",
1217
                "actions": [
1218
                    {"action_type": "output", "port": 3},
1219
                ],
1220
                "priority": EPL_SB_PRIORITY
1221
            },
1222
            {
1223
                "match": {"in_port": 3},
1224
                "cookie": evc.get_cookie(),
1225
                "owner": "mef_eline",
1226
                "table_id": 0,
1227
                "table_group": "epl",
1228
                "actions": [
1229
                    {"action_type": "output", "port": 1},
1230
                ],
1231
                "priority": EPL_SB_PRIORITY
1232
            }
1233
        ]
1234 1
        evc.uni_a = get_uni_mocked(tag_value=uni_a, is_valid=True)
1235 1
        evc.uni_a.interface.port_number = 1
1236 1
        evc.uni_z = get_uni_mocked(tag_value=uni_z, is_valid=True)
1237 1
        evc.uni_z.interface.port_number = 3
1238 1
        expected_dpid = evc.uni_a.interface.switch.id
1239 1
        evc._install_direct_uni_flows()
1240 1
        if uni_a is not None:
1241 1
            expected_flows[0]["match"]["dl_vlan"] = uni_a
1242 1
            expected_flows[0]["table_group"] = "evpl"
1243 1
        if uni_z is not None:
1244 1
            expected_flows[1]["match"]["dl_vlan"] = uni_z
1245 1
            expected_flows[1]["table_group"] = "evpl"
1246 1
        expected_flows[0]["priority"] = EVC.get_priority(uni_a)
1247 1
        expected_flows[1]["priority"] = EVC.get_priority(uni_z)
1248 1
        if uni_z not in evc.special_cases:
1249 1
            expected_flows[0]["actions"].insert(
1250
                0, {"action_type": "set_vlan", "vlan_id": uni_z}
1251
            )
1252 1
        if uni_a not in evc.special_cases:
1253 1
            expected_flows[1]["actions"].insert(
1254
                    0, {"action_type": "set_vlan",
1255
                        "vlan_id": uni_a}
1256
                )
1257 1
            if not uni_z:
1258 1
                expected_flows[1]["actions"].insert(
1259
                    0, {"action_type": "push_vlan",
1260
                        "tag_type": "c"}
1261
                )
1262 1
            if uni_z == 0:
1263 1
                new_action = {"action_type": "pop_vlan"}
1264 1
                expected_flows[0]["actions"].insert(0, new_action)
1265 1
        elif uni_a == "4096/4096":
1266 1
            if uni_z == 0:
1267 1
                new_action = {"action_type": "pop_vlan"}
1268 1
                expected_flows[0]["actions"].insert(0, new_action)
1269 1
        elif uni_a == 0:
1270 1
            if uni_z not in evc.special_cases:
1271 1
                expected_flows[0]["actions"].insert(
1272
                    0, {"action_type": "push_vlan",
1273
                        "tag_type": "c"}
1274
                )
1275 1
            if uni_z:
1276 1
                new_action = {"action_type": "pop_vlan"}
1277 1
                expected_flows[1]["actions"].insert(0, new_action)
1278 1
        elif uni_a is None:
1279 1
            if uni_z not in evc.special_cases:
1280 1
                expected_flows[0]["actions"].insert(
1281
                    0, {"action_type": "push_vlan",
1282
                        "tag_type": "c"}
1283
                )
1284 1
        send_flow_mods_mock.assert_called_with(
1285
            expected_dpid, expected_flows
1286
        )
1287
1288 1
    def test_is_affected_by_link(self):
1289
        """Test is_affected_by_link method"""
1290 1
        self.evc_deploy.current_path = Path(['a', 'b', 'c'])
1291 1
        assert self.evc_deploy.is_affected_by_link('b') is True
1292
1293 1
    def test_is_backup_path_affected_by_link(self):
1294
        """Test is_backup_path_affected_by_link method"""
1295 1
        self.evc_deploy.backup_path = Path(['a', 'b', 'c'])
1296 1
        assert self.evc_deploy.is_backup_path_affected_by_link('d') is False
1297
1298 1
    def test_is_primary_path_affected_by_link(self):
1299
        """Test is_primary_path_affected_by_link method"""
1300 1
        self.evc_deploy.primary_path = Path(['a', 'b', 'c'])
1301 1
        assert self.evc_deploy.is_primary_path_affected_by_link('c') is True
1302
1303 1
    def test_is_using_primary_path(self):
1304
        """Test is_using_primary_path method"""
1305 1
        self.evc_deploy.primary_path = Path(['a', 'b', 'c'])
1306 1
        self.evc_deploy.current_path = Path(['e', 'f', 'g'])
1307 1
        assert self.evc_deploy.is_using_primary_path() is False
1308
1309 1
    def test_is_using_backup_path(self):
1310
        """Test is_using_backup_path method"""
1311 1
        self.evc_deploy.backup_path = Path(['a', 'b', 'c'])
1312 1
        self.evc_deploy.current_path = Path(['e', 'f', 'g'])
1313 1
        assert self.evc_deploy.is_using_backup_path() is False
1314
1315 1
    @patch('napps.kytos.mef_eline.models.path.Path.status')
1316 1
    def test_is_using_dynamic_path(self, mock_status):
1317
        """Test is_using_dynamic_path method"""
1318 1
        mock_status.return_value = False
1319 1
        self.evc_deploy.backup_path = Path([])
1320 1
        self.evc_deploy.primary_path = Path([])
1321 1
        assert self.evc_deploy.is_using_dynamic_path() is False
1322
1323 1
    def test_get_path_status(self):
1324
        """Test get_path_status method"""
1325 1
        path = Path([])
1326 1
        assert self.evc_deploy.get_path_status(path) == EntityStatus.DISABLED
1327 1
        path = Path([
1328
            get_link_mocked(status=EntityStatus.UP),
1329
            get_link_mocked(status=EntityStatus.DOWN)
1330
        ])
1331 1
        assert self.evc_deploy.get_path_status(path) == EntityStatus.DOWN
1332 1
        path = Path([
1333
            get_link_mocked(status=EntityStatus.UP),
1334
            get_link_mocked(status=EntityStatus.UP)
1335
        ])
1336 1
        assert self.evc_deploy.get_path_status(path) == EntityStatus.UP
1337
1338 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._prepare_uni_flows")
1339 1
    def test_get_failover_flows(self, prepare_uni_flows_mock):
1340
        """Test get_failover_flows method."""
1341 1
        evc = self.create_evc_inter_switch()
1342 1
        evc.failover_path = Path([])
1343 1
        assert len(evc.get_failover_flows()) == 0
1344
1345 1
        path = MagicMock()
1346 1
        evc.failover_path = path
1347 1
        evc.get_failover_flows()
1348 1
        prepare_uni_flows_mock.assert_called_with(path, skip_out=True)
1349
1350 1
    @patch("napps.kytos.mef_eline.models.evc.log")
1351 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._send_flow_mods")
1352 1
    @patch("napps.kytos.mef_eline.models.path.Path.make_vlans_available")
1353 1
    def test_remove_path_flows(self, *args):
1354
        """Test remove path flows."""
1355 1
        (
1356
            make_vlans_available_mock,
1357
            send_flow_mods_mock,
1358
            log_mock
1359
        ) = args
1360
1361 1
        evc = self.create_evc_inter_switch()
1362
1363 1
        evc.remove_path_flows()
1364 1
        make_vlans_available_mock.assert_not_called()
1365
1366 1
        expected_flows_1 = [
1367
            {
1368
                'cookie': 12249790986447749121,
1369
                'cookie_mask': 18446744073709551615,
1370
                'match': {'in_port': 9, 'dl_vlan':  5}
1371
            },
1372
        ]
1373 1
        expected_flows_2 = [
1374
            {
1375
                'cookie': 12249790986447749121,
1376
                'cookie_mask': 18446744073709551615,
1377
                'match': {'in_port': 10, 'dl_vlan': 5}
1378
            },
1379
            {
1380
                'cookie': 12249790986447749121,
1381
                'cookie_mask': 18446744073709551615,
1382
                'match': {'in_port': 11, 'dl_vlan': 6}
1383
            },
1384
        ]
1385 1
        expected_flows_3 = [
1386
            {
1387
                'cookie': 12249790986447749121,
1388
                'cookie_mask': 18446744073709551615,
1389
                'match': {'in_port': 12, 'dl_vlan': 6}
1390
            },
1391
        ]
1392
1393 1
        dpid_flows = evc.remove_path_flows(evc.primary_links)
1394 1
        assert dpid_flows
1395 1
        assert len(dpid_flows) == 3
1396 1
        assert sum(len(flows) for flows in dpid_flows.values()) == len(
1397
            expected_flows_1
1398
        ) + len(expected_flows_2) + len(expected_flows_3)
1399 1
        send_flow_mods_mock.assert_has_calls([
1400
            call(1, expected_flows_1, 'delete', force=True),
1401
            call(2, expected_flows_2, 'delete', force=True),
1402
            call(3, expected_flows_3, 'delete', force=True),
1403
        ], any_order=True)
1404
1405 1
        send_flow_mods_mock.side_effect = FlowModException("err")
1406 1
        evc.remove_path_flows(evc.primary_links)
1407 1
        log_mock.error.assert_called()
1408
1409 1
    @patch("httpx.put")
1410 1
    def test_run_bulk_sdntraces(self, put_mock):
1411
        """Test run_bulk_sdntraces method for bulk request."""
1412 1
        evc = self.create_evc_inter_switch()
1413 1
        response = MagicMock()
1414 1
        response.status_code = 200
1415 1
        response.json.return_value = {"result": "ok"}
1416 1
        put_mock.return_value = response
1417
1418 1
        expected_endpoint = f"{SDN_TRACE_CP_URL}/traces"
1419 1
        expected_payload = [
1420
                            {
1421
                                'trace': {
1422
                                    'switch': {'dpid': 1, 'in_port': 2},
1423
                                    'eth': {'dl_type': 0x8100, 'dl_vlan': 82}
1424
                                }
1425
                            }
1426
                        ]
1427 1
        arg_tuple = [(evc.uni_a.interface, evc.uni_a.user_tag.value)]
1428 1
        result = EVCDeploy.run_bulk_sdntraces(arg_tuple)
1429 1
        put_mock.assert_called_with(
1430
                                    expected_endpoint,
1431
                                    json=expected_payload,
1432
                                    timeout=30
1433
                                )
1434 1
        assert result['result'] == "ok"
1435
1436 1
        response.status_code = 400
1437 1
        result = EVCDeploy.run_bulk_sdntraces(arg_tuple)
1438 1
        assert result == {"result": []}
1439
1440 1
        put_mock.side_effect = TimeoutException('Timeout')
1441 1
        response.status_code = 200
1442 1
        result = EVCDeploy.run_bulk_sdntraces(arg_tuple)
1443 1
        assert result == {"result": []}
1444
1445 1
    @patch("httpx.put")
1446 1
    def test_run_bulk_sdntraces_special_vlan(self, put_mock):
1447
        """Test run_bulk_sdntraces method for bulk request."""
1448 1
        evc = self.create_evc_inter_switch()
1449 1
        response = MagicMock()
1450 1
        response.status_code = 200
1451 1
        put_mock.return_value = response
1452
1453 1
        expected_endpoint = f"{SDN_TRACE_CP_URL}/traces"
1454 1
        expected_payload = [
1455
                            {
1456
                                'trace': {
1457
                                    'switch': {'dpid': 1, 'in_port': 2}
1458
                                }
1459
                            }
1460
                        ]
1461 1
        evc.uni_a.user_tag.value = 'untagged'
1462 1
        EVCDeploy.run_bulk_sdntraces(
1463
            [(evc.uni_a.interface, evc.uni_a.user_tag.value)]
1464
        )
1465 1
        put_mock.assert_called_with(
1466
                                    expected_endpoint,
1467
                                    json=expected_payload,
1468
                                    timeout=30
1469
                                )
1470 1
        args = put_mock.call_args[1]['json'][0]
1471 1
        assert 'eth' not in args
1472
1473 1
        evc.uni_a.user_tag.value = 0
1474 1
        EVCDeploy.run_bulk_sdntraces(
1475
            [(evc.uni_a.interface, evc.uni_a.user_tag.value)]
1476
        )
1477 1
        put_mock.assert_called_with(
1478
                                    expected_endpoint,
1479
                                    json=expected_payload,
1480
                                    timeout=30
1481
                                )
1482 1
        args = put_mock.call_args[1]['json'][0]['trace']
1483 1
        assert 'eth' not in args
1484
1485 1
        evc.uni_a.user_tag.value = '5/2'
1486 1
        EVCDeploy.run_bulk_sdntraces(
1487
            [(evc.uni_a.interface, evc.uni_a.user_tag.value)]
1488
        )
1489 1
        put_mock.assert_called_with(
1490
                                    expected_endpoint,
1491
                                    json=expected_payload,
1492
                                    timeout=30
1493
                                )
1494 1
        args = put_mock.call_args[1]['json'][0]['trace']
1495 1
        assert 'eth' not in args
1496
1497 1
        expected_payload[0]['trace']['eth'] = {'dl_type': 0x8100, 'dl_vlan': 1}
1498 1
        evc.uni_a.user_tag.value = 'any'
1499 1
        EVCDeploy.run_bulk_sdntraces(
1500
            [(evc.uni_a.interface, evc.uni_a.user_tag.value)]
1501
        )
1502 1
        put_mock.assert_called_with(
1503
                                    expected_endpoint,
1504
                                    json=expected_payload,
1505
                                    timeout=30
1506
                                )
1507 1
        args = put_mock.call_args[1]['json'][0]['trace']
1508 1
        assert args['eth'] == {'dl_type': 33024, 'dl_vlan': 1}
1509
1510 1
        evc.uni_a.user_tag.value = '4096/4096'
1511 1
        EVCDeploy.run_bulk_sdntraces(
1512
            [(evc.uni_a.interface, evc.uni_a.user_tag.value)]
1513
        )
1514 1
        put_mock.assert_called_with(
1515
                                    expected_endpoint,
1516
                                    json=expected_payload,
1517
                                    timeout=30
1518
                                )
1519 1
        args = put_mock.call_args[1]['json'][0]['trace']
1520 1
        assert args['eth'] == {'dl_type': 33024, 'dl_vlan': 1}
1521
1522 1
        expected_payload[0]['trace']['eth'] = {
1523
            'dl_type': 0x8100,
1524
            'dl_vlan': 10
1525
            }
1526 1
        evc.uni_a.user_tag.value = '10/10'
1527 1
        EVCDeploy.run_bulk_sdntraces(
1528
            [(evc.uni_a.interface, evc.uni_a.user_tag.value)]
1529
        )
1530 1
        put_mock.assert_called_with(
1531
                                    expected_endpoint,
1532
                                    json=expected_payload,
1533
                                    timeout=30
1534
                                )
1535 1
        args = put_mock.call_args[1]['json'][0]['trace']
1536 1
        assert args['eth'] == {'dl_type': 33024, 'dl_vlan': 10}
1537
1538 1
        expected_payload[0]['trace']['eth'] = {
1539
            'dl_type': 0x8100,
1540
            'dl_vlan': 1
1541
            }
1542 1
        evc.uni_a.user_tag.value = '5/3'
1543 1
        EVCDeploy.run_bulk_sdntraces(
1544
            [(evc.uni_a.interface, evc.uni_a.user_tag.value)]
1545
        )
1546 1
        put_mock.assert_called_with(
1547
                                    expected_endpoint,
1548
                                    json=expected_payload,
1549
                                    timeout=30
1550
                                )
1551 1
        args = put_mock.call_args[1]['json'][0]['trace']
1552 1
        assert args['eth'] == {'dl_type': 33024, 'dl_vlan': 1}
1553
1554 1
        expected_payload[0]['trace']['eth'] = {
1555
            'dl_type': 0x8100,
1556
            'dl_vlan': 10
1557
            }
1558 1
        evc.uni_a.user_tag.value = 10
1559 1
        EVCDeploy.run_bulk_sdntraces(
1560
            [(evc.uni_a.interface, evc.uni_a.user_tag.value)]
1561
        )
1562 1
        put_mock.assert_called_with(
1563
                                    expected_endpoint,
1564
                                    json=expected_payload,
1565
                                    timeout=30
1566
                                )
1567
1568 1
    @patch("napps.kytos.mef_eline.models.evc.log")
1569 1
    @patch("napps.kytos.mef_eline.models.evc.EVCDeploy.run_bulk_sdntraces")
1570 1
    def test_check_list_traces_ordered_unordered(self, run_bulk_mock, _):
1571
        """Test check_list_traces with UNIs ordered and unordered."""
1572 1
        evc = self.create_evc_inter_switch()
1573
1574 1
        for link in evc.primary_links:
1575 1
            link.metadata['s_vlan'] = MagicMock(value=link.metadata['s_vlan'])
1576 1
        evc.current_path = evc.primary_links
1577
1578 1
        trace_a = [
1579
            {
1580
                "dpid": 1,
1581
                "port": 2,
1582
                "time": "t1",
1583
                "type": "starting",
1584
                "vlan": 82
1585
            },
1586
            {
1587
                "dpid": 2,
1588
                "port": 10,
1589
                "time": "t2",
1590
                "type": "intermediary",
1591
                "vlan": 5
1592
            },
1593
            {"dpid": 3, "port": 12, "time": "t3", "type": "last", "vlan": 6},
1594
        ]
1595 1
        trace_z = [
1596
            {
1597
                "dpid": 3,
1598
                "port": 3,
1599
                "time": "t1",
1600
                "type": "starting",
1601
                "vlan": 83
1602
            },
1603
            {
1604
                "dpid": 2,
1605
                "port": 11,
1606
                "time": "t2",
1607
                "type": "intermediary",
1608
                "vlan": 6
1609
            },
1610
            {"dpid": 1, "port": 9, "time": "t3", "type": "last", "vlan": 5},
1611
        ]
1612
1613 1
        run_bulk_mock.return_value = {"result": [trace_a, trace_z]}
1614 1
        result = EVCDeploy.check_list_traces([evc])
1615 1
        assert result[evc.id]
1616
1617
        # swapped UNIs since uni_a and uni_z might not be ordered with cur path
1618 1
        run_bulk_mock.return_value = {"result": [trace_z, trace_a]}
1619 1
        evc.uni_a, evc.uni_z = evc.uni_z, evc.uni_a
1620 1
        result = EVCDeploy.check_list_traces([evc])
1621 1
        assert result[evc.id]
1622
1623 1
    @patch("napps.kytos.mef_eline.models.evc.log")
1624 1
    @patch("napps.kytos.mef_eline.models.evc.EVCDeploy.run_bulk_sdntraces")
1625 1
    def test_check_list_traces(self, run_bulk_sdntraces_mock, _):
1626
        """Test check_list_traces method."""
1627 1
        evc = self.create_evc_inter_switch()
1628
1629 1
        for link in evc.primary_links:
1630 1
            link.metadata['s_vlan'] = MagicMock(value=link.metadata['s_vlan'])
1631 1
        evc.current_path = evc.primary_links
1632
1633 1
        trace_a = [
1634
            {
1635
                "dpid": 1,
1636
                "port": 2,
1637
                "time": "t1",
1638
                "type": "starting",
1639
                "vlan": 82
1640
            },
1641
            {
1642
                "dpid": 2,
1643
                "port": 10,
1644
                "time": "t2",
1645
                "type": "intermediary",
1646
                "vlan": 5
1647
            },
1648
            {"dpid": 3, "port": 12, "time": "t3", "type": "last", "vlan": 6},
1649
        ]
1650 1
        trace_z = [
1651
            {
1652
                "dpid": 3,
1653
                "port": 3,
1654
                "time": "t1",
1655
                "type": "starting",
1656
                "vlan": 83
1657
            },
1658
            {
1659
                "dpid": 2,
1660
                "port": 11,
1661
                "time": "t2",
1662
                "type": "intermediary",
1663
                "vlan": 6
1664
            },
1665
            {"dpid": 1, "port": 9, "time": "t3", "type": "last", "vlan": 5},
1666
        ]
1667
1668 1
        run_bulk_sdntraces_mock.return_value = {
1669
                                                "result": [trace_a, trace_z]
1670
                                            }
1671 1
        result = EVCDeploy.check_list_traces([evc])
1672 1
        assert result[evc.id] is True
1673
1674
        # case2: fail incomplete trace from uni_a
1675 1
        run_bulk_sdntraces_mock.return_value = {
1676
                                                "result": [
1677
                                                            trace_a[:2],
1678
                                                            trace_z
1679
                                                        ]
1680
        }
1681 1
        result = EVCDeploy.check_list_traces([evc])
1682 1
        assert result[evc.id] is False
1683
1684
        # case3: fail incomplete trace from uni_z
1685 1
        run_bulk_sdntraces_mock.return_value = {
1686
                                                "result": [
1687
                                                            trace_a,
1688
                                                            trace_z[:2]
1689
                                                        ]
1690
        }
1691 1
        result = EVCDeploy.check_list_traces([evc])
1692 1
        assert result[evc.id] is False
1693
1694
        # case4: fail wrong vlan id in trace from uni_a
1695 1
        trace_a[1]["vlan"] = 5
1696 1
        trace_z[1]["vlan"] = 99
1697 1
        run_bulk_sdntraces_mock.return_value = {
1698
                                                "result": [trace_a, trace_z]
1699
        }
1700 1
        result = EVCDeploy.check_list_traces([evc])
1701 1
        assert result[evc.id] is False
1702
1703
        # case5: fail wrong vlan id in trace from uni_z
1704 1
        trace_a[1]["vlan"] = 99
1705 1
        run_bulk_sdntraces_mock.return_value = {
1706
                                                "result": [trace_a, trace_z]
1707
        }
1708 1
        result = EVCDeploy.check_list_traces([evc])
1709 1
        assert result[evc.id] is False
1710
1711
        # case6: success when no output in traces
1712 1
        trace_a[1]["vlan"] = 5
1713 1
        trace_z[1]["vlan"] = 6
1714 1
        result = EVCDeploy.check_list_traces([evc])
1715 1
        assert result[evc.id] is True
1716
1717
        # case7: fail when output is None in trace_a or trace_b
1718 1
        trace_a[-1]["out"] = None
1719 1
        result = EVCDeploy.check_list_traces([evc])
1720 1
        assert result[evc.id] is False
1721 1
        trace_a[-1].pop("out", None)
1722 1
        trace_z[-1]["out"] = None
1723 1
        result = EVCDeploy.check_list_traces([evc])
1724 1
        assert result[evc.id] is False
1725
1726
        # case8: success when the output is correct on both uni
1727 1
        trace_a[-1]["out"] = {"port": 3, "vlan": 83}
1728 1
        trace_z[-1]["out"] = {"port": 2, "vlan": 82}
1729 1
        result = EVCDeploy.check_list_traces([evc])
1730 1
        assert result[evc.id] is True
1731
1732
        # case9: fail if any output is incorrect
1733 1
        trace_a[-1]["out"] = {"port": 3, "vlan": 99}
1734 1
        trace_z[-1]["out"] = {"port": 2, "vlan": 82}
1735 1
        result = EVCDeploy.check_list_traces([evc])
1736 1
        assert result[evc.id] is False
1737 1
        trace_a[-1]["out"] = {"port": 3, "vlan": 83}
1738 1
        trace_z[-1]["out"] = {"port": 2, "vlan": 99}
1739 1
        result = EVCDeploy.check_list_traces([evc])
1740 1
        assert result[evc.id] is False
1741
1742 1 View Code Duplication
    @patch("napps.kytos.mef_eline.models.evc.log")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1743 1
    @patch("napps.kytos.mef_eline.models.evc.EVCDeploy.run_bulk_sdntraces")
1744 1
    def test_check_list_traces_any_cases(self, run_bulk_sdntraces_mock, _):
1745
        """Test check_list_traces method."""
1746 1
        evc = self.create_evc_inter_switch("any", "any")
1747
1748 1
        for link in evc.primary_links:
1749 1
            link.metadata['s_vlan'] = MagicMock(value=link.metadata['s_vlan'])
1750 1
        evc.current_path = evc.primary_links
1751
1752 1
        trace_a = [
1753
            {
1754
                "dpid": 1,
1755
                "port": 2,
1756
                "time": "t1",
1757
                "type": "starting",
1758
                "vlan": 1
1759
            },
1760
            {
1761
                "dpid": 2,
1762
                "port": 10,
1763
                "time": "t2",
1764
                "type": "intermediary",
1765
                "vlan": 5
1766
            },
1767
            {
1768
                "dpid": 3,
1769
                "port": 12,
1770
                'out': {'port': 3, 'vlan': 1},
1771
                "time": "t3",
1772
                "type": "last",
1773
                "vlan": 6
1774
            },
1775
        ]
1776 1
        trace_z = [
1777
            {
1778
                "dpid": 3,
1779
                "port": 3,
1780
                "time": "t1",
1781
                "type": "starting",
1782
                "vlan": 1
1783
            },
1784
            {
1785
                "dpid": 2,
1786
                "port": 11,
1787
                "time": "t2",
1788
                "type": "intermediary",
1789
                "vlan": 6
1790
            },
1791
            {
1792
                "dpid": 1,
1793
                "port": 9,
1794
                'out': {'port': 2, 'vlan': 1},
1795
                "time": "t3",
1796
                "type": "last",
1797
                "vlan": 5
1798
            },
1799
        ]
1800
1801 1
        run_bulk_sdntraces_mock.return_value = {
1802
                                                "result": [trace_a, trace_z]
1803
                                            }
1804 1
        result = EVCDeploy.check_list_traces([evc])
1805 1
        assert result[evc.id] is True
1806
1807 1 View Code Duplication
    @patch("napps.kytos.mef_eline.models.evc.log")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1808 1
    @patch("napps.kytos.mef_eline.models.evc.EVCDeploy.run_bulk_sdntraces")
1809 1
    def test_check_list_traces_untagged_cases(self, bulk_sdntraces_mock, _):
1810
        """Test check_list_traces method."""
1811 1
        evc = self.create_evc_inter_switch("untagged", "untagged")
1812
1813 1
        for link in evc.primary_links:
1814 1
            link.metadata['s_vlan'] = MagicMock(value=link.metadata['s_vlan'])
1815 1
        evc.current_path = evc.primary_links
1816
1817 1
        trace_a = [
1818
            {
1819
                "dpid": 1,
1820
                "port": 2,
1821
                "time": "t1",
1822
                "type": "starting",
1823
                "vlan": 0
1824
            },
1825
            {
1826
                "dpid": 2,
1827
                "port": 10,
1828
                "time": "t2",
1829
                "type": "intermediary",
1830
                "vlan": 5
1831
            },
1832
            {
1833
                "dpid": 3,
1834
                "port": 12,
1835
                'out': {'port': 3},
1836
                "time": "t3", "type":
1837
                "last",
1838
                "vlan": 6
1839
                },
1840
        ]
1841 1
        trace_z = [
1842
            {
1843
                "dpid": 3,
1844
                "port": 3,
1845
                "time": "t1",
1846
                "type": "starting",
1847
                "vlan": 0
1848
            },
1849
            {
1850
                "dpid": 2,
1851
                "port": 11,
1852
                "time": "t2",
1853
                "type": "intermediary",
1854
                "vlan": 6
1855
            },
1856
            {
1857
                "dpid": 1,
1858
                "port": 9,
1859
                'out': {'port': 2},
1860
                "time": "t3",
1861
                "type": "last",
1862
                "vlan": 5
1863
            },
1864
        ]
1865
1866 1
        bulk_sdntraces_mock.return_value = {
1867
                                                "result": [trace_a, trace_z]
1868
                                            }
1869 1
        result = EVCDeploy.check_list_traces([evc])
1870 1
        assert result[evc.id] is True
1871
1872 1
    @patch("napps.kytos.mef_eline.models.evc.log")
1873 1
    @patch("napps.kytos.mef_eline.models.evc.EVCDeploy.run_bulk_sdntraces")
1874 1
    def test_check_list_traces_invalid_types(self, run_bulk_sdntraces_mock, _):
1875
        """Test check_list_traces method for invalid traces by trace type."""
1876 1
        evc = self.create_evc_inter_switch()
1877
1878 1
        for link in evc.primary_links:
1879 1
            link.metadata['s_vlan'] = MagicMock(value=link.metadata['s_vlan'])
1880 1
        evc.current_path = evc.primary_links
1881
1882 1
        trace_a = [
1883
            {
1884
                "dpid": 1,
1885
                "port": 2,
1886
                "time": "t1",
1887
                "type": "starting",
1888
                "vlan": 82
1889
            },
1890
            {
1891
                "dpid": 2,
1892
                "port": 10,
1893
                "time": "t2",
1894
                "type": "intermediary",
1895
                "vlan": 5
1896
            },
1897
            {"dpid": 3, "port": 12, "time": "t3", "type": "last", "vlan": 6},
1898
        ]
1899 1
        trace_z = [
1900
            {
1901
                "dpid": 3,
1902
                "port": 3,
1903
                "time": "t1",
1904
                "type": "starting",
1905
                "vlan": 83
1906
            },
1907
            {
1908
                "dpid": 2,
1909
                "port": 11,
1910
                "time": "t2",
1911
                "type": "intermediary",
1912
                "vlan": 6
1913
            },
1914
            {
1915
                "dpid": 1,
1916
                "port": 9,
1917
                "time": "t3",
1918
                "type": "last",
1919
                "vlan": 5
1920
            },
1921
        ]
1922
1923 1
        run_bulk_sdntraces_mock.return_value = {
1924
                                                "result": [trace_a, trace_z]
1925
                                            }
1926 1
        result = EVCDeploy.check_list_traces([evc])
1927
1928 1
        assert result[evc.id] is True
1929
1930 1
        trace_z = [
1931
            {
1932
                "dpid": 3,
1933
                "port": 3,
1934
                "time": "t1",
1935
                "type": "starting",
1936
                "vlan": 83
1937
            },
1938
            {
1939
                "dpid": 2,
1940
                "port": 11,
1941
                "time": "t2",
1942
                "type": "loop",
1943
                "vlan": 6
1944
            },
1945
        ]
1946
1947 1
        run_bulk_sdntraces_mock.return_value = {
1948
                                                "result": [trace_a, trace_z]
1949
                                            }
1950 1
        result = EVCDeploy.check_list_traces([evc])
1951
        # type loop
1952 1
        assert result[evc.id] is False
1953
1954 1
    @patch("napps.kytos.mef_eline.models.evc.EVCDeploy.check_trace")
1955 1
    @patch("napps.kytos.mef_eline.models.evc.EVCDeploy.check_range")
1956 1
    @patch("napps.kytos.mef_eline.models.evc.EVCDeploy.run_bulk_sdntraces")
1957 1
    def test_check_list_traces_vlan_list(self, *args):
1958
        """Test check_list_traces with vlan list"""
1959 1
        mock_bulk, mock_range, mock_trace = args
1960 1
        mask_list = [1, '2/4094', '4/4094']
1961 1
        evc = self.create_evc_inter_switch([[1, 5]], [[1, 5]])
1962 1
        evc.uni_a.user_tag.mask_list = mask_list
1963 1
        evc.uni_z.user_tag.mask_list = mask_list
1964 1
        mock_bulk.return_value = {"result": ["mock"] * 6}
1965 1
        mock_range.return_value = True
1966 1
        actual_return = EVC.check_list_traces([evc])
1967 1
        assert actual_return == {evc._id: True}
1968 1
        assert mock_trace.call_count == 0
1969 1
        assert mock_range.call_count == 1
1970 1
        args = mock_range.call_args[0]
1971 1
        assert args[0] == evc
1972 1
        assert args[1] == ["mock"] * 6
1973
1974 1
    @patch("napps.kytos.mef_eline.models.evc.EVCDeploy.check_trace")
1975 1
    @patch("napps.kytos.mef_eline.models.evc.log")
1976 1
    @patch("napps.kytos.mef_eline.models.evc.EVCDeploy.run_bulk_sdntraces")
1977 1
    def test_check_list_traces_empty(self, mock_bulk, mock_log, mock_trace):
1978
        """Test check_list_traces with empty return"""
1979 1
        evc = self.create_evc_inter_switch(1, 1)
1980 1
        actual_return = EVC.check_list_traces([])
1981 1
        assert not actual_return
1982
1983 1
        mock_bulk.return_value = {"result": []}
1984 1
        actual_return = EVC.check_list_traces([evc])
1985 1
        assert not actual_return
1986
1987 1
        mock_bulk.return_value = {"result": ["mock"]}
1988 1
        mock_trace.return_value = True
1989 1
        actual_return = EVC.check_list_traces([evc])
1990 1
        assert mock_log.error.call_count == 1
1991 1
        assert not actual_return
1992
1993 1
    @patch(
1994
        "napps.kytos.mef_eline.models.path.DynamicPathManager"
1995
        ".get_disjoint_paths"
1996
    )
1997 1
    def test_get_failover_path_vandidates(self, get_disjoint_paths_mock):
1998
        """Test get_failover_path_candidates method"""
1999 1
        self.evc_deploy.get_failover_path_candidates()
2000 1
        get_disjoint_paths_mock.assert_called_once()
2001
2002 1
    def test_is_failover_path_affected_by_link(self):
2003
        """Test is_failover_path_affected_by_link method"""
2004 1
        link1 = get_link_mocked(endpoint_a_port=1, endpoint_b_port=2)
2005 1
        link2 = get_link_mocked(endpoint_a_port=3, endpoint_b_port=4)
2006 1
        link3 = get_link_mocked(endpoint_a_port=5, endpoint_b_port=6)
2007 1
        self.evc_deploy.failover_path = Path([link1, link2])
2008 1
        assert self.evc_deploy.is_failover_path_affected_by_link(link1) is True
2009 1
        assert self.evc_deploy.is_failover_path_affected_by_link(link3) \
2010
               is False
2011
2012 1
    def test_is_eligible_for_failover_path(self):
2013
        """Test is_eligible_for_failover_path method"""
2014 1
        assert self.evc_deploy.is_eligible_for_failover_path() is False
2015 1
        self.evc_deploy.dynamic_backup_path = True
2016 1
        self.evc_deploy.primary_path = Path([])
2017 1
        self.evc_deploy.backup_path = Path([])
2018 1
        assert self.evc_deploy.is_eligible_for_failover_path() is True
2019
2020 1
    def test_get_value_from_uni_tag(self):
2021
        """Test _get_value_from_uni_tag"""
2022 1
        uni = get_uni_mocked(tag_value="any")
2023 1
        value = EVC._get_value_from_uni_tag(uni)
2024 1
        assert value == "4096/4096"
2025
2026 1
        uni.user_tag.value = "untagged"
2027 1
        value = EVC._get_value_from_uni_tag(uni)
2028 1
        assert value == 0
2029
2030 1
        uni.user_tag.value = 100
2031 1
        value = EVC._get_value_from_uni_tag(uni)
2032 1
        assert value == 100
2033
2034 1
        uni.user_tag = None
2035 1
        value = EVC._get_value_from_uni_tag(uni)
2036 1
        assert value is None
2037
2038 1
        uni = get_uni_mocked(tag_value=[[12, 20]])
2039 1
        uni.user_tag.mask_list = ['12/4092', '16/4092', '20/4094']
2040
2041 1
        value = EVC._get_value_from_uni_tag(uni)
2042 1
        assert value == ['12/4092', '16/4092', '20/4094']
2043
2044 1
    def test_get_priority(self):
2045
        """Test get_priority_from_vlan"""
2046 1
        evpl_value = EVC.get_priority(100)
2047 1
        assert evpl_value == EVPL_SB_PRIORITY
2048
2049 1
        untagged_value = EVC.get_priority(0)
2050 1
        assert untagged_value == UNTAGGED_SB_PRIORITY
2051
2052 1
        any_value = EVC.get_priority("4096/4096")
2053 1
        assert any_value == ANY_SB_PRIORITY
2054
2055 1
        epl_value = EVC.get_priority(None)
2056 1
        assert epl_value == EPL_SB_PRIORITY
2057
2058 1
        epl_value = EVC.get_priority([[1, 5]])
2059 1
        assert epl_value == EVPL_SB_PRIORITY
2060
2061 1
    def test_set_flow_table_group_id(self):
2062
        """Test set_flow_table_group_id"""
2063 1
        self.evc_deploy.table_group = {"epl": 3, "evpl": 4}
2064 1
        flow_mod = {}
2065 1
        self.evc_deploy.set_flow_table_group_id(flow_mod, 100)
2066 1
        assert flow_mod["table_group"] == "evpl"
2067 1
        assert flow_mod["table_id"] == 4
2068 1
        self.evc_deploy.set_flow_table_group_id(flow_mod, None)
2069 1
        assert flow_mod["table_group"] == "epl"
2070 1
        assert flow_mod["table_id"] == 3
2071
2072 1
    def test_get_endpoint_by_id(self):
2073
        """Test get_endpoint_by_id"""
2074 1
        link = MagicMock()
2075 1
        link.endpoint_a.switch.id = "01"
2076 1
        link.endpoint_b.switch.id = "02"
2077 1
        result = self.evc_deploy.get_endpoint_by_id(link, "01", operator.eq)
2078 1
        assert result == link.endpoint_a
2079 1
        result = self.evc_deploy.get_endpoint_by_id(link, "01", operator.ne)
2080 1
        assert result == link.endpoint_b
2081
2082 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._prepare_pop_flow")
2083 1
    @patch("napps.kytos.mef_eline.models.evc.EVC.get_endpoint_by_id")
2084 1
    @patch("napps.kytos.mef_eline.models.evc.EVC._prepare_push_flow")
2085 1
    def test_prepare_uni_flows(self, mock_push, mock_endpoint, _):
2086
        """Test _prepare_uni_flows"""
2087 1
        mask_list = [1, '2/4094', '4/4094']
2088 1
        uni_a = get_uni_mocked(interface_port=1, tag_value=[[1, 5]])
2089 1
        uni_a.user_tag.mask_list = mask_list
2090 1
        uni_z = get_uni_mocked(interface_port=2, tag_value=[[1, 5]])
2091 1
        uni_z.user_tag.mask_list = mask_list
2092 1
        mock_endpoint.return_value = "mock_endpoint"
2093 1
        attributes = {
2094
            "table_group": {"evpl": 3, "epl": 4},
2095
            "controller": get_controller_mock(),
2096
            "name": "custom_name",
2097
            "uni_a": uni_a,
2098
            "uni_z": uni_z,
2099
        }
2100 1
        evc = EVC(**attributes)
2101 1
        link = get_link_mocked()
2102 1
        evc._prepare_uni_flows(Path([link]))
2103 1
        call_list = []
2104 1
        for i in range(0, 3):
2105 1
            call_list.append(call(
2106
                uni_a.interface,
2107
                "mock_endpoint",
2108
                mask_list[i],
2109
                None,
2110
                mask_list,
2111
                queue_id=-1
2112
            ))
2113 1
        for i in range(0, 3):
2114 1
            call_list.append(call(
2115
                uni_z.interface,
2116
                "mock_endpoint",
2117
                mask_list[i],
2118
                None,
2119
                mask_list,
2120
                queue_id=-1
2121
            ))
2122 1
        mock_push.assert_has_calls(call_list)
2123
2124 1
    def test_prepare_direct_uni_flows(self):
2125
        """Test _prepare_direct_uni_flows"""
2126 1
        mask_list = [1, '2/4094', '4/4094']
2127 1
        uni_a = get_uni_mocked(interface_port=1, tag_value=[[1, 5]])
2128 1
        uni_a.user_tag.mask_list = mask_list
2129 1
        uni_z = get_uni_mocked(interface_port=2, tag_value=[[1, 5]])
2130 1
        uni_z.user_tag.mask_list = mask_list
2131 1
        attributes = {
2132
            "table_group": {"evpl": 3, "epl": 4},
2133
            "controller": get_controller_mock(),
2134
            "name": "custom_name",
2135
            "uni_a": uni_a,
2136
            "uni_z": uni_z,
2137
        }
2138 1
        evc = EVC(**attributes)
2139 1
        flows = evc._prepare_direct_uni_flows()[1]
2140 1
        assert len(flows) == 6
2141 1
        for i in range(0, 3):
2142 1
            assert flows[i]["match"]["in_port"] == 1
2143 1
            assert flows[i]["match"]["dl_vlan"] == mask_list[i]
2144 1
            assert flows[i]["priority"] == EVPL_SB_PRIORITY
2145 1
        for i in range(3, 6):
2146 1
            assert flows[i]["match"]["in_port"] == 2
2147 1
            assert flows[i]["match"]["dl_vlan"] == mask_list[i-3]
2148 1
            assert flows[i]["priority"] == EVPL_SB_PRIORITY
2149
2150 1
    @patch("napps.kytos.mef_eline.models.evc.EVCDeploy.check_trace")
2151 1
    def test_check_range(self, mock_check_range):
2152
        """Test check_range"""
2153 1
        mask_list = [1, '2/4094', '4/4094']
2154 1
        uni_a = get_uni_mocked(interface_port=1, tag_value=[[1, 5]])
2155 1
        uni_a.user_tag.mask_list = mask_list
2156 1
        uni_z = get_uni_mocked(interface_port=2, tag_value=[[1, 5]])
2157 1
        uni_z.user_tag.mask_list = mask_list
2158 1
        attributes = {
2159
            "table_group": {"evpl": 3, "epl": 4},
2160
            "controller": get_controller_mock(),
2161
            "name": "custom_name",
2162
            "uni_a": uni_a,
2163
            "uni_z": uni_z,
2164
        }
2165 1
        circuit = EVC(**attributes)
2166 1
        traces = list(range(0, 6))
2167 1
        mock_check_range.return_value = True
2168 1
        check = EVC.check_range(circuit, traces)
2169 1
        call_list = []
2170 1
        for i in range(0, 3):
2171 1
            call_list.append(call(
2172
                circuit.id, circuit.name,
2173
                mask_list[i], mask_list[i],
2174
                uni_a.interface,
2175
                uni_z.interface,
2176
                circuit.current_path,
2177
                i*2, i*2+1
2178
            ))
2179 1
        mock_check_range.assert_has_calls(call_list)
2180 1
        assert check
2181
2182 1
        mock_check_range.side_effect = [True, False, True]
2183 1
        check = EVC.check_range(circuit, traces)
2184 1
        assert check is False
2185
2186 1
    def test_add_tag_errors(self):
2187
        """Test add_tag_errors"""
2188 1
        msg = "No available path was found."
2189 1
        tag_errors = []
2190 1
        tag_errors.append('Mocked error 1')
2191 1
        actual = self.evc_deploy.add_tag_errors(msg, tag_errors)
2192 1
        assert actual == ('No available path was found. 1 path was rejected'
2193
                          f' with message: {tag_errors}')
2194
2195 1
        tag_errors.append('Mocked error 2')
2196 1
        actual = self.evc_deploy.add_tag_errors(msg, tag_errors)
2197 1
        assert actual == ('No available path was found. 2 paths were'
2198
                          f' rejected with messages: {tag_errors}')
2199
2200 1
    @patch("napps.kytos.mef_eline.models.evc.EVCDeploy.setup_failover_path")
2201 1
    def test_try_setup_failover_path(self, setup_failover_mock):
2202
        """Test try_setup_failover_path"""
2203 1
        self.evc_deploy.failover_path = True
2204 1
        self.evc_deploy.current_path = False
2205 1
        self.evc_deploy.is_active = MagicMock(return_value=False)
2206 1
        self.evc_deploy.try_setup_failover_path()
2207 1
        assert setup_failover_mock.call_count == 0
2208
2209 1
        self.evc_deploy.failover_path = False
2210 1
        self.evc_deploy.current_path = True
2211 1
        self.evc_deploy.is_active = MagicMock(return_value=True)
2212 1
        self.evc_deploy.try_setup_failover_path()
2213 1
        assert setup_failover_mock.call_count == 1
2214
2215 1
    @patch("napps.kytos.mef_eline.models.evc.EVCDeploy._send_flow_mods")
2216 1
    @patch(
2217
        "napps.kytos.mef_eline.models.evc.EVCDeploy._prepare_direct_uni_flows"
2218
    )
2219 1
    def test_install_direct_uni_flows_error(self, prepare_mock, send_mock):
2220
        """Test _install_direct_uni_flows with errors"""
2221 1
        prepare_mock.return_value = True, True
2222 1
        send_mock.side_effect = FlowModException
2223 1
        with pytest.raises(EVCPathNotInstalled):
2224
            self.evc_deploy._install_direct_uni_flows()
2225