1
|
|
|
"""Test Main methods.""" |
2
|
|
|
from unittest import TestCase |
3
|
|
|
from unittest.mock import MagicMock, patch |
4
|
|
|
|
5
|
|
|
from kytos.lib.helpers import (get_connection_mock, get_controller_mock, |
6
|
|
|
get_kytos_event_mock, get_switch_mock, |
7
|
|
|
get_test_client) |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
# pylint: disable=protected-access, too-many-public-methods |
11
|
|
|
class TestMain(TestCase): |
12
|
|
|
"""Tests for the Main class.""" |
13
|
|
|
|
14
|
|
|
API_URL = 'http://localhost:8181/api/kytos/flow_manager' |
15
|
|
|
|
16
|
|
|
def setUp(self): |
17
|
|
|
patch('kytos.core.helpers.run_on_thread', lambda x: x).start() |
18
|
|
|
# pylint: disable=import-outside-toplevel |
19
|
|
|
from napps.kytos.flow_manager.main import Main |
20
|
|
|
|
21
|
|
|
self.addCleanup(patch.stopall) |
22
|
|
|
|
23
|
|
|
controller = get_controller_mock() |
24
|
|
|
self.switch_01 = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
25
|
|
|
self.switch_01.is_enabled.return_value = True |
26
|
|
|
self.switch_01.flows = [] |
27
|
|
|
|
28
|
|
|
self.switch_02 = get_switch_mock("00:00:00:00:00:00:00:02", 0x04) |
29
|
|
|
self.switch_02.is_enabled.return_value = False |
30
|
|
|
self.switch_02.flows = [] |
31
|
|
|
|
32
|
|
|
controller.switches = {"00:00:00:00:00:00:00:01": self.switch_01, |
33
|
|
|
"00:00:00:00:00:00:00:02": self.switch_02} |
34
|
|
|
|
35
|
|
|
self.napp = Main(controller) |
36
|
|
|
|
37
|
|
|
def test_rest_list_without_dpid(self): |
38
|
|
|
"""Test list rest method withoud dpid.""" |
39
|
|
|
flow_dict = { |
40
|
|
|
"priority": 13, |
41
|
|
|
"cookie": 84114964, |
42
|
|
|
"command": "add", |
43
|
|
|
"match": {"dl_dst": "00:15:af:d5:38:98"}, |
44
|
|
|
} |
45
|
|
|
flow_dict_2 = { |
46
|
|
|
"priority": 18, |
47
|
|
|
"cookie": 84114964, |
48
|
|
|
"command": "add", |
49
|
|
|
"match": {"dl_dst": "00:15:af:d5:38:98"}, |
50
|
|
|
} |
51
|
|
|
flow_1 = MagicMock() |
52
|
|
|
flow_1.as_dict.return_value = flow_dict |
53
|
|
|
flow_2 = MagicMock() |
54
|
|
|
flow_2.as_dict.return_value = flow_dict_2 |
55
|
|
|
self.switch_01.flows.append(flow_1) |
56
|
|
|
self.switch_02.flows.append(flow_2) |
57
|
|
|
|
58
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
59
|
|
|
url = f'{self.API_URL}/v2/flows' |
60
|
|
|
|
61
|
|
|
response = api.get(url) |
62
|
|
|
expected = { |
63
|
|
|
'00:00:00:00:00:00:00:01': {'flows': [flow_dict]}, |
64
|
|
|
'00:00:00:00:00:00:00:02': {'flows': [flow_dict_2]}, |
65
|
|
|
} |
66
|
|
|
self.assertEqual(response.json, expected) |
67
|
|
|
self.assertEqual(response.status_code, 200) |
68
|
|
|
|
69
|
|
|
def test_rest_list_with_dpid(self): |
70
|
|
|
"""Test list rest method with dpid.""" |
71
|
|
|
flow_dict = { |
72
|
|
|
"priority": 13, |
73
|
|
|
"cookie": 84114964, |
74
|
|
|
"command": "add", |
75
|
|
|
"match": {"dl_dst": "00:15:af:d5:38:98"}, |
76
|
|
|
} |
77
|
|
|
flow_1 = MagicMock() |
78
|
|
|
flow_1.as_dict.return_value = flow_dict |
79
|
|
|
self.switch_01.flows.append(flow_1) |
80
|
|
|
|
81
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
82
|
|
|
url = f'{self.API_URL}/v2/flows/00:00:00:00:00:00:00:01' |
83
|
|
|
|
84
|
|
|
response = api.get(url) |
85
|
|
|
expected = {'00:00:00:00:00:00:00:01': {'flows': [flow_dict]}} |
86
|
|
|
|
87
|
|
|
self.assertEqual(response.json, expected) |
88
|
|
|
self.assertEqual(response.status_code, 200) |
89
|
|
|
|
90
|
|
|
@patch('napps.kytos.flow_manager.main.Main._install_flows') |
91
|
|
|
def test_rest_add_and_delete_without_dpid(self, mock_install_flows): |
92
|
|
|
"""Test add and delete rest method without dpid.""" |
93
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
94
|
|
|
|
95
|
|
|
for method in ['flows', 'delete']: |
96
|
|
|
url = f'{self.API_URL}/v2/{method}' |
97
|
|
|
|
98
|
|
|
response_1 = api.post(url, json={'data': '123'}) |
99
|
|
|
response_2 = api.post(url) |
100
|
|
|
|
101
|
|
|
self.assertEqual(response_1.status_code, 200) |
102
|
|
|
self.assertEqual(response_2.status_code, 404) |
103
|
|
|
|
104
|
|
|
self.assertEqual(mock_install_flows.call_count, 2) |
105
|
|
|
|
106
|
|
|
@patch('napps.kytos.flow_manager.main.Main._install_flows') |
107
|
|
|
def test_rest_add_and_delete_with_dpid(self, mock_install_flows): |
108
|
|
|
"""Test add and delete rest method with dpid.""" |
109
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
110
|
|
|
|
111
|
|
|
for method in ['flows', 'delete']: |
112
|
|
|
url_1 = f'{self.API_URL}/v2/{method}/00:00:00:00:00:00:00:01' |
113
|
|
|
url_2 = f'{self.API_URL}/v2/{method}/00:00:00:00:00:00:00:02' |
114
|
|
|
url_3 = f'{self.API_URL}/v2/{method}/00:00:00:00:00:00:00:03' |
115
|
|
|
|
116
|
|
|
response_1 = api.post(url_1) |
117
|
|
|
response_2 = api.post(url_1, json={'data': '123'}) |
118
|
|
|
response_3 = api.post(url_2, json={'data': '123'}) |
119
|
|
|
response_4 = api.post(url_3, json={'data': '123'}) |
120
|
|
|
|
121
|
|
|
self.assertEqual(response_1.status_code, 404) |
122
|
|
|
self.assertEqual(response_2.status_code, 200) |
123
|
|
|
if method == 'flows': |
124
|
|
|
self.assertEqual(response_3.status_code, 404) |
125
|
|
|
else: |
126
|
|
|
self.assertEqual(response_3.status_code, 200) |
127
|
|
|
self.assertEqual(response_4.status_code, 404) |
128
|
|
|
|
129
|
|
|
self.assertEqual(mock_install_flows.call_count, 3) |
130
|
|
|
|
131
|
|
|
def test_get_all_switches_enabled(self): |
132
|
|
|
"""Test _get_all_switches_enabled method.""" |
133
|
|
|
switches = self.napp._get_all_switches_enabled() |
134
|
|
|
|
135
|
|
|
self.assertEqual(switches, [self.switch_01]) |
136
|
|
|
|
137
|
|
View Code Duplication |
@patch('napps.kytos.flow_manager.main.Main._store_changed_flows') |
|
|
|
|
138
|
|
|
@patch('napps.kytos.flow_manager.main.Main._send_napp_event') |
139
|
|
|
@patch('napps.kytos.flow_manager.main.Main._add_flow_mod_sent') |
140
|
|
|
@patch('napps.kytos.flow_manager.main.Main._send_flow_mod') |
141
|
|
|
@patch('napps.kytos.flow_manager.main.FlowFactory.get_class') |
142
|
|
|
def test_install_flows(self, *args): |
143
|
|
|
"""Test _install_flows method.""" |
144
|
|
|
(mock_flow_factory, mock_send_flow_mod, mock_add_flow_mod_sent, |
145
|
|
|
mock_send_napp_event, _) = args |
146
|
|
|
serializer = MagicMock() |
147
|
|
|
flow = MagicMock() |
148
|
|
|
flow_mod = MagicMock() |
149
|
|
|
|
150
|
|
|
flow.as_of_add_flow_mod.return_value = flow_mod |
151
|
|
|
serializer.from_dict.return_value = flow |
152
|
|
|
mock_flow_factory.return_value = serializer |
153
|
|
|
|
154
|
|
|
flows_dict = {'flows': [MagicMock()]} |
155
|
|
|
switches = [self.switch_01] |
156
|
|
|
self.napp._install_flows('add', flows_dict, switches) |
157
|
|
|
|
158
|
|
|
mock_send_flow_mod.assert_called_with(flow.switch, flow_mod) |
159
|
|
|
mock_add_flow_mod_sent.assert_called_with(flow_mod.header.xid, |
160
|
|
|
flow, 'add') |
161
|
|
|
mock_send_napp_event.assert_called_with(self.switch_01, flow, 'add') |
162
|
|
|
|
163
|
|
View Code Duplication |
@patch('napps.kytos.flow_manager.main.Main._store_changed_flows') |
|
|
|
|
164
|
|
|
@patch('napps.kytos.flow_manager.main.Main._send_napp_event') |
165
|
|
|
@patch('napps.kytos.flow_manager.main.Main._add_flow_mod_sent') |
166
|
|
|
@patch('napps.kytos.flow_manager.main.Main._send_flow_mod') |
167
|
|
|
@patch('napps.kytos.flow_manager.main.FlowFactory.get_class') |
168
|
|
|
def test_install_flows_with_delete_strict(self, *args): |
169
|
|
|
"""Test _install_flows method with strict delete command.""" |
170
|
|
|
(mock_flow_factory, mock_send_flow_mod, mock_add_flow_mod_sent, |
171
|
|
|
mock_send_napp_event, _) = args |
172
|
|
|
serializer = MagicMock() |
173
|
|
|
flow = MagicMock() |
174
|
|
|
flow_mod = MagicMock() |
175
|
|
|
|
176
|
|
|
flow.as_of_strict_delete_flow_mod.return_value = flow_mod |
177
|
|
|
serializer.from_dict.return_value = flow |
178
|
|
|
mock_flow_factory.return_value = serializer |
179
|
|
|
|
180
|
|
|
flows_dict = {'flows': [MagicMock()]} |
181
|
|
|
switches = [self.switch_01] |
182
|
|
|
self.napp._install_flows('delete_strict', flows_dict, switches) |
183
|
|
|
|
184
|
|
|
mock_send_flow_mod.assert_called_with(flow.switch, flow_mod) |
185
|
|
|
mock_add_flow_mod_sent.assert_called_with(flow_mod.header.xid, |
186
|
|
|
flow, 'delete_strict') |
187
|
|
|
mock_send_napp_event.assert_called_with(self.switch_01, flow, |
188
|
|
|
'delete_strict') |
189
|
|
|
|
190
|
|
|
def test_add_flow_mod_sent(self): |
191
|
|
|
"""Test _add_flow_mod_sent method.""" |
192
|
|
|
xid = 0 |
193
|
|
|
flow = MagicMock() |
194
|
|
|
|
195
|
|
|
self.napp._add_flow_mod_sent(xid, flow, 'add') |
196
|
|
|
|
197
|
|
|
self.assertEqual(self.napp._flow_mods_sent[xid], (flow, 'add')) |
198
|
|
|
|
199
|
|
|
@patch('kytos.core.buffers.KytosEventBuffer.put') |
200
|
|
|
def test_send_flow_mod(self, mock_buffers_put): |
201
|
|
|
"""Test _send_flow_mod method.""" |
202
|
|
|
switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
203
|
|
|
flow_mod = MagicMock() |
204
|
|
|
|
205
|
|
|
self.napp._send_flow_mod(switch, flow_mod) |
206
|
|
|
|
207
|
|
|
mock_buffers_put.assert_called() |
208
|
|
|
|
209
|
|
|
@patch('kytos.core.buffers.KytosEventBuffer.put') |
210
|
|
|
def test_send_napp_event(self, mock_buffers_put): |
211
|
|
|
"""Test _send_napp_event method.""" |
212
|
|
|
switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
213
|
|
|
flow = MagicMock() |
214
|
|
|
|
215
|
|
|
for command in ['add', 'delete', 'delete_strict', 'error']: |
216
|
|
|
self.napp._send_napp_event(switch, flow, command) |
217
|
|
|
|
218
|
|
|
self.assertEqual(mock_buffers_put.call_count, 4) |
219
|
|
|
|
220
|
|
|
@patch('napps.kytos.flow_manager.main.Main._send_napp_event') |
221
|
|
|
def test_handle_errors(self, mock_send_napp_event): |
222
|
|
|
"""Test handle_errors method.""" |
223
|
|
|
flow = MagicMock() |
224
|
|
|
self.napp._flow_mods_sent[0] = (flow, 'add') |
225
|
|
|
|
226
|
|
|
switch = get_switch_mock("00:00:00:00:00:00:00:01") |
227
|
|
|
switch.connection = get_connection_mock( |
228
|
|
|
0x04, get_switch_mock("00:00:00:00:00:00:00:02")) |
229
|
|
|
|
230
|
|
|
protocol = MagicMock() |
231
|
|
|
protocol.unpack.return_value = 'error_packet' |
232
|
|
|
|
233
|
|
|
switch.connection.protocol = protocol |
234
|
|
|
|
235
|
|
|
message = MagicMock() |
236
|
|
|
message.header.xid.value = 0 |
237
|
|
|
message.error_type = 2 |
238
|
|
|
message.code = 5 |
239
|
|
|
event = get_kytos_event_mock(name='.*.of_core.*.ofpt_error', |
240
|
|
|
content={'message': message, |
241
|
|
|
'source': switch.connection}) |
242
|
|
|
self.napp.handle_errors(event) |
243
|
|
|
|
244
|
|
|
mock_send_napp_event.assert_called_with(flow.switch, flow, 'error', |
245
|
|
|
error_command='add', |
246
|
|
|
error_code=5, error_type=2) |
247
|
|
|
|
248
|
|
|
@patch("napps.kytos.flow_manager.main.StoreHouse.get_data") |
249
|
|
|
def test_load_flows(self, mock_storehouse): |
250
|
|
|
"""Test load flows.""" |
251
|
|
|
self.napp._load_flows() |
252
|
|
|
mock_storehouse.assert_called() |
253
|
|
|
|
254
|
|
|
@patch("napps.kytos.flow_manager.main.Main._install_flows") |
255
|
|
|
def test_resend_stored_flows(self, mock_install_flows): |
256
|
|
|
"""Test resend stored flows.""" |
257
|
|
|
dpid = "00:00:00:00:00:00:00:01" |
258
|
|
|
switch = get_switch_mock(dpid, 0x04) |
259
|
|
|
mock_event = MagicMock() |
260
|
|
|
flow = {"command": "add", "flow": MagicMock()} |
261
|
|
|
|
262
|
|
|
flows = {"flow_list": [flow]} |
263
|
|
|
mock_event.content = {"switch": switch} |
264
|
|
|
self.napp.controller.switches = {dpid: switch} |
265
|
|
|
self.napp.stored_flows = {dpid: flows} |
266
|
|
|
self.napp.resend_stored_flows(mock_event) |
267
|
|
|
mock_install_flows.assert_called() |
268
|
|
|
|
269
|
|
|
@patch("napps.kytos.of_core.flow.FlowFactory.get_class") |
270
|
|
|
@patch("napps.kytos.flow_manager.main.StoreHouse.save_flow") |
271
|
|
|
def test_store_changed_flows(self, mock_save_flow, _): |
272
|
|
|
"""Test store changed flows.""" |
273
|
|
|
dpid = "00:00:00:00:00:00:00:01" |
274
|
|
|
switch = get_switch_mock(dpid, 0x04) |
275
|
|
|
switch.id = dpid |
276
|
|
|
flow = { |
277
|
|
|
"priority": 17, |
278
|
|
|
"cookie": 84114964, |
279
|
|
|
"command": "add", |
280
|
|
|
"match": {"dl_dst": "00:15:af:d5:38:98"}, |
281
|
|
|
} |
282
|
|
|
match_fields = { |
283
|
|
|
"priority": 17, |
284
|
|
|
"cookie": 84114964, |
285
|
|
|
"command": "add", |
286
|
|
|
"dl_dst": "00:15:af:d5:38:98", |
287
|
|
|
} |
288
|
|
|
flows = {"flow": flow} |
289
|
|
|
|
290
|
|
|
command = "add" |
291
|
|
|
flow_list = { |
292
|
|
|
"flow_list": [ |
293
|
|
|
{"match_fields": match_fields, "command": "delete", |
294
|
|
|
"flow": flow} |
295
|
|
|
] |
296
|
|
|
} |
297
|
|
|
self.napp.stored_flows = {dpid: flow_list} |
298
|
|
|
self.napp._store_changed_flows(command, flows, switch) |
299
|
|
|
mock_save_flow.assert_called() |
300
|
|
|
|
301
|
|
|
self.napp.stored_flows = {} |
302
|
|
|
self.napp._store_changed_flows(command, flows, switch) |
303
|
|
|
mock_save_flow.assert_called() |
304
|
|
|
|
305
|
|
View Code Duplication |
@patch('napps.kytos.flow_manager.main.Main._install_flows') |
|
|
|
|
306
|
|
|
@patch('napps.kytos.flow_manager.main.FlowFactory.get_class') |
307
|
|
|
def test_check_switch_consistency_add(self, *args): |
308
|
|
|
"""Test check_switch_consistency method. |
309
|
|
|
|
310
|
|
|
This test checks the case when a flow is missing in switch and have the |
311
|
|
|
ADD command. |
312
|
|
|
""" |
313
|
|
|
(mock_flow_factory, mock_install_flows) = args |
314
|
|
|
dpid = "00:00:00:00:00:00:00:01" |
315
|
|
|
switch = get_switch_mock(dpid, 0x04) |
316
|
|
|
switch.flows = [] |
317
|
|
|
|
318
|
|
|
flow_1 = MagicMock() |
319
|
|
|
flow_1.as_dict.return_value = {'flow_1': 'data'} |
320
|
|
|
|
321
|
|
|
flow_list = [{"command": "add", |
322
|
|
|
"flow": {'flow_1': 'data'} |
323
|
|
|
}] |
324
|
|
|
serializer = MagicMock() |
325
|
|
|
|
326
|
|
|
mock_flow_factory.return_value = serializer |
327
|
|
|
self.napp.stored_flows = {dpid: {"flow_list": flow_list}} |
328
|
|
|
self.napp.check_switch_consistency(switch) |
329
|
|
|
mock_install_flows.assert_called() |
330
|
|
|
|
331
|
|
View Code Duplication |
@patch('napps.kytos.flow_manager.main.Main._install_flows') |
|
|
|
|
332
|
|
|
@patch('napps.kytos.flow_manager.main.FlowFactory.get_class') |
333
|
|
|
def test_check_switch_consistency_delete(self, *args): |
334
|
|
|
"""Test check_switch_consistency method. |
335
|
|
|
|
336
|
|
|
This test checks the case when a flow is missing in switch and have the |
337
|
|
|
DELETE command. |
338
|
|
|
""" |
339
|
|
|
(mock_flow_factory, mock_install_flows) = args |
340
|
|
|
dpid = "00:00:00:00:00:00:00:01" |
341
|
|
|
switch = get_switch_mock(dpid, 0x04) |
342
|
|
|
|
343
|
|
|
flow_1 = MagicMock() |
344
|
|
|
flow_1.as_dict.return_value = {'flow_1': 'data'} |
345
|
|
|
|
346
|
|
|
flow_list = [{"command": "delete", |
347
|
|
|
"flow": {'flow_1': 'data'} |
348
|
|
|
}] |
349
|
|
|
serializer = MagicMock() |
350
|
|
|
serializer.from_dict.return_value = flow_1 |
351
|
|
|
|
352
|
|
|
switch.flows = [flow_1] |
353
|
|
|
|
354
|
|
|
mock_flow_factory.return_value = serializer |
355
|
|
|
self.napp.stored_flows = {dpid: {"flow_list": flow_list}} |
356
|
|
|
self.napp.check_switch_consistency(switch) |
357
|
|
|
mock_install_flows.assert_called() |
358
|
|
|
|
359
|
|
View Code Duplication |
@patch('napps.kytos.flow_manager.main.Main._install_flows') |
|
|
|
|
360
|
|
|
@patch('napps.kytos.flow_manager.main.FlowFactory.get_class') |
361
|
|
|
def test_check_storehouse_consistency(self, *args): |
362
|
|
|
"""Test check_storehouse_consistency method. |
363
|
|
|
|
364
|
|
|
This test checks the case when a flow is missing in storehouse. |
365
|
|
|
""" |
366
|
|
|
(mock_flow_factory, mock_install_flows) = args |
367
|
|
|
dpid = "00:00:00:00:00:00:00:01" |
368
|
|
|
switch = get_switch_mock(dpid, 0x04) |
369
|
|
|
|
370
|
|
|
flow_1 = MagicMock() |
371
|
|
|
flow_1.as_dict.return_value = {'flow_1': 'data'} |
372
|
|
|
|
373
|
|
|
switch.flows = [flow_1] |
374
|
|
|
|
375
|
|
|
flow_list = [{"command": "add", |
376
|
|
|
"flow": {'flow_2': 'data'} |
377
|
|
|
}] |
378
|
|
|
serializer = MagicMock() |
379
|
|
|
|
380
|
|
|
mock_flow_factory.return_value = serializer |
381
|
|
|
self.napp.stored_flows = {dpid: {"flow_list": flow_list}} |
382
|
|
|
self.napp.check_storehouse_consistency(switch) |
383
|
|
|
mock_install_flows.assert_called() |
384
|
|
|
|
385
|
|
View Code Duplication |
@patch('napps.kytos.flow_manager.main.Main._install_flows') |
|
|
|
|
386
|
|
|
@patch('napps.kytos.flow_manager.main.FlowFactory.get_class') |
387
|
|
|
@patch("napps.kytos.flow_manager.main.StoreHouse.save_flow") |
388
|
|
|
def test_no_strict_delete(self, *args): |
389
|
|
|
"""Test the non-strict matching method. |
390
|
|
|
|
391
|
|
|
Test non-strict matching to delete a Flow using a cookie. |
392
|
|
|
""" |
393
|
|
|
(mock_save_flow, _, _) = args |
394
|
|
|
dpid = "00:00:00:00:00:00:00:01" |
395
|
|
|
switch = get_switch_mock(dpid, 0x04) |
396
|
|
|
switch.id = dpid |
397
|
|
|
stored_flow = { |
398
|
|
|
"command": "add", |
399
|
|
|
"flow": { |
400
|
|
|
"actions": [{"action_type": "set_vlan", "vlan_id": 300}], |
401
|
|
|
"cookie": 6191162389751548793, |
402
|
|
|
"match": {"dl_vlan": 300, "in_port": 1}, |
403
|
|
|
}, |
404
|
|
|
} |
405
|
|
|
stored_flow2 = { |
406
|
|
|
"command": "add", |
407
|
|
|
"flow": { |
408
|
|
|
"actions": [], |
409
|
|
|
"cookie": 4961162389751548787, |
410
|
|
|
"match": {"in_port": 2}, |
411
|
|
|
}, |
412
|
|
|
} |
413
|
|
|
flow_to_install = { |
414
|
|
|
"cookie": 6191162389751548793, |
415
|
|
|
"cookie_mask": 18446744073709551615, |
416
|
|
|
} |
417
|
|
|
flow_list = {"flow_list": [stored_flow, stored_flow2]} |
418
|
|
|
command = "delete" |
419
|
|
|
self.napp.stored_flows = {dpid: flow_list} |
420
|
|
|
|
421
|
|
|
self.napp._store_changed_flows(command, flow_to_install, switch) |
422
|
|
|
mock_save_flow.assert_called() |
423
|
|
|
self.assertEqual(len(self.napp.stored_flows), 1) |
424
|
|
|
|
425
|
|
View Code Duplication |
@patch('napps.kytos.flow_manager.main.Main._install_flows') |
|
|
|
|
426
|
|
|
@patch('napps.kytos.flow_manager.main.FlowFactory.get_class') |
427
|
|
|
@patch("napps.kytos.flow_manager.main.StoreHouse.save_flow") |
428
|
|
|
def test_no_strict_delete_with_ipv4(self, *args): |
429
|
|
|
"""Test the non-strict matching method. |
430
|
|
|
|
431
|
|
|
Test non-strict matching to delete a Flow using IPv4. |
432
|
|
|
""" |
433
|
|
|
(mock_save_flow, _, _) = args |
434
|
|
|
dpid = "00:00:00:00:00:00:00:01" |
435
|
|
|
switch = get_switch_mock(dpid, 0x04) |
436
|
|
|
switch.id = dpid |
437
|
|
|
stored_flow = { |
438
|
|
|
"command": "add", |
439
|
|
|
"flow": { |
440
|
|
|
"priority": 10, |
441
|
|
|
"cookie": 84114904, |
442
|
|
|
"match": { |
443
|
|
|
"ipv4_src": "192.168.1.120", |
444
|
|
|
"ipv4_dst": "192.168.0.2", |
445
|
|
|
}, |
446
|
|
|
"actions": [], |
447
|
|
|
}, |
448
|
|
|
} |
449
|
|
|
stored_flow2 = { |
450
|
|
|
"command": "add", |
451
|
|
|
"flow": { |
452
|
|
|
"actions": [], |
453
|
|
|
"cookie": 4961162389751548787, |
454
|
|
|
"match": {"in_port": 2}, |
455
|
|
|
}, |
456
|
|
|
} |
457
|
|
|
flow_to_install = {"match": {"ipv4_src": '192.168.1.1/24'}} |
458
|
|
|
flow_list = {"flow_list": [stored_flow, stored_flow2]} |
459
|
|
|
command = "delete" |
460
|
|
|
self.napp.stored_flows = {dpid: flow_list} |
461
|
|
|
|
462
|
|
|
self.napp._store_changed_flows(command, flow_to_install, switch) |
463
|
|
|
mock_save_flow.assert_called() |
464
|
|
|
self.assertEqual(len(self.napp.stored_flows[dpid]['flow_list']), 2) |
465
|
|
|
|
466
|
|
View Code Duplication |
@patch('napps.kytos.flow_manager.main.Main._install_flows') |
|
|
|
|
467
|
|
|
@patch('napps.kytos.flow_manager.main.FlowFactory.get_class') |
468
|
|
|
@patch("napps.kytos.flow_manager.main.StoreHouse.save_flow") |
469
|
|
|
def test_no_strict_delete_with_ipv4_fail(self, *args): |
470
|
|
|
"""Test the non-strict matching method. |
471
|
|
|
|
472
|
|
|
Test non-strict Fail case matching to delete a Flow using IPv4. |
473
|
|
|
""" |
474
|
|
|
(mock_save_flow, _, _) = args |
475
|
|
|
dpid = "00:00:00:00:00:00:00:01" |
476
|
|
|
switch = get_switch_mock(dpid, 0x04) |
477
|
|
|
switch.id = dpid |
478
|
|
|
stored_flow = { |
479
|
|
|
"command": "add", |
480
|
|
|
"flow": { |
481
|
|
|
"priority": 10, |
482
|
|
|
"cookie": 84114904, |
483
|
|
|
"match": { |
484
|
|
|
"ipv4_src": "192.168.2.1", |
485
|
|
|
"ipv4_dst": "192.168.0.2", |
486
|
|
|
}, |
487
|
|
|
"actions": [], |
488
|
|
|
}, |
489
|
|
|
} |
490
|
|
|
stored_flow2 = { |
491
|
|
|
"command": "add", |
492
|
|
|
"flow": { |
493
|
|
|
"actions": [], |
494
|
|
|
"cookie": 4961162389751548787, |
495
|
|
|
"match": {"in_port": 2}, |
496
|
|
|
}, |
497
|
|
|
} |
498
|
|
|
flow_to_install = {"match": {"ipv4_src": '192.168.1.1/24'}} |
499
|
|
|
flow_list = {"flow_list": [stored_flow, stored_flow2]} |
500
|
|
|
command = "delete" |
501
|
|
|
self.napp.stored_flows = {dpid: flow_list} |
502
|
|
|
|
503
|
|
|
self.napp._store_changed_flows(command, flow_to_install, switch) |
504
|
|
|
mock_save_flow.assert_called() |
505
|
|
|
self.assertEqual(len(self.napp.stored_flows[dpid]['flow_list']), 3) |
506
|
|
|
|
507
|
|
|
@patch('napps.kytos.flow_manager.main.Main._install_flows') |
508
|
|
|
@patch('napps.kytos.flow_manager.main.FlowFactory.get_class') |
509
|
|
|
@patch("napps.kytos.flow_manager.main.StoreHouse.save_flow") |
510
|
|
|
def test_no_strict_delete_of10(self, *args): |
511
|
|
|
"""Test the non-strict matching method. |
512
|
|
|
|
513
|
|
|
Test non-strict matching to delete a Flow using OF10. |
514
|
|
|
""" |
515
|
|
|
(mock_save_flow, _, _) = args |
516
|
|
|
dpid = "00:00:00:00:00:00:00:01" |
517
|
|
|
switch = get_switch_mock(dpid, 0x01) |
518
|
|
|
switch.id = dpid |
519
|
|
|
stored_flow = { |
520
|
|
|
"command": "add", |
521
|
|
|
"flow": { |
522
|
|
|
"actions": [{"max_len": 65535, "port": 6}], |
523
|
|
|
"cookie": 4961162389751548787, |
524
|
|
|
"match": { |
525
|
|
|
"in_port": 80, |
526
|
|
|
"dl_src": "00:00:00:00:00:00", |
527
|
|
|
"dl_dst": "f2:0b:a4:7d:f8:ea", |
528
|
|
|
"dl_vlan": 0, |
529
|
|
|
"dl_vlan_pcp": 0, |
530
|
|
|
"dl_type": 0, |
531
|
|
|
"nw_tos": 0, |
532
|
|
|
"nw_proto": 0, |
533
|
|
|
"nw_src": "192.168.0.1", |
534
|
|
|
"nw_dst": "0.0.0.0", |
535
|
|
|
"tp_src": 0, |
536
|
|
|
"tp_dst": 0, |
537
|
|
|
}, |
538
|
|
|
"out_port": 65532, |
539
|
|
|
"priority": 123, |
540
|
|
|
}, |
541
|
|
|
} |
542
|
|
|
stored_flow2 = { |
543
|
|
|
"command": "add", |
544
|
|
|
"flow": { |
545
|
|
|
"actions": [], |
546
|
|
|
"cookie": 4961162389751654, |
547
|
|
|
"match": { |
548
|
|
|
"in_port": 2, |
549
|
|
|
"dl_src": "00:00:00:00:00:00", |
550
|
|
|
"dl_dst": "f2:0b:a4:7d:f8:ea", |
551
|
|
|
"dl_vlan": 0, |
552
|
|
|
"dl_vlan_pcp": 0, |
553
|
|
|
"dl_type": 0, |
554
|
|
|
"nw_tos": 0, |
555
|
|
|
"nw_proto": 0, |
556
|
|
|
"nw_src": "192.168.0.1", |
557
|
|
|
"nw_dst": "0.0.0.0", |
558
|
|
|
"tp_src": 0, |
559
|
|
|
"tp_dst": 0, |
560
|
|
|
}, |
561
|
|
|
"out_port": 655, |
562
|
|
|
"priority": 1, |
563
|
|
|
}, |
564
|
|
|
} |
565
|
|
|
flow_to_install = {"match": {"in_port": 80, "wildcards": 4194303}} |
566
|
|
|
flow_list = {"flow_list": [stored_flow, stored_flow2]} |
567
|
|
|
command = "delete" |
568
|
|
|
self.napp.stored_flows = {dpid: flow_list} |
569
|
|
|
|
570
|
|
|
self.napp._store_changed_flows(command, flow_to_install, switch) |
571
|
|
|
mock_save_flow.assert_called() |
572
|
|
|
self.assertEqual(len(self.napp.stored_flows[dpid]['flow_list']), 1) |
573
|
|
|
|