Test Failed
Pull Request — master (#62)
by Gleyberson
02:10
created

build.tests.test_main   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 137
dl 0
loc 184
rs 10
c 0
b 0
f 0
wmc 11

10 Methods

Rating   Name   Duplication   Size   Complexity  
A TestMain.test_handle_features_reply() 0 17 1
A TestMain.test_handle_port_status_raw_in() 0 19 1
A TestMain.test_handle_multipart_reply() 0 21 1
A TestMain.test_handle_features_request_sent() 0 14 1
A TestMain.test_handle_stats_reply() 0 22 1
A TestMain.test_handle_hello_raw_in() 0 14 1
A TestMain._get_switch_mock() 0 10 1
A TestMain.test_get_event_listeners() 0 16 2
A TestMain.setUp() 0 9 1
A TestMain._get_controller_mock() 0 7 1
1
"""Test main."""
2
3
from unittest import TestCase
4
from unittest.mock import Mock
5
6
from pyof.foundation.basic_types import DPID
7
from pyof.utils import unpack
8
from pyof.v0x01.controller2switch.stats_reply import StatsReply
9
from pyof.v0x04.controller2switch.features_reply import FeaturesReply
10
from pyof.v0x04.controller2switch.features_request import FeaturesRequest
11
from pyof.v0x04.controller2switch.multipart_reply import MultipartReply
12
13
from kytos.core import Controller
14
from kytos.core.config import KytosConfig
15
from kytos.core.connection import Connection, ConnectionState
16
from kytos.core.events import KytosEvent
17
from kytos.core.switch import Switch
18
from napps.kytos.of_core.main import Main
19
20
21
class TestMain(TestCase):
22
    """docstring for TestMain."""
23
24
    def setUp(self):
25
        """Execute steps before each tests.
26
27
        Set the server_name_url from kytos/of_core
28
        """
29
        self.server_name_url = 'http://localhost:8181/api/kytos/of_core'
30
        self.controller = self._get_controller_mock()
31
        self.napp = Main(self.controller)
32
        self.patched_events = []
33
34
    @staticmethod
35
    def _get_controller_mock():
36
        """Return a controller mock."""
37
        options = KytosConfig().options['daemon']
38
        controller = Controller(options)
39
        controller.log = Mock()
40
        return controller
41
42
    @staticmethod
43
    def _get_switch_mock():
44
        """Return a switch mock."""
45
        switch = Switch('dpid')
46
        address = Mock()
47
        port = Mock()
48
        socket = Mock()
49
        switch.connection = Connection(address, port, socket)
50
        switch.connection.protocol.unpack = unpack
51
        return switch
52
53
    def test_get_event_listeners(self):
54
        """Verify all event listeners registered."""
55
        expected_events = [
56
            'kytos/of_core.v0x01.messages.in.ofpt_stats_reply',
57
            'kytos/of_core.v0x0[14].messages.in.ofpt_features_reply',
58
            'kytos/of_core.v0x04.messages.in.ofpt_multipart_reply',
59
            'kytos/core.openflow.raw.in',
60
            'kytos/of_core.v0x0[14].messages.in.ofpt_echo_request',
61
            'kytos/of_core.v0x0[14].messages.out.ofpt_echo_reply',
62
            'kytos/of_core.v0x[0-9a-f]{2}.messages.in.hello_failed',
63
            'kytos/of_core.v0x0[14].messages.out.hello_failed',
64
        ]
65
66
        actual_events = self.napp.listeners()
67
        for _event in expected_events:
68
            self.assertIn(_event, actual_events, '%s' % _event)
69
70
    def test_handle_stats_reply(self):
71
        """Test handling stats reply message."""
72
        event_name = 'kytos/of_core.v0x01.messages.in.ofpt_stats_reply'
73
        switch = self._get_switch_mock()
74
        switch.connection = Mock()
75
        switch.connection.protocol.version = 0x01
76
77
        stats_data = b'\x01\x11\x00\x0c\x00\x00\x00\x01\x00\x01\x00\x01'
78
        stats_reply = StatsReply()
79
        stats_reply.unpack(stats_data[8:])
80
        stats_event = KytosEvent(name=event_name,
81
                                 content={'source': switch.connection,
82
                                          'message': stats_reply})
83
        self.napp.handle_stats_reply(stats_event)
84
85
        desc_stats_data = b'\x01\x11\x00\x0c\x00\x00\x00\x0e\x00\x00\x00\x00'
86
        desc_stats_reply = StatsReply()
87
        desc_stats_reply.unpack(desc_stats_data[8:])
88
        desc_stats_event = KytosEvent(name=event_name,
89
                                      content={'source': switch.connection,
90
                                               'message': desc_stats_reply})
91
        self.napp.handle_stats_reply(desc_stats_event)
92
93
    def test_handle_features_reply(self):
94
        """Test handling features reply message."""
95
        event_name = 'kytos/of_core.v0x0[14].messages.in.ofpt_features_reply'
96
        switch = self._get_switch_mock()
97
        switch.connection.protocol.version = 0x04
98
        switch.connection.protocol.state = 'waiting_features_reply'
99
        switch.connection.state = ConnectionState.SETUP
100
101
        message = FeaturesReply(xid=3,
102
                                datapath_id=DPID('00:00:00:00:00:00:00:01'),
103
                                n_buffers=0, n_tables=254, auxiliary_id=0,
104
                                capabilities=0x0000004f, reserved=0x00000000)
105
106
        event = KytosEvent(name=event_name,
107
                           content={'source': switch.connection,
108
                                    'message': message})
109
        self.napp.handle_features_reply(event)
110
111
    def test_handle_features_request_sent(self):
112
        """Test handling features request sent message."""
113
        event_name = 'kytos/of_core.v0x01.messages.out.ofpt_features_request'
114
        switch = self._get_switch_mock()
115
        switch.connection.protocol.version = 0x01
116
        switch.connection.protocol.state = 'sending_features'
117
118
        data = b'\x04\x05\x00\x08\x00\x00\x00\x03'
119
        features_request = FeaturesRequest()
120
        features_request.unpack(data)
121
122
        stats_event = KytosEvent(name=event_name,
123
                                 content={'destination': switch.connection})
124
        self.napp.handle_features_request_sent(stats_event)
125
126
    def test_handle_hello_raw_in(self):
127
        """Test handling hello raw in message."""
128
        event_name = 'kytos/core.openflow.raw.in'
129
        switch = self._get_switch_mock()
130
        switch.connection.protocol.version = 0x04
131
        switch.connection.state = ConnectionState.NEW
132
133
        data = b'\x04\x00\x00\x10\x00\x00\x00\x3e'
134
        data += b'\x00\x01\x00\x08\x00\x00\x00\x10'
135
136
        stats_event = KytosEvent(name=event_name,
137
                                 content={'source': switch.connection,
138
                                          'new_data': data})
139
        self.napp.handle_raw_in(stats_event)
140
141
    def test_handle_port_status_raw_in(self):
142
        """Test handling port_status raw in message."""
143
        event_name = 'kytos/core.openflow.raw.in'
144
        switch = self._get_switch_mock()
145
        switch.connection.protocol.version = 0x04
146
        switch.connection.switch = self._get_switch_mock()
147
        switch.connection.state = ConnectionState.ESTABLISHED
148
149
        data = b'\x04\x0c\x00\x50\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00'
150
        data += b'\x00\x00\x00\x00\x01\x00\x00\x00\x00\x62\x43\xe5\xdb\x35\x0a'
151
        data += b'\x00\x00\x73\x31\x2d\x65\x74\x68\x31\x00\x00\x00\x00\x00\x00'
152
        data += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x08\x40'
153
        data += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x98\x96'
154
        data += b'\x80\x00\x00\x00\x00'
155
156
        stats_event = KytosEvent(name=event_name,
157
                                 content={'source': switch.connection,
158
                                          'new_data': data})
159
        self.napp.handle_raw_in(stats_event)
160
161
162
163
    def test_handle_multipart_reply(self):
164
        """Test handling ofpt_multipart_reply."""
165
166
        event_name = 'kytos/of_core.v0x04.messages.in.ofpt_multipart_reply'
167
        switch = self._get_switch_mock()
168
        switch.connection.switch = self._get_switch_mock()
169
        data = b'\x04\x13\x00\x68\xac\xc8\xdf\x58\x00\x01\x00\x00\x00\x00\x00\x00'
170
        data += b'\x00\x58\x00\x00\x00\x00\x00\x38\x25\xd9\x54\xc0\x03\xe8\x00\x00'
171
        data += b'\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
172
        data += b'\x00\x00\x00\x00\x00\x00\x00\x12\x00\x00\x00\x00\x00\x00\x02\xf4'
173
        data += b'\x00\x01\x00\x10\x80\x00\x0a\x02\x88\xcc\x80\x00\x0c\x02\x1e\xd7'
174
        data += b'\x00\x04\x00\x18\x00\x00\x00\x00\x00\x00\x00\x10\xff\xff\xff\xfd'
175
        data += b'\xff\xff\x00\x00\x00\x00\x00\x00'
176
177
        multipart_reply = MultipartReply()
178
        multipart_reply.unpack(data[8:])
179
        stats_event = KytosEvent(name=event_name,
180
                                 content={'source': switch.connection,
181
                                          'message': multipart_reply})
182
183
        self.napp.handle_multipart_reply(stats_event)
184