Test Failed
Pull Request — master (#697)
by
unknown
06:22 queued 01:48
created

TestUtils.test_map_dl_vlan()   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
"""Module to test the utls.py file."""
2 1
from unittest.mock import MagicMock, Mock
3 1
import pytest
4
5 1
from kytos.core.common import EntityStatus
6 1
from napps.kytos.mef_eline.exceptions import DisabledSwitch
7 1
from napps.kytos.mef_eline.utils import (compare_endpoint_trace,
8
                                         compare_uni_out_trace,
9
                                         get_vlan_tags_and_masks, map_dl_vlan,
10
                                         merge_flow_dicts, prepare_delete_flow,
11
                                         _does_uni_affect_evc)
12
13
14
# pylint: disable=too-many-public-methods, too-many-lines
15
class TestUtils:
16 1
    """Test utility functions."""
17
18
    @pytest.mark.parametrize(
19 1
        "switch,expected",
20
        [
21
            (
22
                MagicMock(dpid="1234"),
23
                True
24
            ),
25
            (
26
                MagicMock(dpid="2345"),
27
                False
28
            )
29
        ]
30
    )
31
    def test_compare_endpoint_trace(self, switch, expected):
32 1
        """Test method compare_endpoint_trace"""
33
        trace = {"dpid": "1234", "port": 2, "vlan": 123}
34 1
35
        endpoint = MagicMock()
36 1
        endpoint.port_number = 2
37 1
        vlan = 123
38 1
        endpoint.switch = switch
39 1
        assert compare_endpoint_trace(endpoint, vlan, trace) == expected
40 1
        assert compare_endpoint_trace(endpoint, None, trace) == expected
41 1
42
    def test_compare_uni_out_trace(self):
43 1
        """Test compare_uni_out_trace method."""
44
        # case1: trace without 'out' info, should return True
45
        interface = MagicMock()
46 1
        assert compare_uni_out_trace(None, interface, {})
47 1
48
        # case2: trace with valid port and VLAN, should return True
49
        interface.port_number = 1
50 1
        tag_value = 123
51 1
        trace = {"out": {"port": 1, "vlan": 123}}
52 1
        assert compare_uni_out_trace(tag_value, interface, trace)
53 1
54
        # case3: UNI has VLAN but trace dont have, should return False
55
        trace = {"out": {"port": 1}}
56 1
        assert compare_uni_out_trace(tag_value, interface, trace) is False
57 1
58
        # case4: UNI and trace dont have VLAN should return True
59
        assert compare_uni_out_trace(None, interface, trace)
60 1
61
        # case5: UNI dont have VLAN but trace has, should return False
62
        trace = {"out": {"port": 1, "vlan": 123}}
63 1
        assert compare_uni_out_trace(None, interface, trace) is False
64 1
65
    def test_map_dl_vlan(self):
66 1
        """Test map_dl_vlan"""
67
        cases = {0: None, "untagged": None, "any": 1, "4096/4096": 1, 10: 10}
68 1
        for value, mapped in cases.items():
69 1
            result = map_dl_vlan(value)
70 1
            assert result == mapped
71 1
72
    @pytest.mark.parametrize(
73 1
        "vlan_range,expected",
74
        [
75
            (
76
                [[101, 200]],
77
                [
78
                    101,
79
                    "102/4094",
80
                    "104/4088",
81
                    "112/4080",
82
                    "128/4032",
83
                    "192/4088",
84
                    200,
85
                ]
86
            ),
87
            (
88
                [[101, 90]],
89
                []
90
            ),
91
            (
92
                [[34, 34]],
93
                [34]
94
            ),
95
            (
96
                [
97
                    [34, 34],
98
                    [128, 128],
99
                    [130, 135]
100
                ],
101
                [
102
                    34,
103
                    128,
104
                    "130/4094",
105
                    "132/4092"
106
                ]
107
            )
108
        ]
109
    )
110
    def test_get_vlan_tags_and_masks(self, vlan_range, expected):
111 1
        """Test get_vlan_tags_and_masks"""
112
        assert get_vlan_tags_and_masks(vlan_range) == expected
113 1
114
    @pytest.mark.parametrize(
115 1
        "src1,src2,src3,expected",
116
        [
117 1
            (
118 1
                {"dpida": [10, 11, 12]},
119 1
                {"dpida": [11, 20, 21]},
120 1
                {"dpidb": [30, 31, 32]},
121
                {"dpida": [10, 11, 12, 11, 20, 21], "dpidb": [30, 31, 32]},
122 1
            ),
123 1
            (
124
                {"dpida": [10, 11, 12]},
125
                {"dpida": [11, 20, 21], "dpidb": [40, 41, 42]},
126 1
                {"dpidb": [30, 31, 32]},
127 1
                {"dpida": [10, 11, 12, 11, 20, 21],
128
                 "dpidb": [40, 41, 42, 30, 31, 32]},
129
            ),
130 1
        ]
131 1
    )
132 1
    def test_merge_flow_dicts(self, src1, src2, src3, expected) -> None:
133 1
        """test merge flow dicts."""
134
        assert merge_flow_dicts({}, src1, src2, src3) == expected
135
136 1
    def test_prepare_delete_flow(self):
137 1
        """Test prepare_delete_flow"""
138 1
        cookie_mask = int(0xffffffffffffffff)
139 1
        flow_mod = {'00:01': [
140
            {
141
                'match': {'in_port': 1, 'dl_vlan': 22},
142 1
                'cookie': 12275899796742638400,
143 1
                'actions': [{'action_type': 'pop_vlan'}],
144
                'owner': 'mef_eline',
145 1
                'table_group': 'evpl',
146
                'table_id': 0,
147
                'priority': 20000
148
            },
149
            {
150
                'match': {'in_port': 3, 'dl_vlan': 1},
151
                'cookie': 12275899796742638400,
152
                'actions': [{'action_type': 'pop_vlan'}],
153
                'owner': 'mef_eline',
154
                'table_group': 'evpl',
155
                'table_id': 0,
156
                'priority': 20000
157
            }
158
        ]}
159
        actual_flows = prepare_delete_flow(flow_mod)
160
        assert '00:01' in actual_flows
161
        for i in range(len(actual_flows['00:01'])):
162
            assert (actual_flows['00:01'][i]['cookie'] ==
163 1
                    flow_mod["00:01"][i]['cookie'])
164
            assert (actual_flows['00:01'][i]['match'] ==
165 1
                    flow_mod["00:01"][i]['match'])
166
            assert actual_flows['00:01'][i]['cookie_mask'] == cookie_mask
167 1
168
    # pylint: disable=too-many-arguments
169 1
    @pytest.mark.parametrize(
170 1
        "intf_a_status, intf_z_status, is_active, is_uni, event, expected",
171
        [
172
            # link_DOWN
173
            (
174
                EntityStatus.DOWN, EntityStatus.DOWN,
175
                True, True, 'down', True
176
            ),
177
            (
178
                EntityStatus.UP, EntityStatus.UP,
179
                True, True, 'down', False
180
            ),
181
            (
182
                EntityStatus.DOWN, EntityStatus.UP,
183
                False, True, 'down', False
184
            ),
185
            (
186
                EntityStatus.UP, EntityStatus.UP,
187
                False, True, 'down', False
188
            ),
189
            (  # Not UNI
190 1
                EntityStatus.DOWN, EntityStatus.DOWN,
191 1
                True, False, 'down', False
192 1
            ),
193 1
            # link_up
194
            (
195 1
                EntityStatus.DOWN, EntityStatus.DOWN,
196
                True, True, 'up', False
197 1
            ),
198
            (
199
                EntityStatus.UP, EntityStatus.UP,
200 1
                True, True, 'up', False
201
            ),
202
            (
203
                EntityStatus.DOWN, EntityStatus.UP,
204
                False, True, 'up', False
205
            ),
206
            (
207
                EntityStatus.UP, EntityStatus.UP,
208
                False, True, 'up', True
209
            ),
210
            (  # Not UNI
211
                EntityStatus.UP, EntityStatus.UP,
212
                False, False, 'up', False
213
            ),
214
        ]
215
    )
216
    def test_does_uni_affect_evc(
217
        self,
218
        intf_a_status,
219
        intf_z_status,
220
        is_active,
221
        is_uni,
222
        event,
223
        expected
224
    ):
225
        """Test _does_uni_affect_evc when interface."""
226
        evc = Mock()
227
        evc.uni_a.interface.status = intf_a_status
228
        evc.uni_z.interface.status = intf_z_status
229
        if is_uni:
230
            interface = evc.uni_a.interface
231
        else:
232
            interface = Mock()
233
        evc.is_active.return_value = is_active
234
        assert _does_uni_affect_evc(evc, interface, event) is expected
235