| Total Complexity | 52 |
| Total Lines | 1039 |
| Duplicated Lines | 21.46 % |
| 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 | |||
| 303 | self.napp.links_state = {link_id_fail: {"enabled": True}} |
||
| 304 | with self.assertRaises(KeyError): |
||
| 305 | self.napp._restore_link(link_id_fail) |
||
| 306 | |||
| 307 | def test_fail_restore_switch(self): |
||
| 308 | """Test fail restore_switch.""" |
||
| 309 | dpid = '00:00:00:00:00:00:00:01' |
||
| 310 | dpid_fail = '00:00:00:00:00:00:00:06' |
||
| 311 | mock_switch = get_switch_mock(dpid) |
||
| 312 | mock_switch.id = dpid |
||
| 313 | mock_interface = get_interface_mock('s1-eth1', 1, mock_switch) |
||
| 314 | mock_switch.interfaces = {1: mock_interface} |
||
| 315 | self.napp.controller.switche = {dpid: mock_switch} |
||
| 316 | self.napp.switches_state = {dpid: True} |
||
| 317 | self.napp.interfaces_state = {'00:00:00:00:00:00:00:01:1': (True, |
||
| 318 | True)} |
||
| 319 | |||
| 320 | with self.assertRaises(KeyError): |
||
| 321 | self.napp._restore_switch(dpid_fail) |
||
| 322 | |||
| 323 | self.napp.switches_state = {dpid_fail: True} |
||
| 324 | with self.assertRaises(KeyError): |
||
| 325 | self.napp._restore_switch(dpid_fail) |
||
| 326 | |||
| 327 | View Code Duplication | @patch('napps.kytos.topology.main.Main.save_status_on_storehouse') |
|
| 328 | def test_enable_switch(self, mock_save_status): |
||
| 329 | """Test enable_switch.""" |
||
| 330 | dpid = "00:00:00:00:00:00:00:01" |
||
| 331 | mock_switch = get_switch_mock(dpid) |
||
| 332 | self.napp.controller.switches = {dpid: mock_switch} |
||
| 333 | api = get_test_client(self.napp.controller, self.napp) |
||
| 334 | |||
| 335 | url = f'{self.server_name_url}/v3/switches/{dpid}/enable' |
||
| 336 | response = api.post(url) |
||
| 337 | self.assertEqual(response.status_code, 201, response.data) |
||
| 338 | self.assertEqual(mock_switch.enable.call_count, 1) |
||
| 339 | mock_save_status.assert_called() |
||
| 340 | |||
| 341 | # fail case |
||
| 342 | mock_switch.enable.call_count = 0 |
||
| 343 | dpid = "00:00:00:00:00:00:00:02" |
||
| 344 | url = f'{self.server_name_url}/v3/switches/{dpid}/enable' |
||
| 345 | response = api.post(url) |
||
| 346 | self.assertEqual(response.status_code, 404, response.data) |
||
| 347 | self.assertEqual(mock_switch.enable.call_count, 0) |
||
| 348 | |||
| 349 | View Code Duplication | @patch('napps.kytos.topology.main.Main.save_status_on_storehouse') |
|
| 350 | def test_disable_switch(self, mock_save_status): |
||
| 351 | """Test disable_switch.""" |
||
| 352 | dpid = "00:00:00:00:00:00:00:01" |
||
| 353 | mock_switch = get_switch_mock(dpid) |
||
| 354 | self.napp.controller.switches = {dpid: mock_switch} |
||
| 355 | api = get_test_client(self.napp.controller, self.napp) |
||
| 356 | |||
| 357 | url = f'{self.server_name_url}/v3/switches/{dpid}/disable' |
||
| 358 | response = api.post(url) |
||
| 359 | self.assertEqual(response.status_code, 201, response.data) |
||
| 360 | self.assertEqual(mock_switch.disable.call_count, 1) |
||
| 361 | mock_save_status.assert_called() |
||
| 362 | |||
| 363 | # fail case |
||
| 364 | mock_switch.disable.call_count = 0 |
||
| 365 | dpid = "00:00:00:00:00:00:00:02" |
||
| 366 | url = f'{self.server_name_url}/v3/switches/{dpid}/disable' |
||
| 367 | response = api.post(url) |
||
| 368 | self.assertEqual(response.status_code, 404, response.data) |
||
| 369 | self.assertEqual(mock_switch.disable.call_count, 0) |
||
| 370 | |||
| 371 | def test_get_switch_metadata(self): |
||
| 372 | """Test get_switch_metadata.""" |
||
| 373 | dpid = "00:00:00:00:00:00:00:01" |
||
| 374 | mock_switch = get_switch_mock(dpid) |
||
| 375 | mock_switch.metadata = "A" |
||
| 376 | self.napp.controller.switches = {dpid: mock_switch} |
||
| 377 | api = get_test_client(self.napp.controller, self.napp) |
||
| 378 | |||
| 379 | url = f'{self.server_name_url}/v3/switches/{dpid}/metadata' |
||
| 380 | response = api.get(url) |
||
| 381 | self.assertEqual(response.status_code, 200, response.data) |
||
| 382 | |||
| 383 | # fail case |
||
| 384 | dpid = "00:00:00:00:00:00:00:02" |
||
| 385 | url = f'{self.server_name_url}/v3/switches/{dpid}/metadata' |
||
| 386 | response = api.get(url) |
||
| 387 | self.assertEqual(response.status_code, 404, response.data) |
||
| 388 | |||
| 389 | @patch('napps.kytos.topology.main.Main.notify_metadata_changes') |
||
| 390 | def test_add_switch_metadata(self, mock_metadata_changes): |
||
| 391 | """Test add_switch_metadata.""" |
||
| 392 | dpid = "00:00:00:00:00:00:00:01" |
||
| 393 | mock_switch = get_switch_mock(dpid) |
||
| 394 | self.napp.controller.switches = {dpid: mock_switch} |
||
| 395 | api = get_test_client(self.napp.controller, self.napp) |
||
| 396 | payload = {"data": "A"} |
||
| 397 | |||
| 398 | url = f'{self.server_name_url}/v3/switches/{dpid}/metadata' |
||
| 399 | response = api.post(url, data=json.dumps(payload), |
||
| 400 | content_type='application/json') |
||
| 401 | self.assertEqual(response.status_code, 201, response.data) |
||
| 402 | mock_metadata_changes.assert_called() |
||
| 403 | |||
| 404 | # fail case |
||
| 405 | dpid = "00:00:00:00:00:00:00:02" |
||
| 406 | url = f'{self.server_name_url}/v3/switches/{dpid}/metadata' |
||
| 407 | response = api.post(url, data=json.dumps(payload), |
||
| 408 | content_type='application/json') |
||
| 409 | self.assertEqual(response.status_code, 404, response.data) |
||
| 410 | |||
| 411 | @patch('napps.kytos.topology.main.Main.notify_metadata_changes') |
||
| 412 | def test_delete_switch_metadata(self, mock_metadata_changes): |
||
| 413 | """Test delete_switch_metadata.""" |
||
| 414 | dpid = "00:00:00:00:00:00:00:01" |
||
| 415 | mock_switch = get_switch_mock(dpid) |
||
| 416 | self.napp.controller.switches = {dpid: mock_switch} |
||
| 417 | api = get_test_client(self.napp.controller, self.napp) |
||
| 418 | |||
| 419 | key = "A" |
||
| 420 | url = f'{self.server_name_url}/v3/switches/{dpid}/metadata/{key}' |
||
| 421 | response = api.delete(url) |
||
| 422 | mock_metadata_changes.assert_called() |
||
| 423 | self.assertEqual(response.status_code, 200, response.data) |
||
| 424 | |||
| 425 | # fail case |
||
| 426 | key = "A" |
||
| 427 | dpid = "00:00:00:00:00:00:00:02" |
||
| 428 | url = f'{self.server_name_url}/v3/switches/{dpid}/metadata/{key}' |
||
| 429 | response = api.delete(url) |
||
| 430 | mock_metadata_changes.assert_called() |
||
| 431 | self.assertEqual(response.status_code, 404, response.data) |
||
| 432 | |||
| 433 | View Code Duplication | @patch('napps.kytos.topology.main.Main.save_status_on_storehouse') |
|
| 434 | def test_enable_interfaces(self, mock_save_status): |
||
| 435 | """Test enable_interfaces.""" |
||
| 436 | dpid = '00:00:00:00:00:00:00:01' |
||
| 437 | mock_switch = get_switch_mock(dpid) |
||
| 438 | mock_interface_1 = get_interface_mock('s1-eth1', 1, mock_switch) |
||
| 439 | mock_interface_2 = get_interface_mock('s1-eth2', 2, mock_switch) |
||
| 440 | mock_switch.interfaces = {1: mock_interface_1, 2: mock_interface_2} |
||
| 441 | self.napp.controller.switches = {dpid: mock_switch} |
||
| 442 | api = get_test_client(self.napp.controller, self.napp) |
||
| 443 | |||
| 444 | interface_id = '00:00:00:00:00:00:00:01:1' |
||
| 445 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/enable' |
||
| 446 | response = api.post(url) |
||
| 447 | self.assertEqual(response.status_code, 200, response.data) |
||
| 448 | self.assertEqual(mock_interface_1.enable.call_count, 1) |
||
| 449 | self.assertEqual(mock_interface_2.enable.call_count, 0) |
||
| 450 | mock_save_status.assert_called() |
||
| 451 | |||
| 452 | mock_interface_1.enable.call_count = 0 |
||
| 453 | mock_interface_2.enable.call_count = 0 |
||
| 454 | url = f'{self.server_name_url}/v3/interfaces/switch/{dpid}/enable' |
||
| 455 | response = api.post(url) |
||
| 456 | self.assertEqual(response.status_code, 200, response.data) |
||
| 457 | self.assertEqual(mock_interface_1.enable.call_count, 1) |
||
| 458 | self.assertEqual(mock_interface_2.enable.call_count, 1) |
||
| 459 | |||
| 460 | # test interface not found |
||
| 461 | interface_id = '00:00:00:00:00:00:00:01:3' |
||
| 462 | mock_interface_1.enable.call_count = 0 |
||
| 463 | mock_interface_2.enable.call_count = 0 |
||
| 464 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/enable' |
||
| 465 | response = api.post(url) |
||
| 466 | self.assertEqual(response.status_code, 409, response.data) |
||
| 467 | self.assertEqual(mock_interface_1.enable.call_count, 0) |
||
| 468 | self.assertEqual(mock_interface_2.enable.call_count, 0) |
||
| 469 | |||
| 470 | # test switch not found |
||
| 471 | dpid = '00:00:00:00:00:00:00:02' |
||
| 472 | url = f'{self.server_name_url}/v3/interfaces/switch/{dpid}/enable' |
||
| 473 | response = api.post(url) |
||
| 474 | self.assertEqual(response.status_code, 404, response.data) |
||
| 475 | self.assertEqual(mock_interface_1.enable.call_count, 0) |
||
| 476 | self.assertEqual(mock_interface_2.enable.call_count, 0) |
||
| 477 | |||
| 478 | View Code Duplication | @patch('napps.kytos.topology.main.Main.save_status_on_storehouse') |
|
| 479 | def test_disable_interfaces(self, mock_save_status): |
||
| 480 | """Test disable_interfaces.""" |
||
| 481 | interface_id = '00:00:00:00:00:00:00:01:1' |
||
| 482 | dpid = '00:00:00:00:00:00:00:01' |
||
| 483 | mock_switch = get_switch_mock(dpid) |
||
| 484 | mock_interface_1 = get_interface_mock('s1-eth1', 1, mock_switch) |
||
| 485 | mock_interface_2 = get_interface_mock('s1-eth2', 2, mock_switch) |
||
| 486 | mock_switch.interfaces = {1: mock_interface_1, 2: mock_interface_2} |
||
| 487 | self.napp.controller.switches = {dpid: mock_switch} |
||
| 488 | api = get_test_client(self.napp.controller, self.napp) |
||
| 489 | |||
| 490 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/disable' |
||
| 491 | response = api.post(url) |
||
| 492 | self.assertEqual(response.status_code, 200, response.data) |
||
| 493 | self.assertEqual(mock_interface_1.disable.call_count, 1) |
||
| 494 | self.assertEqual(mock_interface_2.disable.call_count, 0) |
||
| 495 | mock_save_status.assert_called() |
||
| 496 | |||
| 497 | mock_interface_1.disable.call_count = 0 |
||
| 498 | mock_interface_2.disable.call_count = 0 |
||
| 499 | url = f'{self.server_name_url}/v3/interfaces/switch/{dpid}/disable' |
||
| 500 | response = api.post(url) |
||
| 501 | self.assertEqual(response.status_code, 200, response.data) |
||
| 502 | self.assertEqual(mock_interface_1.disable.call_count, 1) |
||
| 503 | self.assertEqual(mock_interface_2.disable.call_count, 1) |
||
| 504 | |||
| 505 | # test interface not found |
||
| 506 | interface_id = '00:00:00:00:00:00:00:01:3' |
||
| 507 | mock_interface_1.disable.call_count = 0 |
||
| 508 | mock_interface_2.disable.call_count = 0 |
||
| 509 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/disable' |
||
| 510 | response = api.post(url) |
||
| 511 | self.assertEqual(response.status_code, 409, response.data) |
||
| 512 | self.assertEqual(mock_interface_1.disable.call_count, 0) |
||
| 513 | self.assertEqual(mock_interface_2.disable.call_count, 0) |
||
| 514 | |||
| 515 | # test switch not found |
||
| 516 | dpid = '00:00:00:00:00:00:00:02' |
||
| 517 | url = f'{self.server_name_url}/v3/interfaces/switch/{dpid}/disable' |
||
| 518 | response = api.post(url) |
||
| 519 | self.assertEqual(response.status_code, 404, response.data) |
||
| 520 | self.assertEqual(mock_interface_1.disable.call_count, 0) |
||
| 521 | self.assertEqual(mock_interface_2.disable.call_count, 0) |
||
| 522 | |||
| 523 | def test_get_interface_metadata(self): |
||
| 524 | """Test get_interface_metada.""" |
||
| 525 | interface_id = '00:00:00:00:00:00:00:01:1' |
||
| 526 | dpid = '00:00:00:00:00:00:00:01' |
||
| 527 | mock_switch = get_switch_mock(dpid) |
||
| 528 | mock_interface = get_interface_mock('s1-eth1', 1, mock_switch) |
||
| 529 | mock_interface.metadata = {"metada": "A"} |
||
| 530 | mock_switch.interfaces = {1: mock_interface} |
||
| 531 | self.napp.controller.switches = {dpid: mock_switch} |
||
| 532 | api = get_test_client(self.napp.controller, self.napp) |
||
| 533 | |||
| 534 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/metadata' |
||
| 535 | response = api.get(url) |
||
| 536 | self.assertEqual(response.status_code, 200, response.data) |
||
| 537 | |||
| 538 | # fail case switch not found |
||
| 539 | interface_id = '00:00:00:00:00:00:00:02:1' |
||
| 540 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/metadata' |
||
| 541 | response = api.get(url) |
||
| 542 | self.assertEqual(response.status_code, 404, response.data) |
||
| 543 | |||
| 544 | # fail case interface not found |
||
| 545 | interface_id = '00:00:00:00:00:00:00:01:2' |
||
| 546 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/metadata' |
||
| 547 | response = api.get(url) |
||
| 548 | self.assertEqual(response.status_code, 404, response.data) |
||
| 549 | |||
| 550 | @patch('napps.kytos.topology.main.Main.notify_metadata_changes') |
||
| 551 | def test_add_interface_metadata(self, mock_metadata_changes): |
||
| 552 | """Test add_interface_metadata.""" |
||
| 553 | interface_id = '00:00:00:00:00:00:00:01:1' |
||
| 554 | dpid = '00:00:00:00:00:00:00:01' |
||
| 555 | mock_switch = get_switch_mock(dpid) |
||
| 556 | mock_interface = get_interface_mock('s1-eth1', 1, mock_switch) |
||
| 557 | mock_interface.metadata = {"metada": "A"} |
||
| 558 | mock_switch.interfaces = {1: mock_interface} |
||
| 559 | self.napp.controller.switches = {dpid: mock_switch} |
||
| 560 | api = get_test_client(self.napp.controller, self.napp) |
||
| 561 | |||
| 562 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/metadata' |
||
| 563 | payload = {"metada": "A"} |
||
| 564 | response = api.post(url, data=json.dumps(payload), |
||
| 565 | content_type='application/json') |
||
| 566 | self.assertEqual(response.status_code, 201, response.data) |
||
| 567 | mock_metadata_changes.assert_called() |
||
| 568 | |||
| 569 | # fail case switch not found |
||
| 570 | interface_id = '00:00:00:00:00:00:00:02:1' |
||
| 571 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/metadata' |
||
| 572 | response = api.post(url, data=json.dumps(payload), |
||
| 573 | content_type='application/json') |
||
| 574 | self.assertEqual(response.status_code, 404, response.data) |
||
| 575 | |||
| 576 | # fail case interface not found |
||
| 577 | interface_id = '00:00:00:00:00:00:00:01:2' |
||
| 578 | url = f'{self.server_name_url}/v3/interfaces/{interface_id}/metadata' |
||
| 579 | response = api.post(url, data=json.dumps(payload), |
||
| 580 | content_type='application/json') |
||
| 581 | self.assertEqual(response.status_code, 404, response.data) |
||
| 582 | |||
| 583 | def test_delete_interface_metadata(self): |
||
| 584 | """Test delete_interface_metadata.""" |
||
| 585 | interface_id = '00:00:00:00:00:00:00:01:1' |
||
| 586 | dpid = '00:00:00:00:00:00:00:01' |
||
| 587 | iface_url = '/v3/interfaces/' |
||
| 588 | mock_switch = get_switch_mock(dpid) |
||
| 589 | mock_interface = get_interface_mock('s1-eth1', 1, mock_switch) |
||
| 590 | mock_interface.remove_metadata.side_effect = [True, False] |
||
| 591 | mock_interface.metadata = {"metada": "A"} |
||
| 592 | mock_switch.interfaces = {1: mock_interface} |
||
| 593 | self.napp.controller.switches = {'00:00:00:00:00:00:00:01': |
||
| 594 | mock_switch} |
||
| 595 | api = get_test_client(self.napp.controller, self.napp) |
||
| 596 | |||
| 597 | key = 'A' |
||
| 598 | url = f'{self.server_name_url}{iface_url}{interface_id}/metadata/{key}' |
||
| 599 | response = api.delete(url) |
||
| 600 | self.assertEqual(response.status_code, 200, response.data) |
||
| 601 | |||
| 602 | # fail case switch not found |
||
| 603 | key = 'A' |
||
| 604 | interface_id = '00:00:00:00:00:00:00:02:1' |
||
| 605 | url = f'{self.server_name_url}{iface_url}{interface_id}/metadata/{key}' |
||
| 606 | response = api.delete(url) |
||
| 607 | self.assertEqual(response.status_code, 404, response.data) |
||
| 608 | |||
| 609 | # fail case interface not found |
||
| 610 | key = 'A' |
||
| 611 | interface_id = '00:00:00:00:00:00:00:01:2' |
||
| 612 | url = f'{self.server_name_url}{iface_url}{interface_id}/metadata/{key}' |
||
| 613 | response = api.delete(url) |
||
| 614 | self.assertEqual(response.status_code, 404, response.data) |
||
| 615 | |||
| 616 | # fail case metadata not found |
||
| 617 | key = 'A' |
||
| 618 | interface_id = '00:00:00:00:00:00:00:01:1' |
||
| 619 | url = f'{self.server_name_url}{iface_url}{interface_id}/metadata/{key}' |
||
| 620 | response = api.delete(url) |
||
| 621 | self.assertEqual(response.status_code, 404, response.data) |
||
| 622 | |||
| 623 | @patch('napps.kytos.topology.main.Main.save_status_on_storehouse') |
||
| 624 | def test_enable_link(self, mock_save_status): |
||
| 625 | """Test enable_link.""" |
||
| 626 | mock_link = MagicMock(Link) |
||
| 627 | self.napp.links = {'1': mock_link} |
||
| 628 | api = get_test_client(self.napp.controller, self.napp) |
||
| 629 | mock_save_status.return_value = True |
||
| 630 | |||
| 631 | link_id = 1 |
||
| 632 | url = f'{self.server_name_url}/v3/links/{link_id}/enable' |
||
| 633 | response = api.post(url) |
||
| 634 | self.assertEqual(response.status_code, 201, response.data) |
||
| 635 | self.assertEqual(mock_link.enable.call_count, 1) |
||
| 636 | |||
| 637 | # fail case |
||
| 638 | link_id = 2 |
||
| 639 | url = f'{self.server_name_url}/v3/links/{link_id}/enable' |
||
| 640 | response = api.post(url) |
||
| 641 | self.assertEqual(response.status_code, 404, response.data) |
||
| 642 | |||
| 643 | @patch('napps.kytos.topology.main.Main.save_status_on_storehouse') |
||
| 644 | def test_disable_link(self, mock_save_status): |
||
| 645 | """Test disable_link.""" |
||
| 646 | mock_link = MagicMock(Link) |
||
| 647 | self.napp.links = {'1': mock_link} |
||
| 648 | api = get_test_client(self.napp.controller, self.napp) |
||
| 649 | mock_save_status.return_value = True |
||
| 650 | |||
| 651 | link_id = 1 |
||
| 652 | url = f'{self.server_name_url}/v3/links/{link_id}/disable' |
||
| 653 | response = api.post(url) |
||
| 654 | self.assertEqual(response.status_code, 201, response.data) |
||
| 655 | self.assertEqual(mock_link.disable.call_count, 1) |
||
| 656 | |||
| 657 | # fail case |
||
| 658 | link_id = 2 |
||
| 659 | url = f'{self.server_name_url}/v3/links/{link_id}/disable' |
||
| 660 | response = api.post(url) |
||
| 661 | self.assertEqual(response.status_code, 404, response.data) |
||
| 662 | |||
| 663 | def test_get_link_metadata(self): |
||
| 664 | """Test get_link_metadata.""" |
||
| 665 | mock_link = MagicMock(Link) |
||
| 666 | mock_link.metadata = "A" |
||
| 667 | self.napp.links = {'1': mock_link} |
||
| 668 | msg_success = {"metadata": "A"} |
||
| 669 | api = get_test_client(self.napp.controller, self.napp) |
||
| 670 | |||
| 671 | link_id = 1 |
||
| 672 | url = f'{self.server_name_url}/v3/links/{link_id}/metadata' |
||
| 673 | response = api.get(url) |
||
| 674 | self.assertEqual(response.status_code, 200, response.data) |
||
| 675 | self.assertEqual(msg_success, json.loads(response.data)) |
||
| 676 | |||
| 677 | # fail case |
||
| 678 | link_id = 2 |
||
| 679 | url = f'{self.server_name_url}/v3/links/{link_id}/metadata' |
||
| 680 | response = api.get(url) |
||
| 681 | self.assertEqual(response.status_code, 404, response.data) |
||
| 682 | |||
| 683 | @patch('napps.kytos.topology.main.Main.notify_metadata_changes') |
||
| 684 | def test_add_link_metadata(self, mock_metadata_changes): |
||
| 685 | """Test add_link_metadata.""" |
||
| 686 | mock_link = MagicMock(Link) |
||
| 687 | mock_link.metadata = "A" |
||
| 688 | self.napp.links = {'1': mock_link} |
||
| 689 | payload = {"metadata": "A"} |
||
| 690 | api = get_test_client(self.napp.controller, self.napp) |
||
| 691 | |||
| 692 | link_id = 1 |
||
| 693 | url = f'{self.server_name_url}/v3/links/{link_id}/metadata' |
||
| 694 | response = api.post(url, data=json.dumps(payload), |
||
| 695 | content_type='application/json') |
||
| 696 | self.assertEqual(response.status_code, 201, response.data) |
||
| 697 | mock_metadata_changes.assert_called() |
||
| 698 | |||
| 699 | # fail case |
||
| 700 | link_id = 2 |
||
| 701 | url = f'{self.server_name_url}/v3/links/{link_id}/metadata' |
||
| 702 | response = api.post(url, data=json.dumps(payload), |
||
| 703 | content_type='application/json') |
||
| 704 | self.assertEqual(response.status_code, 404, response.data) |
||
| 705 | |||
| 706 | @patch('napps.kytos.topology.main.Main.notify_metadata_changes') |
||
| 707 | def test_delete_link_metadata(self, mock_metadata_changes): |
||
| 708 | """Test delete_link_metadata.""" |
||
| 709 | mock_link = MagicMock(Link) |
||
| 710 | mock_link.metadata = "A" |
||
| 711 | mock_link.remove_metadata.side_effect = [True, False] |
||
| 712 | self.napp.links = {'1': mock_link} |
||
| 713 | api = get_test_client(self.napp.controller, self.napp) |
||
| 714 | |||
| 715 | link_id = 1 |
||
| 716 | key = 'A' |
||
| 717 | url = f'{self.server_name_url}/v3/links/{link_id}/metadata/{key}' |
||
| 718 | response = api.delete(url) |
||
| 719 | self.assertEqual(response.status_code, 200, response.data) |
||
| 720 | mock_metadata_changes.assert_called() |
||
| 721 | |||
| 722 | # fail case link not found |
||
| 723 | link_id = 2 |
||
| 724 | key = 'A' |
||
| 725 | url = f'{self.server_name_url}/v3/links/{link_id}/metadata/{key}' |
||
| 726 | response = api.delete(url) |
||
| 727 | self.assertEqual(response.status_code, 404, response.data) |
||
| 728 | |||
| 729 | # fail case metadata not found |
||
| 730 | link_id = 1 |
||
| 731 | key = 'A' |
||
| 732 | url = f'{self.server_name_url}/v3/links/{link_id}/metadata/{key}' |
||
| 733 | response = api.delete(url) |
||
| 734 | self.assertEqual(response.status_code, 404, response.data) |
||
| 735 | |||
| 736 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
||
| 737 | @patch('napps.kytos.topology.main.Main.update_instance_metadata') |
||
| 738 | def test_handle_new_switch(self, *args): |
||
| 739 | """Test handle_new_switch.""" |
||
| 740 | (mock_instance_metadata, mock_notify_topology_update) = args |
||
| 741 | mock_event = MagicMock() |
||
| 742 | mock_switch = create_autospec(Switch) |
||
| 743 | mock_event.content['switch'] = mock_switch |
||
| 744 | self.napp.handle_new_switch(mock_event) |
||
| 745 | mock_notify_topology_update.assert_called() |
||
| 746 | mock_instance_metadata.assert_called() |
||
| 747 | |||
| 748 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
||
| 749 | def test_handle_connection_lost(self, mock_notify_topology_update): |
||
| 750 | """Test handle connection_lost.""" |
||
| 751 | mock_event = MagicMock() |
||
| 752 | mock_switch = create_autospec(Switch) |
||
| 753 | mock_switch.return_value = True |
||
| 754 | mock_event.content['source'] = mock_switch |
||
| 755 | self.napp.handle_connection_lost(mock_event) |
||
| 756 | mock_notify_topology_update.assert_called() |
||
| 757 | |||
| 758 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
||
| 759 | @patch('napps.kytos.topology.main.Main.update_instance_metadata') |
||
| 760 | def test_handle_interface_up(self, *args): |
||
| 761 | """Test handle_interface_up.""" |
||
| 762 | (mock_instance_metadata, mock_notify_topology_update) = args |
||
| 763 | mock_event = MagicMock() |
||
| 764 | mock_interface = create_autospec(Interface) |
||
| 765 | mock_event.content['interface'] = mock_interface |
||
| 766 | self.napp.handle_interface_up(mock_event) |
||
| 767 | mock_notify_topology_update.assert_called() |
||
| 768 | mock_instance_metadata.assert_called() |
||
| 769 | |||
| 770 | @patch('napps.kytos.topology.main.Main.handle_interface_up') |
||
| 771 | def test_handle_interface_created(self, mock_handle_interface_up): |
||
| 772 | """Test handle interface created.""" |
||
| 773 | mock_event = MagicMock() |
||
| 774 | self.napp.handle_interface_created(mock_event) |
||
| 775 | mock_handle_interface_up.assert_called() |
||
| 776 | |||
| 777 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
||
| 778 | @patch('napps.kytos.topology.main.Main.handle_interface_link_down') |
||
| 779 | def test_handle_interface_down(self, *args): |
||
| 780 | """Test handle interface down.""" |
||
| 781 | (mock_handle_interface_link_down, mock_notify_topology_update) = args |
||
| 782 | mock_event = MagicMock() |
||
| 783 | mock_interface = create_autospec(Interface) |
||
| 784 | mock_event.content['interface'] = mock_interface |
||
| 785 | self.napp.handle_interface_down(mock_event) |
||
| 786 | mock_handle_interface_link_down.assert_called() |
||
| 787 | mock_notify_topology_update.assert_called() |
||
| 788 | |||
| 789 | @patch('napps.kytos.topology.main.Main.handle_interface_down') |
||
| 790 | def test_interface_deleted(self, mock_handle_interface_link_down): |
||
| 791 | """Test interface deleted.""" |
||
| 792 | mock_event = MagicMock() |
||
| 793 | self.napp.handle_interface_deleted(mock_event) |
||
| 794 | mock_handle_interface_link_down.assert_called() |
||
| 795 | |||
| 796 | @patch('napps.kytos.topology.main.Main._get_link_from_interface') |
||
| 797 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
||
| 798 | @patch('napps.kytos.topology.main.Main.update_instance_metadata') |
||
| 799 | @patch('napps.kytos.topology.main.Main.notify_link_status_change') |
||
| 800 | def test_interface_link_up(self, *args): |
||
| 801 | """Test interface link_up.""" |
||
| 802 | (mock_status_change, mock_instance_metadata, mock_topology_update, |
||
| 803 | mock_link_from_interface) = args |
||
| 804 | |||
| 805 | now = time.time() |
||
| 806 | mock_event = MagicMock() |
||
| 807 | mock_interface_a = create_autospec(Interface) |
||
| 808 | mock_interface_a.is_active.return_value = False |
||
| 809 | mock_interface_b = create_autospec(Interface) |
||
| 810 | mock_interface_b.is_active.return_value = True |
||
| 811 | mock_link = create_autospec(Link) |
||
| 812 | mock_link.get_metadata.return_value = now |
||
| 813 | mock_link.is_active.side_effect = [False, True] |
||
| 814 | mock_link.endpoint_a = mock_interface_a |
||
| 815 | mock_link.endpoint_b = mock_interface_b |
||
| 816 | mock_link_from_interface.return_value = mock_link |
||
| 817 | content = {'interface': mock_interface_a} |
||
| 818 | mock_event.content = content |
||
| 819 | self.napp.link_up_timer = 1 |
||
| 820 | self.napp.handle_interface_link_up(mock_event) |
||
| 821 | mock_topology_update.assert_called() |
||
| 822 | mock_instance_metadata.assert_called() |
||
| 823 | mock_status_change.assert_called() |
||
| 824 | |||
| 825 | @patch('napps.kytos.topology.main.Main._get_link_from_interface') |
||
| 826 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
||
| 827 | @patch('napps.kytos.topology.main.Main.notify_link_status_change') |
||
| 828 | def test_interface_link_down(self, *args): |
||
| 829 | """Test interface link down.""" |
||
| 830 | (mock_status_change, mock_topology_update, |
||
| 831 | mock_link_from_interface) = args |
||
| 832 | |||
| 833 | mock_event = MagicMock() |
||
| 834 | mock_interface = create_autospec(Interface) |
||
| 835 | mock_link = create_autospec(Link) |
||
| 836 | mock_link.is_active.return_value = True |
||
| 837 | mock_link_from_interface.return_value = mock_link |
||
| 838 | mock_event.content['interface'] = mock_interface |
||
| 839 | self.napp.handle_interface_link_down(mock_event) |
||
| 840 | mock_topology_update.assert_called() |
||
| 841 | mock_status_change.assert_called() |
||
| 842 | |||
| 843 | @patch('napps.kytos.topology.main.Main._get_link_or_create') |
||
| 844 | @patch('napps.kytos.topology.main.Main.notify_topology_update') |
||
| 845 | def test_add_links(self, *args): |
||
| 846 | """Test add_links.""" |
||
| 847 | (mock_notify_topology_update, mock_get_link_or_create) = args |
||
| 848 | mock_event = MagicMock() |
||
| 849 | self.napp.add_links(mock_event) |
||
| 850 | mock_get_link_or_create.assert_called() |
||
| 851 | mock_notify_topology_update.assert_called() |
||
| 852 | |||
| 853 | @patch('napps.kytos.topology.main.Main._get_switches_dict') |
||
| 854 | @patch('napps.kytos.topology.main.StoreHouse.save_status') |
||
| 855 | def test_save_status_on_store(self, *args): |
||
| 856 | """Test save_status_on_storehouse.""" |
||
| 857 | (mock_save_status, mock_get_switches_dict) = args |
||
| 858 | self.napp.save_status_on_storehouse() |
||
| 859 | mock_get_switches_dict.assert_called() |
||
| 860 | mock_save_status.assert_called() |
||
| 861 | |||
| 862 | @patch('napps.kytos.topology.main.KytosEvent') |
||
| 863 | @patch('kytos.core.buffers.KytosEventBuffer.put') |
||
| 864 | def test_notify_topology_update(self, *args): |
||
| 865 | """Test notify_topology_update.""" |
||
| 866 | (mock_buffers_put, mock_event) = args |
||
| 867 | self.napp.notify_topology_update() |
||
| 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_link_status_change(self, *args): |
||
| 874 | """Test notify link status change.""" |
||
| 875 | (mock_buffers_put, mock_event) = args |
||
| 876 | mock_link = create_autospec(Link) |
||
| 877 | self.napp.notify_link_status_change(mock_link) |
||
| 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 | @patch('napps.kytos.topology.main.isinstance') |
||
| 884 | def test_notify_metadata_changes(self, *args): |
||
| 885 | """Test notify metadata changes.""" |
||
| 886 | (mock_isinstance, mock_buffers_put, mock_event) = args |
||
| 887 | mock_isinstance.return_value = True |
||
| 888 | mock_obj = MagicMock() |
||
| 889 | mock_action = create_autospec(Switch) |
||
| 890 | self.napp.notify_metadata_changes(mock_obj, mock_action) |
||
| 891 | mock_event.assert_called() |
||
| 892 | mock_isinstance.assert_called() |
||
| 893 | mock_buffers_put.assert_called() |
||
| 894 | |||
| 895 | @patch('napps.kytos.topology.main.KytosEvent') |
||
| 896 | @patch('kytos.core.buffers.KytosEventBuffer.put') |
||
| 897 | def test_notify_port_created(self, *args): |
||
| 898 | """Test notify port created.""" |
||
| 899 | (mock_buffers_put, mock_kytos_event) = args |
||
| 900 | mock_event = MagicMock() |
||
| 901 | self.napp.notify_port_created(mock_event) |
||
| 902 | mock_kytos_event.assert_called() |
||
| 903 | mock_buffers_put.assert_called() |
||
| 904 | |||
| 905 | @patch('napps.kytos.topology.main.KytosEvent') |
||
| 906 | @patch('kytos.core.buffers.KytosEventBuffer.put') |
||
| 907 | def test_save_metadata_on_store(self, *args): |
||
| 908 | """Test test_save_metadata_on_store.""" |
||
| 909 | (mock_buffers_put, mock_kytos_event) = args |
||
| 910 | mock_event = MagicMock() |
||
| 911 | mock_switch = MagicMock() |
||
| 912 | mock_interface = MagicMock() |
||
| 913 | mock_link = MagicMock() |
||
| 914 | self.napp.store_items = {'switches': mock_switch, |
||
| 915 | 'interfaces': mock_interface, |
||
| 916 | 'links': mock_link} |
||
| 917 | # test switches |
||
| 918 | mock_event.content = {'switch': mock_switch} |
||
| 919 | self.napp.save_metadata_on_store(mock_event) |
||
| 920 | mock_kytos_event.assert_called() |
||
| 921 | mock_buffers_put.assert_called() |
||
| 922 | |||
| 923 | # test interfaces |
||
| 924 | mock_event.content = {'interface': mock_interface} |
||
| 925 | self.napp.save_metadata_on_store(mock_event) |
||
| 926 | mock_kytos_event.assert_called() |
||
| 927 | mock_buffers_put.assert_called() |
||
| 928 | |||
| 929 | # test link |
||
| 930 | mock_event.content = {'link': mock_link} |
||
| 931 | self.napp.save_metadata_on_store(mock_event) |
||
| 932 | mock_kytos_event.assert_called() |
||
| 933 | mock_buffers_put.assert_called() |
||
| 934 | |||
| 935 | @patch('napps.kytos.topology.main.KytosEvent') |
||
| 936 | @patch('kytos.core.buffers.KytosEventBuffer.put') |
||
| 937 | def test_verify_storehouse(self, *args): |
||
| 938 | """Test verify_storehouse.""" |
||
| 939 | (mock_buffers_put, mock_kytos_event) = args |
||
| 940 | mock_entities = MagicMock() |
||
| 941 | self.napp.verify_storehouse(mock_entities) |
||
| 942 | mock_buffers_put.assert_called() |
||
| 943 | mock_kytos_event.assert_called() |
||
| 944 | |||
| 945 | @patch('napps.kytos.topology.main.KytosEvent') |
||
| 946 | @patch('kytos.core.buffers.KytosEventBuffer.put') |
||
| 947 | def test_request_retrieve_entities(self, *args): |
||
| 948 | """Test retrive_entities.""" |
||
| 949 | (mock_buffers_put, mock_kytos_event) = args |
||
| 950 | mock_event = MagicMock() |
||
| 951 | mock_data = MagicMock() |
||
| 952 | mock_error = MagicMock() |
||
| 953 | mock_event.content = {"namespace": "test_box"} |
||
| 954 | self.napp.request_retrieve_entities(mock_event, mock_data, mock_error) |
||
| 955 | mock_kytos_event.assert_called() |
||
| 956 | mock_buffers_put.assert_called() |
||
| 957 | |||
| 958 | self.napp.request_retrieve_entities(mock_event, None, mock_error) |
||
| 959 | mock_kytos_event.assert_called() |
||
| 960 | mock_buffers_put.assert_called() |
||
| 961 | |||
| 962 | @patch('napps.kytos.topology.main.Main.notify_link_status_change') |
||
| 963 | def test_handle_link_maintenance_start(self, status_change_mock): |
||
| 964 | """Test handle_link_maintenance_start.""" |
||
| 965 | link1 = MagicMock() |
||
| 966 | link1.id = 2 |
||
| 967 | link2 = MagicMock() |
||
| 968 | link2.id = 3 |
||
| 969 | link3 = MagicMock() |
||
| 970 | link3.id = 4 |
||
| 971 | content = {'links': [link1, link2]} |
||
| 972 | event = MagicMock() |
||
| 973 | event.content = content |
||
| 974 | self.napp.links = {2: link1, 4: link3} |
||
| 975 | self.napp.handle_link_maintenance_start(event) |
||
| 976 | status_change_mock.assert_called_once_with(link1) |
||
| 977 | |||
| 978 | @patch('napps.kytos.topology.main.Main.notify_link_status_change') |
||
| 979 | def test_handle_link_maintenance_end(self, status_change_mock): |
||
| 980 | """Test handle_link_maintenance_end.""" |
||
| 981 | link1 = MagicMock() |
||
| 982 | link1.id = 2 |
||
| 983 | link2 = MagicMock() |
||
| 984 | link2.id = 3 |
||
| 985 | link3 = MagicMock() |
||
| 986 | link3.id = 4 |
||
| 987 | content = {'links': [link1, link2]} |
||
| 988 | event = MagicMock() |
||
| 989 | event.content = content |
||
| 990 | self.napp.links = {2: link1, 4: link3} |
||
| 991 | self.napp.handle_link_maintenance_end(event) |
||
| 992 | status_change_mock.assert_called_once_with(link1) |
||
| 993 | |||
| 994 | View Code Duplication | @patch('napps.kytos.topology.main.Main.handle_link_down') |
|
| 995 | def test_handle_switch_maintenance_start(self, handle_link_down_mock): |
||
| 996 | """Test handle_switch_maintenance_start.""" |
||
| 997 | switch1 = MagicMock() |
||
| 998 | interface1 = MagicMock() |
||
| 999 | interface1.is_active.return_value = True |
||
| 1000 | interface2 = MagicMock() |
||
| 1001 | interface2.is_active.return_value = False |
||
| 1002 | interface3 = MagicMock() |
||
| 1003 | interface3.is_active.return_value = True |
||
| 1004 | switch1.interfaces = {1: interface1, 2: interface2, 3: interface3} |
||
| 1005 | switch2 = MagicMock() |
||
| 1006 | interface4 = MagicMock() |
||
| 1007 | interface4.is_active.return_value = False |
||
| 1008 | interface5 = MagicMock() |
||
| 1009 | interface5.is_active.return_value = True |
||
| 1010 | switch2.interfaces = {1: interface4, 2: interface5} |
||
| 1011 | content = {'switches': [switch1, switch2]} |
||
| 1012 | event = MagicMock() |
||
| 1013 | event.content = content |
||
| 1014 | self.napp.handle_switch_maintenance_start(event) |
||
| 1015 | self.assertEqual(handle_link_down_mock.call_count, 3) |
||
| 1016 | |||
| 1017 | View Code Duplication | @patch('napps.kytos.topology.main.Main.handle_link_up') |
|
| 1018 | def test_handle_switch_maintenance_end(self, handle_link_up_mock): |
||
| 1019 | """Test handle_switch_maintenance_end.""" |
||
| 1020 | switch1 = MagicMock() |
||
| 1021 | interface1 = MagicMock() |
||
| 1022 | interface1.is_active.return_value = True |
||
| 1023 | interface2 = MagicMock() |
||
| 1024 | interface2.is_active.return_value = False |
||
| 1025 | interface3 = MagicMock() |
||
| 1026 | interface3.is_active.return_value = True |
||
| 1027 | switch1.interfaces = {1: interface1, 2: interface2, 3: interface3} |
||
| 1028 | switch2 = MagicMock() |
||
| 1029 | interface4 = MagicMock() |
||
| 1030 | interface4.is_active.return_value = False |
||
| 1031 | interface5 = MagicMock() |
||
| 1032 | interface5.is_active.return_value = True |
||
| 1033 | switch2.interfaces = {1: interface4, 2: interface5} |
||
| 1034 | content = {'switches': [switch1, switch2]} |
||
| 1035 | event = MagicMock() |
||
| 1036 | event.content = content |
||
| 1037 | self.napp.handle_switch_maintenance_end(event) |
||
| 1038 | self.assertEqual(handle_link_up_mock.call_count, 5) |
||
| 1039 |