Passed
Push — master ( 442faf...fe62f4 )
by Vinicius
08:31 queued 06:57
created

TestINTManager.test_redeploy_int()   A

Complexity

Conditions 1

Size

Total Lines 32
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 25
nop 2
dl 0
loc 32
ccs 20
cts 20
cp 1
crap 1
rs 9.28
c 0
b 0
f 0
1
"""Test INTManager"""
2 1
import pytest
3
4 1
from unittest.mock import AsyncMock, MagicMock
5 1
from napps.kytos.telemetry_int.exceptions import ProxyPortSameSourceIntraEVC
6 1
from napps.kytos.telemetry_int.managers.int import INTManager
7 1
from napps.kytos.telemetry_int import exceptions
8 1
from kytos.core.common import EntityStatus
9
10 1
from kytos.lib.helpers import (
11
    get_interface_mock,
12
    get_controller_mock,
13
    get_switch_mock,
14
)
15
16
17 1
class TestINTManager:
18
19
    """TestINTManager."""
20
21 1
    def test_get_proxy_port_or_raise(self) -> None:
22
        """Test proxy_port_or_raise."""
23 1
        dpid_a = "00:00:00:00:00:00:00:01"
24 1
        mock_switch_a = get_switch_mock(dpid_a, 0x04)
25 1
        mock_interface_a = get_interface_mock("s1-eth1", 1, mock_switch_a)
26 1
        mock_interface_a.metadata = {}
27 1
        intf_id = f"{dpid_a}:1"
28 1
        controller = get_controller_mock()
29 1
        evc_id = "3766c105686749"
30 1
        int_manager = INTManager(controller)
31
32
        # Initially the mocked interface and switch hasn't been associated in the ctrllr
33 1
        with pytest.raises(exceptions.ProxyPortNotFound) as exc:
34 1
            int_manager.get_proxy_port_or_raise(intf_id, evc_id)
35 1
        assert f"interface {intf_id} not found" in str(exc)
36
37
        # Now, proxy_port still hasn't been set yet
38 1
        controller.get_interface_by_id = lambda x: mock_interface_a
39 1
        with pytest.raises(exceptions.ProxyPortNotFound) as exc:
40 1
            int_manager.get_proxy_port_or_raise(intf_id, evc_id)
41 1
        assert f"proxy_port metadata not found in {intf_id}" in str(exc)
42
43
        # Now, destination interface hasn't been mocked yet
44 1
        mock_interface_a.metadata = {"proxy_port": 5}
45 1
        with pytest.raises(exceptions.ProxyPortDestNotFound) as exc:
46 1
            int_manager.get_proxy_port_or_raise(intf_id, evc_id)
47 1
        assert "destination interface not found" in str(exc)
48
49 1
        mock_interface_b = get_interface_mock("s1-eth5", 5, mock_switch_a)
50 1
        mock_interface_b.metadata = {"looped": {"port_numbers": [5, 6]}}
51 1
        mock_interface_a.switch.get_interface_by_port_no = lambda x: mock_interface_b
52
        # Now all dependencies have been mocked and it should get the ProxyPort
53 1
        pp = int_manager.get_proxy_port_or_raise(intf_id, evc_id)
54 1
        assert pp.source == mock_interface_b
55
56 1
    def test_load_uni_src_proxy_port(self) -> None:
57
        """Test test_load_uni_src_proxy_port."""
58 1
        dpid_a = "00:00:00:00:00:00:00:01"
59 1
        mock_switch_a = get_switch_mock(dpid_a, 0x04)
60 1
        mock_interface_a = get_interface_mock("s1-eth1", 1, mock_switch_a)
61 1
        mock_interface_a.metadata = {"proxy_port": 3}
62 1
        mock_interface_z = get_interface_mock("s1-eth2", 2, mock_switch_a)
63 1
        mock_interface_z.metadata = {"proxy_port": 5}
64 1
        intf_id_a = f"{dpid_a}:1"
65 1
        intf_id_z = f"{dpid_a}:2"
66 1
        intf_id_a_1 = f"{dpid_a}:3"
67 1
        intf_id_z_1 = f"{dpid_a}:5"
68
69 1
        mock_interface_a_1 = get_interface_mock("s1-eth3", 3, mock_switch_a)
70 1
        mock_interface_a_1.metadata = {"looped": {"port_numbers": [3, 4]}}
71 1
        mock_interface_a_2 = get_interface_mock("s1-eth4", 4, mock_switch_a)
72 1
        mock_interface_z_1 = get_interface_mock("s1-eth5", 5, mock_switch_a)
73 1
        mock_interface_z_1.metadata = {"looped": {"port_numbers": [5, 6]}}
74 1
        mock_interface_z_2 = get_interface_mock("s1-eth6", 6, mock_switch_a)
