1
|
|
|
"""Test Main methods.""" |
2
|
|
|
from unittest import TestCase |
3
|
|
|
from unittest.mock import MagicMock, patch |
4
|
|
|
|
5
|
|
|
from pyof.v0x01.controller2switch.common import StatsType |
6
|
|
|
from pyof.v0x04.controller2switch.common import MultipartType |
7
|
|
|
|
8
|
|
|
from kytos.core.connection import ConnectionState |
9
|
|
|
from tests.helpers import (get_connection_mock, get_controller_mock, |
10
|
|
|
get_kytos_event_mock, get_switch_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
|
|
|
|
174
|
|
|
@patch('napps.kytos.of_core.main.Main.update_port_status') |
175
|
|
|
@patch('napps.kytos.of_core.main.Main.update_links') |
176
|
|
|
def test_emit_message_in(self, *args): |
177
|
|
|
"""Test emit_message_in.""" |
178
|
|
|
(mock_update_links, mock_update_port_status) = args |
179
|
|
|
|
180
|
|
|
mock_port_connection = MagicMock() |
181
|
|
|
msg_port_mock = MagicMock() |
182
|
|
|
msg_port_mock.header.message_type.name = 'ofpt_port_status' |
183
|
|
|
mock_port_connection.side_effect = True |
184
|
|
|
self.napp.emit_message_in(mock_port_connection, |
185
|
|
|
msg_port_mock) |
186
|
|
|
mock_update_port_status.assert_called_with(msg_port_mock, |
187
|
|
|
mock_port_connection) |
188
|
|
|
|
189
|
|
|
mock_packet_in_connection = MagicMock() |
190
|
|
|
msg_packet_in_mock = MagicMock() |
191
|
|
|
mock_packet_in_connection.side_effect = True |
192
|
|
|
msg_packet_in_mock.header.message_type.name = 'ofpt_packet_in' |
193
|
|
|
self.napp.emit_message_in(mock_packet_in_connection, |
194
|
|
|
msg_packet_in_mock) |
195
|
|
|
mock_update_links.assert_called_with(msg_packet_in_mock, |
196
|
|
|
mock_packet_in_connection) |
197
|
|
|
|
198
|
|
|
@patch('pyof.utils.v0x04.symmetric.echo_reply.EchoReply') |
199
|
|
|
@patch('napps.kytos.of_core.main.Main.emit_message_out') |
200
|
|
|
def test_handle_echo_request(self, *args): |
201
|
|
|
"""Test handle echo request messages.""" |
202
|
|
|
(mock_emit_message_out, mock_echo_reply) = args |
203
|
|
|
mock_event = MagicMock() |
204
|
|
|
mock_echo_request = MagicMock() |
205
|
|
|
mock_echo_reply.return_value = "A" |
206
|
|
|
mock_echo_request.header.xid = "A" |
207
|
|
|
mock_echo_request.data = "A" |
208
|
|
|
mock_event.source.protocol.version = 4 |
209
|
|
|
mock_event.message = mock_echo_request |
210
|
|
|
self.napp.handle_echo_request(mock_event) |
211
|
|
|
mock_echo_reply.assert_called_with(xid=mock_echo_request.header.xid, |
212
|
|
|
data=mock_echo_request.data) |
213
|
|
|
mock_emit_message_out.assert_called_with(mock_event.source, "A") |
214
|
|
|
|
215
|
|
|
@patch('napps.kytos.of_core.main.Main.send_features_request') |
216
|
|
|
@patch('napps.kytos.of_core.v0x04.utils.say_hello') |
217
|
|
|
@patch('napps.kytos.of_core.main._get_version_from_bitmask') |
218
|
|
|
@patch('napps.kytos.of_core.main._get_version_from_header') |
219
|
|
|
def test_negotiate(self, *args): |
220
|
|
|
"""Test negotiate.""" |
221
|
|
|
(mock_version_header, mock_version_bitmask, mock_say_hello, |
222
|
|
|
mock_features_request) = args |
223
|
|
|
mock_version_header.return_value = 4 |
224
|
|
|
mock_version_bitmask.return_value = 4 |
225
|
|
|
mock_connection = MagicMock() |
226
|
|
|
mock_message = MagicMock() |
227
|
|
|
mock_message.versions = 4 |
228
|
|
|
self.napp._negotiate(mock_connection, mock_message) |
229
|
|
|
mock_version_bitmask.assert_called_with(mock_message.versions) |
230
|
|
|
mock_say_hello.assert_called_with(self.napp.controller, |
231
|
|
|
mock_connection) |
232
|
|
|
mock_features_request.assert_called_with(mock_connection) |
233
|
|
|
|
234
|
|
|
def test_fail_negotiation(self): |
235
|
|
|
|
236
|
|
|
|
237
|
|
|
@patch('napps.kytos.of_core.settings.SEND_FEATURES_REQUEST_ON_ECHO') |
238
|
|
|
@patch('napps.kytos.of_core.main.Main.send_features_request') |
239
|
|
|
def test_handle_queued_openflow_echo_reply(self, *args): |
240
|
|
|
"""Test handle queued OpenFlow echo reply messages.""" |
241
|
|
|
(mock_send_features_request, mock_settings) = args |
242
|
|
|
mock_settings.return_value = True |
243
|
|
|
mock_event = MagicMock() |
244
|
|
|
self.napp.handle_queued_openflow_echo_reply(mock_event) |
245
|
|
|
mock_send_features_request.assert_called_with(mock_event.destination) |
246
|
|
|
|
247
|
|
|
@patch('pyof.utils.v0x04.controller2switch.' |
248
|
|
|
'features_request.FeaturesRequest') |
249
|
|
|
@patch('napps.kytos.of_core.main.Main.emit_message_out') |
250
|
|
|
def test_send_features_request(self, *args): |
251
|
|
|
"""Test send send_features_request.""" |
252
|
|
|
(mock_emit_message_out, mock_features_request) = args |
253
|
|
|
mock_destination = MagicMock() |
254
|
|
|
mock_destination.protocol.version = 4 |
255
|
|
|
mock_features_request.return_value = "A" |
256
|
|
|
self.napp.send_features_request(mock_destination) |
257
|
|
|
mock_features_request.assert_called() |
258
|
|
|
mock_emit_message_out.assert_called_with(mock_destination, "A") |
259
|
|
|
|