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