75
76 1
        def get_interface_by_port_no(port_no):
77 1
            data = {
78
                1: mock_interface_a,
79
                2: mock_interface_z,
80
                3: mock_interface_a_1,
81
                4: mock_interface_a_2,
82
                5: mock_interface_z_1,
83
                6: mock_interface_z_2,
84
            }
85 1
            return data[port_no]
86
87 1
        def get_interface_by_id(intf_id):
88 1
            data = {
89
                intf_id_a: mock_interface_a,
90
                intf_id_z: mock_interface_z,
91
            }
92 1
            return data[intf_id]
93
94 1
        controller = get_controller_mock()
95 1
        mock_switch_a.get_interface_by_port_no = get_interface_by_port_no
96 1
        controller.get_interface_by_id = get_interface_by_id
97
98 1
        evcs = {
99
            "3766c105686749": {
100
                "metadata": {"telemetry": {"enabled": True}},
101
                "uni_a": {"interface_id": intf_id_a},
102
                "uni_z": {"interface_id": intf_id_z},
103
            },
104
            "3766c105686748": {
105
                "metadata": {"telemetry": {"enabled": True}},
106
                "uni_a": {"interface_id": intf_id_a},
107
                "uni_z": {"interface_id": intf_id_z},
108
            },
109
            "3766c105686747": {
110
                "metadata": {"telemetry": {"enabled": False}},
111
                "uni_a": {"interface_id": intf_id_a},
112
                "uni_z": {"interface_id": intf_id_z},
113
            },
114
        }
115 1
        int_manager = INTManager(controller)
116 1
        int_manager.load_uni_src_proxy_ports(evcs)
117 1
        assert len(int_manager.unis_src) == 2
118 1
        assert int_manager.unis_src[intf_id_a] == intf_id_a_1
119 1
        assert int_manager.unis_src[intf_id_z] == intf_id_z_1
120
121 1
        assert len(int_manager.srcs_pp) == 2
122 1
        assert int_manager.srcs_pp[intf_id_a_1].source == mock_interface_a_1
123 1
        assert int_manager.srcs_pp[intf_id_a_1].destination == mock_interface_a_2
124 1
        assert int_manager.srcs_pp[intf_id_z_1].source == mock_interface_z_1
125 1
        assert int_manager.srcs_pp[intf_id_z_1].destination == mock_interface_z_2
126
127 1
        assert int_manager.srcs_pp[intf_id_a_1].evc_ids == {
128
            "3766c105686749",
129
            "3766c105686748",
130
        }
131 1
        assert int_manager.srcs_pp[intf_id_z_1].evc_ids == {
132
            "3766c105686749",
133
            "3766c105686748",
134
        }
135
136 1
    async def test_handle_pp_link_down(self, monkeypatch):
137
        """Test test_handle_pp_link_down."""
138 1
        int_manager = INTManager(MagicMock())
139 1
        api_mock, link_mock, pp_mock = AsyncMock(), MagicMock(), MagicMock()
140 1
        link_mock.endpoint_a.id = "some_intf_id"
141 1
        evc_id = "3766c105686748"
142 1
        int_manager.srcs_pp[link_mock.endpoint_a.id] = pp_mock
143 1
        pp_mock.evc_ids = {evc_id}
144
145 1
        monkeypatch.setattr("napps.kytos.telemetry_int.managers.int.api", api_mock)
146 1
        api_mock.get_evcs.return_value = {evc_id: {}}
147 1
        int_manager.remove_int_flows = AsyncMock()
148
149 1
        await int_manager.handle_pp_link_down(link_mock)
150 1
        assert api_mock.get_evcs.call_count == 1
151 1
        assert api_mock.get_evcs.call_count == 1
152 1
        assert api_mock.get_evcs.call_args[1] == {
153
            "metadata.telemetry.enabled": "true",
154
            "metadata.telemetry.status": "UP",
155
        }
156 1
        assert int_manager.remove_int_flows.call_count == 1
157 1
        args = int_manager.remove_int_flows.call_args[0]
158 1
        assert evc_id in args[0]
159 1
        assert "telemetry" in args[1]
160 1
        telemetry = args[1]["telemetry"]
161 1
        assert telemetry["enabled"]
162 1
        assert telemetry["status"] == "DOWN"
163 1
        assert telemetry["status_reason"] == ["proxy_port_down"]
164 1
        assert "status_updated_at" in telemetry
165
166 1
    async def test_handle_pp_link_up(self, monkeypatch):
167
        """Test handle_pp_link_up."""
168 1
        int_manager = INTManager(MagicMock())
169 1
        api_mock, link_mock, pp_mock = AsyncMock(), MagicMock(), MagicMock()
170 1
        sleep_mock = AsyncMock()
