|
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
|
|
|
self.addCleanup(patch.stopall) |
|
20
|
|
|
|
|
21
|
|
|
controller = get_controller_mock() |
|
22
|
|
|
self.switch_01 = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
|
23
|
|
|
self.switch_01.is_enabled.return_value = True |
|
24
|
|
|
self.switch_01.flows = [] |
|
25
|
|
|
|
|
26
|
|
|
self.switch_02 = get_switch_mock("00:00:00:00:00:00:00:02", 0x04) |
|
27
|
|
|
self.switch_02.is_enabled.return_value = False |
|
28
|
|
|
self.switch_02.flows = [] |
|
29
|
|
|
|
|
30
|
|
|
controller.switches = {"00:00:00:00:00:00:00:01": self.switch_01, |
|
31
|
|
|
"00:00:00:00:00:00:00:02": self.switch_02} |
|
32
|
|
|
|
|
33
|
|
|
self.napp = Main(controller) |
|
34
|
|
|
|
|
35
|
|
|
def test_rest_list_without_dpid(self): |
|
36
|
|
|
"""Test list rest method withoud dpid.""" |
|
37
|
|
|
flow_1 = MagicMock() |
|
38
|
|
|
flow_1.as_dict.return_value = {'flow_1': 'data'} |
|
39
|
|
|
flow_2 = MagicMock() |
|
40
|
|
|
flow_2.as_dict.return_value = {'flow_2': 'data'} |
|
41
|
|
|
self.switch_01.flows.append(flow_1) |
|
42
|
|
|
self.switch_02.flows.append(flow_2) |
|
43
|
|
|
|
|
44
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
|
45
|
|
|
url = f'{self.API_URL}/v2/flows' |
|
46
|
|
|
|
|
47
|
|
|
response = api.get(url) |
|
48
|
|
|
expected = {'00:00:00:00:00:00:00:01': {'flows': [{'flow_1': 'data'}]}, |
|
49
|
|
|
'00:00:00:00:00:00:00:02': {'flows': [{'flow_2': 'data'}]}} |
|
50
|
|
|
|
|
51
|
|
|
self.assertEqual(response.json, expected) |
|
52
|
|
|
self.assertEqual(response.status_code, 200) |
|
53
|
|
|
|
|
54
|
|
|
def test_rest_list_with_dpid(self): |
|
55
|
|
|
"""Test list rest method with dpid.""" |
|
56
|
|
|
flow_1 = MagicMock() |
|
57
|
|
|
flow_1.as_dict.return_value = {'flow_1': 'data'} |
|
58
|
|
|
self.switch_01.flows.append(flow_1) |
|
59
|
|
|
|
|
60
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
|
61
|
|
|
url = f'{self.API_URL}/v2/flows/00:00:00:00:00:00:00:01' |
|
62
|
|
|
|
|
63
|
|
|
response = api.get(url) |
|
64
|
|
|
expected = {'00:00:00:00:00:00:00:01': {'flows': [{'flow_1': 'data'}]}} |
|
65
|
|
|
|
|
66
|
|
|
self.assertEqual(response.json, expected) |
|
67
|
|
|
self.assertEqual(response.status_code, 200) |
|
68
|
|
|
|
|
69
|
|
|
@patch('napps.kytos.flow_manager.main.Main._install_flows') |
|
70
|
|
|
def test_rest_add_and_delete_without_dpid(self, mock_install_flows): |
|
71
|
|
|
"""Test add and delete rest method without dpid.""" |
|
72
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
|
73
|
|
|
|
|
74
|
|
|
for method in ['flows', 'delete']: |
|
75
|
|
|
url = f'{self.API_URL}/v2/{method}' |
|
76
|
|
|
|
|
77
|
|
|
response_1 = api.post(url, json={'data': '123'}) |
|
78
|
|
|
response_2 = api.post(url) |
|
79
|
|
|
|
|
80
|
|
|
self.assertEqual(response_1.status_code, 200) |
|
81
|
|
|
self.assertEqual(response_2.status_code, 404) |
|
82
|
|
|
|
|
83
|
|
|
self.assertEqual(mock_install_flows.call_count, 2) |
|
84
|
|
|
|
|
85
|
|
|
@patch('napps.kytos.flow_manager.main.Main._install_flows') |
|
86
|
|
|
def test_rest_add_and_delete_with_dpid(self, mock_install_flows): |
|
87
|
|
|
"""Test add and delete rest method with dpid.""" |
|
88
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
|
89
|
|
|
|
|
90
|
|
|
for method in ['flows', 'delete']: |
|
91
|
|
|
url_1 = f'{self.API_URL}/v2/{method}/00:00:00:00:00:00:00:01' |
|
92
|
|
|
url_2 = f'{self.API_URL}/v2/{method}/00:00:00:00:00:00:00:02' |
|
93
|
|
|
url_3 = f'{self.API_URL}/v2/{method}/00:00:00:00:00:00:00:03' |
|
94
|
|
|
|
|
95
|
|
|
response_1 = api.post(url_1) |
|
96
|
|
|
response_2 = api.post(url_1, json={'data': '123'}) |
|
97
|
|
|
response_3 = api.post(url_2, json={'data': '123'}) |
|
98
|
|
|
response_4 = api.post(url_3, json={'data': '123'}) |
|
99
|
|
|
|
|
100
|
|
|
self.assertEqual(response_1.status_code, 404) |
|
101
|
|
|
self.assertEqual(response_2.status_code, 200) |
|
102
|
|
|
self.assertEqual(response_3.status_code, 404) |
|
103
|
|
|
self.assertEqual(response_4.status_code, 404) |
|
104
|
|
|
|
|
105
|
|
|
self.assertEqual(mock_install_flows.call_count, 2) |
|
106
|
|
|
|
|
107
|
|
|
def test_get_all_switches_enabled(self): |
|
108
|
|
|
"""Test _get_all_switches_enabled method.""" |
|
109
|
|
|
switches = self.napp._get_all_switches_enabled() |
|
110
|
|
|
|
|
111
|
|
|
self.assertEqual(switches, [self.switch_01]) |
|
112
|
|
|
|
|
113
|
|
|
@patch('napps.kytos.flow_manager.main.Main._send_napp_event') |
|
114
|
|
|
@patch('napps.kytos.flow_manager.main.Main._add_flow_mod_sent') |
|
115
|
|
|
@patch('napps.kytos.flow_manager.main.Main._send_flow_mod') |
|
116
|
|
|
@patch('napps.kytos.flow_manager.main.FlowFactory.get_class') |
|
117
|
|
|
def test_install_flows(self, *args): |
|
118
|
|
|
"""Test _install_flows method.""" |
|
119
|
|
|
(mock_flow_factory, mock_send_flow_mod, mock_add_flow_mod_sent, |
|
120
|
|
|
mock_send_napp_event) = args |
|
121
|
|
|
serializer = MagicMock() |
|
122
|
|
|
flow = MagicMock() |
|
123
|
|
|
flow_mod = MagicMock() |
|
124
|
|
|
|
|
125
|
|
|
flow.as_of_add_flow_mod.return_value = flow_mod |
|
126
|
|
|
serializer.from_dict.return_value = flow |
|
127
|
|
|
mock_flow_factory.return_value = serializer |
|
128
|
|
|
|
|
129
|
|
|
flows_dict = {'flows': [MagicMock()]} |
|
130
|
|
|
switches = [self.switch_01] |
|
131
|
|
|
self.napp._install_flows('add', flows_dict, switches) |
|
132
|
|
|
|
|
133
|
|
|
mock_send_flow_mod.assert_called_with(flow.switch, flow_mod) |
|
134
|
|
|
mock_add_flow_mod_sent.assert_called_with(flow_mod.header.xid, flow) |
|
135
|
|
|
mock_send_napp_event.assert_called_with(self.switch_01, flow, 'add') |
|
136
|
|
|
|
|
137
|
|
|
def test_add_flow_mod_sent(self): |
|
138
|
|
|
"""Test _add_flow_mod_sent method.""" |
|
139
|
|
|
xid = 0 |
|
140
|
|
|
flow = MagicMock() |
|
141
|
|
|
|
|
142
|
|
|
self.napp._add_flow_mod_sent(xid, flow) |
|
143
|
|
|
|
|
144
|
|
|
self.assertEqual(self.napp._flow_mods_sent[xid], flow) |
|
145
|
|
|
|
|
146
|
|
|
@patch('kytos.core.buffers.KytosEventBuffer.put') |
|
147
|
|
|
def test_send_flow_mod(self, mock_buffers_put): |
|
148
|
|
|
"""Test _send_flow_mod method.""" |
|
149
|
|
|
switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
|
150
|
|
|
flow_mod = MagicMock() |
|
151
|
|
|
|
|
152
|
|
|
self.napp._send_flow_mod(switch, flow_mod) |
|
153
|
|
|
|
|
154
|
|
|
mock_buffers_put.assert_called() |
|
155
|
|
|
|
|
156
|
|
|
@patch('kytos.core.buffers.KytosEventBuffer.put') |
|
157
|
|
|
def test_send_napp_event(self, mock_buffers_put): |
|
158
|
|
|
"""Test _send_napp_event method.""" |
|
159
|
|
|
switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
|
160
|
|
|
flow = MagicMock() |
|
161
|
|
|
|
|
162
|
|
|
for command in ['add', 'delete', 'error']: |
|
163
|
|
|
self.napp._send_napp_event(switch, flow, command) |
|
164
|
|
|
|
|
165
|
|
|
self.assertEqual(mock_buffers_put.call_count, 3) |
|
166
|
|
|
|
|
167
|
|
|
@patch('napps.kytos.flow_manager.main.Main._send_napp_event') |
|
168
|
|
|
def test_handle_errors(self, mock_send_napp_event): |
|
169
|
|
|
"""Test handle_errors method.""" |
|
170
|
|
|
flow = MagicMock() |
|
171
|
|
|
self.napp._flow_mods_sent[0] = flow |
|
172
|
|
|
|
|
173
|
|
|
message = MagicMock() |
|
174
|
|
|
message.header.xid.value = 0 |
|
175
|
|
|
event = get_kytos_event_mock(name='.*.of_core.*.ofpt_error', |
|
176
|
|
|
content={'message': message}) |
|
177
|
|
|
self.napp.handle_errors(event) |
|
178
|
|
|
|
|
179
|
|
|
mock_send_napp_event.assert_called_with(flow.switch, flow, 'error') |
|
180
|
|
|
|