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

test_main.TestMain.test_redeploy_telemetry()   A

Complexity

Conditions 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 21
Ratio 100 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 15
nop 2
dl 21
loc 21
ccs 12
cts 12
cp 1
crap 1
rs 9.65
c 0
b 0
f 0
1
"""Test Main methods."""
2 1
import pytest
3
4 1
from unittest.mock import AsyncMock, MagicMock, patch
5 1
from napps.kytos.telemetry_int.main import Main
6 1
from napps.kytos.telemetry_int import utils
7 1
from kytos.lib.helpers import get_controller_mock, get_test_client
8 1
from kytos.core.events import KytosEvent
9
10
11 1
class TestMain:
12
    """Tests for the Main class."""
13
14 1
    def setup_method(self):
15
        """Setup."""
16 1
        patch("kytos.core.helpers.run_on_thread", lambda x: x).start()
17
        # pylint: disable=import-outside-toplevel
18 1
        controller = get_controller_mock()
19 1
        self.napp = Main(controller)
20 1
        self.api_client = get_test_client(controller, self.napp)
21 1
        self.base_endpoint = "kytos/telemetry_int/v1"
22
23 1 View Code Duplication
    async def test_enable_telemetry(self, monkeypatch) -> None:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
24
        """Test enable telemetry."""
25 1
        api_mock, flow = AsyncMock(), MagicMock()
26 1
        flow.cookie = 0xA800000000000001
27 1
        monkeypatch.setattr(
28
            "napps.kytos.telemetry_int.main.api",
29
            api_mock,
30
        )
31
32 1
        evc_id = utils.get_id_from_cookie(flow.cookie)
33 1
        api_mock.get_evcs.return_value = {
34
            evc_id: {"metadata": {"telemetry": {"enabled": False}}}
35
        }
36
37 1
        self.napp.int_manager = AsyncMock()
38
39 1
        endpoint = f"{self.base_endpoint}/evc/enable"
40 1
        response = await self.api_client.post(endpoint, json={"evc_ids": [evc_id]})
41 1
        assert self.napp.int_manager.enable_int.call_count == 1
42 1
        assert self.napp.int_manager._remove_int_flows.call_count == 1
43 1
        assert response.status_code == 201
44 1
        assert response.json() == [evc_id]
45
46 1 View Code Duplication
    async def test_redeploy_telemetry(self, monkeypatch) -> None:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
47
        """Test redeploy telemetry."""
48 1
        api_mock, flow = AsyncMock(), MagicMock()
49 1
        flow.cookie = 0xA800000000000001
50 1
        monkeypatch.setattr(
51
            "napps.kytos.telemetry_int.main.api",
52
            api_mock,
53
        )
54
55 1
        evc_id = utils.get_id_from_cookie(flow.cookie)
56 1
        api_mock.get_evcs.return_value = {
57
            evc_id: {"metadata": {"telemetry": {"enabled": False}}}
58
        }
59
60 1
        self.napp.int_manager = AsyncMock()
61
62 1
        endpoint = f"{self.base_endpoint}/evc/redeploy"
63 1
        response = await self.api_client.patch(endpoint, json={"evc_ids": [evc_id]})
64 1
        assert self.napp.int_manager.redeploy_int.call_count == 1
65 1
        assert response.status_code == 201
66 1
        assert response.json() == [evc_id]
67
68 1
    @pytest.mark.parametrize("route", ["/evc/enable", "/evc/disable"])
69 1
    async def test_en_dis_openapi_validation(self, route: str) -> None:
70
        """Test OpenAPI enable/disable basic validation."""
71 1
        endpoint = f"{self.base_endpoint}{route}"
72
        # wrong evc_ids payload data type
73 1
        response = await self.api_client.post(endpoint, json={"evc_ids": 1})
74 1
        assert response.status_code == 400
75 1
        assert "evc_ids" in response.json()["description"]
76
77 1 View Code Duplication
    async def test_disable_telemetry(self, monkeypatch) -> None:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
78
        """Test disable telemetry."""
79 1
        api_mock, flow = AsyncMock(), MagicMock()
80 1
        flow.cookie = 0xA800000000000001
81 1
        monkeypatch.setattr(
82
            "napps.kytos.telemetry_int.main.api",
83
            api_mock,
84
        )
85
86 1
        evc_id = utils.get_id_from_cookie(flow.cookie)
87 1
        api_mock.get_evcs.return_value = {
88
            evc_id: {"metadata": {"telemetry": {"enabled": True}}}
89
        }
90
91 1
        self.napp.int_manager = AsyncMock()
92
93 1
        endpoint = f"{self.base_endpoint}/evc/disable"
94 1
        response = await self.api_client.post(endpoint, json={"evc_ids": [evc_id]})
95 1
        assert self.napp.int_manager.disable_int.call_count == 1
