def test_create_delete_diff_switch_same_host(self): """Test create/delete of two Ports, diff switch/same host.""" add_port2_driver_result = [ test_cisco_nexus_base.RESULT_ADD_VLAN.format(268), test_cisco_nexus_base.RESULT_ADD_INTERFACE.format("portchannel", "2", 268), test_cisco_nexus_base.RESULT_ADD_VLAN.format(268), test_cisco_nexus_base.RESULT_ADD_INTERFACE.format("portchannel", "2", 268), ] delete_port2_driver_result = [ test_cisco_nexus_base.RESULT_DEL_INTERFACE.format("portchannel", "2", 268), test_cisco_nexus_base.RESULT_DEL_VLAN.format(268), test_cisco_nexus_base.RESULT_DEL_INTERFACE.format("portchannel", "2", 268), test_cisco_nexus_base.RESULT_DEL_VLAN.format(268), ] self._basic_create_verify_port_vlan("test_config_portchannel2", add_port2_driver_result) # Verify there are 2 port configs. One per switch. bindings = nexus_db_v2.get_nexusport_switch_bindings(test_cisco_nexus_base.NEXUS_IP_ADDRESS_6) self.assertEqual(1, len(bindings)) bindings = nexus_db_v2.get_nexusport_switch_bindings(test_cisco_nexus_base.NEXUS_IP_ADDRESS_7) self.assertEqual(1, len(bindings)) # Clean all the ncclient mock_calls so we can evaluate # results of delete operations. self.mock_ncclient.reset_mock() # For results, pass empty list to verify no nexus action on # first port removal. self._basic_delete_verify_port_vlan("test_config_portchannel2", delete_port2_driver_result)
def test_create_delete_duplicate_port_transaction(self): """Tests creation and deletion same port transaction.""" self._basic_create_verify_port_vlan( 'test_config1', self.duplicate_add_port_driver_result) self.assertEqual( 1, len( nexus_db_v2.get_nexusport_switch_bindings( test_cisco_nexus_base.NEXUS_IP_ADDRESS_1))) self._create_port(self.test_configs['test_config1']) self._verify_results(self.duplicate_add_port_driver_result) self.assertEqual( 1, len( nexus_db_v2.get_nexusport_switch_bindings( test_cisco_nexus_base.NEXUS_IP_ADDRESS_1))) # Clean all the ncclient mock_calls so we can evaluate # results of delete operations. self.mock_ncclient.reset_mock() self._basic_delete_verify_port_vlan( 'test_config1', self.duplicate_delete_port_driver_result, nbr_of_bindings=0) self._basic_delete_verify_port_vlan( 'test_config1', self.duplicate_delete_port_driver_result)
def test_replay_new_port_success_if_one_switch_up(self): """Verifies create port successful if one multi-switch up.""" # Make sure port is not rejected when there are multiple # switches and only one is active. port_cfg1 = self.test_configs['test_replay_dual'] self._cisco_mech_driver.set_switch_ip_and_active_state( port_cfg1.nexus_ip_addr, const.SWITCH_ACTIVE) port_cfg2 = self.test_configs['test_replay_dual2'] self._cisco_mech_driver.set_switch_ip_and_active_state( port_cfg2.nexus_ip_addr, const.SWITCH_INACTIVE) # Set-up successful creation of port vlan config result_add = ( [test_cisco_nexus_base.RESULT_ADD_VLAN.format(269), test_cisco_nexus_base.RESULT_ADD_INTERFACE. format('ethernet', '1\/3', 269)]) self._basic_create_verify_port_vlan('test_replay_dual', result_add, nbr_of_bindings=1) # Even though 2nd entry is inactive, there should be # a data base entry configured for it. # 2 = One entry for port the other for reserved binding assert(len(nexus_db_v2.get_nexusport_switch_bindings( port_cfg2.nexus_ip_addr)) == 2) # Clean-up the port entry result_del = ( [test_cisco_nexus_base.RESULT_DEL_INTERFACE. format('ethernet', '1\/3', 269), test_cisco_nexus_base.RESULT_DEL_VLAN.format(269)]) self._basic_delete_verify_port_vlan('test_replay_dual', result_del, nbr_of_bindings=0)
def test_replay_new_port_success_if_one_switch_up(self): """Verifies create port successful if one multi-switch up.""" # Make sure port is not rejected when there are multiple # switches and only one is active. port_cfg1 = self.test_configs['test_replay_dual'] self._cisco_mech_driver.set_switch_ip_and_active_state( port_cfg1.nexus_ip_addr, const.SWITCH_ACTIVE) port_cfg2 = self.test_configs['test_replay_dual2'] self._cisco_mech_driver.set_switch_ip_and_active_state( port_cfg2.nexus_ip_addr, const.SWITCH_INACTIVE) # Set-up successful creation of port vlan config result_add = ( [test_cisco_nexus_base.RESULT_ADD_VLAN.format(269), test_cisco_nexus_base.RESULT_ADD_INTERFACE. format('ethernet', '1\/3', 269)]) self._basic_create_verify_port_vlan('test_replay_dual', result_add, nbr_of_bindings=1) # Even though 2nd entry is inactive, there should be # a data base entry configured for it. # 2 = One entry for port the other for reserved binding assert(len(nexus_db_v2.get_nexusport_switch_bindings( port_cfg2.nexus_ip_addr)) == 2) # Clean-up the port entry result_del = ( [test_cisco_nexus_base.RESULT_DEL_INTERFACE. format('ethernet', '1\/3', 269), test_cisco_nexus_base.RESULT_DEL_VLAN.format(269)]) self._basic_delete_verify_port_vlan('test_replay_dual', result_del, nbr_of_bindings=0)
def _basic_create_verify_port_vlan(self, test_name, test_result, nbr_of_bindings=1, other_test=None): """Create port vlan and verify results.""" if other_test is None: other_test = self.test_configs[test_name] # Configure port entry config which puts switch in inactive state self._create_port(other_test) # Verify it's in the port binding data base # Add one to count for the reserved switch state entry # if replay is enabled port_cfg = other_test if self._cisco_mech_driver.is_replay_enabled(): nbr_of_bindings += 1 try: port_bindings = nexus_db_v2.get_nexusport_switch_bindings( port_cfg.nexus_ip_addr) except exceptions.NexusPortBindingNotFound: port_bindings = [] assert(len(port_bindings) == nbr_of_bindings) # Make sure there is only a single attempt to configure. self._verify_results(test_result) # Clean all the ncclient mock_calls to clear exception # and other mock_call history. self.mock_ncclient.reset_mock()
def _delete_port_failure(self, attr, match_str, test_case, test_id): """Verifies exception handling during object deletion. This method is a shared method to initiate an exception at various point of object deletion. The points of failure are identified by the caller which can be get operations or edit operations. When the mechanism replay is functioning, the exception should be suppressed and the switch is marked as inactive. attr: Which mock attribute to contain side_effect exception match_str: String for side_effect method to match for exception test_case: which configuration test case to run thru test test_id: String to put in the exception. """ # Set switch state to active switch_ip = self.test_configs[test_case].nexus_ip_addr self._cisco_mech_driver.set_switch_ip_and_active_state( switch_ip, const.SWITCH_ACTIVE) self._create_port(self.test_configs[test_case]) # _create_port should complete successfully and no switch state change. self.assertEqual( self._cisco_mech_driver.get_switch_ip_and_active_state(switch_ip), const.SWITCH_ACTIVE) # Clean all the ncclient mock_calls to clear exception # and other mock_call history. self.mock_ncclient.reset_mock() # Set up driver exception config = { attr: self._config_side_effects_on_count(match_str, Exception(test_id)) } self.mock_ncclient.configure_mock(**config) self.assertRaises(exceptions.NexusConfigFailed, self._delete_port, self.test_configs[test_case]) # Verify nothing in the port binding data base # except Reserved Port Binding if replay is enabled nbr_bindings = 1 if self._cisco_mech_driver.is_replay_enabled() else 0 try: port_bindings = nexus_db_v2.get_nexusport_switch_bindings( switch_ip) except exceptions.NexusPortBindingNotFound: port_bindings = [] assert (len(port_bindings) == nbr_bindings) # Verify nothing in the nve data base assert (len(nexus_db_v2.get_nve_switch_bindings(switch_ip)) == 0) # _delete_port should complete with no switch state change. self.assertEqual( self._cisco_mech_driver.get_switch_ip_and_active_state(switch_ip), const.SWITCH_ACTIVE)
def _basic_delete_verify_port_vlan(self, test_name, test_result, nbr_of_bindings=0, other_test=None): """Create port vlan and verify results.""" if other_test is None: other_test = self.test_configs[test_name] self._delete_port(other_test) # Verify port binding has been removed # Verify failure stats is not reset and # verify no driver transactions have been sent port_cfg = other_test if self._cisco_mech_driver.is_replay_enabled(): # Add one for the reserved switch state entry nbr_of_bindings += 1 try: port_bindings = nexus_db_v2.get_nexusport_switch_bindings( port_cfg.nexus_ip_addr) except exceptions.NexusPortBindingNotFound: port_bindings = [] assert(len(port_bindings) == nbr_of_bindings) # Make sure there is only a single attempt to configure. self._verify_results(test_result) # Clean all the ncclient mock_calls to clear exception # and other mock_call history. self.mock_ncclient.reset_mock()
def replay_config(self, switch_ip): """Sends pending config data in OpenStack to Nexus.""" LOG.debug("Replaying config for switch ip %(switch_ip)s", {'switch_ip': switch_ip}) nve_bindings = nxos_db.get_nve_switch_bindings(switch_ip) for x in nve_bindings: try: self._driver.create_nve_member(switch_ip, const.NVE_INT_NUM, x.vni, x.mcast_group) except Exception as e: LOG.error( _LE("Failed to configure nve_member for " "switch %(switch_ip)s, vni %(vni)s" "Reason:%(reason)s "), { 'switch_ip': switch_ip, 'vni': x.vni, 'reason': e }) if self.monitor_timeout > 0: self._mdriver.register_switch_as_inactive( switch_ip, 'replay create_nve_member') return try: port_bindings = nxos_db.get_nexusport_switch_bindings(switch_ip) except excep.NexusPortBindingNotFound: LOG.warn( _LW("No port entries found for switch ip " "%(switch_ip)s during replay."), {'switch_ip': switch_ip}) return self._mdriver.configure_switch_entries(switch_ip, port_bindings)
def test_create_delete_same_switch_diff_hosts_same_vlan(self): """Test create/delete two Ports, same switch & vlan/diff host.""" add_port2_driver_result = ([ test_cisco_nexus_base.RESULT_ADD_VLAN.format(267), test_cisco_nexus_base.RESULT_ADD_INTERFACE.format( 'ethernet', '1\/20', 267) ]) delete_port2_driver_result = ([ test_cisco_nexus_base.RESULT_DEL_INTERFACE.format( 'ethernet', '1\/20', 267) ]) self._basic_create_verify_port_vlan( 'test_config4', self.duplicate_add_port_driver_result) self._create_port(self.test_configs['test_config5']) self._verify_results(add_port2_driver_result) # Verify there are 2 port configs bindings = nexus_db_v2.get_nexusport_switch_bindings( test_cisco_nexus_base.NEXUS_IP_ADDRESS_8) self.assertEqual(2, len(bindings)) # Clean all the ncclient mock_calls so we can evaluate # results of delete operations. self.mock_ncclient.reset_mock() self._basic_delete_verify_port_vlan('test_config5', delete_port2_driver_result, nbr_of_bindings=1) self._basic_delete_verify_port_vlan( 'test_config4', self.duplicate_delete_port_driver_result)
def _basic_create_verify_port_vlan(self, test_name, test_result, nbr_of_bindings=1, other_test=None): """Create port vlan and verify results.""" if other_test is None: other_test = self.test_configs[test_name] # Configure port entry config which puts switch in inactive state self._create_port(other_test) # Verify it's in the port binding data base # Add one to count for the reserved switch state entry # if replay is enabled port_cfg = other_test if self._cisco_mech_driver.is_replay_enabled(): nbr_of_bindings += 1 try: port_bindings = nexus_db_v2.get_nexusport_switch_bindings( port_cfg.nexus_ip_addr) except exceptions.NexusPortBindingNotFound: port_bindings = [] self.assertEqual(nbr_of_bindings, len(port_bindings)) # Make sure there is only a single attempt to configure. self._verify_results(test_result) # Clean all the ncclient mock_calls to clear exception # and other mock_call history. self.mock_ncclient.reset_mock()
def _basic_delete_verify_port_vlan(self, test_name, test_result, nbr_of_bindings=0, other_test=None): """Create port vlan and verify results.""" if other_test is None: other_test = self.test_configs[test_name] self._delete_port(other_test) # Verify port binding has been removed # Verify failure stats is not reset and # verify no driver transactions have been sent port_cfg = other_test if self._cisco_mech_driver.is_replay_enabled(): # Add one for the reserved switch state entry nbr_of_bindings += 1 try: port_bindings = nexus_db_v2.get_nexusport_switch_bindings( port_cfg.nexus_ip_addr) except exceptions.NexusPortBindingNotFound: port_bindings = [] self.assertEqual(nbr_of_bindings, len(port_bindings)) # Make sure there is only a single attempt to configure. self._verify_results(test_result) # Clean all the ncclient mock_calls to clear exception # and other mock_call history. self.mock_ncclient.reset_mock()
def test_create_delete_same_switch_diff_hosts_same_vlan(self): """Test create/delete two Ports, same switch & vlan/diff host.""" add_port2_driver_result = [ test_cisco_nexus_base.RESULT_ADD_VLAN.format(267), test_cisco_nexus_base.RESULT_ADD_INTERFACE.format("ethernet", "1\/20", 267), ] delete_port2_driver_result = [test_cisco_nexus_base.RESULT_DEL_INTERFACE.format("ethernet", "1\/20", 267)] self._basic_create_verify_port_vlan("test_config4", self.duplicate_add_port_driver_result) self._create_port(self.test_configs["test_config5"]) self._verify_results(add_port2_driver_result) # Verify there are 2 port configs bindings = nexus_db_v2.get_nexusport_switch_bindings(test_cisco_nexus_base.NEXUS_IP_ADDRESS_8) self.assertEqual(2, len(bindings)) # Clean all the ncclient mock_calls so we can evaluate # results of delete operations. self.mock_ncclient.reset_mock() self._basic_delete_verify_port_vlan("test_config5", delete_port2_driver_result, nbr_of_bindings=1) self._basic_delete_verify_port_vlan("test_config4", self.duplicate_delete_port_driver_result)
def replay_config(self, switch_ip): """Sends pending config data in OpenStack to Nexus.""" LOG.debug("Replaying config for switch ip %(switch_ip)s", {'switch_ip': switch_ip}) nve_bindings = nxos_db.get_nve_switch_bindings(switch_ip) for x in nve_bindings: try: self._driver.create_nve_member(switch_ip, const.NVE_INT_NUM, x.vni, x.mcast_group) except Exception as e: LOG.error(_LE("Failed to configure nve_member for " "switch %(switch_ip)s, vni %(vni)s" "Reason:%(reason)s "), {'switch_ip': switch_ip, 'vni': x.vni, 'reason': e}) if self.monitor_timeout > 0: self._mdriver.register_switch_as_inactive(switch_ip, 'replay create_nve_member') return try: port_bindings = nxos_db.get_nexusport_switch_bindings(switch_ip) except excep.NexusPortBindingNotFound: LOG.warn(_LW("No port entries found for switch ip " "%(switch_ip)s during replay."), {'switch_ip': switch_ip}) return self._mdriver.configure_switch_entries(switch_ip, port_bindings)
def test_create_delete_duplicate_port_transaction(self): """Tests creation and deletion same port transaction.""" self._basic_create_verify_port_vlan("test_config1", self.duplicate_add_port_driver_result) self.assertEqual(1, len(nexus_db_v2.get_nexusport_switch_bindings(test_cisco_nexus_base.NEXUS_IP_ADDRESS_1))) self._create_port(self.test_configs["test_config1"]) self._verify_results(self.duplicate_add_port_driver_result) self.assertEqual(1, len(nexus_db_v2.get_nexusport_switch_bindings(test_cisco_nexus_base.NEXUS_IP_ADDRESS_1))) # Clean all the ncclient mock_calls so we can evaluate # results of delete operations. self.mock_ncclient.reset_mock() self._basic_delete_verify_port_vlan("test_config1", self.duplicate_delete_port_driver_result, nbr_of_bindings=0) self._basic_delete_verify_port_vlan("test_config1", self.duplicate_delete_port_driver_result)
def test_replay_port_success_if_one_switch_restored(self): """Verifies port restored after one of multi-switch restored.""" # Make sure port is not rejected when there are multiple # switches and one is active. Then proceed to bring-up # the other switch and it gets configured successfully. # Then remove all. port_cfg1 = self.test_configs['test_replay_dual'] self._cisco_mech_driver.set_switch_ip_and_active_state( port_cfg1.nexus_ip_addr, const.SWITCH_ACTIVE) port_cfg2 = self.test_configs['test_replay_dual2'] self._cisco_mech_driver.set_switch_ip_and_active_state( port_cfg2.nexus_ip_addr, const.SWITCH_INACTIVE) # Set-up successful creation of port vlan config result_add = ([ test_cisco_nexus_base.RESULT_ADD_VLAN.format(269), test_cisco_nexus_base.RESULT_ADD_INTERFACE.format( 'ethernet', '1\/3', 269) ]) self._basic_create_verify_port_vlan('test_replay_dual', result_add, nbr_of_bindings=1) # Even though 2nd entry is inactive, there should be # a data base entry configured for it. # 2 = One entry for port the other for reserved binding assert (len( nexus_db_v2.get_nexusport_switch_bindings( port_cfg2.nexus_ip_addr)) == 2) # Restore port data for that switch self._cfg_monitor.check_connections() result_replay = ([ test_cisco_nexus_base.RESULT_ADD_INTERFACE.format( 'ethernet', '1\/2', 269), test_cisco_nexus_base.RESULT_ADD_VLAN.format(269) ]) self._verify_results(result_replay) # Clear mock_call history. self.mock_ncclient.reset_mock() # Clean-up the port entries result_del = ([ test_cisco_nexus_base.RESULT_DEL_INTERFACE.format( 'ethernet', '1\/3', 269), test_cisco_nexus_base.RESULT_DEL_VLAN.format(269), test_cisco_nexus_base.RESULT_DEL_INTERFACE.format( 'ethernet', '1\/2', 269), test_cisco_nexus_base.RESULT_DEL_VLAN.format(269) ]) self._basic_delete_verify_port_vlan('test_replay_dual', result_del, nbr_of_bindings=0)
def test_create_delete_diff_switch_same_host(self): """Test create/delete of two Ports, diff switch/same host.""" add_port2_driver_result = ([ test_cisco_nexus_base.RESULT_ADD_VLAN.format(268), test_cisco_nexus_base.RESULT_ADD_INTERFACE.format( 'portchannel', '2', 268), test_cisco_nexus_base.RESULT_ADD_VLAN.format(268), test_cisco_nexus_base.RESULT_ADD_INTERFACE.format( 'portchannel', '2', 268) ]) delete_port2_driver_result = ([ test_cisco_nexus_base.RESULT_DEL_INTERFACE.format( 'portchannel', '2', 268), test_cisco_nexus_base.RESULT_DEL_VLAN.format(268), test_cisco_nexus_base.RESULT_DEL_INTERFACE.format( 'portchannel', '2', 268), test_cisco_nexus_base.RESULT_DEL_VLAN.format(268) ]) self._basic_create_verify_port_vlan('test_config_portchannel2', add_port2_driver_result) # Verify there are 2 port configs. One per switch. bindings = nexus_db_v2.get_nexusport_switch_bindings( test_cisco_nexus_base.NEXUS_IP_ADDRESS_6) self.assertEqual(1, len(bindings)) bindings = nexus_db_v2.get_nexusport_switch_bindings( test_cisco_nexus_base.NEXUS_IP_ADDRESS_7) self.assertEqual(1, len(bindings)) # Clean all the ncclient mock_calls so we can evaluate # results of delete operations. self.mock_ncclient.reset_mock() # For results, pass empty list to verify no nexus action on # first port removal. self._basic_delete_verify_port_vlan('test_config_portchannel2', delete_port2_driver_result)
def test_replay_port_success_if_one_switch_restored(self): """Verifies port restored after one of multi-switch restored.""" # Make sure port is not rejected when there are multiple # switches and one is active. Then proceed to bring-up # the other switch and it gets configured successfully. # Then remove all. port_cfg1 = self.test_configs['test_replay_dual'] self._cisco_mech_driver.set_switch_ip_and_active_state( port_cfg1.nexus_ip_addr, const.SWITCH_ACTIVE) port_cfg2 = self.test_configs['test_replay_dual2'] self._cisco_mech_driver.set_switch_ip_and_active_state( port_cfg2.nexus_ip_addr, const.SWITCH_INACTIVE) # Set-up successful creation of port vlan config result_add = ( [test_cisco_nexus_base.RESULT_ADD_VLAN.format(269), test_cisco_nexus_base.RESULT_ADD_INTERFACE. format('ethernet', '1\/3', 269)]) self._basic_create_verify_port_vlan('test_replay_dual', result_add, nbr_of_bindings=1) # Even though 2nd entry is inactive, there should be # a data base entry configured for it. # 2 = One entry for port the other for reserved binding assert(len(nexus_db_v2.get_nexusport_switch_bindings( port_cfg2.nexus_ip_addr)) == 2) # Restore port data for that switch self._cfg_monitor.check_connections() result_replay = ( [test_cisco_nexus_base.RESULT_ADD_INTERFACE. format('ethernet', '1\/2', 269), test_cisco_nexus_base.RESULT_ADD_VLAN.format(269)]) self._verify_results(result_replay) # Clear mock_call history. self.mock_ncclient.reset_mock() # Clean-up the port entries result_del = ( [test_cisco_nexus_base.RESULT_DEL_INTERFACE. format('ethernet', '1\/3', 269), test_cisco_nexus_base.RESULT_DEL_VLAN.format(269), test_cisco_nexus_base.RESULT_DEL_INTERFACE. format('ethernet', '1\/2', 269), test_cisco_nexus_base.RESULT_DEL_VLAN.format(269)]) self._basic_delete_verify_port_vlan('test_replay_dual', result_del, nbr_of_bindings=0)
def _basic_delete_verify_port_vlan(self, test_name, test_result, nbr_of_bindings=0, nbr_of_mappings=0, other_test=None): """Create port vlan and verify results.""" if other_test is None: other_test = self.test_configs[test_name] self._delete_port(other_test) # Verify port binding has been removed # Verify failure stats is not reset and # verify no driver transactions have been sent port_cfg = other_test ipaddrs = self._get_ip_addrs(port_cfg) bindings_found = 0 for ipaddr in ipaddrs: try: port_bindings = nexus_db_v2.get_nexusport_switch_bindings( ipaddr) bindings_found += len(port_bindings) if self._cisco_mech_driver.is_replay_enabled(): # Add one for the reserved switch state entry nbr_of_bindings += 1 except exceptions.NexusPortBindingNotFound: pass self.assertEqual(nbr_of_bindings, bindings_found) port_context = self._generate_port_context(other_test) if nexus_help.is_baremetal(port_context.current): connections = self._cisco_mech_driver._get_baremetal_connections( port_context.current, False, True) for switch_ip, intf_type, port, is_p_vlan, _ in connections: port_id = intf_type + ':' + port try: host_mapping = nexus_db_v2.get_switch_if_host_mappings( switch_ip, port_id) except exceptions.NexusHostMappingNotFound: host_mapping = [] self.assertEqual(nbr_of_mappings, len(host_mapping)) # Make sure there is only a single attempt to configure. self._verify_results(test_result) # Clean all the ncclient mock_calls to clear exception # and other mock_call history. self.mock_ncclient.reset_mock()
def test_replay_no_retry_failure_handling(self): """Tests to check replay 'no retry' failure handling. 1) Verify config_failure is incremented upon failure during replay config and verify create_vlan transactions are seen. 2) Verify contact_failure is incremented upon failure during get_nexus_type transaction. 3) Verify receipt of new transaction does not reset failure statistics. 4) Verify config&contact_failure is reset when replay is successful. """ # Due to 2 retries in driver to deal with stale ncclient # handle, the results are doubled. vlan267 = '<vlan-id-create-delete\>\s+\<__XML__PARAM_value\>267' driver_result2 = ([test_cisco_nexus_base.RESULT_ADD_INTERFACE. format('ethernet', '1\/10', 267)] + [vlan267] * 2) * 4 config_replay = MAX_REPLAY_COUNT port_cfg = self.test_configs['test_replay_unique1'] self._cisco_mech_driver.set_switch_ip_and_active_state( port_cfg.nexus_ip_addr, const.SWITCH_ACTIVE) # Set-up successful creation of port vlan config self._basic_create_verify_port_vlan('test_replay_unique1', self.driver_result_unique_add1) # Test 1: # Set the edit create vlan driver exception # Perform replay MAX_REPLAY_COUNT times # This should not roll-up an exception but merely quit # and increment FAIL_CONFIG statistics config = {'connect.return_value.edit_config.side_effect': self._config_side_effects_on_count( 'vlan-id-create-delete', Exception(__name__ + '1'))} self.mock_ncclient.configure_mock(**config) self._cisco_mech_driver.set_switch_ip_and_active_state( port_cfg.nexus_ip_addr, const.SWITCH_INACTIVE) for i in range(config_replay): self._cfg_monitor.check_connections() # Verify FAIL_CONFIG reached(MAX_REPLAY_COUNT) and there # were only MAX_REPLAY_COUNT+1 attempts to send create_vlan. # first is from test_replay_create_vlan_failure() # and MAX_REPLAY_COUNT from check_connections() assert(self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONFIG, test_cisco_nexus_base.NEXUS_IP_ADDRESS_1) == config_replay) self._verify_results(driver_result2) # Verify there exists a single port binding # plus 1 for reserved switch entry assert(len(nexus_db_v2.get_nexusport_switch_bindings( test_cisco_nexus_base.NEXUS_IP_ADDRESS_1)) == 2) # Clear mock_call history. self.mock_ncclient.reset_mock() # Clear the edit driver exception for next test. config = {'connect.return_value.edit_config.side_effect': None} self.mock_ncclient.configure_mock(**config) # Test 2) # Set it up so get nexus type returns exception. # FAIL_CONTACT should increment. self._set_nexus_type_failure() # Perform replay MAX_REPLAY_COUNT times # This should not roll-up an exception but merely quit for i in range(config_replay): self._cfg_monitor.check_connections() # Verify switch FAIL_CONTACT reached (MAX_REPLAY_COUNT) # and there were no attempts to send create_vlan. assert(self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONFIG, test_cisco_nexus_base.NEXUS_IP_ADDRESS_1) == config_replay) assert(self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONTACT, test_cisco_nexus_base.NEXUS_IP_ADDRESS_1) == config_replay) self._verify_results([]) # Test 3) # Verify delete transaction doesn't affect failure stats. self._basic_delete_verify_port_vlan('test_replay_unique1', []) # Verify failure stats is not reset assert(self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONFIG, test_cisco_nexus_base.NEXUS_IP_ADDRESS_1) == config_replay) assert(self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONTACT, test_cisco_nexus_base.NEXUS_IP_ADDRESS_1) == config_replay) # Clear the get nexus type driver exception. config = {'connect.return_value.get.side_effect': None} self.mock_ncclient.configure_mock(**config) # Test 4) # Verify config&contact_failure is reset when replay is # successful. # Perform replay once which will be successful causing # failure stats to be reset to 0. # Then verify these stats are indeed 0. self._cfg_monitor.check_connections() assert(self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONFIG, test_cisco_nexus_base.NEXUS_IP_ADDRESS_1) == 0) assert(self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONTACT, test_cisco_nexus_base.NEXUS_IP_ADDRESS_1) == 0) # Verify switch state is now active following successful replay. self.assertEqual( self._cisco_mech_driver.get_switch_ip_and_active_state( test_cisco_nexus_base.NEXUS_IP_ADDRESS_1), const.SWITCH_ACTIVE)
def test_replay_retry_handling(self): """Verifies a series of events to check retry_count operations. 1) Verify retry count is incremented upon failure during replay. 2) Verify further attempts to configure replay data stops. 3) Verify upon receipt of new transaction that retry count is reset to 0 so replay attempts will restart. """ unique_driver_result1 = [ '\<vlan\-name\>q\-267\<\/vlan\-name>', ] unique_driver_result2 = [ '\<vlan\-name\>q\-267\<\/vlan\-name>', '\<vlan\-name\>q\-267\<\/vlan\-name>', '\<vlan\-name\>q\-267\<\/vlan\-name>', '\<vlan\-name\>q\-267\<\/vlan\-name>', ] config_replay = cisco_config.cfg.CONF.ml2_cisco.switch_replay_count #Set-up failed config which puts switch in inactive state self.test_replay_create_vlan_failure() # Make sure there is only a single attempt to configure. self._verify_replay_results(unique_driver_result1) # Don't reset_mock so create_vlan continues failing # Perform replay 4 times to exceed retry count of 3. # This should not roll-up an exception but merely quit for i in range(config_replay + 1): self._cfg_monitor.check_connections() # Verify switch retry count reached configured max and # verify only 4 attempts to send create_vlan. # first is from test_replay_create_vlan_failure() # and only 3 from check_connections() assert (self._cisco_mech_driver.get_switch_retry_count( RP_NEXUS_IP_ADDRESS_1) == (config_replay + 1)) self._verify_replay_results(unique_driver_result2) # Clean all the ncclient mock_calls to clear exception # and other mock_call history. self.mock_ncclient.reset_mock() # Verify there exists a single port binding assert (len( nexus_db_v2.get_nexusport_switch_bindings(RP_NEXUS_IP_ADDRESS_1)) == 1) # Sent another config which should reset retry count # Verify replay results again self._delete_port( TestCiscoNexusReplay.test_configs['test_replay_unique1']) # Verify port binding has been removed # Verify switch retry count reset to 0 and # verify no driver transactions have been sent self.assertRaises(exceptions.NexusPortBindingNotFound, nexus_db_v2.get_nexusport_switch_bindings, RP_NEXUS_IP_ADDRESS_1) assert (self._cisco_mech_driver.get_switch_retry_count( RP_NEXUS_IP_ADDRESS_1) == 0) self._verify_replay_results([])
def test_replay_retry_handling(self): """Verifies a series of events to check retry_count operations. 1) Verify retry count is incremented upon failure during replay. 2) Verify further attempts to configure replay data stops. 3) Verify upon receipt of new transaction that retry count is reset to 0 so replay attempts will restart. """ unique_driver_result1 = [ '\<vlan\-name\>q\-267\<\/vlan\-name>', ] unique_driver_result2 = [ '\<vlan\-name\>q\-267\<\/vlan\-name>', '\<vlan\-name\>q\-267\<\/vlan\-name>', '\<vlan\-name\>q\-267\<\/vlan\-name>', '\<vlan\-name\>q\-267\<\/vlan\-name>', ] config_replay = cisco_config.cfg.CONF.ml2_cisco.switch_replay_count #Set-up failed config which puts switch in inactive state self.test_replay_create_vlan_failure() # Make sure there is only a single attempt to configure. self._verify_replay_results(unique_driver_result1) # Don't reset_mock so create_vlan continues failing # Perform replay 4 times to exceed retry count of 3. # This should not roll-up an exception but merely quit for i in range(config_replay + 1): self._cfg_monitor.check_connections() # Verify switch retry count reached configured max and # verify only 4 attempts to send create_vlan. # first is from test_replay_create_vlan_failure() # and only 3 from check_connections() assert(self._cisco_mech_driver.get_switch_retry_count( RP_NEXUS_IP_ADDRESS_1) == (config_replay + 1)) self._verify_replay_results(unique_driver_result2) # Clean all the ncclient mock_calls to clear exception # and other mock_call history. self.mock_ncclient.reset_mock() # Verify there exists a single port binding assert(len(nexus_db_v2.get_nexusport_switch_bindings( RP_NEXUS_IP_ADDRESS_1)) == 1) # Sent another config which should reset retry count # Verify replay results again self._delete_port( TestCiscoNexusReplay.test_configs['test_replay_unique1']) # Verify port binding has been removed # Verify switch retry count reset to 0 and # verify no driver transactions have been sent self.assertRaises(exceptions.NexusPortBindingNotFound, nexus_db_v2.get_nexusport_switch_bindings, RP_NEXUS_IP_ADDRESS_1) assert(self._cisco_mech_driver.get_switch_retry_count( RP_NEXUS_IP_ADDRESS_1) == 0) self._verify_replay_results([])
def test_replay_no_retry_failure_handling(self): """Tests to check replay 'no retry' failure handling. 1) Verify config_failure is incremented upon failure during replay config and verify create_vlan transactions are seen. 2) Verify contact_failure is incremented upon failure during get_nexus_type transaction. 3) Verify receipt of new transaction does not reset failure statistics. 4) Verify config&contact_failure is reset when replay is successful. """ unique_driver_result1 = [ '\<vlan\-name\>q\-267\<\/vlan\-name>', ] unique_driver_result2 = [ '\<vlan\-name\>q\-267\<\/vlan\-name>', '\<vlan\-name\>q\-267\<\/vlan\-name>', '\<vlan\-name\>q\-267\<\/vlan\-name>', '\<vlan\-name\>q\-267\<\/vlan\-name>', '\<vlan\-name\>q\-267\<\/vlan\-name>', ] config_replay = MAX_REPLAY_COUNT #Set-up failed config which puts switch in inactive state self.test_replay_create_vlan_failure() # Make sure there is only a single attempt to configure. self._verify_replay_results(unique_driver_result1) # Don't reset_mock so create_vlan continues failing # Test 1: # Perform replay MAX_REPLAY_COUNT times # This should not roll-up an exception but merely quit for i in range(config_replay): self._cfg_monitor.check_connections() # Verify FAIL_CONFIG reached(MAX_REPLAY_COUNT) and there # were only MAX_REPLAY_COUNT+1 attempts to send create_vlan. # first is from test_replay_create_vlan_failure() # and MAX_REPLAY_COUNT from check_connections() assert (self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONFIG, RP_NEXUS_IP_ADDRESS_1) == config_replay) self._verify_replay_results(unique_driver_result2) # Clean all the ncclient mock_calls to clear exception # and other mock_call history. self.mock_ncclient.reset_mock() # Verify there exists a single port binding assert (len( nexus_db_v2.get_nexusport_switch_bindings(RP_NEXUS_IP_ADDRESS_1)) == 1) # Test 2) # Set it up so get nexus type returns exception. # So FAIL_CONTACT increments # Clear the edit driver exception to make sure it's not # interfering in this test. config = {'connect.return_value.edit_config.side_effect': None} self.mock_ncclient.configure_mock(**config) self._set_nexus_type_failure() # Perform replay MAX_REPLAY_COUNT times # This should not roll-up an exception but merely quit for i in range(config_replay): self._cfg_monitor.check_connections() # Verify switch FAIL_CONTACT reached (MAX_REPLAY_COUNT) # and there were no attempts to send create_vlan. assert (self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONFIG, RP_NEXUS_IP_ADDRESS_1) == config_replay) assert (self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONTACT, RP_NEXUS_IP_ADDRESS_1) == config_replay) self._verify_replay_results([]) # Test 3) # Verify this transaction doesn't affect failure stats. self._delete_port( TestCiscoNexusReplay.test_configs['test_replay_unique1']) # Verify port binding has been removed # Verify failure stats is not reset and # verify no driver transactions have been sent self.assertRaises(exceptions.NexusPortBindingNotFound, nexus_db_v2.get_nexusport_switch_bindings, RP_NEXUS_IP_ADDRESS_1) assert (self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONFIG, RP_NEXUS_IP_ADDRESS_1) == config_replay) assert (self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONTACT, RP_NEXUS_IP_ADDRESS_1) == config_replay) self._verify_replay_results([]) # Test 4) # Verify config&contact_failure is reset when replay is # successful. # Clear the get driver exception. config = {'connect.return_value.get.side_effect': None} self.mock_ncclient.configure_mock(**config) # Perform replay once which will be successful causing # failure stats to be reset to 0. # Then verify these stats are indeed 0. self._cfg_monitor.check_connections() assert (self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONFIG, RP_NEXUS_IP_ADDRESS_1) == 0) assert (self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONTACT, RP_NEXUS_IP_ADDRESS_1) == 0)
def _delete_port_failure(self, attr, match_str, test_case, test_id): """Verifies exception handling during object deletion. This method is a shared method to initiate an exception at various point of object deletion. The points of failure are identified by the caller which can be get operations or edit operations. When the mechanism replay is functioning, the exception should be suppressed and the switch is marked as inactive. attr: Which mock attribute to contain side_effect exception match_str: String for side_effect method to match for exception test_case: which configuration test case to run thru test test_id: String to put in the exception. """ # Set switch state to active switch_ip = self.test_configs[test_case].nexus_ip_addr self._cisco_mech_driver.set_switch_ip_and_active_state( switch_ip, const.SWITCH_ACTIVE) self._create_port( self.test_configs[test_case]) # _create_port should complete successfully and no switch state change. self.assertEqual( self._cisco_mech_driver.get_switch_ip_and_active_state(switch_ip), const.SWITCH_ACTIVE) # Clean all the ncclient mock_calls to clear exception # and other mock_call history. self.mock_ncclient.reset_mock() # Set up driver exception config = {attr: self._config_side_effects_on_count(match_str, Exception(test_id))} self.mock_ncclient.configure_mock(**config) self.assertRaises( exceptions.NexusConfigFailed, self._delete_port, self.test_configs[test_case]) # Verify nothing in the port binding data base # except Reserved Port Binding if replay is enabled nbr_bindings = 1 if self._cisco_mech_driver.is_replay_enabled() else 0 try: port_bindings = nexus_db_v2.get_nexusport_switch_bindings( switch_ip) except exceptions.NexusPortBindingNotFound: port_bindings = [] assert(len(port_bindings) == nbr_bindings) # Verify nothing in the nve data base assert(len(nexus_db_v2.get_nve_switch_bindings( switch_ip)) == 0) # _delete_port should complete with no switch state change. self.assertEqual( self._cisco_mech_driver.get_switch_ip_and_active_state(switch_ip), const.SWITCH_ACTIVE)
def test_replay_no_retry_failure_handling(self): """Tests to check replay 'no retry' failure handling. 1) Verify config_failure is incremented upon failure during replay config and verify create_vlan transactions are seen. 2) Verify contact_failure is incremented upon failure during get_nexus_type transaction. 3) Verify receipt of new transaction does not reset failure statistics. 4) Verify config&contact_failure is reset when replay is successful. """ # Due to 2 retries in driver to deal with stale ncclient # handle, the results are doubled. vlan267 = '<vlan-id-create-delete\>\s+\<__XML__PARAM_value\>267' driver_result2 = ([ test_cisco_nexus_base.RESULT_ADD_INTERFACE.format( 'ethernet', '1\/10', 267) ] + [vlan267] * 2) * 4 config_replay = MAX_REPLAY_COUNT port_cfg = self.test_configs['test_replay_unique1'] self._cisco_mech_driver.set_switch_ip_and_active_state( port_cfg.nexus_ip_addr, const.SWITCH_ACTIVE) # Set-up successful creation of port vlan config self._basic_create_verify_port_vlan('test_replay_unique1', self.driver_result_unique_add1) # Test 1: # Set the edit create vlan driver exception # Perform replay MAX_REPLAY_COUNT times # This should not roll-up an exception but merely quit # and increment FAIL_CONFIG statistics config = { 'connect.return_value.edit_config.side_effect': self._config_side_effects_on_count('vlan-id-create-delete', Exception(__name__ + '1')) } self.mock_ncclient.configure_mock(**config) self._cisco_mech_driver.set_switch_ip_and_active_state( port_cfg.nexus_ip_addr, const.SWITCH_INACTIVE) for i in range(config_replay): self._cfg_monitor.check_connections() # Verify FAIL_CONFIG reached(MAX_REPLAY_COUNT) and there # were only MAX_REPLAY_COUNT+1 attempts to send create_vlan. # first is from test_replay_create_vlan_failure() # and MAX_REPLAY_COUNT from check_connections() assert (self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONFIG, test_cisco_nexus_base.NEXUS_IP_ADDRESS_1) == config_replay) self._verify_results(driver_result2) # Verify there exists a single port binding # plus 1 for reserved switch entry assert (len( nexus_db_v2.get_nexusport_switch_bindings( test_cisco_nexus_base.NEXUS_IP_ADDRESS_1)) == 2) # Clear mock_call history. self.mock_ncclient.reset_mock() # Clear the edit driver exception for next test. config = {'connect.return_value.edit_config.side_effect': None} self.mock_ncclient.configure_mock(**config) # Test 2) # Set it up so get nexus type returns exception. # FAIL_CONTACT should increment. self._set_nexus_type_failure() # Perform replay MAX_REPLAY_COUNT times # This should not roll-up an exception but merely quit for i in range(config_replay): self._cfg_monitor.check_connections() # Verify switch FAIL_CONTACT reached (MAX_REPLAY_COUNT) # and there were no attempts to send create_vlan. assert (self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONFIG, test_cisco_nexus_base.NEXUS_IP_ADDRESS_1) == config_replay) assert (self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONTACT, test_cisco_nexus_base.NEXUS_IP_ADDRESS_1) == config_replay) self._verify_results([]) # Test 3) # Verify delete transaction doesn't affect failure stats. self._basic_delete_verify_port_vlan('test_replay_unique1', []) # Verify failure stats is not reset assert (self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONFIG, test_cisco_nexus_base.NEXUS_IP_ADDRESS_1) == config_replay) assert (self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONTACT, test_cisco_nexus_base.NEXUS_IP_ADDRESS_1) == config_replay) # Clear the get nexus type driver exception. config = {'connect.return_value.get.side_effect': None} self.mock_ncclient.configure_mock(**config) # Test 4) # Verify config&contact_failure is reset when replay is # successful. # Perform replay once which will be successful causing # failure stats to be reset to 0. # Then verify these stats are indeed 0. self._cfg_monitor.check_connections() assert (self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONFIG, test_cisco_nexus_base.NEXUS_IP_ADDRESS_1) == 0) assert (self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONTACT, test_cisco_nexus_base.NEXUS_IP_ADDRESS_1) == 0) # Verify switch state is now active following successful replay. self.assertEqual( self._cisco_mech_driver.get_switch_ip_and_active_state( test_cisco_nexus_base.NEXUS_IP_ADDRESS_1), const.SWITCH_ACTIVE)
def test_replay_no_retry_failure_handling(self): """Tests to check replay 'no retry' failure handling. 1) Verify config_failure is incremented upon failure during replay config and verify create_vlan transactions are seen. 2) Verify contact_failure is incremented upon failure during get_nexus_type transaction. 3) Verify receipt of new transaction does not reset failure statistics. 4) Verify config&contact_failure is reset when replay is successful. """ unique_driver_result1 = [ '\<vlan\-name\>q\-267\<\/vlan\-name>', ] unique_driver_result2 = [ '\<vlan\-name\>q\-267\<\/vlan\-name>', '\<vlan\-name\>q\-267\<\/vlan\-name>', '\<vlan\-name\>q\-267\<\/vlan\-name>', '\<vlan\-name\>q\-267\<\/vlan\-name>', '\<vlan\-name\>q\-267\<\/vlan\-name>', ] config_replay = MAX_REPLAY_COUNT #Set-up failed config which puts switch in inactive state self.test_replay_create_vlan_failure() # Make sure there is only a single attempt to configure. self._verify_replay_results(unique_driver_result1) # Don't reset_mock so create_vlan continues failing # Test 1: # Perform replay MAX_REPLAY_COUNT times # This should not roll-up an exception but merely quit for i in range(config_replay): self._cfg_monitor.check_connections() # Verify FAIL_CONFIG reached(MAX_REPLAY_COUNT) and there # were only MAX_REPLAY_COUNT+1 attempts to send create_vlan. # first is from test_replay_create_vlan_failure() # and MAX_REPLAY_COUNT from check_connections() assert(self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONFIG, RP_NEXUS_IP_ADDRESS_1) == config_replay) self._verify_replay_results(unique_driver_result2) # Clean all the ncclient mock_calls to clear exception # and other mock_call history. self.mock_ncclient.reset_mock() # Verify there exists a single port binding assert(len(nexus_db_v2.get_nexusport_switch_bindings( RP_NEXUS_IP_ADDRESS_1)) == 1) # Test 2) # Set it up so get nexus type returns exception. # So FAIL_CONTACT increments # Clear the edit driver exception to make sure it's not # interfering in this test. config = {'connect.return_value.edit_config.side_effect': None} self.mock_ncclient.configure_mock(**config) self._set_nexus_type_failure() # Perform replay MAX_REPLAY_COUNT times # This should not roll-up an exception but merely quit for i in range(config_replay): self._cfg_monitor.check_connections() # Verify switch FAIL_CONTACT reached (MAX_REPLAY_COUNT) # and there were no attempts to send create_vlan. assert(self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONFIG, RP_NEXUS_IP_ADDRESS_1) == config_replay) assert(self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONTACT, RP_NEXUS_IP_ADDRESS_1) == config_replay) self._verify_replay_results([]) # Test 3) # Verify this transaction doesn't affect failure stats. self._delete_port( TestCiscoNexusReplay.test_configs['test_replay_unique1']) # Verify port binding has been removed # Verify failure stats is not reset and # verify no driver transactions have been sent self.assertRaises(exceptions.NexusPortBindingNotFound, nexus_db_v2.get_nexusport_switch_bindings, RP_NEXUS_IP_ADDRESS_1) assert(self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONFIG, RP_NEXUS_IP_ADDRESS_1) == config_replay) assert(self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONTACT, RP_NEXUS_IP_ADDRESS_1) == config_replay) self._verify_replay_results([]) # Test 4) # Verify config&contact_failure is reset when replay is # successful. # Clear the get driver exception. config = {'connect.return_value.get.side_effect': None} self.mock_ncclient.configure_mock(**config) # Perform replay once which will be successful causing # failure stats to be reset to 0. # Then verify these stats are indeed 0. self._cfg_monitor.check_connections() assert(self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONFIG, RP_NEXUS_IP_ADDRESS_1) == 0) assert(self._cisco_mech_driver.get_switch_replay_failure( const.FAIL_CONTACT, RP_NEXUS_IP_ADDRESS_1) == 0)