Total Complexity | 106 |
Total Lines | 2165 |
Duplicated Lines | 9.19 % |
Coverage | 89.1% |
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 | """Module to test the main napp file.""" |
||
2 | # pylint: disable=import-error,no-name-in-module,wrong-import-order |
||
3 | # pylint: disable=import-outside-toplevel,attribute-defined-outside-init |
||
4 | 1 | import asyncio |
|
5 | 1 | import pytest |
|
6 | 1 | import time |
|
7 | 1 | from datetime import timedelta |
|
8 | 1 | from unittest.mock import MagicMock, create_autospec, patch, call, Mock |
|
9 | 1 | import tenacity |
|
10 | 1 | from kytos.core.common import EntityStatus |
|
11 | 1 | from kytos.core.helpers import now |
|
12 | 1 | from kytos.core.events import KytosEvent |
|
13 | 1 | from kytos.core.exceptions import (KytosSetTagRangeError, |
|
14 | KytosTagtypeNotSupported) |
||
15 | 1 | from kytos.core.interface import Interface |
|
16 | 1 | from kytos.core.link import Link |
|
17 | 1 | from kytos.core.switch import Switch |
|
18 | 1 | from kytos.lib.helpers import (get_interface_mock, get_link_mock, |
|
19 | get_controller_mock, get_switch_mock, |
||
20 | get_test_client) |
||
21 | 1 | from napps.kytos.topology.exceptions import RestoreError |
|
22 | |||
23 | |||
24 | 1 | @pytest.mark.parametrize("liveness_status, status", |
|
25 | [("up", EntityStatus.UP), |
||
26 | ("down", EntityStatus.DOWN)]) |
||
27 | 1 | def test_handle_link_liveness_status(liveness_status, status) -> None: |
|
28 | """Test handle link liveness.""" |
||
29 | 1 | from napps.kytos.topology.main import Main |
|
30 | 1 | napp = Main(get_controller_mock()) |
|
31 | 1 | napp.notify_topology_update = MagicMock() |
|
32 | 1 | napp.notify_link_status_change = MagicMock() |
|
33 | |||
34 | 1 | link = MagicMock(id="some_id", status=status) |
|
35 | 1 | napp.handle_link_liveness_status(link, liveness_status) |
|
36 | |||
37 | 1 | link.extend_metadata.assert_called_with({"liveness_status": |
|
38 | liveness_status}) |
||
39 | 1 | assert napp.notify_topology_update.call_count == 1 |
|
40 | 1 | assert napp.notify_link_status_change.call_count == 1 |
|
41 | 1 | reason = f"liveness_{liveness_status}" |
|
42 | 1 | napp.notify_link_status_change.assert_called_with(link, reason=reason) |
|
43 | |||
44 | |||
45 | # pylint: disable=too-many-public-methods |
||
46 | 1 | class TestMain: |
|
47 | """Test the Main class.""" |
||
48 | |||
49 | # pylint: disable=too-many-public-methods, protected-access,C0302 |
||
50 | |||
51 | 1 | def setup_method(self): |
|
52 | """Execute steps before each tests.""" |
||
53 | 1 | patch('kytos.core.helpers.run_on_thread', lambda x: x).start() |
|
54 | # pylint: disable=import-outside-toplevel |
||
55 | 1 | from napps.kytos.topology.main import Main |
|
56 | 1 | Main.get_topo_controller = MagicMock() |
|
57 | 1 | controller = get_controller_mock() |
|
58 | 1 | self.napp = Main(controller) |
|
59 | 1 | self.api_client = get_test_client(controller, self.napp) |
|
60 | 1 | self.base_endpoint = 'kytos/topology/v3' |
|
61 | |||
62 | 1 | def test_get_event_listeners(self): |
|
63 | """Verify all event listeners registered.""" |
||
64 | 1 | expected_events = [ |
|
65 | 'kytos/core.shutdown', |
||
66 | 'kytos/core.shutdown.kytos/topology', |
||
67 | '.*.topo_controller.upsert_switch', |
||
68 | '.*.of_lldp.network_status.updated', |
||
69 | '.*.interface.is.nni', |
||
70 | '.*.connection.lost', |
||
71 | '.*.switch.interfaces.created', |
||
72 | '.*.topology.switch.interface.created', |
||
73 | '.*.switch.interface.deleted', |
||
74 | '.*.switch.interface.link_down', |
||
75 | '.*.switch.interface.link_up', |
||
76 | '.*.switch.(new|reconnected)', |
||
77 | 'kytos/.*.liveness.(up|down|disabled)', |
||
78 | '.*.switch.port.created', |
||
79 | 'kytos/topology.notify_link_up_if_status', |
||
80 | 'topology.interruption.(start|end)', |
||
81 | 'kytos/core.interface_tags', |
||
82 | ] |
||
83 | 1 | actual_events = self.napp.listeners() |
|
84 | 1 | assert sorted(expected_events) == sorted(actual_events) |
|
85 | |||
86 | 1 | async def test_get_topology(self): |
|
87 | """Test get_topology.""" |
||
88 | 1 | dpid_a = "00:00:00:00:00:00:00:01" |
|
89 | 1 | dpid_b = "00:00:00:00:00:00:00:02" |
|
90 | 1 | expected = { |
|
91 | "topology": { |
||
92 | "switches": { |
||
93 | "00:00:00:00:00:00:00:01": { |
||
94 | "metadata": { |
||
95 | "lat": "0.0", |
||
96 | "lng": "-30.0" |
||
97 | } |
||
98 | }, |
||
99 | "00:00:00:00:00:00:00:02": { |
||
100 | "metadata": { |
||
101 | "lat": "0.0", |
||
102 | "lng": "-30.0" |
||
103 | } |
||
104 | } |
||
105 | }, |
||
106 | "links": { |
||
107 | "cf0f4071be4": { |
||
108 | "id": "cf0f4071be4" |
||
109 | } |
||
110 | } |
||
111 | } |
||
112 | } |
||
113 | |||
114 | 1 | mock_switch_a = get_switch_mock(dpid_a, 0x04) |
|
115 | 1 | mock_switch_b = get_switch_mock(dpid_b, 0x04) |
|
116 | 1 | mock_interface_a = get_interface_mock('s1-eth1', 1, mock_switch_a) |
|
117 | 1 | mock_interface_b = get_interface_mock('s2-eth1', 1, mock_switch_b) |
|
118 | |||
119 | 1 | mock_link = get_link_mock(mock_interface_a, mock_interface_b) |
|
120 | 1 | mock_link.id = 'cf0f4071be4' |
|
121 | 1 | mock_switch_a.id = dpid_a |
|
122 | 1 | mock_switch_a.as_dict.return_value = {'metadata': {'lat': '0.0', |
|
123 | 'lng': '-30.0'}} |
||
124 | 1 | mock_switch_b.id = dpid_b |
|
125 | 1 | mock_switch_b.as_dict.return_value = {'metadata': {'lat': '0.0', |
|
126 | 'lng': '-30.0'}} |
||
127 | |||
128 | 1 | self.napp.controller.switches = {dpid_a: mock_switch_a, |
|
129 | dpid_b: mock_switch_b} |
||
130 | |||
131 | 1 | self.napp.controller.links = {"cf0f4071be4": mock_link} |
|
132 | 1 | mock_link.as_dict.return_value = {"id": "cf0f4071be4"} |
|
133 | 1 | endpoint = f"{self.base_endpoint}/" |
|
134 | 1 | response = await self.api_client.get(endpoint) |
|
135 | 1 | assert response.status_code == 200 |
|
136 | 1 | assert response.json() == expected |
|
137 | |||
138 | 1 | def test_load_topology(self): |
|
139 | """Test load_topology.""" |
||
140 | 1 | mock_buffers_put = MagicMock() |
|
141 | 1 | self.napp.controller.buffers.app.put = mock_buffers_put |
|
142 | 1 | link_id = \ |
|
143 | 'cf0f4071be426b3f745027f5d22bc61f8312ae86293c9b28e7e66015607a9260' |
||
144 | 1 | dpid_a = '00:00:00:00:00:00:00:01' |
|
145 | 1 | dpid_b = '00:00:00:00:00:00:00:02' |
|
146 | 1 | topology = { |
|
147 | "topology": { |
||
148 | "links": { |
||
149 | link_id: { |
||
150 | "enabled": True, |
||
151 | "id": link_id, |
||
152 | "endpoint_a": {"id": f"{dpid_a}:2"}, |
||
153 | "endpoint_b": {"id": f"{dpid_b}:2"}, |
||
154 | } |
||
155 | }, |
||
156 | "switches": { |
||
157 | dpid_a: { |
||
158 | "dpid": dpid_a, |
||
159 | "enabled": True, |
||
160 | "metadata": {}, |
||
161 | "id": dpid_a, |
||
162 | "interfaces": { |
||
163 | f"{dpid_a}:2": { |
||
164 | "enabled": True, |
||
165 | "metadata": {}, |
||
166 | "lldp": True, |
||
167 | "port_number": 2, |
||
168 | "name": "s1-eth2", |
||
169 | } |
||
170 | }, |
||
171 | }, |
||
172 | dpid_b: { |
||
173 | "dpid": dpid_b, |
||
174 | "enabled": True, |
||
175 | "metadata": {}, |
||
176 | "id": dpid_b, |
||
177 | "interfaces": { |
||
178 | f"{dpid_b}:2": { |
||
179 | "enabled": True, |
||
180 | "metadata": {}, |
||
181 | "lldp": True, |
||
182 | "port_number": 2, |
||
183 | "name": "s2-eth2", |
||
184 | } |
||
185 | }, |
||
186 | }, |
||
187 | }, |
||
188 | } |
||
189 | } |
||
190 | 1 | switches_expected = [dpid_a, dpid_b] |
|
191 | 1 | interfaces_expected = [f'{dpid_a}:2', f'{dpid_b}:2'] |
|
192 | 1 | links_expected = [link_id] |
|
193 | 1 | self.napp.topo_controller.get_topology.return_value = topology |
|
194 | 1 | self.napp.load_topology() |
|
195 | 1 | assert switches_expected == list(self.napp.controller.switches.keys()) |
|
196 | 1 | interfaces = [] |
|
197 | 1 | for switch in self.napp.controller.switches.values(): |
|
198 | 1 | for iface in switch.interfaces.values(): |
|
199 | 1 | interfaces.append(iface.id) |
|
200 | 1 | assert interfaces_expected == interfaces |
|
201 | 1 | assert links_expected == list(self.napp.controller.links.keys()) |
|
202 | 1 | assert mock_buffers_put.call_args[1] == {"timeout": 1} |
|
203 | |||
204 | 1 | @patch('napps.kytos.topology.main.Main._load_switch') |
|
205 | 1 | @patch('napps.kytos.topology.main.Main._load_link') |
|
206 | 1 | def test_load_topology_does_nothing(self, *args): |
|
207 | """Test _load_network_status doing nothing.""" |
||
208 | 1 | (mock_load_link, mock_load_switch) = args |
|
209 | 1 | self.napp.topo_controller.get_topology.return_value = { |
|
210 | "topology": {"switches": {}, "links": {}} |
||
211 | } |
||
212 | 1 | self.napp.topo_controller.load_topology() |
|
213 | 1 | assert mock_load_link.call_count == 0 |
|
214 | 1 | assert mock_load_switch.call_count == 0 |
|
215 | |||
216 | 1 | @patch('napps.kytos.topology.main.Main._load_switch') |
|
217 | 1 | @patch('napps.kytos.topology.main.log') |
|
218 | 1 | def test_load_topology_fail_switch(self, *args): |
|
219 | """Test load_topology failure in switch.""" |
||
220 | 1 | (mock_log, mock_load_switch) = args |
|
221 | 1 | topology = { |
|
222 | 'topology': { |
||
223 | 'links': {}, |
||
224 | 'switches': { |
||
225 | '1': {} |
||
226 | } |
||
227 | } |
||
228 | } |
||
229 | 1 | mock_log.error.return_value = True |
|
230 | 1 | self.napp.topo_controller.get_topology.return_value = topology |
|
231 | 1 | mock_load_switch.side_effect = AttributeError('xpto') |
|
232 | 1 | self.napp.load_topology() |
|
233 | 1 | error = 'Error loading switch: xpto' |
|
234 | 1 | mock_log.error.assert_called_with(error) |
|
235 | |||
236 | 1 | @patch('napps.kytos.topology.main.Main._load_link') |
|
237 | 1 | @patch('napps.kytos.topology.main.log') |
|
238 | 1 | def test_load_topology_fail_link(self, *args): |
|
239 | """Test load_topology failure in link.""" |
||
240 | 1 | (mock_log, mock_load_link) = args |
|
241 | 1 | topology = { |
|
242 | 'topology': { |
||
243 | 'switches': {}, |
||
244 | 'links': { |
||
245 | '1': {} |
||
246 | } |
||
247 | } |
||
248 | } |
||
249 | 1 | mock_log.error.return_value = True |
|
250 | 1 | self.napp.topo_controller.get_topology.return_value = topology |
|
251 | 1 | mock_load_link.side_effect = AttributeError('xpto') |
|
252 | 1 | self.napp.load_topology() |
|
253 | 1 | error = 'Error loading link 1: xpto' |
|
254 | 1 | mock_log.error.assert_called_with(error) |
|
255 | |||
256 | 1 | @patch('napps.kytos.topology.main.Main.load_interfaces_tags_values') |
|
257 | 1 | @patch('napps.kytos.topology.main.KytosEvent') |
|
258 | 1 | def test_load_switch(self, *args): |
|
259 | """Test _load_switch.""" |
||
260 | 1 | (mock_event, mock_load_tags) = args |
|
261 | 1 | mock_buffers_put = MagicMock() |
|
262 | 1 | self.napp.controller.buffers.app.put = mock_buffers_put |
|
263 | 1 | dpid_a = "00:00:00:00:00:00:00:01" |
|
264 | 1 | dpid_x = "00:00:00:00:00:00:00:XX" |
|
265 | 1 | iface_a = f'{dpid_a}:1' |
|
266 | 1 | switch_attrs = { |
|
267 | 'dpid': dpid_a, |
||
268 | 'enabled': True, |
||
269 | 'id': dpid_a, |
||
270 | 'metadata': {}, |
||
271 | 'interfaces': { |
||
272 | iface_a: { |
||
273 | 'enabled': True, |
||
274 | 'active': True, |
||
275 | 'lldp': True, |
||
276 | 'id': iface_a, |
||
277 | 'switch': dpid_a, |
||
278 | 'metadata': {}, |
||
279 | 'name': 's2-eth1', |
||
280 | 'port_number': 1 |
||
281 | } |
||
282 | } |
||
283 | } |
||
284 | 1 | self.napp._load_switch(dpid_a, switch_attrs) |
|
285 | |||
286 | 1 | assert len(self.napp.controller.switches) == 1 |
|
287 | 1 | assert dpid_a in self.napp.controller.switches |
|
288 | 1 | assert dpid_x not in self.napp.controller.switches |
|
289 | 1 | switch = self.napp.controller.switches[dpid_a] |
|
290 | 1 | interface_details = self.napp.topo_controller.get_interfaces_details |
|
291 | 1 | interface_details.assert_called_once_with([iface_a]) |
|
292 | 1 | mock_load_tags.assert_called() |
|
293 | |||
294 | 1 | assert switch.id == dpid_a |
|
295 | 1 | assert switch.dpid == dpid_a |
|
296 | 1 | assert switch.is_enabled() |
|
297 | 1 | assert not switch.is_active() |
|
298 | |||
299 | 1 | assert len(switch.interfaces) == 1 |
|
300 | 1 | assert 1 in switch.interfaces |
|
301 | 1 | assert 2 not in switch.interfaces |
|
302 | 1 | mock_event.assert_called() |
|
303 | 1 | mock_buffers_put.assert_called() |
|
304 | 1 | assert mock_buffers_put.call_args[1] == {"timeout": 1} |
|
305 | |||
306 | 1 | interface = switch.interfaces[1] |
|
307 | 1 | assert interface.id == iface_a |
|
308 | 1 | assert interface.switch.id == dpid_a |
|
309 | 1 | assert interface.port_number == 1 |
|
310 | 1 | assert interface.is_enabled() |
|
311 | 1 | assert not interface.is_active() |
|
312 | 1 | assert interface.lldp |
|
313 | 1 | assert interface.uni |
|
314 | 1 | assert not interface.nni |
|
315 | |||
316 | 1 | def test_load_switch_attrs(self): |
|
317 | """Test _load_switch.""" |
||
318 | 1 | dpid_b = "00:00:00:00:00:00:00:02" |
|
319 | 1 | iface_b = f'{dpid_b}:1' |
|
320 | 1 | switch_attrs = { |
|
321 | "active": True, |
||
322 | "connection": "127.0.0.1:43230", |
||
323 | "data_path": "XX Human readable desc of dp", |
||
324 | "dpid": "00:00:00:00:00:00:00:02", |
||
325 | "enabled": False, |
||
326 | "hardware": "Open vSwitch", |
||
327 | "id": "00:00:00:00:00:00:00:02", |
||
328 | "interfaces": { |
||
329 | "00:00:00:00:00:00:00:02:1": { |
||
330 | "active": True, |
||
331 | "enabled": False, |
||
332 | "id": "00:00:00:00:00:00:00:02:1", |
||
333 | "link": "", |
||
334 | "lldp": False, |
||
335 | "mac": "de:58:c3:30:b7:b7", |
||
336 | "metadata": {}, |
||
337 | "name": "s2-eth1", |
||
338 | "nni": False, |
||
339 | "port_number": 1, |
||
340 | "speed": 1250000000, |
||
341 | "switch": "00:00:00:00:00:00:00:02", |
||
342 | "type": "interface", |
||
343 | "uni": True |
||
344 | }, |
||
345 | }, |
||
346 | "manufacturer": "Nicira, Inc.", |
||
347 | "metadata": {}, |
||
348 | "name": "00:00:00:00:00:00:00:04", |
||
349 | "ofp_version": "0x04", |
||
350 | "serial": "XX serial number", |
||
351 | "software": "2.10.7", |
||
352 | "type": "switch" |
||
353 | } |
||
354 | |||
355 | 1 | assert len(self.napp.controller.switches) == 0 |
|
356 | 1 | self.napp._load_switch(dpid_b, switch_attrs) |
|
357 | 1 | assert len(self.napp.controller.switches) == 1 |
|
358 | 1 | assert dpid_b in self.napp.controller.switches |
|
359 | |||
360 | 1 | switch = self.napp.controller.switches[dpid_b] |
|
361 | 1 | assert switch.id == dpid_b |
|
362 | 1 | assert switch.dpid == dpid_b |
|
363 | 1 | assert not switch.is_enabled() |
|
364 | 1 | assert not switch.is_active() |
|
365 | 1 | assert switch.description['manufacturer'] == 'Nicira, Inc.' |
|
366 | 1 | assert switch.description['hardware'] == 'Open vSwitch' |
|
367 | 1 | assert switch.description['software'] == '2.10.7' |
|
368 | 1 | assert switch.description['serial'] == 'XX serial number' |
|
369 | 1 | exp_data_path = 'XX Human readable desc of dp' |
|
370 | 1 | assert switch.description['data_path'] == exp_data_path |
|
371 | |||
372 | 1 | assert len(switch.interfaces) == 1 |
|
373 | 1 | assert 1 in switch.interfaces |
|
374 | 1 | assert 2 not in switch.interfaces |
|
375 | |||
376 | 1 | interface = switch.interfaces[1] |
|
377 | 1 | assert interface.id == iface_b |
|
378 | 1 | assert interface.switch.id == dpid_b |
|
379 | 1 | assert interface.port_number == 1 |
|
380 | 1 | assert not interface.is_enabled() |
|
381 | 1 | assert not interface.lldp |
|
382 | 1 | assert interface.uni |
|
383 | 1 | assert not interface.nni |
|
384 | |||
385 | 1 | def test_load_interfaces_tags_values(self): |
|
386 | """Test load_interfaces_tags_values.""" |
||
387 | 1 | dpid_a = "00:00:00:00:00:00:00:01" |
|
388 | 1 | mock_switch_a = get_switch_mock(dpid_a, 0x04) |
|
389 | 1 | mock_interface_a = get_interface_mock('s1-eth1', 1, mock_switch_a) |
|
390 | 1 | mock_interface_a.id = dpid_a + ':1' |
|
391 | 1 | mock_switch_a.interfaces = {1: mock_interface_a} |
|
392 | 1 | ava_tags = {'vlan': [[10, 4095]]} |
|
393 | 1 | tag_ranges = {'vlan': [[5, 4095]]} |
|
394 | 1 | special_available_tags = {'vlan': ["untagged", "any"]} |
|
395 | 1 | special_tags = {'vlan': ["untagged", "any"]} |
|
396 | 1 | interface_details = [{ |
|
397 | "id": mock_interface_a.id, |
||
398 | "available_tags": ava_tags, |
||
399 | "tag_ranges": tag_ranges, |
||
400 | "special_available_tags": special_available_tags, |
||
401 | "special_tags": special_tags |
||
402 | }] |
||
403 | 1 | self.napp.load_interfaces_tags_values(mock_switch_a, |
|
404 | interface_details) |
||
405 | 1 | set_method = mock_interface_a.set_available_tags_tag_ranges |
|
406 | 1 | set_method.assert_called_once_with( |
|
407 | ava_tags, tag_ranges, |
||
408 | special_available_tags, special_tags |
||
409 | ) |
||
410 | |||
411 | 1 | def test_handle_on_interface_tags(self): |
|
412 | """test_handle_on_interface_tags.""" |
||
413 | 1 | dpid_a = "00:00:00:00:00:00:00:01" |
|
414 | 1 | available_tags = {'vlan': [[200, 3000]]} |
|
415 | 1 | tag_ranges = {'vlan': [[20, 20], [200, 3000]]} |
|
416 | 1 | special_available_tags = {'vlan': ["untagged", "any"]} |
|
417 | 1 | mock_switch_a = get_switch_mock(dpid_a, 0x04) |
|
418 | 1 | mock_interface_a = get_interface_mock('s1-eth1', 1, mock_switch_a) |
|
419 | 1 | mock_interface_a.available_tags = available_tags |
|
420 | 1 | mock_interface_a.tag_ranges = tag_ranges |
|
421 | 1 | mock_interface_a.special_available_tags = special_available_tags |
|
422 | 1 | mock_interface_a.special_tags = special_available_tags |
|
423 | 1 | self.napp.handle_on_interface_tags(mock_interface_a) |
|
424 | 1 | tp_controller = self.napp.topo_controller |
|
425 | 1 | args = tp_controller.upsert_interface_details.call_args[0] |
|
426 | 1 | assert args[0] == '00:00:00:00:00:00:00:01:1' |
|
427 | 1 | assert args[1] == {'vlan': [[200, 3000]]} |
|
428 | 1 | assert args[2] == {'vlan': [[20, 20], [200, 3000]]} |
|
429 | |||
430 | 1 | def test_load_link(self): |
|
431 | """Test _load_link.""" |
||
432 | 1 | dpid_a = "00:00:00:00:00:00:00:01" |
|
433 | 1 | dpid_b = "00:00:00:00:00:00:00:02" |
|
434 | 1 | link_id = '4d42dc08522' |
|
435 | 1 | mock_switch_a = get_switch_mock(dpid_a, 0x04) |
|
436 | 1 | mock_switch_b = get_switch_mock(dpid_b, 0x04) |
|
437 | 1 | mock_interface_a = get_interface_mock('s1-eth1', 1, mock_switch_a) |
|
438 | 1 | mock_interface_a.id = dpid_a + ':1' |
|
439 | 1 | mock_interface_a.available_tags = [1, 2, 3] |
|
440 | 1 | mock_interface_b = get_interface_mock('s2-eth1', 1, mock_switch_b) |
|
441 | 1 | mock_interface_b.id = dpid_b + ':1' |
|
442 | 1 | mock_interface_b.available_tags = [1, 2, 3] |
|
443 | 1 | mock_switch_a.interfaces = {1: mock_interface_a} |
|
444 | 1 | mock_switch_b.interfaces = {1: mock_interface_b} |
|
445 | 1 | self.napp.controller.switches[dpid_a] = mock_switch_a |
|
446 | 1 | self.napp.controller.switches[dpid_b] = mock_switch_b |
|
447 | 1 | link_attrs = { |
|
448 | 'enabled': True, |
||
449 | 'id': link_id, |
||
450 | 'metadata': {}, |
||
451 | 'endpoint_a': { |
||
452 | 'id': mock_interface_a.id |
||
453 | }, |
||
454 | 'endpoint_b': { |
||
455 | 'id': mock_interface_b.id |
||
456 | } |
||
457 | } |
||
458 | |||
459 | 1 | self.napp._load_link(link_attrs) |
|
460 | |||
461 | 1 | assert len(self.napp.controller.links) == 1 |
|
462 | 1 | link = list(self.napp.controller.links.values())[0] |
|
463 | |||
464 | 1 | assert link.endpoint_a.id == mock_interface_a.id |
|
465 | 1 | assert link.endpoint_b.id == mock_interface_b.id |
|
466 | 1 | assert mock_interface_a.nni |
|
467 | 1 | assert mock_interface_b.nni |
|
468 | 1 | assert mock_interface_a.update_link.call_count == 1 |
|
469 | 1 | assert mock_interface_b.update_link.call_count == 1 |
|
470 | |||
471 | # test enable/disable |
||
472 | 1 | link_id = '4d42dc08522' |
|
473 | 1 | mock_interface_a = get_interface_mock('s1-eth1', 1, mock_switch_a) |
|
474 | 1 | mock_interface_b = get_interface_mock('s2-eth1', 1, mock_switch_b) |
|
475 | 1 | mock_link = get_link_mock(mock_interface_a, mock_interface_b) |
|
476 | 1 | mock_link.id = link_id |
|
477 | |||
478 | 1 | self.napp.controller.get_link_or_create = MagicMock() |
|
479 | 1 | mock_get_link_or_create = self.napp.controller.get_link_or_create |
|
480 | 1 | mock_get_link_or_create.return_value = (mock_link, True) |
|
481 | |||
482 | 1 | def test_fail_load_link(self): |
|
483 | """Test fail load_link.""" |
||
484 | 1 | self.napp.controller.get_link_or_create = MagicMock() |
|
485 | 1 | mock_get_link_or_create = self.napp.controller.get_link_or_create |
|
486 | 1 | dpid_a = '00:00:00:00:00:00:00:01' |
|
487 | 1 | dpid_b = '00:00:00:00:00:00:00:02' |
|
488 | 1 | link_id = '4d42dc08522' |
|
489 | 1 | mock_switch_a = get_switch_mock(dpid_a) |
|
490 | 1 | mock_switch_b = get_switch_mock(dpid_b) |
|
491 | 1 | mock_interface_a_1 = get_interface_mock('s1-eth1', 1, mock_switch_a) |
|
492 | 1 | mock_interface_b_1 = get_interface_mock('s2-eth1', 1, mock_switch_b) |
|
493 | 1 | mock_link = get_link_mock(mock_interface_a_1, mock_interface_b_1) |
|
494 | 1 | mock_link.id = link_id |
|
495 | 1 | self.napp.controller.links = {link_id: mock_link} |
|
496 | 1 | mock_get_link_or_create.return_value = mock_link |
|
497 | |||
498 | 1 | link_attrs_fail = { |
|
499 | 'enabled': True, |
||
500 | 'id': link_id, |
||
501 | 'metadata': {}, |
||
502 | 'endpoint_a': { |
||
503 | 'id': f"{dpid_a}:999", |
||
504 | }, |
||
505 | 'endpoint_b': { |
||
506 | 'id': f"{dpid_b}:999", |
||
507 | } |
||
508 | } |
||
509 | 1 | with pytest.raises(RestoreError): |
|
510 | 1 | self.napp._load_link(link_attrs_fail) |
|
511 | |||
512 | 1 | link_attrs_fail = { |
|
513 | 'enabled': True, |
||
514 | 'id': link_id, |
||
515 | 'endpoint_a': { |
||
516 | 'id': f"{dpid_a}:1", |
||
517 | }, |
||
518 | 'endpoint_b': { |
||
519 | 'id': f"{dpid_b}:1", |
||
520 | } |
||
521 | } |
||
522 | 1 | with pytest.raises(RestoreError): |
|
523 | 1 | self.napp._load_link(link_attrs_fail) |
|
524 | |||
525 | 1 | @patch('napps.kytos.topology.main.Main.notify_switch_links_status') |
|
526 | 1 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
|
527 | 1 | async def test_enable_switch(self, mock_notify_topo, mock_sw_l_status): |
|
528 | """Test enable_switch.""" |
||
529 | 1 | dpid = "00:00:00:00:00:00:00:01" |
|
530 | 1 | mock_switch = get_switch_mock(dpid) |
|
531 | 1 | self.napp.controller.switches = {dpid: mock_switch} |
|
532 | |||
533 | 1 | endpoint = f"{self.base_endpoint}/switches/{dpid}/enable" |
|
534 | 1 | response = await self.api_client.post(endpoint) |
|
535 | 1 | assert response.status_code == 201 |
|
536 | 1 | assert mock_switch.enable.call_count == 1 |
|
537 | 1 | self.napp.topo_controller.enable_switch.assert_called_once_with(dpid) |
|
538 | 1 | mock_notify_topo.assert_called() |
|
539 | 1 | mock_sw_l_status.assert_called() |
|
540 | |||
541 | # fail case |
||
542 | 1 | mock_switch.enable.call_count = 0 |
|
543 | 1 | dpid = "00:00:00:00:00:00:00:02" |
|
544 | 1 | endpoint = f"{self.base_endpoint}/switches/{dpid}/enable" |
|
545 | 1 | response = await self.api_client.post(endpoint) |
|
546 | assert response.status_code == 404 |
||
547 | assert mock_switch.enable.call_count == 0 |
||
548 | |||
549 | 1 | @patch('napps.kytos.topology.main.Main.notify_link_enabled_state') |
|
550 | 1 | @patch('napps.kytos.topology.main.Main.notify_switch_links_status') |
|
551 | 1 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
|
552 | 1 | async def test_disable_switch(self, *args): |
|
553 | """Test disable_switch.""" |
||
554 | 1 | mock_notify_topo, mock_sw_l_status, mock_noti_link = args |
|
555 | 1 | dpid = "00:00:00:00:00:00:00:01" |
|
556 | 1 | mock_switch = get_switch_mock(dpid) |
|
557 | 1 | interface = Mock() |
|
558 | 1 | interface.link.is_enabled = lambda: True |
|
559 | 1 | interface.link.link_lock = MagicMock() |
|
560 | 1 | mock_switch.interfaces = {1: interface} |
|
561 | 1 | self.napp.controller.switches = {dpid: mock_switch} |
|
562 | |||
563 | 1 | endpoint = f"{self.base_endpoint}/switches/{dpid}/disable" |
|
564 | 1 | response = await self.api_client.post(endpoint) |
|
565 | 1 | assert response.status_code == 201 |
|
566 | 1 | assert mock_switch.disable.call_count == 1 |
|
567 | 1 | assert mock_noti_link.call_count == 1 |
|
568 | 1 | assert mock_noti_link.call_args[0][0] == interface.link |
|
569 | 1 | assert mock_noti_link.call_args[0][1] == "disabled" |
|
570 | 1 | assert interface.link.disable.call_count == 1 |
|
571 | 1 | assert self.napp.topo_controller.bulk_disable_links.call_count == 1 |
|
572 | 1 | self.napp.topo_controller.disable_switch.assert_called_once_with(dpid) |
|
573 | 1 | mock_notify_topo.assert_called() |
|
574 | 1 | mock_sw_l_status.assert_called() |
|
575 | |||
576 | # fail case |
||
577 | 1 | mock_switch.disable.call_count = 0 |
|
578 | 1 | dpid = "00:00:00:00:00:00:00:02" |
|
579 | 1 | endpoint = f"{self.base_endpoint}/switches/{dpid}/disable" |
|
580 | 1 | response = await self.api_client.post(endpoint) |
|
581 | assert response.status_code == 404 |
||
582 | assert mock_switch.disable.call_count == 0 |
||
583 | |||
584 | 1 | async def test_get_switch_metadata(self): |
|
585 | """Test get_switch_metadata.""" |
||
586 | 1 | dpid = "00:00:00:00:00:00:00:01" |
|
587 | 1 | mock_switch = get_switch_mock(dpid) |
|
588 | 1 | mock_switch.metadata = "A" |
|
589 | 1 | self.napp.controller.switches = {dpid: mock_switch} |
|
590 | |||
591 | 1 | endpoint = f"{self.base_endpoint}/switches/{dpid}/metadata" |
|
592 | 1 | response = await self.api_client.get(endpoint) |
|
593 | 1 | assert response.status_code == 200 |
|
594 | 1 | assert response.json() == {"metadata": mock_switch.metadata} |
|
595 | |||
596 | # fail case |
||
597 | 1 | dpid = "00:00:00:00:00:00:00:02" |
|
598 | 1 | endpoint = f"{self.base_endpoint}/switches/{dpid}/metadata" |
|
599 | 1 | response = await self.api_client.get(endpoint) |
|
600 | assert response.status_code == 404 |
||
601 | |||
602 | 1 | @patch('napps.kytos.topology.main.Main.notify_metadata_changes') |
|
603 | 1 | async def test_add_switch_metadata( |
|
604 | self, mock_metadata_changes |
||
605 | ): |
||
606 | """Test add_switch_metadata.""" |
||
607 | 1 | self.napp.controller.loop = asyncio.get_running_loop() |
|
608 | 1 | dpid = "00:00:00:00:00:00:00:01" |
|
609 | 1 | mock_switch = get_switch_mock(dpid) |
|
610 | 1 | self.napp.controller.switches = {dpid: mock_switch} |
|
611 | 1 | payload = {"data": "A"} |
|
612 | |||
613 | 1 | endpoint = f"{self.base_endpoint}/switches/{dpid}/metadata" |
|
614 | 1 | response = await self.api_client.post(endpoint, json=payload) |
|
615 | 1 | assert response.status_code == 201 |
|
616 | |||
617 | 1 | mock_metadata_changes.assert_called() |
|
618 | 1 | self.napp.topo_controller.add_switch_metadata.assert_called_once_with( |
|
619 | dpid, payload |
||
620 | ) |
||
621 | |||
622 | # fail case |
||
623 | 1 | dpid = "00:00:00:00:00:00:00:02" |
|
624 | 1 | endpoint = f"{self.base_endpoint}/switches/{dpid}/metadata" |
|
625 | 1 | response = await self.api_client.post(endpoint, json=payload) |
|
626 | assert response.status_code == 404 |
||
627 | |||
628 | 1 | async def test_add_switch_metadata_wrong_format(self): |
|
629 | """Test add_switch_metadata_wrong_format.""" |
||
630 | 1 | self.napp.controller.loop = asyncio.get_running_loop() |
|
631 | 1 | dpid = "00:00:00:00:00:00:00:01" |
|
632 | 1 | payload = 'A' |
|
633 | |||
634 | 1 | endpoint = f"{self.base_endpoint}/switches/{dpid}/metadata" |
|
635 | 1 | response = await self.api_client.post(endpoint, json=payload) |
|
636 | assert response.status_code == 400 |
||
637 | |||
638 | payload = None |
||
639 | response = await self.api_client.post(endpoint, json=payload) |
||
640 | assert response.status_code == 415 |
||
641 | |||
642 | 1 | @patch('napps.kytos.topology.main.Main.notify_metadata_changes') |
|
643 | 1 | async def test_delete_switch_metadata( |
|
644 | self, mock_metadata_changes |
||
645 | ): |
||
646 | """Test delete_switch_metadata.""" |
||
647 | 1 | self.napp.controller.loop = asyncio.get_running_loop() |
|
648 | 1 | dpid = "00:00:00:00:00:00:00:01" |
|
649 | 1 | mock_switch = get_switch_mock(dpid) |
|
650 | 1 | mock_switch.metadata = {"A": "A"} |
|
651 | 1 | self.napp.controller.switches = {dpid: mock_switch} |
|
652 | |||
653 | 1 | key = "A" |
|
654 | 1 | endpoint = f"{self.base_endpoint}/switches/{dpid}/metadata/{key}" |
|
655 | 1 | response = await self.api_client.delete(endpoint) |
|
656 | |||
657 | 1 | assert response.status_code == 200 |
|
658 | 1 | assert mock_metadata_changes.call_count == 1 |
|
659 | 1 | del_key_mock = self.napp.topo_controller.delete_switch_metadata_key |
|
660 | 1 | del_key_mock.assert_called_with( |
|
661 | dpid, key |
||
662 | ) |
||
663 | |||
664 | # 404, Metadata not found |
||
665 | 1 | mock_switch.metadata = {} |
|
666 | 1 | endpoint = f"{self.base_endpoint}/switches/{dpid}/metadata/{key}" |
|
667 | 1 | response = await self.api_client.delete(endpoint) |
|
668 | assert response.status_code == 404 |
||
669 | |||
670 | # 404, Switch not found |
||
671 | key = "A" |
||
672 | dpid = "00:00:00:00:00:00:00:02" |
||
673 | endpoint = f"{self.base_endpoint}/switches/{dpid}/metadata/{key}" |
||
674 | response = await self.api_client.delete(endpoint) |
||
675 | assert mock_metadata_changes.call_count == 1 |
||
676 | assert response.status_code == 404 |
||
677 | |||
678 | # pylint: disable=too-many-statements |
||
679 | 1 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
|
680 | 1 | async def test_enable_interfaces(self, mock_notify_topo): |
|
681 | """Test enable_interfaces.""" |
||
682 | 1 | dpid = '00:00:00:00:00:00:00:01' |
|
683 | 1 | mock_switch = get_switch_mock(dpid) |
|
684 | 1 | mock_interface_1 = get_interface_mock('s1-eth1', 1, mock_switch) |
|
685 | 1 | mock_interface_1.link = Mock() |
|
686 | 1 | mock_interface_1.link.link_lock = MagicMock() |
|
687 | 1 | mock_interface_1.link._enabled = True |
|
688 | 1 | mock_interface_2 = get_interface_mock('s1-eth2', 2, mock_switch) |
|
689 | 1 | mock_interface_2.link = Mock() |
|
690 | 1 | mock_interface_2.link.link_lock = MagicMock() |
|
691 | 1 | mock_interface_2.link._enabled = False |
|
692 | 1 | mock_switch.interfaces = {1: mock_interface_1, 2: mock_interface_2} |
|
693 | |||
694 | # Switch not found |
||
695 | 1 | interface_id = '00:00:00:00:00:00:00:01:1' |
|
696 | 1 | endpoint = f"{self.base_endpoint}/interfaces/{interface_id}/enable" |
|
697 | 1 | response = await self.api_client.post(endpoint) |
|
698 | assert response.status_code == 404 |
||
699 | |||
700 | # Switch not enabled |
||
701 | 1 | mock_switch.is_enabled = lambda: False |
|
702 | self.napp.controller.switches = {dpid: mock_switch} |
||
703 | endpoint = f"{self.base_endpoint}/interfaces/{interface_id}/enable" |
||
704 | response = await self.api_client.post(endpoint) |
||
705 | assert response.status_code == 409 |
||
706 | |||
707 | # Success |
||
708 | 1 | mock_switch.is_enabled = lambda: True |
|
709 | endpoint = f"{self.base_endpoint}/interfaces/{interface_id}/enable" |
||
710 | response = await self.api_client.post(endpoint) |
||
711 | 1 | assert response.status_code == 200 |
|
712 | 1 | assert mock_interface_1.enable.call_count == 1 |
|
713 | 1 | assert mock_interface_2.enable.call_count == 0 |
|
714 | 1 | self.napp.topo_controller.enable_interface.assert_called_with( |
|
715 | interface_id |
||
716 | ) |
||
717 | 1 | mock_notify_topo.assert_called() |
|
718 | |||
719 | 1 | mock_interface_1.enable.call_count = 0 |
|
720 | 1 | mock_interface_2.enable.call_count = 0 |
|
721 | 1 | endpoint = f"{self.base_endpoint}/interfaces/switch/{dpid}/enable" |
|
722 | 1 | response = await self.api_client.post(endpoint) |
|
723 | 1 | assert response.status_code == 200 |
|
724 | 1 | self.napp.topo_controller.upsert_switch.assert_called_with( |
|
725 | mock_switch.id, mock_switch.as_dict() |
||
726 | ) |
||
727 | 1 | assert mock_interface_1.enable.call_count == 1 |
|
728 | 1 | assert mock_interface_2.enable.call_count == 1 |
|
729 | |||
730 | # test interface not found |
||
731 | 1 | interface_id = '00:00:00:00:00:00:00:01:3' |
|
732 | 1 | mock_interface_1.enable.call_count = 0 |
|
733 | 1 | mock_interface_2.enable.call_count = 0 |
|
734 | 1 | endpoint = f"{self.base_endpoint}/interfaces/{interface_id}/enable" |
|
735 | 1 | response = await self.api_client.post(endpoint) |
|
736 | assert response.status_code == 404 |
||
737 | assert mock_interface_1.enable.call_count == 0 |
||
738 | assert mock_interface_2.enable.call_count == 0 |
||
739 | |||
740 | # test switch not found |
||
741 | dpid = '00:00:00:00:00:00:00:02' |
||
742 | endpoint = f"{self.base_endpoint}/interfaces/{interface_id}/enable" |
||
743 | response = await self.api_client.post(endpoint) |
||
744 | assert response.status_code == 404 |
||
745 | assert mock_interface_1.enable.call_count == 0 |
||
746 | assert mock_interface_2.enable.call_count == 0 |
||
747 | |||
748 | 1 | @patch('napps.kytos.topology.main.Main.notify_link_enabled_state') |
|
749 | 1 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
|
750 | 1 | async def test_disable_interfaces(self, mock_notify_topo, mock_noti_link): |
|
751 | """Test disable_interfaces.""" |
||
752 | 1 | interface_id = '00:00:00:00:00:00:00:01:1' |
|
753 | 1 | dpid = '00:00:00:00:00:00:00:01' |
|
754 | 1 | mock_switch = get_switch_mock(dpid) |
|
755 | 1 | mock_interface_1 = get_interface_mock('s1-eth1', 1, mock_switch) |
|
756 | 1 | mock_interface_1.link = Mock() |
|
757 | 1 | mock_interface_1.link.is_enabled = lambda: True |
|
758 | 1 | mock_interface_1.link.link_lock = MagicMock() |
|
759 | 1 | mock_interface_2 = get_interface_mock('s1-eth2', 2, mock_switch) |
|
760 | 1 | mock_interface_2.link = Mock() |
|
761 | 1 | mock_interface_2.link.is_enabled = lambda: False |
|
762 | 1 | mock_interface_2.link.link_lock = MagicMock() |
|
763 | 1 | mock_switch.interfaces = {1: mock_interface_1, 2: mock_interface_2} |
|
764 | 1 | self.napp.controller.switches = {dpid: mock_switch} |
|
765 | |||
766 | 1 | endpoint = f"{self.base_endpoint}/interfaces/{interface_id}/disable" |
|
767 | 1 | response = await self.api_client.post(endpoint) |
|
768 | 1 | assert response.status_code == 200 |
|
769 | |||
770 | 1 | self.napp.topo_controller.disable_interface.assert_called_with( |
|
771 | interface_id |
||
772 | ) |
||
773 | 1 | assert mock_interface_1.disable.call_count == 1 |
|
774 | 1 | assert mock_interface_2.disable.call_count == 0 |
|
775 | 1 | assert mock_interface_1.link.disable.call_count == 1 |
|
776 | 1 | assert mock_noti_link.call_count == 1 |
|
777 | 1 | assert mock_noti_link.call_args[0][0] == mock_interface_1.link |
|
778 | 1 | assert mock_noti_link.call_args[0][1] == "disabled" |
|
779 | 1 | assert self.napp.topo_controller.disable_interface.call_count == 1 |
|
780 | 1 | mock_notify_topo.assert_called() |
|
781 | |||
782 | 1 | mock_interface_1.disable.call_count = 0 |
|
783 | 1 | mock_interface_2.disable.call_count = 0 |
|
784 | |||
785 | 1 | endpoint = f"{self.base_endpoint}/interfaces/switch/{dpid}/disable" |
|
786 | 1 | response = await self.api_client.post(endpoint) |
|
787 | 1 | assert response.status_code == 200 |
|
788 | |||
789 | 1 | self.napp.topo_controller.upsert_switch.assert_called_with( |
|
790 | mock_switch.id, mock_switch.as_dict() |
||
791 | ) |
||
792 | 1 | assert mock_interface_1.disable.call_count == 1 |
|
793 | 1 | assert mock_interface_1.link.disable.call_count == 2 |
|
794 | 1 | assert mock_interface_2.disable.call_count == 1 |
|
795 | 1 | assert mock_noti_link.call_count == 2 |
|
796 | 1 | assert mock_noti_link.call_args[0][0] == mock_interface_1.link |
|
797 | 1 | assert mock_noti_link.call_args[0][1] == "disabled" |
|
798 | 1 | bulk_controller = self.napp.topo_controller.bulk_disable_links |
|
799 | 1 | assert bulk_controller.call_count == 2 |
|
800 | 1 | assert len(bulk_controller.call_args[0][0]) == 1 |
|
801 | |||
802 | # test interface not found |
||
803 | 1 | interface_id = '00:00:00:00:00:00:00:01:3' |
|
804 | 1 | mock_interface_1.disable.call_count = 0 |
|
805 | 1 | mock_interface_2.disable.call_count = 0 |
|
806 | 1 | endpoint = f"{self.base_endpoint}/interfaces/{interface_id}/disable" |
|
807 | 1 | response = await self.api_client.post(endpoint) |
|
808 | |||
809 | assert response.status_code == 404 |
||
810 | assert mock_interface_1.disable.call_count == 0 |
||
811 | assert mock_interface_2.disable.call_count == 0 |
||
812 | |||
813 | # test switch not found |
||
814 | dpid = '00:00:00:00:00:00:00:02' |
||
815 | endpoint = f"{self.base_endpoint}/interfaces/switch/{dpid}/disable" |
||
816 | response = await self.api_client.post(endpoint) |
||
817 | assert response.status_code == 404 |
||
818 | assert mock_interface_1.disable.call_count == 0 |
||
819 | assert mock_interface_2.disable.call_count == 0 |
||
820 | |||
821 | 1 | async def test_get_interface_metadata(self): |
|
822 | """Test get_interface_metada.""" |
||
823 | 1 | interface_id = '00:00:00:00:00:00:00:01:1' |
|
824 | 1 | dpid = '00:00:00:00:00:00:00:01' |
|
825 | 1 | mock_switch = get_switch_mock(dpid) |
|
826 | 1 | mock_interface = get_interface_mock('s1-eth1', 1, mock_switch) |
|
827 | 1 | mock_interface.metadata = {"A": "B"} |
|
828 | 1 | mock_switch.interfaces = {1: mock_interface} |
|
829 | 1 | self.napp.controller.switches = {dpid: mock_switch} |
|
830 | |||
831 | 1 | endpoint = f"{self.base_endpoint}/interfaces/{interface_id}/metadata" |
|
832 | 1 | response = await self.api_client.get(endpoint) |
|
833 | 1 | assert response.status_code == 200 |
|
834 | 1 | assert response.json() == {"metadata": mock_interface.metadata} |
|
835 | |||
836 | # fail case switch not found |
||
837 | 1 | interface_id = '00:00:00:00:00:00:00:02:1' |
|
838 | 1 | endpoint = f"{self.base_endpoint}/interfaces/{interface_id}/metadata" |
|
839 | 1 | response = await self.api_client.get(endpoint) |
|
840 | assert response.status_code == 404 |
||
841 | |||
842 | # fail case interface not found |
||
843 | interface_id = '00:00:00:00:00:00:00:01:2' |
||
844 | endpoint = f"{self.base_endpoint}/interfaces/{interface_id}/metadata" |
||
845 | response = await self.api_client.get(endpoint) |
||
846 | assert response.status_code == 404 |
||
847 | |||
848 | 1 | @patch('napps.kytos.topology.main.Main.notify_metadata_changes') |
|
849 | 1 | async def test_add_interface_metadata( |
|
850 | self, mock_metadata_changes |
||
851 | ): |
||
852 | """Test add_interface_metadata.""" |
||
853 | 1 | self.napp.controller.loop = asyncio.get_running_loop() |
|
854 | 1 | interface_id = '00:00:00:00:00:00:00:01:1' |
|
855 | 1 | dpid = '00:00:00:00:00:00:00:01' |
|
856 | 1 | mock_switch = get_switch_mock(dpid) |
|
857 | 1 | mock_interface = get_interface_mock('s1-eth1', 1, mock_switch) |
|
858 | 1 | mock_interface.metadata = {"metada": "A"} |
|
859 | 1 | mock_switch.interfaces = {1: mock_interface} |
|
860 | 1 | self.napp.controller.switches = {dpid: mock_switch} |
|
861 | 1 | payload = {"metada": "A"} |
|
862 | 1 | endpoint = f"{self.base_endpoint}/interfaces/{interface_id}/metadata" |
|
863 | 1 | response = await self.api_client.post(endpoint, json=payload) |
|
864 | 1 | assert response.status_code == 201 |
|
865 | 1 | mock_metadata_changes.assert_called() |
|
866 | |||
867 | # fail case switch not found |
||
868 | 1 | interface_id = '00:00:00:00:00:00:00:02:1' |
|
869 | 1 | endpoint = f"{self.base_endpoint}/interfaces/{interface_id}/metadata" |
|
870 | 1 | response = await self.api_client.post(endpoint, json=payload) |
|
871 | assert response.status_code == 404 |
||
872 | |||
873 | # fail case interface not found |
||
874 | interface_id = '00:00:00:00:00:00:00:01:2' |
||
875 | endpoint = f"{self.base_endpoint}/interfaces/{interface_id}/metadata" |
||
876 | response = await self.api_client.post(endpoint, json=payload) |
||
877 | assert response.status_code == 404 |
||
878 | |||
879 | 1 | async def test_add_interface_metadata_wrong_format(self): |
|
880 | """Test add_interface_metadata_wrong_format.""" |
||
881 | 1 | self.napp.controller.loop = asyncio.get_running_loop() |
|
882 | 1 | interface_id = "00:00:00:00:00:00:00:01:1" |
|
883 | 1 | endpoint = f"{self.base_endpoint}/interfaces/{interface_id}/metadata" |
|
884 | 1 | response = await self.api_client.post(endpoint, json='A') |
|
885 | assert response.status_code == 400 |
||
886 | response = await self.api_client.post(endpoint, json=None) |
||
887 | assert response.status_code == 415 |
||
888 | |||
889 | 1 | async def test_delete_interface_metadata(self): |
|
890 | """Test delete_interface_metadata.""" |
||
891 | 1 | self.napp.controller.loop = asyncio.get_running_loop() |
|
892 | 1 | interface_id = '00:00:00:00:00:00:00:01:1' |
|
893 | 1 | dpid = '00:00:00:00:00:00:00:01' |
|
894 | 1 | mock_switch = get_switch_mock(dpid) |
|
895 | 1 | mock_interface = get_interface_mock('s1-eth1', 1, mock_switch) |
|
896 | 1 | mock_interface.remove_metadata.side_effect = [True, False] |
|
897 | 1 | mock_interface.metadata = {"A": "A"} |
|
898 | 1 | mock_switch.interfaces = {1: mock_interface} |
|
899 | 1 | self.napp.controller.switches = {'00:00:00:00:00:00:00:01': |
|
900 | mock_switch} |
||
901 | |||
902 | 1 | key = 'A' |
|
903 | 1 | url = f"{self.base_endpoint}/interfaces/{interface_id}/metadata/{key}" |
|
904 | 1 | response = await self.api_client.delete(url) |
|
905 | 1 | assert response.status_code == 200 |
|
906 | |||
907 | 1 | del_key_mock = self.napp.topo_controller.delete_interface_metadata_key |
|
908 | 1 | del_key_mock.assert_called_once_with(interface_id, key) |
|
909 | |||
910 | # fail case switch not found |
||
911 | 1 | key = 'A' |
|
912 | 1 | interface_id = '00:00:00:00:00:00:00:02:1' |
|
913 | 1 | url = f"{self.base_endpoint}/interfaces/{interface_id}/metadata/{key}" |
|
914 | 1 | response = await self.api_client.delete(url) |
|
915 | assert response.status_code == 404 |
||
916 | |||
917 | # fail case interface not found |
||
918 | key = 'A' |
||
919 | interface_id = '00:00:00:00:00:00:00:01:2' |
||
920 | url = f"{self.base_endpoint}/interfaces/{interface_id}/metadata/{key}" |
||
921 | response = await self.api_client.delete(url) |
||
922 | assert response.status_code == 404 |
||
923 | |||
924 | # fail case metadata not found |
||
925 | key = 'B' |
||
926 | interface_id = '00:00:00:00:00:00:00:01:1' |
||
927 | url = f"{self.base_endpoint}/interfaces/{interface_id}/metadata/{key}" |
||
928 | response = await self.api_client.delete(url) |
||
929 | assert response.status_code == 404 |
||
930 | |||
931 | 1 | @patch('napps.kytos.topology.main.Main.notify_link_enabled_state') |
|
932 | 1 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
|
933 | 1 | async def test_enable_link(self, mock_notify_topo, mock_noti_link): |
|
934 | """Test enable_link.""" |
||
935 | 1 | mock_link = MagicMock(Link) |
|
936 | 1 | mock_link.link_lock = MagicMock() |
|
937 | 1 | link_id = "1" |
|
938 | 1 | mock_link.id = link_id |
|
939 | 1 | mock_link.is_enabled = lambda: False |
|
940 | 1 | mock_link.endpoint_a = MagicMock(is_enabled=lambda: True) |
|
941 | 1 | mock_link.endpoint_b = MagicMock(is_enabled=lambda: True) |
|
942 | 1 | self.napp.controller.links = {'1': mock_link} |
|
943 | |||
944 | # 409, endpoint is/are disabled |
||
945 | 1 | mock_link.endpoint_a.is_enabled = lambda: False |
|
946 | 1 | mock_link.endpoint_b.is_enabled = lambda: False |
|
947 | 1 | endpoint = f"{self.base_endpoint}/links/{link_id}/enable" |
|
948 | 1 | response = await self.api_client.post(endpoint) |
|
949 | assert response.status_code == 409 |
||
950 | |||
951 | 1 | mock_link.endpoint_a.is_enabled = lambda: True |
|
952 | endpoint = f"{self.base_endpoint}/links/{link_id}/enable" |
||
953 | response = await self.api_client.post(endpoint) |
||
954 | assert response.status_code == 409 |
||
955 | |||
956 | # Success |
||
957 | 1 | mock_link.endpoint_b.is_enabled = lambda: True |
|
958 | endpoint = f"{self.base_endpoint}/links/{link_id}/enable" |
||
959 | response = await self.api_client.post(endpoint) |
||
960 | 1 | assert response.status_code == 201 |
|
961 | 1 | assert mock_noti_link.call_count == 1 |
|
962 | 1 | assert mock_noti_link.call_args[0][0] == mock_link |
|
963 | 1 | assert mock_noti_link.call_args[0][1] == "enabled" |
|
964 | 1 | mock_notify_topo.assert_called() |
|
965 | |||
966 | # 404, link not found |
||
967 | 1 | link_id = "2" |
|
968 | 1 | endpoint = f"{self.base_endpoint}/links/{link_id}/enable" |
|
969 | 1 | response = await self.api_client.post(endpoint) |
|
970 | assert response.status_code == 404 |
||
971 | assert mock_noti_link.call_count == 1 |
||
972 | |||
973 | 1 | @patch('napps.kytos.topology.main.Main.notify_link_enabled_state') |
|
974 | 1 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
|
975 | 1 | async def test_disable_link(self, mock_notify_topo, mock_notify): |
|
976 | """Test disable_link.""" |
||
977 | 1 | mock_link = MagicMock(Link) |
|
978 | 1 | mock_link.link_lock = MagicMock() |
|
979 | 1 | mock_link.is_enabled = lambda: True |
|
980 | 1 | self.napp.controller.links = {'1': mock_link} |
|
981 | |||
982 | 1 | link_id = "1" |
|
983 | 1 | endpoint = f"{self.base_endpoint}/links/{link_id}/disable" |
|
984 | 1 | response = await self.api_client.post(endpoint) |
|
985 | 1 | assert response.status_code == 201 |
|
986 | 1 | assert mock_notify_topo.call_count == 1 |
|
987 | 1 | assert mock_notify.call_count == 1 |
|
988 | 1 | assert mock_notify.call_args[0][0] == mock_link |
|
989 | 1 | assert mock_notify.call_args[0][1] == "disabled" |
|
990 | 1 | assert mock_link.disable.call_count == 1 |
|
991 | 1 | assert self.napp.topo_controller.disable_link.call_count == 1 |
|
992 | |||
993 | # fail case |
||
994 | 1 | link_id = "2" |
|
995 | 1 | endpoint = f"{self.base_endpoint}/links/{link_id}/disable" |
|
996 | 1 | response = await self.api_client.post(endpoint) |
|
997 | assert response.status_code == 404 |
||
998 | |||
999 | 1 | def test_handle_lldp_status_updated(self): |
|
1000 | """Test handle_lldp_status_updated.""" |
||
1001 | 1 | event = MagicMock() |
|
1002 | 1 | self.napp.controller.buffers.app.put = MagicMock() |
|
1003 | |||
1004 | 1 | dpid_a = "00:00:00:00:00:00:00:01" |
|
1005 | 1 | dpid_b = "00:00:00:00:00:00:00:02" |
|
1006 | 1 | dpids = [dpid_a, dpid_b] |
|
1007 | 1 | interface_ids = [f"{dpid}:1" for dpid in dpids] |
|
1008 | |||
1009 | 1 | mock_switch_a = get_switch_mock(dpid_a, 0x04) |
|
1010 | 1 | mock_switch_b = get_switch_mock(dpid_b, 0x04) |
|
1011 | 1 | self.napp.controller.switches = {dpid_a: mock_switch_a, |
|
1012 | dpid_b: mock_switch_b} |
||
1013 | |||
1014 | 1 | event.content = {"interface_ids": interface_ids, "state": "disabled"} |
|
1015 | 1 | self.napp.handle_lldp_status_updated(event) |
|
1016 | |||
1017 | 1 | mock_put = self.napp.controller.buffers.app.put |
|
1018 | 1 | assert mock_put.call_count == len(interface_ids) |
|
1019 | |||
1020 | 1 | def test_handle_topo_controller_upsert_switch(self): |
|
1021 | """Test handle_topo_controller_upsert_switch.""" |
||
1022 | 1 | event = MagicMock() |
|
1023 | 1 | self.napp.handle_topo_controller_upsert_switch(event) |
|
1024 | 1 | mock = self.napp.topo_controller.upsert_switch |
|
1025 | 1 | mock.assert_called_with(event.id, event.as_dict()) |
|
1026 | |||
1027 | 1 | async def test_get_link_metadata(self): |
|
1028 | """Test get_link_metadata.""" |
||
1029 | 1 | mock_link = MagicMock(Link) |
|
1030 | 1 | mock_link.link_lock = MagicMock() |
|
1031 | 1 | mock_link.metadata = "A" |
|
1032 | 1 | self.napp.controller.links = {'1': mock_link} |
|
1033 | 1 | msg_success = {"metadata": "A"} |
|
1034 | |||
1035 | 1 | link_id = "1" |
|
1036 | 1 | endpoint = f"{self.base_endpoint}/links/{link_id}/metadata" |
|
1037 | 1 | response = await self.api_client.get(endpoint) |
|
1038 | 1 | assert response.status_code == 200 |
|
1039 | 1 | assert msg_success == response.json() |
|
1040 | |||
1041 | # fail case |
||
1042 | 1 | link_id = "2" |
|
1043 | 1 | endpoint = f"{self.base_endpoint}/links/{link_id}/metadata" |
|
1044 | 1 | response = await self.api_client.get(endpoint) |
|
1045 | assert response.status_code == 404 |
||
1046 | |||
1047 | 1 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
|
1048 | 1 | @patch('napps.kytos.topology.main.Main.notify_metadata_changes') |
|
1049 | 1 | async def test_add_link_metadata( |
|
1050 | self, |
||
1051 | mock_metadata_changes, |
||
1052 | mock_topology_update |
||
1053 | ): |
||
1054 | """Test add_link_metadata.""" |
||
1055 | 1 | self.napp.controller.loop = asyncio.get_running_loop() |
|
1056 | 1 | mock_link = MagicMock(Link) |
|
1057 | 1 | mock_link.link_lock = MagicMock() |
|
1058 | 1 | mock_link.metadata = "A" |
|
1059 | 1 | self.napp.controller.links = {'1': mock_link} |
|
1060 | 1 | payload = {"metadata": "A"} |
|
1061 | 1 | link_id = 1 |
|
1062 | |||
1063 | 1 | endpoint = f"{self.base_endpoint}/links/{link_id}/metadata" |
|
1064 | 1 | response = await self.api_client.post(endpoint, json=payload) |
|
1065 | 1 | assert response.status_code == 201 |
|
1066 | 1 | mock_metadata_changes.assert_called() |
|
1067 | 1 | mock_topology_update.assert_called() |
|
1068 | |||
1069 | # fail case |
||
1070 | 1 | link_id = 2 |
|
1071 | 1 | endpoint = f"{self.base_endpoint}/links/{link_id}/metadata" |
|
1072 | 1 | response = await self.api_client.post(endpoint, json=payload) |
|
1073 | assert response.status_code == 404 |
||
1074 | |||
1075 | 1 | async def test_add_link_metadata_wrong_format(self): |
|
1076 | """Test add_link_metadata_wrong_format.""" |
||
1077 | 1 | self.napp.controller.loop = asyncio.get_running_loop() |
|
1078 | 1 | link_id = 'cf0f4071be426b3f745027f5d22' |
|
1079 | 1 | payload = "A" |
|
1080 | 1 | endpoint = f"{self.base_endpoint}/links/{link_id}/metadata" |
|
1081 | 1 | response = await self.api_client.post(endpoint, json=payload) |
|
1082 | assert response.status_code == 400 |
||
1083 | |||
1084 | payload = None |
||
1085 | response = await self.api_client.post(endpoint, json=payload) |
||
1086 | assert response.status_code == 415 |
||
1087 | |||
1088 | 1 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
|
1089 | 1 | @patch('napps.kytos.topology.main.Main.notify_metadata_changes') |
|
1090 | 1 | async def test_delete_link_metadata( |
|
1091 | self, |
||
1092 | mock_metadata_changes, |
||
1093 | mock_topology_update |
||
1094 | ): |
||
1095 | """Test delete_link_metadata.""" |
||
1096 | 1 | mock_link = MagicMock(Link) |
|
1097 | 1 | mock_link.link_lock = MagicMock() |
|
1098 | 1 | mock_link.metadata = {"A": "A"} |
|
1099 | 1 | mock_link.remove_metadata.side_effect = [True, False] |
|
1100 | 1 | self.napp.controller.links = {'1': mock_link} |
|
1101 | |||
1102 | 1 | link_id = 1 |
|
1103 | 1 | key = 'A' |
|
1104 | 1 | endpoint = f"{self.base_endpoint}/links/{link_id}/metadata/{key}" |
|
1105 | 1 | response = await self.api_client.delete(endpoint) |
|
1106 | 1 | assert response.status_code == 200 |
|
1107 | 1 | del_mock = self.napp.topo_controller.delete_link_metadata_key |
|
1108 | 1 | del_mock.assert_called_once_with(mock_link.id, key) |
|
1109 | 1 | mock_metadata_changes.assert_called() |
|
1110 | 1 | mock_topology_update.assert_called() |
|
1111 | |||
1112 | # fail case link not found |
||
1113 | 1 | link_id = 2 |
|
1114 | 1 | key = 'A' |
|
1115 | 1 | endpoint = f"{self.base_endpoint}/links/{link_id}/metadata/{key}" |
|
1116 | 1 | response = await self.api_client.delete(endpoint) |
|
1117 | assert response.status_code == 404 |
||
1118 | |||
1119 | # fail case metadata not found |
||
1120 | link_id = 1 |
||
1121 | key = 'B' |
||
1122 | endpoint = f"{self.base_endpoint}/links/{link_id}/metadata/{key}" |
||
1123 | response = await self.api_client.delete(endpoint) |
||
1124 | assert response.status_code == 404 |
||
1125 | |||
1126 | 1 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
|
1127 | 1 | def test_handle_new_switch(self, mock_notify_topology_update): |
|
1128 | """Test handle_new_switch.""" |
||
1129 | 1 | mock_event = MagicMock() |
|
1130 | 1 | mock_switch = create_autospec(Switch) |
|
1131 | 1 | mock_event.content['switch'] = mock_switch |
|
1132 | 1 | self.napp.handle_new_switch(mock_event) |
|
1133 | 1 | mock = self.napp.topo_controller.upsert_switch |
|
1134 | 1 | mock.assert_called_once_with(mock_event.content['switch'].id, |
|
1135 | mock_event.content['switch'].as_dict()) |
||
1136 | 1 | mock_notify_topology_update.assert_called() |
|
1137 | |||
1138 | 1 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
|
1139 | 1 | def test_handle_connection_lost(self, mock_notify_topology_update): |
|
1140 | """Test handle connection_lost.""" |
||
1141 | 1 | mock_event = MagicMock() |
|
1142 | 1 | mock_switch = create_autospec(Switch) |
|
1143 | 1 | mock_switch.return_value = True |
|
1144 | 1 | mock_event.content['source'] = mock_switch |
|
1145 | 1 | self.napp.handle_connection_lost(mock_event) |
|
1146 | 1 | mock_notify_topology_update.assert_called() |
|
1147 | |||
1148 | 1 | @patch('napps.kytos.topology.main.Main.handle_interface_link_down') |
|
1149 | 1 | @patch('napps.kytos.topology.main.Main.handle_interface_link_up') |
|
1150 | 1 | def test_handle_interface_created(self, mock_link_up, mock_link_down): |
|
1151 | """Test handle_interface_created.""" |
||
1152 | 1 | mock_event = MagicMock() |
|
1153 | 1 | mock_interface = create_autospec(Interface) |
|
1154 | 1 | mock_interface.id = "1" |
|
1155 | 1 | mock_event.content = {'interface': mock_interface} |
|
1156 | 1 | self.napp.handle_interface_created(mock_event) |
|
1157 | 1 | mock_link_up.assert_called() |
|
1158 | 1 | mock_link_down.assert_not_called() |
|
1159 | |||
1160 | 1 | @patch('napps.kytos.topology.main.Main.handle_interface_link_down') |
|
1161 | 1 | @patch('napps.kytos.topology.main.Main.handle_interface_link_up') |
|
1162 | 1 | def test_handle_interface_created_inactive(self, mock_link_up, |
|
1163 | mock_link_down): |
||
1164 | """Test handle_interface_created inactive.""" |
||
1165 | 1 | mock_event = MagicMock() |
|
1166 | 1 | mock_interface = create_autospec(Interface) |
|
1167 | 1 | mock_interface.id = "1" |
|
1168 | 1 | mock_event.content = {'interface': mock_interface} |
|
1169 | 1 | mock_interface.is_active.return_value = False |
|
1170 | 1 | self.napp.handle_interface_created(mock_event) |
|
1171 | 1 | mock_link_up.assert_not_called() |
|
1172 | 1 | mock_link_down.assert_called() |
|
1173 | |||
1174 | 1 | def test_handle_interfaces_created(self): |
|
1175 | """Test handle_interfaces_created.""" |
||
1176 | 1 | buffers_app_mock = MagicMock() |
|
1177 | 1 | self.napp.controller.buffers.app = buffers_app_mock |
|
1178 | 1 | mock_switch = create_autospec(Switch) |
|
1179 | 1 | mock_event = MagicMock() |
|
1180 | 1 | mock_interface = create_autospec(Interface) |
|
1181 | 1 | mock_interface.id = "1" |
|
1182 | 1 | mock_interface.switch = mock_switch |
|
1183 | 1 | mock_interface_two = create_autospec(Interface) |
|
1184 | 1 | mock_interface_two.id = "2" |
|
1185 | 1 | mock_event.content = {'interfaces': [mock_interface, |
|
1186 | mock_interface_two]} |
||
1187 | 1 | self.napp.handle_interfaces_created(mock_event) |
|
1188 | 1 | upsert_mock = self.napp.topo_controller.upsert_switch |
|
1189 | 1 | upsert_mock.assert_called_with(mock_switch.id, mock_switch.as_dict()) |
|
1190 | 1 | assert self.napp.controller.buffers.app.put.call_count == 2 |
|
1191 | |||
1192 | 1 | @patch('napps.kytos.topology.main.Main.handle_interface_link_down') |
|
1193 | 1 | def test_handle_interface_down(self, mock_handle_interface_link_down): |
|
1194 | """Test handle interface down.""" |
||
1195 | 1 | mock_event = MagicMock() |
|
1196 | 1 | mock_interface = create_autospec(Interface) |
|
1197 | 1 | mock_event.content['interface'] = mock_interface |
|
1198 | 1 | self.napp.handle_interface_down(mock_event) |
|
1199 | 1 | mock_handle_interface_link_down.assert_called() |
|
1200 | |||
1201 | 1 | @patch('napps.kytos.topology.main.Main.handle_interface_down') |
|
1202 | 1 | def test_interface_deleted(self, mock_handle_interface_link_down): |
|
1203 | """Test interface deleted.""" |
||
1204 | 1 | mock_event = MagicMock() |
|
1205 | 1 | self.napp.handle_interface_deleted(mock_event) |
|
1206 | 1 | mock_handle_interface_link_down.assert_called() |
|
1207 | |||
1208 | 1 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
|
1209 | 1 | def test_interface_link_up(self, mock_notify_topology_update): |
|
1210 | """Test interface link_up.""" |
||
1211 | 1 | self.napp.controller.buffers.app.put = MagicMock() |
|
1212 | |||
1213 | 1 | tnow = time.time() |
|
1214 | 1 | mock_switch_a = create_autospec(Switch) |
|
1215 | 1 | mock_switch_a.is_active.return_value = True |
|
1216 | 1 | mock_switch_b = create_autospec(Switch) |
|
1217 | 1 | mock_switch_b.is_active.return_value = True |
|
1218 | 1 | mock_interface_a = create_autospec(Interface) |
|
1219 | 1 | mock_interface_a.switch = mock_switch_a |
|
1220 | 1 | mock_interface_a.is_active.return_value = False |
|
1221 | 1 | mock_interface_b = create_autospec(Interface) |
|
1222 | 1 | mock_interface_b.switch = mock_switch_b |
|
1223 | 1 | mock_interface_b.is_active.return_value = True |
|
1224 | 1 | mock_link = create_autospec(Link) |
|
1225 | 1 | mock_link.link_lock = MagicMock() |
|
1226 | 1 | mock_link.get_metadata.return_value = tnow |
|
1227 | 1 | mock_link.is_active.return_value = False |
|
1228 | 1 | mock_link.endpoint_a = mock_interface_a |
|
1229 | 1 | mock_link.endpoint_b = mock_interface_b |
|
1230 | 1 | mock_link.status = EntityStatus.UP |
|
1231 | 1 | mock_interface_a.link = mock_link |
|
1232 | 1 | mock_interface_b.link = mock_link |
|
1233 | 1 | event = KytosEvent("kytos.of_core.switch.interface.down") |
|
1234 | 1 | self.napp.handle_interface_link_up(mock_interface_a, event) |
|
1235 | 1 | mock_notify_topology_update.assert_called() |
|
1236 | 1 | assert mock_link.id in self.napp.link_status_change |
|
1237 | 1 | mock_link.activate.assert_called() |
|
1238 | 1 | self.napp.controller.buffers.app.put.assert_not_called() |
|
1239 | |||
1240 | 1 | mock_interface_a.is_active.return_value = True |
|
1241 | 1 | event = KytosEvent("kytos.of_core.switch.interface.down") |
|
1242 | 1 | self.napp.handle_interface_link_up(mock_interface_a, event) |
|
1243 | |||
1244 | 1 | assert mock_link.id in self.napp.link_status_change |
|
1245 | 1 | link_status_info = self.napp.link_status_change[mock_link.id] |
|
1246 | 1 | mock_link.activate.assert_called() |
|
1247 | 1 | assert self.napp.controller.buffers.app.put.call_count == 1 |
|
1248 | 1 | ev = "kytos/topology.notify_link_up_if_status" |
|
1249 | 1 | assert self.napp.controller.buffers.app.put.call_args[0][0].name == ev |
|
1250 | |||
1251 | 1 | mock_link.is_active.return_value = True |
|
1252 | 1 | orig_change_time = link_status_info["last_status_change"] |
|
1253 | |||
1254 | 1 | self.napp.handle_interface_link_up(mock_interface_a, event) |
|
1255 | |||
1256 | 1 | link_status_info = self.napp.link_status_change[mock_link.id] |
|
1257 | 1 | new_change_time = link_status_info["last_status_change"] |
|
1258 | 1 | assert orig_change_time == new_change_time |
|
1259 | |||
1260 | 1 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
|
1261 | 1 | @patch('napps.kytos.topology.main.Main.notify_link_status_change') |
|
1262 | 1 | def test_interface_link_down(self, *args): |
|
1263 | """Test interface link down.""" |
||
1264 | 1 | mock_status_change, mock_topology_update = args |
|
1265 | |||
1266 | 1 | mock_interface = create_autospec(Interface) |
|
1267 | 1 | mock_link = create_autospec(Link) |
|
1268 | 1 | mock_link.link_lock = MagicMock() |
|
1269 | 1 | mock_link.is_active.return_value = True |
|
1270 | 1 | mock_interface.link = mock_link |
|
1271 | 1 | event = KytosEvent("kytos.of_core.switch.interface.link_up") |
|
1272 | 1 | self.napp.handle_interface_link_down(mock_interface, event) |
|
1273 | 1 | mock_topology_update.assert_called() |
|
1274 | 1 | mock_status_change.assert_called() |
|
1275 | |||
1276 | 1 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
|
1277 | 1 | @patch('napps.kytos.topology.main.Main.notify_link_status_change') |
|
1278 | 1 | def test_interface_link_down_unordered_event(self, *args): |
|
1279 | """Test interface link down unordered event.""" |
||
1280 | 1 | (mock_status_change, mock_topology_update) = args |
|
1281 | |||
1282 | 1 | mock_interface = create_autospec(Interface) |
|
1283 | 1 | mock_interface.id = "1" |
|
1284 | 1 | event_2 = KytosEvent("kytos.of_core.switch.interface.down") |
|
1285 | 1 | event_1 = KytosEvent("kytos.of_core.switch.interface.up") |
|
1286 | 1 | assert event_1.timestamp > event_2.timestamp |
|
1287 | 1 | self.napp._intfs_updated_at[mock_interface.id] = event_1.timestamp |
|
1288 | 1 | self.napp.handle_interface_link_down(mock_interface, event_2) |
|
1289 | 1 | mock_topology_update.assert_not_called() |
|
1290 | 1 | mock_status_change.assert_not_called() |
|
1291 | |||
1292 | 1 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
|
1293 | 1 | @patch('napps.kytos.topology.main.Main.notify_link_status_change') |
|
1294 | 1 | def test_interface_link_up_unordered_event(self, *args): |
|
1295 | """Test interface link up unordered event.""" |
||
1296 | 1 | (mock_status_change, mock_topology_update) = args |
|
1297 | |||
1298 | 1 | mock_interface = create_autospec(Interface) |
|
1299 | 1 | mock_interface.id = "1" |
|
1300 | 1 | event_2 = KytosEvent("kytos.of_core.switch.interface.up") |
|
1301 | 1 | event_1 = KytosEvent("kytos.of_core.switch.interface.down") |
|
1302 | 1 | assert event_1.timestamp > event_2.timestamp |
|
1303 | 1 | self.napp._intfs_updated_at[mock_interface.id] = event_1.timestamp |
|
1304 | 1 | self.napp.handle_interface_link_up(mock_interface, event_2) |
|
1305 | 1 | mock_topology_update.assert_not_called() |
|
1306 | 1 | mock_status_change.assert_not_called() |
|
1307 | |||
1308 | 1 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
|
1309 | 1 | @patch('napps.kytos.topology.main.Main.notify_link_status_change') |
|
1310 | 1 | def test_handle_link_down(self, *args): |
|
1311 | """Test interface link down.""" |
||
1312 | 1 | (mock_status_change, mock_topology_update) = args |
|
1313 | |||
1314 | 1 | mock_interface = create_autospec(Interface) |
|
1315 | 1 | mock_link = create_autospec(Link) |
|
1316 | 1 | mock_link.link_lock = MagicMock() |
|
1317 | 1 | mock_link.is_active.return_value = True |
|
1318 | 1 | mock_interface.link = mock_link |
|
1319 | 1 | self.napp.handle_link_down(mock_interface) |
|
1320 | 1 | mock_interface.deactivate.assert_not_called() |
|
1321 | 1 | mock_link.deactivate.assert_called() |
|
1322 | 1 | assert mock_topology_update.call_count == 1 |
|
1323 | 1 | mock_status_change.assert_called() |
|
1324 | |||
1325 | 1 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
|
1326 | 1 | def test_handle_link_down_not_active(self, args): |
|
1327 | """Test interface link down with link not active.""" |
||
1328 | 1 | mock_topology_update = args |
|
1329 | 1 | self.napp.controller.buffers.app.put = MagicMock() |
|
1330 | |||
1331 | 1 | mock_interface = create_autospec(Interface) |
|
1332 | 1 | mock_link = create_autospec(Link) |
|
1333 | 1 | mock_link.link_lock = MagicMock() |
|
1334 | 1 | mock_link.is_active.return_value = False |
|
1335 | 1 | mock_link.get_metadata.return_value = False |
|
1336 | 1 | mock_interface.link = mock_link |
|
1337 | 1 | self.napp.link_up = set() |
|
1338 | 1 | self.napp.link_status_change[mock_link.id] = {} |
|
1339 | 1 | self.napp.handle_link_down(mock_interface) |
|
1340 | 1 | mock_topology_update.assert_called() |
|
1341 | 1 | self.napp.controller.buffers.app.put.assert_not_called() |
|
1342 | |||
1343 | 1 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
|
1344 | 1 | @patch('napps.kytos.topology.main.Main.notify_link_status_change') |
|
1345 | 1 | def test_handle_link_down_not_active_last_status(self, *args): |
|
1346 | """Test interface link down with link not active.""" |
||
1347 | 1 | (mock_status_change, mock_topology_update) = args |
|
1348 | |||
1349 | 1 | mock_interface = create_autospec(Interface) |
|
1350 | 1 | mock_link = create_autospec(Link) |
|
1351 | 1 | mock_link.link_lock = MagicMock() |
|
1352 | 1 | mock_link.is_active.return_value = False |
|
1353 | 1 | mock_link.get_metadata.return_value = True |
|
1354 | 1 | mock_interface.link = mock_link |
|
1355 | 1 | self.napp.handle_link_down(mock_interface) |
|
1356 | 1 | mock_topology_update.assert_called() |
|
1357 | 1 | mock_status_change.assert_called() |
|
1358 | |||
1359 | 1 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
|
1360 | 1 | def test_handle_link_up(self, mock_notify_topology_update): |
|
1361 | """Test handle link up.""" |
||
1362 | 1 | mock_switch_a = create_autospec(Switch) |
|
1363 | 1 | mock_switch_a.is_active.return_value = True |
|
1364 | 1 | mock_interface = create_autospec(Interface) |
|
1365 | 1 | mock_interface.switch = mock_switch_a |
|
1366 | 1 | mock_interface.is_active.return_value = True |
|
1367 | 1 | mock_link = MagicMock(status=EntityStatus.UP) |
|
1368 | 1 | mock_link.is_active.return_value = True |
|
1369 | 1 | mock_interface.link = mock_link |
|
1370 | 1 | self.napp.handle_link_up(mock_interface) |
|
1371 | 1 | mock_interface.activate.assert_not_called() |
|
1372 | 1 | mock_notify_topology_update.assert_called() |
|
1373 | 1 | assert self.napp.controller.buffers.app.put.call_count == 2 |
|
1374 | 1 | ev = "kytos/topology.notify_link_up_if_status" |
|
1375 | 1 | assert self.napp.controller.buffers.app.put.call_args[0][0].name == ev |
|
1376 | |||
1377 | 1 | @patch('time.sleep') |
|
1378 | 1 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
|
1379 | 1 | @patch('napps.kytos.topology.main.Main.notify_link_status_change') |
|
1380 | 1 | def test_handle_link_up_intf_down(self, *args): |
|
1381 | """Test handle link up but one intf down.""" |
||
1382 | 1 | (mock_status_change, mock_topology_update, _) = args |
|
1383 | |||
1384 | 1 | mock_switch = create_autospec(Switch) |
|
1385 | 1 | mock_interface = create_autospec(Interface) |
|
1386 | 1 | mock_interface.switch = mock_switch |
|
1387 | 1 | mock_link = MagicMock() |
|
1388 | 1 | mock_link.endpoint_a.is_active.return_value = False |
|
1389 | 1 | mock_link.is_active.return_value = False |
|
1390 | 1 | mock_interface.link = mock_link |
|
1391 | 1 | self.napp.handle_link_up(mock_interface) |
|
1392 | 1 | mock_interface.activate.assert_not_called() |
|
1393 | 1 | assert mock_topology_update.call_count == 1 |
|
1394 | 1 | mock_status_change.assert_not_called() |
|
1395 | |||
1396 | 1 | @patch('napps.kytos.topology.main.Main.notify_link_up_if_status') |
|
1397 | 1 | def test_add_links(self, mock_notify_link_up_if_status): |
|
1398 | """Test add_links.""" |
||
1399 | 1 | mock_link = MagicMock() |
|
1400 | 1 | self.napp.controller.get_link_or_create = MagicMock() |
|
1401 | 1 | mock_get_link_or_create = self.napp.controller.get_link_or_create |
|
1402 | 1 | mock_get_link_or_create.return_value = (mock_link, True) |
|
1403 | 1 | mock_event = MagicMock() |
|
1404 | 1 | mock_intf_a = MagicMock() |
|
1405 | 1 | mock_intf_b = MagicMock() |
|
1406 | 1 | mock_event.content = { |
|
1407 | "interface_a": mock_intf_a, |
||
1408 | "interface_b": mock_intf_b |
||
1409 | } |
||
1410 | 1 | self.napp.add_links(mock_event) |
|
1411 | 1 | assert mock_link.id in self.napp.link_status_change |
|
1412 | 1 | mock_get_link_or_create.assert_called() |
|
1413 | 1 | mock_notify_link_up_if_status.assert_called() |
|
1414 | |||
1415 | 1 | def test_notify_switch_enabled(self): |
|
1416 | """Test notify switch enabled.""" |
||
1417 | 1 | dpid = "00:00:00:00:00:00:00:01" |
|
1418 | 1 | mock_buffers_put = MagicMock() |
|
1419 | 1 | self.napp.controller.buffers.app.put = mock_buffers_put |
|
1420 | 1 | self.napp.notify_switch_enabled(dpid) |
|
1421 | 1 | mock_buffers_put.assert_called() |
|
1422 | |||
1423 | 1 | def test_notify_switch_disabled(self): |
|
1424 | """Test notify switch disabled.""" |
||
1425 | 1 | dpid = "00:00:00:00:00:00:00:01" |
|
1426 | 1 | mock_buffers_put = MagicMock() |
|
1427 | 1 | self.napp.controller.buffers.app.put = mock_buffers_put |
|
1428 | 1 | self.napp.notify_switch_disabled(dpid) |
|
1429 | 1 | mock_buffers_put.assert_called() |
|
1430 | |||
1431 | 1 | def test_notify_topology_update(self): |
|
1432 | """Test notify_topology_update.""" |
||
1433 | 1 | mock_buffers_put = MagicMock() |
|
1434 | 1 | self.napp.controller.buffers.app.put = mock_buffers_put |
|
1435 | 1 | self.napp.notify_topology_update() |
|
1436 | 1 | mock_buffers_put.assert_called() |
|
1437 | |||
1438 | 1 | def test_notify_link_status_change(self): |
|
1439 | """Test notify link status change.""" |
||
1440 | 1 | mock_buffers_put = MagicMock() |
|
1441 | 1 | self.napp.controller.buffers.app.put = mock_buffers_put |
|
1442 | 1 | mock_link = create_autospec(Link) |
|
1443 | 1 | mock_link.id = 'test_link' |
|
1444 | 1 | mock_link.status_reason = frozenset() |
|
1445 | 1 | mock_link.status = EntityStatus.UP |
|
1446 | |||
1447 | # Check when switching to up |
||
1448 | 1 | self.napp.notify_link_status_change(mock_link, 'test') |
|
1449 | 1 | assert mock_buffers_put.call_count == 1 |
|
1450 | 1 | args, _ = mock_buffers_put.call_args |
|
1451 | 1 | event = args[0] |
|
1452 | 1 | assert event.content['link'] is mock_link |
|
1453 | 1 | assert event.content['reason'] == 'test' |
|
1454 | 1 | assert event.name == 'kytos/topology.link_up' |
|
1455 | |||
1456 | # Check result when no change |
||
1457 | 1 | self.napp.notify_link_status_change(mock_link, 'test2') |
|
1458 | 1 | assert mock_buffers_put.call_count == 1 |
|
1459 | |||
1460 | # Check when switching to down |
||
1461 | 1 | mock_link.status_reason = frozenset({'disabled'}) |
|
1462 | 1 | mock_link.status = EntityStatus.DOWN |
|
1463 | 1 | self.napp.notify_link_status_change(mock_link, 'test3') |
|
1464 | 1 | assert mock_buffers_put.call_count == 2 |
|
1465 | 1 | args, _ = mock_buffers_put.call_args |
|
1466 | 1 | event = args[0] |
|
1467 | 1 | assert event.content['link'] is mock_link |
|
1468 | 1 | assert event.content['reason'] == 'test3' |
|
1469 | 1 | assert event.name == 'kytos/topology.link_down' |
|
1470 | |||
1471 | 1 | def test_notify_metadata_changes(self): |
|
1472 | """Test notify metadata changes.""" |
||
1473 | 1 | mock_buffers_put = MagicMock() |
|
1474 | 1 | self.napp.controller.buffers.app.put = mock_buffers_put |
|
1475 | 1 | count = 0 |
|
1476 | 1 | for spec in [Switch, Interface, Link]: |
|
1477 | 1 | mock_obj = create_autospec(spec) |
|
1478 | 1 | mock_obj.metadata = {"some_key": "some_value"} |
|
1479 | 1 | self.napp.notify_metadata_changes(mock_obj, 'added') |
|
1480 | 1 | assert mock_buffers_put.call_count == count+1 |
|
1481 | 1 | count += 1 |
|
1482 | 1 | with pytest.raises(ValueError): |
|
1483 | 1 | self.napp.notify_metadata_changes(MagicMock(), 'added') |
|
1484 | |||
1485 | 1 | def test_notify_port_created(self): |
|
1486 | """Test notify port created.""" |
||
1487 | 1 | mock_buffers_put = MagicMock() |
|
1488 | 1 | self.napp.controller.buffers.app.put = mock_buffers_put |
|
1489 | 1 | event = KytosEvent("some_event") |
|
1490 | 1 | expected_name = "kytos/topology.port.created" |
|
1491 | 1 | self.napp.notify_port_created(event) |
|
1492 | 1 | assert mock_buffers_put.call_count == 1 |
|
1493 | 1 | assert mock_buffers_put.call_args_list[0][0][0].name == expected_name |
|
1494 | |||
1495 | 1 | def test_handle_link_liveness_disabled(self) -> None: |
|
1496 | """Test handle_link_liveness_disabled.""" |
||
1497 | 1 | interfaces = [MagicMock(id=f"intf{n}") for n in range(4)] |
|
1498 | 1 | links = { |
|
1499 | "link1": MagicMock(id="link1", |
||
1500 | endpoint_a=interfaces[0], |
||
1501 | endpoint_b=interfaces[1]), |
||
1502 | "link2": MagicMock(id="link2", |
||
1503 | endpoint_a=interfaces[2], |
||
1504 | endpoint_b=interfaces[3]), |
||
1505 | } |
||
1506 | 1 | self.napp.controller.links = links |
|
1507 | 1 | self.napp.notify_topology_update = MagicMock() |
|
1508 | 1 | self.napp.notify_link_status_change = MagicMock() |
|
1509 | |||
1510 | 1 | self.napp.handle_link_liveness_disabled(interfaces) |
|
1511 | |||
1512 | 1 | assert self.napp.notify_topology_update.call_count == 1 |
|
1513 | 1 | assert self.napp.notify_link_status_change.call_count == len(links) |
|
1514 | |||
1515 | 1 | def test_link_status_hook_link_up_timer(self) -> None: |
|
1516 | """Test status hook link up timer.""" |
||
1517 | 1 | last_change = time.time() - self.napp.link_up_timer + 5 |
|
1518 | 1 | link = MagicMock(metadata={"last_status_change": last_change}) |
|
1519 | 1 | self.napp.link_status_change[link.id] = { |
|
1520 | "last_status_change": last_change, |
||
1521 | } |
||
1522 | 1 | link.is_active.return_value = True |
|
1523 | 1 | link.is_enabled.return_value = True |
|
1524 | 1 | res = self.napp.link_status_hook_link_up_timer(link) |
|
1525 | 1 | assert res == EntityStatus.DOWN |
|
1526 | |||
1527 | 1 | last_change = time.time() - self.napp.link_up_timer |
|
1528 | 1 | self.napp.link_status_change[link.id] = { |
|
1529 | "last_status_change": last_change, |
||
1530 | } |
||
1531 | 1 | res = self.napp.link_status_hook_link_up_timer(link) |
|
1532 | 1 | assert res is None |
|
1533 | |||
1534 | 1 | @patch('napps.kytos.topology.main.Main.notify_link_status_change') |
|
1535 | 1 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
|
1536 | 1 | @patch('time.sleep') |
|
1537 | 1 | def test_notify_link_up_if_status( |
|
1538 | self, |
||
1539 | mock_sleep, |
||
1540 | mock_notify_topo, |
||
1541 | mock_notify_link, |
||
1542 | ) -> None: |
||
1543 | """Test notify link up if status.""" |
||
1544 | |||
1545 | 1 | link = MagicMock(status=EntityStatus.UP) |
|
1546 | 1 | self.napp.link_status_change[link.id] = { |
|
1547 | "notified_up_at": now(), |
||
1548 | } |
||
1549 | 1 | assert not self.napp.notify_link_up_if_status(link, "link up") |
|
1550 | 1 | link.update_metadata.assert_not_called() |
|
1551 | 1 | mock_notify_topo.assert_not_called() |
|
1552 | 1 | mock_notify_link.assert_not_called() |
|
1553 | |||
1554 | 1 | link = MagicMock(status=EntityStatus.UP) |
|
1555 | 1 | orig_time = now() - timedelta(seconds=60) |
|
1556 | 1 | self.napp.link_status_change[link.id] = { |
|
1557 | "notified_up_at": orig_time, |
||
1558 | } |
||
1559 | 1 | assert not self.napp.notify_link_up_if_status(link, "link up") |
|
1560 | 1 | link_status_info = self.napp.link_status_change[link.id] |
|
1561 | 1 | new_time = link_status_info["notified_up_at"] |
|
1562 | 1 | assert new_time != orig_time |
|
1563 | 1 | mock_notify_topo.assert_called() |
|
1564 | 1 | mock_notify_link.assert_called() |
|
1565 | |||
1566 | 1 | assert mock_sleep.call_count == 2 |
|
1567 | |||
1568 | 1 | @patch('napps.kytos.topology.main.Main.notify_link_status_change') |
|
1569 | 1 | def test_notify_switch_links_status(self, mock_notify_link_status_change): |
|
1570 | """Test switch links notification when switch status change""" |
||
1571 | 1 | buffers_app_mock = MagicMock() |
|
1572 | 1 | self.napp.controller.buffers.app = buffers_app_mock |
|
1573 | 1 | dpid = "00:00:00:00:00:00:00:01" |
|
1574 | 1 | mock_switch = get_switch_mock(dpid) |
|
1575 | 1 | link1 = MagicMock() |
|
1576 | 1 | link1.endpoint_a.switch = mock_switch |
|
1577 | 1 | self.napp.controller.links = {1: link1} |
|
1578 | |||
1579 | 1 | self.napp.notify_switch_links_status(mock_switch, "link enabled") |
|
1580 | 1 | assert self.napp.controller.buffers.app.put.call_count == 1 |
|
1581 | |||
1582 | 1 | self.napp.notify_switch_links_status(mock_switch, "link disabled") |
|
1583 | 1 | assert self.napp.controller.buffers.app.put.call_count == 1 |
|
1584 | 1 | assert mock_notify_link_status_change.call_count == 1 |
|
1585 | |||
1586 | # Without notification |
||
1587 | 1 | link1.endpoint_a.switch = None |
|
1588 | 1 | self.napp.notify_switch_links_status(mock_switch, "link enabled") |
|
1589 | 1 | assert self.napp.controller.buffers.app.put.call_count == 1 |
|
1590 | |||
1591 | 1 | @patch('napps.kytos.topology.main.Main.notify_link_status_change') |
|
1592 | 1 | def test_notify_interface_link_status(self, args): |
|
1593 | """Test interface links notification when enable""" |
||
1594 | 1 | mock_notify_link_status_change = args |
|
1595 | 1 | buffers_app_mock = MagicMock() |
|
1596 | 1 | self.napp.controller.buffers.app = buffers_app_mock |
|
1597 | 1 | mock_intf = MagicMock(link=MagicMock()) |
|
1598 | 1 | self.napp._notify_interface_link_status([mock_intf], "link enabled") |
|
1599 | 1 | assert self.napp.controller.buffers.app.put.call_count == 1 |
|
1600 | |||
1601 | 1 | self.napp._notify_interface_link_status([mock_intf], "link disabled") |
|
1602 | 1 | assert mock_notify_link_status_change.call_count == 1 |
|
1603 | 1 | assert self.napp.controller.buffers.app.put.call_count == 1 |
|
1604 | |||
1605 | # Without notification |
||
1606 | 1 | mock_intf.link = None |
|
1607 | 1 | self.napp._notify_interface_link_status([mock_intf], "link enabled") |
|
1608 | 1 | assert self.napp.controller.buffers.app.put.call_count == 1 |
|
1609 | |||
1610 | 1 | View Code Duplication | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
|
|||
1611 | 1 | @patch('napps.kytos.topology.main.Main.notify_link_status_change') |
|
1612 | 1 | def test_interruption_start( |
|
1613 | self, |
||
1614 | mock_notify_link_status_change, |
||
1615 | mock_notify_topology_update |
||
1616 | ): |
||
1617 | """Tests processing of received interruption start events.""" |
||
1618 | 1 | link_a = MagicMock() |
|
1619 | 1 | link_b = MagicMock() |
|
1620 | 1 | link_c = MagicMock() |
|
1621 | 1 | self.napp.controller.links = { |
|
1622 | 'link_a': link_a, |
||
1623 | 'link_b': link_b, |
||
1624 | 'link_c': link_c, |
||
1625 | } |
||
1626 | 1 | event = KytosEvent( |
|
1627 | "topology.interruption.start", |
||
1628 | { |
||
1629 | 'type': 'test_interruption', |
||
1630 | 'switches': [ |
||
1631 | ], |
||
1632 | 'interfaces': [ |
||
1633 | ], |
||
1634 | 'links': [ |
||
1635 | 'link_a', |
||
1636 | 'link_c', |
||
1637 | ], |
||
1638 | } |
||
1639 | ) |
||
1640 | 1 | self.napp.handle_interruption_start(event) |
|
1641 | 1 | mock_notify_link_status_change.assert_has_calls( |
|
1642 | [ |
||
1643 | call(link_a, 'test_interruption'), |
||
1644 | call(link_c, 'test_interruption'), |
||
1645 | ] |
||
1646 | ) |
||
1647 | 1 | assert mock_notify_link_status_change.call_count == 2 |
|
1648 | 1 | mock_notify_topology_update.assert_called_once() |
|
1649 | |||
1650 | 1 | View Code Duplication | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
1651 | 1 | @patch('napps.kytos.topology.main.Main.notify_link_status_change') |
|
1652 | 1 | def test_interruption_end( |
|
1653 | self, |
||
1654 | mock_notify_link_status_change, |
||
1655 | mock_notify_topology_update |
||
1656 | ): |
||
1657 | """Tests processing of received interruption end events.""" |
||
1658 | 1 | link_a = MagicMock() |
|
1659 | 1 | link_b = MagicMock() |
|
1660 | 1 | link_c = MagicMock() |
|
1661 | 1 | self.napp.controller.links = { |
|
1662 | 'link_a': link_a, |
||
1663 | 'link_b': link_b, |
||
1664 | 'link_c': link_c, |
||
1665 | } |
||
1666 | 1 | event = KytosEvent( |
|
1667 | "topology.interruption.start", |
||
1668 | { |
||
1669 | 'type': 'test_interruption', |
||
1670 | 'switches': [ |
||
1671 | ], |
||
1672 | 'interfaces': [ |
||
1673 | ], |
||
1674 | 'links': [ |
||
1675 | 'link_a', |
||
1676 | 'link_c', |
||
1677 | ], |
||
1678 | } |
||
1679 | ) |
||
1680 | 1 | self.napp.handle_interruption_end(event) |
|
1681 | 1 | mock_notify_link_status_change.assert_has_calls( |
|
1682 | [ |
||
1683 | call(link_a, 'test_interruption'), |
||
1684 | call(link_c, 'test_interruption'), |
||
1685 | ] |
||
1686 | ) |
||
1687 | 1 | assert mock_notify_link_status_change.call_count == 2 |
|
1688 | 1 | mock_notify_topology_update.assert_called_once() |
|
1689 | |||
1690 | 1 | async def test_set_tag_range(self): |
|
1691 | """Test set_tag_range""" |
||
1692 | 1 | self.napp.controller.loop = asyncio.get_running_loop() |
|
1693 | 1 | interface_id = '00:00:00:00:00:00:00:01:1' |
|
1694 | 1 | dpid = '00:00:00:00:00:00:00:01' |
|
1695 | 1 | mock_switch = get_switch_mock(dpid) |
|
1696 | 1 | mock_interface = get_interface_mock('s1-eth1', 1, mock_switch) |
|
1697 | 1 | mock_interface.set_tag_ranges = MagicMock() |
|
1698 | 1 | self.napp.handle_on_interface_tags = MagicMock() |
|
1699 | 1 | self.napp.controller.get_interface_by_id = MagicMock() |
|
1700 | 1 | self.napp.controller.get_interface_by_id.return_value = mock_interface |
|
1701 | 1 | payload = { |
|
1702 | "tag_type": "vlan", |
||
1703 | "tag_ranges": [[20, 20], [200, 3000]] |
||
1704 | } |
||
1705 | 1 | url = f"{self.base_endpoint}/interfaces/{interface_id}/tag_ranges" |
|
1706 | 1 | response = await self.api_client.post(url, json=payload) |
|
1707 | 1 | assert response.status_code == 200 |
|
1708 | |||
1709 | 1 | args = mock_interface.set_tag_ranges.call_args[0] |
|
1710 | 1 | assert args[0] == payload['tag_ranges'] |
|
1711 | 1 | assert args[1] == payload['tag_type'] |
|
1712 | 1 | assert self.napp.handle_on_interface_tags.call_count == 1 |
|
1713 | |||
1714 | 1 | async def test_set_tag_range_not_found(self): |
|
1715 | """Test set_tag_range. Not found""" |
||
1716 | 1 | self.napp.controller.loop = asyncio.get_running_loop() |
|
1717 | 1 | interface_id = '00:00:00:00:00:00:00:01:1' |
|
1718 | 1 | self.napp.controller.get_interface_by_id = MagicMock() |
|
1719 | 1 | self.napp.controller.get_interface_by_id.return_value = None |
|
1720 | 1 | payload = { |
|
1721 | "tag_type": "vlan", |
||
1722 | "tag_ranges": [[20, 20], [200, 3000]] |
||
1723 | } |
||
1724 | 1 | url = f"{self.base_endpoint}/interfaces/{interface_id}/tag_ranges" |
|
1725 | 1 | response = await self.api_client.post(url, json=payload) |
|
1726 | assert response.status_code == 404 |
||
1727 | |||
1728 | 1 | View Code Duplication | async def test_set_tag_range_tag_error(self): |
1729 | """Test set_tag_range TagRangeError""" |
||
1730 | 1 | self.napp.controller.loop = asyncio.get_running_loop() |
|
1731 | 1 | interface_id = '00:00:00:00:00:00:00:01:1' |
|
1732 | 1 | dpid = '00:00:00:00:00:00:00:01' |
|
1733 | 1 | mock_switch = get_switch_mock(dpid) |
|
1734 | 1 | mock_interface = get_interface_mock('s1-eth1', 1, mock_switch) |
|
1735 | 1 | mock_interface.set_tag_ranges = MagicMock() |
|
1736 | 1 | mock_interface.set_tag_ranges.side_effect = KytosSetTagRangeError("") |
|
1737 | 1 | mock_interface.notify_interface_tags = MagicMock() |
|
1738 | 1 | self.napp.controller.get_interface_by_id = MagicMock() |
|
1739 | 1 | self.napp.controller.get_interface_by_id.return_value = mock_interface |
|
1740 | 1 | payload = { |
|
1741 | "tag_type": "vlan", |
||
1742 | "tag_ranges": [[20, 20], [200, 3000]] |
||
1743 | } |
||
1744 | 1 | url = f"{self.base_endpoint}/interfaces/{interface_id}/tag_ranges" |
|
1745 | 1 | response = await self.api_client.post(url, json=payload) |
|
1746 | assert response.status_code == 400 |
||
1747 | assert mock_interface.notify_interface_tags.call_count == 0 |
||
1748 | |||
1749 | 1 | View Code Duplication | async def test_set_tag_range_type_error(self): |
1750 | """Test set_tag_range TagRangeError""" |
||
1751 | 1 | self.napp.controller.loop = asyncio.get_running_loop() |
|
1752 | 1 | interface_id = '00:00:00:00:00:00:00:01:1' |
|
1753 | 1 | dpid = '00:00:00:00:00:00:00:01' |
|
1754 | 1 | mock_switch = get_switch_mock(dpid) |
|
1755 | 1 | mock_interface = get_interface_mock('s1-eth1', 1, mock_switch) |
|
1756 | 1 | mock_interface.set_tag_ranges = MagicMock() |
|
1757 | 1 | mock_interface.set_tag_ranges.side_effect = KytosTagtypeNotSupported( |
|
1758 | "" |
||
1759 | ) |
||
1760 | 1 | self.napp.handle_on_interface_tags = MagicMock() |
|
1761 | 1 | self.napp.controller.get_interface_by_id = MagicMock() |
|
1762 | 1 | self.napp.controller.get_interface_by_id.return_value = mock_interface |
|
1763 | 1 | payload = { |
|
1764 | "tag_type": "wrong_tag_type", |
||
1765 | "tag_ranges": [[20, 20], [200, 3000]] |
||
1766 | } |
||
1767 | 1 | url = f"{self.base_endpoint}/interfaces/{interface_id}/tag_ranges" |
|
1768 | 1 | response = await self.api_client.post(url, json=payload) |
|
1769 | assert response.status_code == 400 |
||
1770 | assert self.napp.handle_on_interface_tags.call_count == 0 |
||
1771 | |||
1772 | 1 | View Code Duplication | async def test_delete_tag_range(self): |
1773 | """Test delete_tag_range""" |
||
1774 | 1 | self.napp.controller.loop = asyncio.get_running_loop() |
|
1775 | 1 | interface_id = '00:00:00:00:00:00:00:01:1' |
|
1776 | 1 | dpid = '00:00:00:00:00:00:00:01' |
|
1777 | 1 | mock_switch = get_switch_mock(dpid) |
|
1778 | 1 | mock_interface = get_interface_mock('s1-eth1', 1, mock_switch) |
|
1779 | 1 | mock_interface.remove_tag_ranges = MagicMock() |
|
1780 | 1 | self.napp.handle_on_interface_tags = MagicMock() |
|
1781 | 1 | self.napp.controller.get_interface_by_id = MagicMock() |
|
1782 | 1 | self.napp.controller.get_interface_by_id.return_value = mock_interface |
|
1783 | 1 | url = f"{self.base_endpoint}/interfaces/{interface_id}/tag_ranges" |
|
1784 | 1 | response = await self.api_client.delete(url) |
|
1785 | 1 | assert response.status_code == 200 |
|
1786 | 1 | assert mock_interface.remove_tag_ranges.call_count == 1 |
|
1787 | |||
1788 | 1 | View Code Duplication | async def test_delete_tag_range_not_found(self): |
1789 | """Test delete_tag_range. Not found""" |
||
1790 | 1 | self.napp.controller.loop = asyncio.get_running_loop() |
|
1791 | 1 | interface_id = '00:00:00:00:00:00:00:01:1' |
|
1792 | 1 | dpid = '00:00:00:00:00:00:00:01' |
|
1793 | 1 | mock_switch = get_switch_mock(dpid) |
|
1794 | 1 | mock_interface = get_interface_mock('s1-eth1', 1, mock_switch) |
|
1795 | 1 | mock_interface.remove_tag_ranges = MagicMock() |
|
1796 | 1 | self.napp.controller.get_interface_by_id = MagicMock() |
|
1797 | 1 | self.napp.controller.get_interface_by_id.return_value = None |
|
1798 | 1 | url = f"{self.base_endpoint}/interfaces/{interface_id}/tag_ranges" |
|
1799 | 1 | response = await self.api_client.delete(url) |
|
1800 | assert response.status_code == 404 |
||
1801 | assert mock_interface.remove_tag_ranges.call_count == 0 |
||
1802 | |||
1803 | 1 | async def test_delete_tag_range_type_error(self): |
|
1804 | """Test delete_tag_range TagRangeError""" |
||
1805 | 1 | self.napp.controller.loop = asyncio.get_running_loop() |
|
1806 | 1 | interface_id = '00:00:00:00:00:00:00:01:1' |
|
1807 | 1 | dpid = '00:00:00:00:00:00:00:01' |
|
1808 | 1 | mock_switch = get_switch_mock(dpid) |
|
1809 | 1 | mock_interface = get_interface_mock('s1-eth1', 1, mock_switch) |
|
1810 | 1 | mock_interface.remove_tag_ranges = MagicMock() |
|
1811 | 1 | remove_tag = mock_interface.remove_tag_ranges |
|
1812 | 1 | remove_tag.side_effect = KytosTagtypeNotSupported("") |
|
1813 | 1 | self.napp.controller.get_interface_by_id = MagicMock() |
|
1814 | 1 | self.napp.controller.get_interface_by_id.return_value = mock_interface |
|
1815 | 1 | url = f"{self.base_endpoint}/interfaces/{interface_id}/tag_ranges" |
|
1816 | 1 | response = await self.api_client.delete(url) |
|
1817 | assert response.status_code == 400 |
||
1818 | |||
1819 | 1 | View Code Duplication | async def test_get_all_tag_ranges(self): |
1820 | """Test get_all_tag_ranges""" |
||
1821 | 1 | self.napp.controller.loop = asyncio.get_running_loop() |
|
1822 | 1 | dpid = '00:00:00:00:00:00:00:01' |
|
1823 | 1 | switch = get_switch_mock(dpid) |
|
1824 | 1 | interface = get_interface_mock('s1-eth1', 1, switch) |
|
1825 | 1 | tags = {'vlan': [[1, 4095]]} |
|
1826 | 1 | special_tags = {'vlan': ["vlan"]} |
|
1827 | 1 | interface.tag_ranges = tags |
|
1828 | 1 | interface.available_tags = tags |
|
1829 | 1 | interface.special_available_tags = special_tags |
|
1830 | 1 | interface.special_tags = special_tags |
|
1831 | 1 | switch.interfaces = {1: interface} |
|
1832 | 1 | self.napp.controller.switches = {dpid: switch} |
|
1833 | 1 | url = f"{self.base_endpoint}/interfaces/tag_ranges" |
|
1834 | 1 | response = await self.api_client.get(url) |
|
1835 | 1 | expected = {dpid + ":1": { |
|
1836 | 'available_tags': tags, |
||
1837 | 'tag_ranges': tags, |
||
1838 | 'special_available_tags': special_tags, |
||
1839 | 'special_tags': special_tags |
||
1840 | }} |
||
1841 | 1 | assert response.status_code == 200 |
|
1842 | 1 | assert response.json() == expected |
|
1843 | |||
1844 | 1 | View Code Duplication | async def test_get_tag_ranges_by_intf(self): |
1845 | """Test get_tag_ranges_by_intf""" |
||
1846 | 1 | self.napp.controller.loop = asyncio.get_running_loop() |
|
1847 | 1 | dpid = '00:00:00:00:00:00:00:01' |
|
1848 | 1 | switch = get_switch_mock(dpid) |
|
1849 | 1 | interface = get_interface_mock('s1-eth1', 1, switch) |
|
1850 | 1 | tags = {'vlan': [[1, 4095]]} |
|
1851 | 1 | special_tags = {'vlan': ["vlan"]} |
|
1852 | 1 | interface.tag_ranges = tags |
|
1853 | 1 | interface.available_tags = tags |
|
1854 | 1 | interface.special_available_tags = special_tags |
|
1855 | 1 | interface.special_tags = special_tags |
|
1856 | 1 | self.napp.controller.get_interface_by_id = MagicMock() |
|
1857 | 1 | self.napp.controller.get_interface_by_id.return_value = interface |
|
1858 | 1 | url = f"{self.base_endpoint}/interfaces/{dpid}:1/tag_ranges" |
|
1859 | 1 | response = await self.api_client.get(url) |
|
1860 | 1 | expected = { |
|
1861 | '00:00:00:00:00:00:00:01:1': { |
||
1862 | "available_tags": tags, |
||
1863 | "tag_ranges": tags, |
||
1864 | 'special_available_tags': special_tags, |
||
1865 | 'special_tags': special_tags |
||
1866 | } |
||
1867 | } |
||
1868 | 1 | assert response.status_code == 200 |
|
1869 | 1 | assert response.json() == expected |
|
1870 | |||
1871 | 1 | async def test_get_tag_ranges_by_intf_error(self): |
|
1872 | """Test get_tag_ranges_by_intf with NotFound""" |
||
1873 | 1 | self.napp.controller.loop = asyncio.get_running_loop() |
|
1874 | 1 | dpid = '00:00:00:00:00:00:00:01' |
|
1875 | 1 | self.napp.controller.get_interface_by_id = MagicMock() |
|
1876 | 1 | self.napp.controller.get_interface_by_id.return_value = None |
|
1877 | 1 | url = f"{self.base_endpoint}/interfaces/{dpid}:1/tag_ranges" |
|
1878 | 1 | response = await self.api_client.get(url) |
|
1879 | assert response.status_code == 404 |
||
1880 | |||
1881 | 1 | async def test_set_special_tags(self): |
|
1882 | """Test set_special_tags""" |
||
1883 | 1 | self.napp.controller.loop = asyncio.get_running_loop() |
|
1884 | 1 | interface_id = '00:00:00:00:00:00:00:01:1' |
|
1885 | 1 | dpid = '00:00:00:00:00:00:00:01' |
|
1886 | 1 | mock_switch = get_switch_mock(dpid) |
|
1887 | 1 | mock_intf = get_interface_mock('s1-eth1', 1, mock_switch) |
|
1888 | 1 | mock_intf.set_special_tags = MagicMock() |
|
1889 | 1 | self.napp.handle_on_interface_tags = MagicMock() |
|
1890 | 1 | self.napp.controller.get_interface_by_id = MagicMock() |
|
1891 | 1 | self.napp.controller.get_interface_by_id.return_value = mock_intf |
|
1892 | 1 | payload = { |
|
1893 | "tag_type": "vlan", |
||
1894 | "special_tags": ["untagged"], |
||
1895 | } |
||
1896 | 1 | url = f"{self.base_endpoint}/interfaces/{interface_id}/"\ |
|
1897 | "special_tags" |
||
1898 | 1 | response = await self.api_client.post(url, json=payload) |
|
1899 | 1 | assert response.status_code == 200 |
|
1900 | |||
1901 | 1 | args = mock_intf.set_special_tags.call_args[0] |
|
1902 | 1 | assert args[0] == payload["tag_type"] |
|
1903 | 1 | assert args[1] == payload['special_tags'] |
|
1904 | 1 | assert self.napp.handle_on_interface_tags.call_count == 1 |
|
1905 | |||
1906 | # KytosTagError |
||
1907 | 1 | mock_intf.set_special_tags.side_effect = KytosTagtypeNotSupported("") |
|
1908 | 1 | url = f"{self.base_endpoint}/interfaces/{interface_id}/"\ |
|
1909 | "special_tags" |
||
1910 | 1 | response = await self.api_client.post(url, json=payload) |
|
1911 | assert response.status_code == 400 |
||
1912 | assert self.napp.handle_on_interface_tags.call_count == 1 |
||
1913 | |||
1914 | # Interface Not Found |
||
1915 | self.napp.controller.get_interface_by_id.return_value = None |
||
1916 | url = f"{self.base_endpoint}/interfaces/{interface_id}/"\ |
||
1917 | "special_tags" |
||
1918 | response = await self.api_client.post(url, json=payload) |
||
1919 | assert response.status_code == 404 |
||
1920 | assert self.napp.handle_on_interface_tags.call_count == 1 |
||
1921 | |||
1922 | 1 | async def test_delete_link(self): |
|
1923 | """Test delete_link""" |
||
1924 | 1 | dpid_a = '00:00:00:00:00:00:00:01' |
|
1925 | 1 | dpid_b = '00:00:00:00:00:00:00:02' |
|
1926 | 1 | link_id = 'mock_link' |
|
1927 | 1 | mock_switch_a = get_switch_mock(dpid_a) |
|
1928 | 1 | mock_switch_b = get_switch_mock(dpid_b) |
|
1929 | 1 | mock_interface_a = get_interface_mock('s1-eth1', 1, mock_switch_a) |
|
1930 | 1 | mock_interface_b = get_interface_mock('s2-eth1', 1, mock_switch_b) |
|
1931 | 1 | mock_link = get_link_mock(mock_interface_a, mock_interface_b) |
|
1932 | 1 | mock_link.id = link_id |
|
1933 | 1 | mock_link.status = EntityStatus.DISABLED |
|
1934 | 1 | mock_interface_a.link = mock_link |
|
1935 | 1 | mock_interface_b.link = mock_link |
|
1936 | 1 | self.napp.controller.links = {link_id: mock_link} |
|
1937 | |||
1938 | 1 | call_count = self.napp.controller.buffers.app.put.call_count |
|
1939 | 1 | endpoint = f"{self.base_endpoint}/links/{link_id}" |
|
1940 | 1 | response = await self.api_client.delete(endpoint) |
|
1941 | 1 | assert response.status_code == 200 |
|
1942 | 1 | assert self.napp.topo_controller.delete_link.call_count == 1 |
|
1943 | 1 | assert len(self.napp.controller.links) == 0 |
|
1944 | 1 | call_count += 2 |
|
1945 | 1 | assert self.napp.controller.buffers.app.put.call_count == call_count |
|
1946 | |||
1947 | # Link is up |
||
1948 | 1 | self.napp.controller.links = {link_id: mock_link} |
|
1949 | 1 | mock_link.status = EntityStatus.UP |
|
1950 | 1 | endpoint = f"{self.base_endpoint}/links/{link_id}" |
|
1951 | 1 | response = await self.api_client.delete(endpoint) |
|
1952 | assert response.status_code == 409 |
||
1953 | assert self.napp.topo_controller.delete_link.call_count == 1 |
||
1954 | assert self.napp.controller.buffers.app.put.call_count == call_count |
||
1955 | |||
1956 | # Link does not exist |
||
1957 | del self.napp.controller.links[link_id] |
||
1958 | endpoint = f"{self.base_endpoint}/links/{link_id}" |
||
1959 | response = await self.api_client.delete(endpoint) |
||
1960 | assert response.status_code == 404 |
||
1961 | assert self.napp.topo_controller.delete_link.call_count == 1 |
||
1962 | assert self.napp.controller.buffers.app.put.call_count == call_count |
||
1963 | |||
1964 | 1 | @patch('napps.kytos.topology.main.Main.get_flows_by_switch') |
|
1965 | 1 | async def test_delete_switch(self, mock_get): |
|
1966 | """Test delete_switch""" |
||
1967 | # Error 404 NotFound |
||
1968 | 1 | dpid = '00:00:00:00:00:00:00:01' |
|
1969 | 1 | endpoint = f"{self.base_endpoint}/switches/{dpid}" |
|
1970 | 1 | response = await self.api_client.delete(endpoint) |
|
1971 | assert response.status_code == 404, response |
||
1972 | |||
1973 | # Error 409 Switch not disabled |
||
1974 | mock_switch = get_switch_mock(dpid) |
||
1975 | mock_switch.status = EntityStatus.UP |
||
1976 | self.napp.controller.switches = {dpid: mock_switch} |
||
1977 | endpoint = f"{self.base_endpoint}/switches/{dpid}" |
||
1978 | response = await self.api_client.delete(endpoint) |
||
1979 | assert response.status_code == 409, response |
||
1980 | |||
1981 | # Error 409 Interface vlan is being used |
||
1982 | 1 | mock_intf = MagicMock(all_tags_available=lambda: False) |
|
1983 | mock_switch.interfaces = {1: mock_intf} |
||
1984 | mock_switch.status = EntityStatus.DISABLED |
||
1985 | endpoint = f"{self.base_endpoint}/switches/{dpid}" |
||
1986 | response = await self.api_client.delete(endpoint) |
||
1987 | assert response.status_code == 409, response |
||
1988 | |||
1989 | # Error 409 Swith have links |
||
1990 | 1 | mock_switch.interfaces[1].all_tags_available = lambda: True |
|
1991 | mock_switch_2 = get_switch_mock("00:00:00:00:00:00:00:02") |
||
1992 | mock_interface_a = get_interface_mock('s1-eth1', 1, mock_switch) |
||
1993 | mock_interface_b = get_interface_mock('s2-eth1', 1, mock_switch_2) |
||
1994 | mock_link = get_link_mock(mock_interface_a, mock_interface_b) |
||
1995 | self.napp.controller.links = {'0e2b5d7bc858b9f38db11b69': mock_link} |
||
1996 | endpoint = f"{self.base_endpoint}/switches/{dpid}" |
||
1997 | response = await self.api_client.delete(endpoint) |
||
1998 | assert response.status_code == 409, response |
||
1999 | |||
2000 | # Error 409 Switch has flows |
||
2001 | mock_get.return_value = {dpid: {}} |
||
2002 | endpoint = f"{self.base_endpoint}/switches/{dpid}" |
||
2003 | response = await self.api_client.delete(endpoint) |
||
2004 | assert response.status_code == 409, response |
||
2005 | |||
2006 | # Success 202 |
||
2007 | mock_get.return_value = {} |
||
2008 | self.napp.controller.links = {} |
||
2009 | endpoint = f"{self.base_endpoint}/switches/{dpid}" |
||
2010 | response = await self.api_client.delete(endpoint) |
||
2011 | 1 | assert response.status_code == 200, response |
|
2012 | |||
2013 | 1 | def test_notify_link_status(self): |
|
2014 | """Test notify_link_enabled_state""" |
||
2015 | 1 | self.napp.controller.buffers.app.put.reset_mock() |
|
2016 | 1 | link = Mock() |
|
2017 | 1 | link.id = 'mock_link' |
|
2018 | 1 | self.napp.notify_link_enabled_state(link, 'enabled') |
|
2019 | 1 | assert self.napp.controller.buffers.app.put.call_count == 1 |
|
2020 | |||
2021 | 1 | self.napp.notify_link_enabled_state(link, 'disabled') |
|
2022 | 1 | assert self.napp.controller.buffers.app.put.call_count == 2 |
|
2023 | |||
2024 | 1 | @patch('napps.kytos.topology.main.tenacity.nap.time') |
|
2025 | 1 | @patch('httpx.get') |
|
2026 | 1 | def test_get_flows_by_switch(self, mock_get, _): |
|
2027 | """Test get_flows_by_switch""" |
||
2028 | 1 | dpid = "00:01" |
|
2029 | 1 | mock_get.return_value.status_code = 400 |
|
2030 | 1 | with pytest.raises(tenacity.RetryError): |
|
2031 | 1 | self.napp.get_flows_by_switch(dpid) |
|
2032 | |||
2033 | 1 | mock_get.return_value.status_code = 200 |
|
2034 | 1 | mock_get.return_value.is_server_error = False |
|
2035 | 1 | expected = {dpid: "mocked_flows"} |
|
2036 | 1 | mock_get.return_value.json.return_value = expected |
|
2037 | 1 | actual = self.napp.get_flows_by_switch(dpid) |
|
2038 | 1 | assert actual == "mocked_flows" |
|
2039 | |||
2040 | 1 | @patch('napps.kytos.topology.main.Main.get_intf_usage') |
|
2041 | 1 | @patch('napps.kytos.topology.main.Main._delete_interface') |
|
2042 | 1 | async def test_delete_interface_api(self, mock_delete, mock_usage): |
|
2043 | """Test delete interface API call""" |
||
2044 | 1 | switch_id = "00:00:00:00:00:00:00:01" |
|
2045 | 1 | intf_id = "00:00:00:00:00:00:00:01:1" |
|
2046 | |||
2047 | # Error 400 Invalid interface id |
||
2048 | 1 | endpoint = f"{self.base_endpoint}/interfaces/{intf_id}x" |
|
2049 | 1 | response = await self.api_client.delete(endpoint) |
|
2050 | assert response.status_code == 400, response |
||
2051 | |||
2052 | # Error 404 Switch not found |
||
2053 | endpoint = f"{self.base_endpoint}/interfaces/{intf_id}" |
||
2054 | response = await self.api_client.delete(endpoint) |
||
2055 | assert response.status_code == 404, response |
||
2056 | |||
2057 | # Error 404 Interface not found |
||
2058 | mock_switch = get_switch_mock(switch_id) |
||
2059 | mock_switch.interfaces = {} |
||
2060 | self.napp.controller.switches = {switch_id: mock_switch} |
||
2061 | response = await self.api_client.delete(endpoint) |
||
2062 | assert response.status_code == 404, response |
||
2063 | |||
2064 | # Error 409 Interface is used |
||
2065 | mock_switch.interfaces = {1: Mock()} |
||
2066 | self.napp.controller.switches = {switch_id: mock_switch} |
||
2067 | mock_usage.return_value = "It is enabled or active." |
||
2068 | response = await self.api_client.delete(endpoint) |
||
2069 | assert response.status_code == 409, response |
||
2070 | |||
2071 | # Success |
||
2072 | mock_usage.return_value = None |
||
2073 | mock_delete.return_value = True |
||
2074 | response = await self.api_client.delete(endpoint) |
||
2075 | 1 | assert response.status_code == 200, response |
|
2076 | |||
2077 | 1 | def test_get_intf_usage(self): |
|
2078 | """Test get_intf_usage""" |
||
2079 | 1 | switch_id = "00:00:00:00:00:00:00:01" |
|
2080 | 1 | mock_switch = get_switch_mock(switch_id) |
|
2081 | 1 | mock_intf = get_interface_mock('s1-eth1', 1, mock_switch) |
|
2082 | |||
2083 | 1 | mock_intf.is_enabled.return_value = False |
|
2084 | 1 | mock_intf.is_active.return_value = True |
|
2085 | 1 | actual_usage = self.napp.get_intf_usage(mock_intf) |
|
2086 | 1 | assert actual_usage == "It is enabled or active." |
|
2087 | |||
2088 | 1 | mock_intf.is_active.return_value = False |
|
2089 | 1 | mock_intf.link = Mock() |
|
2090 | 1 | actual_usage = self.napp.get_intf_usage(mock_intf) |
|
2091 | 1 | assert "It has a link," in actual_usage |
|
2092 | |||
2093 | 1 | mock_intf.link = None |
|
2094 | 1 | self.napp.get_flow_id_by_intf = MagicMock(return_value="mock_flow") |
|
2095 | 1 | actual_usage = self.napp.get_intf_usage(mock_intf) |
|
2096 | 1 | assert "There is a flow installed" in actual_usage |
|
2097 | |||
2098 | 1 | self.napp.get_flow_id_by_intf.return_value = None |
|
2099 | 1 | actual_usage = self.napp.get_intf_usage(mock_intf) |
|
2100 | 1 | assert actual_usage is None |
|
2101 | |||
2102 | 1 | @patch('napps.kytos.topology.main.Main.get_flows_by_switch') |
|
2103 | 1 | def test_get_flow_id_by_intf(self, mock_flows): |
|
2104 | """Test get_flow_id_by_intf""" |
||
2105 | 1 | flows = [ |
|
2106 | { |
||
2107 | "flow": { |
||
2108 | "match": {"in_port": 1, "dl_vlan": 200}, |
||
2109 | }, |
||
2110 | "flow_id": "flow_0", |
||
2111 | }, |
||
2112 | { |
||
2113 | "flow": { |
||
2114 | "actions": [{"action_type": "output", "port": 1}] |
||
2115 | }, |
||
2116 | "flow_id": "flow_1", |
||
2117 | }, |
||
2118 | { |
||
2119 | "flow": { |
||
2120 | "instructions": [{ |
||
2121 | "instruction_type": "apply_actions", |
||
2122 | "actions": [{"action_type": "output", "port": 1}] |
||
2123 | }] |
||
2124 | }, |
||
2125 | "flow_id": "flow_2", |
||
2126 | }, |
||
2127 | { |
||
2128 | "flow": { |
||
2129 | "match": {"dl_src": "ee:ee:ee:ee:ee:02"}, |
||
2130 | }, |
||
2131 | "flow_id": "flow_3", |
||
2132 | } |
||
2133 | ] |
||
2134 | |||
2135 | 1 | switch_id = "00:00:00:00:00:00:00:01" |
|
2136 | 1 | mock_switch = get_switch_mock(switch_id) |
|
2137 | 1 | mock_intf = get_interface_mock('s1-eth1', 1, mock_switch) |
|
2138 | |||
2139 | 1 | mock_flows.return_value = [flows[0]] |
|
2140 | 1 | flow_id = self.napp.get_flow_id_by_intf(mock_intf) |
|
2141 | 1 | assert flow_id == flows[0]["flow_id"] |
|
2142 | |||
2143 | 1 | mock_flows.return_value = [flows[1]] |
|
2144 | 1 | flow_id = self.napp.get_flow_id_by_intf(mock_intf) |
|
2145 | 1 | assert flow_id == flows[1]["flow_id"] |
|
2146 | |||
2147 | 1 | mock_flows.return_value = [flows[2]] |
|
2148 | 1 | flow_id = self.napp.get_flow_id_by_intf(mock_intf) |
|
2149 | 1 | assert flow_id == flows[2]["flow_id"] |
|
2150 | |||
2151 | 1 | mock_flows.return_value = [flows[3]] |
|
2152 | 1 | flow_id = self.napp.get_flow_id_by_intf(mock_intf) |
|
2153 | 1 | assert flow_id is None |
|
2154 | |||
2155 | 1 | def test_delete_interface(self): |
|
2156 | """Test _delete_interface""" |
||
2157 | 1 | switch_id = "00:00:00:00:00:00:00:01" |
|
2158 | 1 | mock_switch = get_switch_mock(switch_id) |
|
2159 | 1 | mock_intf = get_interface_mock('s1-eth1', 1, mock_switch) |
|
2160 | 1 | self.napp._delete_interface(mock_intf) |
|
2161 | 1 | assert mock_switch.remove_interface.call_count == 1 |
|
2162 | 1 | assert self.napp.topo_controller.upsert_switch.call_count == 1 |
|
2163 | 1 | delete = self.napp.topo_controller.delete_interface_from_details |
|
2164 | assert delete.call_count == 1 |
||
2165 |