1
|
|
|
"""Module to test the main napp file.""" |
2
|
|
|
import json |
3
|
|
|
from unittest import TestCase |
4
|
|
|
from unittest.mock import MagicMock, patch |
5
|
|
|
from kytos.lib.helpers import ( |
6
|
|
|
get_controller_mock, |
7
|
|
|
get_test_client, |
8
|
|
|
get_kytos_event_mock, |
9
|
|
|
get_switch_mock, |
10
|
|
|
) |
11
|
|
|
from napps.amlight.flow_stats.main import Main |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
# pylint: disable=too-many-public-methods, too-many-lines |
15
|
|
|
class TestMain(TestCase): |
16
|
|
|
"""Test the Main class.""" |
17
|
|
|
|
18
|
|
|
def setUp(self): |
19
|
|
|
"""Execute steps before each tests. |
20
|
|
|
|
21
|
|
|
Set the server_name_url_url from amlight/flow_stats |
22
|
|
|
""" |
23
|
|
|
self.server_name_url = "http://localhost:8181/api/amlight/flow_stats" |
24
|
|
|
self.napp = Main(get_controller_mock()) |
25
|
|
|
|
26
|
|
|
@staticmethod |
27
|
|
|
def get_napp_urls(napp): |
28
|
|
|
"""Return the amlight/flow_stats urls. |
29
|
|
|
|
30
|
|
|
The urls will be like: |
31
|
|
|
|
32
|
|
|
urls = [ |
33
|
|
|
(options, methods, url) |
34
|
|
|
] |
35
|
|
|
|
36
|
|
|
""" |
37
|
|
|
controller = napp.controller |
38
|
|
|
controller.api_server.register_napp_endpoints(napp) |
39
|
|
|
|
40
|
|
|
urls = [] |
41
|
|
|
for rule in controller.api_server.app.url_map.iter_rules(): |
42
|
|
|
options = {} |
43
|
|
|
for arg in rule.arguments: |
44
|
|
|
options[arg] = f"[{0}]".format(arg) |
45
|
|
|
|
46
|
|
|
if f"{napp.username}/{napp.name}" in str(rule): |
47
|
|
|
urls.append((options, rule.methods, f"{str(rule)}")) |
48
|
|
|
|
49
|
|
|
return urls |
50
|
|
|
|
51
|
|
|
def test_verify_api_urls(self): |
52
|
|
|
"""Verify all APIs registered.""" |
53
|
|
|
|
54
|
|
|
expected_urls = [ |
55
|
|
|
( |
56
|
|
|
{"dpid": "[dpid]"}, |
57
|
|
|
{"OPTIONS", "HEAD", "GET"}, |
58
|
|
|
"/api/amlight/flow_stats/v1/flow/stats/", |
59
|
|
|
), |
60
|
|
|
( |
61
|
|
|
{"flow_id": "[flow_id]"}, |
62
|
|
|
{"OPTIONS", "HEAD", "GET"}, |
63
|
|
|
"/api/amlight/flow_stats/packet_count/", |
64
|
|
|
), |
65
|
|
|
( |
66
|
|
|
{"flow_id": "[flow_id]"}, |
67
|
|
|
{"OPTIONS", "HEAD", "GET"}, |
68
|
|
|
"/api/amlight/flow_stats/bytes_count/", |
69
|
|
|
), |
70
|
|
|
( |
71
|
|
|
{"dpid": "[dpid]"}, |
72
|
|
|
{"OPTIONS", "HEAD", "GET"}, |
73
|
|
|
"/api/amlight/flow_stats/packet_count/per_flow/", |
74
|
|
|
), |
75
|
|
|
( |
76
|
|
|
{"dpid": "[dpid]"}, |
77
|
|
|
{"OPTIONS", "HEAD", "GET"}, |
78
|
|
|
"/api/amlight/flow_stats/packet_count/sum/", |
79
|
|
|
), |
80
|
|
|
( |
81
|
|
|
{"dpid": "[dpid]"}, |
82
|
|
|
{"OPTIONS", "HEAD", "GET"}, |
83
|
|
|
"/api/amlight/flow_stats/bytes_count/per_flow/", |
84
|
|
|
), |
85
|
|
|
( |
86
|
|
|
{"dpid": "[dpid]"}, |
87
|
|
|
{"OPTIONS", "HEAD", "GET"}, |
88
|
|
|
"/api/amlight/flow_stats/bytes_count/sum/", |
89
|
|
|
), |
90
|
|
|
] |
91
|
|
|
urls = self.get_napp_urls(self.napp) |
92
|
|
|
assert len(expected_urls) == len(urls) |
93
|
|
|
|
94
|
|
|
def test_packet_count__fail(self): |
95
|
|
|
"""Test bytes_count rest call with wrong flow_id.""" |
96
|
|
|
flow_id = "123456789" |
97
|
|
|
rest_name = "packet_count" |
98
|
|
|
response = self._get_rest_response(rest_name, flow_id) |
99
|
|
|
|
100
|
|
|
assert response.data == b"Flow does not exist" |
101
|
|
|
|
102
|
|
View Code Duplication |
@patch("napps.amlight.flow_stats.main.Main.flow_from_id") |
|
|
|
|
103
|
|
|
def test_packet_count(self, mock_from_flow): |
104
|
|
|
"""Test packet_count rest call.""" |
105
|
|
|
flow_id = '1' |
106
|
|
|
dpid_id = '1' |
107
|
|
|
mock_from_flow.return_value = self._get_mocked_flow_base() |
108
|
|
|
|
109
|
|
|
rest_name = "packet_count" |
110
|
|
|
self._patch_switch_flow(flow_id) |
111
|
|
|
|
112
|
|
|
response = self._get_rest_response(rest_name, dpid_id) |
113
|
|
|
json_response = json.loads(response.data) |
114
|
|
|
assert json_response["flow_id"] == flow_id |
115
|
|
|
assert json_response["packet_counter"] == 40 |
116
|
|
|
assert json_response["packet_per_second"] == 2.0 |
117
|
|
|
|
118
|
|
|
def test_bytes_count__fail(self): |
119
|
|
|
"""Test bytes_count rest call with wrong flow_id.""" |
120
|
|
|
flow_id = "123456789" |
121
|
|
|
rest_name = "bytes_count" |
122
|
|
|
response = self._get_rest_response(rest_name, flow_id) |
123
|
|
|
|
124
|
|
|
assert response.data == b"Flow does not exist" |
125
|
|
|
|
126
|
|
View Code Duplication |
@patch("napps.amlight.flow_stats.main.Main.flow_from_id") |
|
|
|
|
127
|
|
|
def test_bytes_count(self, mock_from_flow): |
128
|
|
|
"""Test bytes_count rest call.""" |
129
|
|
|
flow_id = '1' |
130
|
|
|
dpid_id = '1' |
131
|
|
|
mock_from_flow.return_value = self._get_mocked_flow_base() |
132
|
|
|
|
133
|
|
|
rest_name = "bytes_count" |
134
|
|
|
self._patch_switch_flow(flow_id) |
135
|
|
|
|
136
|
|
|
response = self._get_rest_response(rest_name, dpid_id) |
137
|
|
|
json_response = json.loads(response.data) |
138
|
|
|
assert json_response["flow_id"] == flow_id |
139
|
|
|
assert json_response["bytes_counter"] == 10 |
140
|
|
|
assert json_response["bits_per_second"] == 4.0 |
141
|
|
|
|
142
|
|
View Code Duplication |
@patch("napps.amlight.flow_stats.main.Main.flow_stats_by_dpid_flow_id") |
|
|
|
|
143
|
|
|
def test_packet_count_per_flow(self, mock_from_flow): |
144
|
|
|
"""Test packet_count_per_flow rest call.""" |
145
|
|
|
flow_stats = { |
146
|
|
|
'byte_count': 10, |
147
|
|
|
'duration_sec': 20, |
148
|
|
|
'duration_nsec': 30, |
149
|
|
|
'packet_count': 40 |
150
|
|
|
} |
151
|
|
|
flow_id = '6055f13593fad45e0b4699f49d56b105' |
152
|
|
|
flow_stats_dict_mock = {flow_id: flow_stats} |
153
|
|
|
dpid_id = "00:00:00:00:00:00:00:01" |
154
|
|
|
flow_by_sw = {dpid_id: flow_stats_dict_mock} |
155
|
|
|
mock_from_flow.return_value = flow_by_sw |
156
|
|
|
|
157
|
|
|
rest_name = "packet_count/per_flow" |
158
|
|
|
self._patch_switch_flow(flow_id) |
159
|
|
|
|
160
|
|
|
mock_from_flow.return_value = flow_by_sw |
161
|
|
|
response = self._get_rest_response(rest_name, dpid_id) |
162
|
|
|
json_response = json.loads(response.data) |
163
|
|
|
assert json_response[0]["flow_id"] == flow_id |
164
|
|
|
assert json_response[0]["packet_counter"] == 40 |
165
|
|
|
assert json_response[0]["packet_per_second"] == 2.0 |
166
|
|
|
|
167
|
|
View Code Duplication |
@patch("napps.amlight.flow_stats.main.Main.flow_stats_by_dpid_flow_id") |
|
|
|
|
168
|
|
|
def test_packet_count_sum(self, mock_from_flow): |
169
|
|
|
"""Test packet_count_sum rest call.""" |
170
|
|
|
flow_stats = { |
171
|
|
|
'byte_count': 10, |
172
|
|
|
'duration_sec': 20, |
173
|
|
|
'duration_nsec': 30, |
174
|
|
|
'packet_count': 40 |
175
|
|
|
} |
176
|
|
|
flow_id = '6055f13593fad45e0b4699f49d56b105' |
177
|
|
|
flow_stats_dict_mock = {flow_id: flow_stats} |
178
|
|
|
dpid_id = "00:00:00:00:00:00:00:01" |
179
|
|
|
flow_by_sw = {dpid_id: flow_stats_dict_mock} |
180
|
|
|
mock_from_flow.return_value = flow_by_sw |
181
|
|
|
|
182
|
|
|
rest_name = "packet_count/sum" |
183
|
|
|
self._patch_switch_flow(flow_id) |
184
|
|
|
|
185
|
|
|
mock_from_flow.return_value = flow_by_sw |
186
|
|
|
response = self._get_rest_response(rest_name, dpid_id) |
187
|
|
|
json_response = json.loads(response.data) |
188
|
|
|
assert json_response == 40 |
189
|
|
|
|
190
|
|
View Code Duplication |
@patch("napps.amlight.flow_stats.main.Main.flow_stats_by_dpid_flow_id") |
|
|
|
|
191
|
|
|
def test_bytes_count_sum(self, mock_from_flow): |
192
|
|
|
"""Test bytes_count_sum rest call.""" |
193
|
|
|
flow_stats = { |
194
|
|
|
'byte_count': 10, |
195
|
|
|
'duration_sec': 20, |
196
|
|
|
'duration_nsec': 30, |
197
|
|
|
'packet_count': 40 |
198
|
|
|
} |
199
|
|
|
flow_id = '6055f13593fad45e0b4699f49d56b105' |
200
|
|
|
flow_stats_dict_mock = {flow_id: flow_stats} |
201
|
|
|
dpid_id = "00:00:00:00:00:00:00:01" |
202
|
|
|
flow_by_sw = {dpid_id: flow_stats_dict_mock} |
203
|
|
|
mock_from_flow.return_value = flow_by_sw |
204
|
|
|
|
205
|
|
|
rest_name = "bytes_count/sum" |
206
|
|
|
self._patch_switch_flow(flow_id) |
207
|
|
|
|
208
|
|
|
mock_from_flow.return_value = flow_by_sw |
209
|
|
|
response = self._get_rest_response(rest_name, dpid_id) |
210
|
|
|
json_response = json.loads(response.data) |
211
|
|
|
assert json_response == 10 |
212
|
|
|
|
213
|
|
View Code Duplication |
@patch("napps.amlight.flow_stats.main.Main.flow_stats_by_dpid_flow_id") |
|
|
|
|
214
|
|
|
def test_bytes_count_per_flow(self, mock_from_flow): |
215
|
|
|
"""Test bytes_count_per_flow rest call.""" |
216
|
|
|
flow_stats = { |
217
|
|
|
'byte_count': 10, |
218
|
|
|
'duration_sec': 20, |
219
|
|
|
'duration_nsec': 30, |
220
|
|
|
'packet_count': 40 |
221
|
|
|
} |
222
|
|
|
flow_id = '6055f13593fad45e0b4699f49d56b105' |
223
|
|
|
flow_stats_dict_mock = {flow_id: flow_stats} |
224
|
|
|
dpid_id = "00:00:00:00:00:00:00:01" |
225
|
|
|
flow_by_sw = {dpid_id: flow_stats_dict_mock} |
226
|
|
|
mock_from_flow.return_value = flow_by_sw |
227
|
|
|
|
228
|
|
|
rest_name = "bytes_count/per_flow" |
229
|
|
|
self._patch_switch_flow(flow_id) |
230
|
|
|
|
231
|
|
|
mock_from_flow.return_value = flow_by_sw |
232
|
|
|
response = self._get_rest_response(rest_name, dpid_id) |
233
|
|
|
json_response = json.loads(response.data) |
234
|
|
|
assert json_response[0]["flow_id"] == flow_id |
235
|
|
|
assert json_response[0]["bytes_counter"] == 10 |
236
|
|
|
assert json_response[0]["bits_per_second"] == 4.0 |
237
|
|
|
|
238
|
|
View Code Duplication |
@patch("napps.amlight.flow_stats.main.Main.flow_stats_by_dpid_flow_id") |
|
|
|
|
239
|
|
|
def test_flow_stats_by_dpid_flow_id(self, mock_from_flow): |
240
|
|
|
"""Test flow_stats rest call.""" |
241
|
|
|
flow_stats = { |
242
|
|
|
'byte_count': 148, |
243
|
|
|
'duration_sec': 1589, |
244
|
|
|
'duration_nsec': 556000000, |
245
|
|
|
'packet_count': 2 |
246
|
|
|
} |
247
|
|
|
flow_stats_dict_mock = {'6055f13593fad45e0b4699f49d56b105': flow_stats} |
248
|
|
|
flow_by_sw = {"00:00:00:00:00:00:00:01": flow_stats_dict_mock} |
249
|
|
|
mock_from_flow.return_value = flow_by_sw |
250
|
|
|
|
251
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
252
|
|
|
endpoint = "/v1/flow/stats?dpid=00:00:00:00:00:00:00:01" |
253
|
|
|
url = f"{self.server_name_url}"+endpoint |
254
|
|
|
|
255
|
|
|
response = api.get(url) |
256
|
|
|
expected = flow_by_sw |
257
|
|
|
assert response.json == expected |
258
|
|
|
assert response.status_code == 200 |
259
|
|
|
|
260
|
|
View Code Duplication |
@patch("napps.amlight.flow_stats.main.Main.flow_stats_by_dpid_flow_id") |
|
|
|
|
261
|
|
|
def test_flow_stats_by_dpid_flow_id_with_dpid(self, mock_from_flow): |
262
|
|
|
"""Test flow_stats rest call.""" |
263
|
|
|
flow_stats = { |
264
|
|
|
'byte_count': 148, |
265
|
|
|
'duration_sec': 1589, |
266
|
|
|
'duration_nsec': 556000000, |
267
|
|
|
'packet_count': 2 |
268
|
|
|
} |
269
|
|
|
flow_stats_dict_mock = {'6055f13593fad45e0b4699f49d56b105': flow_stats} |
270
|
|
|
flow_by_sw = {"00:00:00:00:00:00:00:01": flow_stats_dict_mock} |
271
|
|
|
mock_from_flow.return_value = flow_by_sw |
272
|
|
|
|
273
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
274
|
|
|
endpoint = "/v1/flow/stats?dpid=00:00:00:00:00:00:00:01" |
275
|
|
|
url = f"{self.server_name_url}"+endpoint |
276
|
|
|
|
277
|
|
|
response = api.get(url) |
278
|
|
|
expected = flow_by_sw |
279
|
|
|
assert response.json == expected |
280
|
|
|
assert response.status_code == 200 |
281
|
|
|
|
282
|
|
|
def _patch_switch_flow(self, flow_id): |
283
|
|
|
"""Helper method to patch controller to return switch/flow data.""" |
284
|
|
|
# patching the flow_stats object in the switch |
285
|
|
|
flow = self._get_mocked_flow_stats() |
286
|
|
|
flow.id = flow_id |
287
|
|
|
switch = MagicMock() |
288
|
|
|
self.napp.controller.switches = {"1": switch} |
289
|
|
|
self.napp.controller.get_switch_by_dpid = MagicMock() |
290
|
|
|
self.napp.controller.get_switch_by_dpid.return_value = switch |
291
|
|
|
|
292
|
|
|
def _get_rest_response(self, rest_name, url_id): |
293
|
|
|
"""Helper method to call a rest endpoint.""" |
294
|
|
|
# call rest |
295
|
|
|
api = get_test_client(get_controller_mock(), self.napp) |
296
|
|
|
url = f"{self.server_name_url}/{rest_name}/{url_id}" |
297
|
|
|
response = api.get(url, content_type="application/json") |
298
|
|
|
|
299
|
|
|
return response |
300
|
|
|
|
301
|
|
|
def _get_mocked_flow_stats(self): |
302
|
|
|
"""Helper method to create a mock flow_stats object.""" |
303
|
|
|
flow_stats = MagicMock() |
304
|
|
|
flow_stats.id = 123 |
305
|
|
|
flow_stats.byte_count = 10 |
306
|
|
|
flow_stats.duration_sec = 20 |
307
|
|
|
flow_stats.duration_nsec = 30 |
308
|
|
|
flow_stats.packet_count = 40 |
309
|
|
|
return flow_stats |
310
|
|
|
|
311
|
|
|
def _get_mocked_multipart_replies_flows(self): |
312
|
|
|
"""Helper method to create mock multipart replies flows""" |
313
|
|
|
flow = self._get_mocked_flow_base() |
314
|
|
|
|
315
|
|
|
instruction = MagicMock() |
316
|
|
|
flow.instructions = [instruction] |
317
|
|
|
|
318
|
|
|
replies_flows = [flow] |
319
|
|
|
return replies_flows |
320
|
|
|
|
321
|
|
|
def _get_mocked_flow_base(self): |
322
|
|
|
"""Helper method to create a mock flow object.""" |
323
|
|
|
flow = MagicMock() |
324
|
|
|
flow.id = 456 |
325
|
|
|
flow.switch = None |
326
|
|
|
flow.table_id = None |
327
|
|
|
flow.match = None |
328
|
|
|
flow.priority = None |
329
|
|
|
flow.idle_timeout = None |
330
|
|
|
flow.hard_timeout = None |
331
|
|
|
flow.cookie = None |
332
|
|
|
flow.stats = self._get_mocked_flow_stats() |
333
|
|
|
return flow |
334
|
|
|
|
335
|
|
|
@patch("napps.amlight.flow_stats.main.Main.handle_stats_reply_received") |
336
|
|
|
def test_handle_stats_received(self, mock_handle_stats): |
337
|
|
|
"""Test handle_stats_received function.""" |
338
|
|
|
|
339
|
|
|
switch_v0x04 = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
340
|
|
|
replies_flows = self._get_mocked_multipart_replies_flows() |
341
|
|
|
name = "kytos/of_core.flow_stats.received" |
342
|
|
|
content = {"switch": switch_v0x04, "replies_flows": replies_flows} |
343
|
|
|
|
344
|
|
|
event = get_kytos_event_mock(name=name, content=content) |
345
|
|
|
|
346
|
|
|
self.napp.handle_stats_received(event) |
347
|
|
|
mock_handle_stats.assert_called_once() |
348
|
|
|
|
349
|
|
|
@patch("napps.amlight.flow_stats.main.Main.handle_stats_reply_received") |
350
|
|
|
def test_handle_stats_received_fail(self, mock_handle_stats): |
351
|
|
|
"""Test handle_stats_received function for |
352
|
|
|
fail when replies_flows is not in content.""" |
353
|
|
|
|
354
|
|
|
switch_v0x04 = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
355
|
|
|
name = "kytos/of_core.flow_stats.received" |
356
|
|
|
content = {"switch": switch_v0x04} |
357
|
|
|
|
358
|
|
|
event = get_kytos_event_mock(name=name, content=content) |
359
|
|
|
|
360
|
|
|
self.napp.handle_stats_received(event) |
361
|
|
|
mock_handle_stats.assert_not_called() |
362
|
|
|
|
363
|
|
|
def test_handle_stats_reply_received(self): |
364
|
|
|
"""Test handle_stats_reply_received call.""" |
365
|
|
|
|
366
|
|
|
flows_mock = self._get_mocked_multipart_replies_flows() |
367
|
|
|
self.napp.handle_stats_reply_received(flows_mock) |
368
|
|
|
|
369
|
|
|
assert list(self.napp.flows_stats_dict.values())[0].id == 456 |
370
|
|
|
|