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