Passed
Pull Request — master (#64)
by Vinicius
07:12 queued 04:20
created

test_utils.test_set_table_group()   A

Complexity

Conditions 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 10
nop 0
dl 0
loc 13
ccs 6
cts 6
cp 1
crap 1
rs 9.9
c 0
b 0
f 0
1
"""Test utils."""
2 1
import pytest
3 1
from napps.kytos.telemetry_int import utils
4 1
from napps.kytos.telemetry_int import exceptions
5
6 1
from kytos.lib.helpers import (
7
    get_interface_mock,
8
    get_controller_mock,
9
    get_switch_mock,
10
)
11
12
13 1
@pytest.mark.parametrize(
14
    "flow,expected",
15
    [
16
        (
17
            {
18
                "flow": {
19
                    "priority": 100,
20
                    "match": {"in_port": 100},
21
                    "actions": [{"action_type": "output", "port": 1}],
22
                }
23
            },
24
            {
25
                "flow": {
26
                    "priority": 100,
27
                    "match": {"in_port": 100},
28
                    "instructions": [
29
                        {
30
                            "instruction_type": "apply_actions",
31
                            "actions": [{"action_type": "output", "port": 1}],
32
                        }
33
                    ],
34
                }
35
            },
36
        ),
37
        (
38
            {
39
                "flow": {
40
                    "priority": 100,
41
                    "match": {"in_port": 100},
42
                    "instructions": [
43
                        {
44
                            "instruction_type": "apply_actions",
45
                            "actions": [{"action_type": "output", "port": 1}],
46
                        }
47
                    ],
48
                }
49
            },
50
            {
51
                "flow": {
52
                    "priority": 100,
53
                    "match": {"in_port": 100},
54
                    "instructions": [
55
                        {
56
                            "instruction_type": "apply_actions",
57
                            "actions": [{"action_type": "output", "port": 1}],
58
                        }
59
                    ],
60
                }
61
            },
62
        ),
63
    ],
64
)
65 1
def test_instructions_from_actions(flow, expected) -> None:
66
    """Test instructions from actions."""
67 1
    assert utils.set_instructions_from_actions(flow) == expected
68
69
70 1
@pytest.mark.parametrize(
71
    "cookie,expected",
72
    [
73
        (0xAA3766C105686749, 0xA83766C105686749),
74
        (0xAACBEE9338673946, 0xA8CBEE9338673946),
75
    ],
76
)
77 1
def test_get_new_cookie(cookie, expected) -> None:
78
    """test get_new_cookie."""
79 1
    assert utils.get_new_cookie(cookie) == expected
80
81
82 1
@pytest.mark.parametrize(
83
    "cookie,expected_evc_id",
84
    [
85
        (0xAA3766C105686749, "3766c105686749"),
86
        (0xAACBEE9338673946, "cbee9338673946"),
87
    ],
88
)
89 1
def test_get_id_from_cookie(cookie, expected_evc_id) -> None:
90
    """test get_id_from_cookie."""
91 1
    assert utils.get_id_from_cookie(cookie) == expected_evc_id
92
93
94 1
def test_set_new_cookie() -> None:
95
    """Test set_new_cookie."""
96 1
    flow = {"flow": {"cookie": 0xAA3766C105686749}}
97 1
    utils.set_new_cookie(flow)
98 1
    assert flow["flow"]["cookie"] == 0xA83766C105686749
99
100
101 1
@pytest.mark.parametrize(
102
    "evc_dict,expected",
103
    [
104
        ({"metadata": {"telemetry": {"enabled": True}}}, True),
105
        ({"metadata": {"telemetry": {"enabled": False}}}, False),
106
        ({"metadata": {}}, False),
107
    ],
108
)
109 1
def test_has_int_enabled(evc_dict, expected) -> None:
110
    """test has_int_enabled."""
111 1
    assert utils.has_int_enabled(evc_dict) == expected
112
113
114 1
def test_get_evc_unis() -> None:
115
    """test get_evc_unis."""
116 1
    evc = {
117
        "uni_a": {
118
            "tag": {"tag_type": 1, "value": 200},
119
            "interface_id": "00:00:00:00:00:00:00:01:1",
120
        },
121
        "uni_z": {
122
            "tag": {"tag_type": 1, "value": 200},
123
            "interface_id": "00:00:00:00:00:00:00:01:2",
124
        },
125
    }
126 1
    uni_a, uni_z = utils.get_evc_unis(evc)
127 1
    assert uni_a["interface_id"] == evc["uni_a"]["interface_id"]
128 1
    assert uni_a["port_number"] == 1
129 1
    assert uni_a["switch"] == "00:00:00:00:00:00:00:01"
130
131 1
    assert uni_z["interface_id"] == evc["uni_z"]["interface_id"]
132 1
    assert uni_z["port_number"] == 2
133 1
    assert uni_z["switch"] == "00:00:00:00:00:00:00:01"
134
135
136 1
def test_is_intra_switch_evc() -> None:
137
    """test is_instra_switch_evc."""
138 1
    evc = {
139
        "uni_a": {
140
            "tag": {"tag_type": 1, "value": 200},
141
            "interface_id": "00:00:00:00:00:00:00:01:1",
142
        },
143
        "uni_z": {
144
            "tag": {"tag_type": 1, "value": 200},
145
            "interface_id": "00:00:00:00:00:00:00:01:2",
146
        },
147
    }
148 1
    assert utils.is_intra_switch_evc(evc)
149 1
    evc["uni_a"]["interface_id"] = "00:00:00:00:00:00:00:02:1"
150 1
    assert not utils.is_intra_switch_evc(evc)
151
152
153 1
def test_add_to_apply_actions() -> None:
154
    """Test add to apply actions."""
155 1
    instructions = [
156
        {
157
            "instruction_type": "apply_actions",
158
            "actions": [
159
                {"action_type": "set_vlan", "vlan_id": 200},
160
                {"action_type": "output", "port": 5},
161
            ],
162
        }
163
    ]
164 1
    assert instructions[0]["actions"][0] == {"action_type": "set_vlan", "vlan_id": 200}
165 1
    new_instruction = {"action_type": "add_int_metadata"}
166 1
    utils.add_to_apply_actions(instructions, new_instruction, position=0)
167 1
    assert instructions[0]["actions"][0] == new_instruction
168
169
170 1
def test_set_owner() -> None:
171
    """Test set_owner."""
172 1
    flow = {
173
        "flow": {
174
            "priority": 100,
175
            "match": {"in_port": 100},
176
            "actions": [{"action_type": "output", "port": 1}],
177
        }
178
    }
179 1
    utils.set_owner(flow)
180 1
    assert flow["flow"]["owner"] == "telemetry_int"
181
182
183 1
@pytest.mark.parametrize(
184
    "actions,actions_to_change,remove,expected_actions",
185
    [
186
        (
187
            [
188
                {"action_type": "set_queue", "queue_id": 1},
189
                {"action_type": "set_vlan", "vlan_id": 200},
190
                {"action_type": "output", "port": 5},
191
            ],
192
            ["set_vlan"],
193
            True,
194
            [
195
                {"action_type": "set_queue", "queue_id": 1},
196
                {"action_type": "output", "port": 5},
197
            ],
198
        ),
199
        (
200
            [
201
                {"action_type": "set_queue", "queue_id": 1},
202
                {"action_type": "set_vlan", "vlan_id": 200},
203
                {"action_type": "output", "port": 5},
204
            ],
205
            ["set_vlan"],
206
            False,
207
            [
208
                {"action_type": "set_vlan", "vlan_id": 200},
209
            ],
210
        ),
211
    ],
212
)
213 1
def test_modify_actions(actions, actions_to_change, remove, expected_actions) -> None:
214
    """test modify_actions."""
215 1
    assert utils.modify_actions(actions, actions_to_change, remove) == expected_actions
216
217
218 1
def test_get_proxy_port_or_raise() -> None:
219
    """Test proxy_port_or_raise."""
220 1
    dpid_a = "00:00:00:00:00:00:00:01"
221 1
    mock_switch_a = get_switch_mock(dpid_a, 0x04)
222 1
    mock_interface_a = get_interface_mock("s1-eth1", 1, mock_switch_a)
223 1
    mock_interface_a.metadata = {}
224 1
    intf_id = f"{dpid_a}:1"
225 1
    controller = get_controller_mock()
226 1
    evc_id = "3766c105686749"
227
228
    # Initially the mocked interface and switch hasn't been associated in the controller
229 1
    with pytest.raises(exceptions.ProxyPortNotFound) as exc:
230 1
        utils.get_proxy_port_or_raise(controller, intf_id, evc_id)
231 1
    assert f"interface {intf_id} not found" in str(exc)
232
233
    # Now, proxy_port still hasn't been set yet
234 1
    controller.get_interface_by_id = lambda x: mock_interface_a
235 1
    with pytest.raises(exceptions.ProxyPortNotFound) as exc:
236 1
        utils.get_proxy_port_or_raise(controller, intf_id, evc_id)
237 1
    assert f"proxy_port metadata not found in {intf_id}" in str(exc)
238
239
    # Now, destination interface hasn't been mocked yet
240 1
    mock_interface_a.metadata = {"proxy_port": 5}
241 1
    with pytest.raises(exceptions.ProxyPortNotFound) as exc:
242 1
        utils.get_proxy_port_or_raise(controller, intf_id, evc_id)
243 1
    assert "destination interface not found" in str(exc)
244
245 1
    mock_interface_b = get_interface_mock("s1-eth5", 5, mock_switch_a)
246 1
    mock_interface_b.metadata = {"looped": {"port_numbers": [5, 6]}}
247 1
    mock_interface_a.switch.get_interface_by_port_no = lambda x: mock_interface_b
248
    # Now all dependencies have been mocked and it should get the ProxyPort
249 1
    pp = utils.get_proxy_port_or_raise(controller, intf_id, evc_id)
250
    assert pp.source == mock_interface_b
251