Total Complexity | 41 |
Total Lines | 618 |
Duplicated Lines | 38.03 % |
Coverage | 98.91% |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like unit.test_main often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | """Module to test the main napp file.""" |
||
2 | 1 | from unittest.mock import MagicMock, patch |
|
3 | 1 | from kytos.lib.helpers import ( |
|
4 | get_controller_mock, |
||
5 | get_test_client, |
||
6 | get_kytos_event_mock, |
||
7 | get_switch_mock, |
||
8 | ) |
||
9 | 1 | from napps.amlight.kytos_stats.main import Main |
|
10 | |||
11 | |||
12 | # pylint: disable=too-many-public-methods, too-many-lines |
||
13 | 1 | class TestMain: |
|
14 | """Test the Main class.""" |
||
15 | |||
16 | 1 | def setup_method(self): |
|
17 | """Execute steps before each tests.""" |
||
18 | 1 | controller = get_controller_mock() |
|
19 | 1 | self.napp = Main(controller) |
|
20 | 1 | self.api_client = get_test_client(controller, self.napp) |
|
21 | 1 | self.base_endpoint = "amlight/kytos_stats/v1" |
|
22 | |||
23 | 1 | def test_get_event_listeners(self): |
|
24 | """Verify all event listeners registered.""" |
||
25 | 1 | expected_events = [ |
|
26 | 'kytos/of_core.flow_stats.received', |
||
27 | 'kytos/of_core.table_stats.received', |
||
28 | 'kytos/of_core.port_stats', |
||
29 | ] |
||
30 | 1 | actual_events = self.napp.listeners() |
|
31 | |||
32 | 1 | for _event in expected_events: |
|
33 | 1 | assert _event in actual_events |
|
34 | |||
35 | 1 | def test_execute(self): |
|
36 | """Test execute.""" |
||
37 | |||
38 | 1 | def test_shutdown(self): |
|
39 | """Test shutdown.""" |
||
40 | |||
41 | 1 | def test_flow_from_id(self): |
|
42 | """Test flow_from_id function""" |
||
43 | 1 | flow = self._get_mocked_flow_base() |
|
44 | 1 | self.napp.flows_stats_dict = { |
|
45 | flow.id: flow |
||
46 | } |
||
47 | 1 | results = self.napp.flow_from_id(flow.id) |
|
48 | 1 | assert results.id == flow.id |
|
49 | |||
50 | 1 | def test_flow_from_id__fail(self): |
|
51 | """Test flow_from_id function""" |
||
52 | 1 | flow = self._get_mocked_flow_base() |
|
53 | 1 | self.napp.flows_stats_dict = { |
|
54 | flow.id: flow |
||
55 | } |
||
56 | 1 | results = self.napp.flow_from_id('1') |
|
57 | 1 | assert results is None |
|
58 | |||
59 | 1 | def test_flow_from_id__empty(self): |
|
60 | """Test flow_from_id function when flows_stats_dict is empty""" |
||
61 | 1 | self.napp.flows_stats_dict = {} |
|
62 | 1 | results = self.napp.flow_from_id('1') |
|
63 | 1 | assert results is None |
|
64 | |||
65 | 1 | async def test_packet_count_not_found(self): |
|
66 | """Test packet_count rest call with wrong flow_id.""" |
||
67 | 1 | flow_id = "123456789" |
|
68 | 1 | endpoint = f"{self.base_endpoint}/packet_count/{flow_id}" |
|
69 | 1 | response = await self.api_client.get(endpoint) |
|
70 | assert response.status_code == 404 |
||
71 | assert response.json()["description"] == "Flow does not exist" |
||
72 | |||
73 | 1 | View Code Duplication | @patch("napps.amlight.kytos_stats.main.Main.flow_from_id") |
|
|||
74 | 1 | async def test_packet_count(self, mock_from_flow): |
|
75 | """Test packet_count rest call.""" |
||
76 | 1 | flow_id = '1' |
|
77 | 1 | mock_from_flow.return_value = self._get_mocked_flow_base() |
|
78 | |||
79 | 1 | self._patch_switch_flow(flow_id) |
|
80 | 1 | endpoint = f"{self.base_endpoint}/packet_count/{flow_id}" |
|
81 | 1 | response = await self.api_client.get(endpoint) |
|
82 | 1 | assert response.status_code == 200 |
|
83 | 1 | json_response = response.json() |
|
84 | 1 | assert json_response["flow_id"] == flow_id |
|
85 | 1 | assert json_response["packet_counter"] == 40 |
|
86 | 1 | assert json_response["packet_per_second"] == 2.0 |
|
87 | |||
88 | 1 | async def test_bytes_count_not_found(self): |
|
89 | """Test bytes_count rest call with wrong flow_id.""" |
||
90 | 1 | flow_id = "123456789" |
|
91 | 1 | endpoint = f"{self.base_endpoint}/bytes_count/{flow_id}" |
|
92 | 1 | response = await self.api_client.get(endpoint) |
|
93 | assert response.status_code == 404 |
||
94 | assert response.json()["description"] == "Flow does not exist" |
||
95 | |||
96 | 1 | View Code Duplication | @patch("napps.amlight.kytos_stats.main.Main.flow_from_id") |
97 | 1 | async def test_bytes_count(self, mock_from_flow): |
|
98 | """Test bytes_count rest call.""" |
||
99 | 1 | flow_id = '1' |
|
100 | 1 | mock_from_flow.return_value = self._get_mocked_flow_base() |
|
101 | 1 | self._patch_switch_flow(flow_id) |
|
102 | |||
103 | 1 | endpoint = f"{self.base_endpoint}/bytes_count/{flow_id}" |
|
104 | 1 | response = await self.api_client.get(endpoint) |
|
105 | 1 | assert response.status_code == 200 |
|
106 | 1 | json_response = response.json() |
|
107 | 1 | assert json_response["flow_id"] == flow_id |
|
108 | 1 | assert json_response["bytes_counter"] == 10 |
|
109 | 1 | assert json_response["bits_per_second"] == 4.0 |
|
110 | |||
111 | 1 | async def test_packet_count_per_flow_empty(self): |
|
112 | """Test packet_count rest call with a flow that does not exist .""" |
||
113 | 1 | flow_id = "123456789" |
|
114 | 1 | endpoint = f"{self.base_endpoint}/packet_count/per_flow/{flow_id}" |
|
115 | 1 | response = await self.api_client.get(endpoint) |
|
116 | 1 | assert response.status_code == 200 |
|
117 | 1 | assert len(response.json()) == 0 |
|
118 | |||
119 | 1 | View Code Duplication | @patch("napps.amlight.kytos_stats.main.Main.flow_stats_by_dpid_flow_id") |
120 | 1 | async def test_packet_count_per_flow(self, mock_from_flow): |
|
121 | """Test packet_count_per_flow rest call.""" |
||
122 | 1 | flow_info = { |
|
123 | "byte_count": 10, |
||
124 | "duration_sec": 20, |
||
125 | "duration_nsec": 30, |
||
126 | "packet_count": 40, |
||
127 | "cookie": 12310228866111668291, |
||
128 | "match": {"in_port": 1}, |
||
129 | "priority": 32768 |
||
130 | } |
||
131 | 1 | flow_id = '6055f13593fad45e0b4699f49d56b105' |
|
132 | 1 | flow_stats_dict_mock = {flow_id: flow_info} |
|
133 | 1 | dpid = "00:00:00:00:00:00:00:01" |
|
134 | 1 | flow_by_sw = {dpid: flow_stats_dict_mock} |
|
135 | 1 | mock_from_flow.return_value = flow_by_sw |
|
136 | |||
137 | 1 | self._patch_switch_flow(flow_id) |
|
138 | 1 | endpoint = f"{self.base_endpoint}/packet_count/per_flow/{dpid}" |
|
139 | 1 | response = await self.api_client.get(endpoint) |
|
140 | |||
141 | 1 | json_response = response.json() |
|
142 | 1 | assert json_response[0]["flow_id"] == flow_id |
|
143 | 1 | assert json_response[0]["packet_counter"] == 40 |
|
144 | 1 | assert json_response[0]["packet_per_second"] == 2.0 |
|
145 | |||
146 | 1 | async def test_bytes_count_per_flow__empty(self): |
|
147 | """Test bytes_count rest call with a flow that does not exist .""" |
||
148 | 1 | flow_id = "123456789" |
|
149 | 1 | endpoint = f"{self.base_endpoint}/bytes_count/per_flow/{flow_id}" |
|
150 | 1 | response = await self.api_client.get(endpoint) |
|
151 | 1 | assert response.status_code == 200 |
|
152 | 1 | assert len(response.json()) == 0 |
|
153 | |||
154 | 1 | View Code Duplication | @patch("napps.amlight.kytos_stats.main.Main.flow_stats_by_dpid_flow_id") |
155 | 1 | async def test_bytes_count_per_flow(self, mock_from_flow): |
|
156 | """Test bytes_count_per_flow rest call.""" |
||
157 | 1 | flow_info = { |
|
158 | "byte_count": 10, |
||
159 | "duration_sec": 20, |
||
160 | "duration_nsec": 30, |
||
161 | "packet_count": 40, |
||
162 | "cookie": 12310228866111668291, |
||
163 | "match": {"in_port": 1}, |
||
164 | "priority": 32768 |
||
165 | } |
||
166 | 1 | flow_id = '6055f13593fad45e0b4699f49d56b105' |
|
167 | 1 | flow_stats_dict_mock = {flow_id: flow_info} |
|
168 | 1 | dpid = "00:00:00:00:00:00:00:01" |
|
169 | 1 | flow_by_sw = {dpid: flow_stats_dict_mock} |
|
170 | 1 | mock_from_flow.return_value = flow_by_sw |
|
171 | |||
172 | 1 | self._patch_switch_flow(flow_id) |
|
173 | |||
174 | 1 | endpoint = f"{self.base_endpoint}/bytes_count/per_flow/{dpid}" |
|
175 | 1 | response = await self.api_client.get(endpoint) |
|
176 | 1 | assert response.status_code == 200 |
|
177 | |||
178 | 1 | json_response = response.json() |
|
179 | 1 | assert json_response[0]["flow_id"] == flow_id |
|
180 | 1 | assert json_response[0]["bytes_counter"] == 10 |
|
181 | 1 | assert json_response[0]["bits_per_second"] == 4.0 |
|
182 | |||
183 | 1 | View Code Duplication | @patch("napps.amlight.kytos_stats.main.Main.flow_stats_by_dpid_flow_id") |
184 | 1 | async def test_flows_counters_packet(self, mock_from_flow): |
|
185 | """Test flows_counters function for packet""" |
||
186 | 1 | flow_info = { |
|
187 | "byte_count": 10, |
||
188 | "duration_sec": 20, |
||
189 | "duration_nsec": 30, |
||
190 | "packet_count": 40, |
||
191 | "cookie": 12310228866111668291, |
||
192 | "match": {"in_port": 1}, |
||
193 | "priority": 32768 |
||
194 | } |
||
195 | 1 | flow_id = '6055f13593fad45e0b4699f49d56b105' |
|
196 | 1 | flow_stats_dict_mock = {flow_id: flow_info} |
|
197 | 1 | dpid = "00:00:00:00:00:00:00:01" |
|
198 | 1 | flow_by_sw = {dpid: flow_stats_dict_mock} |
|
199 | 1 | mock_from_flow.return_value = flow_by_sw |
|
200 | |||
201 | 1 | endpoint = f"{self.base_endpoint}/packet_count/per_flow/{dpid}" |
|
202 | 1 | response = await self.api_client.get(endpoint) |
|
203 | 1 | assert response.status_code == 200 |
|
204 | 1 | assert len(response.json()) == 1 |
|
205 | |||
206 | 1 | View Code Duplication | @patch("napps.amlight.kytos_stats.main.Main.flow_stats_by_dpid_flow_id") |
207 | 1 | async def test_flows_counters_bytes(self, mock_from_flow): |
|
208 | """Test flows_counters function for bytes""" |
||
209 | 1 | flow_info = { |
|
210 | "byte_count": 10, |
||
211 | "duration_sec": 20, |
||
212 | "duration_nsec": 30, |
||
213 | "packet_count": 40, |
||
214 | "cookie": 12310228866111668291, |
||
215 | "match": {"in_port": 1}, |
||
216 | "priority": 32768 |
||
217 | } |
||
218 | 1 | flow_id = '6055f13593fad45e0b4699f49d56b105' |
|
219 | 1 | flow_stats_dict_mock = {flow_id: flow_info} |
|
220 | 1 | dpid = "00:00:00:00:00:00:00:01" |
|
221 | 1 | flow_by_sw = {dpid: flow_stats_dict_mock} |
|
222 | 1 | mock_from_flow.return_value = flow_by_sw |
|
223 | |||
224 | 1 | endpoint = f"{self.base_endpoint}/bytes_count/per_flow/{dpid}" |
|
225 | 1 | response = await self.api_client.get(endpoint) |
|
226 | 1 | assert response.status_code == 200 |
|
227 | 1 | assert len(response.json()) == 1 |
|
228 | |||
229 | 1 | View Code Duplication | @patch("napps.amlight.kytos_stats.main.Main.flow_stats_by_dpid_flow_id") |
230 | 1 | async def test_flow_stats_by_dpid_flow_id(self, mock_from_flow): |
|
231 | """Test flow_stats rest call.""" |
||
232 | 1 | flow_info = { |
|
233 | "byte_count": 10, |
||
234 | "duration_sec": 20, |
||
235 | "duration_nsec": 30, |
||
236 | "packet_count": 40, |
||
237 | "cookie": 12310228866111668291, |
||
238 | "match": {"in_port": 1}, |
||
239 | "priority": 32768 |
||
240 | } |
||
241 | 1 | flow_stats_dict_mock = {'6055f13593fad45e0b4699f49d56b105': flow_info} |
|
242 | 1 | flow_by_sw = {"00:00:00:00:00:00:00:01": flow_stats_dict_mock} |
|
243 | 1 | mock_from_flow.return_value = flow_by_sw |
|
244 | |||
245 | 1 | endpoint = "/flow/stats?dpid=00:00:00:00:00:00:00:01" |
|
246 | 1 | url = f"{self.base_endpoint}{endpoint}" |
|
247 | 1 | response = await self.api_client.get(url) |
|
248 | 1 | assert response.status_code == 200 |
|
249 | 1 | expected = flow_by_sw |
|
250 | 1 | assert response.json() == expected |
|
251 | |||
252 | 1 | View Code Duplication | @patch("napps.amlight.kytos_stats.main.Main.flow_stats_by_dpid_flow_id") |
253 | 1 | async def test_flow_stats_by_dpid_flow_id_without_dpid(self, |
|
254 | mock_from_flow): |
||
255 | """Test flow_stats rest call.""" |
||
256 | 1 | flow_info = { |
|
257 | "byte_count": 10, |
||
258 | "duration_sec": 20, |
||
259 | "duration_nsec": 30, |
||
260 | "packet_count": 40, |
||
261 | "cookie": 12310228866111668291, |
||
262 | "match": {"in_port": 1}, |
||
263 | "priority": 32768 |
||
264 | } |
||
265 | 1 | flow_stats_dict_mock = {'6055f13593fad45e0b4699f49d56b105': flow_info} |
|
266 | 1 | flow_by_sw = {"00:00:00:00:00:00:00:01": flow_stats_dict_mock} |
|
267 | 1 | mock_from_flow.return_value = flow_by_sw |
|
268 | |||
269 | 1 | endpoint = "/flow/stats" |
|
270 | 1 | url = f"{self.base_endpoint}{endpoint}" |
|
271 | 1 | response = await self.api_client.get(url) |
|
272 | 1 | assert response.status_code == 200 |
|
273 | |||
274 | 1 | expected = flow_by_sw |
|
275 | 1 | assert response.json() == expected |
|
276 | |||
277 | 1 | View Code Duplication | @patch("napps.amlight.kytos_stats.main.Main.flow_stats_by_dpid_flow_id") |
278 | 1 | async def test_flow_stats_by_dpid_flow_id_with_dpid(self, mock_from_flow): |
|
279 | """Test flow_stats rest call.""" |
||
280 | 1 | flow_info = { |
|
281 | "byte_count": 10, |
||
282 | "duration_sec": 20, |
||
283 | "duration_nsec": 30, |
||
284 | "packet_count": 40, |
||
285 | "cookie": 12310228866111668291, |
||
286 | "match": {"in_port": 1}, |
||
287 | "priority": 32768 |
||
288 | } |
||
289 | 1 | flow_stats_dict_mock = {'6055f13593fad45e0b4699f49d56b105': flow_info} |
|
290 | 1 | flow_by_sw = {"00:00:00:00:00:00:00:01": flow_stats_dict_mock} |
|
291 | 1 | mock_from_flow.return_value = flow_by_sw |
|
292 | |||
293 | 1 | endpoint = "/flow/stats?dpid=00:00:00:00:00:00:00:01" |
|
294 | 1 | url = f"{self.base_endpoint}{endpoint}" |
|
295 | 1 | response = await self.api_client.get(url) |
|
296 | 1 | assert response.status_code == 200 |
|
297 | |||
298 | 1 | expected = flow_by_sw |
|
299 | 1 | assert response.json() == expected |
|
300 | |||
301 | 1 | @patch("napps.amlight.kytos_stats.main.Main.flow_stats_by_dpid_flow_id") |
|
302 | 1 | async def test_flow_stats_by_dpid_flow_id_not_found(self, mock_from_flow): |
|
303 | """Test flow_stats rest call.""" |
||
304 | 1 | flow_by_sw = {} |
|
305 | 1 | mock_from_flow.return_value = flow_by_sw |
|
306 | 1 | endpoint = "/flow/stats?dpid=00:00:00:00:00:00:00:01" |
|
307 | 1 | url = f"{self.base_endpoint}{endpoint}" |
|
308 | 1 | response = await self.api_client.get(url) |
|
309 | 1 | assert response.status_code == 200 |
|
310 | 1 | assert len(response.json()) == 0 |
|
311 | |||
312 | 1 | View Code Duplication | @patch("napps.amlight.kytos_stats.main.Main.table_stats_by_dpid_table_id") |
313 | 1 | async def test_table_stats_by_dpid_table_id(self, mock_from_table): |
|
314 | """Test table_stats rest call.""" |
||
315 | 1 | table_info = { |
|
316 | "table_id": 10, |
||
317 | "active_count": 20, |
||
318 | "lookup_count": 30, |
||
319 | "matched_count": 32768 |
||
320 | } |
||
321 | 1 | table_stats_dict_mock = {'10': table_info} |
|
322 | 1 | table_by_sw = {"00:00:00:00:00:00:00:01": table_stats_dict_mock} |
|
323 | 1 | mock_from_table.return_value = table_by_sw |
|
324 | |||
325 | 1 | endpoint = "/table/stats?dpid=00:00:00:00:00:00:00:01&table=10" |
|
326 | 1 | url = f"{self.base_endpoint}{endpoint}" |
|
327 | 1 | response = await self.api_client.get(url) |
|
328 | 1 | assert response.status_code == 200 |
|
329 | 1 | expected = table_by_sw |
|
330 | 1 | assert response.json() == expected |
|
331 | |||
332 | 1 | async def test_port_stats_filter(self): |
|
333 | """Test table_stats rest call.""" |
||
334 | 1 | self.napp.port_stats_dict = { |
|
335 | "0x1": { |
||
336 | 1: { |
||
337 | "port_no": 1, |
||
338 | }, |
||
339 | 2: { |
||
340 | "port_no": 2, |
||
341 | }, |
||
342 | }, |
||
343 | "0x2": { |
||
344 | 99: { |
||
345 | "port_no": 99, |
||
346 | }, |
||
347 | }, |
||
348 | } |
||
349 | 1 | expected_result = {} |
|
350 | 1 | for dpid, sw in self.napp.port_stats_dict.items(): |
|
351 | 1 | expected_result[dpid] = {} |
|
352 | 1 | for port_no, port in sw.items(): |
|
353 | 1 | expected_result[dpid][str(port_no)] = port |
|
354 | |||
355 | 1 | endpoint = "/port/stats" |
|
356 | 1 | url = f"{self.base_endpoint}{endpoint}" |
|
357 | 1 | response = await self.api_client.get(url) |
|
358 | 1 | assert response.status_code == 200 |
|
359 | 1 | assert response.json() == expected_result |
|
360 | |||
361 | 1 | endpoint = "/port/stats?dpid=0x1&port=1" |
|
362 | 1 | url = f"{self.base_endpoint}{endpoint}" |
|
363 | 1 | response = await self.api_client.get(url) |
|
364 | 1 | assert response.status_code == 200 |
|
365 | 1 | data = response.json() |
|
366 | 1 | assert len(data) == 1 |
|
367 | 1 | assert "0x1" in data |
|
368 | 1 | assert len(data["0x1"]) == 1 |
|
369 | 1 | assert "1" in data["0x1"] |
|
370 | |||
371 | 1 | endpoint = "/port/stats?dpid=0x1&port=a" |
|
372 | 1 | url = f"{self.base_endpoint}{endpoint}" |
|
373 | 1 | response = await self.api_client.get(url) |
|
374 | 1 | assert response.status_code == 400 |
|
375 | 1 | data = response.json() |
|
376 | 1 | desc = data["description"] |
|
377 | 1 | assert "'port' value is supposed to be an integer" in desc |
|
378 | |||
379 | 1 | async def test_on_port_stats(self, monkeypatch): |
|
380 | """Test handle_stats_received function.""" |
||
381 | 1 | mock = MagicMock() |
|
382 | 1 | mock.utcnow.return_value = "some_time" |
|
383 | 1 | monkeypatch.setattr("napps.amlight.kytos_stats.main.datetime", mock) |
|
384 | 1 | expected_dict = { |
|
385 | "00:00:00:00:00:00:00:01": { |
||
386 | 1: { |
||
387 | "port_no": 1, |
||
388 | "rx_packets": 0, |
||
389 | "tx_packets": 0, |
||
390 | "rx_bytes": 0, |
||
391 | "tx_bytes": 0, |
||
392 | "rx_dropped": 0, |
||
393 | "tx_dropped": 0, |
||
394 | "rx_errors": 0, |
||
395 | "tx_errors": 0, |
||
396 | "rx_frame_err": 0, |
||
397 | "rx_over_err": 0, |
||
398 | "rx_crc_err": 0, |
||
399 | "collisions": 0, |
||
400 | "duration_sec": 0, |
||
401 | "duration_nsec": 0, |
||
402 | "updated_at": "some_time" |
||
403 | }, |
||
404 | }, |
||
405 | } |
||
406 | |||
407 | 1 | name = "kytos/of_core.port_stats" |
|
408 | 1 | event = get_kytos_event_mock(name=name, content={}) |
|
409 | |||
410 | 1 | await self.napp.on_port_stats(event) |
|
411 | |||
412 | 1 | assert not self.napp.port_stats_dict |
|
413 | |||
414 | 1 | switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
|
415 | 1 | switch.id = switch.dpid |
|
416 | 1 | port_stats = self._get_mocked_port_stat(port_no=1) |
|
417 | 1 | content = {"switch": switch, "port_stats": [port_stats]} |
|
418 | |||
419 | 1 | event = get_kytos_event_mock(name=name, content=content) |
|
420 | |||
421 | 1 | await self.napp.on_port_stats(event) |
|
422 | |||
423 | 1 | assert self.napp.port_stats_dict == expected_dict |
|
424 | |||
425 | 1 | View Code Duplication | @patch("napps.amlight.kytos_stats.main.Main.table_stats_by_dpid_table_id") |
426 | 1 | async def test_table_stats_by_dpid_table_id_without_dpid(self, |
|
427 | mock_from_table): |
||
428 | """Test table_stats rest call.""" |
||
429 | 1 | table_info = { |
|
430 | "table_id": 10, |
||
431 | "active_count": 20, |
||
432 | "lookup_count": 30, |
||
433 | "matched_count": 32768 |
||
434 | } |
||
435 | 1 | table_stats_dict_mock = {'10': table_info} |
|
436 | 1 | table_by_sw = {"00:00:00:00:00:00:00:01": table_stats_dict_mock} |
|
437 | 1 | mock_from_table.return_value = table_by_sw |
|
438 | |||
439 | 1 | endpoint = "/table/stats" |
|
440 | 1 | url = f"{self.base_endpoint}{endpoint}" |
|
441 | 1 | response = await self.api_client.get(url) |
|
442 | 1 | assert response.status_code == 200 |
|
443 | |||
444 | 1 | expected = table_by_sw |
|
445 | 1 | assert response.json() == expected |
|
446 | |||
447 | 1 | @patch("napps.amlight.kytos_stats.main.Main.table_stats_by_dpid_table_id") |
|
448 | 1 | async def test_table_stats_by_dpid_table_id_not_found(self, |
|
449 | mock_from_table): |
||
450 | """Test table_stats rest call.""" |
||
451 | 1 | table_by_sw = {} |
|
452 | 1 | mock_from_table.return_value = table_by_sw |
|
453 | 1 | endpoint = "/flow/stats?dpid=00:00:00:00:00:00:00:01" |
|
454 | 1 | url = f"{self.base_endpoint}{endpoint}" |
|
455 | 1 | response = await self.api_client.get(url) |
|
456 | 1 | assert response.status_code == 200 |
|
457 | 1 | assert len(response.json()) == 0 |
|
458 | |||
459 | 1 | def _patch_switch_flow(self, flow_id): |
|
460 | """Helper method to patch controller to return switch/flow data.""" |
||
461 | # patching the flow_stats object in the switch |
||
462 | 1 | flow = self._get_mocked_flow_stats() |
|
463 | 1 | flow.id = flow_id |
|
464 | 1 | switch = MagicMock() |
|
465 | 1 | self.napp.controller.switches = {"1": switch} |
|
466 | 1 | self.napp.controller.get_switch_by_dpid = MagicMock() |
|
467 | 1 | self.napp.controller.get_switch_by_dpid.return_value = switch |
|
468 | |||
469 | 1 | def _get_mocked_flow_stats(self): |
|
470 | """Helper method to create a mock flow_stats object.""" |
||
471 | 1 | flow_stats = MagicMock() |
|
472 | 1 | flow_stats.id = 123 |
|
473 | 1 | flow_stats.byte_count = 10 |
|
474 | 1 | flow_stats.duration_sec = 20 |
|
475 | 1 | flow_stats.duration_nsec = 30 |
|
476 | 1 | flow_stats.packet_count = 40 |
|
477 | 1 | return flow_stats |
|
478 | |||
479 | 1 | def _get_mocked_multipart_replies_flows(self): |
|
480 | """Helper method to create mock multipart replies flows""" |
||
481 | 1 | flow = self._get_mocked_flow_base() |
|
482 | |||
483 | 1 | instruction = MagicMock() |
|
484 | 1 | flow.instructions = [instruction] |
|
485 | |||
486 | 1 | replies_flows = [flow] |
|
487 | 1 | return replies_flows |
|
488 | |||
489 | 1 | def _get_mocked_multipart_replies_tables(self): |
|
490 | """Helper method to create mock multipart replies tables""" |
||
491 | 1 | table = MagicMock() |
|
492 | 1 | table.table_id = 10 |
|
493 | 1 | table.active_count = 0 |
|
494 | 1 | table.lookup_count = 0 |
|
495 | 1 | table.matched_count = 0 |
|
496 | |||
497 | 1 | replies_tables = [table] |
|
498 | 1 | return replies_tables |
|
499 | |||
500 | 1 | def _get_mocked_port_stat(self, **kwargs): |
|
501 | """Helper method to create mock port stats.""" |
||
502 | 1 | port_stats = MagicMock() |
|
503 | 1 | port_stats.port_no.value = kwargs.get("port_no", 0) |
|
504 | 1 | port_stats.rx_packets.value = kwargs.get("rx_packets", 0) |
|
505 | 1 | port_stats.tx_packets.value = kwargs.get("tx_packets", 0) |
|
506 | 1 | port_stats.rx_bytes.value = kwargs.get("rx_bytes", 0) |
|
507 | 1 | port_stats.tx_bytes.value = kwargs.get("tx_bytes", 0) |
|
508 | 1 | port_stats.rx_dropped.value = kwargs.get("rx_dropped", 0) |
|
509 | 1 | port_stats.tx_dropped.value = kwargs.get("tx_dropped", 0) |
|
510 | 1 | port_stats.rx_errors.value = kwargs.get("rx_errors", 0) |
|
511 | 1 | port_stats.tx_errors.value = kwargs.get("tx_errors", 0) |
|
512 | 1 | port_stats.rx_frame_err.value = kwargs.get("rx_frame_err", 0) |
|
513 | 1 | port_stats.rx_over_err.value = kwargs.get("rx_over_err", 0) |
|
514 | 1 | port_stats.rx_crc_err.value = kwargs.get("rx_crc_err", 0) |
|
515 | 1 | port_stats.collisions.value = kwargs.get("collisions", 0) |
|
516 | 1 | port_stats.duration_sec.value = kwargs.get("duration_sec", 0) |
|
517 | 1 | port_stats.duration_nsec.value = kwargs.get("duration_nsec", 0) |
|
518 | 1 | return port_stats |
|
519 | |||
520 | 1 | def _get_mocked_flow_base(self): |
|
521 | """Helper method to create a mock flow object.""" |
||
522 | 1 | flow = MagicMock() |
|
523 | 1 | flow.id = 456 |
|
524 | 1 | flow.switch = None |
|
525 | 1 | flow.table_id = None |
|
526 | 1 | flow.match = None |
|
527 | 1 | flow.priority = None |
|
528 | 1 | flow.idle_timeout = None |
|
529 | 1 | flow.hard_timeout = None |
|
530 | 1 | flow.cookie = None |
|
531 | 1 | flow.stats = self._get_mocked_flow_stats() |
|
532 | 1 | return flow |
|
533 | |||
534 | 1 | @patch("napps.amlight.kytos_stats.main.Main.handle_stats_reply_received") |
|
535 | 1 | def test_handle_stats_received(self, mock_handle_stats): |
|
536 | """Test handle_stats_received function.""" |
||
537 | |||
538 | 1 | switch_v0x04 = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
|
539 | 1 | replies_flows = self._get_mocked_multipart_replies_flows() |
|
540 | 1 | name = "kytos/of_core.flow_stats.received" |
|
541 | 1 | content = {"switch": switch_v0x04, "replies_flows": replies_flows} |
|
542 | |||
543 | 1 | event = get_kytos_event_mock(name=name, content=content) |
|
544 | |||
545 | 1 | self.napp.handle_stats_received(event) |
|
546 | 1 | mock_handle_stats.assert_called_once() |
|
547 | |||
548 | 1 | @patch("napps.amlight.kytos_stats.main.Main.handle_stats_reply_received") |
|
549 | 1 | def test_handle_stats_received__fail(self, mock_handle_stats): |
|
550 | """Test handle_stats_received function for |
||
551 | fail when replies_flows is not in content.""" |
||
552 | |||
553 | 1 | switch_v0x04 = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
|
554 | 1 | name = "kytos/of_core.flow_stats.received" |
|
555 | 1 | content = {"switch": switch_v0x04} |
|
556 | |||
557 | 1 | event = get_kytos_event_mock(name=name, content=content) |
|
558 | |||
559 | 1 | self.napp.handle_stats_received(event) |
|
560 | 1 | mock_handle_stats.assert_not_called() |
|
561 | |||
562 | 1 | def test_handle_stats_reply_received(self): |
|
563 | """Test handle_stats_reply_received call.""" |
||
564 | |||
565 | 1 | flows_mock = self._get_mocked_multipart_replies_flows() |
|
566 | 1 | self.napp.handle_stats_reply_received(flows_mock) |
|
567 | |||
568 | 1 | assert list(self.napp.flows_stats_dict.values())[0].id == 456 |
|
569 | |||
570 | 1 | @patch("napps.amlight.kytos_stats.main.Main.handle_table_stats_received") |
|
571 | 1 | def test_handle_table_stats_received(self, mock_handle_stats): |
|
572 | """Test handle_table_stats_received function.""" |
||
573 | |||
574 | 1 | switch_v0x04 = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
|
575 | 1 | replies_tables = self._get_mocked_multipart_replies_tables() |
|
576 | 1 | name = "kytos/of_core.table_stats.received" |
|
577 | 1 | content = {"switch": switch_v0x04, "replies_tables": replies_tables} |
|
578 | |||
579 | 1 | event = get_kytos_event_mock(name=name, content=content) |
|
580 | |||
581 | 1 | self.napp.handle_table_stats_received(event) |
|
582 | 1 | mock_handle_stats.assert_called_once() |
|
583 | |||
584 | 1 | def test_handle_table_stats_reply_received(self): |
|
585 | """Test handle_table_stats_reply_received call.""" |
||
586 | |||
587 | 1 | tables_mock = self._get_mocked_multipart_replies_tables() |
|
588 | 1 | self.napp.handle_table_stats_reply_received(tables_mock) |
|
589 | 1 | table = list(self.napp.tables_stats_dict.values())[0] |
|
590 | 1 | assert list(table.keys())[0] == 10 |
|
591 | |||
592 | 1 | @patch("napps.amlight.kytos_stats.main.Main.flow_stats_by_dpid_flow_id") |
|
593 | 1 | async def test_flows_counters_div_zero(self, mock_from_flow): |
|
594 | """Test that there is no error due to division by zero.""" |
||
595 | 1 | flow_info = { |
|
596 | "byte_count": 10, |
||
597 | "packet_count": 20, |
||
598 | "duration_sec": 0 |
||
599 | } |
||
600 | 1 | flow_id = '6055f13593fad45e0b4699f49d56b105' |
|
601 | 1 | flow_stats_dict_mock = {flow_id: flow_info} |
|
602 | 1 | dpid = "00:00:00:00:00:00:00:01" |
|
603 | 1 | flow_by_sw = {dpid: flow_stats_dict_mock} |
|
604 | 1 | mock_from_flow.return_value = flow_by_sw |
|
605 | |||
606 | 1 | self._patch_switch_flow(flow_id) |
|
607 | 1 | endpoint = f"{self.base_endpoint}/packet_count/per_flow/{dpid}" |
|
608 | 1 | response = await self.api_client.get(endpoint) |
|
609 | 1 | response = response.json() |
|
610 | 1 | assert response[0]["flow_id"] == flow_id |
|
611 | 1 | assert response[0]["packet_per_second"] == 0 |
|
612 | |||
613 | 1 | endpoint = f"{self.base_endpoint}/bytes_count/per_flow/{dpid}" |
|
614 | 1 | response = await self.api_client.get(endpoint) |
|
615 | 1 | response = response.json() |
|
616 | 1 | assert response[0]["flow_id"] == flow_id |
|
617 | assert response[0]["bits_per_second"] == 0 |
||
618 |