Passed
Push — master ( 4b7f5d...442faf )
by Vinicius
02:07 queued 31s
created

test_main   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 161
Duplicated Lines 26.09 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 113
dl 42
loc 161
ccs 96
cts 96
cp 1
rs 10
c 0
b 0
f 0
wmc 11

10 Methods

Rating   Name   Duplication   Size   Complexity  
A TestMain.test_enable_telemetry() 21 21 1
A TestMain.setup_method() 0 8 2
A TestMain.test_en_dis_openapi_validation() 0 8 1
A TestMain.test_disable_telemetry() 21 21 1
A TestMain.test_get_enabled_evcs() 0 21 1
A TestMain.test_on_table_enabled_error() 0 11 1
A TestMain.test_on_table_enabled() 0 8 1
A TestMain.test_on_flow_mod_error() 0 26 1
A TestMain.test_on_mef_eline_evcs_loaded() 0 7 1
A TestMain.test_on_intf_metadata_remove() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 response.status_code == 201
43 1
        assert response.json() == [evc_id]
44
45 1
    @pytest.mark.parametrize("route", ["/evc/enable", "/evc/disable"])
46 1
    async def test_en_dis_openapi_validation(self, route: str) -> None:
47
        """Test OpenAPI enable/disable basic validation."""
48 1
        endpoint = f"{self.base_endpoint}{route}"
49
        # wrong evc_ids payload data type
50 1
        response = await self.api_client.post(endpoint, json={"evc_ids": 1})
51 1
        assert response.status_code == 400
52 1
        assert "evc_ids" in response.json()["description"]
53
54 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...
55
        """Test disable telemetry."""
56 1
        api_mock, flow = AsyncMock(), MagicMock()
57 1
        flow.cookie = 0xA800000000000001
58 1
        monkeypatch.setattr(
59
            "napps.kytos.telemetry_int.main.api",
60
            api_mock,
61
        )
62
63 1
        evc_id = utils.get_id_from_cookie(flow.cookie)
64 1
        api_mock.get_evcs.return_value = {
65
            evc_id: {"metadata": {"telemetry": {"enabled": True}}}
66
        }
67
68 1
        self.napp.int_manager = AsyncMock()
69
70 1
        endpoint = f"{self.base_endpoint}/evc/disable"
71 1
        response = await self.api_client.post(endpoint, json={"evc_ids": [evc_id]})
72 1
        assert self.napp.int_manager.disable_int.call_count == 1
73 1
        assert response.status_code == 200
74 1
        assert response.json() == [evc_id]
75
76 1
    async def test_get_enabled_evcs(self, monkeypatch) -> None:
77
        """Test get enabled evcs."""
78 1
        api_mock, flow = AsyncMock(), MagicMock()
79 1
        flow.cookie = 0xA800000000000001
80 1
        monkeypatch.setattr(
81
            "napps.kytos.telemetry_int.main.api",
82
            api_mock,
83
        )
84
85 1
        evc_id = utils.get_id_from_cookie(flow.cookie)
86 1
        api_mock.get_evcs.return_value = {
87
            evc_id: {"metadata": {"telemetry": {"enabled": True}}},
88
        }
89
90 1
        endpoint = f"{self.base_endpoint}/evc"
91 1
        response = await self.api_client.get(endpoint)
92 1
        assert api_mock.get_evcs.call_args[1] == {"metadata.telemetry.enabled": "true"}
93 1
        assert response.status_code == 200
94 1
        data = response.json()
95 1
        assert len(data) == 1
96 1
        assert evc_id in data
97
98 1
    async def test_on_table_enabled(self) -> None:
99
        """Test on_table_enabled."""
100 1
        assert self.napp.int_manager.flow_builder.table_group == {"evpl": 2, "epl": 3}
101 1
        await self.napp.on_table_enabled(
102
            KytosEvent(content={"telemetry_int": {"evpl": 22, "epl": 33}})
103
        )
104 1
        assert self.napp.int_manager.flow_builder.table_group == {"evpl": 22, "epl": 33}
105 1
        assert self.napp.controller.buffers.app.aput.call_count == 1
106
107 1
    async def test_on_table_enabled_error(self, monkeypatch) -> None:
108
        """Test on_table_enabled error case."""
109 1
        assert self.napp.int_manager.flow_builder.table_group == {"evpl": 2, "epl": 3}
110 1
        log_mock = MagicMock()
111 1
        monkeypatch.setattr("napps.kytos.telemetry_int.main.log", log_mock)
112 1
        await self.napp.on_table_enabled(
113
            KytosEvent(content={"telemetry_int": {"invalid": 1}})
114
        )
115 1
        assert self.napp.int_manager.flow_builder.table_group == {"evpl": 2, "epl": 3}
116 1
        assert log_mock.error.call_count == 1
117 1
        assert not self.napp.controller.buffers.app.aput.call_count
118
119 1
    async def test_on_flow_mod_error(self, monkeypatch) -> None:
120
        """Test on_flow_mod_error."""
121 1
        api_mock_main, api_mock_int, flow = AsyncMock(), AsyncMock(), MagicMock()
122 1
        flow.cookie = 0xA800000000000001
123 1
        monkeypatch.setattr(
124
            "napps.kytos.telemetry_int.main.api",
125
            api_mock_main,
126
        )
127 1
        monkeypatch.setattr(
128
            "napps.kytos.telemetry_int.managers.int.api",
129
            api_mock_int,
130
        )
131 1
        cookie = utils.get_id_from_cookie(flow.cookie)
132 1
        api_mock_main.get_evc.return_value = {
133
            cookie: {"metadata": {"telemetry": {"enabled": True}}}
134
        }
135 1
        api_mock_int.get_stored_flows.return_value = {cookie: [MagicMock()]}
136 1
        self.napp.int_manager._remove_int_flows = AsyncMock()
137
138 1
        event = KytosEvent(content={"flow": flow, "error_command": "add"})
139 1
        await self.napp.on_flow_mod_error(event)
140
141 1
        assert api_mock_main.get_evc.call_count == 1
142 1
        assert api_mock_int.get_stored_flows.call_count == 1
143 1
        assert api_mock_int.add_evcs_metadata.call_count == 1
144 1
        assert self.napp.int_manager._remove_int_flows.call_count == 1
145
146 1
    async def test_on_mef_eline_evcs_loaded(self):
147
        """Test on_mef_eline_evcs_loaded."""
148 1
        evcs = {"1": {}, "2": {}}
149 1
        event = KytosEvent(content=evcs)
150 1
        self.napp.int_manager = MagicMock()
151 1
        await self.napp.on_mef_eline_evcs_loaded(event)
152 1
        self.napp.int_manager.load_uni_src_proxy_ports.assert_called_with(evcs)
153
154 1
    async def test_on_intf_metadata_remove(self):
155
        """Test on_intf_metadata_removed."""
156 1
        intf = MagicMock()
157 1
        event = KytosEvent(content={"interface": intf})
158 1
        self.napp.int_manager = MagicMock()
159 1
        await self.napp.on_intf_metadata_removed(event)
160
        self.napp.int_manager.handle_pp_metadata_removed.assert_called_with(intf)
161