Total Complexity | 54 |
Total Lines | 1060 |
Duplicated Lines | 21.04 % |
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, get_link_mock) |
||
13 | |||
14 | |||
15 | from tests.unit.helpers import get_controller_mock, get_napp_urls |
||
16 | from napps.kytos.topology.exceptions import RestoreError |
||
17 | |||
18 | |||
19 | # pylint: disable=too-many-public-methods |
||
20 | class TestMain(TestCase): |
||
21 | """Test the Main class.""" |
||
22 | |||
23 | # pylint: disable=too-many-public-methods, protected-access,C0302 |
||
24 | |||
25 | def setUp(self): |
||
26 | """Execute steps before each tests. |
||
27 | |||
28 | Set the server_name_url_url from kytos/topology |
||
29 | """ |
||
30 | self.server_name_url = 'http://localhost:8181/api/kytos/topology' |
||
31 | |||
32 | patch('kytos.core.helpers.run_on_thread', lambda x: x).start() |
||
33 | from napps.kytos.topology.main import Main |
||
34 | self.addCleanup(patch.stopall) |
||
35 | |||
36 | self.napp = Main(get_controller_mock()) |
||
37 | |||
38 | def test_get_event_listeners(self): |
||
39 | """Verify all event listeners registered.""" |
||
40 | expected_events = ['kytos/core.shutdown', |
||
41 | 'kytos/core.shutdown.kytos/topology', |
||
42 | 'kytos/maintenance.start_link', |
||
43 | 'kytos/maintenance.end_link', |
||
44 | 'kytos/maintenance.start_switch', |
||
45 | 'kytos/maintenance.end_switch', |
||
46 | '.*.network_status.updated', |
||
47 | '.*.interface.is.nni', |
||
48 | '.*.connection.lost', |
||
49 | '.*.switch.interface.created', |
||
50 | '.*.switch.interface.deleted', |
||
51 | '.*.switch.interface.link_down', |
||
52 | '.*.switch.interface.link_up', |
||
53 | '.*.switch.(new|reconnected)', |
||
54 | '.*.switch.port.created', |
||
55 | 'kytos/topology.*.metadata.*'] |
||
56 | actual_events = self.napp.listeners() |
||
57 | self.assertCountEqual(expected_events, actual_events) |
||
58 | |||
59 | View Code Duplication | def test_verify_api_urls(self): |
|
|
|||
60 | """Verify all APIs registered.""" |
||
61 | expected_urls = [ |
||
62 | ({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/interfaces'), |
||
63 | ({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/switches'), |
||
64 | ({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/links'), |
||
65 | ({}, {'GET', 'OPTIONS', 'HEAD'}, '/api/kytos/topology/v3/'), |
||
66 | ({'dpid': '[dpid]'}, {'POST', 'OPTIONS'}, |
||
67 | '/api/kytos/topology/v3/interfaces/switch/<dpid>/disable'), |
||
68 | ({'dpid': '[dpid]'}, {'POST', 'OPTIONS'}, |
||
69 | '/api/kytos/topology/v3/interfaces/switch/<dpid>/enable'), |
||
70 | ({'key': '[key]', 'interface_id': '[interface_id]'}, |
||
71 | {'OPTIONS', 'DELETE'}, |
||
72 | '/api/kytos/topology/v3/interfaces/<interface_id>/metadata/<key>'), |
||
73 | ({'interface_id': '[interface_id]'}, {'POST', 'OPTIONS'}, |
||
74 | '/api/kytos/topology/v3/interfaces/<interface_id>/metadata'), |
||
75 | ({'interface_id': '[interface_id]'}, {'GET', 'OPTIONS', 'HEAD'}, |
||
76 | '/api/kytos/topology/v3/interfaces/<interface_id>/metadata'), |
||
77 | ({'interface_disable_id': '[interface_disable_id]'}, |
||
78 | {'POST', 'OPTIONS'}, |
||
79 | '/api/kytos/topology/v3/interfaces/<interface_disable_id>/disable'), |
||
80 | ({'interface_enable_id': '[interface_enable_id]'}, |
||
81 | {'POST', 'OPTIONS'}, |
||
82 | '/api/kytos/topology/v3/interfaces/<interface_enable_id>/enable'), |
||
83 | ({'dpid': '[dpid]', 'key': '[key]'}, {'OPTIONS', 'DELETE'}, |
||
84 | '/api/kytos/topology/v3/switches/<dpid>/metadata/<key>'), |
||
85 | ({'dpid': '[dpid]'}, {'POST', 'OPTIONS'}, |
||
86 | '/api/kytos/topology/v3/switches/<dpid>/metadata'), |
||
87 | ({'dpid': '[dpid]'}, {'GET', 'OPTIONS', 'HEAD'}, |
||
88 | '/api/kytos/topology/v3/switches/<dpid>/metadata'), |
||
89 | ({'dpid': '[dpid]'}, {'POST', 'OPTIONS'}, |
||
90 | '/api/kytos/topology/v3/switches/<dpid>/disable'), |
||
91 | ({'dpid': '[dpid]'}, {'POST', 'OPTIONS'}, |
||
92 | '/api/kytos/topology/v3/switches/<dpid>/enable'), |
||
93 | ({'link_id': '[link_id]', 'key': '[key]'}, {'OPTIONS', 'DELETE'}, |
||
94 | '/api/kytos/topology/v3/links/<link_id>/metadata/<key>'), |
||
95 | ({'link_id': '[link_id]'}, {'POST', 'OPTIONS'}, |
||
96 | '/api/kytos/topology/v3/links/<link_id>/metadata'), |
||
97 | ({'link_id': '[link_id]'}, {'GET', 'OPTIONS', 'HEAD'}, |
||
98 | '/api/kytos/topology/v3/links/<link_id>/metadata'), |
||
99 | ({'link_id': '[link_id]'}, {'POST', 'OPTIONS'}, |
||
100 | '/api/kytos/topology/v3/links/<link_id>/disable'), |
||
101 | ({'link_id': '[link_id]'}, {'POST', 'OPTIONS'}, |
||
102 | '/api/kytos/topology/v3/links/<link_id>/enable')] |
||
103 | |||
104 | urls = get_napp_urls(self.napp) |
||
105 | self.assertEqual(expected_urls, urls) |
||
106 | |||
107 | def test_get_link_or_create(self): |
||
108 | """Test _get_link_or_create.""" |
||
109 | dpid_a = "00:00:00:00:00:00:00:01" |
||
110 | dpid_b = "00:00:00:00:00:00:00:02" |
||
111 | mock_switch_a = get_switch_mock(dpid_a, 0x04) |
||
112 | mock_switch_b = get_switch_mock(dpid_b, 0x04) |
||
113 | mock_interface_a = get_interface_mock('s1-eth1', 1, mock_switch_a) |
||
114 | mock_interface_b = get_interface_mock('s2-eth1', 1, mock_switch_b) |
||
115 | mock_interface_a.id = dpid_a |
||
116 | mock_interface_b.id = dpid_b |
||
117 | |||
118 | link = self.napp._get_link_or_create(mock_interface_a, |
||
119 | mock_interface_b) |
||
120 | self.assertEqual(link.endpoint_a.id, dpid_a) |
||
121 | self.assertEqual(link.endpoint_b.id, dpid_b) |
||
122 | |||
123 | def test_get_link_from_interface(self): |
||
124 | """Test _get_link_from_interface.""" |
||
125 | mock_switch_a = get_switch_mock("00:00:00:00:00:00:00:01", 0x04) |
||
126 | mock_switch_b = get_switch_mock("00:00:00:00:00:00:00:02", 0x04) |
||
127 | mock_interface_a = get_interface_mock('s1-eth1', 1, mock_switch_a) |
||
128 | mock_interface_b = get_interface_mock('s2-eth1', 1, mock_switch_b) |
||
129 | mock_interface_c = get_interface_mock('s2-eth1', 2, mock_switch_b) |
||
130 | mock_link = get_link_mock(mock_interface_a, mock_interface_b) |
||
131 | self.napp.links = {'0e2b5d7bc858b9f38db11b69': mock_link} |
||
132 | response = self.napp._get_link_from_interface(mock_interface_a) |
||
133 | self.assertEqual(response, mock_link) |
||
134 | |||
135 | response = self.napp._get_link_from_interface(mock_interface_c) |
||
136 | self.assertEqual(response, None) |
||
137 | |||
138 | def test_get_topology(self): |
||
139 | """Test get_topology.""" |
||
140 | dpid_a = "00:00:00:00:00:00:00:01" |
||
141 | dpid_b = "00:00:00:00:00:00:00:02" |
||
142 | expected = { |
||
143 | "topology": { |
||
144 | "switches": { |
||
145 | "00:00:00:00:00:00:00:01": { |
||
146 | "metadata": { |
||
147 | "lat": "0.0", |
||
148 | "lng": "-30.0" |
||
149 | } |
||
150 | }, |
||
151 | "00:00:00:00:00:00:00:02": { |
||
152 | "metadata": { |
||
153 | "lat": "0.0", |
||
154 | "lng": "-30.0" |
||
155 | } |
||
156 | } |
||
157 | }, |
||
158 | "links": { |
||
159 | "cf0f4071be4": { |
||
160 | "id": "cf0f4071be4" |
||
161 | } |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | |||
166 | mock_switch_a = get_switch_mock(dpid_a, 0x04) |
||
167 | mock_switch_b = get_switch_mock(dpid_b, 0x04) |
||
168 | mock_interface_a = get_interface_mock('s1-eth1', 1, mock_switch_a) |
||
169 | mock_interface_b = get_interface_mock('s2-eth1', 1, mock_switch_b) |
||
170 | |||
171 | mock_link = get_link_mock(mock_interface_a, mock_interface_b) |
||
172 | mock_link.id = 'cf0f4071be4' |
||
173 | mock_switch_a.id = dpid_a |
||
174 | mock_switch_a.as_dict.return_value = {'metadata': {'lat': '0.0', |
||
175 | 'lng': '-30.0'}} |
||
176 | mock_switch_b.id = dpid_b |
||
177 | mock_switch_b.as_dict.return_value = {'metadata': {'lat': '0.0', |
||
178 | 'lng': '-30.0'}} |
||
179 | |||
180 | self.napp.controller.switches = {dpid_a: mock_switch_a, |
||
181 | dpid_b: mock_switch_b} |
||
182 | |||
183 | self.napp.links = {"cf0f4071be4": mock_link} |
||
184 | mock_link.as_dict.return_value = {"id": "cf0f4071be4"} |
||
185 | api = get_test_client(self.napp.controller, self.napp) |
||
186 | |||
187 | url = f'{self.server_name_url}/v3/' |
||
188 | response = api.get(url) |
||
189 | self.assertEqual(response.status_code, 200) |
||
190 | self.assertEqual(json.loads(response.data), expected) |
||
191 | |||
192 | @patch('napps.kytos.topology.main.StoreHouse.get_data') |
||
193 | def test_load_network_status(self, mock_storehouse_get_data): |
||
194 | """Test _load_network_status.""" |
||
195 | status = { |
||
196 | 'network_status': { |
||
197 | 'id': 'network_status', |
||
198 | 'links': { |
||
199 | '4d42dc08522': { |
||
200 | 'enabled': True, |
||
201 | 'endpoint_a': { |
||
202 | 'switch': '00:00:00:00:00:00:00:01', |
||
203 | 'id': '00:00:00:00:00:00:00:00:1' |
||
204 | }, |
||
205 | 'endpoint_b': { |
||
206 | 'switch': '00:00:00:00:00:00:00:01', |
||
207 | 'id': '00:00:00:00:00:00:00:00:2' |
||
208 | } |
||
209 | } |
||
210 | }, |
||
211 | 'switches': { |
||
212 | '00:00:00:00:00:00:00:01': { |
||
213 | 'dpid': '00:00:00:00:00:00:00:01', |
||
214 | 'enabled': True, |
||
215 | 'id': '00:00:00:00:00:00:00:01', |
||
216 | 'interfaces': { |
||
217 | '00:00:00:00:00:00:00:01:1': { |
||
218 | 'enabled': True, |
||
219 | 'lldp': True, |
||
220 | 'id': '00:00:00:00:00:00:00:00:1', |
||
221 | } |
||
222 | } |
||
223 | } |
||
224 | } |
||
225 | } |
||
226 | } |
||
227 | switches_expected = {'00:00:00:00:00:00:00:01': True} |
||
228 | interfaces_expected = {'00:00:00:00:00:00:00:01:1': (True, True)} |
||
229 | mock_storehouse_get_data.return_value = status |
||
230 | self.napp._load_network_status() |
||
231 | self.assertDictEqual(switches_expected, self.napp.switches_state) |
||
232 | self.assertDictEqual(interfaces_expected, self.napp.interfaces_state) |
||
233 | self.assertDictEqual(status['network_status']['links'], |
||
234 | self.napp.links_state) |
||
235 | |||
236 | # pylint: disable=too-many-locals |
||
237 | def test_restore_network_status(self): |
||
238 | """Test restore_network_status.""" |
||
239 | dpid = '00:00:00:00:00:00:00:01' |
||
240 | mock_switch = get_switch_mock(dpid) |
||
241 | mock_switch.id = dpid |
||
242 | mock_interface = get_interface_mock('s1-eth1', 1, mock_switch) |
||
243 | mock_switch.interfaces = {1: mock_interface} |
||
244 | self.napp.controller.switches = {dpid: mock_switch} |
||
245 | self.napp.switches_state = {dpid: True} |
||
246 | self.napp.interfaces_state = {'00:00:00:00:00:00:00:01:1': (True, |
||
247 | True)} |
||
248 | |||
249 | # enable |
||
250 | self.napp.restore_network_status(mock_switch) |
||
251 | self.assertEqual(mock_switch.enable.call_count, 1) |
||
252 | self.assertEqual(mock_interface.enable.call_count, 1) |
||
253 | self.assertEqual(mock_interface.lldp, True) |
||
254 | |||
255 | # disable |
||
256 | self.napp.switches_state = {dpid: False} |
||
257 | self.napp.interfaces_state = {'00:00:00:00:00:00:00:01:1': (False, |
||
258 | False)} |
||
259 | self.napp.restore_network_status(mock_switch) |
||
260 | self.assertEqual(mock_switch.disable.call_count, 1) |
||
261 | self.assertEqual(mock_interface.disable.call_count, 1) |
||
262 | self.assertEqual(mock_interface.lldp, False) |
||
263 | |||
264 | def test_restore_links(self): |
||
265 | """Test restore_link.""" |
||
266 | dpid = '00:00:00:00:00:00:00:01' |
||
267 | dpid_b = '00:00:00:00:00:00:00:02' |
||
268 | link_id = '4d42dc08522' |
||
269 | mock_switch_a = get_switch_mock(dpid) |
||
270 | mock_switch_b = get_switch_mock(dpid_b) |
||
271 | mock_interface_a_1 = get_interface_mock('s1-eth1', 1, mock_switch_a) |
||
272 | mock_interface_b_1 = get_interface_mock('s2-eth1', 1, mock_switch_b) |
||
273 | mock_link = get_link_mock(mock_interface_a_1, mock_interface_b_1) |
||
274 | mock_link.id = link_id |
||
275 | self.napp.links = {link_id: mock_link} |
||
276 | self.napp.links_state = {link_id: {'enabled': True}} |
||
277 | # enable link |
||
278 | self.napp.restore_network_status(mock_link) |
||
279 | self.assertEqual(mock_link.enable.call_count, 1) |
||
280 | |||
281 | # disable link |
||
282 | self.napp.links_state = {link_id: {"enabled": False}} |
||
283 | self.napp._verified_links = [] |
||
284 | self.napp.restore_network_status(mock_link) |
||
285 | self.assertEqual(mock_link.disable.call_count, 1) |
||
286 | |||
287 | def test_fail_restore_link(self): |
||
288 | """Test fail restore_link.""" |
||
289 | dpid = '00:00:00:00:00:00:00:01' |
||
290 | dpid_b = '00:00:00:00:00:00:00:02' |
||
291 | link_id = '4d42dc08522' |
||
292 | link_id_fail = '4cd52' |
||
293 | mock_switch_a = get_switch_mock(dpid) |
||
294 | mock_switch_b = get_switch_mock(dpid_b) |
||
295 | mock_interface_a_1 = get_interface_mock('s1-eth1', 1, mock_switch_a) |
||
296 | mock_interface_b_1 = get_interface_mock('s2-eth1', 1, mock_switch_b) |
||
297 | mock_link = get_link_mock(mock_interface_a_1, mock_interface_b_1) |
||
298 | mock_link.id = link_id |
||
299 | self.napp.links = {link_id: mock_link} |
||
300 | self.napp.links_state = {link_id: {"enabled": True}} |
||
301 | with self.assertRaises(RestoreError): |
||
302 | self.napp._restore_link(link_id_fail) |
||
303 | |||
304 | self.napp.links_state = {link_id_fail: {"enabled": True}} |
||
305 | with self.assertRaises(RestoreError): |
||
306 | self.napp._restore_link(link_id_fail) |
||
307 | |||
308 | def test_fail_restore_switch(self): |
||
309 | """Test fail restore_switch.""" |
||
310 | dpid = '00:00:00:00:00:00:00:01' |
||
311 | dpid_fail = '00:00:00:00:00:00:00:06' |
||
312 | mock_switch = get_switch_mock(dpid) |
||
313 | mock_switch.id = dpid |
||
314 | mock_interface = get_interface_mock('s1-eth1', 1, mock_switch) |
||
315 | mock_switch.interfaces = {1: mock_interface} |
||
316 | self.napp.controller.switche = {dpid: mock_switch} |
||
317 | self.napp.switches_state = {dpid: True} |
||
318 | self.napp.interfaces_state = {'00:00:00:00:00:00:00:01:1': (True, |
||
319 | True)} |
||
320 | |||
321 | with self.assertRaises(RestoreError): |
||
322 | self.napp._restore_switch(dpid_fail) |
||
323 | |||
324 | self.napp.switches_state = {dpid_fail: True} |
||
325 | with self.assertRaises(RestoreError): |
||
326 | self.napp._restore_switch(dpid_fail) |
||
327 | |||
328 | View Code Duplication | @patch('napps.kytos.topology.main.Main.save_status_on_storehouse') |
|
329 | def test_enable_switch(self, mock_save_status): |
||
330 | """Test enable_switch.""" |
||
331 | dpid = "00:00:00:00:00:00:00:01" |
||
332 | mock_switch = get_switch_mock(dpid) |
||
333 | self.napp.controller.switches = {dpid: mock_switch} |
||
334 | api = get_test_client(self.napp.controller, self.napp) |
||
335 | |||
336 | url = f'{self.server_name_url}/v3/switches/{dpid}/enable' |
||
337 | response = api.post(url) |
||
338 | self.assertEqual(response.status_code, 201, response.data) |
||
339 | self.assertEqual(mock_switch.enable.call_count, 1) |
||
340 | mock_save_status.assert_called() |
||
341 | |||
342 | # fail case |
||
343 | mock_switch.enable.call_count = 0 |
||
344 | dpid = "00:00:00:00:00:00:00:02" |
||
345 | url = f'{self.server_name_url}/v3/switches/{dpid}/enable' |
||
346 | response = api.post(url) |
||
347 | self.assertEqual(response.status_code, 404, response.data) |
||
348 | self.assertEqual(mock_switch.enable.call_count, 0) |
||
349 | |||
350 | View Code Duplication | @patch('napps.kytos.topology.main.Main.save_status_on_storehouse') |
|
351 | def test_disable_switch(self, mock_save_status): |
||
352 | """Test disable_switch.""" |
||
353 | dpid = "00:00:00:00:00:00:00:01" |
||
354 | mock_switch = get_switch_mock(dpid) |
||
355 | self.napp.controller.switches = {dpid: mock_switch} |
||
356 | api = get_test_client(self.napp.controller, self.napp) |
||
357 | |||
358 | url = f'{self.server_name_url}/v3/switches/{dpid}/disable' |
||
359 | response = api.post(url) |
||
360 | self.assertEqual(response.status_code, 201, response.data) |
||
361 | self.assertEqual(mock_switch.disable.call_count, 1) |
||
362 | mock_save_status.assert_called() |
||
363 | |||
364 | # fail case |
||
365 | mock_switch.disable.call_count = 0 |
||
366 | dpid = "00:00:00:00:00:00:00:02" |
||
367 | url = f'{self.server_name_url}/v3/switches/{dpid}/disable' |
||
368 | response = api.post(url) |
||
369 | self.assertEqual(response.status_code, 404, response.data) |
||
370 | self.assertEqual(mock_switch.disable.call_count, 0) |
||
371 | |||
372 | def test_get_switch_metadata(self): |
||
373 | """Test get_switch_metadata.""" |
||
374 | dpid = "00:00:00:00:00:00:00:01" |
||
375 | mock_switch = get_switch_mock(dpid) |
||
376 | mock_switch.metadata = "A" |
||
377 | self.napp.controller.switches = {dpid: mock_switch} |
||
378 | api = get_test_client(self.napp.controller, self.napp) |
||
379 | |||
380 | url = f'{self.server_name_url}/v3/switches/{dpid}/metadata' |
||
381 | response = api.get(url) |
||
382 | self.assertEqual(response.status_code, 200, response.data) |
||
383 | |||
384 | # fail case |
||
385 | dpid = "00:00:00:00:00:00:00:02" |
||
386 | url = f'{self.server_name_url}/v3/switches/{dpid}/metadata' |
||
387 | response = api.get(url) |
||
388 | self.assertEqual(response.status_code, 404, response.data) |
||
389 | |||
390 | @patch('napps.kytos.topology.main.Main.notify_metadata_changes') |
||
391 | def test_add_switch_metadata(self, mock_metadata_changes): |
||
392 | """Test add_switch_metadata.""" |
||
393 | dpid = "00:00:00:00:00:00:00:01" |
||
394 | mock_switch = get_switch_mock(dpid) |
||
395 | self.napp.controller.switches = {dpid: mock_switch} |
||
396 | api = get_test_client(self.napp.controller, self.napp) |
||
397 | payload = {"data": "A"} |
||
398 | |||
399 | url = f'{self.server_name_url}/v3/switches/{dpid}/metadata' |
||
400 | response = api.post(url, data=json.dumps(payload), |
||
401 | content_type='application/json') |
||
402 | self.assertEqual(response.status_code, 201, response.data) |
||
403 | mock_metadata_changes.assert_called() |
||
404 | |||
405 | # fail case |
||
406 | dpid = "00:00:00:00:00:00:00:02" |
||
407 | url = f'{self.server_name_url}/v3/switches/{dpid}/metadata' |
||
408 | response = api.post(url, data=json.dumps(payload), |
||
409 | content_type='application/json') |
||
410 | self.assertEqual(response.status_code, 404, response.data) |
||
411 | |||
412 | @patch('napps.kytos.topology.main.Main.notify_metadata_changes') |
||
413 | def test_delete_switch_metadata(self, mock_metadata_changes): |
||
414 | """Test delete_switch_metadata.""" |
||
415 | dpid = "00:00:00:00:00:00:00:01" |
||
416 | mock_switch = get_switch_mock(dpid) |
||
417 | self.napp.controller.switches = {dpid: mock_switch} |
||
418 | api = get_test_client(self.napp.controller, self.napp) |
||
419 | |||
420 | key = "A" |
||
421 | url = f'{self.server_name_url}/v3/switches/{dpid}/metadata/{key}' |
||
422 | response = api.delete(url) |
||
423 | mock_metadata_changes.assert_called() |
||
424 | self.assertEqual(response.status_code, 200, response.data) |
||
425 | |||
426 | # fail case |
||
427 | key = "A" |
||
428 | dpid = "00:00:00:00:00:00:00:02" |
||
429 | url = f'{self.server_name_url}/v3/switches/{dpid}/metadata/{key}' |
||
430 | response = api.delete(url) |
||
431 | mock_metadata_changes.assert_called() |
||
432 | self.assertEqual(response.status_code, 404, response.data) |
||
433 | |||
434 | View Code Duplication | @patch('napps.kytos.topology.main.Main.save_status_on_storehouse') |
|
435 | def test_enable_interfaces(self, mock_save_status): |
||
436 | """Test enable_interfaces.""" |
||
437 | dpid = '00:00:00:00:00:00:00:01' |
||
438 | mock_switch = get_switch_mock(dpid) |
||
439 | mock_interface_1 = get_interface_mock('s1-eth1', 1, mock_switch) |
||
440 | mock_interface_2 = get_interface_mock('s1-eth2', 2, mock_switch) |
||
441 | mock_switch.interfaces = {1: mock_interface_1, 2: mock_interface_2} |
||
442 | self.napp.controller.switches = {dpid: mock_switch} |
||
443 | api = get_test_client(self.napp.controller, self.napp) |
||
444 | |||
445 | interface_id = '00:00:00:00:00:00:00:01:1' |
||
446 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/enable' |
||
447 | response = api.post(url) |
||
448 | self.assertEqual(response.status_code, 200, response.data) |
||
449 | self.assertEqual(mock_interface_1.enable.call_count, 1) |
||
450 | self.assertEqual(mock_interface_2.enable.call_count, 0) |
||
451 | mock_save_status.assert_called() |
||
452 | |||
453 | mock_interface_1.enable.call_count = 0 |
||
454 | mock_interface_2.enable.call_count = 0 |
||
455 | url = f'{self.server_name_url}/v3/interfaces/switch/{dpid}/enable' |
||
456 | response = api.post(url) |
||
457 | self.assertEqual(response.status_code, 200, response.data) |
||
458 | self.assertEqual(mock_interface_1.enable.call_count, 1) |
||
459 | self.assertEqual(mock_interface_2.enable.call_count, 1) |
||
460 | |||
461 | # test interface not found |
||
462 | interface_id = '00:00:00:00:00:00:00:01:3' |
||
463 | mock_interface_1.enable.call_count = 0 |
||
464 | mock_interface_2.enable.call_count = 0 |
||
465 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/enable' |
||
466 | response = api.post(url) |
||
467 | self.assertEqual(response.status_code, 409, response.data) |
||
468 | self.assertEqual(mock_interface_1.enable.call_count, 0) |
||
469 | self.assertEqual(mock_interface_2.enable.call_count, 0) |
||
470 | |||
471 | # test switch not found |
||
472 | dpid = '00:00:00:00:00:00:00:02' |
||
473 | url = f'{self.server_name_url}/v3/interfaces/switch/{dpid}/enable' |
||
474 | response = api.post(url) |
||
475 | self.assertEqual(response.status_code, 404, response.data) |
||
476 | self.assertEqual(mock_interface_1.enable.call_count, 0) |
||
477 | self.assertEqual(mock_interface_2.enable.call_count, 0) |
||
478 | |||
479 | View Code Duplication | @patch('napps.kytos.topology.main.Main.save_status_on_storehouse') |
|
480 | def test_disable_interfaces(self, mock_save_status): |
||
481 | """Test disable_interfaces.""" |
||
482 | interface_id = '00:00:00:00:00:00:00:01:1' |
||
483 | dpid = '00:00:00:00:00:00:00:01' |
||
484 | mock_switch = get_switch_mock(dpid) |
||
485 | mock_interface_1 = get_interface_mock('s1-eth1', 1, mock_switch) |
||
486 | mock_interface_2 = get_interface_mock('s1-eth2', 2, mock_switch) |
||
487 | mock_switch.interfaces = {1: mock_interface_1, 2: mock_interface_2} |
||
488 | self.napp.controller.switches = {dpid: mock_switch} |
||
489 | api = get_test_client(self.napp.controller, self.napp) |
||
490 | |||
491 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/disable' |
||
492 | response = api.post(url) |
||
493 | self.assertEqual(response.status_code, 200, response.data) |
||
494 | self.assertEqual(mock_interface_1.disable.call_count, 1) |
||
495 | self.assertEqual(mock_interface_2.disable.call_count, 0) |
||
496 | mock_save_status.assert_called() |
||
497 | |||
498 | mock_interface_1.disable.call_count = 0 |
||
499 | mock_interface_2.disable.call_count = 0 |
||
500 | url = f'{self.server_name_url}/v3/interfaces/switch/{dpid}/disable' |
||
501 | response = api.post(url) |
||
502 | self.assertEqual(response.status_code, 200, response.data) |
||
503 | self.assertEqual(mock_interface_1.disable.call_count, 1) |
||
504 | self.assertEqual(mock_interface_2.disable.call_count, 1) |
||
505 | |||
506 | # test interface not found |
||
507 | interface_id = '00:00:00:00:00:00:00:01:3' |
||
508 | mock_interface_1.disable.call_count = 0 |
||
509 | mock_interface_2.disable.call_count = 0 |
||
510 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/disable' |
||
511 | response = api.post(url) |
||
512 | self.assertEqual(response.status_code, 409, response.data) |
||
513 | self.assertEqual(mock_interface_1.disable.call_count, 0) |
||
514 | self.assertEqual(mock_interface_2.disable.call_count, 0) |
||
515 | |||
516 | # test switch not found |
||
517 | dpid = '00:00:00:00:00:00:00:02' |
||
518 | url = f'{self.server_name_url}/v3/interfaces/switch/{dpid}/disable' |
||
519 | response = api.post(url) |
||
520 | self.assertEqual(response.status_code, 404, response.data) |
||
521 | self.assertEqual(mock_interface_1.disable.call_count, 0) |
||
522 | self.assertEqual(mock_interface_2.disable.call_count, 0) |
||
523 | |||
524 | def test_get_interface_metadata(self): |
||
525 | """Test get_interface_metada.""" |
||
526 | interface_id = '00:00:00:00:00:00:00:01:1' |
||
527 | dpid = '00:00:00:00:00:00:00:01' |
||
528 | mock_switch = get_switch_mock(dpid) |
||
529 | mock_interface = get_interface_mock('s1-eth1', 1, mock_switch) |
||
530 | mock_interface.metadata = {"metada": "A"} |
||
531 | mock_switch.interfaces = {1: mock_interface} |
||
532 | self.napp.controller.switches = {dpid: mock_switch} |
||
533 | api = get_test_client(self.napp.controller, self.napp) |
||
534 | |||
535 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/metadata' |
||
536 | response = api.get(url) |
||
537 | self.assertEqual(response.status_code, 200, response.data) |
||
538 | |||
539 | # fail case switch not found |
||
540 | interface_id = '00:00:00:00:00:00:00:02:1' |
||
541 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/metadata' |
||
542 | response = api.get(url) |
||
543 | self.assertEqual(response.status_code, 404, response.data) |
||
544 | |||
545 | # fail case interface not found |
||
546 | interface_id = '00:00:00:00:00:00:00:01:2' |
||
547 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/metadata' |
||
548 | response = api.get(url) |
||
549 | self.assertEqual(response.status_code, 404, response.data) |
||
550 | |||
551 | @patch('napps.kytos.topology.main.Main.notify_metadata_changes') |
||
552 | def test_add_interface_metadata(self, mock_metadata_changes): |
||
553 | """Test add_interface_metadata.""" |
||
554 | interface_id = '00:00:00:00:00:00:00:01:1' |
||
555 | dpid = '00:00:00:00:00:00:00:01' |
||
556 | mock_switch = get_switch_mock(dpid) |
||
557 | mock_interface = get_interface_mock('s1-eth1', 1, mock_switch) |
||
558 | mock_interface.metadata = {"metada": "A"} |
||
559 | mock_switch.interfaces = {1: mock_interface} |
||
560 | self.napp.controller.switches = {dpid: mock_switch} |
||
561 | api = get_test_client(self.napp.controller, self.napp) |
||
562 | |||
563 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/metadata' |
||
564 | payload = {"metada": "A"} |
||
565 | response = api.post(url, data=json.dumps(payload), |
||
566 | content_type='application/json') |
||
567 | self.assertEqual(response.status_code, 201, response.data) |
||
568 | mock_metadata_changes.assert_called() |
||
569 | |||
570 | # fail case switch not found |
||
571 | interface_id = '00:00:00:00:00:00:00:02:1' |
||
572 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/metadata' |
||
573 | response = api.post(url, data=json.dumps(payload), |
||
574 | content_type='application/json') |
||
575 | self.assertEqual(response.status_code, 404, response.data) |
||
576 | |||
577 | # fail case interface not found |
||
578 | interface_id = '00:00:00:00:00:00:00:01:2' |
||
579 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/metadata' |
||
580 | response = api.post(url, data=json.dumps(payload), |
||
581 | content_type='application/json') |
||
582 | self.assertEqual(response.status_code, 404, response.data) |
||
583 | |||
584 | def test_delete_interface_metadata(self): |
||
585 | """Test delete_interface_metadata.""" |
||
586 | interface_id = '00:00:00:00:00:00:00:01:1' |
||
587 | dpid = '00:00:00:00:00:00:00:01' |
||
588 | iface_url = '/v3/interfaces/' |
||
589 | mock_switch = get_switch_mock(dpid) |
||
590 | mock_interface = get_interface_mock('s1-eth1', 1, mock_switch) |
||
591 | mock_interface.remove_metadata.side_effect = [True, False] |
||
592 | mock_interface.metadata = {"metada": "A"} |
||
593 | mock_switch.interfaces = {1: mock_interface} |
||
594 | self.napp.controller.switches = {'00:00:00:00:00:00:00:01': |
||
595 | mock_switch} |
||
596 | api = get_test_client(self.napp.controller, self.napp) |
||
597 | |||
598 | key = 'A' |
||
599 | url = f'{self.server_name_url}{iface_url}{interface_id}/metadata/{key}' |
||
600 | response = api.delete(url) |
||
601 | self.assertEqual(response.status_code, 200, response.data) |
||
602 | |||
603 | # fail case switch not found |
||
604 | key = 'A' |
||
605 | interface_id = '00:00:00:00:00:00:00:02:1' |
||
606 | url = f'{self.server_name_url}{iface_url}{interface_id}/metadata/{key}' |
||
607 | response = api.delete(url) |
||
608 | self.assertEqual(response.status_code, 404, response.data) |
||
609 | |||
610 | # fail case interface not found |
||
611 | key = 'A' |
||
612 | interface_id = '00:00:00:00:00:00:00:01:2' |
||
613 | url = f'{self.server_name_url}{iface_url}{interface_id}/metadata/{key}' |
||
614 | response = api.delete(url) |
||
615 | self.assertEqual(response.status_code, 404, response.data) |
||
616 | |||
617 | # fail case metadata not found |
||
618 | key = 'A' |
||
619 | interface_id = '00:00:00:00:00:00:00:01:1' |
||
620 | url = f'{self.server_name_url}{iface_url}{interface_id}/metadata/{key}' |
||
621 | response = api.delete(url) |
||
622 | self.assertEqual(response.status_code, 404, response.data) |
||
623 | |||
624 | @patch('napps.kytos.topology.main.Main.save_status_on_storehouse') |
||
625 | def test_enable_link(self, mock_save_status): |
||
626 | """Test enable_link.""" |
||
627 | mock_link = MagicMock(Link) |
||
628 | self.napp.links = {'1': mock_link} |
||
629 | api = get_test_client(self.napp.controller, self.napp) |
||
630 | mock_save_status.return_value = True |
||
631 | |||
632 | link_id = 1 |
||
633 | url = f'{self.server_name_url}/v3/links/{link_id}/enable' |
||
634 | response = api.post(url) |
||
635 | self.assertEqual(response.status_code, 201, response.data) |
||
636 | self.assertEqual(mock_link.enable.call_count, 1) |
||
637 | |||
638 | # fail case |
||
639 | link_id = 2 |
||
640 | url = f'{self.server_name_url}/v3/links/{link_id}/enable' |
||
641 | response = api.post(url) |
||
642 | self.assertEqual(response.status_code, 404, response.data) |
||
643 | |||
644 | @patch('napps.kytos.topology.main.Main.save_status_on_storehouse') |
||
645 | def test_disable_link(self, mock_save_status): |
||
646 | """Test disable_link.""" |
||
647 | mock_link = MagicMock(Link) |
||
648 | self.napp.links = {'1': mock_link} |
||
649 | api = get_test_client(self.napp.controller, self.napp) |
||
650 | mock_save_status.return_value = True |
||
651 | |||
652 | link_id = 1 |
||
653 | url = f'{self.server_name_url}/v3/links/{link_id}/disable' |
||
654 | response = api.post(url) |
||
655 | self.assertEqual(response.status_code, 201, response.data) |
||
656 | self.assertEqual(mock_link.disable.call_count, 1) |
||
657 | |||
658 | # fail case |
||
659 | link_id = 2 |
||
660 | url = f'{self.server_name_url}/v3/links/{link_id}/disable' |
||
661 | response = api.post(url) |
||
662 | self.assertEqual(response.status_code, 404, response.data) |
||
663 | |||
664 | def test_get_link_metadata(self): |
||
665 | """Test get_link_metadata.""" |
||
666 | mock_link = MagicMock(Link) |
||
667 | mock_link.metadata = "A" |
||
668 | self.napp.links = {'1': mock_link} |
||
669 | msg_success = {"metadata": "A"} |
||
670 | api = get_test_client(self.napp.controller, self.napp) |
||
671 | |||
672 | link_id = 1 |
||
673 | url = f'{self.server_name_url}/v3/links/{link_id}/metadata' |
||
674 | response = api.get(url) |
||
675 | self.assertEqual(response.status_code, 200, response.data) |
||
676 | self.assertEqual(msg_success, json.loads(response.data)) |
||
677 | |||
678 | # fail case |
||
679 | link_id = 2 |
||
680 | url = f'{self.server_name_url}/v3/links/{link_id}/metadata' |
||
681 | response = api.get(url) |
||
682 | self.assertEqual(response.status_code, 404, response.data) |
||
683 | |||
684 | @patch('napps.kytos.topology.main.Main.notify_metadata_changes') |
||
685 | def test_add_link_metadata(self, mock_metadata_changes): |
||
686 | """Test add_link_metadata.""" |
||
687 | mock_link = MagicMock(Link) |
||
688 | mock_link.metadata = "A" |
||
689 | self.napp.links = {'1': mock_link} |
||
690 | payload = {"metadata": "A"} |
||
691 | api = get_test_client(self.napp.controller, self.napp) |
||
692 | |||
693 | link_id = 1 |
||
694 | url = f'{self.server_name_url}/v3/links/{link_id}/metadata' |
||
695 | response = api.post(url, data=json.dumps(payload), |
||
696 | content_type='application/json') |
||
697 | self.assertEqual(response.status_code, 201, response.data) |
||
698 | mock_metadata_changes.assert_called() |
||
699 | |||
700 | # fail case |
||
701 | link_id = 2 |
||
702 | url = f'{self.server_name_url}/v3/links/{link_id}/metadata' |
||
703 | response = api.post(url, data=json.dumps(payload), |
||
704 | content_type='application/json') |
||
705 | self.assertEqual(response.status_code, 404, response.data) |
||
706 | |||
707 | @patch('napps.kytos.topology.main.Main.notify_metadata_changes') |
||
708 | def test_delete_link_metadata(self, mock_metadata_changes): |
||
709 | """Test delete_link_metadata.""" |
||
710 | mock_link = MagicMock(Link) |
||
711 | mock_link.metadata = "A" |
||
712 | mock_link.remove_metadata.side_effect = [True, False] |
||
713 | self.napp.links = {'1': mock_link} |
||
714 | api = get_test_client(self.napp.controller, self.napp) |
||
715 | |||
716 | link_id = 1 |
||
717 | key = 'A' |
||
718 | url = f'{self.server_name_url}/v3/links/{link_id}/metadata/{key}' |
||
719 | response = api.delete(url) |
||
720 | self.assertEqual(response.status_code, 200, response.data) |
||
721 | mock_metadata_changes.assert_called() |
||
722 | |||
723 | # fail case link not found |
||
724 | link_id = 2 |
||
725 | key = 'A' |
||
726 | url = f'{self.server_name_url}/v3/links/{link_id}/metadata/{key}' |
||
727 | response = api.delete(url) |
||
728 | self.assertEqual(response.status_code, 404, response.data) |
||
729 | |||
730 | # fail case metadata not found |
||
731 | link_id = 1 |
||
732 | key = 'A' |
||
733 | url = f'{self.server_name_url}/v3/links/{link_id}/metadata/{key}' |
||
734 | response = api.delete(url) |
||
735 | self.assertEqual(response.status_code, 404, response.data) |
||
736 | |||
737 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
||
738 | @patch('napps.kytos.topology.main.Main.update_instance_metadata') |
||
739 | def test_handle_new_switch(self, *args): |
||
740 | """Test handle_new_switch.""" |
||
741 | (mock_instance_metadata, mock_notify_topology_update) = args |
||
742 | mock_event = MagicMock() |
||
743 | mock_switch = create_autospec(Switch) |
||
744 | mock_event.content['switch'] = mock_switch |
||
745 | self.napp.handle_new_switch(mock_event) |
||
746 | mock_notify_topology_update.assert_called() |
||
747 | mock_instance_metadata.assert_called() |
||
748 | |||
749 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
||
750 | def test_handle_connection_lost(self, mock_notify_topology_update): |
||
751 | """Test handle connection_lost.""" |
||
752 | mock_event = MagicMock() |
||
753 | mock_switch = create_autospec(Switch) |
||
754 | mock_switch.return_value = True |
||
755 | mock_event.content['source'] = mock_switch |
||
756 | self.napp.handle_connection_lost(mock_event) |
||
757 | mock_notify_topology_update.assert_called() |
||
758 | |||
759 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
||
760 | @patch('napps.kytos.topology.main.Main.update_instance_metadata') |
||
761 | def test_handle_interface_up(self, *args): |
||
762 | """Test handle_interface_up.""" |
||
763 | (mock_instance_metadata, mock_notify_topology_update) = args |
||
764 | mock_event = MagicMock() |
||
765 | mock_interface = create_autospec(Interface) |
||
766 | mock_event.content['interface'] = mock_interface |
||
767 | self.napp.handle_interface_up(mock_event) |
||
768 | mock_notify_topology_update.assert_called() |
||
769 | mock_instance_metadata.assert_called() |
||
770 | |||
771 | @patch('napps.kytos.topology.main.Main.handle_interface_up') |
||
772 | def test_handle_interface_created(self, mock_handle_interface_up): |
||
773 | """Test handle interface created.""" |
||
774 | mock_event = MagicMock() |
||
775 | self.napp.handle_interface_created(mock_event) |
||
776 | mock_handle_interface_up.assert_called() |
||
777 | |||
778 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
||
779 | @patch('napps.kytos.topology.main.Main.handle_interface_link_down') |
||
780 | def test_handle_interface_down(self, *args): |
||
781 | """Test handle interface down.""" |
||
782 | (mock_handle_interface_link_down, mock_notify_topology_update) = args |
||
783 | mock_event = MagicMock() |
||
784 | mock_interface = create_autospec(Interface) |
||
785 | mock_event.content['interface'] = mock_interface |
||
786 | self.napp.handle_interface_down(mock_event) |
||
787 | mock_handle_interface_link_down.assert_called() |
||
788 | mock_notify_topology_update.assert_called() |
||
789 | |||
790 | @patch('napps.kytos.topology.main.Main.handle_interface_down') |
||
791 | def test_interface_deleted(self, mock_handle_interface_link_down): |
||
792 | """Test interface deleted.""" |
||
793 | mock_event = MagicMock() |
||
794 | self.napp.handle_interface_deleted(mock_event) |
||
795 | mock_handle_interface_link_down.assert_called() |
||
796 | |||
797 | @patch('napps.kytos.topology.main.Main._get_link_from_interface') |
||
798 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
||
799 | @patch('napps.kytos.topology.main.Main.update_instance_metadata') |
||
800 | @patch('napps.kytos.topology.main.Main.notify_link_status_change') |
||
801 | def test_interface_link_up(self, *args): |
||
802 | """Test interface link_up.""" |
||
803 | (mock_status_change, mock_instance_metadata, mock_topology_update, |
||
804 | mock_link_from_interface) = args |
||
805 | |||
806 | now = time.time() |
||
807 | mock_event = MagicMock() |
||
808 | mock_interface_a = create_autospec(Interface) |
||
809 | mock_interface_a.is_active.return_value = False |
||
810 | mock_interface_b = create_autospec(Interface) |
||
811 | mock_interface_b.is_active.return_value = True |
||
812 | mock_link = create_autospec(Link) |
||
813 | mock_link.get_metadata.return_value = now |
||
814 | mock_link.is_active.side_effect = [False, True] |
||
815 | mock_link.endpoint_a = mock_interface_a |
||
816 | mock_link.endpoint_b = mock_interface_b |
||
817 | mock_link_from_interface.return_value = mock_link |
||
818 | content = {'interface': mock_interface_a} |
||
819 | mock_event.content = content |
||
820 | self.napp.link_up_timer = 1 |
||
821 | self.napp.handle_interface_link_up(mock_event) |
||
822 | mock_topology_update.assert_called() |
||
823 | mock_instance_metadata.assert_called() |
||
824 | mock_status_change.assert_called() |
||
825 | |||
826 | @patch('napps.kytos.topology.main.Main._get_link_from_interface') |
||
827 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
||
828 | @patch('napps.kytos.topology.main.Main.notify_link_status_change') |
||
829 | def test_interface_link_down(self, *args): |
||
830 | """Test interface link down.""" |
||
831 | (mock_status_change, mock_topology_update, |
||
832 | mock_link_from_interface) = args |
||
833 | |||
834 | mock_event = MagicMock() |
||
835 | mock_interface = create_autospec(Interface) |
||
836 | mock_link = create_autospec(Link) |
||
837 | mock_link.is_active.return_value = True |
||
838 | mock_link_from_interface.return_value = mock_link |
||
839 | mock_event.content['interface'] = mock_interface |
||
840 | self.napp.handle_interface_link_down(mock_event) |
||
841 | mock_topology_update.assert_called() |
||
842 | mock_status_change.assert_called() |
||
843 | |||
844 | @patch('napps.kytos.topology.main.Main._get_link_or_create') |
||
845 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
||
846 | def test_add_links(self, *args): |
||
847 | """Test add_links.""" |
||
848 | (mock_notify_topology_update, mock_get_link_or_create) = args |
||
849 | mock_event = MagicMock() |
||
850 | self.napp.add_links(mock_event) |
||
851 | mock_get_link_or_create.assert_called() |
||
852 | mock_notify_topology_update.assert_called() |
||
853 | |||
854 | @patch('napps.kytos.topology.main.Main._get_switches_dict') |
||
855 | @patch('napps.kytos.topology.main.StoreHouse.save_status') |
||
856 | def test_save_status_on_store(self, *args): |
||
857 | """Test save_status_on_storehouse.""" |
||
858 | (mock_save_status, mock_get_switches_dict) = args |
||
859 | self.napp.save_status_on_storehouse() |
||
860 | mock_get_switches_dict.assert_called() |
||
861 | mock_save_status.assert_called() |
||
862 | |||
863 | @patch('napps.kytos.topology.main.KytosEvent') |
||
864 | @patch('kytos.core.buffers.KytosEventBuffer.put') |
||
865 | def test_notify_switch_enabled(self, *args): |
||
866 | """Test notify switch enabled.""" |
||
867 | dpid = "00:00:00:00:00:00:00:01" |
||
868 | (mock_buffers_put, mock_event) = args |
||
869 | self.napp.notify_switch_enabled(dpid) |
||
870 | mock_event.assert_called() |
||
871 | mock_buffers_put.assert_called() |
||
872 | |||
873 | @patch('napps.kytos.topology.main.KytosEvent') |
||
874 | @patch('kytos.core.buffers.KytosEventBuffer.put') |
||
875 | def test_notify_switch_disabled(self, *args): |
||
876 | """Test notify switch disabled.""" |
||
877 | dpid = "00:00:00:00:00:00:00:01" |
||
878 | (mock_buffers_put, mock_event) = args |
||
879 | self.napp.notify_switch_disabled(dpid) |
||
880 | mock_event.assert_called() |
||
881 | mock_buffers_put.assert_called() |
||
882 | |||
883 | @patch('napps.kytos.topology.main.KytosEvent') |
||
884 | @patch('kytos.core.buffers.KytosEventBuffer.put') |
||
885 | def test_notify_topology_update(self, *args): |
||
886 | """Test notify_topology_update.""" |
||
887 | (mock_buffers_put, mock_event) = args |
||
888 | self.napp.notify_topology_update() |
||
889 | mock_event.assert_called() |
||
890 | mock_buffers_put.assert_called() |
||
891 | |||
892 | @patch('napps.kytos.topology.main.KytosEvent') |
||
893 | @patch('kytos.core.buffers.KytosEventBuffer.put') |
||
894 | def test_notify_link_status_change(self, *args): |
||
895 | """Test notify link status change.""" |
||
896 | (mock_buffers_put, mock_event) = args |
||
897 | mock_link = create_autospec(Link) |
||
898 | self.napp.notify_link_status_change(mock_link) |
||
899 | mock_event.assert_called() |
||
900 | mock_buffers_put.assert_called() |
||
901 | |||
902 | @patch('napps.kytos.topology.main.KytosEvent') |
||
903 | @patch('kytos.core.buffers.KytosEventBuffer.put') |
||
904 | @patch('napps.kytos.topology.main.isinstance') |
||
905 | def test_notify_metadata_changes(self, *args): |
||
906 | """Test notify metadata changes.""" |
||
907 | (mock_isinstance, mock_buffers_put, mock_event) = args |
||
908 | mock_isinstance.return_value = True |
||
909 | mock_obj = MagicMock() |
||
910 | mock_action = create_autospec(Switch) |
||
911 | self.napp.notify_metadata_changes(mock_obj, mock_action) |
||
912 | mock_event.assert_called() |
||
913 | mock_isinstance.assert_called() |
||
914 | mock_buffers_put.assert_called() |
||
915 | |||
916 | @patch('napps.kytos.topology.main.KytosEvent') |
||
917 | @patch('kytos.core.buffers.KytosEventBuffer.put') |
||
918 | def test_notify_port_created(self, *args): |
||
919 | """Test notify port created.""" |
||
920 | (mock_buffers_put, mock_kytos_event) = args |
||
921 | mock_event = MagicMock() |
||
922 | self.napp.notify_port_created(mock_event) |
||
923 | mock_kytos_event.assert_called() |
||
924 | mock_buffers_put.assert_called() |
||
925 | |||
926 | @patch('napps.kytos.topology.main.KytosEvent') |
||
927 | @patch('kytos.core.buffers.KytosEventBuffer.put') |
||
928 | def test_save_metadata_on_store(self, *args): |
||
929 | """Test test_save_metadata_on_store.""" |
||
930 | (mock_buffers_put, mock_kytos_event) = args |
||
931 | mock_event = MagicMock() |
||
932 | mock_switch = MagicMock() |
||
933 | mock_interface = MagicMock() |
||
934 | mock_link = MagicMock() |
||
935 | self.napp.store_items = {'switches': mock_switch, |
||
936 | 'interfaces': mock_interface, |
||
937 | 'links': mock_link} |
||
938 | # test switches |
||
939 | mock_event.content = {'switch': mock_switch} |
||
940 | self.napp.save_metadata_on_store(mock_event) |
||
941 | mock_kytos_event.assert_called() |
||
942 | mock_buffers_put.assert_called() |
||
943 | |||
944 | # test interfaces |
||
945 | mock_event.content = {'interface': mock_interface} |
||
946 | self.napp.save_metadata_on_store(mock_event) |
||
947 | mock_kytos_event.assert_called() |
||
948 | mock_buffers_put.assert_called() |
||
949 | |||
950 | # test link |
||
951 | mock_event.content = {'link': mock_link} |
||
952 | self.napp.save_metadata_on_store(mock_event) |
||
953 | mock_kytos_event.assert_called() |
||
954 | mock_buffers_put.assert_called() |
||
955 | |||
956 | @patch('napps.kytos.topology.main.KytosEvent') |
||
957 | @patch('kytos.core.buffers.KytosEventBuffer.put') |
||
958 | def test_verify_storehouse(self, *args): |
||
959 | """Test verify_storehouse.""" |
||
960 | (mock_buffers_put, mock_kytos_event) = args |
||
961 | mock_entities = MagicMock() |
||
962 | self.napp.verify_storehouse(mock_entities) |
||
963 | mock_buffers_put.assert_called() |
||
964 | mock_kytos_event.assert_called() |
||
965 | |||
966 | @patch('napps.kytos.topology.main.KytosEvent') |
||
967 | @patch('kytos.core.buffers.KytosEventBuffer.put') |
||
968 | def test_request_retrieve_entities(self, *args): |
||
969 | """Test retrive_entities.""" |
||
970 | (mock_buffers_put, mock_kytos_event) = args |
||
971 | mock_event = MagicMock() |
||
972 | mock_data = MagicMock() |
||
973 | mock_error = MagicMock() |
||
974 | mock_event.content = {"namespace": "test_box"} |
||
975 | self.napp.request_retrieve_entities(mock_event, mock_data, mock_error) |
||
976 | mock_kytos_event.assert_called() |
||
977 | mock_buffers_put.assert_called() |
||
978 | |||
979 | self.napp.request_retrieve_entities(mock_event, None, mock_error) |
||
980 | mock_kytos_event.assert_called() |
||
981 | mock_buffers_put.assert_called() |
||
982 | |||
983 | @patch('napps.kytos.topology.main.Main.notify_link_status_change') |
||
984 | def test_handle_link_maintenance_start(self, status_change_mock): |
||
985 | """Test handle_link_maintenance_start.""" |
||
986 | link1 = MagicMock() |
||
987 | link1.id = 2 |
||
988 | link2 = MagicMock() |
||
989 | link2.id = 3 |
||
990 | link3 = MagicMock() |
||
991 | link3.id = 4 |
||
992 | content = {'links': [link1, link2]} |
||
993 | event = MagicMock() |
||
994 | event.content = content |
||
995 | self.napp.links = {2: link1, 4: link3} |
||
996 | self.napp.handle_link_maintenance_start(event) |
||
997 | status_change_mock.assert_called_once_with(link1) |
||
998 | |||
999 | @patch('napps.kytos.topology.main.Main.notify_link_status_change') |
||
1000 | def test_handle_link_maintenance_end(self, status_change_mock): |
||
1001 | """Test handle_link_maintenance_end.""" |
||
1002 | link1 = MagicMock() |
||
1003 | link1.id = 2 |
||
1004 | link2 = MagicMock() |
||
1005 | link2.id = 3 |
||
1006 | link3 = MagicMock() |
||
1007 | link3.id = 4 |
||
1008 | content = {'links': [link1, link2]} |
||
1009 | event = MagicMock() |
||
1010 | event.content = content |
||
1011 | self.napp.links = {2: link1, 4: link3} |
||
1012 | self.napp.handle_link_maintenance_end(event) |
||
1013 | status_change_mock.assert_called_once_with(link1) |
||
1014 | |||
1015 | View Code Duplication | @patch('napps.kytos.topology.main.Main.handle_link_down') |
|
1016 | def test_handle_switch_maintenance_start(self, handle_link_down_mock): |
||
1017 | """Test handle_switch_maintenance_start.""" |
||
1018 | switch1 = MagicMock() |
||
1019 | interface1 = MagicMock() |
||
1020 | interface1.is_active.return_value = True |
||
1021 | interface2 = MagicMock() |
||
1022 | interface2.is_active.return_value = False |
||
1023 | interface3 = MagicMock() |
||
1024 | interface3.is_active.return_value = True |
||
1025 | switch1.interfaces = {1: interface1, 2: interface2, 3: interface3} |
||
1026 | switch2 = MagicMock() |
||
1027 | interface4 = MagicMock() |
||
1028 | interface4.is_active.return_value = False |
||
1029 | interface5 = MagicMock() |
||
1030 | interface5.is_active.return_value = True |
||
1031 | switch2.interfaces = {1: interface4, 2: interface5} |
||
1032 | content = {'switches': [switch1, switch2]} |
||
1033 | event = MagicMock() |
||
1034 | event.content = content |
||
1035 | self.napp.handle_switch_maintenance_start(event) |
||
1036 | self.assertEqual(handle_link_down_mock.call_count, 3) |
||
1037 | |||
1038 | View Code Duplication | @patch('napps.kytos.topology.main.Main.handle_link_up') |
|
1039 | def test_handle_switch_maintenance_end(self, handle_link_up_mock): |
||
1040 | """Test handle_switch_maintenance_end.""" |
||
1041 | switch1 = MagicMock() |
||
1042 | interface1 = MagicMock() |
||
1043 | interface1.is_active.return_value = True |
||
1044 | interface2 = MagicMock() |
||
1045 | interface2.is_active.return_value = False |
||
1046 | interface3 = MagicMock() |
||
1047 | interface3.is_active.return_value = True |
||
1048 | switch1.interfaces = {1: interface1, 2: interface2, 3: interface3} |
||
1049 | switch2 = MagicMock() |
||
1050 | interface4 = MagicMock() |
||
1051 | interface4.is_active.return_value = False |
||
1052 | interface5 = MagicMock() |
||
1053 | interface5.is_active.return_value = True |
||
1054 | switch2.interfaces = {1: interface4, 2: interface5} |
||
1055 | content = {'switches': [switch1, switch2]} |
||
1056 | event = MagicMock() |
||
1057 | event.content = content |
||
1058 | self.napp.handle_switch_maintenance_end(event) |
||
1059 | self.assertEqual(handle_link_up_mock.call_count, 5) |
||
1060 |