def TestSataCtlrReconfig(vm1): """ Test add and remove for SATA controller """ Log("Adding SATA controller to VM") cspec = Vim.Vm.ConfigSpec() cspec = vmconfig.AddSataCtlr(cspec, cfgInfo = vm1.config) vm.Reconfigure(vm1, cspec) # Check for controller presence in VM's config ctlrs = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualAHCIController) if len(ctlrs) != 1: raise Exception("Failed to find added SATA controller :" + str(len(ctlrs))) Log("Powering on VM " + vm1.config.GetName()) vm.PowerOn(vm1) Log("Hot-add SATA controller to VM") cspec = Vim.Vm.ConfigSpec() cspec = vmconfig.AddSataCtlr(cspec, cfgInfo = vm1.config) vm.Reconfigure(vm1, cspec) ctlrs = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualAHCIController) if len(ctlrs) != 2: raise Exception("Failed to find added SATA controller :" + str(len(ctlrs))) vm.PowerOff(vm1) # Remove SATA controller from VM Log("Removing SATA controllers from VM") cspec = Vim.Vm.ConfigSpec() vmconfig.AddDeviceToSpec(cspec, ctlrs[0], Vim.Vm.Device.VirtualDeviceSpec.Operation.remove) vmconfig.AddDeviceToSpec(cspec, ctlrs[1], Vim.Vm.Device.VirtualDeviceSpec.Operation.remove) vm.Reconfigure(vm1, cspec)
def TestEditSataCdrom(vm1): """ Test reconfigures of SATA cdroms """ cspec = Vim.Vm.ConfigSpec() cspec = vmconfig.AddSataCtlr(cspec) vm.Reconfigure(vm1, cspec) Log("Add SATA cdrom.") AddSataCdrom(vm1) Log("Reconfigure cdrom backing.") TestReconfigCdromBacking(vm1) Log("Snapshot VM and revert to snapshot.") TestSnapshotCdrom(vm1) Log("Moving cdrom from SATA to IDE controller.") ideCtlrs = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualIDEController) cdrom = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualCdrom)[0] TestMoveDevice(vm1, cdrom, ideCtlrs[0]) Log("Remove cdrom.") RemoveSataCdrom(vm1) Log("Testing hot-add and hot-remove of SATA cdrom.") vm.PowerOn(vm1) AddSataCdrom(vm1) RemoveSataCdrom(vm1) vm.PowerOff(vm1) ctlrs = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualSATAController) vm.RemoveDevice(vm1, ctlrs[0])
def TestNegVMCIDevice(vm1, isVMCIDefault): if not isVMCIDefault: devices = vmconfig.CheckDevice(vm1.GetConfig(), Vim.Vm.Device.VirtualVMCIDevice) if len(devices) != 0: raise Exception("VMCI device found by default in VM") return failure = False Log("Adding new VMCI device to a VM") try: cspec = Vim.Vm.ConfigSpec() cpsec = vmconfig.AddVMCI(cspec) task = vm1.Reconfigure(cspec) WaitForTask(task) except Exception as e: Log("Verified request was rejected as expected.") failure = True if not failure: raise Exception("Adding VMCI device to VM was allowed!") Log("Attempt to remove VMCI device from VM") devices = vmconfig.CheckDevice(vm1.GetConfig(), Vim.Vm.Device.VirtualVMCIDevice) cspec = Vim.Vm.ConfigSpec() cpsec = vmconfig.RemoveDeviceFromSpec(cspec, devices[0]) try: task = vm1.Reconfigure(cspec) WaitForTask(task) except Exception as e: Log("Verified request was rejected as expected.") return raise Exception("Did not hit an exception as expected")
def test1_IncreaseDiskSize(self): """ Increase disk size. """ devices = vmconfig.CheckDevice(self._vm.GetConfig(), Vim.Vm.Device.VirtualDisk) disk_count = len(devices) self.failIf(disk_count < 1, "Expected at least one disk. Found %s disks." % disk_count) disk = devices[0] cspec = Vim.Vm.ConfigSpec() newCapacity = disk.GetCapacityInKB() + 8192 self.failIfRaises("disk.SetCapacityInKB should not raise.", disk.SetCapacityInKB, newCapacity) self.failIfRaises("vmconfig.AddDeviceToSpec should not raise.", vmconfig.AddDeviceToSpec, cspec, disk, Vim.Vm.Device.VirtualDeviceSpec.Operation.edit) self.failIfRaises("Reconfiguring the VM should not raise.", self._vm.Reconfigure, cspec) devices = vmconfig.CheckDevice(self._vm.GetConfig(), Vim.Vm.Device.VirtualDisk, {"capacityInKB": newCapacity}) new_disk_count = len(devices) self.failIf(new_disk_count != disk_count, "Disk count expected to remain the same: %s, it is: %s." % \ (disk_count, new_disk_count))
def TestWithSnapshot(vm1): """ Verify that it is not possible to extend delta disk by changing capacityInBytes. """ cspec = Vim.Vm.ConfigSpec() vmconfig.AddScsiDisk(cspec, cfgInfo = vm1.config) vm.Reconfigure(vm1, cspec) devices = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualDisk) if len(devices) != 1: raise Exception("Failed to find new disk!") # attempt to extend a disk with a parent Log("Creating a snapshot on the VM for negative disk extend test.") vm.CreateSnapshot(vm1, "dummy snapshot", "dummy desc", False, False) Log("Attempting to extend disk size of delta disk.") disk = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualDisk)[0] cspec = Vim.Vm.ConfigSpec() disk.capacityInBytes = disk.capacityInBytes + 1024 vmconfig.AddDeviceToSpec(cspec, disk, Vim.Vm.Device.VirtualDeviceSpec.Operation.edit) task = vm1.Reconfigure(cspec) try: WaitForTask(task) except Exception as e: Log("Hit an exception extending delta disk as expected" + str(e)) else: raise Exception("Error: Extending delta disk was allowed!") Log("Removing all snapshots on the VM.") vm.RemoveAllSnapshots(vm1)
def RemoveSataDisk(vm1): disk = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualDisk)[0] cspec = Vim.Vm.ConfigSpec() fileOp = Vim.Vm.Device.VirtualDeviceSpec.FileOperation.destroy vmconfig.RemoveDeviceFromSpec(cspec, disk, fileOp) vm.Reconfigure(vm1, cspec) devices = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualDisk) if len(devices) != 0: raise Exception("Found disk after delete")
def CheckDevice(vm1, devType, label): retry = 0 devices = vmconfig.CheckDevice(vm1.GetConfig(), devType) while len(devices) != 1 and retry < 10: Log("Device not there yet...") devices = vmconfig.CheckDevice(vm1.GetConfig(), devType) retry += 1 if len(devices) != 1: raise Exception("Failed to find added " + label) return devices[0]
def TestHotPlugDevice( vm1, func, label, devType, positive, testRemove=True, leaveInVm=False, fileOp=None, ): Log("Testing " + label + " hot plug for VM " + vm1.GetConfig().GetName()) failed = False Log("Add a new " + label + " to the VM") cspec = Vim.Vm.ConfigSpec() cspec = func(cspec, cfgInfo=vm1.GetConfig()) devicesBefore = vmconfig.CheckDevice(vm1.GetConfig(), devType) # Hot-add the device try: vm.Reconfigure(vm1, cspec) except Exception as e: failed = True if not positive: Log("Caught exception as expected") return else: raise if not positive and not failed: raise Exception("Did not hit an exception as expected") # Check for device presence in VM's config devicesAfter = vmconfig.CheckDevice(vm1.GetConfig(), devType) if len(devicesAfter) == len(devicesBefore): raise Exception("Failed to find added " + label) if not testRemove: return # Hot-remove the device newDev = devicesAfter[len(devicesAfter) - 1] Log("Removing " + label + " from the VM") cspec = Vim.Vm.ConfigSpec() vmconfig.RemoveDeviceFromSpec(cspec, newDev, fileOp) vm.Reconfigure(vm1, cspec) devicesAfter = vmconfig.CheckDevice(vm1.GetConfig(), devType) # Check for device absence in the VM's config if len(devicesAfter) != len(devicesBefore): raise Exception(label + " still found in the VM") # Add device back into the VM if necessary if leaveInVm: cspec = Vim.Vm.ConfigSpec() cspec = func(cspec) vm.Reconfigure(vm1, cspec)
def TestReconfigCdromBacking(vm1): cdrom = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualCdrom)[0] cspec = Vim.Vm.ConfigSpec() backing = Vim.Vm.Device.VirtualCdrom.IsoBackingInfo(fileName="[]") cdrom.backing = backing vmconfig.AddDeviceToSpec(cspec, cdrom, Vim.Vm.Device.VirtualDeviceSpec.Operation.edit) vm.Reconfigure(vm1, cspec) devices = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualCdrom, {"backing.fileName": "[]"}) if len(devices) != 1: raise Exception("Failed to find edited cdrom!")
def TestDeviceRemove(vm1, devType, devLabel): devices = vmconfig.CheckDevice(vm1.GetConfig(), devType) if len(devices) != 1: raise Exception("Failed to find " + devLabel + " device.") cspec = Vim.Vm.ConfigSpec() cspec = vmconfig.RemoveDeviceFromSpec(cspec, devices[0]) task = vm1.Reconfigure(cspec) WaitForTask(task) devices = vmconfig.CheckDevice(vm1.GetConfig(), devType) if len(devices) != 0: raise Exception("Found " + devLabel + " device even after delete.")
def TestExtendDisk(vm1): disk = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualDisk)[0] # increase disk size Log("Increase disk size by 8 MB") cspec = Vim.Vm.ConfigSpec() newCapacity = disk.capacityInKB + 8192 disk.capacityInKB = newCapacity vmconfig.AddDeviceToSpec(cspec, disk, Vim.Vm.Device.VirtualDeviceSpec.Operation.edit) vm.Reconfigure(vm1, cspec) devices = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualDisk, {"capacityInKB": newCapacity}) if len(devices) != 1: raise Exception("Failed to find the disk with updated capacity")
def AddCdromISO(vm1, isoFile): cspec = Vim.Vm.ConfigSpec() cspec = vmconfig.AddSataCtlr(cspec) vmconfig.AddSataCdrom(cspec, isoFilePath='[] ' + isoFile) vm.Reconfigure(vm1, cspec) cdromDevs = vmconfig.CheckDevice(vm1.GetConfig(), Vim.Vm.Device.VirtualCdrom) if len(cdromDevs) != 1: raise Exception('Failed to add cdrom device') ctlrs = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualSATAController) assert (len(ctlrs) == 1) return cdromDevs[0], ctlrs[0]
def AddSataDisk(vm1): cspec = Vim.Vm.ConfigSpec() vmconfig.AddSataDisk(cspec, cfgInfo = vm1.config) vm.Reconfigure(vm1, cspec) devices = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualDisk) if len(devices) < 1: raise Exception("Failed to find SATA disk!") ctlrKey = devices[0].controllerKey ctlrs = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualSATAController, {"key": ctlrKey}) if len(ctlrs) != 1: raise Exception("Failed to find SATA controller!")
def TestCreateDisk(vm1): """ Creating disk with various combination of capacityInBytes and capacityInKB. """ Log("Add a new disk with capacityInKB greater than capacityInBytes.") # server must consider capacityInBytes cspec = Vim.Vm.ConfigSpec() cspec = vmconfig.AddScsiCtlr(cspec, cfgInfo = vm1.config) capacityKB = 4 * 1024 vmconfig.AddScsiDisk(cspec, cfgInfo = vm1.config, capacity = capacityKB) vm.Reconfigure(vm1, cspec) # Server must always return "capacityInBytes" devices = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualDisk, {"capacityInKB": capacityKB, "capacityInBytes": (capacityKB * 1024)}) if len(devices) != 1: raise Exception("Failed to find new disk!") Log("Add a new disk with capacityInBytes bigger than capacityInKB.") cspec = Vim.Vm.ConfigSpec() capacityBytes = (capacityKB * 1024) + SECTOR_SIZE vmconfig.AddScsiDisk(cspec, cfgInfo = vm1.GetConfig(), capacity = capacityKB, capacityInBytes = capacityBytes) vm.Reconfigure(vm1, cspec) devices = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualDisk, {"capacityInKB": capacityKB, "capacityInBytes": capacityBytes}) if len(devices) != 1: raise Exception("Capacity did not match expected values!") cspec = Vim.Vm.ConfigSpec() # server must consider capacityInBytes capacityBytes = 2*capacityKB*1024 - SECTOR_SIZE vmconfig.AddScsiDisk(cspec, cfgInfo = vm1.config, capacity = capacityKB, capacityInBytes = capacityBytes) vm.Reconfigure(vm1, cspec) devices = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualDisk, {"capacityInKB": (2 * capacityKB)-1, "capacityInBytes": capacityBytes}) if len(devices) != 1: raise Exception("Capacity did not match expected values!") Log("Removing virtual disks from VM.") devices = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualDisk) fileOp = Vim.Vm.Device.VirtualDeviceSpec.FileOperation.destroy cspec = Vim.Vm.ConfigSpec() for device in devices: vmconfig.RemoveDeviceFromSpec(cspec, device, fileOp) vm.Reconfigure(vm1, cspec)
def EditFloppySetAndUnsetAutodetect(vm1): # Test 9. Edit a floppy device and toggle the device autodetect setting cspec = Vim.Vm.ConfigSpec() cspec = vmconfig.AddFloppy(cspec, autodetect=True) task = vm1.Reconfigure(cspec) WaitForTask(task) devices = vmconfig.CheckDevice(vm1.GetConfig(), Vim.Vm.Device.VirtualFloppy, {"backing.useAutoDetect": True}) if len(devices) != 1: raise "Failed to find added floppy" cspec = Vim.Vm.ConfigSpec() dev = devices[0] backing = dev.GetBacking() backing.SetUseAutoDetect(False) backing.SetDeviceName( vmconfig.GetDeviceName(None, vmconfig.GetCfgTarget(None).GetFloppy)) dev.SetBacking(backing) vmconfig.AddDeviceToSpec(cspec, dev, Vim.Vm.Device.VirtualDeviceSpec.Operation.edit) task = vm1.Reconfigure(cspec) WaitForTask(task) devices = vmconfig.CheckDevice(vm1.GetConfig(), Vim.Vm.Device.VirtualFloppy, {"backing.useAutoDetect": False}) if len(devices) != 1: raise "Failed to find added floppy" cspec = Vim.Vm.ConfigSpec() dev = devices[0] backing = dev.GetBacking() backing.SetUseAutoDetect(True) dev.SetBacking(backing) vmconfig.AddDeviceToSpec(cspec, dev, Vim.Vm.Device.VirtualDeviceSpec.Operation.edit) task = vm1.Reconfigure(cspec) WaitForTask(task) devices = vmconfig.CheckDevice(vm1.GetConfig(), Vim.Vm.Device.VirtualFloppy, {"backing.useAutoDetect": True}) if len(devices) != 1: raise "Failed to find added floppy" print("Toggling floppy autodetect works!")
def EditExistingEnsoniqToSetAndUnsetAutodetect(vm1): # Test 9. Edit an ensoniq device and toggle the device autodetect setting cspec = Vim.Vm.ConfigSpec() cspec = vmconfig.AddSoundCard(cspec, autodetect=True, type="ensoniq") task = vm1.Reconfigure(cspec) WaitForTask(task) devices = vmconfig.CheckDevice(vm1.GetConfig(), Vim.Vm.Device.VirtualEnsoniq1371, {"backing.useAutoDetect": True}) if len(devices) != 1: raise "Failed to find added sound card" cspec = Vim.Vm.ConfigSpec() dev = devices[0] backing = dev.GetBacking() backing.SetUseAutoDetect(False) backing.SetDeviceName( vmconfig.GetDeviceName(None, vmconfig.GetCfgTarget(None).GetSound)) dev.SetBacking(backing) vmconfig.AddDeviceToSpec(cspec, dev, Vim.Vm.Device.VirtualDeviceSpec.Operation.edit) task = vm1.Reconfigure(cspec) WaitForTask(task) devices = vmconfig.CheckDevice(vm1.GetConfig(), Vim.Vm.Device.VirtualEnsoniq1371, {"backing.useAutoDetect": False}) if len(devices) != 1: raise "Failed to find added sound card" cspec = Vim.Vm.ConfigSpec() dev = devices[0] backing = dev.GetBacking() backing.SetUseAutoDetect(True) dev.SetBacking(backing) vmconfig.AddDeviceToSpec(cspec, dev, Vim.Vm.Device.VirtualDeviceSpec.Operation.edit) task = vm1.Reconfigure(cspec) WaitForTask(task) devices = vmconfig.CheckDevice(vm1.GetConfig(), Vim.Vm.Device.VirtualEnsoniq1371, {"backing.useAutoDetect": True}) if len(devices) != 1: raise "Failed to find added sound card" print("Toggling soundcard autodetect works!")
def CreateMultipleSoundCards(vm1): ## Test 6. Create two sound cards and verify the operation fails success = False cspec = Vim.Vm.ConfigSpec() cspec = vmconfig.AddSoundCard(cspec, autodetect=False, type="ensoniq") cspec = vmconfig.AddSoundCard(cspec, autodetect=False, type="sb16") try: task = vm1.Reconfigure(cspec) WaitForTask(task) except Vim.Fault.TooManyDevices as e: success = True if success == True: success = False cspec = Vim.Vm.ConfigSpec() cspec = vmconfig.AddSoundCard(cspec, autodetect=False, type="ensoniq") try: task = vm1.Reconfigure(cspec) WaitForTask(task) except Vim.Fault.TooManyDevices as e: success = True devices = vmconfig.CheckDevice(vm1.GetConfig(), Vim.Vm.Device.VirtualSoundCard) if len(devices) != 0: raise "Found sound card though operation should have failed!!" if (success == True): print("Successful failing to create 2 sound devices") else: print("Apparently didnt see expected exceptions")
def GetCloneSpec(host, vm1, datastore): disks = vmconfig.CheckDevice(vm1.GetConfig(), Vim.Vm.Device.VirtualDisk) location = Vim.Vm.RelocateSpec() snapshot = vm1.GetSnapshot().GetCurrentSnapshot() location.SetHost(host) location.SetDatastore(datastore) location.SetPool(host.parent.resourcePool) location.SetDiskMoveType("createNewChildDiskBacking") diskLocators = [] diskLoc = Vim.Vm.RelocateSpec.DiskLocator() flatThickEager = Vim.vm.Device.VirtualDisk.FlatVer2BackingInfo() #flatThickEager.SetDeltaDiskFormat("seSparseFormat") diskLoc.SetDiskBackingInfo(flatThickEager) diskLoc.diskId = disks[0].GetKey() diskLoc.SetDatastore(datastore) diskLocators.append(diskLoc) location.SetDisk(diskLocators) cspec = Vim.Vm.CloneSpec() cspec.SetLocation(location) cspec.SetTemplate(False) cspec.SetPowerOn(False) cspec.SetSnapshot(snapshot) return cspec
def CheckWDTNotPresent(vm1): """ Confirm that watchdog timer is not present in a VM. """ wdts = vmconfig.CheckDevice(vm1.config, vim.vm.device.VirtualWDT) if len(wdts) != 0: raise Exception("Watchdog timer found in a VM: " + str(wdts))
def TestVQATDeviceType(vm1): """ Test adding QAT devices with various deviceTypes """ CheckQATNotPresent(vm1) Log("Adding VQAT with deviceType") cspec = vim.vm.ConfigSpec() # Add VQAT devices with a variety of deviceTypes to VM. deviceTypes = ["C62XVF", "C62XVF-crypto", "C62XVF-compression"] for deviceType in deviceTypes: AddVQAT(cspec, deviceType=deviceType) vm.Reconfigure(vm1, cspec) # Iterate through qatDevices and build both a remove spec # and a list of the QAT backing deviceTypes seen. qatDevices = vmconfig.CheckDevice(vm1.config, vim.vm.device.VirtualQAT) backingDeviceTypes = [] cspec = vim.vm.ConfigSpec() for qat in qatDevices: deviceType = qat.backing.deviceType if deviceType is None: raise Exception("qat device with unset deviceType") backingDeviceTypes.append(deviceType) vmconfig.AddDeviceToSpec( cspec, qat, vim.vm.device.VirtualDeviceSpec.Operation.remove) # Should see all of the configured deviceTypes in the returned # backing deviceTypes. if sorted(deviceTypes) != sorted(backingDeviceTypes): raise Exception("Invalid device types after reconfiguration: " + ",".join(backingDeviceTypes)) # Remove all QAT device and verify no QAT devices remain on VM. vm.Reconfigure(vm1, cspec) CheckQATNotPresent(vm1)
def CheckQATNotPresent(vm1): """ Confirm that QAT is not present in a VM. """ qatDevices = vmconfig.CheckDevice(vm1.config, vim.vm.device.VirtualQAT) if len(qatDevices) != 0: raise Exception("QAT found in a VM: " + str(qatDevices))
def CheckTPMNotPresent(vm1): """ Confirm that TPM is not present in a VM. """ tpms = vmconfig.CheckDevice(vm1.config, vim.vm.device.VirtualTPM) if len(tpms) != 0: raise Exception("TPM found in a VM: " + str(tpms))
def CheckVideoCard3DSupport(vm1, toggle): devices = vmconfig.CheckDevice(vm1.GetConfig(), Vim.Vm.Device.VirtualVideoCard, {"enable3DSupport": toggle}) if len(devices) != 1: raise Exception("Video card with 3D support set to " + str(toggle) + " not found!") return devices[0]
def DeltaTest(dc, dsName, host, backingType, deltaDiskFormat, diskSize, vmx, positive, test): msg = test + "positive =" + str(positive) + '; ' + \ "backing=" + backingType + '; ' + \ "delta=" + str(deltaDiskFormat) + '; ' + \ "vmx version=" + str(vmx) +'; ' + \ 'diskSize=' + str(diskSize) +'; ' + 'result=' vm1 = None snapshotVm = None try: spec = CreateVmSpec(backingType + "Vm", dsName, backingType, diskSize, vmx) vm1 = VmTest(spec, dc, host, remove=False) vm.CreateSnapshot(vm1, "S1", "snap shot of vm1", False, False) snapshot = vm1.GetSnapshot().GetCurrentSnapshot() disks = vmconfig.CheckDevice(snapshot.GetConfig(), \ Vim.Vm.Device.VirtualDisk) if len(disks) != 1: print(snapshot.GetConfig()) raise AssertionError("Did not find correct number of snapshot") parentDisk = disks[0].GetBacking().GetFileName() spec = CreateVmSpec(backingType + "Vm-Clone", dsName, backingType, diskSize, vmx) childDiskBacking = spec.GetDeviceChange()[1].GetDevice().GetBacking() #print(childDiskBacking) if backingType == 'flat': parentBacking = Vim.Vm.Device.VirtualDisk.FlatVer2BackingInfo() if backingType == 'seSparse': parentBacking = Vim.Vm.Device.VirtualDisk.SeSparseBackingInfo() parentBacking.SetFileName(parentDisk) childDiskBacking.SetParent(parentBacking) childDiskBacking.SetDeltaDiskFormat(deltaDiskFormat) #print(spec) snapshotVm = VmTest(spec, dc, host, remove=False) except Vmodl.MethodFault as e: if not positive: print(msg + 'SUCCESS') print(e) else: print(msg + "FAILURE") raise except Exception: print(msg + "FAILURE") raise else: if positive: print(msg + 'SUCCESS') else: print(msg + "FAILURE, negative test through") raise AssertionError(msg + "FAILURE, negative test through") finally: if vm1: vm.Destroy(vm1) if snapshotVm: vm.Destroy(snapshotVm)
def BasicCreateDelete(vm1): ## Test 1. Create and delete a basic floppy cspec = Vim.Vm.ConfigSpec() cspec = vmconfig.AddFloppy(cspec) task = vm1.Reconfigure(cspec) WaitForTask(task) devices = vmconfig.CheckDevice(vm1.GetConfig(), VF) if len(devices) != 1: raise "Failed to find added floppy" cspec = Vim.Vm.ConfigSpec() cspec = vmconfig.RemoveDeviceFromSpec(cspec, devices[0]) task = vm1.Reconfigure(cspec) WaitForTask(task) devices = vmconfig.CheckDevice(vm1.GetConfig(), VF) if len(devices) != 0: raise "Found floppy even after delete" print("Basic create and delete of floppy works!")
def TestSnapshotCdrom(vm1): cdrom = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualCdrom)[0] Log("Creating a snapshot on the VM") vm.CreateSnapshot(vm1, "dummy snapshot", "dummy desc", False, False) snap = vm1.snapshot.currentSnapshot if len(Diff(vm1.config.hardware.device, snap.config.hardware.device)): Log("Mismatch between config and snapshot") raise Exception("Snapshot hardware differ VM hardware list!") Log("Revert to current snapshot") vm.RevertToCurrentSnapshot(vm1) propList = {"key": cdrom.key, "controllerKey": cdrom.controllerKey} Log("Check if cdrom is present after reverting to snapshot.") devices = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualCdrom, propList) if len(devices) != 1: raise Exception("Failed to find cdrom after revert to snapshot.") Log("Removing all snapshots on the VM.") vm.RemoveAllSnapshots(vm1)
def CheckWDTPresent(vm1): """ Validate watchdog timer presence. """ wdts = vmconfig.CheckDevice(vm1.config, vim.vm.device.VirtualWDT) if len(wdts) != 1: raise Exception("Invalid watchdog timer configuration: " + str(wdts)) wdt = wdts[0] if wdt.key != WATCHDOGTIMER_DEV_KEY: raise Exception("Watchdog timer has unexpected key: " + wdt.key)
def TestEditDisk(vm1): """ Test reconfigures of capacityInBytes and capacityInKB when both are set and differ. """ Log("Adding a new disk.") cspec = Vim.Vm.ConfigSpec() vmconfig.AddScsiDisk(cspec, cfgInfo = vm1.config) vm.Reconfigure(vm1, cspec) devices = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualDisk) if len(devices) != 1: raise Exception("Failed to find new disk!") disk = devices[0] Log("Increase disk size by 4MB setting capacityInBytes") cspec = Vim.Vm.ConfigSpec() newCapacity = disk.capacityInBytes + 4 * 1024 * 1024 + SECTOR_SIZE newCapacityKB = (newCapacity - SECTOR_SIZE) / 1024 disk.capacityInBytes = newCapacity vmconfig.AddDeviceToSpec(cspec, disk, Vim.Vm.Device.VirtualDeviceSpec.Operation.edit) vm.Reconfigure(vm1, cspec) devices = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualDisk, {"capacityInBytes": newCapacity, "capacityInKB": newCapacityKB}) if len(devices) != 1: raise Exception("Failed to find the disk with updated capacity: " + str(len(devices))) Log("Atempt to increase only capacityInKB.") newCapacityKB = newCapacityKB + 4*1024 disk.capacityInKB = newCapacityKB cspec = Vim.Vm.ConfigSpec() vmconfig.AddDeviceToSpec(cspec, disk, Vim.Vm.Device.VirtualDeviceSpec.Operation.edit) vm.Reconfigure(vm1, cspec) devices = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualDisk, {"capacityInKB": newCapacityKB}) if len(devices) != 1: raise Exception("Failed to find the disk with updated capacity: " + str(len(devices))) Log("Removing virtual disk from VM") cspec = Vim.Vm.ConfigSpec() fileOp = Vim.Vm.Device.VirtualDeviceSpec.FileOperation.destroy vmconfig.RemoveDeviceFromSpec(cspec, devices[0], fileOp) vm.Reconfigure(vm1, cspec)
def CheckVMCIDeviceStatus(vm1, toggle): devices = vmconfig.CheckDevice(vm1.GetConfig(), Vim.Vm.Device.VirtualVMCIDevice, {"allowUnrestrictedCommunication": toggle}) if len(devices) < 1: raise Exception( "Failed to find VMCI device with unrestricted set to " + str(toggle)) elif len(devices) > 1: raise Exception("More than one VMCI device found.") return devices[0]
def TestEditSataDisk(vm1): """ Test reconfigures of SATA disks """ cspec = Vim.Vm.ConfigSpec() cspec = vmconfig.AddSataCtlr(cspec) cspec = vmconfig.AddScsiCtlr(cspec) vm.Reconfigure(vm1, cspec) Log("Add SATA disk.") AddSataDisk(vm1) Log("Reconfigure disk capacity.") TestExtendDisk(vm1) Log("Snapshot and reconfigure delta disk.") TestReconfigDeltaDisk(vm1) Log("Move SATA disk to SCSI controller.") scsiCtlrs = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualSCSIController) if len(scsiCtlrs) < 1: raise Exception("Failed to find SCSI controller!") disk = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualDisk)[0] TestMoveDevice(vm1, disk, scsiCtlrs[0]) Log("Move SCSI disk to SATA controller.") ctlrs = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualSATAController) disk = vmconfig.CheckDevice(vm1.config, Vim.Vm.Device.VirtualDisk)[0] TestMoveDevice(vm1, disk, ctlrs[0]) vm.RemoveDevice(vm1, scsiCtlrs[0]) Log("Remove SATA disk.") RemoveSataDisk(vm1); Log("Testing hot-add and hot-remove of SATA disk.") vm.PowerOn(vm1) AddSataDisk(vm1) RemoveSataDisk(vm1); vm.PowerOff(vm1) vm.RemoveDevice(vm1, ctlrs[0])