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