Total Complexity | 41 |
Total Lines | 880 |
Duplicated Lines | 25.23 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like build.tests.unit.test_main often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | """Module to test the main napp file.""" |
||
2 | 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_interface_mock, |
||
12 | get_test_client) |
||
13 | |||
14 | |||
15 | from tests.unit.helpers import get_controller_mock, get_napp_urls |
||
16 | |||
17 | |||
18 | # pylint: disable=too-many-public-methods |
||
19 | class TestMain(TestCase): |
||
20 | """Test the Main class.""" |
||
21 | # pylint: disable=too-many-public-methods |
||
22 | |||
23 | def setUp(self): |
||
24 | """Execute steps before each tests. |
||
25 | |||
26 | Set the server_name_url_url from kytos/topology |
||
27 | """ |
||
28 | self.server_name_url = 'http://localhost:8181/api/kytos/topology' |
||
29 | |||
30 | patch('kytos.core.helpers.run_on_thread', lambda x: x).start() |
||
31 | from napps.kytos.topology.main import Main |
||
32 | self.addCleanup(patch.stopall) |
||
33 | |||
34 | self.napp = Main(get_controller_mock()) |
||
35 | |||
36 | def test_get_event_listeners(self): |
||
37 | """Verify all event listeners registered.""" |
||
38 | expected_events = ['kytos/core.shutdown', |
||
39 | 'kytos/core.shutdown.kytos/topology', |
||
40 | 'kytos/maintenance.start_link', |
||
41 | 'kytos/maintenance.end_link', |
||
42 | 'kytos/maintenance.start_switch', |
||
43 | 'kytos/maintenance.end_switch', |
||
44 | '.*.interface.is.nni', |
||
45 | '.*.connection.lost', |
||
46 | '.*.switch.interface.created', |
||
47 | '.*.switch.interface.deleted', |
||
48 | '.*.switch.interface.link_down', |
||
49 | '.*.switch.interface.link_up', |
||
50 | '.*.switch.(new|reconnected)', |
||
51 | '.*.switch.port.created', |
||
52 | 'kytos/topology.*.metadata.*'] |
||
53 | actual_events = self.napp.listeners() |
||
54 | self.assertCountEqual(expected_events, actual_events) |
||
55 | |||
56 | View Code Duplication | def test_verify_api_urls(self): |
|
|
|||
57 | """Verify all APIs registered.""" |
||
58 | expected_urls = [ |
||
59 | ({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/interfaces'), |
||
60 | ({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/switches'), |
||
61 | ({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/restore'), |
||
62 | ({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/links'), |
||
63 | ({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/'), |
||
64 | ({'dpid': '[dpid]'}, {'POST', 'OPTIONS'}, |
||
65 | '/api/kytos/topology/v3/interfaces/switch/<dpid>/disable'), |
||
66 | ({'dpid': '[dpid]'}, {'POST', 'OPTIONS'}, |
||
67 | '/api/kytos/topology/v3/interfaces/switch/<dpid>/enable'), |
||
68 | ({'key': '[key]', 'interface_id': '[interface_id]'}, |
||
69 | {'OPTIONS', 'DELETE'}, |
||
70 | '/api/kytos/topology/v3/interfaces/<interface_id>/metadata/<key>'), |
||
71 | ({'interface_id': '[interface_id]'}, {'POST', 'OPTIONS'}, |
||
72 | '/api/kytos/topology/v3/interfaces/<interface_id>/metadata'), |
||
73 | ({'interface_id': '[interface_id]'}, {'GET', 'OPTIONS', 'HEAD'}, |
||
74 | '/api/kytos/topology/v3/interfaces/<interface_id>/metadata'), |
||
75 | ({'interface_disable_id': '[interface_disable_id]'}, |
||
76 | {'POST', 'OPTIONS'}, |
||
77 | '/api/kytos/topology/v3/interfaces/<interface_disable_id>/disable'), |
||
78 | ({'interface_enable_id': '[interface_enable_id]'}, |
||
79 | {'POST', 'OPTIONS'}, |
||
80 | '/api/kytos/topology/v3/interfaces/<interface_enable_id>/enable'), |
||
81 | ({'dpid': '[dpid]', 'key': '[key]'}, {'OPTIONS', 'DELETE'}, |
||
82 | '/api/kytos/topology/v3/switches/<dpid>/metadata/<key>'), |
||
83 | ({'dpid': '[dpid]'}, {'POST', 'OPTIONS'}, |
||
84 | '/api/kytos/topology/v3/switches/<dpid>/metadata'), |
||
85 | ({'dpid': '[dpid]'}, {'GET', 'OPTIONS', 'HEAD'}, |
||
86 | '/api/kytos/topology/v3/switches/<dpid>/metadata'), |
||
87 | ({'dpid': '[dpid]'}, {'POST', 'OPTIONS'}, |
||
88 | '/api/kytos/topology/v3/switches/<dpid>/disable'), |
||
89 | ({'dpid': '[dpid]'}, {'POST', 'OPTIONS'}, |
||
90 | '/api/kytos/topology/v3/switches/<dpid>/enable'), |
||
91 | ({'link_id': '[link_id]', 'key': '[key]'}, {'OPTIONS', 'DELETE'}, |
||
92 | '/api/kytos/topology/v3/links/<link_id>/metadata/<key>'), |
||
93 | ({'link_id': '[link_id]'}, {'POST', 'OPTIONS'}, |
||
94 | '/api/kytos/topology/v3/links/<link_id>/metadata'), |
||
95 | ({'link_id': '[link_id]'}, {'GET', 'OPTIONS', 'HEAD'}, |
||
96 | '/api/kytos/topology/v3/links/<link_id>/metadata'), |
||
97 | ({'link_id': '[link_id]'}, {'POST', 'OPTIONS'}, |
||
98 | '/api/kytos/topology/v3/links/<link_id>/disable'), |
||
99 | ({'link_id': '[link_id]'}, {'POST', 'OPTIONS'}, |
||
100 | '/api/kytos/topology/v3/links/<link_id>/enable')] |
||
101 | |||
102 | urls = get_napp_urls(self.napp) |
||
103 | self.assertEqual(expected_urls, urls) |
||
104 | |||
105 | @patch('napps.kytos.topology.main.StoreHouse.get_data') |
||
106 | def test_restore_network_status(self, mock_storehouse): |
||
107 | """Test restore_network_status.""" |
||
108 | dpid = '00:00:00:00:00:00:00:01' |
||
109 | mock_switch = get_switch_mock(dpid) |
||
110 | mock_interface = get_interface_mock('s1-eth1', 1, mock_switch) |
||
111 | mock_switch.interfaces = {1: mock_interface} |
||
112 | self.napp.controller.switches = {dpid: mock_switch} |
||
113 | status = { |
||
114 | 'network_status': { |
||
115 | 'id': 'network_status', |
||
116 | 'switches': { |
||
117 | '00:00:00:00:00:00:00:01': { |
||
118 | 'dpid': '00:00:00:00:00:00:00:01', |
||
119 | 'enabled': True, |
||
120 | 'id': '00:00:00:00:00:00:00:01', |
||
121 | 'interfaces': { |
||
122 | '00:00:00:00:00:00:00:01:1': { |
||
123 | 'enabled': True, |
||
124 | 'id': '00:00:00:00:00:00:00:01:1', |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | } |
||
131 | status_disable = { |
||
132 | 'network_status': { |
||
133 | 'id': 'network_status', |
||
134 | 'switches': { |
||
135 | '00:00:00:00:00:00:00:01': { |
||
136 | 'dpid': '00:00:00:00:00:00:00:01', |
||
137 | 'enabled': False, |
||
138 | 'id': '00:00:00:00:00:00:00:01', |
||
139 | 'interfaces': { |
||
140 | '00:00:00:00:00:00:00:01:1': { |
||
141 | 'enabled': False, |
||
142 | 'id': '00:00:00:00:00:00:00:01:1', |
||
143 | } |
||
144 | } |
||
145 | } |
||
146 | } |
||
147 | } |
||
148 | } |
||
149 | |||
150 | api = get_test_client(self.napp.controller, self.napp) |
||
151 | mock_storehouse.side_effect = [{}, status, status_disable] |
||
152 | |||
153 | # fail case |
||
154 | url = f'{self.server_name_url}/v3/restore' |
||
155 | response = api.get(url) |
||
156 | self.assertEqual(response.status_code, 404, response.data) |
||
157 | |||
158 | # enable |
||
159 | url = f'{self.server_name_url}/v3/restore' |
||
160 | response = api.get(url) |
||
161 | self.assertEqual(response.status_code, 200, response.data) |
||
162 | self.assertEqual(mock_switch.enable.call_count, 1) |
||
163 | self.assertEqual(mock_interface.enable.call_count, 1) |
||
164 | |||
165 | # disable |
||
166 | url = f'{self.server_name_url}/v3/restore' |
||
167 | response = api.get(url) |
||
168 | self.assertEqual(response.status_code, 200, response.data) |
||
169 | self.assertEqual(mock_switch.disable.call_count, 1) |
||
170 | self.assertEqual(mock_interface.disable.call_count, 1) |
||
171 | |||
172 | View Code Duplication | @patch('napps.kytos.topology.main.Main.save_status_on_storehouse') |
|
173 | def test_enable_switch(self, mock_save_status): |
||
174 | """Test enable_switch.""" |
||
175 | dpid = "00:00:00:00:00:00:00:01" |
||
176 | mock_switch = get_switch_mock(dpid) |
||
177 | self.napp.controller.switches = {dpid: mock_switch} |
||
178 | api = get_test_client(self.napp.controller, self.napp) |
||
179 | |||
180 | url = f'{self.server_name_url}/v3/switches/{dpid}/enable' |
||
181 | response = api.post(url) |
||
182 | self.assertEqual(response.status_code, 201, response.data) |
||
183 | self.assertEqual(mock_switch.enable.call_count, 1) |
||
184 | mock_save_status.assert_called() |
||
185 | |||
186 | # fail case |
||
187 | mock_switch.enable.call_count = 0 |
||
188 | dpid = "00:00:00:00:00:00:00:02" |
||
189 | url = f'{self.server_name_url}/v3/switches/{dpid}/enable' |
||
190 | response = api.post(url) |
||
191 | self.assertEqual(response.status_code, 404, response.data) |
||
192 | self.assertEqual(mock_switch.enable.call_count, 0) |
||
193 | |||
194 | View Code Duplication | @patch('napps.kytos.topology.main.Main.save_status_on_storehouse') |
|
195 | def test_disable_switch(self, mock_save_status): |
||
196 | """Test disable_switch.""" |
||
197 | dpid = "00:00:00:00:00:00:00:01" |
||
198 | mock_switch = get_switch_mock(dpid) |
||
199 | self.napp.controller.switches = {dpid: mock_switch} |
||
200 | api = get_test_client(self.napp.controller, self.napp) |
||
201 | |||
202 | url = f'{self.server_name_url}/v3/switches/{dpid}/disable' |
||
203 | response = api.post(url) |
||
204 | self.assertEqual(response.status_code, 201, response.data) |
||
205 | self.assertEqual(mock_switch.disable.call_count, 1) |
||
206 | mock_save_status.assert_called() |
||
207 | |||
208 | # fail case |
||
209 | mock_switch.disable.call_count = 0 |
||
210 | dpid = "00:00:00:00:00:00:00:02" |
||
211 | url = f'{self.server_name_url}/v3/switches/{dpid}/disable' |
||
212 | response = api.post(url) |
||
213 | self.assertEqual(response.status_code, 404, response.data) |
||
214 | self.assertEqual(mock_switch.disable.call_count, 0) |
||
215 | |||
216 | def test_get_switch_metadata(self): |
||
217 | """Test get_switch_metadata.""" |
||
218 | dpid = "00:00:00:00:00:00:00:01" |
||
219 | mock_switch = get_switch_mock(dpid) |
||
220 | mock_switch.metadata = "A" |
||
221 | self.napp.controller.switches = {dpid: mock_switch} |
||
222 | api = get_test_client(self.napp.controller, self.napp) |
||
223 | |||
224 | url = f'{self.server_name_url}/v3/switches/{dpid}/metadata' |
||
225 | response = api.get(url) |
||
226 | self.assertEqual(response.status_code, 200, response.data) |
||
227 | |||
228 | # fail case |
||
229 | dpid = "00:00:00:00:00:00:00:02" |
||
230 | url = f'{self.server_name_url}/v3/switches/{dpid}/metadata' |
||
231 | response = api.get(url) |
||
232 | self.assertEqual(response.status_code, 404, response.data) |
||
233 | |||
234 | @patch('napps.kytos.topology.main.Main.notify_metadata_changes') |
||
235 | def test_add_switch_metadata(self, mock_metadata_changes): |
||
236 | """Test add_switch_metadata.""" |
||
237 | dpid = "00:00:00:00:00:00:00:01" |
||
238 | mock_switch = get_switch_mock(dpid) |
||
239 | self.napp.controller.switches = {dpid: mock_switch} |
||
240 | api = get_test_client(self.napp.controller, self.napp) |
||
241 | payload = {"data": "A"} |
||
242 | |||
243 | url = f'{self.server_name_url}/v3/switches/{dpid}/metadata' |
||
244 | response = api.post(url, data=json.dumps(payload), |
||
245 | content_type='application/json') |
||
246 | self.assertEqual(response.status_code, 201, response.data) |
||
247 | mock_metadata_changes.assert_called() |
||
248 | |||
249 | # fail case |
||
250 | dpid = "00:00:00:00:00:00:00:02" |
||
251 | url = f'{self.server_name_url}/v3/switches/{dpid}/metadata' |
||
252 | response = api.post(url, data=json.dumps(payload), |
||
253 | content_type='application/json') |
||
254 | self.assertEqual(response.status_code, 404, response.data) |
||
255 | |||
256 | @patch('napps.kytos.topology.main.Main.notify_metadata_changes') |
||
257 | def test_delete_switch_metadata(self, mock_metadata_changes): |
||
258 | """Test delete_switch_metadata.""" |
||
259 | dpid = "00:00:00:00:00:00:00:01" |
||
260 | mock_switch = get_switch_mock(dpid) |
||
261 | self.napp.controller.switches = {dpid: mock_switch} |
||
262 | api = get_test_client(self.napp.controller, self.napp) |
||
263 | |||
264 | key = "A" |
||
265 | url = f'{self.server_name_url}/v3/switches/{dpid}/metadata/{key}' |
||
266 | response = api.delete(url) |
||
267 | mock_metadata_changes.assert_called() |
||
268 | self.assertEqual(response.status_code, 200, response.data) |
||
269 | |||
270 | # fail case |
||
271 | key = "A" |
||
272 | dpid = "00:00:00:00:00:00:00:02" |
||
273 | url = f'{self.server_name_url}/v3/switches/{dpid}/metadata/{key}' |
||
274 | response = api.delete(url) |
||
275 | mock_metadata_changes.assert_called() |
||
276 | self.assertEqual(response.status_code, 404, response.data) |
||
277 | |||
278 | View Code Duplication | @patch('napps.kytos.topology.main.Main.save_status_on_storehouse') |
|
279 | def test_enable_interfaces(self, mock_save_status): |
||
280 | """Test enable_interfaces.""" |
||
281 | dpid = '00:00:00:00:00:00:00:01' |
||
282 | mock_switch = get_switch_mock(dpid) |
||
283 | mock_interface_1 = get_interface_mock('s1-eth1', 1, mock_switch) |
||
284 | mock_interface_2 = get_interface_mock('s1-eth2', 2, mock_switch) |
||
285 | mock_switch.interfaces = {1: mock_interface_1, 2: mock_interface_2} |
||
286 | self.napp.controller.switches = {dpid: mock_switch} |
||
287 | api = get_test_client(self.napp.controller, self.napp) |
||
288 | |||
289 | interface_id = '00:00:00:00:00:00:00:01:1' |
||
290 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/enable' |
||
291 | response = api.post(url) |
||
292 | self.assertEqual(response.status_code, 200, response.data) |
||
293 | self.assertEqual(mock_interface_1.enable.call_count, 1) |
||
294 | self.assertEqual(mock_interface_2.enable.call_count, 0) |
||
295 | mock_save_status.assert_called() |
||
296 | |||
297 | mock_interface_1.enable.call_count = 0 |
||
298 | mock_interface_2.enable.call_count = 0 |
||
299 | url = f'{self.server_name_url}/v3/interfaces/switch/{dpid}/enable' |
||
300 | response = api.post(url) |
||
301 | self.assertEqual(response.status_code, 200, response.data) |
||
302 | self.assertEqual(mock_interface_1.enable.call_count, 1) |
||
303 | self.assertEqual(mock_interface_2.enable.call_count, 1) |
||
304 | |||
305 | # test interface not found |
||
306 | interface_id = '00:00:00:00:00:00:00:01:3' |
||
307 | mock_interface_1.enable.call_count = 0 |
||
308 | mock_interface_2.enable.call_count = 0 |
||
309 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/enable' |
||
310 | response = api.post(url) |
||
311 | self.assertEqual(response.status_code, 409, response.data) |
||
312 | self.assertEqual(mock_interface_1.enable.call_count, 0) |
||
313 | self.assertEqual(mock_interface_2.enable.call_count, 0) |
||
314 | |||
315 | # test switch not found |
||
316 | dpid = '00:00:00:00:00:00:00:02' |
||
317 | url = f'{self.server_name_url}/v3/interfaces/switch/{dpid}/enable' |
||
318 | response = api.post(url) |
||
319 | self.assertEqual(response.status_code, 404, response.data) |
||
320 | self.assertEqual(mock_interface_1.enable.call_count, 0) |
||
321 | self.assertEqual(mock_interface_2.enable.call_count, 0) |
||
322 | |||
323 | View Code Duplication | @patch('napps.kytos.topology.main.Main.save_status_on_storehouse') |
|
324 | def test_disable_interfaces(self, mock_save_status): |
||
325 | """Test disable_interfaces.""" |
||
326 | interface_id = '00:00:00:00:00:00:00:01:1' |
||
327 | dpid = '00:00:00:00:00:00:00:01' |
||
328 | mock_switch = get_switch_mock(dpid) |
||
329 | mock_interface_1 = get_interface_mock('s1-eth1', 1, mock_switch) |
||
330 | mock_interface_2 = get_interface_mock('s1-eth2', 2, mock_switch) |
||
331 | mock_switch.interfaces = {1: mock_interface_1, 2: mock_interface_2} |
||
332 | self.napp.controller.switches = {dpid: mock_switch} |
||
333 | api = get_test_client(self.napp.controller, self.napp) |
||
334 | |||
335 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/disable' |
||
336 | response = api.post(url) |
||
337 | self.assertEqual(response.status_code, 200, response.data) |
||
338 | self.assertEqual(mock_interface_1.disable.call_count, 1) |
||
339 | self.assertEqual(mock_interface_2.disable.call_count, 0) |
||
340 | mock_save_status.assert_called() |
||
341 | |||
342 | mock_interface_1.disable.call_count = 0 |
||
343 | mock_interface_2.disable.call_count = 0 |
||
344 | url = f'{self.server_name_url}/v3/interfaces/switch/{dpid}/disable' |
||
345 | response = api.post(url) |
||
346 | self.assertEqual(response.status_code, 200, response.data) |
||
347 | self.assertEqual(mock_interface_1.disable.call_count, 1) |
||
348 | self.assertEqual(mock_interface_2.disable.call_count, 1) |
||
349 | |||
350 | # test interface not found |
||
351 | interface_id = '00:00:00:00:00:00:00:01:3' |
||
352 | mock_interface_1.disable.call_count = 0 |
||
353 | mock_interface_2.disable.call_count = 0 |
||
354 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/disable' |
||
355 | response = api.post(url) |
||
356 | self.assertEqual(response.status_code, 409, response.data) |
||
357 | self.assertEqual(mock_interface_1.disable.call_count, 0) |
||
358 | self.assertEqual(mock_interface_2.disable.call_count, 0) |
||
359 | |||
360 | # test switch not found |
||
361 | dpid = '00:00:00:00:00:00:00:02' |
||
362 | url = f'{self.server_name_url}/v3/interfaces/switch/{dpid}/disable' |
||
363 | response = api.post(url) |
||
364 | self.assertEqual(response.status_code, 404, response.data) |
||
365 | self.assertEqual(mock_interface_1.disable.call_count, 0) |
||
366 | self.assertEqual(mock_interface_2.disable.call_count, 0) |
||
367 | |||
368 | def test_get_interface_metadata(self): |
||
369 | """Test get_interface_metada.""" |
||
370 | interface_id = '00:00:00:00:00:00:00:01:1' |
||
371 | dpid = '00:00:00:00:00:00:00:01' |
||
372 | mock_switch = get_switch_mock(dpid) |
||
373 | mock_interface = get_interface_mock('s1-eth1', 1, mock_switch) |
||
374 | mock_interface.metadata = {"metada": "A"} |
||
375 | mock_switch.interfaces = {1: mock_interface} |
||
376 | self.napp.controller.switches = {dpid: mock_switch} |
||
377 | api = get_test_client(self.napp.controller, self.napp) |
||
378 | |||
379 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/metadata' |
||
380 | response = api.get(url) |
||
381 | self.assertEqual(response.status_code, 200, response.data) |
||
382 | |||
383 | # fail case switch not found |
||
384 | interface_id = '00:00:00:00:00:00:00:02:1' |
||
385 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/metadata' |
||
386 | response = api.get(url) |
||
387 | self.assertEqual(response.status_code, 404, response.data) |
||
388 | |||
389 | # fail case interface not found |
||
390 | interface_id = '00:00:00:00:00:00:00:01:2' |
||
391 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/metadata' |
||
392 | response = api.get(url) |
||
393 | self.assertEqual(response.status_code, 404, response.data) |
||
394 | |||
395 | @patch('napps.kytos.topology.main.Main.notify_metadata_changes') |
||
396 | def test_add_interface_metadata(self, mock_metadata_changes): |
||
397 | """Test add_interface_metadata.""" |
||
398 | interface_id = '00:00:00:00:00:00:00:01:1' |
||
399 | dpid = '00:00:00:00:00:00:00:01' |
||
400 | mock_switch = get_switch_mock(dpid) |
||
401 | mock_interface = get_interface_mock('s1-eth1', 1, mock_switch) |
||
402 | mock_interface.metadata = {"metada": "A"} |
||
403 | mock_switch.interfaces = {1: mock_interface} |
||
404 | self.napp.controller.switches = {dpid: mock_switch} |
||
405 | api = get_test_client(self.napp.controller, self.napp) |
||
406 | |||
407 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/metadata' |
||
408 | payload = {"metada": "A"} |
||
409 | response = api.post(url, data=json.dumps(payload), |
||
410 | content_type='application/json') |
||
411 | self.assertEqual(response.status_code, 201, response.data) |
||
412 | mock_metadata_changes.assert_called() |
||
413 | |||
414 | # fail case switch not found |
||
415 | interface_id = '00:00:00:00:00:00:00:02:1' |
||
416 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/metadata' |
||
417 | response = api.post(url, data=json.dumps(payload), |
||
418 | content_type='application/json') |
||
419 | self.assertEqual(response.status_code, 404, response.data) |
||
420 | |||
421 | # fail case interface not found |
||
422 | interface_id = '00:00:00:00:00:00:00:01:2' |
||
423 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/metadata' |
||
424 | response = api.post(url, data=json.dumps(payload), |
||
425 | content_type='application/json') |
||
426 | self.assertEqual(response.status_code, 404, response.data) |
||
427 | |||
428 | def test_delete_interface_metadata(self): |
||
429 | """Test delete_interface_metadata.""" |
||
430 | interface_id = '00:00:00:00:00:00:00:01:1' |
||
431 | dpid = '00:00:00:00:00:00:00:01' |
||
432 | iface_url = '/v3/interfaces/' |
||
433 | mock_switch = get_switch_mock(dpid) |
||
434 | mock_interface = get_interface_mock('s1-eth1', 1, mock_switch) |
||
435 | mock_interface.remove_metadata.side_effect = [True, False] |
||
436 | mock_interface.metadata = {"metada": "A"} |
||
437 | mock_switch.interfaces = {1: mock_interface} |
||
438 | self.napp.controller.switches = {'00:00:00:00:00:00:00:01': |
||
439 | mock_switch} |
||
440 | api = get_test_client(self.napp.controller, self.napp) |
||
441 | |||
442 | key = 'A' |
||
443 | url = f'{self.server_name_url}{iface_url}{interface_id}/metadata/{key}' |
||
444 | response = api.delete(url) |
||
445 | self.assertEqual(response.status_code, 200, response.data) |
||
446 | |||
447 | # fail case switch not found |
||
448 | key = 'A' |
||
449 | interface_id = '00:00:00:00:00:00:00:02:1' |
||
450 | url = f'{self.server_name_url}{iface_url}{interface_id}/metadata/{key}' |
||
451 | response = api.delete(url) |
||
452 | self.assertEqual(response.status_code, 404, response.data) |
||
453 | |||
454 | # fail case interface not found |
||
455 | key = 'A' |
||
456 | interface_id = '00:00:00:00:00:00:00:01:2' |
||
457 | url = f'{self.server_name_url}{iface_url}{interface_id}/metadata/{key}' |
||
458 | response = api.delete(url) |
||
459 | self.assertEqual(response.status_code, 404, response.data) |
||
460 | |||
461 | # fail case metadata not found |
||
462 | key = 'A' |
||
463 | interface_id = '00:00:00:00:00:00:00:01:1' |
||
464 | url = f'{self.server_name_url}{iface_url}{interface_id}/metadata/{key}' |
||
465 | response = api.delete(url) |
||
466 | self.assertEqual(response.status_code, 404, response.data) |
||
467 | |||
468 | def test_enable_link(self): |
||
469 | """Test enable_link.""" |
||
470 | mock_link = MagicMock(Link) |
||
471 | self.napp.links = {'1': mock_link} |
||
472 | api = get_test_client(self.napp.controller, self.napp) |
||
473 | |||
474 | link_id = 1 |
||
475 | url = f'{self.server_name_url}/v3/links/{link_id}/enable' |
||
476 | response = api.post(url) |
||
477 | self.assertEqual(response.status_code, 201, response.data) |
||
478 | self.assertEqual(mock_link.enable.call_count, 1) |
||
479 | |||
480 | # fail case |
||
481 | link_id = 2 |
||
482 | url = f'{self.server_name_url}/v3/links/{link_id}/enable' |
||
483 | response = api.post(url) |
||
484 | self.assertEqual(response.status_code, 404, response.data) |
||
485 | |||
486 | def test_disable_link(self): |
||
487 | """Test disable_link.""" |
||
488 | mock_link = MagicMock(Link) |
||
489 | self.napp.links = {'1': mock_link} |
||
490 | api = get_test_client(self.napp.controller, self.napp) |
||
491 | |||
492 | link_id = 1 |
||
493 | url = f'{self.server_name_url}/v3/links/{link_id}/disable' |
||
494 | response = api.post(url) |
||
495 | self.assertEqual(response.status_code, 201, response.data) |
||
496 | self.assertEqual(mock_link.disable.call_count, 1) |
||
497 | |||
498 | # fail case |
||
499 | link_id = 2 |
||
500 | url = f'{self.server_name_url}/v3/links/{link_id}/disable' |
||
501 | response = api.post(url) |
||
502 | self.assertEqual(response.status_code, 404, response.data) |
||
503 | |||
504 | def test_get_link_metadata(self): |
||
505 | """Test get_link_metadata.""" |
||
506 | mock_link = MagicMock(Link) |
||
507 | mock_link.metadata = "A" |
||
508 | self.napp.links = {'1': mock_link} |
||
509 | msg_success = {"metadata": "A"} |
||
510 | api = get_test_client(self.napp.controller, self.napp) |
||
511 | |||
512 | link_id = 1 |
||
513 | url = f'{self.server_name_url}/v3/links/{link_id}/metadata' |
||
514 | response = api.get(url) |
||
515 | self.assertEqual(response.status_code, 200, response.data) |
||
516 | self.assertEqual(msg_success, json.loads(response.data)) |
||
517 | |||
518 | # fail case |
||
519 | link_id = 2 |
||
520 | url = f'{self.server_name_url}/v3/links/{link_id}/metadata' |
||
521 | response = api.get(url) |
||
522 | self.assertEqual(response.status_code, 404, response.data) |
||
523 | |||
524 | @patch('napps.kytos.topology.main.Main.notify_metadata_changes') |
||
525 | def test_add_link_metadata(self, mock_metadata_changes): |
||
526 | """Test add_link_metadata.""" |
||
527 | mock_link = MagicMock(Link) |
||
528 | mock_link.metadata = "A" |
||
529 | self.napp.links = {'1': mock_link} |
||
530 | payload = {"metadata": "A"} |
||
531 | api = get_test_client(self.napp.controller, self.napp) |
||
532 | |||
533 | link_id = 1 |
||
534 | url = f'{self.server_name_url}/v3/links/{link_id}/metadata' |
||
535 | response = api.post(url, data=json.dumps(payload), |
||
536 | content_type='application/json') |
||
537 | self.assertEqual(response.status_code, 201, response.data) |
||
538 | mock_metadata_changes.assert_called() |
||
539 | |||
540 | # fail case |
||
541 | link_id = 2 |
||
542 | url = f'{self.server_name_url}/v3/links/{link_id}/metadata' |
||
543 | response = api.post(url, data=json.dumps(payload), |
||
544 | content_type='application/json') |
||
545 | self.assertEqual(response.status_code, 404, response.data) |
||
546 | |||
547 | @patch('napps.kytos.topology.main.Main.notify_metadata_changes') |
||
548 | def test_delete_link_metadata(self, mock_metadata_changes): |
||
549 | """Test delete_link_metadata.""" |
||
550 | mock_link = MagicMock(Link) |
||
551 | mock_link.metadata = "A" |
||
552 | mock_link.remove_metadata.side_effect = [True, False] |
||
553 | self.napp.links = {'1': mock_link} |
||
554 | api = get_test_client(self.napp.controller, self.napp) |
||
555 | |||
556 | link_id = 1 |
||
557 | key = 'A' |
||
558 | url = f'{self.server_name_url}/v3/links/{link_id}/metadata/{key}' |
||
559 | response = api.delete(url) |
||
560 | self.assertEqual(response.status_code, 200, response.data) |
||
561 | mock_metadata_changes.assert_called() |
||
562 | |||
563 | # fail case link not found |
||
564 | link_id = 2 |
||
565 | key = 'A' |
||
566 | url = f'{self.server_name_url}/v3/links/{link_id}/metadata/{key}' |
||
567 | response = api.delete(url) |
||
568 | self.assertEqual(response.status_code, 404, response.data) |
||
569 | |||
570 | # fail case metadata not found |
||
571 | link_id = 1 |
||
572 | key = 'A' |
||
573 | url = f'{self.server_name_url}/v3/links/{link_id}/metadata/{key}' |
||
574 | response = api.delete(url) |
||
575 | self.assertEqual(response.status_code, 404, response.data) |
||
576 | |||
577 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
||
578 | @patch('napps.kytos.topology.main.Main.update_instance_metadata') |
||
579 | def test_handle_new_switch(self, *args): |
||
580 | """Test handle_new_switch.""" |
||
581 | (mock_instance_metadata, mock_notify_topology_update) = args |
||
582 | mock_event = MagicMock() |
||
583 | mock_switch = create_autospec(Switch) |
||
584 | mock_event.content['switch'] = mock_switch |
||
585 | self.napp.handle_new_switch(mock_event) |
||
586 | mock_notify_topology_update.assert_called() |
||
587 | mock_instance_metadata.assert_called() |
||
588 | |||
589 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
||
590 | def test_handle_connection_lost(self, mock_notify_topology_update): |
||
591 | """Test handle connection_lost.""" |
||
592 | mock_event = MagicMock() |
||
593 | mock_switch = create_autospec(Switch) |
||
594 | mock_switch.return_value = True |
||
595 | mock_event.content['source'] = mock_switch |
||
596 | self.napp.handle_connection_lost(mock_event) |
||
597 | mock_notify_topology_update.assert_called() |
||
598 | |||
599 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
||
600 | @patch('napps.kytos.topology.main.Main.update_instance_metadata') |
||
601 | def test_handle_interface_up(self, *args): |
||
602 | """Test handle_interface_up.""" |
||
603 | (mock_instance_metadata, mock_notify_topology_update) = args |
||
604 | mock_event = MagicMock() |
||
605 | mock_interface = create_autospec(Interface) |
||
606 | mock_event.content['interface'] = mock_interface |
||
607 | self.napp.handle_interface_up(mock_event) |
||
608 | mock_notify_topology_update.assert_called() |
||
609 | mock_instance_metadata.assert_called() |
||
610 | |||
611 | @patch('napps.kytos.topology.main.Main.handle_interface_up') |
||
612 | def test_handle_interface_created(self, mock_handle_interface_up): |
||
613 | """Test handle interface created.""" |
||
614 | mock_event = MagicMock() |
||
615 | self.napp.handle_interface_created(mock_event) |
||
616 | mock_handle_interface_up.assert_called() |
||
617 | |||
618 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
||
619 | @patch('napps.kytos.topology.main.Main.handle_interface_link_down') |
||
620 | def test_handle_interface_down(self, *args): |
||
621 | """Test handle interface down.""" |
||
622 | (mock_handle_interface_link_down, mock_notify_topology_update) = args |
||
623 | mock_event = MagicMock() |
||
624 | mock_interface = create_autospec(Interface) |
||
625 | mock_event.content['interface'] = mock_interface |
||
626 | self.napp.handle_interface_down(mock_event) |
||
627 | mock_handle_interface_link_down.assert_called() |
||
628 | mock_notify_topology_update.assert_called() |
||
629 | |||
630 | @patch('napps.kytos.topology.main.Main.handle_interface_down') |
||
631 | def test_interface_deleted(self, mock_handle_interface_link_down): |
||
632 | """Test interface deleted.""" |
||
633 | mock_event = MagicMock() |
||
634 | self.napp.handle_interface_deleted(mock_event) |
||
635 | mock_handle_interface_link_down.assert_called() |
||
636 | |||
637 | @patch('napps.kytos.topology.main.Main._get_link_from_interface') |
||
638 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
||
639 | @patch('napps.kytos.topology.main.Main.update_instance_metadata') |
||
640 | @patch('napps.kytos.topology.main.Main.notify_link_status_change') |
||
641 | def test_interface_link_up(self, *args): |
||
642 | """Test interface link_up.""" |
||
643 | (mock_status_change, mock_instance_metadata, mock_topology_update, |
||
644 | mock_link_from_interface) = args |
||
645 | |||
646 | now = time.time() |
||
647 | mock_event = MagicMock() |
||
648 | mock_interface_a = create_autospec(Interface) |
||
649 | mock_interface_a.is_active.return_value = False |
||
650 | mock_interface_b = create_autospec(Interface) |
||
651 | mock_interface_b.is_active.return_value = True |
||
652 | mock_link = create_autospec(Link) |
||
653 | mock_link.get_metadata.return_value = now |
||
654 | mock_link.is_active.side_effect = [False, True] |
||
655 | mock_link.endpoint_a = mock_interface_a |
||
656 | mock_link.endpoint_b = mock_interface_b |
||
657 | mock_link_from_interface.return_value = mock_link |
||
658 | content = {'interface': mock_interface_a} |
||
659 | mock_event.content = content |
||
660 | self.napp.link_up_timer = 1 |
||
661 | self.napp.handle_interface_link_up(mock_event) |
||
662 | mock_topology_update.assert_called() |
||
663 | mock_instance_metadata.assert_called() |
||
664 | mock_status_change.assert_called() |
||
665 | |||
666 | @patch('napps.kytos.topology.main.Main._get_link_from_interface') |
||
667 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
||
668 | @patch('napps.kytos.topology.main.Main.notify_link_status_change') |
||
669 | def test_interface_link_down(self, *args): |
||
670 | """Test interface link down.""" |
||
671 | (mock_status_change, mock_topology_update, |
||
672 | mock_link_from_interface) = args |
||
673 | |||
674 | mock_event = MagicMock() |
||
675 | mock_interface = create_autospec(Interface) |
||
676 | mock_link = create_autospec(Link) |
||
677 | mock_link.is_active.return_value = True |
||
678 | mock_link_from_interface.return_value = mock_link |
||
679 | mock_event.content['interface'] = mock_interface |
||
680 | self.napp.handle_interface_link_down(mock_event) |
||
681 | mock_topology_update.assert_called() |
||
682 | mock_status_change.assert_called() |
||
683 | |||
684 | @patch('napps.kytos.topology.main.Main._get_link_or_create') |
||
685 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
||
686 | def test_add_links(self, *args): |
||
687 | """Test add_links.""" |
||
688 | (mock_notify_topology_update, mock_get_link_or_create) = args |
||
689 | mock_event = MagicMock() |
||
690 | self.napp.add_links(mock_event) |
||
691 | mock_get_link_or_create.assert_called() |
||
692 | mock_notify_topology_update.assert_called() |
||
693 | |||
694 | @patch('napps.kytos.topology.main.Main._get_switches_dict') |
||
695 | @patch('napps.kytos.topology.main.StoreHouse.save_status') |
||
696 | def test_save_status_on_store(self, *args): |
||
697 | """Test save_status_on_storehouse.""" |
||
698 | (mock_save_status, mock_get_switches_dict) = args |
||
699 | self.napp.save_status_on_storehouse() |
||
700 | mock_get_switches_dict.assert_called() |
||
701 | mock_save_status.assert_called() |
||
702 | |||
703 | @patch('napps.kytos.topology.main.KytosEvent') |
||
704 | @patch('kytos.core.buffers.KytosEventBuffer.put') |
||
705 | def test_notify_topology_update(self, *args): |
||
706 | """Test notify_topology_update.""" |
||
707 | (mock_buffers_put, mock_event) = args |
||
708 | self.napp.notify_topology_update() |
||
709 | mock_event.assert_called() |
||
710 | mock_buffers_put.assert_called() |
||
711 | |||
712 | @patch('napps.kytos.topology.main.KytosEvent') |
||
713 | @patch('kytos.core.buffers.KytosEventBuffer.put') |
||
714 | def test_notify_link_status_change(self, *args): |
||
715 | """Test notify link status change.""" |
||
716 | (mock_buffers_put, mock_event) = args |
||
717 | mock_link = create_autospec(Link) |
||
718 | self.napp.notify_link_status_change(mock_link) |
||
719 | mock_event.assert_called() |
||
720 | mock_buffers_put.assert_called() |
||
721 | |||
722 | @patch('napps.kytos.topology.main.KytosEvent') |
||
723 | @patch('kytos.core.buffers.KytosEventBuffer.put') |
||
724 | @patch('napps.kytos.topology.main.isinstance') |
||
725 | def test_notify_metadata_changes(self, *args): |
||
726 | """Test notify metadata changes.""" |
||
727 | (mock_isinstance, mock_buffers_put, mock_event) = args |
||
728 | mock_isinstance.return_value = True |
||
729 | mock_obj = MagicMock() |
||
730 | mock_action = create_autospec(Switch) |
||
731 | self.napp.notify_metadata_changes(mock_obj, mock_action) |
||
732 | mock_event.assert_called() |
||
733 | mock_isinstance.assert_called() |
||
734 | mock_buffers_put.assert_called() |
||
735 | |||
736 | @patch('napps.kytos.topology.main.KytosEvent') |
||
737 | @patch('kytos.core.buffers.KytosEventBuffer.put') |
||
738 | def test_notify_port_created(self, *args): |
||
739 | """Test notify port created.""" |
||
740 | (mock_buffers_put, mock_kytos_event) = args |
||
741 | mock_event = MagicMock() |
||
742 | self.napp.notify_port_created(mock_event) |
||
743 | mock_kytos_event.assert_called() |
||
744 | mock_buffers_put.assert_called() |
||
745 | |||
746 | @patch('napps.kytos.topology.main.KytosEvent') |
||
747 | @patch('kytos.core.buffers.KytosEventBuffer.put') |
||
748 | def test_save_metadata_on_store(self, *args): |
||
749 | """Test test_save_metadata_on_store.""" |
||
750 | (mock_buffers_put, mock_kytos_event) = args |
||
751 | mock_event = MagicMock() |
||
752 | mock_switch = MagicMock() |
||
753 | mock_interface = MagicMock() |
||
754 | mock_link = MagicMock() |
||
755 | self.napp.store_items = {'switches': mock_switch, |
||
756 | 'interfaces': mock_interface, |
||
757 | 'links': mock_link} |
||
758 | # test switches |
||
759 | mock_event.content = {'switch': mock_switch} |
||
760 | self.napp.save_metadata_on_store(mock_event) |
||
761 | mock_kytos_event.assert_called() |
||
762 | mock_buffers_put.assert_called() |
||
763 | |||
764 | # test interfaces |
||
765 | mock_event.content = {'interface': mock_interface} |
||
766 | self.napp.save_metadata_on_store(mock_event) |
||
767 | mock_kytos_event.assert_called() |
||
768 | mock_buffers_put.assert_called() |
||
769 | |||
770 | # test link |
||
771 | mock_event.content = {'link': mock_link} |
||
772 | self.napp.save_metadata_on_store(mock_event) |
||
773 | mock_kytos_event.assert_called() |
||
774 | mock_buffers_put.assert_called() |
||
775 | |||
776 | @patch('napps.kytos.topology.main.KytosEvent') |
||
777 | @patch('kytos.core.buffers.KytosEventBuffer.put') |
||
778 | def test_verify_storehouse(self, *args): |
||
779 | """Test verify_storehouse.""" |
||
780 | (mock_buffers_put, mock_kytos_event) = args |
||
781 | mock_entities = MagicMock() |
||
782 | self.napp.verify_storehouse(mock_entities) |
||
783 | mock_buffers_put.assert_called() |
||
784 | mock_kytos_event.assert_called() |
||
785 | |||
786 | @patch('napps.kytos.topology.main.KytosEvent') |
||
787 | @patch('kytos.core.buffers.KytosEventBuffer.put') |
||
788 | def test_request_retrieve_entities(self, *args): |
||
789 | """Test retrive_entities.""" |
||
790 | (mock_buffers_put, mock_kytos_event) = args |
||
791 | mock_event = MagicMock() |
||
792 | mock_data = MagicMock() |
||
793 | mock_error = MagicMock() |
||
794 | mock_event.content = {"namespace": "test_box"} |
||
795 | self.napp.request_retrieve_entities(mock_event, mock_data, mock_error) |
||
796 | mock_kytos_event.assert_called() |
||
797 | mock_buffers_put.assert_called() |
||
798 | |||
799 | self.napp.request_retrieve_entities(mock_event, None, mock_error) |
||
800 | mock_kytos_event.assert_called() |
||
801 | mock_buffers_put.assert_called() |
||
802 | |||
803 | @patch('napps.kytos.topology.main.Main.notify_link_status_change') |
||
804 | def test_handle_link_maintenance_start(self, status_change_mock): |
||
805 | """Test handle_link_maintenance_start.""" |
||
806 | link1 = MagicMock() |
||
807 | link1.id = 2 |
||
808 | link2 = MagicMock() |
||
809 | link2.id = 3 |
||
810 | link3 = MagicMock() |
||
811 | link3.id = 4 |
||
812 | content = {'links': [link1, link2]} |
||
813 | event = MagicMock() |
||
814 | event.content = content |
||
815 | self.napp.links = {2: link1, 4: link3} |
||
816 | self.napp.handle_link_maintenance_start(event) |
||
817 | status_change_mock.assert_called_once_with(link1) |
||
818 | |||
819 | @patch('napps.kytos.topology.main.Main.notify_link_status_change') |
||
820 | def test_handle_link_maintenance_end(self, status_change_mock): |
||
821 | """Test handle_link_maintenance_end.""" |
||
822 | link1 = MagicMock() |
||
823 | link1.id = 2 |
||
824 | link2 = MagicMock() |
||
825 | link2.id = 3 |
||
826 | link3 = MagicMock() |
||
827 | link3.id = 4 |
||
828 | content = {'links': [link1, link2]} |
||
829 | event = MagicMock() |
||
830 | event.content = content |
||
831 | self.napp.links = {2: link1, 4: link3} |
||
832 | self.napp.handle_link_maintenance_end(event) |
||
833 | status_change_mock.assert_called_once_with(link1) |
||
834 | |||
835 | View Code Duplication | @patch('napps.kytos.topology.main.Main.handle_link_down') |
|
836 | def test_handle_switch_maintenance_start(self, handle_link_down_mock): |
||
837 | """Test handle_switch_maintenance_start.""" |
||
838 | switch1 = MagicMock() |
||
839 | interface1 = MagicMock() |
||
840 | interface1.is_active.return_value = True |
||
841 | interface2 = MagicMock() |
||
842 | interface2.is_active.return_value = False |
||
843 | interface3 = MagicMock() |
||
844 | interface3.is_active.return_value = True |
||
845 | switch1.interfaces = {1: interface1, 2: interface2, 3: interface3} |
||
846 | switch2 = MagicMock() |
||
847 | interface4 = MagicMock() |
||
848 | interface4.is_active.return_value = False |
||
849 | interface5 = MagicMock() |
||
850 | interface5.is_active.return_value = True |
||
851 | switch2.interfaces = {1: interface4, 2: interface5} |
||
852 | content = {'switches': [switch1, switch2]} |
||
853 | event = MagicMock() |
||
854 | event.content = content |
||
855 | self.napp.handle_switch_maintenance_start(event) |
||
856 | self.assertEqual(handle_link_down_mock.call_count, 3) |
||
857 | |||
858 | View Code Duplication | @patch('napps.kytos.topology.main.Main.handle_link_up') |
|
859 | def test_handle_switch_maintenance_end(self, handle_link_up_mock): |
||
860 | """Test handle_switch_maintenance_end.""" |
||
861 | switch1 = MagicMock() |
||
862 | interface1 = MagicMock() |
||
863 | interface1.is_active.return_value = True |
||
864 | interface2 = MagicMock() |
||
865 | interface2.is_active.return_value = False |
||
866 | interface3 = MagicMock() |
||
867 | interface3.is_active.return_value = True |
||
868 | switch1.interfaces = {1: interface1, 2: interface2, 3: interface3} |
||
869 | switch2 = MagicMock() |
||
870 | interface4 = MagicMock() |
||
871 | interface4.is_active.return_value = False |
||
872 | interface5 = MagicMock() |
||
873 | interface5.is_active.return_value = True |
||
874 | switch2.interfaces = {1: interface4, 2: interface5} |
||
875 | content = {'switches': [switch1, switch2]} |
||
876 | event = MagicMock() |
||
877 | event.content = content |
||
878 | self.napp.handle_switch_maintenance_end(event) |
||
879 | self.assertEqual(handle_link_up_mock.call_count, 5) |
||
880 |