def test_create_port_invalid_cluster(self): vm_id = fake_vmware_api.Constants.VM_UUID vlan = model.Vlan(vlan_ids=["1001"]) network_config = model.NetworkConfig(vlan) network = model.Network(name="net-1234", network_type=p_const.TYPE_VLAN, config=network_config) net_id = "net" port = model.Port(name=None, mac_address=None, ipaddresses=None, vm_id=vm_id) virtual_nic = model.VirtualNic(mac_address=None, port_uuid=None, vm_id=vm_id, vm_name=None, nic_type=None, pg_id=None) cluster_mor = fake_vmware_api.DataObject() cluster_mor.value = "invalid_id" cache.VCCache.add_cluster_mor_for_vm(vm_id, cluster_mor) exc = self.assertRaises(error.VcenterConfigurationError, self.vc_driver.create_port, network, net_id, port, virtual_nic) self.assertIn("Cluster for VM %s could not be determined" % vm_id, str(exc))
def test_create_port_exc(self): vm_id = fake_vmware_api.Constants.VM_UUID vlan = model.Vlan(vlan_ids=["1001"]) network_config = model.NetworkConfig(vlan) network = model.Network(name="net-1234", network_type=p_const.TYPE_VLAN, config=network_config) net_id = "net" port = model.Port(name=None, mac_address=None, ipaddresses=None, vm_id=vm_id) virtual_nic = model.VirtualNic(mac_address=None, port_uuid=None, vm_id=vm_id, vm_name=None, nic_type=None, pg_id=None) with mock.patch.object(self.vc_driver, "is_valid_switch", return_value=None): exc = self.assertRaises(error.VcenterConfigurationError, self.vc_driver.create_port, network, net_id, port, virtual_nic) self.assertIn("Invalid Switch", str(exc))
def test_model_virtualmachine(self): key = "1-2-3" vm_name = "vm1" vm_uuid = "uuid1" vnic = model.VirtualNic(None, None, vm_uuid, vm_name, None, None) vm = model.VirtualMachine(vm_name, [vnic], vm_uuid, key) self.assertEqual(vm.uuid, vm_uuid, "vm uuid does not match") self.assertEqual(vm.key, key, "vm key does not match") self.assertEqual(vm.name, vm_name, "vm name does not match") self.assertEqual(len(vm.vnics), 1, "vm vnics size does not match") self.assertEqual(vm.vnics[0].vm_id, vm_uuid, "vm vnics vm_id does not match")
def test_model_virtualnic(self): key = "1-2-3" mac_address = "ABC-DEFG-HIJK" port_uuid = "uuid1" vm_id = "uuid2" vm_name = "vm1" nic_type = "VMXNET" virtual_nic = model.VirtualNic(mac_address, port_uuid, vm_id, vm_name, nic_type, key) self.assertEqual(virtual_nic.key, key, "vnic key does not match") self.assertEqual(virtual_nic.mac_address, mac_address, "vnic mac_address does not match") self.assertEqual(virtual_nic.port_uuid, port_uuid, "vnic port_uuid does not match") self.assertEqual(virtual_nic.vm_id, vm_id, "vnic vm_id does not match") self.assertEqual(virtual_nic.vm_name, vm_name, "vnic vm_name does not match") self.assertEqual(virtual_nic.nic_type, nic_type, "vnic nic_type does not match")
def test_create_port(self): vm_id = fake_vmware_api.Constants.VM_UUID vlan = model.Vlan(vlanIds=["1001"]) network_config = model.NetworkConfig(vlan) network = model.Network(name="net-1234", network_type=constants.NETWORK_VLAN, config=network_config) port = model.Port(name=None, mac_address=None, ipaddresses=None, vm_id=vm_id) virtual_nic = model.VirtualNic(mac_address=None, port_uuid=None, vm_id=vm_id, vm_name=None, nic_type=None) with mock.patch.object(model, "VirtualSwitch") as vswitch: self.vc_driver.create_port(network, port, virtual_nic) self.assertTrue(vswitch.called)
def _process_update_set(self, updateSet): """Processes the updateSet and returns VM events.""" events = [] host_name = None clus_name = None clus_id = None LOG.debug("Processing UpdateSet version: %s.", updateSet.version) filterSet = updateSet.filterSet if not filterSet: return events for propFilterUpdate in filterSet: objectSet = propFilterUpdate.objectSet if not objectSet: continue for objectUpdate in objectSet: try: obj_mor = objectUpdate.obj if obj_mor._type != "VirtualMachine": continue if objectUpdate.kind == "enter": event_type = constants.VM_CREATED elif objectUpdate.kind == "modify": event_type = constants.VM_UPDATED elif objectUpdate.kind == "leave": event_type = constants.VM_DELETED else: continue host_changed = False vm_uuid = None changes = common_util.convert_objectupdate_to_dict( objectUpdate) if changes.get('config.extraConfig["nvp.vm-uuid"]'): vm_uuid = changes.get('config.extraConfig' '["nvp.vm-uuid"]').value event_type = constants.VM_CREATED if vm_uuid is not None: vm_mor = cache.VCCache.get_vm_mor_for_uuid(vm_uuid) if vm_mor is None: cache.VCCache.add_vm_mor_for_uuid(vm_uuid, obj_mor) else: vm_uuid = cache.VCCache.get_vmuuid_for_moid( obj_mor.value) if vm_uuid: old_vm = cache.VCCache.get_vm_model_for_uuid(vm_uuid) LOG.debug("Old VM: %s.", old_vm) LOG.debug("cache.VCCache.vm_uuid_to_model: %s.", cache.VCCache.vm_uuid_to_model) new_vm = None if old_vm: if event_type == constants.VM_CREATED: # Our cache has information about VM. But event # received is VM_CREATED. This means it is # session restart case. So we should not add # this to new event. LOG.debug("Session restart event for VM %s", vm_uuid) continue new_vm = copy.deepcopy(old_vm) else: new_vm = model.VirtualMachine(name=None, vnics=[], uuid=None, key=None) LOG.debug("VM not found in cache. New created: " " %s.", new_vm) new_vm.uuid = vm_uuid new_vm.key = obj_mor.value if changes.get('name'): new_vm.name = changes.get('name') if changes.get('runtime.host'): # Host got changed / New VM. clus_mor = self.session._call_method( vim_util, "get_dynamic_property", changes.get('runtime.host'), "HostSystem", "parent") # Cache the VM and Cluster. cache.VCCache.add_cluster_mor_for_vm(vm_uuid, clus_mor) if event_type != constants.VM_DELETED: extraconfigs = ( resource_util.get_extraconfigs_for_vm( self.session, obj_mor)) if changes.get('config.hardware.device'): devices = changes.get('config.hardware.device') nicdvs = network_util.get_vnics_from_devices( devices) i = 0 vnics = [] for nicdev in nicdvs: macadd = nicdev.macAddress port = nicdev.backing.port pgkey = port.portgroupKey portid = extraconfigs.get("nvp.iface-id.%d" % i) vnic = model.VirtualNic( mac_address=macadd, port_uuid=portid, vm_id=vm_uuid, vm_name=new_vm.name, nic_type=None, pg_id=pgkey, key=None) vnics.append(vnic) i += 1 new_vm.vnics = vnics host_mor = resource_util.get_host_mor_for_vm( self.session, vm_uuid) clus_mor = resource_util.get_cluster_mor_for_vm( self.session, vm_uuid) host_name = ( resource_util.get_hostname_for_host_mor( self.session, host_mor)) old_host_name = ( cache.VCCache.get_esx_hostname_for_vm(vm_uuid)) if old_host_name and old_host_name != host_name: host_changed = True cache.VCCache.add_esx_hostname_for_vm(vm_uuid, host_name) clus_name = ( resource_util.get_clustername_for_cluster_mor( self.session, clus_mor)) clus_id = ( resource_util.get_clusterid_for_cluster_mor( self.session, clus_mor)) elif event_type == constants.VM_DELETED: host_name = cache.VCCache.get_esx_hostname_for_vm( vm_uuid) event = model.Event(event_type, new_vm, None, host_name, clus_name, clus_id, host_changed) events.append(event) cache.VCCache.add_vm_model_for_uuid(vm_uuid, new_vm) LOG.debug("Added vm to cache: %s.", new_vm.uuid) else: LOG.debug("Ignoring update for VM: %s.", changes.get('name')) except Exception: LOG.exception(_LE("Exception while processing update set " "for event %(event)s for vm %(vm)s."), {'event': event_type, 'vm': vm_uuid}) LOG.debug("Finished processing UpdateSet version: %s.", updateSet.version) return events