1
|
|
|
"""Test Main methods.""" |
2
|
|
|
from unittest import TestCase |
3
|
|
|
from unittest.mock import MagicMock, patch |
4
|
|
|
|
5
|
|
|
from kytos.lib.helpers import (get_controller_mock, get_kytos_event_mock, |
6
|
|
|
get_switch_mock, get_test_client) |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
# pylint: disable=protected-access |
10
|
|
|
class TestMain(TestCase): |
11
|
|
|
"""Tests for the Main class.""" |
12
|
|
|
|
13
|
|
|
API_URL = 'http://localhost:8181/api/kytos/flow_manager' |
14
|
|
|
|
15
|
|
|
def setUp(self): |
16
|
|
|
patch('kytos.core.helpers.run_on_thread', lambda x: x).start() |
17
|
|
|
# pylint: disable=bad-option-value |
18
|
|
|
from napps.kytos.flow_manager.main import Main |
19
|
|
|
|
20
|
|
|
self.addCleanup(patch.stopall) |
21
|
|
|
|
22
|
|
|
controller = get_controller_mock() |
23
|
|
|
self.switch_01 = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
24
|
|
|
self.switch_01.is_enabled.return_value = True |
25
|
|
|
self.switch_01.flows = [] |
26
|
|
|
|
27
|
|
|
self.switch_02 = get_switch_mock("00:00:00:00:00:00:00:02", 0x04) |
28
|
|
|
self.switch_02.is_enabled.return_value = False |
29
|
|
|
self.switch_02.flows = [] |
30
|
|
|
|
31
|
|
|
controller.switches = {"00:00:00:00:00:00:00:01": self.switch_01, |
32
|
|
|
"00:00:00:00:00:00:00:02": self.switch_02} |
33
|
|
|
|
34
|
|
|
self.napp = Main(controller) |
35
|
|
|
|
36
|
|
|
def test_rest_list_without_dpid(self): |
37
|
|
|
"""Test list rest method withoud dpid.""" |
38
|
|
|
flow_1 = MagicMock() |
39
|
|
|
flow_1.as_dict.return_value = {'flow_1': 'data'} |
40
|
|
|
flow_2 = MagicMock() |
41
|
|
|
flow_2.as_dict.return_value = {'flow_2': 'data'} |
42
|
|
|
self.switch_01.flows.append(flow_1) |
43
|
|
|
self.switch_02.flows.append(flow_2) |
44
|
|
|
|
45
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
46
|
|
|
url = f'{self.API_URL}/v2/flows' |
47
|
|
|
|
48
|
|
|
response = api.get(url) |
49
|
|
|
expected = { |
50
|
|
|
|
51
|
|
|
"00:00:00:00:00:00:00:01": {"flows": [{"flow_1": "data"}]}, |
52
|
|
|
|
53
|
|
|
"00:00:00:00:00:00:00:02": {"flows": [{"flow_2": "data"}]}, |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
self.assertEqual(response.json, expected) |
57
|
|
|
self.assertEqual(response.status_code, 200) |
58
|
|
|
|
59
|
|
|
def test_rest_list_with_dpid(self): |
60
|
|
|
"""Test list rest method with dpid.""" |
61
|
|
|
flow_1 = MagicMock() |
62
|
|
|
flow_1.as_dict.return_value = {'flow_1': 'data'} |
63
|
|
|
self.switch_01.flows.append(flow_1) |
64
|
|
|
|
65
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
66
|
|
|
url = f'{self.API_URL}/v2/flows/00:00:00:00:00:00:00:01' |
67
|
|
|
|
68
|
|
|
response = api.get(url) |
69
|
|
|
expected = {'00:00:00:00:00:00:00:01': {'flows': [{'flow_1': 'data'}]}} |
70
|
|
|
|
71
|
|
|
self.assertEqual(response.json, expected) |
72
|
|
|
self.assertEqual(response.status_code, 200) |
73
|
|
|
|
74
|
|
|
@patch('napps.kytos.flow_manager.main.Main._install_flows') |
75
|
|
|
def test_rest_add_and_delete_without_dpid(self, mock_install_flows): |
76
|
|
|
"""Test add and delete rest method without dpid.""" |
77
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
78
|
|
|
|
79
|
|
|
for method in ['flows', 'delete']: |
80
|
|
|
url = f'{self.API_URL}/v2/{method}' |
81
|
|
|
|
82
|
|
|
response_1 = api.post(url, json={'data': '123'}) |
83
|
|
|
response_2 = api.post(url) |
84
|
|
|
|
85
|
|
|
self.assertEqual(response_1.status_code, 200) |
86
|
|
|
self.assertEqual(response_2.status_code, 404) |
87
|
|
|
|
88
|
|
|
self.assertEqual(mock_install_flows.call_count, 2) |
89
|
|
|
|
90
|
|
|
@patch('napps.kytos.flow_manager.main.Main._install_flows') |
91
|
|
|
def test_rest_add_and_delete_with_dpid(self, mock_install_flows): |
92
|
|
|
"""Test add and delete rest method with dpid.""" |
93
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
94
|
|
|
|
95
|
|
|
for method in ['flows', 'delete']: |
96
|
|
|
url_1 = f'{self.API_URL}/v2/{method}/00:00:00:00:00:00:00:01' |
97
|
|
|
url_2 = f'{self.API_URL}/v2/{method}/00:00:00:00:00:00:00:02' |
98
|
|
|
url_3 = f'{self.API_URL}/v2/{method}/00:00:00:00:00:00:00:03' |
99
|
|
|
|
100
|
|
|
response_1 = api.post(url_1) |
101
|
|
|
response_2 = api.post(url_1, json={'data': '123'}) |
102
|
|
|
response_3 = api.post(url_2, json={'data': '123'}) |
103
|
|
|
response_4 = api.post(url_3, json={'data': '123'}) |
104
|
|
|
|
105
|
|
|
self.assertEqual(response_1.status_code, 404) |
106
|
|
|
self.assertEqual(response_2.status_code, 200) |
107
|
|
|
self.assertEqual(response_3.status_code, 404) |
108
|
|
|
self.assertEqual(response_4.status_code, 404) |
109
|
|
|
|
110
|
|
|
self.assertEqual(mock_install_flows.call_count, 2) |
111
|
|
|
|
112
|
|
|
def test_get_all_switches_enabled(self): |
113
|
|
|
"""Test _get_all_switches_enabled method.""" |
114
|
|
|
switches = self.napp._get_all_switches_enabled() |
115
|
|
|
|
116
|
|
|
self.assertEqual(switches, [self.switch_01]) |
117
|
|
|
|
118
|
|
|
@patch("napps.kytos.flow_manager.main.Main._flows_change") |
119
|
|
|
@patch("napps.kytos.flow_manager.main.Main._send_napp_event") |
120
|
|
|
@patch("napps.kytos.flow_manager.main.Main._add_flow_mod_sent") |
121
|
|
|
@patch("napps.kytos.flow_manager.main.Main._send_flow_mod") |
122
|
|
|
@patch("napps.kytos.flow_manager.main.FlowFactory.get_class") |
123
|
|
|
def test_install_flows(self, *args): |
124
|
|
|
"""Test _install_flows method.""" |
125
|
|
|
(mock_flow_factory, mock_send_flow_mod, mock_add_flow_mod_sent, |
126
|
|
|
mock_send_napp_event, _) = args |
127
|
|
|
serializer = MagicMock() |
128
|
|
|
flow = MagicMock() |
129
|
|
|
flow_mod = MagicMock() |
130
|
|
|
|
131
|
|
|
flow.as_of_add_flow_mod.return_value = flow_mod |
132
|
|
|
serializer.from_dict.return_value = flow |
133
|
|
|
mock_flow_factory.return_value = serializer |
134
|
|
|
|
135
|
|
|
flows_dict = {'flows': [MagicMock()]} |
136
|
|
|
switches = [self.switch_01] |
137
|
|
|
self.napp._install_flows('add', flows_dict, switches) |
138
|
|
|
|
139
|
|
|
mock_send_flow_mod.assert_called_with(flow.switch, flow_mod) |
140
|
|
|
mock_add_flow_mod_sent.assert_called_with(flow_mod.header.xid, flow) |
141
|
|
|
mock_send_napp_event.assert_called_with(self.switch_01, flow, 'add') |
142
|
|
|
|
143
|
|
|
def test_add_flow_mod_sent(self): |
144
|
|
|
"""Test _add_flow_mod_sent method.""" |
145
|
|
|
xid = 0 |
146
|
|
|
flow = MagicMock() |
147
|
|
|
|
148
|
|
|
self.napp._add_flow_mod_sent(xid, flow) |
149
|
|
|
|
150
|
|
|
self.assertEqual(self.napp._flow_mods_sent[xid], flow) |
151
|
|
|
|
152
|
|
|
@patch('kytos.core.buffers.KytosEventBuffer.put') |
153
|
|
|
def test_send_flow_mod(self, mock_buffers_put): |
154
|
|
|
"""Test _send_flow_mod method.""" |
155
|
|
|
switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
156
|
|
|
flow_mod = MagicMock() |
157
|
|
|
|
158
|
|
|
self.napp._send_flow_mod(switch, flow_mod) |
159
|
|
|
|
160
|
|
|
mock_buffers_put.assert_called() |
161
|
|
|
|
162
|
|
|
@patch("kytos.core.buffers.KytosEventBuffer.put") |
163
|
|
|
def test_send_napp_event(self, mock_buffers_put): |
164
|
|
|
"""Test _send_napp_event method.""" |
165
|
|
|
switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
166
|
|
|
flow = MagicMock() |
167
|
|
|
|
168
|
|
|
for command in ['add', 'delete', 'error']: |
169
|
|
|
self.napp._send_napp_event(switch, flow, command) |
170
|
|
|
|
171
|
|
|
self.assertEqual(mock_buffers_put.call_count, 3) |
172
|
|
|
|
173
|
|
|
@patch("napps.kytos.flow_manager.main.Main._send_napp_event") |
174
|
|
|
def test_handle_errors(self, mock_send_napp_event): |
175
|
|
|
"""Test handle_errors method.""" |
176
|
|
|
flow = MagicMock() |
177
|
|
|
self.napp._flow_mods_sent[0] = flow |
178
|
|
|
|
179
|
|
|
message = MagicMock() |
180
|
|
|
message.header.xid.value = 0 |
181
|
|
|
message.error_type = 2 |
182
|
|
|
message.code = 5 |
183
|
|
|
event = get_kytos_event_mock(name=".*.of_core.*.ofpt_error", |
184
|
|
|
content={"message": message}) |
185
|
|
|
self.napp.handle_errors(event) |
186
|
|
|
|
187
|
|
|
mock_send_napp_event.assert_called_with(flow.switch, flow, "error", |
188
|
|
|
error_code=5, error_type=2) |
189
|
|
|
|
190
|
|
|
@patch("napps.kytos.flow_manager.main.StoreHouse.get_data") |
191
|
|
|
def test_load_flows(self, mock_storehouse): |
192
|
|
|
"""Test load flows.""" |
193
|
|
|
self.napp._load_flows() |
194
|
|
|
mock_storehouse.assert_called() |
195
|
|
|
|
196
|
|
|
@patch("napps.kytos.flow_manager.main.Main._install_flows") |
197
|
|
|
def test_resend_stored_flows(self, mock_install_flows): |
198
|
|
|
"""Test resend stored flows.""" |
199
|
|
|
dpid = "00:00:00:00:00:00:00:01" |
200
|
|
|
switch = get_switch_mock(dpid, 0x04) |
201
|
|
|
mock_event = MagicMock() |
202
|
|
|
flow = {"command": "add", "data": MagicMock()} |
203
|
|
|
|
204
|
|
|
flows = {"flow_list": [flow]} |
205
|
|
|
mock_event.content = {"switch": dpid} |
206
|
|
|
self.napp.controller.switches = {dpid: switch} |
207
|
|
|
self.napp.stored_flows = {dpid: flows} |
208
|
|
|
self.napp.resend_stored_flows(mock_event) |
209
|
|
|
mock_install_flows.assert_called() |
210
|
|
|
|
211
|
|
|
@patch("napps.kytos.flow_manager.main.StoreHouse.save_flow") |
212
|
|
|
def test_flows_change(self, mock_save_flow): |
213
|
|
|
"""Test flows change.""" |
214
|
|
|
dpid = "00:00:00:00:00:00:00:01" |
215
|
|
|
switch = get_switch_mock(dpid, 0x04) |
216
|
|
|
switch.id = dpid |
217
|
|
|
flow = { |
218
|
|
|
"priority": 17, |
219
|
|
|
"cookie": 84114964, |
220
|
|
|
"command": "add", |
221
|
|
|
"match": {"dl_dst": "00:15:af:d5:38:98"}, |
222
|
|
|
} |
223
|
|
|
match_fields = { |
224
|
|
|
"priority": 17, |
225
|
|
|
"cookie": 84114964, |
226
|
|
|
"command": "add", |
227
|
|
|
"dl_dst": "00:15:af:d5:38:98", |
228
|
|
|
} |
229
|
|
|
flows = {"flows": [flow]} |
230
|
|
|
|
231
|
|
|
command = "add" |
232
|
|
|
flow_list = { |
233
|
|
|
"flow_list": [ |
234
|
|
|
{"match_fields": match_fields, "command": "add", "data": flows} |
235
|
|
|
] |
236
|
|
|
} |
237
|
|
|
self.napp.stored_flows = {dpid: flow_list} |
238
|
|
|
self.napp._flows_change(command, flows, [switch]) |
239
|
|
|
mock_save_flow.assert_called() |
240
|
|
|
|
241
|
|
|
self.napp.stored_flows = {} |
242
|
|
|
self.napp._flows_change(command, flows, [switch]) |
243
|
|
|
mock_save_flow.assert_called() |
244
|
|
|
|