Passed
Pull Request — master (#62)
by Gleyberson
04:44 queued 02:35
created

build.tests.test_main.TestMain.setUp()   A

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
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
12
from kytos.core import Controller
13
from kytos.core.config import KytosConfig
14
from kytos.core.connection import Connection, ConnectionState
15
from kytos.core.events import KytosEvent
16
from kytos.core.switch import Switch
17
from napps.kytos.of_core.main import Main
18
19
20
class TestMain(TestCase):
21
    """docstring for TestMain."""
22
23
    def setUp(self):
24
        """Execute steps before each tests.
25
26
        Set the server_name_url from kytos/of_core
27
        """
28
        self.server_name_url = 'http://localhost:8181/api/kytos/of_core'
29
        self.controller = self._get_controller_mock()
30
        self.napp = Main(self.controller)
31
        self.patched_events = []
32
33
    @staticmethod
34
    def _get_controller_mock():
35
        """Return a controller mock."""
36
        options = KytosConfig().options['daemon']
37
        controller = Controller(options)
38
        controller.log = Mock()
39
        return controller
40
41
    @staticmethod
42
    def _get_switch_mock():
43
        """Return a switch mock."""
44
        switch = Switch('dpid')
45
        address = Mock()
46
        port = Mock()
47
        socket = Mock()
48
        switch.connection = Connection(address, port, socket)
49
        switch.connection.protocol.unpack = unpack
50
        return switch
51
52
    def test_get_event_listeners(self):
53
        """Verify all event listeners registered."""
54
        expected_events = [
55
            'kytos/of_core.v0x01.messages.in.ofpt_stats_reply',
56
            'kytos/of_core.v0x0[14].messages.in.ofpt_features_reply',
57
            'kytos/of_core.v0x04.messages.in.ofpt_multipart_reply',
58
            'kytos/core.openflow.raw.in',
59
            'kytos/of_core.v0x0[14].messages.in.ofpt_echo_request',
60
            'kytos/of_core.v0x0[14].messages.out.ofpt_echo_reply',
61
            'kytos/of_core.v0x[0-9a-f]{2}.messages.in.hello_failed',
62
            'kytos/of_core.v0x0[14].messages.out.hello_failed',
63
        ]
64
65
        actual_events = self.napp.listeners()
66
        for _event in expected_events:
67
            self.assertIn(_event, actual_events, '%s' % _event)
68
69
    def test_handle_stats_reply(self):
70
        """Test handling stats reply message."""
71
        event_name = 'kytos/of_core.v0x01.messages.in.ofpt_stats_reply'
72
        switch = self._get_switch_mock()
73
        switch.connection = Mock()
74
        switch.connection.protocol.version = 0x01
75
76
        stats_data = b'\x01\x11\x00\x0c\x00\x00\x00\x01\x00\x01\x00\x01'
77
        stats_reply = StatsReply()
78
        stats_reply.unpack(stats_data[8:])
79
        stats_event = KytosEvent(name=event_name,
80
                                 content={'source': switch.connection,
81
                                          'message': stats_reply})
82
        self.napp.handle_stats_reply(stats_event)
83
84
        desc_stats_data = b'\x01\x11\x00\x0c\x00\x00\x00\x0e\x00\x00\x00\x00'
85
        desc_stats_reply = StatsReply()
86
        desc_stats_reply.unpack(desc_stats_data[8:])
87
        desc_stats_event = KytosEvent(name=event_name,
88
                                      content={'source': switch.connection,
89
                                               'message': desc_stats_reply})
90
        self.napp.handle_stats_reply(desc_stats_event)
91
92
    def test_handle_features_reply(self):
93
        """Test handling features reply message."""
94
        event_name = 'kytos/of_core.v0x0[14].messages.in.ofpt_features_reply'
95
        switch = self._get_switch_mock()
96
        switch.connection.protocol.version = 0x04
97
        switch.connection.protocol.state = 'waiting_features_reply'
98
        switch.connection.state = ConnectionState.SETUP
99
100
        message = FeaturesReply(xid=3,
101
                                datapath_id=DPID('00:00:00:00:00:00:00:01'),
102
                                n_buffers=0, n_tables=254, auxiliary_id=0,
103
                                capabilities=0x0000004f, reserved=0x00000000)
104
105
        event = KytosEvent(name=event_name,
106
                           content={'source': switch.connection,
107
                                    'message': message})
108
        self.napp.handle_features_reply(event)
109
110
    def test_handle_features_request_sent(self):
111
        """Test handling features request sent message."""
112
        event_name = 'kytos/of_core.v0x01.messages.out.ofpt_features_request'
113
        switch = self._get_switch_mock()
114
        switch.connection.protocol.version = 0x01
115
        switch.connection.protocol.state = 'sending_features'
116
117
        data = b'\x04\x05\x00\x08\x00\x00\x00\x03'
118
        features_request = FeaturesRequest()
119
        features_request.unpack(data)
120
        stats_event = KytosEvent(name=event_name,
121
                                 content={'destination': switch.connection})
122
        self.napp.handle_features_request_sent(stats_event)
123
124
    def test_handle_hello_raw_in(self):
125
        """Test handling hello raw in message."""
126
        event_name = 'kytos/core.openflow.raw.in'
127
        data = b'\x04\x00\x00\x10\x00\x00\x00\x3e'
128
        data += b'\x00\x01\x00\x08\x00\x00\x00\x10'
129
        switch = self._get_switch_mock()
130
        switch.connection.protocol.version = 0x04
131
        switch.connection.state = ConnectionState.NEW
132
        stats_event = KytosEvent(name=event_name,
133
                                 content={'source': switch.connection,
134
                                          'new_data': data})
135
        self.napp.handle_raw_in(stats_event)
136
137
    def test_handle_port_status_raw_in(self):
138
        """Test handling port_status raw in message."""
139
        event_name = 'kytos/core.openflow.raw.in'
140
        data = b'\x04\x0c\x00\x50\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00'
141
        data += b'\x00\x00\x00\x00\x01\x00\x00\x00\x00\x62\x43\xe5\xdb\x35\x0a'
142
        data += b'\x00\x00\x73\x31\x2d\x65\x74\x68\x31\x00\x00\x00\x00\x00\x00'
143
        data += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x08\x40'
144
        data += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x98\x96'
145
        data += b'\x80\x00\x00\x00\x00'
146
        switch = self._get_switch_mock()
147
        switch.connection.protocol.version = 0x04
148
        switch.connection.switch = self._get_switch_mock()
149
        switch.connection.state = ConnectionState.ESTABLISHED
150
        stats_event = KytosEvent(name=event_name,
151
                                 content={'source': switch.connection,
152
                                          'new_data': data})
153
        self.napp.handle_raw_in(stats_event)
154