def test_handle_link_up_case_4(self, *args): """Test if not path is found a dynamic path is used.""" (_install_uni_flows_mocked, _install_nni_flows_mocked, get_best_path_mocked, deploy_to_path_mocked) = args deploy_to_path_mocked.return_value = True primary_path = [ get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, metadata={"s_vlan": 5}, status=EntityStatus.UP), get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, metadata={"s_vlan": 6}, status=EntityStatus.DOWN), ] backup_path = [ get_link_mocked(endpoint_a_port=13, endpoint_b_port=14, metadata={"s_vlan": 5}, status=EntityStatus.DOWN), get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, metadata={"s_vlan": 6}, status=EntityStatus.DOWN), ] # Setup best_path mock best_path = Path() best_path.append(primary_path[0]) get_best_path_mocked.return_value = best_path attributes = { "controller": get_controller_mock(), "name": "circuit_name", "uni_a": get_uni_mocked(is_valid=True), "uni_z": get_uni_mocked(is_valid=True), "primary_path": primary_path, "backup_path": backup_path, "enabled": True, "dynamic_backup_path": True } evc = EVC(**attributes) evc.current_path = Path([]) # storehouse mock evc._storehouse.box = Mock() # pylint: disable=protected-access evc._storehouse.box.data = {} # pylint: disable=protected-access current_handle_link_up = evc.handle_link_up(backup_path[0]) self.assertEqual(get_best_path_mocked.call_count, 0) self.assertEqual(deploy_to_path_mocked.call_count, 1) deploy_to_path_mocked.assert_called_once_with() self.assertTrue(current_handle_link_up)
def test_link_affected_by_interface_2(self): """Test method to get the link using an interface.""" link1 = Mock() link1.endpoint_a = "a" link1.endpoint_b = "b" link2 = Mock() link2.endpoint_a = "c" link2.endpoint_b = "d" path = Path([link1, link2]) self.assertEqual(path.link_affected_by_interface("a"), link1)
def test_as_dict(self): """Test path as dict.""" links = [ get_link_mocked(link_dict={"id": 3}), get_link_mocked(link_dict={"id": 2}) ] current_path = Path(links) expected_dict = [{"id": 3}, {"id": 2}] self.assertEqual(expected_dict, current_path.as_dict())
def test_link_affected_by_interface_2(self): """Test method to get the link using an interface.""" link1 = Mock() link1.endpoint_a = 'a' link1.endpoint_b = 'b' link2 = Mock() link2.endpoint_a = 'c' link2.endpoint_b = 'd' path = Path([link1, link2]) self.assertEqual(path.link_affected_by_interface('a'), link1)
def test_compare_same_paths(self): """Test compare paths with same links.""" links = [ get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, metadata={"s_vlan": 5}), get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, metadata={"s_vlan": 6}) ] path_1 = Path(links) path_2 = Path(links) self.assertEqual(path_1, path_2)
def test_deploy_successfully(self, *args): """Test if all methods to deploy are called.""" # pylint: disable=too-many-locals (should_deploy_mock, activate_mock, install_uni_flows_mock, install_nni_flows, chose_vlans_mock, log_mock) = args should_deploy_mock.return_value = True uni_a = get_uni_mocked(interface_port=2, tag_value=82, switch_id="switch_uni_a", is_valid=True) uni_z = get_uni_mocked(interface_port=3, tag_value=83, switch_id="switch_uni_z", is_valid=True) primary_links = [ get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, metadata={"s_vlan": 5}), get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, metadata={"s_vlan": 6}) ] attributes = { "controller": get_controller_mock(), "name": "custom_name", "uni_a": uni_a, "uni_z": uni_z, "primary_links": primary_links } # Setup path to deploy path = Path() path.append(primary_links[0]) path.append(primary_links[1]) evc = EVC(**attributes) # storehouse mock evc._storehouse.box = Mock() # pylint: disable=protected-access evc._storehouse.box.data = {} # pylint: disable=protected-access deployed = evc.deploy_to_path(path) self.assertEqual(should_deploy_mock.call_count, 1) self.assertEqual(activate_mock.call_count, 1) self.assertEqual(install_uni_flows_mock.call_count, 1) self.assertEqual(install_nni_flows.call_count, 1) self.assertEqual(chose_vlans_mock.call_count, 1) log_mock.info.assert_called_once_with(f"{evc} was deployed.") self.assertTrue(deployed)
def load_evcs(self, event): """Try to load the unloaded EVCs from storehouse.""" with self._lock: log.debug("Event load_evcs %s", event) circuits = self.storehouse.get_data() if not self._circuits_by_interface: self.load_circuits_by_interface(circuits) interface_id = '{}:{}'.format(event.content['switch'], event.content['port']) for circuit_id in self._circuits_by_interface.get( interface_id, []): if circuit_id in circuits and circuit_id not in self.circuits: try: evc = self._evc_from_dict(circuits[circuit_id]) except ValueError as exception: log.error(f'Could not load EVC {circuit_id} ' f'because {exception}') continue evc.deactivate() evc.current_path = Path([]) evc.sync() self.circuits.setdefault(circuit_id, evc) self.sched.add(evc)
def test_compare_different_paths(self): """Test compare paths with different links.""" links_1 = [ get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, metadata={"s_vlan": 5}), get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, metadata={"s_vlan": 6}) ] links_2 = [ get_link_mocked(endpoint_a_port=12, endpoint_b_port=11, metadata={"s_vlan": 5}), get_link_mocked(endpoint_a_port=14, endpoint_b_port=16, metadata={"s_vlan": 11}) ] path_1 = Path(links_1) path_2 = Path(links_2) self.assertNotEqual(path_1, path_2)
def test_status_case_4(self, requests_mocked): # pylint: disable=unused-argument """Test if link status is UP.""" link1 = get_link_mocked() link2 = get_link_mocked() link1.id = 'def' link2.id = 'abc' links = [link1, link2] current_path = Path(links) self.assertEqual(current_path.status, EntityStatus.UP)
def test_is_valid(self): """Test is_valid method.""" switch1 = "00:00:00:00:00:00:00:01" switch2 = "00:00:00:00:00:00:00:02" switch3 = "00:00:00:00:00:00:00:03" switch4 = "00:00:00:00:00:00:00:04" switch5 = "00:00:00:00:00:00:00:05" switch6 = "00:00:00:00:00:00:00:06" links1 = [ get_link_mocked(switch_a=switch1, switch_b=switch2), get_link_mocked(switch_a=switch2, switch_b=switch3), get_link_mocked(switch_a=switch3, switch_b=switch4), get_link_mocked(switch_a=switch4, switch_b=switch5), get_link_mocked(switch_a=switch5, switch_b=switch6), ] links2 = [ get_link_mocked(switch_a=switch1, switch_b=switch2), get_link_mocked(switch_a=switch3, switch_b=switch2), get_link_mocked(switch_a=switch3, switch_b=switch4), ] for links, switch_a, switch_z, expected in ( (links1, switch1, switch6, True), (links2, switch1, switch4, False), (links1, switch2, switch6, False), ): with self.subTest( links=links, switch_a=switch_a, switch_z=switch_z, expected=expected, ): path = Path(links) if expected: self.assertEqual(path.is_valid(switch_a, switch_z), expected) else: with self.assertRaises(InvalidPath): path.is_valid(switch_a, switch_z)
def test_handle_link_up_case_3(self, _install_uni_flows_mocked, _install_nni_flows_mocked, get_best_path_mocked, deploy_to_path_mocked, deploy_mocked): """Test if it is deployed after the backup is up.""" deploy_mocked.return_value = True deploy_to_path_mocked.return_value = True primary_path = [ get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, metadata={"s_vlan": 5}, status=EntityStatus.DOWN), get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, metadata={"s_vlan": 6}, status=EntityStatus.UP), ] backup_path = [ get_link_mocked(endpoint_a_port=13, endpoint_b_port=14, metadata={"s_vlan": 5}, status=EntityStatus.DOWN), get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, metadata={"s_vlan": 6}, status=EntityStatus.UP), ] attributes = { "controller": get_controller_mock(), "name": "circuit_name", "uni_a": get_uni_mocked(is_valid=True), "uni_z": get_uni_mocked(is_valid=True), "primary_path": primary_path, "backup_path": backup_path, "enabled": True, "dynamic_backup_path": True } evc = EVC(**attributes) # storehouse initialization mock evc._storehouse.box = Mock() # pylint: disable=protected-access evc._storehouse.box.data = {} # pylint: disable=protected-access evc.current_path = Path([]) current_handle_link_up = evc.handle_link_up(backup_path[0]) self.assertEqual(get_best_path_mocked.call_count, 0) self.assertEqual(deploy_mocked.call_count, 0) self.assertEqual(deploy_to_path_mocked.call_count, 1) deploy_to_path_mocked.assert_called_once_with(evc.backup_path) self.assertTrue(current_handle_link_up)
def test_deploy_without_path_case1(self, *args): """Test if not path is found a dynamic path is used.""" # pylint: disable=too-many-locals (discover_new_path_mocked, should_deploy_mock, activate_mock, install_uni_flows_mock, install_nni_flows, chose_vlans_mock, log_mock) = args should_deploy_mock.return_value = False uni_a = get_uni_mocked(interface_port=2, tag_value=82, switch_id="switch_uni_a", is_valid=True) uni_z = get_uni_mocked(interface_port=3, tag_value=83, switch_id="switch_uni_z", is_valid=True) attributes = { "controller": get_controller_mock(), "name": "custom_name", "uni_a": uni_a, "uni_z": uni_z, "enabled": True, "dynamic_backup_path": True } dynamic_backup_path = Path([ get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, metadata={"s_vlan": 5}), get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, metadata={"s_vlan": 6}) ]) evc = EVC(**attributes) discover_new_path_mocked.return_value = dynamic_backup_path # storehouse initialization mock evc._storehouse.box = Mock() # pylint: disable=protected-access evc._storehouse.box.data = {} # pylint: disable=protected-access deployed = evc.deploy_to_path() self.assertEqual(should_deploy_mock.call_count, 1) self.assertEqual(discover_new_path_mocked.call_count, 1) self.assertEqual(activate_mock.call_count, 1) self.assertEqual(install_uni_flows_mock.call_count, 1) self.assertEqual(install_nni_flows.call_count, 1) self.assertEqual(chose_vlans_mock.call_count, 1) self.assertEqual(log_mock.info.call_count, 1) self.assertTrue(deployed)
def _evc_dict_with_instances(self, evc_dict): """Convert some dict values to instance of EVC classes. This method will convert: [UNI, Link] """ data = evc_dict.copy() # Do not modify the original dict for attribute, value in data.items(): # Get multiple attributes. # Ex: uni_a, uni_z if "uni" in attribute: try: data[attribute] = self._uni_from_dict(value) except ValueError: result = "Error creating UNI: Invalid value" raise BadRequest(result) from BadRequest if attribute == "circuit_scheduler": data[attribute] = [] for schedule in value: data[attribute].append(CircuitSchedule.from_dict(schedule)) # Get multiple attributes. # Ex: primary_links, # backup_links, # current_links_cache, # primary_links_cache, # backup_links_cache if "links" in attribute: data[attribute] = [ self._link_from_dict(link) for link in value ] # Ex: current_path, # primary_path, # backup_path if "path" in attribute and attribute != "dynamic_backup_path": data[attribute] = Path( [self._link_from_dict(link) for link in value] ) return data
def _evc_dict_with_instances(self, evc_dict): """Convert some dict values to instance of EVC classes. This method will convert: [UNI, Link] """ data = evc_dict.copy() # Do not modify the original dict for attribute, value in data.items(): # Get multiple attributes. # Ex: uni_a, uni_z if 'uni' in attribute: try: data[attribute] = self._uni_from_dict(value) except ValueError as exc: raise ValueError(f'Error creating UNI: {exc}') if attribute == 'circuit_scheduler': data[attribute] = [] for schedule in value: data[attribute].append(CircuitSchedule.from_dict(schedule)) # Get multiple attributes. # Ex: primary_links, # backup_links, # current_links_cache, # primary_links_cache, # backup_links_cache if 'links' in attribute: data[attribute] = [ self._link_from_dict(link) for link in value ] # Get multiple attributes. # Ex: current_path, # primary_path, # backup_path if 'path' in attribute and attribute != 'dynamic_backup_path': data[attribute] = Path( [self._link_from_dict(link) for link in value]) return data
def test_status_case_3(self): """Test if link status is DISABLED.""" links = [] current_path = Path(links) self.assertEqual(current_path.status, EntityStatus.DISABLED)
def test_status_case_1(self): """Test if empty link is DISABLED.""" current_path = Path() self.assertEqual(current_path.status, EntityStatus.DISABLED)
def test_deploy_error(self, *args): """Test if all methods is ignored when the should_deploy is false.""" # pylint: disable=too-many-locals ( sync_mock, remove_current_flows, should_deploy_mock, install_nni_flows, choose_vlans_mock, discover_new_paths, log_mock, ) = args install_nni_flows.side_effect = FlowModException should_deploy_mock.return_value = True uni_a = get_uni_mocked( interface_port=2, tag_value=82, switch_id="switch_uni_a", is_valid=True, ) uni_z = get_uni_mocked( interface_port=3, tag_value=83, switch_id="switch_uni_z", is_valid=True, ) primary_links = [ get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, metadata={"s_vlan": 5}), get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, metadata={"s_vlan": 6}), ] attributes = { "controller": get_controller_mock(), "name": "custom_name", "uni_a": uni_a, "uni_z": uni_z, "primary_links": primary_links, "queue_id": 5, } # Setup path to deploy path = Path() path.append(primary_links[0]) path.append(primary_links[1]) evc = EVC(**attributes) deployed = evc.deploy_to_path(path) self.assertEqual(discover_new_paths.call_count, 0) self.assertEqual(should_deploy_mock.call_count, 1) self.assertEqual(install_nni_flows.call_count, 1) self.assertEqual(choose_vlans_mock.call_count, 1) self.assertEqual(log_mock.error.call_count, 1) self.assertEqual(sync_mock.call_count, 0) self.assertEqual(remove_current_flows.call_count, 2) self.assertFalse(deployed)
def test_deploy_without_path_case1(self, *args): """Test if not path is found a dynamic path is used.""" # pylint: disable=too-many-locals ( discover_new_paths_mocked, should_deploy_mock, activate_mock, install_uni_flows_mock, install_nni_flows, chose_vlans_mock, log_mock, _, requests_mock, ) = args response = MagicMock() response.status_code = 201 requests_mock.return_value = response should_deploy_mock.return_value = False uni_a = get_uni_mocked( interface_port=2, tag_value=82, switch_id="switch_uni_a", is_valid=True, ) uni_z = get_uni_mocked( interface_port=3, tag_value=83, switch_id="switch_uni_z", is_valid=True, ) attributes = { "controller": get_controller_mock(), "name": "custom_name", "uni_a": uni_a, "uni_z": uni_z, "enabled": True, "dynamic_backup_path": False, } dynamic_backup_path = Path([ get_link_mocked( endpoint_a_port=9, endpoint_b_port=10, metadata={"s_vlan": 5}, ), get_link_mocked( endpoint_a_port=11, endpoint_b_port=12, metadata={"s_vlan": 6}, ), ]) evc = EVC(**attributes) discover_new_paths_mocked.return_value = [dynamic_backup_path] deployed = evc.deploy_to_path() self.assertEqual(should_deploy_mock.call_count, 1) self.assertEqual(discover_new_paths_mocked.call_count, 1) self.assertEqual(activate_mock.call_count, 1) self.assertEqual(install_uni_flows_mock.call_count, 1) self.assertEqual(install_nni_flows.call_count, 1) self.assertEqual(chose_vlans_mock.call_count, 1) log_mock.info.assert_called_with(f"{evc} was deployed.") self.assertTrue(deployed)
def test_is_affected_by_link_1(self): """Test method is affected by link.""" path = Path() self.assertIs(path.is_affected_by_link(), False)