Passed
Pull Request — master (#63)
by Gleyberson
02:00
created

build.tests.test_main   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 136
dl 0
loc 173
rs 10
c 0
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
B TestMain.test_handle_features_reply() 0 42 1
A TestMain.test_request_flow_list() 0 13 1
A TestMain.test_handle_multipart_reply() 0 34 1
A TestMain.test_handle_stats_reply() 0 23 1
A TestMain.test_handle_multipart_flow_stats() 0 22 1
A TestMain.setUp() 0 17 2
1
"""Test Main methods."""
2
from unittest import TestCase
3
from unittest.mock import MagicMock, patch
4
5
from pyof.v0x04.controller2switch.common import MultipartType
6
from pyof.v0x01.controller2switch.common import StatsType
7
from kytos.core.connection import ConnectionState
8
9
from tests.helpers import (get_controller_mock, get_kytos_event_mock,
10
                           get_switch_mock, get_connection_mock)
11
12
13
# pylint: disable=protected-access
14
class TestMain(TestCase):
15
    """docstring for TestMain."""
16
17
    def setUp(self):
18
        """Execute steps before each tests.
19
20
        Set the server_name_url from kytos/of_core
21
        """
22
        self.switch_v0x01 = get_switch_mock("00:00:00:00:00:00:00:01")
23
        self.switch_v0x04 = get_switch_mock("00:00:00:00:00:00:00:02")
24
        self.switch_v0x01.connection = get_connection_mock(
25
            0x01, get_switch_mock("00:00:00:00:00:00:00:03"))
26
        self.switch_v0x04.connection = get_connection_mock(
27
            0x04, get_switch_mock("00:00:00:00:00:00:00:04"))
28
29
        patch('kytos.core.helpers.run_on_thread', lambda x: x).start()
30
        from napps.kytos.of_core.main import Main
31
        self.addCleanup(patch.stopall)
32
33
        self.napp = Main(get_controller_mock())
34
35
    @patch('napps.kytos.of_core.v0x04.utils.update_flow_list')
36
    @patch('napps.kytos.of_core.v0x01.utils.update_flow_list')
37
    def test_request_flow_list(self, *args):
38
        """Test request flow list."""
39
        (mock_update_flow_list_v0x01, mock_update_flow_list_v0x04) = args
40
        mock_update_flow_list_v0x04.return_value = "ABC"
41
42
        self.napp._request_flow_list(self.switch_v0x01)
43
        mock_update_flow_list_v0x01.assert_called_with(self.napp.controller,
44
                                                       self.switch_v0x01)
45
        self.napp._request_flow_list(self.switch_v0x04)
46
        mock_update_flow_list_v0x04.assert_called_with(self.napp.controller,
47
                                                       self.switch_v0x04)
48
49
    @patch('napps.kytos.of_core.v0x01.flow.Flow.from_of_flow_stats')
50
    @patch('kytos.core.switch.Switch.update_description')
51
    def test_handle_stats_reply(self, *args):
52
        """Test handle stats reply."""
53
        (mock_update_description, mock_from_of_flow_stats_v0x01) = args
54
        mock_from_of_flow_stats_v0x01.return_value = "ABC"
55
56
        flow_msg = MagicMock()
57
        flow_msg.body = "A"
58
        flow_msg.body_type = StatsType.OFPST_FLOW
59
        event = get_kytos_event_mock(source=self.switch_v0x01.connection,
60
                                     message=flow_msg)
61
        self.napp.handle_stats_reply(event)
62
        mock_from_of_flow_stats_v0x01.assert_called_with(
63
            flow_msg.body, self.switch_v0x01.connection.switch)
64
65
        desc_msg = MagicMock()
66
        desc_msg.body = "A"
67
        desc_msg.body_type = StatsType.OFPST_DESC
68
        event = get_kytos_event_mock(source=self.switch_v0x01.connection,
69
                                     message=desc_msg)
70
        self.napp.handle_stats_reply(event)
71
        mock_update_description.assert_called_with(desc_msg.body)
72
73
    @patch('kytos.core.switch.Switch.update_description')
74
    @patch('napps.kytos.of_core.main.Main._handle_multipart_flow_stats')
75
    @patch('napps.kytos.of_core.v0x04.utils.handle_port_desc')
76
    def test_handle_multipart_reply(self, *args):
77
        """Test handle multipart reply."""
78
        (mock_of_core_v0x04_utils, mock_from_of_flow_stats_v0x04,
79
         mock_update_description) = args
80
81
        flow_msg = MagicMock()
82
        flow_msg.multipart_type = MultipartType.OFPMP_FLOW
83
        event = get_kytos_event_mock(source=self.switch_v0x01.connection,
84
                                     message=flow_msg)
85
86
        self.napp.handle_multipart_reply(event)
87
        mock_from_of_flow_stats_v0x04.assert_called_with(
88
            flow_msg, self.switch_v0x01.connection.switch)
89
90
        ofpmp_port_desc = MagicMock()
91
        ofpmp_port_desc.body = "A"
92
        ofpmp_port_desc.multipart_type = MultipartType.OFPMP_PORT_DESC
93
        event = get_kytos_event_mock(source=self.switch_v0x01.connection,
94
                                     message=ofpmp_port_desc)
95
        self.napp.handle_multipart_reply(event)
96
        mock_of_core_v0x04_utils.assert_called_with(
97
            self.napp.controller, self.switch_v0x01.connection.switch,
98
            ofpmp_port_desc.body)
99
100
        ofpmp_desc = MagicMock()
101
        ofpmp_desc.body = "A"
102
        ofpmp_desc.multipart_type = MultipartType.OFPMP_DESC
103
        event = get_kytos_event_mock(source=self.switch_v0x01.connection,
104
                                     message=ofpmp_desc)
105
        self.napp.handle_multipart_reply(event)
106
        mock_update_description.assert_called_with(ofpmp_desc.body)
107
108
    @patch('kytos.core.buffers.KytosEventBuffer.put')
109
    @patch('napps.kytos.of_core.v0x04.utils.send_set_config')
110
    @patch('napps.kytos.of_core.v0x01.utils.send_set_config')
111
    @patch('napps.kytos.of_core.v0x04.utils.send_desc_request')
112
    @patch('napps.kytos.of_core.v0x01.utils.send_desc_request')
113
    @patch('kytos.core.connection.Connection.set_established_state')
114
    @patch('kytos.core.connection.Connection.is_during_setup')
115
    @patch('napps.kytos.of_core.v0x04.utils.handle_features_reply')
116
    @patch('napps.kytos.of_core.v0x01.utils.handle_features_reply')
117
    def test_handle_features_reply(self, *args):
118
        """Test handle features reply."""
119
        (mock_freply_v0x01, mock_freply_v0x04, mock_is_during_setup,
120
         mock_set_established_state, mock_send_desc_request_v0x01,
121
         mock_send_desc_request_v0x04, mock_send_set_config_v0x01,
122
         mock_send_set_config_v0x04, mock_buffers_put) = args
123
        mock_freply_v0x01.return_value = self.switch_v0x01.connection.switch
124
        mock_freply_v0x04.return_value = self.switch_v0x04.connection.switch
125
        mock_is_during_setup.return_value = True
126
127
        self.switch_v0x01.connection.state = ConnectionState.SETUP
128
        self.switch_v0x01.connection.protocol.state = 'waiting_features_reply'
129
        event = get_kytos_event_mock(source=self.switch_v0x01.connection)
130
        self.napp.handle_features_reply(event)
131
        mock_freply_v0x01.assert_called_with(self.napp.controller, event)
132
        mock_send_desc_request_v0x01.assert_called_with(
133
            self.napp.controller, self.switch_v0x01.connection.switch)
134
        mock_send_set_config_v0x01.assert_called_with(
135
            self.napp.controller, self.switch_v0x01.connection.switch)
136
137
        self.switch_v0x04.connection.state = ConnectionState.SETUP
138
        self.switch_v0x04.connection.protocol.state = 'waiting_features_reply'
139
        event = get_kytos_event_mock(source=self.switch_v0x04.connection)
140
        self.napp.handle_features_reply(event)
141
        mock_freply_v0x04.assert_called_with(self.napp.controller, event)
142
        mock_send_desc_request_v0x04.assert_called_with(
143
            self.napp.controller, self.switch_v0x04.connection.switch)
144
        mock_send_set_config_v0x04.assert_called_with(
145
            self.napp.controller, self.switch_v0x04.connection.switch)
146
147
        mock_is_during_setup.assert_called()
148
        mock_set_established_state.assert_called()
149
        mock_buffers_put.assert_called()
150
151
    @patch('napps.kytos.of_core.main.Main._update_switch_flows')
152
    @patch('napps.kytos.of_core.v0x04.flow.Flow.from_of_flow_stats')
153
    @patch('napps.kytos.of_core.main.Main._is_multipart_reply_ours')
154
    def test_handle_multipart_flow_stats(self, *args):
155
        """Test handle multipart flow stats."""
156
        (mock_is_multipart_reply_ours, mock_from_of_flow_stats_v0x01,
157
         mock_update_switch_flows) = args
158
        mock_is_multipart_reply_ours.return_value = True
159
        mock_from_of_flow_stats_v0x01.return_value = "ABC"
160
161
        flow_msg = MagicMock()
162
        flow_msg.body = "A"
163
        flow_msg.flags.value = 2
164
        flow_msg.body_type = StatsType.OFPST_FLOW
165
166
        self.napp._handle_multipart_flow_stats(flow_msg, self.switch_v0x04)
167
168
        mock_is_multipart_reply_ours.assert_called_with(flow_msg,
169
                                                        self.switch_v0x04)
170
        mock_from_of_flow_stats_v0x01.assert_called_with(flow_msg.body,
171
                                                         self.switch_v0x04)
172
        mock_update_switch_flows.assert_called_with(self.switch_v0x04)
173