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=import-outside-toplevel |
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 = {'00:00:00:00:00:00:00:01': {'flows': [{'flow_1': 'data'}]}, |
50
|
|
|
'00:00:00:00:00:00:00:02': {'flows': [{'flow_2': 'data'}]}} |
51
|
|
|
|
52
|
|
|
self.assertEqual(response.json, expected) |
53
|
|
|
self.assertEqual(response.status_code, 200) |
54
|
|
|
|
55
|
|
|
def test_rest_list_with_dpid(self): |
56
|
|
|
"""Test list rest method with dpid.""" |
57
|
|
|
flow_1 = MagicMock() |
58
|
|
|
flow_1.as_dict.return_value = {'flow_1': 'data'} |
59
|
|
|
self.switch_01.flows.append(flow_1) |
60
|
|
|
|
61
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
62
|
|
|
url = f'{self.API_URL}/v2/flows/00:00:00:00:00:00:00:01' |
63
|
|
|
|
64
|
|
|
response = api.get(url) |
65
|
|
|
expected = {'00:00:00:00:00:00:00:01': {'flows': [{'flow_1': 'data'}]}} |
66
|
|
|
|
67
|
|
|
self.assertEqual(response.json, expected) |
68
|
|
|
self.assertEqual(response.status_code, 200) |
69
|
|
|
|
70
|
|
|
@patch('napps.kytos.flow_manager.main.Main._install_flows') |
71
|
|
|
def test_rest_add_and_delete_without_dpid(self, mock_install_flows): |
72
|
|
|
"""Test add and delete rest method without dpid.""" |
73
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
74
|
|
|
|
75
|
|
|
for method in ['flows', 'delete']: |
76
|
|
|
url = f'{self.API_URL}/v2/{method}' |
77
|
|
|
|
78
|
|
|
response_1 = api.post(url, json={'data': '123'}) |
79
|
|
|
response_2 = api.post(url) |
80
|
|
|
|
81
|
|
|
self.assertEqual(response_1.status_code, 200) |
82
|
|
|
self.assertEqual(response_2.status_code, 404) |
83
|
|
|
|
84
|
|
|
self.assertEqual(mock_install_flows.call_count, 2) |
85
|
|
|
|
86
|
|
|
@patch('napps.kytos.flow_manager.main.Main._install_flows') |
87
|
|
|
def test_rest_add_and_delete_with_dpid(self, mock_install_flows): |
88
|
|
|
"""Test add and delete rest method with dpid.""" |
89
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
90
|
|
|
|
91
|
|
|
for method in ['flows', 'delete']: |
92
|
|
|
url_1 = f'{self.API_URL}/v2/{method}/00:00:00:00:00:00:00:01' |
93
|
|
|
url_2 = f'{self.API_URL}/v2/{method}/00:00:00:00:00:00:00:02' |
94
|
|
|
url_3 = f'{self.API_URL}/v2/{method}/00:00:00:00:00:00:00:03' |
95
|
|
|
|
96
|
|
|
response_1 = api.post(url_1) |
97
|
|
|
response_2 = api.post(url_1, json={'data': '123'}) |
98
|
|
|
response_3 = api.post(url_2, json={'data': '123'}) |
99
|
|
|
response_4 = api.post(url_3, json={'data': '123'}) |
100
|
|
|
|
101
|
|
|
self.assertEqual(response_1.status_code, 404) |
102
|
|
|
self.assertEqual(response_2.status_code, 200) |
103
|
|
|
if method == 'flows': |
104
|
|
|
self.assertEqual(response_3.status_code, 404) |
105
|
|
|
else: |
106
|
|
|
self.assertEqual(response_3.status_code, 200) |
107
|
|
|
self.assertEqual(response_4.status_code, 404) |
108
|
|
|
|
109
|
|
|
self.assertEqual(mock_install_flows.call_count, 3) |
110
|
|
|
|
111
|
|
|
def test_get_all_switches_enabled(self): |
112
|
|
|
"""Test _get_all_switches_enabled method.""" |
113
|
|
|
switches = self.napp._get_all_switches_enabled() |
114
|
|
|
|
115
|
|
|
self.assertEqual(switches, [self.switch_01]) |
116
|
|
|
|
117
|
|
|
@patch('napps.kytos.flow_manager.main.Main._store_changed_flows') |
118
|
|
|
@patch('napps.kytos.flow_manager.main.Main._send_napp_event') |
119
|
|
|
@patch('napps.kytos.flow_manager.main.Main._add_flow_mod_sent') |
120
|
|
|
@patch('napps.kytos.flow_manager.main.Main._send_flow_mod') |
121
|
|
|
@patch('napps.kytos.flow_manager.main.FlowFactory.get_class') |
122
|
|
|
def test_install_flows(self, *args): |
123
|
|
|
"""Test _install_flows method.""" |
124
|
|
|
(mock_flow_factory, mock_send_flow_mod, mock_add_flow_mod_sent, |
125
|
|
|
mock_send_napp_event, _) = args |
126
|
|
|
serializer = MagicMock() |
127
|
|
|
flow = MagicMock() |
128
|
|
|
flow_mod = MagicMock() |
129
|
|
|
|
130
|
|
|
flow.as_of_add_flow_mod.return_value = flow_mod |
131
|
|
|
serializer.from_dict.return_value = flow |
132
|
|
|
mock_flow_factory.return_value = serializer |
133
|
|
|
|
134
|
|
|
flows_dict = {'flows': [MagicMock()]} |
135
|
|
|
switches = [self.switch_01] |
136
|
|
|
self.napp._install_flows('add', flows_dict, switches) |
137
|
|
|
|
138
|
|
|
mock_send_flow_mod.assert_called_with(flow.switch, flow_mod) |
139
|
|
|
mock_add_flow_mod_sent.assert_called_with(flow_mod.header.xid, |
140
|
|
|
flow, 'add') |
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, 'add') |
149
|
|
|
|
150
|
|
|
self.assertEqual(self.napp._flow_mods_sent[xid], (flow, 'add')) |
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, 'add') |
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_command='add', |
189
|
|
|
error_code=5, error_type=2) |
190
|
|
|
|
191
|
|
|
@patch("napps.kytos.flow_manager.main.StoreHouse.get_data") |
192
|
|
|
def test_load_flows(self, mock_storehouse): |
193
|
|
|
"""Test load flows.""" |
194
|
|
|
self.napp._load_flows() |
195
|
|
|
mock_storehouse.assert_called() |
196
|
|
|
|
197
|
|
|
@patch("napps.kytos.flow_manager.main.Main._install_flows") |
198
|
|
|
def test_resend_stored_flows(self, mock_install_flows): |
199
|
|
|
"""Test resend stored flows.""" |
200
|
|
|
dpid = "00:00:00:00:00:00:00:01" |
201
|
|
|
switch = get_switch_mock(dpid, 0x04) |
202
|
|
|
mock_event = MagicMock() |
203
|
|
|
flow = {"command": "add", "flow": MagicMock()} |
204
|
|
|
|
205
|
|
|
flows = {"flow_list": [flow]} |
206
|
|
|
mock_event.content = {"switch": switch} |
207
|
|
|
self.napp.controller.switches = {dpid: switch} |
208
|
|
|
self.napp.stored_flows = {dpid: flows} |
209
|
|
|
self.napp.resend_stored_flows(mock_event) |
210
|
|
|
mock_install_flows.assert_called() |
211
|
|
|
|
212
|
|
|
@patch("napps.kytos.of_core.flow.FlowFactory.get_class") |
213
|
|
|
@patch("napps.kytos.flow_manager.main.StoreHouse.save_flow") |
214
|
|
|
def test_store_changed_flows(self, mock_save_flow, _): |
215
|
|
|
"""Test store changed flows.""" |
216
|
|
|
dpid = "00:00:00:00:00:00:00:01" |
217
|
|
|
switch = get_switch_mock(dpid, 0x04) |
218
|
|
|
switch.id = dpid |
219
|
|
|
flow = { |
220
|
|
|
"priority": 17, |
221
|
|
|
"cookie": 84114964, |
222
|
|
|
"command": "add", |
223
|
|
|
"match": {"dl_dst": "00:15:af:d5:38:98"}, |
224
|
|
|
} |
225
|
|
|
match_fields = { |
226
|
|
|
"priority": 17, |
227
|
|
|
"cookie": 84114964, |
228
|
|
|
"command": "add", |
229
|
|
|
"dl_dst": "00:15:af:d5:38:98", |
230
|
|
|
} |
231
|
|
|
flows = {"flow": flow} |
232
|
|
|
|
233
|
|
|
command = "add" |
234
|
|
|
flow_list = { |
235
|
|
|
"flow_list": [ |
236
|
|
|
{"match_fields": match_fields, "command": "delete", |
237
|
|
|
"flow": flow} |
238
|
|
|
] |
239
|
|
|
} |
240
|
|
|
self.napp.stored_flows = {dpid: flow_list} |
241
|
|
|
self.napp._store_changed_flows(command, flows, switch) |
242
|
|
|
mock_save_flow.assert_called() |
243
|
|
|
|
244
|
|
|
self.napp.stored_flows = {} |
245
|
|
|
self.napp._store_changed_flows(command, flows, switch) |
246
|
|
|
mock_save_flow.assert_called() |
247
|
|
|
|