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