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