Total Complexity | 40 |
Total Lines | 609 |
Duplicated Lines | 24.47 % |
Coverage | 100% |
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 build.tests.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 | """Test Main methods.""" |
||
2 | 1 | from unittest.mock import AsyncMock, MagicMock, call, patch |
|
3 | |||
4 | 1 | import pytest |
|
5 | 1 | from httpx import RequestError |
|
6 | 1 | from kytos.core.events import KytosEvent |
|
7 | 1 | from kytos.lib.helpers import (get_controller_mock, get_interface_mock, |
|
8 | get_kytos_event_mock, get_switch_mock, |
||
9 | get_test_client) |
||
10 | 1 | from napps.kytos.of_lldp.utils import get_cookie |
|
11 | 1 | from tenacity import RetryError |
|
12 | |||
13 | 1 | from tests.helpers import get_topology_mock |
|
14 | |||
15 | |||
16 | 1 | View Code Duplication | @patch('kytos.core.controller.Controller.get_switch_by_dpid') |
|
|||
17 | 1 | @patch('napps.kytos.of_lldp.main.Main._unpack_non_empty') |
|
18 | 1 | @patch('napps.kytos.of_lldp.main.UBInt32') |
|
19 | 1 | @patch('napps.kytos.of_lldp.main.DPID') |
|
20 | 1 | @patch('napps.kytos.of_lldp.main.LLDP') |
|
21 | 1 | @patch('napps.kytos.of_lldp.main.Ethernet') |
|
22 | 1 | async def test_on_ofpt_packet_in(*args): |
|
23 | """Test on_ofpt_packet_in.""" |
||
24 | 1 | (mock_ethernet, mock_lldp, mock_dpid, mock_ubint32, |
|
25 | mock_unpack_non_empty, mock_get_switch_by_dpid) = args |
||
26 | |||
27 | # pylint: disable=bad-option-value, import-outside-toplevel |
||
28 | 1 | from napps.kytos.of_lldp.main import Main |
|
29 | 1 | Main.get_liveness_controller = MagicMock() |
|
30 | 1 | topology = get_topology_mock() |
|
31 | 1 | controller = get_controller_mock() |
|
32 | 1 | controller.buffers.app.aput = AsyncMock() |
|
33 | 1 | controller.switches = topology.switches |
|
34 | 1 | napp = Main(controller) |
|
35 | 1 | napp.loop_manager.process_if_looped = AsyncMock() |
|
36 | 1 | napp.liveness_manager.consume_hello_if_enabled = AsyncMock() |
|
37 | |||
38 | 1 | switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
|
39 | 1 | message = MagicMock(in_port=1, data='data') |
|
40 | 1 | event = KytosEvent('ofpt_packet_in', content={'source': switch.connection, |
|
41 | 'message': message}) |
||
42 | |||
43 | 1 | mocked, ethernet, lldp, dpid, port_b = [MagicMock() for _ in range(5)] |
|
44 | 1 | mocked.value = 1 |
|
45 | 1 | mock_ubint32.return_value = mocked |
|
46 | 1 | ethernet.ether_type = 0x88CC |
|
47 | 1 | ethernet.data = 'eth_data' |
|
48 | 1 | lldp.chassis_id.sub_value = 'chassis_id' |
|
49 | 1 | lldp.port_id.sub_value = 'port_id' |
|
50 | 1 | dpid.value = "00:00:00:00:00:00:00:02" |
|
51 | 1 | port_b.value = 2 |
|
52 | |||
53 | 1 | mock_unpack_non_empty.side_effect = [ethernet, lldp, dpid, port_b] |
|
54 | 1 | mock_get_switch_by_dpid.return_value = get_switch_mock(dpid.value, |
|
55 | 0x04) |
||
56 | 1 | await napp.on_ofpt_packet_in(event) |
|
57 | |||
58 | 1 | calls = [call(mock_ethernet, message.data), |
|
59 | call(mock_lldp, ethernet.data), |
||
60 | call(mock_dpid, lldp.chassis_id.sub_value), |
||
61 | call(mock_ubint32, lldp.port_id.sub_value)] |
||
62 | 1 | mock_unpack_non_empty.assert_has_calls(calls) |
|
63 | 1 | assert napp.loop_manager.process_if_looped.call_count == 1 |
|
64 | 1 | assert napp.liveness_manager.consume_hello_if_enabled.call_count == 1 |
|
65 | 1 | assert controller.buffers.app.aput.call_count == 1 |
|
66 | |||
67 | |||
68 | 1 | View Code Duplication | @patch('kytos.core.controller.Controller.get_switch_by_dpid') |
69 | 1 | @patch('napps.kytos.of_lldp.main.Main._unpack_non_empty') |
|
70 | 1 | @patch('napps.kytos.of_lldp.main.UBInt32') |
|
71 | 1 | @patch('napps.kytos.of_lldp.main.DPID') |
|
72 | 1 | @patch('napps.kytos.of_lldp.main.LLDP') |
|
73 | 1 | @patch('napps.kytos.of_lldp.main.Ethernet') |
|
74 | 1 | async def test_on_ofpt_packet_in_early_intf(*args): |
|
75 | """Test on_ofpt_packet_in early intf return.""" |
||
76 | 1 | (mock_ethernet, mock_lldp, mock_dpid, mock_ubint32, |
|
77 | mock_unpack_non_empty, mock_get_switch_by_dpid) = args |
||
78 | |||
79 | # pylint: disable=bad-option-value, import-outside-toplevel |
||
80 | 1 | from napps.kytos.of_lldp.main import Main |
|
81 | 1 | Main.get_liveness_controller = MagicMock() |
|
82 | 1 | topology = get_topology_mock() |
|
83 | 1 | controller = get_controller_mock() |
|
84 | 1 | controller.buffers.app.aput = AsyncMock() |
|
85 | 1 | controller.switches = topology.switches |
|
86 | 1 | napp = Main(controller) |
|
87 | 1 | napp.loop_manager.process_if_looped = AsyncMock() |
|
88 | 1 | napp.liveness_manager.consume_hello_if_enabled = AsyncMock() |
|
89 | |||
90 | 1 | switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
|
91 | 1 | message = MagicMock(in_port=1, data='data') |
|
92 | 1 | event = KytosEvent('ofpt_packet_in', content={'source': switch.connection, |
|
93 | 'message': message}) |
||
94 | |||
95 | 1 | mocked, ethernet, lldp, dpid, port_b = [MagicMock() for _ in range(5)] |
|
96 | 1 | mocked.value = 1 |
|
97 | 1 | mock_ubint32.return_value = mocked |
|
98 | 1 | ethernet.ether_type = 0x88CC |
|
99 | 1 | ethernet.data = 'eth_data' |
|
100 | 1 | lldp.chassis_id.sub_value = 'chassis_id' |
|
101 | 1 | lldp.port_id.sub_value = 'port_id' |
|
102 | 1 | dpid.value = "00:00:00:00:00:00:00:02" |
|
103 | 1 | port_b.value = 2 |
|
104 | |||
105 | 1 | mock_unpack_non_empty.side_effect = [ethernet, lldp, dpid, port_b] |
|
106 | 1 | mock_get_switch_by_dpid.return_value = get_switch_mock(dpid.value, |
|
107 | 0x04) |
||
108 | 1 | switch.get_interface_by_port_no = MagicMock(return_value=None) |
|
109 | 1 | await napp.on_ofpt_packet_in(event) |
|
110 | |||
111 | 1 | calls = [call(mock_ethernet, message.data), |
|
112 | call(mock_lldp, ethernet.data), |
||
113 | call(mock_dpid, lldp.chassis_id.sub_value), |
||
114 | call(mock_ubint32, lldp.port_id.sub_value)] |
||
115 | 1 | mock_unpack_non_empty.assert_has_calls(calls) |
|
116 | 1 | switch.get_interface_by_port_no.assert_called() |
|
117 | # early return shouldn't allow these to get called |
||
118 | 1 | assert napp.loop_manager.process_if_looped.call_count == 0 |
|
119 | 1 | assert napp.liveness_manager.consume_hello_if_enabled.call_count == 0 |
|
120 | 1 | assert controller.buffers.app.aput.call_count == 0 |
|
121 | |||
122 | |||
123 | 1 | async def test_on_table_enabled(): |
|
124 | """Test on_table_enabled""" |
||
125 | # pylint: disable=bad-option-value, import-outside-toplevel |
||
126 | 1 | from napps.kytos.of_lldp.main import Main |
|
127 | 1 | controller = get_controller_mock() |
|
128 | 1 | controller.buffers.app.aput = AsyncMock() |
|
129 | 1 | napp = Main(controller) |
|
130 | |||
131 | # Succesfully setting table groups |
||
132 | 1 | content = {"of_lldp": {"base": 123}} |
|
133 | 1 | event = KytosEvent(name="kytos/of_multi_table.enable_table", |
|
134 | content=content) |
||
135 | 1 | await napp.on_table_enabled(event) |
|
136 | 1 | assert napp.table_group == content["of_lldp"] |
|
137 | 1 | assert controller.buffers.app.aput.call_count == 1 |
|
138 | |||
139 | # Failure at setting table groups |
||
140 | 1 | content = {"of_lldp": {"unknown": 123}} |
|
141 | 1 | event = KytosEvent(name="kytos/of_multi_table.enable_table", |
|
142 | content=content) |
||
143 | 1 | await napp.on_table_enabled(event) |
|
144 | 1 | assert controller.buffers.app.aput.call_count == 1 |
|
145 | |||
146 | |||
147 | # pylint: disable=protected-access,too-many-public-methods |
||
148 | 1 | class TestMain: |
|
149 | """Tests for the Main class.""" |
||
150 | |||
151 | 1 | def setup_method(self): |
|
152 | """Execute steps before each tests.""" |
||
153 | # patch('kytos.core.helpers.run_on_thread', lambda x: x).start() |
||
154 | # pylint: disable=bad-option-value, import-outside-toplevel |
||
155 | 1 | from napps.kytos.of_lldp.main import Main |
|
156 | 1 | Main.get_liveness_controller = MagicMock() |
|
157 | 1 | self.topology = get_topology_mock() |
|
158 | 1 | controller = get_controller_mock() |
|
159 | 1 | controller.switches = self.topology.switches |
|
160 | 1 | self.base_endpoint = "kytos/of_lldp/v1" |
|
161 | 1 | self.napp = Main(controller) |
|
162 | 1 | self.api_client = get_test_client(controller, self.napp) |
|
163 | |||
164 | 1 | def teardown_method(self) -> None: |
|
165 | """Teardown.""" |
||
166 | 1 | patch.stopall() |
|
167 | |||
168 | 1 | def get_topology_interfaces(self): |
|
169 | """Return interfaces present in topology.""" |
||
170 | 1 | interfaces = [] |
|
171 | 1 | for switch in list(self.topology.switches.values()): |
|
172 | 1 | interfaces += list(switch.interfaces.values()) |
|
173 | 1 | return interfaces |
|
174 | |||
175 | 1 | @patch('napps.kytos.of_lldp.main.of_msg_prio') |
|
176 | 1 | @patch('napps.kytos.of_lldp.main.KytosEvent') |
|
177 | 1 | @patch('napps.kytos.of_lldp.main.VLAN') |
|
178 | 1 | @patch('napps.kytos.of_lldp.main.Ethernet') |
|
179 | 1 | @patch('napps.kytos.of_lldp.main.DPID') |
|
180 | 1 | @patch('napps.kytos.of_lldp.main.LLDP') |
|
181 | 1 | def test_execute(self, *args): |
|
182 | """Test execute method.""" |
||
183 | 1 | (_, _, mock_ethernet, _, mock_kytos_event, mock_of_msg_prio) = args |
|
184 | 1 | mock_buffer_put = MagicMock() |
|
185 | 1 | self.napp.controller.buffers.msg_out.put = mock_buffer_put |
|
186 | |||
187 | 1 | ethernet = MagicMock() |
|
188 | 1 | ethernet.pack.return_value = 'pack' |
|
189 | 1 | interfaces = self.get_topology_interfaces() |
|
190 | 1 | po_args = [(interface.switch.connection.protocol.version, |
|
191 | interface.port_number, 'pack') for interface in interfaces] |
||
192 | |||
193 | 1 | mock_ethernet.return_value = ethernet |
|
194 | 1 | mock_kytos_event.side_effect = po_args |
|
195 | |||
196 | 1 | mock_publish_stopped = MagicMock() |
|
197 | 1 | self.napp.try_to_publish_stopped_loops = mock_publish_stopped |
|
198 | 1 | self.napp.execute() |
|
199 | |||
200 | 1 | mock_of_msg_prio.assert_called() |
|
201 | 1 | mock_buffer_put.assert_has_calls([call(arg) |
|
202 | for arg in po_args]) |
||
203 | 1 | mock_publish_stopped.assert_called() |
|
204 | |||
205 | 1 | @patch('napps.kytos.of_lldp.main.Main.get_flows_by_switch') |
|
206 | 1 | @patch('httpx.request') |
|
207 | 1 | @patch('httpx.post') |
|
208 | 1 | def test_handle_lldp_flows(self, mock_post, mock_request, mock_flows): |
|
209 | """Test handle_lldp_flow method.""" |
||
210 | 1 | dpid = "00:00:00:00:00:00:00:01" |
|
211 | 1 | switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
|
212 | 1 | self.napp.controller.switches = {dpid: switch} |
|
213 | 1 | event_post = get_kytos_event_mock(name='kytos/topology.switch.enabled', |
|
214 | content={'dpid': dpid}) |
||
215 | |||
216 | 1 | event_del = get_kytos_event_mock(name='kytos/topology.switch.disabled', |
|
217 | content={'dpid': dpid}) |
||
218 | |||
219 | 1 | mock_post.return_value = MagicMock(status_code=202) |
|
220 | 1 | mock_request.return_value = MagicMock(status_code=202) |
|
221 | |||
222 | 1 | mock_flows.return_value = {} |
|
223 | 1 | self.napp._handle_lldp_flows(event_post) |
|
224 | 1 | mock_post.assert_called() |
|
225 | |||
226 | 1 | mock_flows.return_value = {"flows": "mocked_flows"} |
|
227 | 1 | self.napp._handle_lldp_flows(event_del) |
|
228 | 1 | mock_request.assert_called() |
|
229 | |||
230 | 1 | @patch('napps.kytos.of_lldp.main.Main.get_flows_by_switch') |
|
231 | 1 | @patch("time.sleep") |
|
232 | 1 | @patch("httpx.post") |
|
233 | 1 | def test_handle_lldp_flows_retries(self, mock_post, _, mock_flows): |
|
234 | """Test handle_lldp_flow method retries.""" |
||
235 | 1 | dpid = "00:00:00:00:00:00:00:01" |
|
236 | 1 | switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
|
237 | 1 | mock_flows.return_value = {} |
|
238 | 1 | self.napp.controller.switches = {dpid: switch} |
|
239 | 1 | event_post = get_kytos_event_mock(name="kytos/topology.switch.enabled", |
|
240 | content={"dpid": dpid}) |
||
241 | |||
242 | 1 | mock = MagicMock() |
|
243 | 1 | mock.request.method = "POST" |
|
244 | 1 | mock.status_code = 500 |
|
245 | 1 | mock.text = "some_err" |
|
246 | 1 | mock_post.return_value = mock |
|
247 | 1 | self.napp._handle_lldp_flows(event_post) |
|
248 | 1 | assert mock_post.call_count == 3 |
|
249 | |||
250 | 1 | @patch('napps.kytos.of_lldp.main.log') |
|
251 | 1 | @patch('napps.kytos.of_lldp.main.httpx') |
|
252 | 1 | def test_handle_lldp_flows_error(self, mock_httpx, mock_log): |
|
253 | """Test _handle_lldp_flows""" |
||
254 | 1 | dpid = "00:00:00:00:00:00:00:01" |
|
255 | 1 | event_post = get_kytos_event_mock(name='kytos/topology.switch.enabled', |
|
256 | content={'dpid': dpid}) |
||
257 | 1 | mock_httpx.get.side_effect = RequestError(message="mocked error") |
|
258 | 1 | self.napp._handle_lldp_flows(event_post) |
|
259 | 1 | assert mock_log.error.call_count == 1 |
|
260 | |||
261 | 1 | @patch('napps.kytos.of_lldp.main.PO13') |
|
262 | 1 | @patch('napps.kytos.of_lldp.main.AO13') |
|
263 | 1 | def test_build_lldp_packet_out(self, *args): |
|
264 | """Test _build_lldp_packet_out method.""" |
||
265 | 1 | (mock_ao13, mock_po13) = args |
|
266 | |||
267 | 1 | ao13 = MagicMock() |
|
268 | 1 | po13 = MagicMock() |
|
269 | 1 | po13.actions = [] |
|
270 | |||
271 | 1 | mock_ao13.return_value = ao13 |
|
272 | 1 | mock_po13.return_value = po13 |
|
273 | |||
274 | 1 | packet_out13 = self.napp._build_lldp_packet_out(0x04, 2, 'data2') |
|
275 | 1 | packet_out14 = self.napp._build_lldp_packet_out(0x05, 3, 'data3') |
|
276 | |||
277 | 1 | assert packet_out13.data == 'data2' |
|
278 | 1 | assert packet_out13.actions == [ao13] |
|
279 | 1 | assert packet_out13.actions[0].port == 2 |
|
280 | 1 | assert packet_out14 is None |
|
281 | |||
282 | 1 | @patch('napps.kytos.of_lldp.main.settings') |
|
283 | 1 | @patch('napps.kytos.of_lldp.main.EtherType') |
|
284 | 1 | @patch('napps.kytos.of_lldp.main.Port13') |
|
285 | 1 | def test_build_lldp_flow(self, *args): |
|
286 | """Test _build_lldp_flow method.""" |
||
287 | 1 | (mock_v0x04_port, mock_ethertype, |
|
288 | mock_settings) = args |
||
289 | 1 | self.napp.vlan_id = None |
|
290 | 1 | mock_v0x04_port.OFPP_CONTROLLER = 1234 |
|
291 | |||
292 | 1 | mock_ethertype.LLDP = 10 |
|
293 | 1 | mock_settings.FLOW_VLAN_VID = None |
|
294 | 1 | mock_settings.FLOW_PRIORITY = 1500 |
|
295 | 1 | dpid = "00:00:00:00:00:00:00:01" |
|
296 | |||
297 | 1 | flow = {} |
|
298 | 1 | match = {} |
|
299 | 1 | flow['priority'] = 1500 |
|
300 | 1 | flow['table_id'] = 0 |
|
301 | 1 | match['dl_type'] = 10 |
|
302 | |||
303 | 1 | flow['match'] = match |
|
304 | 1 | expected_flow_v0x04 = flow.copy() |
|
305 | 1 | expected_flow_v0x04['cookie'] = get_cookie(dpid) |
|
306 | 1 | expected_flow_v0x04['cookie_mask'] = 0xffffffffffffffff |
|
307 | |||
308 | 1 | expected_flow_v0x04['actions'] = [{'action_type': 'output', |
|
309 | 'port': 1234}] |
||
310 | 1 | expected_flow_v0x04['table_group'] = 'base' |
|
311 | 1 | expected_flow_v0x04['owner'] = 'of_lldp' |
|
312 | |||
313 | 1 | flow_mod10 = self.napp._build_lldp_flow(0x01, get_cookie(dpid)) |
|
314 | 1 | flow_mod13 = self.napp._build_lldp_flow(0x04, get_cookie(dpid)) |
|
315 | |||
316 | 1 | assert flow_mod10 is None |
|
317 | 1 | assert flow_mod13 == expected_flow_v0x04 |
|
318 | |||
319 | 1 | def test_unpack_non_empty(self): |
|
320 | """Test _unpack_non_empty method.""" |
||
321 | 1 | desired_class = MagicMock() |
|
322 | 1 | data = MagicMock() |
|
323 | 1 | data.value = 'data' |
|
324 | |||
325 | 1 | obj = self.napp._unpack_non_empty(desired_class, data) |
|
326 | |||
327 | 1 | obj.unpack.assert_called_with('data') |
|
328 | |||
329 | 1 | def test_get_data(self, monkeypatch): |
|
330 | """Test _get_data method.""" |
||
331 | 1 | interfaces = ['00:00:00:00:00:00:00:01:1', '00:00:00:00:00:00:00:01:2'] |
|
332 | 1 | monkeypatch.setattr("napps.kytos.of_lldp.main.get_json_or_400", |
|
333 | lambda req, loop: {"interfaces": interfaces}) |
||
334 | 1 | data = self.napp._get_data(MagicMock()) |
|
335 | 1 | assert data == interfaces |
|
336 | |||
337 | 1 | def test_load_liveness(self) -> None: |
|
338 | """Test load_liveness.""" |
||
339 | 1 | self.napp.load_liveness() |
|
340 | 1 | count = self.napp.liveness_controller.get_enabled_interfaces.call_count |
|
341 | 1 | assert count == 1 |
|
342 | |||
343 | 1 | def test_handle_topology_loaded(self) -> None: |
|
344 | """Test handle_topology_loaded.""" |
||
345 | 1 | event = KytosEvent("kytos/topology.topology_loaded", |
|
346 | content={"topology": {}}) |
||
347 | 1 | self.napp.load_liveness = MagicMock() |
|
348 | 1 | self.napp.loop_manager.handle_topology_loaded = MagicMock() |
|
349 | 1 | self.napp.handle_topology_loaded(event) |
|
350 | 1 | assert self.napp.loop_manager.handle_topology_loaded.call_count == 1 |
|
351 | 1 | assert self.napp.load_liveness.call_count == 1 |
|
352 | |||
353 | 1 | def test_publish_liveness_status(self) -> None: |
|
354 | """Test publish_liveness_status.""" |
||
355 | 1 | self.napp.controller.buffers.app.put = MagicMock() |
|
356 | 1 | event_suffix, interfaces = "up", [MagicMock(id=1), MagicMock(id=2)] |
|
357 | 1 | self.napp.publish_liveness_status(event_suffix, interfaces) |
|
358 | 1 | assert self.napp.controller.buffers.app.put.call_count == 1 |
|
359 | 1 | event = self.napp.controller.buffers.app.put.call_args[0][0] |
|
360 | 1 | assert event.name == f"kytos/of_lldp.liveness.{event_suffix}" |
|
361 | 1 | assert event.content["interfaces"] == interfaces |
|
362 | |||
363 | 1 | def test_get_interfaces(self): |
|
364 | """Test _get_interfaces method.""" |
||
365 | 1 | expected_interfaces = self.get_topology_interfaces() |
|
366 | 1 | interfaces = self.napp._get_interfaces() |
|
367 | 1 | assert interfaces == expected_interfaces |
|
368 | |||
369 | 1 | def test_get_interfaces_dict(self): |
|
370 | """Test _get_interfaces_dict method.""" |
||
371 | 1 | interfaces = self.napp._get_interfaces() |
|
372 | 1 | expected_interfaces = {inter.id: inter for inter in interfaces} |
|
373 | 1 | interfaces_dict = self.napp._get_interfaces_dict(interfaces) |
|
374 | 1 | assert interfaces_dict == expected_interfaces |
|
375 | |||
376 | 1 | def test_get_lldp_interfaces(self): |
|
377 | """Test _get_lldp_interfaces method.""" |
||
378 | 1 | lldp_interfaces = self.napp._get_lldp_interfaces() |
|
379 | 1 | expected_interfaces = ['00:00:00:00:00:00:00:01:1', |
|
380 | '00:00:00:00:00:00:00:01:2', |
||
381 | '00:00:00:00:00:00:00:02:1', |
||
382 | '00:00:00:00:00:00:00:02:2'] |
||
383 | 1 | assert lldp_interfaces == expected_interfaces |
|
384 | |||
385 | 1 | async def test_rest_get_lldp_interfaces(self): |
|
386 | """Test get_lldp_interfaces method.""" |
||
387 | 1 | endpoint = f"{self.base_endpoint}/interfaces" |
|
388 | 1 | response = await self.api_client.get(endpoint) |
|
389 | 1 | expected_data = {"interfaces": ['00:00:00:00:00:00:00:01:1', |
|
390 | '00:00:00:00:00:00:00:01:2', |
||
391 | '00:00:00:00:00:00:00:02:1', |
||
392 | '00:00:00:00:00:00:00:02:2']} |
||
393 | 1 | assert response.status_code == 200 |
|
394 | 1 | assert response.json() == expected_data |
|
395 | |||
396 | 1 | async def test_enable_disable_lldp_200(self, event_loop): |
|
397 | """Test 200 response for enable_lldp and disable_lldp methods.""" |
||
398 | 1 | data = {"interfaces": ['00:00:00:00:00:00:00:01:1', |
|
399 | '00:00:00:00:00:00:00:01:2', |
||
400 | '00:00:00:00:00:00:00:02:1', |
||
401 | '00:00:00:00:00:00:00:02:2']} |
||
402 | 1 | self.napp.controller.loop = event_loop |
|
403 | 1 | self.napp.publish_liveness_status = MagicMock() |
|
404 | 1 | endpoint = f"{self.base_endpoint}/interfaces/disable" |
|
405 | 1 | response = await self.api_client.post(endpoint, json=data) |
|
406 | 1 | assert response.status_code == 200 |
|
407 | 1 | assert self.napp.liveness_controller.disable_interfaces.call_count == 1 |
|
408 | 1 | assert self.napp.publish_liveness_status.call_count == 1 |
|
409 | 1 | endpoint = f"{self.base_endpoint}/interfaces/enable" |
|
410 | 1 | response = await self.api_client.post(endpoint, json=data) |
|
411 | 1 | assert response.status_code == 200 |
|
412 | |||
413 | 1 | async def test_enable_disable_lldp_404(self): |
|
414 | """Test 404 response for enable_lldp and disable_lldp methods.""" |
||
415 | 1 | data = {"interfaces": []} |
|
416 | 1 | self.napp.controller.switches = {} |
|
417 | 1 | endpoint = f"{self.base_endpoint}/disable" |
|
418 | 1 | response = await self.api_client.post(endpoint, json=data) |
|
419 | 1 | assert response.status_code == 404 |
|
420 | 1 | endpoint = f"{self.base_endpoint}/enable" |
|
421 | 1 | response = await self.api_client.post(endpoint, json=data) |
|
422 | 1 | assert response.status_code == 404 |
|
423 | |||
424 | 1 | async def test_enable_disable_lldp_400(self, event_loop): |
|
425 | """Test 400 response for enable_lldp and disable_lldp methods.""" |
||
426 | 1 | data = {"interfaces": ['00:00:00:00:00:00:00:01:1', |
|
427 | '00:00:00:00:00:00:00:01:2', |
||
428 | '00:00:00:00:00:00:00:02:1', |
||
429 | '00:00:00:00:00:00:00:02:2', |
||
430 | '00:00:00:00:00:00:00:03:1', |
||
431 | '00:00:00:00:00:00:00:03:2', |
||
432 | '00:00:00:00:00:00:00:04:1']} |
||
433 | 1 | self.napp.controller.loop = event_loop |
|
434 | 1 | self.napp.publish_liveness_status = MagicMock() |
|
435 | 1 | url = f'{self.base_endpoint}/interfaces/disable' |
|
436 | 1 | response = await self.api_client.post(url, json=data) |
|
437 | 1 | assert response.status_code == 400 |
|
438 | 1 | assert self.napp.publish_liveness_status.call_count == 1 |
|
439 | |||
440 | 1 | url = f'{self.base_endpoint}/interfaces/enable' |
|
441 | 1 | response = await self.api_client.post(url, json=data) |
|
442 | 1 | assert response.status_code == 400 |
|
443 | |||
444 | 1 | async def test_get_time(self): |
|
445 | """Test get polling time.""" |
||
446 | 1 | url = f"{self.base_endpoint}/polling_time" |
|
447 | 1 | response = await self.api_client.get(url) |
|
448 | 1 | assert response.status_code == 200 |
|
449 | |||
450 | 1 | async def test_set_polling_time(self): |
|
451 | """Test update polling time.""" |
||
452 | 1 | url = f"{self.base_endpoint}/polling_time" |
|
453 | 1 | data = {'polling_time': 5} |
|
454 | 1 | response = await self.api_client.post(url, json=data) |
|
455 | 1 | assert response.status_code == 200 |
|
456 | |||
457 | 1 | async def test_set_time_400(self): |
|
458 | """Test fail case the update polling time.""" |
||
459 | 1 | url = f"{self.base_endpoint}/polling_time" |
|
460 | 1 | data = {'polling_time': 'A'} |
|
461 | 1 | response = await self.api_client.post(url, json=data) |
|
462 | 1 | assert response.status_code == 400 |
|
463 | |||
464 | 1 | async def test_endpoint_enable_liveness(self, event_loop): |
|
465 | """Test POST v1/liveness/enable.""" |
||
466 | 1 | self.napp.controller.loop = event_loop |
|
467 | 1 | self.napp.liveness_manager.enable = MagicMock() |
|
468 | 1 | self.napp.publish_liveness_status = MagicMock() |
|
469 | 1 | url = f"{self.base_endpoint}/liveness/enable" |
|
470 | 1 | data = {"interfaces": ["00:00:00:00:00:00:00:01:1"]} |
|
471 | 1 | response = await self.api_client.post(url, json=data) |
|
472 | 1 | assert response.status_code == 200 |
|
473 | 1 | assert response.json() == {} |
|
474 | 1 | assert self.napp.liveness_controller.enable_interfaces.call_count == 1 |
|
475 | 1 | assert self.napp.liveness_manager.enable.call_count == 1 |
|
476 | 1 | assert self.napp.publish_liveness_status.call_count == 1 |
|
477 | |||
478 | 1 | async def test_endpoint_disable_liveness(self, event_loop): |
|
479 | """Test POST v1/liveness/disable.""" |
||
480 | 1 | self.napp.controller.loop = event_loop |
|
481 | 1 | self.napp.liveness_manager.disable = MagicMock() |
|
482 | 1 | self.napp.publish_liveness_status = MagicMock() |
|
483 | 1 | url = f"{self.base_endpoint}/liveness/disable" |
|
484 | 1 | data = {"interfaces": ["00:00:00:00:00:00:00:01:1"]} |
|
485 | 1 | response = await self.api_client.post(url, json=data) |
|
486 | 1 | assert response.status_code == 200 |
|
487 | 1 | assert response.json() == {} |
|
488 | 1 | assert self.napp.liveness_controller.disable_interfaces.call_count == 1 |
|
489 | 1 | assert self.napp.liveness_manager.disable.call_count == 1 |
|
490 | 1 | assert self.napp.publish_liveness_status.call_count == 1 |
|
491 | |||
492 | 1 | async def test_endpoint_get_liveness(self): |
|
493 | """Test GET v1/liveness/.""" |
||
494 | 1 | self.napp.liveness_manager.enable = MagicMock() |
|
495 | 1 | self.napp.publish_liveness_status = MagicMock() |
|
496 | 1 | url = f"{self.base_endpoint}/liveness/" |
|
497 | 1 | response = await self.api_client.get(url) |
|
498 | 1 | assert response.status_code == 200 |
|
499 | 1 | assert response.json() == {"interfaces": []} |
|
500 | |||
501 | 1 | async def test_endpoint_get_pair_liveness(self): |
|
502 | """Test GET v1/liveness//pair.""" |
||
503 | 1 | self.napp.liveness_manager.enable = MagicMock() |
|
504 | 1 | self.napp.publish_liveness_status = MagicMock() |
|
505 | 1 | url = f"{self.base_endpoint}/liveness/pair" |
|
506 | 1 | response = await self.api_client.get(url) |
|
507 | 1 | assert response.status_code == 200 |
|
508 | 1 | assert response.json() == {"pairs": []} |
|
509 | |||
510 | 1 | def test_set_flow_table_group_owner(self): |
|
511 | """Test set_flow_table_group_owner""" |
||
512 | 1 | self.napp.table_group = {"base": 2} |
|
513 | 1 | flow = {} |
|
514 | 1 | self.napp.set_flow_table_group_owner(flow, "base") |
|
515 | 1 | assert "table_group" in flow |
|
516 | 1 | assert "owner" in flow |
|
517 | 1 | assert flow["table_id"] == 2 |
|
518 | |||
519 | 1 | View Code Duplication | @patch('napps.kytos.of_lldp.main.log') |
520 | 1 | def test_use_vlan(self, mock_log): |
|
521 | """Test use_vlan""" |
||
522 | 1 | switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
|
523 | 1 | interface_a = get_interface_mock("mock_a", 1, switch) |
|
524 | 1 | interface_a.use_tags = MagicMock() |
|
525 | 1 | interface_b = get_interface_mock("mock_b", 2, switch) |
|
526 | 1 | interface_b.use_tags = MagicMock() |
|
527 | 1 | switch.interfaces = {1: interface_a, 2: interface_b} |
|
528 | 1 | self.napp.use_vlan(switch) |
|
529 | 1 | assert interface_a.use_tags.call_count == 1 |
|
530 | 1 | assert interface_b.use_tags.call_count == 1 |
|
531 | |||
532 | 1 | interface_a.use_tags.return_value = False |
|
533 | 1 | self.napp.use_vlan(switch) |
|
534 | 1 | assert interface_a.use_tags.call_count == 2 |
|
535 | 1 | assert interface_b.use_tags.call_count == 2 |
|
536 | 1 | assert mock_log.warning.call_count == 1 |
|
537 | |||
538 | 1 | self.napp.vlan_id = None |
|
539 | 1 | self.napp.use_vlan(switch) |
|
540 | 1 | assert interface_a.use_tags.call_count == 2 |
|
541 | 1 | assert interface_b.use_tags.call_count == 2 |
|
542 | |||
543 | 1 | View Code Duplication | @patch('napps.kytos.of_lldp.main.log') |
544 | 1 | def test_make_vlan_available(self, mock_log): |
|
545 | """Test make_vlan_available""" |
||
546 | 1 | switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
|
547 | 1 | interface_a = get_interface_mock("mock_a", 1, switch) |
|
548 | 1 | interface_a.make_tags_available = MagicMock() |
|
549 | 1 | interface_b = get_interface_mock("mock_b", 2, switch) |
|
550 | 1 | interface_b.make_tags_available = MagicMock() |
|
551 | 1 | switch.interfaces = {1: interface_a, 2: interface_b} |
|
552 | 1 | self.napp.make_vlan_available(switch) |
|
553 | 1 | assert interface_a.make_tags_available.call_count == 1 |
|
554 | 1 | assert interface_b.make_tags_available.call_count == 1 |
|
555 | |||
556 | 1 | interface_a.make_tags_available.return_value = False |
|
557 | 1 | self.napp.make_vlan_available(switch) |
|
558 | 1 | assert interface_a.make_tags_available.call_count == 2 |
|
559 | 1 | assert interface_b.make_tags_available.call_count == 2 |
|
560 | 1 | assert mock_log.warning.call_count == 1 |
|
561 | |||
562 | 1 | self.napp.vlan_id = None |
|
563 | 1 | self.napp.make_vlan_available(switch) |
|
564 | 1 | assert interface_a.make_tags_available.call_count == 2 |
|
565 | 1 | assert interface_b.make_tags_available.call_count == 2 |
|
566 | |||
567 | 1 | @patch('httpx.get') |
|
568 | 1 | def test_get_flows_by_switch_retry(self, mock_get): |
|
569 | """Test get_flows_by_switch retry""" |
||
570 | 1 | dpid = "00:00:00:00:00:00:00:01" |
|
571 | 1 | mock_get.return_value = MagicMock( |
|
572 | status_code=400, text="mock_error" |
||
573 | ) |
||
574 | 1 | with pytest.raises(RetryError): |
|
575 | 1 | self.napp.get_flows_by_switch(dpid) |
|
576 | 1 | assert mock_get.call_count == 3 |
|
577 | |||
578 | 1 | @patch('httpx.post') |
|
579 | 1 | @patch('napps.kytos.of_lldp.main.Main.use_vlan') |
|
580 | 1 | def test_send_flow_enabled(self, mock_use, mock_post): |
|
581 | """Test send_flows when switch is enabled""" |
||
582 | 1 | mock_post.return_value = MagicMock( |
|
583 | status_code=202, is_server_error=False |
||
584 | ) |
||
585 | 1 | event_name = 'kytos/topology.switch.enabled' |
|
586 | 1 | switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
|
587 | 1 | data = {'flows': [{'cookie_mask': "mock_cookie"}]} |
|
588 | 1 | self.napp.send_flow(switch, event_name, data=data) |
|
589 | |||
590 | 1 | assert mock_use.call_count == 1 |
|
591 | 1 | assert mock_use.call_args[0][0] == switch |
|
592 | 1 | assert data['flows'] == [{}] |
|
593 | |||
594 | 1 | @patch('httpx.request') |
|
595 | 1 | @patch('napps.kytos.of_lldp.main.Main.make_vlan_available') |
|
596 | 1 | def test_send_flow_disabled(self, mock_avaialble, mock_request): |
|
597 | """Test send_flows when switch is disabled""" |
||
598 | 1 | mock_request.return_value = MagicMock( |
|
599 | status_code=202, is_server_error=False |
||
600 | ) |
||
601 | 1 | event_name = 'kytos/topology.switch.disabled' |
|
602 | 1 | switch = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
|
603 | 1 | data = {'flows': [{'cookie_mask': "mock_cookie"}]} |
|
604 | 1 | self.napp.send_flow(switch, event_name, data=data) |
|
605 | |||
606 | 1 | assert mock_avaialble.call_count == 1 |
|
607 | 1 | assert mock_avaialble.call_args[0][0] == switch |
|
608 | assert data['flows'] == [{'cookie_mask': "mock_cookie"}] |
||
609 |