Test Failed
Pull Request — master (#129)
by Vinicius
09:59 queued 04:16
created

build.tests.integration.test_main   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 292
Duplicated Lines 24.32 %

Importance

Changes 0
Metric Value
eloc 232
dl 71
loc 292
rs 10
c 0
b 0
f 0
wmc 16

7 Functions

Rating   Name   Duplication   Size   Complexity  
A test_main.TestMain.test_get_event_listeners() 0 26 1
A test_main.TestMain.test_handle_interface_deleted() 0 11 1
A test_main.TestMain.test_get_interfaces() 0 17 2
A test_main.TestMain.test_handle_connection_lost() 0 10 1
A test_main.TestMain.test_handle_new_switch() 0 10 1
A test_main.TestMain.setup_method() 0 13 2
A test_main.TestMain.test_get_switches_dict() 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
"""Module to test the main napp file."""
2
from unittest.mock import Mock, patch, MagicMock
3
4
# pylint: disable=import-error,no-name-in-module
5
from kytos.core.events import KytosEvent
6
from kytos.core.buffers import KytosBuffers
7
from kytos.lib.helpers import get_controller_mock, get_test_client
8
from tests.integration.helpers import (get_interface_mock, get_switch_mock)
9
10
11
# pylint: disable=import-outside-toplevel
12
class TestMain:
13
    """Test the Main class."""
14
15
    def setup_method(self):
16
        """Execute steps before each tests.
17
18
        Set the server_name_url from kytos/topology
19
        """
20
        patch('kytos.core.helpers.run_on_thread', lambda x: x).start()
21
        from napps.kytos.topology.main import Main
22
        Main.get_topo_controller = MagicMock()
23
        controller = get_controller_mock()
24
        self.napp = Main(controller)
25
        _ = self.napp.controller.buffers.app.get()
26
        self.api_client = get_test_client(controller, self.napp)
27
        self.base_endpoint = "kytos/topology/v3"
28
29
    def test_get_switches_dict(self):
30
        """Basic test for switch listing."""
31
        # pylint: disable=protected-access,
32
        # pylint: disable=use-implicit-booleaness-not-comparison
33
        switches = self.napp._get_switches_dict()
34
        assert isinstance(switches['switches'], dict)
35
        assert switches['switches'] == {}
36
37
    def test_get_event_listeners(self):
38
        """Verify all event listeners registered."""
39
        actual_events = self.napp.listeners()
40
        expected_events = ['kytos/core.shutdown',
41
                           'kytos/core.shutdown.kytos/topology',
42
                           'kytos/maintenance.start_link',
43
                           'kytos/maintenance.end_link',
44
                           'kytos/maintenance.start_switch',
45
                           'kytos/maintenance.end_switch',
46
                           'kytos/.*.link_available_tags',
47
                           'kytos/.*.liveness.(up|down)',
48
                           'kytos/.*.liveness.disabled',
49
                           'kytos/topology.get',
50
                           '.*.topo_controller.upsert_switch',
51
                           '.*.of_lldp.network_status.updated',
52
                           '.*.interface.is.nni',
53
                           '.*.connection.lost',
54
                           '.*.switch.interfaces.created',
55
                           '.*.topology.switch.interface.created',
56
                           '.*.switch.interface.deleted',
57
                           '.*.switch.interface.link_down',
58
                           '.*.switch.interface.link_up',
59
                           '.*.switch.(new|reconnected)',
60
                           '.*.switch.port.created',
61
                           'kytos/topology.notify_link_up_if_status']
62
        assert sorted(expected_events) == sorted(actual_events)
63
64
    async def test_get_interfaces(self):
65
        """test_get_interfaces."""
66
        dpid = "00:00:00:00:00:00:00:01"
67
        switch = get_switch_mock(0x04, dpid=dpid)
68
        switch.dpid = dpid
69
        switch.metadata = {"lat": 0, "lng": 0}
70
        switch.interfaces = {f"{dpid}:1": f"{dpid}:1"}
71
        switch.as_dict = lambda: {"dpid": dpid, "metadata": switch.metadata,
72
                                  "interfaces": switch.interfaces}
73
        self.napp.controller.switches = {dpid: switch}
74
        endpoint = f"{self.base_endpoint}/interfaces"
75
        response = await self.api_client.get(endpoint)
76
        assert response.status_code == 200
77
        data = response.json()
78
        assert "interfaces" in data
79
        assert len(data["interfaces"]) == 1
80
        assert data["interfaces"][f"{dpid}:1"]
81
82
    async def test_handle_new_switch(self):
83
        """Test handle new switch."""
84
        self.napp.controller._buffers = KytosBuffers()
85
        event_name = '.*.switch.(new|reconnected)'
86
        switch = get_switch_mock(0x04)
87
        event = KytosEvent(name=event_name,
88
                           content={'switch': switch})
89
        self.napp.handle_new_switch(event)
90
        event_response = self.napp.controller.buffers.app.get()
91
        assert event_response.name == 'kytos/topology.updated'
92
93
    async def test_handle_interface_deleted(self):
94
        """Test handle interface deleted."""
95
        self.napp.controller._buffers = KytosBuffers()
96
        event_name = '.*.switch.interface.deleted'
97
        interface = get_interface_mock("interface1", 7)
98
        interface.switch.dpid = "00:00:00:00:00:00:00:01"
99
        stats_event = KytosEvent(name=event_name,
100
                                 content={'interface': interface})
101
        self.napp.handle_interface_deleted(stats_event)
102
        event_updated_response = self.napp.controller.buffers.app.get()
103
        assert event_updated_response.name == 'kytos/topology.updated'
104
105
    async def test_handle_connection_lost(self):
106
        """Test handle connection lost."""
107
        self.napp.controller._buffers = KytosBuffers()
108
        event_name = '.*.connection.lost'
109
        source = Mock()
110
        stats_event = KytosEvent(name=event_name,
111
                                 content={'source': source})
112
        self.napp.handle_connection_lost(stats_event)
113
        event_updated_response = self.napp.controller.buffers.app.get()
114
        assert event_updated_response.name == 'kytos/topology.updated'
115