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