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