96 1
        assert response.status_code == 200
97 1
        assert response.json() == [evc_id]
98
99 1
    async def test_get_enabled_evcs(self, monkeypatch) -> None:
100
        """Test get enabled evcs."""
101 1
        api_mock, flow = AsyncMock(), MagicMock()
102 1
        flow.cookie = 0xA800000000000001
103 1
        monkeypatch.setattr(
104
            "napps.kytos.telemetry_int.main.api",
105
            api_mock,
106
        )
107
108 1
        evc_id = utils.get_id_from_cookie(flow.cookie)
109 1
        api_mock.get_evcs.return_value = {
110
            evc_id: {"metadata": {"telemetry": {"enabled": True}}},
111
        }
112
113 1
        endpoint = f"{self.base_endpoint}/evc"
114 1
        response = await self.api_client.get(endpoint)
115 1
        assert api_mock.get_evcs.call_args[1] == {"metadata.telemetry.enabled": "true"}
116 1
        assert response.status_code == 200
117 1
        data = response.json()
118 1
        assert len(data) == 1
119 1
        assert evc_id in data
120
121 1
    async def test_on_table_enabled(self) -> None:
122
        """Test on_table_enabled."""
123 1
        assert self.napp.int_manager.flow_builder.table_group == {"evpl": 2, "epl": 3}
124 1
        await self.napp.on_table_enabled(
125
            KytosEvent(content={"telemetry_int": {"evpl": 22, "epl": 33}})
126
        )
127 1
        assert self.napp.int_manager.flow_builder.table_group == {"evpl": 22, "epl": 33}
128 1
        assert self.napp.controller.buffers.app.aput.call_count == 1
129
130 1
    async def test_on_table_enabled_error(self, monkeypatch) -> None:
131
        """Test on_table_enabled error case."""
132 1
        assert self.napp.int_manager.flow_builder.table_group == {"evpl": 2, "epl": 3}
133 1
        log_mock = MagicMock()
134 1
        monkeypatch.setattr("napps.kytos.telemetry_int.main.log", log_mock)
135 1
        await self.napp.on_table_enabled(
136
            KytosEvent(content={"telemetry_int": {"invalid": 1}})
137
        )
138 1
        assert self.napp.int_manager.flow_builder.table_group == {"evpl": 2, "epl": 3}
139 1
        assert log_mock.error.call_count == 1
140 1
        assert not self.napp.controller.buffers.app.aput.call_count
141
142 1
    async def test_on_flow_mod_error(self, monkeypatch) -> None:
143
        """Test on_flow_mod_error."""
144 1
        api_mock_main, api_mock_int, flow = AsyncMock(), AsyncMock(), MagicMock()
145 1
        flow.cookie = 0xA800000000000001
146 1
        monkeypatch.setattr(
147
            "napps.kytos.telemetry_int.main.api",
148
            api_mock_main,
149
        )
150 1
        monkeypatch.setattr(
151
            "napps.kytos.telemetry_int.managers.int.api",
152
            api_mock_int,
153
        )
154 1
        cookie = utils.get_id_from_cookie(flow.cookie)
155 1
        api_mock_main.get_evc.return_value = {
156
            cookie: {"metadata": {"telemetry": {"enabled": True}}}
157
        }
158 1
        api_mock_int.get_stored_flows.return_value = {cookie: [MagicMock()]}
159 1
        self.napp.int_manager._remove_int_flows = AsyncMock()
160
161 1
        event = KytosEvent(content={"flow": flow, "error_command": "add"})
162 1
        await self.napp.on_flow_mod_error(event)
163
164 1
        assert api_mock_main.get_evc.call_count == 1
165 1
        assert api_mock_int.get_stored_flows.call_count == 1
166 1
        assert api_mock_int.add_evcs_metadata.call_count == 1
167 1
        assert self.napp.int_manager._remove_int_flows.call_count == 1
168
169 1
    async def test_on_mef_eline_evcs_loaded(self):
170
        """Test on_mef_eline_evcs_loaded."""
171 1
        evcs = {"1": {}, "2": {}}
172 1
        event = KytosEvent(content=evcs)
173 1
        self.napp.int_manager = MagicMock()
174 1
        await self.napp.on_mef_eline_evcs_loaded(event)
175 1
        self.napp.int_manager.load_uni_src_proxy_ports.assert_called_with(evcs)
176
177 1
    async def test_on_intf_metadata_remove(self):
178
        """Test on_intf_metadata_removed."""
179 1
        intf = MagicMock()
180 1
        event = KytosEvent(content={"interface": intf})
181 1
        self.napp.int_manager = MagicMock()
182 1
        await self.napp.on_intf_metadata_removed(event)
183
        self.napp.int_manager.handle_pp_metadata_removed.assert_called_with(intf)
184