def test_remove_interfaces_from_vm_with_filter(self): # arrange device1 = Mock(spec=vim.vm.device.VirtualEthernetCard) device2 = Mock(spec=vim.vm.device.VirtualEthernetCard) device3 = Mock(spec=vim.vm.device.VirtualSoundCard) device1.name = 'this is the one' device2.name = 'very close' device3.name = 'not it' vm = Mock() vm.config = Mock() vm.config.hardware() vm.config.hardware.device = [device3, device2, device1] connector = VirtualSwitchToMachineDisconnectCommand( Mock(), Mock(), 'anetwork') #connector.remove_devices = Mock(return_value=True) # act condition = lambda device: device.name == device1.name res = connector.remove_interfaces_from_vm_task(vm, condition) # assert self.assertTrue(res)
def test_delete(self): # arrange uuid = 'uuid' network_name = 'network_name' network = Mock() network.name = network_name si = Mock() vm = Mock() vm.network = [network] connection_detail = Mock() connection_detail.host = Mock() connection_detail.username = Mock() connection_detail.password = Mock() connection_detail.port = Mock() pv_service = Mock() pv_service.connect = Mock(return_value=si) pv_service.find_by_uuid = Mock(return_value=vm) connector = VirtualSwitchToMachineDisconnectCommand( pv_service, Mock(), 'anetwork') # act res = connector.disconnect(si, uuid, network_name) # assert self.assertTrue( pv_service.connect.called_with(connection_detail.host, connection_detail.username, connection_detail.password, connection_detail.port)) self.assertTrue(pv_service.find_by_uuid.called_with(si, uuid)) self.assertTrue(res)
def test_delete_all(self): # arrange uuid = 'uuid' si = Mock() vm = Mock() connection_detail = Mock() connection_detail.host = Mock() connection_detail.username = Mock() connection_detail.password = Mock() connection_detail.port = Mock() pv_service = Mock() pv_service.connect = Mock(return_value=si) pv_service.find_by_uuid = Mock(return_value=vm) connector = VirtualSwitchToMachineDisconnectCommand( pv_service, Mock(), 'anetwork') # virtual_switch_to_machine_connector.remove_interfaces_from_vm = Mock(return_value=True) connector.get_network_by_name = lambda x, y: Mock() # act res = connector.disconnect(si, uuid) # assert self.assertTrue( pv_service.connect.called_with(connection_detail.host, connection_detail.username, connection_detail.password, connection_detail.port)) self.assertTrue(pv_service.find_by_uuid.called_with(si, uuid)) # self.assertTrue(virtual_switch_to_machine_connector.remove_interfaces_from_vm.called_with(vm)) self.assertTrue(res)
def test_delete_all(self): # arrange uuid = 'uuid' si = Mock() vm = Mock() connection_detail = Mock() connection_detail.host = Mock() connection_detail.username = Mock() connection_detail.password = Mock() connection_detail.port = Mock() pv_service = Mock() pv_service.connect = Mock(return_value=si) pv_service.find_by_uuid = Mock(return_value=vm) connector = VirtualSwitchToMachineDisconnectCommand(pv_service, Mock(), 'anetwork') # virtual_switch_to_machine_connector.remove_interfaces_from_vm = Mock(return_value=True) connector.get_network_by_name = lambda x, y: Mock() # act res = connector.disconnect(si, uuid) # assert self.assertTrue(pv_service.connect.called_with(connection_detail.host, connection_detail.username, connection_detail.password, connection_detail.port)) self.assertTrue(pv_service.find_by_uuid.called_with(si, uuid)) # self.assertTrue(virtual_switch_to_machine_connector.remove_interfaces_from_vm.called_with(vm)) self.assertTrue(res)
def test_delete(self): # arrange uuid = 'uuid' network_name = 'network_name' network = Mock() network.name = network_name si = Mock() vm = Mock() vm.network = [network] connection_detail = Mock() connection_detail.host = Mock() connection_detail.username = Mock() connection_detail.password = Mock() connection_detail.port = Mock() pv_service = Mock() pv_service.connect = Mock(return_value=si) pv_service.find_by_uuid = Mock(return_value=vm) connector = VirtualSwitchToMachineDisconnectCommand(pv_service, Mock(), 'anetwork') # act res = connector.disconnect(si, uuid, network_name) # assert self.assertTrue(pv_service.connect.called_with(connection_detail.host, connection_detail.username, connection_detail.password, connection_detail.port)) self.assertTrue(pv_service.find_by_uuid.called_with(si, uuid)) self.assertTrue(res)
def integrationtest_remove_interface(self): default_network = None connector = VirtualSwitchToMachineDisconnectCommand( self.py_vmomi_service, self.resource_connection_details_retriever, self.configurer, default_network) vm = self.get_vm(self.py_vmomi_service, self.virtual_machine_name) print "Remove vNIC. Machine: '{}' UUID: [{}]".format( self.virtual_machine_name, self.vm_uuid) task = connector.remove_interfaces_from_vm_task(vm) self.synchronous_task_waiter.wait_for_task(task)
def integrationtest_remove_interface(self): default_network = None connector = VirtualSwitchToMachineDisconnectCommand(self.py_vmomi_service, self.resource_connection_details_retriever, self.configurer, default_network) vm = self.get_vm(self.py_vmomi_service, self.virtual_machine_name) print "Remove vNIC. Machine: '{}' UUID: [{}]".format(self.virtual_machine_name, self.vm_uuid) task = connector.remove_interfaces_from_vm_task(vm) self.synchronous_task_waiter.wait_for_task(task)
def integrationtest_all_disconnect(self): default_network = None connector = VirtualSwitchToMachineDisconnectCommand(self.py_vmomi_service, self.resource_connection_details_retriever, self.configurer, default_network) self.vm_uuid = self.vm_uuid or self.get_vm_uuid(self.py_vmomi_service, self.virtual_machine_name) print "DISCONNECT ALL. Machine: '{}' UUID: [{}]".format(self.virtual_machine_name, self.vm_uuid) result = connector.disconnect_all(self.vcenter_name, self.vm_uuid) print result
def integrationtest_all_disconnect(self): default_network = None connector = VirtualSwitchToMachineDisconnectCommand( self.py_vmomi_service, self.resource_connection_details_retriever, self.configurer, default_network) self.vm_uuid = self.vm_uuid or self.get_vm_uuid( self.py_vmomi_service, self.virtual_machine_name) print "DISCONNECT ALL. Machine: '{}' UUID: [{}]".format( self.virtual_machine_name, self.vm_uuid) result = connector.disconnect_all(self.vcenter_name, self.vm_uuid) print result
def test_remove_interfaces_from_vm_no_nic_found(self): # arrange vm = Mock() vm.config = Mock() vm.config.hardware() vm.config.hardware.device = [] virtual_switch_to_machine_connector = VirtualSwitchToMachineDisconnectCommand(Mock(), Mock(), 'anetwork') # act res = virtual_switch_to_machine_connector.remove_interfaces_from_vm_task(vm) # assert self.assertIsNone(res)
def __init__(self): py_vmomi_service = pyVmomiService(SmartConnect, Disconnect) cloudshell_data_retriever_service = CloudshellDataRetrieverService() resource_connection_details_retriever = ResourceConnectionDetailsRetriever( helpers, cloudshell_data_retriever_service) resource_remover = CloudshellResourceRemover(helpers) command_wrapper = CommandWrapper(getLogger, py_vmomi_service) template_deployer = VirtualMachineDeployer(py_vmomi_service, generate_unique_name) deploy_from_template_command = DeployFromTemplateCommand( template_deployer) resource_model_parser = ResourceModelParser() vc_model_retriever = VCenterDataModelRetriever(helpers, resource_model_parser) vc_data_model = vc_model_retriever.get_vcenter_data_model() #vnic_to_network_mapper = VnicToNetworkMapper(name_generator, vc_data_model.default_network) port_group_name_generator = DvPortGroupNameGenerator() vnic_to_network_mapper = VnicToNetworkMapper(port_group_name_generator) # Virtual Switch Connect synchronous_task_waiter = SynchronousTaskWaiter() dv_port_group_creator = DvPortGroupCreator(py_vmomi_service, synchronous_task_waiter) virtual_machine_port_group_configurer = VirtualMachinePortGroupConfigurer( py_vmomi_service, synchronous_task_waiter, vnic_to_network_mapper, VNicService()) virtual_switch_to_machine_connector = VirtualSwitchToMachineConnector( dv_port_group_creator, virtual_machine_port_group_configurer) virtual_switch_connect_command = VirtualSwitchConnectCommand( py_vmomi_service, virtual_switch_to_machine_connector, DvPortGroupNameGenerator(), VlanSpecFactory(), VLanIdRangeParser(), getLogger('VirtualSwitchConnectCommand')) # Virtual Switch Revoke virtual_switch_disconnect_command = \ VirtualSwitchToMachineDisconnectCommand(py_vmomi_service, virtual_machine_port_group_configurer, vc_data_model.default_network) destroy_virtual_machine_command = DestroyVirtualMachineCommand( py_vmomi_service, resource_remover, virtual_switch_disconnect_command) # Power Command vm_power_management_command = VirtualMachinePowerManagementCommand( py_vmomi_service, synchronous_task_waiter) # Refresh IP command refresh_ip_command = RefreshIpCommand( py_vmomi_service, cloudshell_data_retriever_service, helpers, resource_model_parser) self.commandExecuterService = CommandExecuterService( jsonpickle, helpers, command_wrapper, resource_connection_details_retriever, vc_data_model, destroy_virtual_machine_command, deploy_from_template_command, virtual_switch_connect_command, virtual_switch_disconnect_command, vm_power_management_command, refresh_ip_command)
def test_remove_interfaces_from_vm_no_nic_found(self): # arrange vm = Mock() vm.config = Mock() vm.config.hardware() vm.config.hardware.device = [] virtual_switch_to_machine_connector = VirtualSwitchToMachineDisconnectCommand( Mock(), Mock(), 'anetwork') # act res = virtual_switch_to_machine_connector.remove_interfaces_from_vm_task( vm) # assert self.assertIsNone(res)
def test_remove_interfaces_from_vm_no_filter(self): # arrange device1 = Mock(spec=vim.vm.device.VirtualEthernetCard) device2 = Mock(spec=vim.vm.device.VirtualSoundCard) vm = Mock() vm.config = Mock() vm.config.hardware() vm.config.hardware.device = [device2, device1] virtual_switch_to_machine_connector = VirtualSwitchToMachineDisconnectCommand(Mock(), Mock(), 'anetwork') # act res = virtual_switch_to_machine_connector.remove_interfaces_from_vm_task(vm) # assert self.assertTrue(res)
def test_remove_interfaces_from_vm_no_filter(self): # arrange device1 = Mock(spec=vim.vm.device.VirtualEthernetCard) device2 = Mock(spec=vim.vm.device.VirtualSoundCard) vm = Mock() vm.config = Mock() vm.config.hardware() vm.config.hardware.device = [device2, device1] virtual_switch_to_machine_connector = VirtualSwitchToMachineDisconnectCommand( Mock(), Mock(), 'anetwork') # act res = virtual_switch_to_machine_connector.remove_interfaces_from_vm_task( vm) # assert self.assertTrue(res)
def test_is_device_match_network_not_found(self): # arrange device = Mock() device.backing = Mock(spec=[]) virtual_switch_to_machine_connector = VirtualSwitchToMachineDisconnectCommand( Mock(), Mock(), 'anetwork') # act # res = virtual_switch_to_machine_connector.is_device_match_network(device, 'Fake name') res = VNicService.device_is_attached_to_network(device, 'Fake name') # assert self.assertFalse(res)
def test_remove_interfaces_from_vm_with_filter(self): # arrange device1 = Mock(spec=vim.vm.device.VirtualEthernetCard) device2 = Mock(spec=vim.vm.device.VirtualEthernetCard) device3 = Mock(spec=vim.vm.device.VirtualSoundCard) device1.name = 'this is the one' device2.name = 'very close' device3.name = 'not it' vm = Mock() vm.config = Mock() vm.config.hardware() vm.config.hardware.device = [device3, device2, device1] connector = VirtualSwitchToMachineDisconnectCommand(Mock(), Mock(), 'anetwork') #connector.remove_devices = Mock(return_value=True) # act condition = lambda device: device.name == device1.name res = connector.remove_interfaces_from_vm_task(vm, condition) # assert self.assertTrue(res)