Passed
Push — master ( e3f2ff...728489 )
by Vinicius
02:32 queued 14s
created

TestUtils.test_check_disabled_component()   A

Complexity

Conditions 4

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 19
nop 1
dl 0
loc 29
ccs 19
cts 19
cp 1
crap 4
rs 9.45
c 0
b 0
f 0
1
"""Module to test the utls.py file."""
2 1
from unittest import TestCase
3 1
from unittest.mock import MagicMock
4
5 1
import pytest
6
7 1
from kytos.core.common import EntityStatus
8 1
from napps.kytos.mef_eline.exceptions import DisabledSwitch
9 1
from napps.kytos.mef_eline.utils import (check_disabled_component,
10
                                         compare_endpoint_trace,
11
                                         compare_uni_out_trace,
12
                                         get_vlan_tags_and_masks, map_dl_vlan)
13
14
15
# pylint: disable=too-many-public-methods, too-many-lines
16 1
class TestUtils(TestCase):
17
    """Test utility functions."""
18
19 1
    def test_compare_endpoint_trace(self):
20
        """Test method compare_endpoint_trace"""
21
22 1
        trace = {"dpid": "1234", "port": 2, "vlan": 123}
23
24 1
        switch1 = MagicMock()
25 1
        switch1.dpid = "1234"
26 1
        switch2 = MagicMock()
27 1
        switch2.dpid = "2345"
28
29 1
        endpoint = MagicMock()
30 1
        endpoint.port_number = 2
31 1
        vlan = 123
32
33 1
        for switch, expected in ((switch1, True), (switch2, False)):
34 1
            with self.subTest(switch=switch, expected=expected):
35 1
                endpoint.switch = switch
36 1
                self.assertEqual(
37
                    compare_endpoint_trace(endpoint, vlan, trace), expected
38
                )
39 1
                self.assertEqual(
40
                    compare_endpoint_trace(endpoint, None, trace), expected
41
                )
42
43 1
    def test_compare_uni_out_trace(self):
44
        """Test compare_uni_out_trace method."""
45
        # case1: trace without 'out' info, should return True
46 1
        uni = MagicMock()
47 1
        self.assertTrue(compare_uni_out_trace(uni, {}))
48
49
        # case2: trace with valid port and VLAN, should return True
50 1
        uni.interface.port_number = 1
51 1
        uni.user_tag.value = 123
52 1
        trace = {"out": {"port": 1, "vlan": 123}}
53 1
        self.assertTrue(compare_uni_out_trace(uni, trace))
54
55
        # case3: UNI has VLAN but trace dont have, should return False
56 1
        trace = {"out": {"port": 1}}
57 1
        self.assertFalse(compare_uni_out_trace(uni, trace))
58
59
        # case4: UNI and trace dont have VLAN should return True
60 1
        uni.user_tag = None
61 1
        self.assertTrue(compare_uni_out_trace(uni, trace))
62
63
        # case5: UNI dont have VLAN but trace has, should return False
64 1
        trace = {"out": {"port": 1, "vlan": 123}}
65 1
        self.assertFalse(compare_uni_out_trace(uni, trace))
66
67 1
    def test_map_dl_vlan(self):
68
        """Test map_dl_vlan"""
69 1
        cases = {0: None, "untagged": None, "any": 1, "4096/4096": 1, 10: 10}
70 1
        for value, mapped in cases.items():
71 1
            result = map_dl_vlan(value)
72 1
            assert result == mapped
73
74 1
    def test_get_vlan_tags_and_masks(self):
75
        """Test get_vlan_tags_and_masks"""
76 1
        vlan_ranges = [[101, 200], [101, 90], [34, 34]]
77 1
        expecteds = [
78
            [
79
                (101, 4095),
80
                (102, 4094),
81
                (104, 4088),
82
                (112, 4080),
83
                (128, 4032),
84
                (192, 4088),
85
                (200, 4095),
86
            ],
87
            [],
88
            [(34, 4095)],
89
        ]
90 1
        for vlan_range, expected in zip(vlan_ranges, expecteds):
91 1
            with self.subTest(range=vlan_range, expected=expected):
92 1
                assert get_vlan_tags_and_masks(*vlan_range) == expected
93
94 1
    def test_check_disabled_component(self):
95
        """Test check disabled component"""
96 1
        uni_a = MagicMock()
97 1
        switch = MagicMock()
98 1
        switch.status = EntityStatus.DISABLED
99 1
        uni_a.interface.switch = switch
100
101 1
        uni_z = MagicMock()
102 1
        uni_z.interface.switch = switch
103
104
        # Switch disabled
105 1
        with pytest.raises(DisabledSwitch):
106 1
            check_disabled_component(uni_a, uni_z)
107
108
        # Uni_a interface disabled
109 1
        switch.status = EntityStatus.UP
110 1
        uni_a.interface.status = EntityStatus.DISABLED
111 1
        with pytest.raises(DisabledSwitch):
112 1
            check_disabled_component(uni_a, uni_z)
113
114
        # Uni_z interface disabled
115 1
        uni_a.interface.status = EntityStatus.UP
116 1
        uni_z.interface.status = EntityStatus.DISABLED
117 1
        with pytest.raises(DisabledSwitch):
118 1
            check_disabled_component(uni_a, uni_z)
119
120
        # There is no disabled component
121 1
        uni_z.interface.status = EntityStatus.UP
122
        check_disabled_component(uni_a, uni_z)
123