Test Failed
Pull Request — master (#84)
by Humberto
02:32
created

TestMain.test_get_switches_dict()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
"""Module to test the main napp file."""
2
from unittest import TestCase
3
from unittest.mock import Mock
4
5
from kytos.core import Controller
6
from kytos.core.config import KytosConfig
7
8
from napps.kytos.topology.main import Main
9
10
11
class TestMain(TestCase):
12
    """Test the Main class."""
13
14
    def setUp(self):
15
        """Execute steps before each tests.
16
17
        Set the server_name_url_url from kytos/topology
18
        """
19
        self.server_name_url = 'http://localhost:8181/api/kytos/topology'
20
        self.napp = Main(self.get_controller_mock())
21
22
    def test_get_switches_dict(self):
23
        """Basic test for switch listing."""
24
        switches = self.napp._get_switches_dict()
25
        assert isinstance(switches['switches'], dict)
26
        assert switches['switches'] == {}
27
28
    def test_get_event_listeners(self):
29
        """Verify all event listeners registered."""
30
        expected_events = ['kytos/core.shutdown',
31
                           'kytos/core.shutdown.kytos/topology',
32
                           '.*.interface.is.nni',
33
                           '.*.connection.lost',
34
                           '.*.switch.interface.created',
35
                           '.*.switch.interface.deleted',
36
                           '.*.switch.interface.link_down',
37
                           '.*.switch.interface.link_up',
38
                           '.*.switch.(new|reconnected)',
39
                           '.*.switch.port.created',
40
                           'kytos/topology.*.metadata.*']
41
        actual_events = self.napp.listeners()
42
        self.assertEqual(expected_events, actual_events)
43
44
    def test_verify_api_urls(self):
45
        """Verify all APIs registered."""
46
        expected_urls = [
47
         ({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/interfaces'),
48
         ({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/switches'),
49
         ({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/links'),
50
         ({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/'),
51
         ({'key': '[key]', 'interface_id': '[interface_id]'},
52
          {'OPTIONS', 'DELETE'},
53
          '/api/kytos/topology/v3/interfaces/<interface_id>/metadata/<key>'),
54
         ({'interface_id': '[interface_id]'}, {'POST', 'OPTIONS'},
55
          '/api/kytos/topology/v3/interfaces/<interface_id>/metadata'),
56
         ({'interface_id': '[interface_id]'}, {'GET', 'OPTIONS', 'HEAD'},
57
          '/api/kytos/topology/v3/interfaces/<interface_id>/metadata'),
58
         ({'interface_id': '[interface_id]'}, {'POST', 'OPTIONS'},
59
          '/api/kytos/topology/v3/interfaces/<interface_id>/disable'),
60
         ({'interface_id': '[interface_id]'}, {'POST', 'OPTIONS'},
61
          '/api/kytos/topology/v3/interfaces/<interface_id>/enable'),
62
         ({'dpid': '[dpid]', 'key': '[key]'}, {'OPTIONS', 'DELETE'},
63
          '/api/kytos/topology/v3/switches/<dpid>/metadata/<key>'),
64
         ({'dpid': '[dpid]'}, {'POST', 'OPTIONS'},
65
          '/api/kytos/topology/v3/switches/<dpid>/metadata'),
66
         ({'dpid': '[dpid]'}, {'GET', 'OPTIONS', 'HEAD'},
67
          '/api/kytos/topology/v3/switches/<dpid>/metadata'),
68
         ({'dpid': '[dpid]'}, {'POST', 'OPTIONS'},
69
          '/api/kytos/topology/v3/switches/<dpid>/disable'),
70
         ({'dpid': '[dpid]'}, {'POST', 'OPTIONS'},
71
          '/api/kytos/topology/v3/switches/<dpid>/enable'),
72
         ({'link_id': '[link_id]', 'key': '[key]'}, {'OPTIONS', 'DELETE'},
73
          '/api/kytos/topology/v3/links/<link_id>/metadata/<key>'),
74
         ({'link_id': '[link_id]'}, {'POST', 'OPTIONS'},
75
          '/api/kytos/topology/v3/links/<link_id>/metadata'),
76
         ({'link_id': '[link_id]'}, {'GET', 'OPTIONS', 'HEAD'},
77
          '/api/kytos/topology/v3/links/<link_id>/metadata'),
78
         ({'link_id': '[link_id]'}, {'POST', 'OPTIONS'},
79
          '/api/kytos/topology/v3/links/<link_id>/disable'),
80
         ({'link_id': '[link_id]'}, {'POST', 'OPTIONS'},
81
          '/api/kytos/topology/v3/links/<link_id>/enable')]
82
83
        urls = self.get_napp_urls(self.napp)
84
        self.assertEqual(expected_urls, urls)
85
86
    @staticmethod
87
    def get_controller_mock():
88
        """Return a controller mock."""
89
        options = KytosConfig().options['daemon']
90
        controller = Controller(options)
91
        controller.log = Mock()
92
        return controller
93
94
    @staticmethod
95
    def get_napp_urls(napp):
96
        """Return the kytos/topology urls.
97
98
        The urls will be like:
99
100
        urls = [
101
            (options, methods, url)
102
        ]
103
104
        """
105
        controller = napp.controller
106
        controller.api_server.register_napp_endpoints(napp)
107
108
        urls = []
109
        for rule in controller.api_server.app.url_map.iter_rules():
110
            options = {}
111
            for arg in rule.arguments:
112
                options[arg] = "[{0}]".format(arg)
113
114
            if f'{napp.username}/{napp.name}' in str(rule):
115
                urls.append((options, rule.methods, f'{str(rule)}'))
116
117
        return urls
118
119
    @staticmethod
120
    def get_app_test_client(napp):
121
        """Return a flask api test client."""
122
        napp.controller.api_server.register_napp_endpoints(napp)
123
        return napp.controller.api_server.app.test_client()
124