171 1
        link_mock.endpoint_a.id = "3"
172 1
        pp_mock.status = EntityStatus.UP
173 1
        link_mock.status = EntityStatus.UP
174 1
        link_mock.status_reason = []
175 1
        evc_id = "3766c105686748"
176 1
        uni_a_id, uni_z_id = "1", "2"
177 1
        src_a_id, src_z_id = "3", "5"
178 1
        int_manager.srcs_pp[src_a_id] = pp_mock
179 1
        int_manager.srcs_pp[src_z_id] = pp_mock
180 1
        int_manager.unis_src[uni_a_id] = src_a_id
181 1
        int_manager.unis_src[uni_z_id] = src_z_id
182 1
        pp_mock.evc_ids = {evc_id}
183
184 1
        monkeypatch.setattr("napps.kytos.telemetry_int.managers.int.api", api_mock)
185 1
        monkeypatch.setattr(
186
            "napps.kytos.telemetry_int.managers.int.asyncio.sleep", sleep_mock
187
        )
188 1
        api_mock.get_evcs.return_value = {
189
            evc_id: {
190
                "active": True,
191
                "archived": False,
192
                "uni_a": {"interface_id": uni_a_id},
193
                "uni_z": {"interface_id": uni_z_id},
194
            }
195
        }
196 1
        int_manager.install_int_flows = AsyncMock()
197 1
        int_manager._validate_map_enable_evcs = MagicMock()
198
199 1
        await int_manager.handle_pp_link_up(link_mock)
200 1
        assert sleep_mock.call_count == 1
201 1
        assert api_mock.get_evcs.call_count == 1
202 1
        assert api_mock.get_evcs.call_args[1] == {
203
            "metadata.telemetry.enabled": "true",
204
            "metadata.telemetry.status": "DOWN",
205
        }
206 1
        assert int_manager.install_int_flows.call_count == 1
207 1
        args = int_manager.install_int_flows.call_args[0]
208 1
        assert "telemetry" in args[1]
209 1
        telemetry_dict = args[1]["telemetry"]
210 1
        expected_keys = ["enabled", "status", "status_reason", "status_updated_at"]
211 1
        assert sorted(list(telemetry_dict.keys())) == sorted(expected_keys)
212 1
        assert telemetry_dict["enabled"]
213 1
        assert telemetry_dict["status"] == "UP"
214 1
        assert not telemetry_dict["status_reason"]
215
216 1
    async def test_handle_pp_metadata_removed(self, monkeypatch):
217
        """Test handle_pp_metadata_removed."""
218 1
        int_manager = INTManager(MagicMock())
219 1
        api_mock, intf_mock, pp_mock = AsyncMock(), MagicMock(), MagicMock()
220 1
        intf_mock.id = "some_intf_id"
221 1
        source_id = "some_source_id"
222 1
        evc_id = "3766c105686748"
223 1
        int_manager.unis_src[intf_mock.id] = source_id
224 1
        int_manager.srcs_pp[source_id] = pp_mock
225 1
        pp_mock.evc_ids = {evc_id}
226
227 1
        assert "proxy_port" not in intf_mock.metadata
228 1
        monkeypatch.setattr("napps.kytos.telemetry_int.managers.int.api", api_mock)
229 1
        api_mock.get_evcs.return_value = {evc_id: {}}
230 1
        int_manager.remove_int_flows = AsyncMock()
231
232 1
        await int_manager.handle_pp_metadata_removed(intf_mock)
233 1
        assert api_mock.get_evcs.call_count == 1
234 1
        assert api_mock.get_evcs.call_count == 1
235 1
        assert api_mock.get_evcs.call_args[1] == {
236
            "metadata.telemetry.enabled": "true",
237
            "metadata.telemetry.status": "UP",
238
        }
239 1
        assert int_manager.remove_int_flows.call_count == 1
240 1
        args = int_manager.remove_int_flows.call_args[0]
241 1
        assert evc_id in args[0]
242 1
        assert "telemetry" in args[1]
243 1
        telemetry = args[1]["telemetry"]
244 1
        assert telemetry["enabled"]
245 1
        assert telemetry["status"] == "DOWN"
246 1
        assert telemetry["status_reason"] == ["proxy_port_metadata_removed"]
247 1
        assert "status_updated_at" in telemetry
248
249 1
    async def test_disable_int_metadata(self, monkeypatch) -> None:
250
        """Test disable INT metadata args."""
251 1
        controller = MagicMock()
252 1
        api_mock = AsyncMock()
253 1
        monkeypatch.setattr("napps.kytos.telemetry_int.managers.int.api", api_mock)
254
255 1
        int_manager = INTManager(controller)
256 1
        int_manager._remove_int_flows = AsyncMock()
