def test_vnic_attached_to_network_1(self): #arrange network = create_autospec(spec=vim.dvs.DistributedVirtualPortgroup) nicspec = create_autospec(spec=vim.vm.device.VirtualDeviceSpec) VNicService.vnic_attach_to_network_distributed = Mock() #act VNicService.vnic_attached_to_network(nicspec, network, logger=Mock()) #assert self.assertTrue(VNicService.vnic_attach_to_network_distributed.called)
def test_vnic_attached_to_network_2(self): #arrange network = create_autospec(spec=vim.Network) nicspec = create_autospec(spec=vim.vm.device.VirtualDeviceSpec) VNicService.vnic_attach_to_network_standard = Mock() #act VNicService.vnic_attached_to_network(nicspec, network, logger=Mock()) #assert self.assertTrue(VNicService.vnic_attach_to_network_standard.called)
def integrationtest_attach_vnic(self, network): nicspes = VNicService.vnic_new_attached_to_network(network) path = "{}/{}".format(self.vcenter_name, self.virtual_machine_path) vm = self.py_vmomi_service.find_vm_by_name(self.si, path, self.virtual_machine_name) if not vm: print "No VM named '{}/{}'".format(path, self.virtual_machine_name) return print "VM found. \n{}".format(vm) task = VNicService.vnic_add_to_vm_task(nicspes, vm) self.synchronous_task_waiter.wait_for_task(task, Mock())
def test_vnic_add_new_to_vm_task(self): #arrange vm = create_autospec(spec=vim.vm) VNicService.vnic_new_attached_to_network = Mock() #VNicService.vnic_add_to_vm_task = Mock() #act VNicService.vnic_add_new_to_vm_task(vm=vm, network=None, logger=Mock()) #assert self.assertTrue(VNicService.vnic_new_attached_to_network.called)
def test_get_device_spec(self): #arrange vnic = Mock() VNicService.create_vnic_spec = Mock() VNicService.set_vnic_connectivity_status = Mock() #act VNicService.get_device_spec(vnic, True) #assert self.assertTrue(VNicService.create_vnic_spec.called) self.assertTrue(VNicService.set_vnic_connectivity_status.called)
def test_set_connectiv(self): nicspec = Mock() nicspec.device = Mock() connect_status = True nicspec = VNicService.vnic_set_connectivity_status( nicspec=nicspec, is_connected=connect_status, logger=Mock()) self.assertEquals(nicspec.device.connectable.connected, connect_status)
def integration_test_connect_B(self): py_vmomi_service = pyVmomiService(SmartConnect, Disconnect) cred = TestCredentials() si = py_vmomi_service.connect(cred.host, cred.username, cred.password, cred.port) vm_uuid = py_vmomi_service.find_vm_by_name(si, 'QualiSB/Boris', 'Boris2-win7').config.uuid mapping = VmNetworkMapping() mapping.vlan_id = '114' # mapping.dv_port_name = 65 mapping.dv_switch_name = 'dvSwitch' mapping.dv_switch_path = 'QualiSB' mapping.vlan_spec = 'Trunk' vlan_spec = VlanSpecFactory() range_fac = VLanIdRangeParser() synchronous_task_waiter = SynchronousTaskWaiter() name_gen = DvPortGroupNameGenerator() mapper = VnicToNetworkMapper(name_gen) dv_port_group_creator = DvPortGroupCreator(py_vmomi_service, synchronous_task_waiter) virtual_machine_port_group_configurer = VirtualMachinePortGroupConfigurer( py_vmomi_service, synchronous_task_waiter, mapper, VNicService()) connector = VirtualSwitchToMachineConnector( dv_port_group_creator, virtual_machine_port_group_configurer) command = VirtualSwitchConnectCommand(py_vmomi_service, connector, name_gen, vlan_spec, range_fac, Mock()) command.connect_to_networks(si, vm_uuid, [mapping], 'QualiSB/anetwork', [])
def integrationtest(self): resource_connection_details_retriever = Mock() credentials = TestCredentials() resource_connection_details_retriever.connection_details = Mock( return_value=VCenterConnectionDetails( credentials.host, credentials.username, credentials.password)) py_vmomi_service = pyVmomiService(SmartConnect, Disconnect) synchronous_task_waiter = SynchronousTaskWaiter() dv_port_group_creator = DvPortGroupCreator(py_vmomi_service, synchronous_task_waiter) pg_name_generator = DvPortGroupNameGenerator vnic_to_network_mapper = VnicToNetworkMapper(pg_name_generator) 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) si = py_vmomi_service.connect(credentials.host, credentials.username, credentials.password, credentials.port) virtual_machine_path = 'Boris' virtual_machine_name = 'boris1' vm = py_vmomi_service.get_obj(si.content, [vim.VirtualMachine], virtual_machine_name) vm_uuid = vm.config.uuid #vm_uuid = self.get_vm_uuid(py_vmomi_service, si, virtual_machine_name) dv_switch_path = 'QualiSB' dv_switch_name = 'dvSwitch' dv_port_name = 'boris_group59'
def integration_test_connect_A(self): py_vmomi_service = pyVmomiService(SmartConnect, Disconnect) cred = TestCredentials() si = py_vmomi_service.connect(cred.host, cred.username, cred.password, cred.port) synchronous_task_waiter = SynchronousTaskWaiter() mapper = VnicToNetworkMapper(DvPortGroupNameGenerator()) dv_port_group_creator = DvPortGroupCreator(py_vmomi_service, synchronous_task_waiter) port_group_name_generator = DvPortGroupNameGenerator() virtual_machine_port_group_configurer = VirtualMachinePortGroupConfigurer( py_vmomi_service, synchronous_task_waiter, mapper, VNicService(), port_group_name_generator) mapping = VmNetworkMapping() mapping.vlan_id = [vim.NumericRange(start=65, end=65)] mapping.dv_port_name = DvPortGroupNameGenerator( ).generate_port_group_name(65, 'Trunk') mapping.dv_switch_name = 'dvSwitch' mapping.dv_switch_path = 'QualiSB' mapping.vlan_spec = vim.dvs.VmwareDistributedVirtualSwitch.TrunkVlanSpec( ) connector = VirtualSwitchToMachineConnector( dv_port_group_creator, virtual_machine_port_group_configurer) vm = py_vmomi_service.find_vm_by_name(si, 'QualiSB/Raz', '2') # Act connector.connect_by_mapping(si, vm, [mapping], None, []) pass
def test_compose_empty(self): nicspec = VNicService.vnic_compose_empty() self.assertTrue(isinstance(nicspec, vim.vm.device.VirtualDeviceSpec)) self.assertTrue( isinstance(nicspec.device, vim.vm.device.VirtualVmxnet3)) self.assertTrue( isinstance(nicspec.device.connectable, vim.vm.device.VirtualDevice.ConnectInfo))
def test_xx(self): vm = Mock() vm.ReconfigVM_Task = lambda x: isinstance(x, vim.vm.ConfigSpec) nicspec = Mock() res = VNicService.vnic_add_to_vm_task(nicspec=nicspec, virtual_machine=vm, logger=Mock()) self.assertIsNone(res)
def _get_vm_network_data(self, vm, reserved_networks, ip_regex, wait_for_ip, logger): network_interfaces = [] if wait_for_ip == 'True': primary_ip = self._get_primary_ip(vm, ip_regex, logger) else: primary_ip = None net_devices = [ d for d in vm.config.hardware.device if isinstance(d, vim.vm.device.VirtualEthernetCard) ] for device in net_devices: network = VNicService.get_network_by_device( vm, device, self.pyvmomi_service, logger) vlan_id = self._convert_vlan_id_to_str( VNicService.get_network_vlan_id(network)) private_ip = self._get_ip_by_device(vm, device) if vlan_id and (network.name.startswith('QS_') or network.name in reserved_networks): is_primary = private_ip and primary_ip == private_ip is_predefined = network.name in reserved_networks network_data = [ VmDetailsProperty(key='IP', value=private_ip), VmDetailsProperty(key='MAC Address', value=device.macAddress), VmDetailsProperty(key='Network Adapter', value=device.deviceInfo.label), VmDetailsProperty(key='Port Group Name', value=network.name) ] current_interface = VmDetailsNetworkInterface( interfaceId=device.macAddress, networkId=vlan_id, isPrimary=is_primary, isPredefined=is_predefined, networkData=network_data, privateIpAddress=private_ip) network_interfaces.append(current_interface) return network_interfaces
def test_device_attahed_to_network_standard(self): self.assertFalse(VNicService.device_is_attached_to_network(None, None)) network_name = "TEST" device = Mock() device.backing = Mock() device.backing.network = Mock() device.backing.network.name = network_name self.assertTrue(VNicService.device_is_attached_to_network(device, network_name)) network = Mock(spec=vim.Network) network.name = "xnet" nicspec = Mock() nicspec.device = device res = VNicService.vnic_attach_to_network_standard(nicspec=nicspec,network= network, logger=Mock()) self.assertEquals(res.device.backing.network.name, "xnet")
def test_device_attahed_to_network_distributed(self): network_name = "PORT-GROUP" device = Mock() device.backing = MagicMock() device.backing.port = Mock() hasattr(device.backing, "network") del device.backing.network device.backing.port.portgroupKey = network_name self.assertTrue(VNicService.device_is_attached_to_network(device, network_name)) port_group = Mock(spec=vim.dvs.DistributedVirtualPortgroup) port_group.key = "group_net" port_group.config.distributedVirtualSwitch.uuid = "6686" nicspec = Mock() nicspec.device = device res = VNicService.vnic_attach_to_network_distributed(nicspec=nicspec, port_group=port_group, logger=Mock()) self.assertEquals(res.device.backing.port.portgroupKey, "group_net")
def test_device_attahed_to_network_distributed(self): network_name = "PORT-GROUP" device = Mock() device.backing = MagicMock() device.backing.port = Mock() hasattr(device.backing, "network") del device.backing.network device.backing.port.portgroupKey = network_name self.assertTrue( VNicService.device_is_attached_to_network(device, network_name)) port_group = Mock(spec=vim.dvs.DistributedVirtualPortgroup) port_group.key = "group_net" port_group.config.distributedVirtualSwitch.uuid = "6686" nicspec = Mock() nicspec.device = device res = VNicService.vnic_attach_to_network_distributed( nicspec=nicspec, port_group=port_group, logger=Mock()) self.assertEquals(res.device.backing.port.portgroupKey, "group_net")
def test_vnic_remove_from_vm_list(self): #arrange vm = create_autospec(spec=vim.vm) vm.config = Mock() vm.config.hardware = Mock() vm.config.hardware.device = [create_autospec(spec=vim.vm.device.VirtualEthernetCard)] #act device_change = VNicService.vnic_remove_from_vm_list(vm) #assert self.assertTrue(len(device_change) == 1)
def test_device_attahed_to_network_standard(self): self.assertFalse(VNicService.device_is_attached_to_network(None, None)) network_name = "TEST" device = Mock() device.backing = Mock() device.backing.network = Mock() device.backing.network.name = network_name self.assertTrue( VNicService.device_is_attached_to_network(device, network_name)) network = Mock(spec=vim.Network) network.name = "xnet" nicspec = Mock() nicspec.device = device res = VNicService.vnic_attach_to_network_standard(nicspec=nicspec, network=network, logger=Mock()) self.assertEquals(res.device.backing.network.name, "xnet")
def test_vnic_add_to_vm_task(self): #arrange nicspec = vim.vm.device.VirtualDeviceSpec() vm = Mock() VNicService.vnic_set_connectivity_status = Mock() pyVmomiService.vm_reconfig_task = Mock() #act res = VNicService.vnic_add_to_vm_task(nicspec=nicspec, virtual_machine=vm, logger=Mock()) #assert self.assertTrue(VNicService.vnic_set_connectivity_status.called) self.assertTrue(pyVmomiService.vm_reconfig_task.called)
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_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_vnic_remove_from_vm_list(self): #arrange vm = create_autospec(spec=vim.vm) vm.config = Mock() vm.config.hardware = Mock() vm.config.hardware.device = [ create_autospec(spec=vim.vm.device.VirtualEthernetCard) ] #act device_change = VNicService.vnic_remove_from_vm_list(vm) #assert self.assertTrue(len(device_change) == 1)
def test_is_device_match_network_port_type(self): # arrange backing = Mock(spec=[]) device = Mock() port = Mock() device.backing = backing backing.port = port port.portgroupKey = 'port key' # act res = VNicService.device_is_attached_to_network(device, port.portgroupKey) # assert self.assertTrue(res)
def test_is_device_match_network_other_type(self): # arrange backing = Mock(spec=[]) device = Mock() nerwork = Mock() device.backing = backing backing.network = nerwork nerwork.name = 'vln name or network name' # act res = VNicService.device_is_attached_to_network(device, nerwork.name) # assert self.assertTrue(res)
def test_is_device_match_network_port_type(self): # arrange backing = Mock(spec=[]) device = Mock() port = Mock() device.backing = backing backing.port = port port.portgroupKey = 'port key' # act res = VNicService.device_is_attached_to_network( device, port.portgroupKey) # assert self.assertTrue(res)
def setUp(self): self._si = None self.vm = None self.virtual_machine_path = 'SergiiT' self.virtual_machine_name = 'test_4f383119' self.vm_uuid = None self.vcenter_name = "QualiSB" self.dv_switch_path = 'QualiSB' self.network_path = 'QualiSB' #self.dv_switch_name = 'dvSwitch-SergiiT' self.dv_switch_name = 'dvSwitch' #self.dv_port_group_name = 'aa-dvPortGroup3B' self.dv_port_group_name = 'dvPortGroup' self.standard_network_name = "Anetwork" try: self.py_vmomi_service = pyVmomiService(SmartConnect, Disconnect) self.credentials = TestCredentials() self.resource_connection_details_retriever = Mock() self.resource_connection_details_retriever.connection_details = Mock( return_value=VCenterConnectionDetails(self.credentials.host, self.credentials.username, self.credentials.password)) self.synchronous_task_waiter = SynchronousTaskWaiter() self.pg_name_generator = DvPortGroupNameGenerator self.vnic_to_network_mapper = VnicToNetworkMapper(self.pg_name_generator) self.configurer = VirtualMachinePortGroupConfigurer(self.py_vmomi_service, self.synchronous_task_waiter, self.vnic_to_network_mapper, VNicService()) except: print "Infrastructure not available - pretty OK or UnitTesting environment"
def __init__(self): """ Initialize the driver session, this function is called everytime a new instance of the driver is created in here the driver is going to be bootstrapped :param context: models.QualiDriverModels.InitCommandContext """ self.cs_helper = CloudshellDriverHelper() synchronous_task_waiter = SynchronousTaskWaiter() pv_service = pyVmomiService(connect=SmartConnect, disconnect=Disconnect, task_waiter=synchronous_task_waiter) self.resource_model_parser = ResourceModelParser() port_group_name_generator = DvPortGroupNameGenerator() vnic_to_network_mapper = VnicToNetworkMapper( quali_name_generator=port_group_name_generator) resource_remover = CloudshellResourceRemover() ovf_service = OvfImageDeployerService(self.resource_model_parser) self.vm_loader = VMLoader(pv_service) vm_deployer = VirtualMachineDeployer( pv_service=pv_service, name_generator=generate_unique_name, ovf_service=ovf_service, cs_helper=self.cs_helper, resource_model_parser=ResourceModelParser()) dv_port_group_creator = DvPortGroupCreator( pyvmomi_service=pv_service, synchronous_task_waiter=synchronous_task_waiter) virtual_machine_port_group_configurer = \ VirtualMachinePortGroupConfigurer(pyvmomi_service=pv_service, synchronous_task_waiter=synchronous_task_waiter, vnic_to_network_mapper=vnic_to_network_mapper, vnic_service=VNicService(), name_gen=port_group_name_generator) virtual_switch_to_machine_connector = VirtualSwitchToMachineConnector( dv_port_group_creator, virtual_machine_port_group_configurer) # Command Wrapper self.command_wrapper = CommandWrapper( pv_service=pv_service, cloud_shell_helper=self.cs_helper, resource_model_parser=self.resource_model_parser, context_based_logger_factory=ContextBasedLoggerFactory()) # Deploy Command self.deploy_command = DeployCommand(deployer=vm_deployer) # Virtual Switch Revoke self.virtual_switch_disconnect_command = \ VirtualSwitchToMachineDisconnectCommand( pyvmomi_service=pv_service, port_group_configurer=virtual_machine_port_group_configurer, resource_model_parser=self.resource_model_parser) # Virtual Switch Connect virtual_switch_connect_command = \ VirtualSwitchConnectCommand( pv_service=pv_service, virtual_switch_to_machine_connector=virtual_switch_to_machine_connector, dv_port_group_name_generator=DvPortGroupNameGenerator(), vlan_spec_factory=VlanSpecFactory(), vlan_id_range_parser=VLanIdRangeParser()) self.connection_orchestrator = ConnectionCommandOrchestrator( connector=virtual_switch_connect_command, disconnector=self.virtual_switch_disconnect_command, resource_model_parser=self.resource_model_parser) # Destroy VM Command self.destroy_virtual_machine_command = \ DestroyVirtualMachineCommand(pv_service=pv_service, resource_remover=resource_remover, disconnector=self.virtual_switch_disconnect_command) # Power Command self.vm_power_management_command = \ VirtualMachinePowerManagementCommand(pyvmomi_service=pv_service, synchronous_task_waiter=synchronous_task_waiter) ip_manager = VMIPManager() # Refresh IP command self.refresh_ip_command = RefreshIpCommand( pyvmomi_service=pv_service, resource_model_parser=ResourceModelParser(), ip_manager=ip_manager) # Save Snapshot self.snapshot_saver = SaveSnapshotCommand( pyvmomi_service=pv_service, task_waiter=synchronous_task_waiter) # Snapshot Restorer self.snapshot_restorer = SnapshotRestoreCommand( pyvmomi_service=pv_service, task_waiter=synchronous_task_waiter) self.snapshots_retriever = RetrieveSnapshotsCommand( pyvmomi_service=pv_service)
def test_compose_empty(self): nicspec = VNicService.vnic_compose_empty() self.assertTrue(isinstance(nicspec, vim.vm.device.VirtualDeviceSpec)) self.assertTrue(isinstance(nicspec.device, vim.vm.device.VirtualVmxnet3)) self.assertTrue(isinstance(nicspec.device.connectable, vim.vm.device.VirtualDevice.ConnectInfo))
def test_set_connectiv(self): nicspec = Mock() nicspec.device = Mock() connect_status = True nicspec = VNicService.vnic_set_connectivity_status(nicspec=nicspec, is_connected=connect_status, logger=Mock()) self.assertEquals(nicspec.device.connectable.connected, connect_status)
def test_vnic_is_attachet_to_network(self): nicspec = Mock() nicspec.device = Mock() res = VNicService.vnic_is_attachet_to_network(nicspec, Mock()) self.assertFalse(res)