1
|
|
|
"""Module to test the main napp file.""" |
2
|
|
|
import time |
3
|
|
|
import json |
4
|
|
|
|
5
|
|
|
from unittest import TestCase |
6
|
|
|
from unittest.mock import MagicMock, create_autospec, patch |
7
|
|
|
|
8
|
|
|
from kytos.core.switch import Switch |
9
|
|
|
from kytos.core.interface import Interface |
10
|
|
|
from kytos.core.link import Link |
11
|
|
|
from kytos.lib.helpers import get_switch_mock, get_test_client |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
from tests.unit.helpers import get_controller_mock, get_napp_urls |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
# pylint: disable=too-many-public-methods |
18
|
|
|
class TestMain(TestCase): |
19
|
|
|
"""Test the Main class.""" |
20
|
|
|
# pylint: disable=too-many-public-methods |
21
|
|
|
|
22
|
|
|
def setUp(self): |
23
|
|
|
"""Execute steps before each tests. |
24
|
|
|
|
25
|
|
|
Set the server_name_url_url from kytos/topology |
26
|
|
|
""" |
27
|
|
|
self.server_name_url = 'http://localhost:8181/api/kytos/topology' |
28
|
|
|
|
29
|
|
|
patch('kytos.core.helpers.run_on_thread', lambda x: x).start() |
30
|
|
|
from napps.kytos.topology.main import Main |
31
|
|
|
self.addCleanup(patch.stopall) |
32
|
|
|
|
33
|
|
|
self.napp = Main(get_controller_mock()) |
34
|
|
|
|
35
|
|
|
def test_get_event_listeners(self): |
36
|
|
|
"""Verify all event listeners registered.""" |
37
|
|
|
expected_events = ['kytos/core.shutdown', |
38
|
|
|
'kytos/core.shutdown.kytos/topology', |
39
|
|
|
'kytos/maintenance.start_link', |
40
|
|
|
'kytos/maintenance.end_link', |
41
|
|
|
'kytos/maintenance.start_switch', |
42
|
|
|
'kytos/maintenance.end_switch', |
43
|
|
|
'.*.interface.is.nni', |
44
|
|
|
'.*.connection.lost', |
45
|
|
|
'.*.switch.interface.created', |
46
|
|
|
'.*.switch.interface.deleted', |
47
|
|
|
'.*.switch.interface.link_down', |
48
|
|
|
'.*.switch.interface.link_up', |
49
|
|
|
'.*.switch.(new|reconnected)', |
50
|
|
|
'.*.switch.port.created', |
51
|
|
|
'kytos/topology.*.metadata.*'] |
52
|
|
|
actual_events = self.napp.listeners() |
53
|
|
|
self.assertCountEqual(expected_events, actual_events) |
54
|
|
|
|
55
|
|
View Code Duplication |
def test_verify_api_urls(self): |
|
|
|
|
56
|
|
|
"""Verify all APIs registered.""" |
57
|
|
|
expected_urls = [ |
58
|
|
|
({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/interfaces'), |
59
|
|
|
({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/switches'), |
60
|
|
|
({}, {'POST', 'OPTIONS'}, '/api/kytos/topology/v3/restore'), |
61
|
|
|
({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/links'), |
62
|
|
|
({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/'), |
63
|
|
|
({'dpid': '[dpid]'}, {'POST', 'OPTIONS'}, |
64
|
|
|
'/api/kytos/topology/v3/interfaces/switch/<dpid>/disable'), |
65
|
|
|
({'dpid': '[dpid]'}, {'POST', 'OPTIONS'}, |
66
|
|
|
'/api/kytos/topology/v3/interfaces/switch/<dpid>/enable'), |
67
|
|
|
({'key': '[key]', 'interface_id': '[interface_id]'}, |
68
|
|
|
{'OPTIONS', 'DELETE'}, |
69
|
|
|
'/api/kytos/topology/v3/interfaces/<interface_id>/metadata/<key>'), |
70
|
|
|
({'interface_id': '[interface_id]'}, {'POST', 'OPTIONS'}, |
71
|
|
|
'/api/kytos/topology/v3/interfaces/<interface_id>/metadata'), |
72
|
|
|
({'interface_id': '[interface_id]'}, {'GET', 'OPTIONS', 'HEAD'}, |
73
|
|
|
'/api/kytos/topology/v3/interfaces/<interface_id>/metadata'), |
74
|
|
|
({'interface_disable_id': '[interface_disable_id]'}, |
75
|
|
|
{'POST', 'OPTIONS'}, |
76
|
|
|
'/api/kytos/topology/v3/interfaces/<interface_disable_id>/disable'), |
77
|
|
|
({'interface_enable_id': '[interface_enable_id]'}, |
78
|
|
|
{'POST', 'OPTIONS'}, |
79
|
|
|
'/api/kytos/topology/v3/interfaces/<interface_enable_id>/enable'), |
80
|
|
|
({'dpid': '[dpid]', 'key': '[key]'}, {'OPTIONS', 'DELETE'}, |
81
|
|
|
'/api/kytos/topology/v3/switches/<dpid>/metadata/<key>'), |
82
|
|
|
({'dpid': '[dpid]'}, {'POST', 'OPTIONS'}, |
83
|
|
|
'/api/kytos/topology/v3/switches/<dpid>/metadata'), |
84
|
|
|
({'dpid': '[dpid]'}, {'GET', 'OPTIONS', 'HEAD'}, |
85
|
|
|
'/api/kytos/topology/v3/switches/<dpid>/metadata'), |
86
|
|
|
({'dpid': '[dpid]'}, {'POST', 'OPTIONS'}, |
87
|
|
|
'/api/kytos/topology/v3/switches/<dpid>/disable'), |
88
|
|
|
({'dpid': '[dpid]'}, {'POST', 'OPTIONS'}, |
89
|
|
|
'/api/kytos/topology/v3/switches/<dpid>/enable'), |
90
|
|
|
({'link_id': '[link_id]', 'key': '[key]'}, {'OPTIONS', 'DELETE'}, |
91
|
|
|
'/api/kytos/topology/v3/links/<link_id>/metadata/<key>'), |
92
|
|
|
({'link_id': '[link_id]'}, {'POST', 'OPTIONS'}, |
93
|
|
|
'/api/kytos/topology/v3/links/<link_id>/metadata'), |
94
|
|
|
({'link_id': '[link_id]'}, {'GET', 'OPTIONS', 'HEAD'}, |
95
|
|
|
'/api/kytos/topology/v3/links/<link_id>/metadata'), |
96
|
|
|
({'link_id': '[link_id]'}, {'POST', 'OPTIONS'}, |
97
|
|
|
'/api/kytos/topology/v3/links/<link_id>/disable'), |
98
|
|
|
({'link_id': '[link_id]'}, {'POST', 'OPTIONS'}, |
99
|
|
|
'/api/kytos/topology/v3/links/<link_id>/enable')] |
100
|
|
|
|
101
|
|
|
urls = get_napp_urls(self.napp) |
102
|
|
|
self.assertEqual(expected_urls, urls) |
103
|
|
|
|
104
|
|
View Code Duplication |
def test_enable_switch(self): |
|
|
|
|
105
|
|
|
"""Test enable_swicth.""" |
106
|
|
|
dpid = "00:00:00:00:00:00:00:01" |
107
|
|
|
mock_switch = get_switch_mock(dpid) |
108
|
|
|
msg_success = "Operation successful" |
109
|
|
|
msg_fail = "Switch not found" |
110
|
|
|
self.napp.controller.switches = {"00:00:00:00:00:00:00:01": |
111
|
|
|
mock_switch} |
112
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
113
|
|
|
|
114
|
|
|
url = f'{self.server_name_url}/v3/switches/{dpid}/enable' |
115
|
|
|
response = api.post(url) |
116
|
|
|
self.assertEqual(response.status_code, 201, response.data) |
117
|
|
|
self.assertEqual(msg_success, json.loads(response.data)) |
118
|
|
|
self.assertEqual(mock_switch.enable.call_count, 1) |
119
|
|
|
|
120
|
|
|
# fail case |
121
|
|
|
mock_switch.enable.call_count = 0 |
122
|
|
|
dpid = "00:00:00:00:00:00:00:02" |
123
|
|
|
url = f'{self.server_name_url}/v3/switches/{dpid}/enable' |
124
|
|
|
response = api.post(url) |
125
|
|
|
self.assertEqual(response.status_code, 404, response.data) |
126
|
|
|
self.assertEqual(msg_fail, json.loads(response.data)) |
127
|
|
|
self.assertEqual(mock_switch.enable.call_count, 0) |
128
|
|
|
|
129
|
|
View Code Duplication |
def test_disable_switch(self): |
|
|
|
|
130
|
|
|
"""Test disable_swicth.""" |
131
|
|
|
dpid = "00:00:00:00:00:00:00:01" |
132
|
|
|
mock_switch = get_switch_mock(dpid) |
133
|
|
|
msg_success = "Operation successful" |
134
|
|
|
msg_fail = "Switch not found" |
135
|
|
|
self.napp.controller.switches = {dpid: mock_switch} |
136
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
137
|
|
|
|
138
|
|
|
url = f'{self.server_name_url}/v3/switches/{dpid}/disable' |
139
|
|
|
response = api.post(url) |
140
|
|
|
self.assertEqual(response.status_code, 201, response.data) |
141
|
|
|
self.assertEqual(msg_success, json.loads(response.data)) |
142
|
|
|
self.assertEqual(mock_switch.disable.call_count, 1) |
143
|
|
|
|
144
|
|
|
# fail case |
145
|
|
|
mock_switch.disable.call_count = 0 |
146
|
|
|
dpid = "00:00:00:00:00:00:00:02" |
147
|
|
|
url = f'{self.server_name_url}/v3/switches/{dpid}/disable' |
148
|
|
|
response = api.post(url) |
149
|
|
|
self.assertEqual(response.status_code, 404, response.data) |
150
|
|
|
self.assertEqual(msg_fail, json.loads(response.data)) |
151
|
|
|
self.assertEqual(mock_switch.disable.call_count, 0) |
152
|
|
|
|
153
|
|
|
def test_get_switch_metadata(self): |
154
|
|
|
"""Test get_switch_metadata.""" |
155
|
|
|
dpid = "00:00:00:00:00:00:00:01" |
156
|
|
|
mock_switch = get_switch_mock(dpid) |
157
|
|
|
mock_switch.metadata = "A" |
158
|
|
|
self.napp.controller.switches = {dpid: mock_switch} |
159
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
160
|
|
|
|
161
|
|
|
url = f'{self.server_name_url}/v3/switches/{dpid}/metadata' |
162
|
|
|
response = api.get(url) |
163
|
|
|
self.assertEqual(response.status_code, 200, response.data) |
164
|
|
|
|
165
|
|
|
# fail case |
166
|
|
|
dpid = "00:00:00:00:00:00:00:02" |
167
|
|
|
url = f'{self.server_name_url}/v3/switches/{dpid}/metadata' |
168
|
|
|
response = api.get(url) |
169
|
|
|
self.assertEqual(response.status_code, 404, response.data) |
170
|
|
|
|
171
|
|
|
@patch('napps.kytos.topology.main.Main.notify_metadata_changes') |
172
|
|
|
def test_add_switch_metadata(self, mock_metadata_changes): |
173
|
|
|
"""Test add_switch_metadata.""" |
174
|
|
|
dpid = "00:00:00:00:00:00:00:01" |
175
|
|
|
mock_switch = get_switch_mock(dpid) |
176
|
|
|
self.napp.controller.switches = {dpid: mock_switch} |
177
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
178
|
|
|
payload = {"data": "A"} |
179
|
|
|
|
180
|
|
|
url = f'{self.server_name_url}/v3/switches/{dpid}/metadata' |
181
|
|
|
response = api.post(url, data=json.dumps(payload), |
182
|
|
|
content_type='application/json') |
183
|
|
|
self.assertEqual(response.status_code, 201, response.data) |
184
|
|
|
mock_metadata_changes.assert_called() |
185
|
|
|
|
186
|
|
|
# fail case |
187
|
|
|
dpid = "00:00:00:00:00:00:00:02" |
188
|
|
|
url = f'{self.server_name_url}/v3/switches/{dpid}/metadata' |
189
|
|
|
response = api.post(url, data=json.dumps(payload), |
190
|
|
|
content_type='application/json') |
191
|
|
|
self.assertEqual(response.status_code, 404, response.data) |
192
|
|
|
|
193
|
|
|
@patch('napps.kytos.topology.main.Main.notify_metadata_changes') |
194
|
|
|
def test_delete_switch_metadata(self, mock_metadata_changes): |
195
|
|
|
"""Test delete_switch_metadata.""" |
196
|
|
|
dpid = "00:00:00:00:00:00:00:01" |
197
|
|
|
mock_switch = get_switch_mock(dpid) |
198
|
|
|
self.napp.controller.switches = {dpid: mock_switch} |
199
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
200
|
|
|
|
201
|
|
|
key = "A" |
202
|
|
|
url = f'{self.server_name_url}/v3/switches/{dpid}/metadata/{key}' |
203
|
|
|
response = api.delete(url) |
204
|
|
|
mock_metadata_changes.assert_called() |
205
|
|
|
self.assertEqual(response.status_code, 200, response.data) |
206
|
|
|
|
207
|
|
|
# fail case |
208
|
|
|
key = "A" |
209
|
|
|
dpid = "00:00:00:00:00:00:00:02" |
210
|
|
|
url = f'{self.server_name_url}/v3/switches/{dpid}/metadata/{key}' |
211
|
|
|
response = api.delete(url) |
212
|
|
|
mock_metadata_changes.assert_called() |
213
|
|
|
self.assertEqual(response.status_code, 404, response.data) |
214
|
|
|
|
215
|
|
View Code Duplication |
def test_enable_interfaces(self): |
|
|
|
|
216
|
|
|
"""Test enable_interfaces.""" |
217
|
|
|
mock_switch = create_autospec(Switch) |
218
|
|
|
mock_interface_1 = create_autospec(Interface) |
219
|
|
|
mock_interface_2 = create_autospec(Interface) |
220
|
|
|
mock_switch.interfaces = {1: mock_interface_1, 2: mock_interface_2} |
221
|
|
|
self.napp.controller.switches = {'00:00:00:00:00:00:00:01': |
222
|
|
|
mock_switch} |
223
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
224
|
|
|
expected_success = 'Operation successful' |
225
|
|
|
|
226
|
|
|
interface_id = '00:00:00:00:00:00:00:01:1' |
227
|
|
|
url = f'{self.server_name_url}/v3/interfaces/{interface_id}/enable' |
228
|
|
|
response = api.post(url) |
229
|
|
|
self.assertEqual(response.status_code, 200, response.data) |
230
|
|
|
self.assertEqual(expected_success, json.loads(response.data)) |
231
|
|
|
self.assertEqual(mock_interface_1.enable.call_count, 1) |
232
|
|
|
self.assertEqual(mock_interface_2.enable.call_count, 0) |
233
|
|
|
|
234
|
|
|
dpid = '00:00:00:00:00:00:00:01' |
235
|
|
|
mock_interface_1.enable.call_count = 0 |
236
|
|
|
mock_interface_2.enable.call_count = 0 |
237
|
|
|
url = f'{self.server_name_url}/v3/interfaces/switch/{dpid}/enable' |
238
|
|
|
response = api.post(url) |
239
|
|
|
self.assertEqual(response.status_code, 200, response.data) |
240
|
|
|
self.assertEqual(expected_success, json.loads(response.data)) |
241
|
|
|
self.assertEqual(mock_interface_1.enable.call_count, 1) |
242
|
|
|
self.assertEqual(mock_interface_2.enable.call_count, 1) |
243
|
|
|
|
244
|
|
|
# test interface not found |
245
|
|
|
interface_id = '00:00:00:00:00:00:00:01:3' |
246
|
|
|
mock_interface_1.enable.call_count = 0 |
247
|
|
|
mock_interface_2.enable.call_count = 0 |
248
|
|
|
url = f'{self.server_name_url}/v3/interfaces/{interface_id}/enable' |
249
|
|
|
response = api.post(url) |
250
|
|
|
self.assertEqual(response.status_code, 409, response.data) |
251
|
|
|
self.assertEqual(mock_interface_1.enable.call_count, 0) |
252
|
|
|
self.assertEqual(mock_interface_2.enable.call_count, 0) |
253
|
|
|
|
254
|
|
|
# test switch not found |
255
|
|
|
dpid = '00:00:00:00:00:00:00:02' |
256
|
|
|
expected_fail = f"Switch not found: '{dpid}'" |
257
|
|
|
url = f'{self.server_name_url}/v3/interfaces/switch/{dpid}/enable' |
258
|
|
|
response = api.post(url) |
259
|
|
|
self.assertEqual(response.status_code, 404, response.data) |
260
|
|
|
self.assertEqual(expected_fail, json.loads(response.data)) |
261
|
|
|
self.assertEqual(mock_interface_1.enable.call_count, 0) |
262
|
|
|
self.assertEqual(mock_interface_2.enable.call_count, 0) |
263
|
|
|
|
264
|
|
View Code Duplication |
def test_disable_interfaces(self): |
|
|
|
|
265
|
|
|
"""Test disable_interfaces.""" |
266
|
|
|
interface_id = '00:00:00:00:00:00:00:01:1' |
267
|
|
|
dpid = '00:00:00:00:00:00:00:01' |
268
|
|
|
expected = 'Operation successful' |
269
|
|
|
mock_switch = create_autospec(Switch) |
270
|
|
|
mock_interface_1 = create_autospec(Interface) |
271
|
|
|
mock_interface_2 = create_autospec(Interface) |
272
|
|
|
mock_switch.interfaces = {1: mock_interface_1, 2: mock_interface_2} |
273
|
|
|
self.napp.controller.switches = {'00:00:00:00:00:00:00:01': |
274
|
|
|
mock_switch} |
275
|
|
|
api = get_test_client(self.napp.controller, self.napp) |
276
|
|
|
|
277
|
|
|
url = f'{self.server_name_url}/v3/interfaces/{interface_id}/disable' |
278
|
|
|
response = api.post(url) |
279
|
|
|
self.assertEqual(response.status_code, 200, response.data) |
280
|
|
|
self.assertEqual(expected, json.loads(response.data)) |
281
|
|
|
self.assertEqual(mock_interface_1.disable.call_count, 1) |
282
|
|
|
self.assertEqual(mock_interface_2.disable.call_count, 0) |
283
|
|
|
|
284
|
|
|
mock_interface_1.disable.call_count = 0 |
285
|
|
|
mock_interface_2.disable.call_count = 0 |
286
|
|
|
url = f'{self.server_name_url}/v3/interfaces/switch/{dpid}/disable' |
287
|
|
|
response = api.post(url) |
288
|
|
|
self.assertEqual(response.status_code, 200, response.data) |
289
|
|
|
self.assertEqual(expected, json.loads(response.data)) |
290
|
|
|
self.assertEqual(mock_interface_1.disable.call_count, 1) |
291
|
|
|
self.assertEqual(mock_interface_2.disable.call_count, 1) |
292
|
|
|
|
293
|
|
|
# test interface not found |
294
|
|
|
interface_id = '00:00:00:00:00:00:00:01:3' |
295
|
|
|
mock_interface_1.disable.call_count = 0 |
296
|
|
|
mock_interface_2.disable.call_count = 0 |
297
|
|
|
url = f'{self.server_name_url}/v3/interfaces/{interface_id}/disable' |
298
|
|
|
response = api.post(url) |
299
|
|
|
self.assertEqual(response.status_code, 409, response.data) |
300
|
|
|
self.assertEqual(mock_interface_1.disable.call_count, 0) |
301
|
|
|
self.assertEqual(mock_interface_2.disable.call_count, 0) |
302
|
|
|
|
303
|
|
|
# test switch not found |
304
|
|
|
dpid = '00:00:00:00:00:00:00:02' |
305
|
|
|
expected_fail = f"Switch not found: '{dpid}'" |
306
|
|
|
url = f'{self.server_name_url}/v3/interfaces/switch/{dpid}/disable' |
307
|
|
|
response = api.post(url) |
308
|
|
|
self.assertEqual(response.status_code, 404, response.data) |
309
|
|
|
self.assertEqual(expected_fail, json.loads(response.data)) |
310
|
|
|
self.assertEqual(mock_interface_1.disable.call_count, 0) |
311
|
|
|
self.assertEqual(mock_interface_2.disable.call_count, 0) |
312
|
|
|
|
313
|
|
|
@patch('napps.kytos.topology.main.Main.notify_topology_update') |
314
|
|
|
@patch('napps.kytos.topology.main.Main.update_instance_metadata') |
315
|
|
|
def test_handle_new_switch(self, *args): |
316
|
|
|
"""Test handle_new_switch.""" |
317
|
|
|
(mock_instance_metadata, mock_notify_topology_update) = args |
318
|
|
|
mock_event = MagicMock() |
319
|
|
|
mock_switch = create_autospec(Switch) |
320
|
|
|
mock_event.content['switch'] = mock_switch |
321
|
|
|
self.napp.handle_new_switch(mock_event) |
322
|
|
|
mock_notify_topology_update.assert_called() |
323
|
|
|
mock_instance_metadata.assert_called() |
324
|
|
|
|
325
|
|
|
@patch('napps.kytos.topology.main.Main.notify_topology_update') |
326
|
|
|
def test_handle_connection_lost(self, mock_notify_topology_update): |
327
|
|
|
"""Test handle connection_lost.""" |
328
|
|
|
mock_event = MagicMock() |
329
|
|
|
mock_switch = create_autospec(Switch) |
330
|
|
|
mock_switch.return_value = True |
331
|
|
|
mock_event.content['source'] = mock_switch |
332
|
|
|
self.napp.handle_connection_lost(mock_event) |
333
|
|
|
mock_notify_topology_update.assert_called() |
334
|
|
|
|
335
|
|
|
@patch('napps.kytos.topology.main.Main.notify_topology_update') |
336
|
|
|
@patch('napps.kytos.topology.main.Main.update_instance_metadata') |
337
|
|
|
def test_handle_interface_up(self, *args): |
338
|
|
|
"""Test handle_interface_up.""" |
339
|
|
|
(mock_instance_metadata, mock_notify_topology_update) = args |
340
|
|
|
mock_event = MagicMock() |
341
|
|
|
mock_interface = create_autospec(Interface) |
342
|
|
|
mock_event.content['interface'] = mock_interface |
343
|
|
|
self.napp.handle_interface_up(mock_event) |
344
|
|
|
mock_notify_topology_update.assert_called() |
345
|
|
|
mock_instance_metadata.assert_called() |
346
|
|
|
|
347
|
|
|
@patch('napps.kytos.topology.main.Main.handle_interface_up') |
348
|
|
|
def test_handle_interface_created(self, mock_handle_interface_up): |
349
|
|
|
"""Test handle interface created.""" |
350
|
|
|
mock_event = MagicMock() |
351
|
|
|
self.napp.handle_interface_created(mock_event) |
352
|
|
|
mock_handle_interface_up.assert_called() |
353
|
|
|
|
354
|
|
|
@patch('napps.kytos.topology.main.Main.notify_topology_update') |
355
|
|
|
@patch('napps.kytos.topology.main.Main.handle_interface_link_down') |
356
|
|
|
def test_handle_interface_down(self, *args): |
357
|
|
|
"""Test handle interface down.""" |
358
|
|
|
(mock_handle_interface_link_down, mock_notify_topology_update) = args |
359
|
|
|
mock_event = MagicMock() |
360
|
|
|
mock_interface = create_autospec(Interface) |
361
|
|
|
mock_event.content['interface'] = mock_interface |
362
|
|
|
self.napp.handle_interface_down(mock_event) |
363
|
|
|
mock_handle_interface_link_down.assert_called() |
364
|
|
|
mock_notify_topology_update.assert_called() |
365
|
|
|
|
366
|
|
|
@patch('napps.kytos.topology.main.Main.handle_interface_down') |
367
|
|
|
def test_interface_deleted(self, mock_handle_interface_link_down): |
368
|
|
|
"""Test interface deleted.""" |
369
|
|
|
mock_event = MagicMock() |
370
|
|
|
self.napp.handle_interface_deleted(mock_event) |
371
|
|
|
mock_handle_interface_link_down.assert_called() |
372
|
|
|
|
373
|
|
|
@patch('napps.kytos.topology.main.Main._get_link_from_interface') |
374
|
|
|
@patch('napps.kytos.topology.main.Main.notify_topology_update') |
375
|
|
|
@patch('napps.kytos.topology.main.Main.update_instance_metadata') |
376
|
|
|
@patch('napps.kytos.topology.main.Main.notify_link_status_change') |
377
|
|
|
def test_interface_link_up(self, *args): |
378
|
|
|
"""Test interface link_up.""" |
379
|
|
|
(mock_status_change, mock_instance_metadata, mock_topology_update, |
380
|
|
|
mock_link_from_interface) = args |
381
|
|
|
|
382
|
|
|
now = time.time() |
383
|
|
|
mock_event = MagicMock() |
384
|
|
|
mock_interface_a = create_autospec(Interface) |
385
|
|
|
mock_interface_a.is_active.return_value = False |
386
|
|
|
mock_interface_b = create_autospec(Interface) |
387
|
|
|
mock_interface_b.is_active.return_value = True |
388
|
|
|
mock_link = create_autospec(Link) |
389
|
|
|
mock_link.get_metadata.return_value = now |
390
|
|
|
mock_link.is_active.side_effect = [False, True] |
391
|
|
|
mock_link.endpoint_a = mock_interface_a |
392
|
|
|
mock_link.endpoint_b = mock_interface_b |
393
|
|
|
mock_link_from_interface.return_value = mock_link |
394
|
|
|
content = {'interface': mock_interface_a} |
395
|
|
|
mock_event.content = content |
396
|
|
|
self.napp.link_up_timer = 1 |
397
|
|
|
self.napp.handle_interface_link_up(mock_event) |
398
|
|
|
mock_topology_update.assert_called() |
399
|
|
|
mock_instance_metadata.assert_called() |
400
|
|
|
mock_status_change.assert_called() |
401
|
|
|
|
402
|
|
|
@patch('napps.kytos.topology.main.Main._get_link_from_interface') |
403
|
|
|
@patch('napps.kytos.topology.main.Main.notify_topology_update') |
404
|
|
|
@patch('napps.kytos.topology.main.Main.notify_link_status_change') |
405
|
|
|
def test_interface_link_down(self, *args): |
406
|
|
|
"""Test interface link down.""" |
407
|
|
|
(mock_status_change, mock_topology_update, |
408
|
|
|
mock_link_from_interface) = args |
409
|
|
|
|
410
|
|
|
mock_event = MagicMock() |
411
|
|
|
mock_interface = create_autospec(Interface) |
412
|
|
|
mock_link = create_autospec(Link) |
413
|
|
|
mock_link.is_active.return_value = True |
414
|
|
|
mock_link_from_interface.return_value = mock_link |
415
|
|
|
mock_event.content['interface'] = mock_interface |
416
|
|
|
self.napp.handle_interface_link_down(mock_event) |
417
|
|
|
mock_topology_update.assert_called() |
418
|
|
|
mock_status_change.assert_called() |
419
|
|
|
|
420
|
|
|
@patch('napps.kytos.topology.main.Main._get_link_or_create') |
421
|
|
|
@patch('napps.kytos.topology.main.Main.notify_topology_update') |
422
|
|
|
def test_add_links(self, *args): |
423
|
|
|
"""Test add_links.""" |
424
|
|
|
(mock_notify_topology_update, mock_get_link_or_create) = args |
425
|
|
|
mock_event = MagicMock() |
426
|
|
|
self.napp.add_links(mock_event) |
427
|
|
|
mock_get_link_or_create.assert_called() |
428
|
|
|
mock_notify_topology_update.assert_called() |
429
|
|
|
|
430
|
|
|
@patch('napps.kytos.topology.main.KytosEvent') |
431
|
|
|
@patch('kytos.core.buffers.KytosEventBuffer.put') |
432
|
|
|
def test_notify_topology_update(self, *args): |
433
|
|
|
"""Test notify_topology_update.""" |
434
|
|
|
(mock_buffers_put, mock_event) = args |
435
|
|
|
self.napp.notify_topology_update() |
436
|
|
|
mock_event.assert_called() |
437
|
|
|
mock_buffers_put.assert_called() |
438
|
|
|
|
439
|
|
|
@patch('napps.kytos.topology.main.KytosEvent') |
440
|
|
|
@patch('kytos.core.buffers.KytosEventBuffer.put') |
441
|
|
|
def test_notify_link_status_change(self, *args): |
442
|
|
|
"""Test notify link status change.""" |
443
|
|
|
(mock_buffers_put, mock_event) = args |
444
|
|
|
mock_link = create_autospec(Link) |
445
|
|
|
self.napp.notify_link_status_change(mock_link) |
446
|
|
|
mock_event.assert_called() |
447
|
|
|
mock_buffers_put.assert_called() |
448
|
|
|
|
449
|
|
|
@patch('napps.kytos.topology.main.KytosEvent') |
450
|
|
|
@patch('kytos.core.buffers.KytosEventBuffer.put') |
451
|
|
|
@patch('napps.kytos.topology.main.isinstance') |
452
|
|
|
def test_notify_metadata_changes(self, *args): |
453
|
|
|
"""Test notify metadata changes.""" |
454
|
|
|
(mock_isinstance, mock_buffers_put, mock_event) = args |
455
|
|
|
mock_isinstance.return_value = True |
456
|
|
|
mock_obj = MagicMock() |
457
|
|
|
mock_action = create_autospec(Switch) |
458
|
|
|
self.napp.notify_metadata_changes(mock_obj, mock_action) |
459
|
|
|
mock_event.assert_called() |
460
|
|
|
mock_isinstance.assert_called() |
461
|
|
|
mock_buffers_put.assert_called() |
462
|
|
|
|
463
|
|
|
@patch('napps.kytos.topology.main.KytosEvent') |
464
|
|
|
@patch('kytos.core.buffers.KytosEventBuffer.put') |
465
|
|
|
def test_notify_port_created(self, *args): |
466
|
|
|
"""Test notify port created.""" |
467
|
|
|
(mock_buffers_put, mock_kytos_event) = args |
468
|
|
|
mock_event = MagicMock() |
469
|
|
|
self.napp.notify_port_created(mock_event) |
470
|
|
|
mock_kytos_event.assert_called() |
471
|
|
|
mock_buffers_put.assert_called() |
472
|
|
|
|
473
|
|
|
@patch('napps.kytos.topology.main.KytosEvent') |
474
|
|
|
@patch('kytos.core.buffers.KytosEventBuffer.put') |
475
|
|
|
def test_verify_storehouse(self, *args): |
476
|
|
|
"""Test verify_storehouse.""" |
477
|
|
|
(mock_buffers_put, mock_kytos_event) = args |
478
|
|
|
mock_entities = MagicMock() |
479
|
|
|
self.napp.verify_storehouse(mock_entities) |
480
|
|
|
mock_buffers_put.assert_called() |
481
|
|
|
mock_kytos_event.assert_called() |
482
|
|
|
|
483
|
|
|
@patch('napps.kytos.topology.main.Main.notify_link_status_change') |
484
|
|
|
def test_handle_link_maintenance_start(self, status_change_mock): |
485
|
|
|
"""Test handle_link_maintenance_start.""" |
486
|
|
|
link1 = MagicMock() |
487
|
|
|
link1.id = 2 |
488
|
|
|
link2 = MagicMock() |
489
|
|
|
link2.id = 3 |
490
|
|
|
link3 = MagicMock() |
491
|
|
|
link3.id = 4 |
492
|
|
|
content = {'links': [link1, link2]} |
493
|
|
|
event = MagicMock() |
494
|
|
|
event.content = content |
495
|
|
|
self.napp.links = {2: link1, 4: link3} |
496
|
|
|
self.napp.handle_link_maintenance_start(event) |
497
|
|
|
status_change_mock.assert_called_once_with(link1) |
498
|
|
|
|
499
|
|
|
@patch('napps.kytos.topology.main.Main.notify_link_status_change') |
500
|
|
|
def test_handle_link_maintenance_end(self, status_change_mock): |
501
|
|
|
"""Test handle_link_maintenance_end.""" |
502
|
|
|
link1 = MagicMock() |
503
|
|
|
link1.id = 2 |
504
|
|
|
link2 = MagicMock() |
505
|
|
|
link2.id = 3 |
506
|
|
|
link3 = MagicMock() |
507
|
|
|
link3.id = 4 |
508
|
|
|
content = {'links': [link1, link2]} |
509
|
|
|
event = MagicMock() |
510
|
|
|
event.content = content |
511
|
|
|
self.napp.links = {2: link1, 4: link3} |
512
|
|
|
self.napp.handle_link_maintenance_end(event) |
513
|
|
|
status_change_mock.assert_called_once_with(link1) |
514
|
|
|
|
515
|
|
View Code Duplication |
@patch('napps.kytos.topology.main.Main.handle_link_down') |
|
|
|
|
516
|
|
|
def test_handle_switch_maintenance_start(self, handle_link_down_mock): |
517
|
|
|
"""Test handle_switch_maintenance_start.""" |
518
|
|
|
switch1 = MagicMock() |
519
|
|
|
interface1 = MagicMock() |
520
|
|
|
interface1.is_active.return_value = True |
521
|
|
|
interface2 = MagicMock() |
522
|
|
|
interface2.is_active.return_value = False |
523
|
|
|
interface3 = MagicMock() |
524
|
|
|
interface3.is_active.return_value = True |
525
|
|
|
switch1.interfaces = {1: interface1, 2: interface2, 3: interface3} |
526
|
|
|
switch2 = MagicMock() |
527
|
|
|
interface4 = MagicMock() |
528
|
|
|
interface4.is_active.return_value = False |
529
|
|
|
interface5 = MagicMock() |
530
|
|
|
interface5.is_active.return_value = True |
531
|
|
|
switch2.interfaces = {1: interface4, 2: interface5} |
532
|
|
|
content = {'switches': [switch1, switch2]} |
533
|
|
|
event = MagicMock() |
534
|
|
|
event.content = content |
535
|
|
|
self.napp.handle_switch_maintenance_start(event) |
536
|
|
|
self.assertEqual(handle_link_down_mock.call_count, 3) |
537
|
|
|
|
538
|
|
View Code Duplication |
@patch('napps.kytos.topology.main.Main.handle_link_up') |
|
|
|
|
539
|
|
|
def test_handle_switch_maintenance_end(self, handle_link_up_mock): |
540
|
|
|
"""Test handle_switch_maintenance_end.""" |
541
|
|
|
switch1 = MagicMock() |
542
|
|
|
interface1 = MagicMock() |
543
|
|
|
interface1.is_active.return_value = True |
544
|
|
|
interface2 = MagicMock() |
545
|
|
|
interface2.is_active.return_value = False |
546
|
|
|
interface3 = MagicMock() |
547
|
|
|
interface3.is_active.return_value = True |
548
|
|
|
switch1.interfaces = {1: interface1, 2: interface2, 3: interface3} |
549
|
|
|
switch2 = MagicMock() |
550
|
|
|
interface4 = MagicMock() |
551
|
|
|
interface4.is_active.return_value = False |
552
|
|
|
interface5 = MagicMock() |
553
|
|
|
interface5.is_active.return_value = True |
554
|
|
|
switch2.interfaces = {1: interface4, 2: interface5} |
555
|
|
|
content = {'switches': [switch1, switch2]} |
556
|
|
|
event = MagicMock() |
557
|
|
|
event.content = content |
558
|
|
|
self.napp.handle_switch_maintenance_end(event) |
559
|
|
|
self.assertEqual(handle_link_up_mock.call_count, 5) |
560
|
|
|
|