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