257 1
        await int_manager.disable_int({}, False)
258
259 1
        assert api_mock.add_evcs_metadata.call_count == 1
260 1
        args = api_mock.add_evcs_metadata.call_args[0]
261 1
        assert args[0] == {}
262 1
        assert "telemetry" in args[1]
263 1
        telemetry_dict = args[1]["telemetry"]
264 1
        expected_keys = ["enabled", "status", "status_reason", "status_updated_at"]
265 1
        assert sorted(list(telemetry_dict.keys())) == sorted(expected_keys)
266
267 1
        assert not telemetry_dict["enabled"]
268 1
        assert telemetry_dict["status"] == "DOWN"
269 1
        assert telemetry_dict["status_reason"] == ["disabled"]
270
271 1
        assert args[2] is False
272
273 1
    async def test_enable_int_metadata(self, monkeypatch) -> None:
274
        """Test enable INT metadata args."""
275 1
        controller = MagicMock()
276 1
        api_mock = AsyncMock()
277 1
        stored_flows_mock = AsyncMock()
278 1
        monkeypatch.setattr("napps.kytos.telemetry_int.managers.int.api", api_mock)
279 1
        monkeypatch.setattr(
280
            "napps.kytos.telemetry_int.utils.get_found_stored_flows", stored_flows_mock
281
        )
282
283 1
        int_manager = INTManager(controller)
284 1
        int_manager.remove_int_flows = AsyncMock()
285 1
        await int_manager.enable_int({}, False)
286
287 1
        assert stored_flows_mock.call_count == 1
288 1
        assert api_mock.add_evcs_metadata.call_count == 1
289 1
        args = api_mock.add_evcs_metadata.call_args[0]
290 1
        assert args[0] == {}
291 1
        assert "telemetry" in args[1]
292 1
        telemetry_dict = args[1]["telemetry"]
293 1
        expected_keys = ["enabled", "status", "status_reason", "status_updated_at"]
294 1
        assert sorted(list(telemetry_dict.keys())) == sorted(expected_keys)
295
296 1
        assert telemetry_dict["enabled"] is True
297 1
        assert telemetry_dict["status"] == "UP"
298 1
        assert telemetry_dict["status_reason"] == []
299
300 1
    async def test_redeploy_int(self, monkeypatch) -> None:
301
        """Test redeploy int."""
302 1
        controller = MagicMock()
303 1
        api_mock = AsyncMock()
304 1
        stored_flows_mock = AsyncMock()
305 1
        monkeypatch.setattr("napps.kytos.telemetry_int.managers.int.api", api_mock)
306 1
        monkeypatch.setattr(
307
            "napps.kytos.telemetry_int.utils.get_found_stored_flows", stored_flows_mock
308
        )
309
310 1
        int_manager = INTManager(controller)
311 1
        int_manager._remove_int_flows = AsyncMock()
312 1
        int_manager._install_int_flows = AsyncMock()
313
314 1
        dpid_a = "00:00:00:00:00:00:00:01"
315 1
        intf_id_a = f"{dpid_a}:1"
316 1
        intf_id_z = f"{dpid_a}:2"
317 1
        evc_id = "3766c105686749"
318 1
        evcs = {
319
            evc_id: {
320
                "metadata": {"telemetry": {"enabled": True}},
321
                "uni_a": {"interface_id": intf_id_a},
322
                "uni_z": {"interface_id": intf_id_z},
323
            }
324
        }
325 1
        int_manager._validate_map_enable_evcs = MagicMock()
326 1
        await int_manager.redeploy_int(evcs)
327
328 1
        assert stored_flows_mock.call_count == 1
329 1
        assert int_manager._remove_int_flows.call_count == 1
330 1
        assert api_mock.get_stored_flows.call_count == 1
331 1
        assert int_manager._install_int_flows.call_count == 1
332
333 1
    def test_validate_intra_evc_different_proxy_ports(self) -> None:
334
        """Test _validate_intra_evc_different_proxy_ports."""
335 1
        pp_a, pp_z, controller = MagicMock(), MagicMock(), MagicMock()
336 1
        evc = {
337
            "id": "some_id",
338
            "uni_a": {"proxy_port": pp_a, "interface_id": "00:00:00:00:00:00:00:01:1"},
339
            "uni_z": {"proxy_port": pp_z, "interface_id": "00:00:00:00:00:00:00:01:2"},
340
        }
341
342 1
        int_manager = INTManager(controller)
343 1
        int_manager._validate_intra_evc_different_proxy_ports(evc)
344
345 1
        source = MagicMock()
346 1
        pp_a.source, pp_z.source = source, source
347 1
        with pytest.raises(ProxyPortSameSourceIntraEVC):
348
            int_manager._validate_intra_evc_different_proxy_ports(evc)
349