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 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 CreateFloppyWithNoDeviceAndNoAutoDetect(vm1): ## Test 8. Create a floppy with autodetect disabled and no device name specified success = False cspec = Vim.Vm.ConfigSpec() cspec = vmconfig.AddFloppy(cspec, autodetect=False, backingName="") try: task = vm1.Reconfigure(cspec) WaitForTask(task) except Vim.Fault.InvalidDeviceBacking as e: success = True if success == True: print( "Creating floppy with no device name and no autodetect failed as expected" ) else: print( "Failed test: Created an audio card with no device and no autodetect" )
def CreateFloppyRemoteWithAutoDetect(vm1, autodetectVal): ## Test 3. Create floppy remote backing with auto detect set as specified and delete it cspec = Vim.Vm.ConfigSpec() cspec = vmconfig.AddFloppy(cspec, autodetect=autodetectVal, type="remote") task = vm1.Reconfigure(cspec) WaitForTask(task) devices = vmconfig.CheckDevice(vm1.GetConfig(), VF, { "backing": VFREMOTE, "backing.useAutoDetect": autodetectVal }) if len(devices) != 1: raise "Failed to find added floppy" cspec2 = Vim.Vm.ConfigSpec() cspec2 = vmconfig.RemoveDeviceFromSpec(cspec2, devices[0]) task2 = vm1.Reconfigure(cspec2) WaitForTask(task2) devices = vmconfig.CheckDevice(vm1.GetConfig(), VF) if len(devices) != 0: raise "Found floppy even after delete" print("Creating floppy (remote) with no autodetect works!")
def CreateFloppyWithNoDeviceAndAutoDetect(vm1): ## Test 7. Create a floppy with autodetect and no device name specified cspec = Vim.Vm.ConfigSpec() cspec = vmconfig.AddFloppy(cspec, autodetect=True, backingName="") task = vm1.Reconfigure(cspec) WaitForTask(task) devices = vmconfig.CheckDevice(vm1.GetConfig(), Vim.Vm.Device.VirtualFloppy, { "backing.useAutoDetect": True, "backing.deviceName": "" }) if len(devices) != 1: raise "Failed to find added flopppy" cspec = Vim.Vm.ConfigSpec() vmconfig.RemoveDeviceFromSpec(cspec, devices[0]) task = vm1.Reconfigure(cspec) WaitForTask(task) devices = vmconfig.CheckDevice(vm1.GetConfig(), Vim.Vm.Device.VirtualFloppy) if len(devices) != 0: raise "Found floppy even after delete" print("Creating floppy with no device name and autodetect works")
def main(): supportedArgs = [ (["h:", "host="], "localhost", "Host name", "host"), (["u:", "user="******"root", "User name", "user"), (["p:", "pwd="], "ca$hc0w", "Password", "pwd"), (["v:", "vmname="], "CreateTest", "Name of the virtual machine", "vmname")] supportedToggles = [(["usage", "help"], False, "Show usage information", "usage")] args = arguments.Arguments(sys.argv, supportedArgs, supportedToggles) if args.GetKeyValue("usage") == True: args.Usage() sys.exit(0) # Connect si = Connect(args.GetKeyValue("host"), 443, args.GetKeyValue("user"), args.GetKeyValue("pwd")) atexit.register(Disconnect, si) # Process command line vmname = args.GetKeyValue("vmname") # Cleanup from previous runs. vm1 = folder.Find(vmname) if vm1 != None: vm1.Destroy() # Create vms envBrowser = invt.GetEnv() config = vm.CreateQuickDummySpec(vmname) cfgOption = envBrowser.QueryConfigOption(None, None) cfgTarget = envBrowser.QueryConfigTarget(None) NIC_DIFFERENCE = 7 config = vmconfig.AddNic(config, cfgOption, cfgTarget, unitNumber = NIC_DIFFERENCE + 0) config = vmconfig.AddNic(config, cfgOption, cfgTarget, unitNumber = NIC_DIFFERENCE + 2) config = vmconfig.AddNic(config, cfgOption, cfgTarget, unitNumber = NIC_DIFFERENCE + 3) config = vmconfig.AddNic(config, cfgOption, cfgTarget, unitNumber = NIC_DIFFERENCE + 4) config = vmconfig.AddNic(config, cfgOption, cfgTarget) config = vmconfig.AddNic(config, cfgOption, cfgTarget, unitNumber = NIC_DIFFERENCE + 6) config = vmconfig.AddNic(config, cfgOption, cfgTarget, unitNumber = NIC_DIFFERENCE + 7) config = vmconfig.AddNic(config, cfgOption, cfgTarget, unitNumber = NIC_DIFFERENCE + 8) config = vmconfig.AddNic(config, cfgOption, cfgTarget, unitNumber = NIC_DIFFERENCE + 9) config = vmconfig.AddScsiCtlr(config, cfgOption, cfgTarget, unitNumber = 3) config = vmconfig.AddScsiDisk(config, cfgOption, cfgTarget, unitNumber = 0) isofile = "[] /usr/lib/vmware/isoimages/linux.iso" config = vmconfig.AddCdrom(config, cfgOption, cfgTarget, unitNumber = 0, isoFilePath=isofile) image = "[] /vmimages/floppies/vmscsi.flp" config = vmconfig.AddFloppy(config, cfgOption, cfgTarget, unitNumber = 0, type="image", backingName=image) config = vmconfig.AddFloppy(config, cfgOption, cfgTarget, unitNumber = 1, type="image", backingName=image) backing = Vim.Vm.Device.VirtualSerialPort.FileBackingInfo() backing.SetFileName("[]") config = vmconfig.AddSerial(config, backing) try: vmFolder = invt.GetVmFolder() vimutil.InvokeAndTrack(vmFolder.CreateVm, config, invt.GetResourcePool(), None) except Exception as e: raise vm1 = folder.Find(vmname) printNicUnitNumbers(vm1, "Test 1: Creating a vm with lots of nics") cspec = Vim.Vm.ConfigSpec() cspec = vmconfig.AddNic(cspec, cfgOption, cfgTarget) task = vm1.Reconfigure(cspec) WaitForTask(task) printNicUnitNumbers(vm1, "Test 2: Added a nic") cspec = Vim.Vm.ConfigSpec() cspec = vmconfig.AddNic(cspec, cfgOption, cfgTarget) task = vm1.Reconfigure(cspec) try: WaitForTask(task) except Vim.Fault.TooManyDevices as e: print("Caught too many devices as expected") nics = printNicUnitNumbers(vm1, "Test 3: Added too many nics") cspec = Vim.Vm.ConfigSpec() uni = nics[4].GetUnitNumber() cspec = vmconfig.RemoveDeviceFromSpec(cspec, nics[0]) cspec = vmconfig.RemoveDeviceFromSpec(cspec, nics[2]) cspec = vmconfig.RemoveDeviceFromSpec(cspec, nics[4]) cspec = vmconfig.RemoveDeviceFromSpec(cspec, nics[5]) cspec = vmconfig.RemoveDeviceFromSpec(cspec, nics[6]) cspec = vmconfig.AddNic(cspec, cfgOption, cfgTarget) task = vm1.Reconfigure(cspec) WaitForTask(task) printNicUnitNumbers(vm1, "Test 4: Removed a bunch of nics") cspec = Vim.Vm.ConfigSpec() cspec = vmconfig.AddNic(cspec, cfgOption, cfgTarget, unitNumber = NIC_DIFFERENCE - 2) task = vm1.Reconfigure(cspec) WaitForTask(task) printNicUnitNumbers(vm1, "Test 5: Added a nic with slot incorrectly specified") cspec = Vim.Vm.ConfigSpec() cspec = vmconfig.AddNic(cspec, cfgOption, cfgTarget, unitNumber = uni) cspec = vmconfig.AddScsiCtlr(cspec, cfgOption, cfgTarget, unitNumber = 4) cspec = vmconfig.AddScsiDisk(cspec, cfgOption, cfgTarget, unitNumber = 1) task = vm1.Reconfigure(cspec) WaitForTask(task) printNicUnitNumbers(vm1, "Test 6: Added a nic with a slot correctly specified") cspec = Vim.Vm.ConfigSpec() cspec = vmconfig.AddNic(cspec, cfgOption, cfgTarget, unitNumber = uni) task = vm1.Reconfigure(cspec) try: WaitForTask(task) except Vim.Fault.InvalidDeviceSpec as e: print("Exception caught for adding same nic twice") printNicUnitNumbers(vm1, "Test 7: Added a nic with s slot specified to be an occupied slot") vm1.Destroy()
def test(si, delta, backingType, vmxVersion, ds): suffix = ''.join( random.choice(string.letters + string.digits) for i in xrange(8)) vm1Name = '-'.join(['LinkedParent', suffix]) print('Creating %s VM on %s' % (vm1Name, ds)) task.WaitForTasks( [vm1.Destroy() for vm1 in folder.GetVmAll() if vm1.name == vm1Name]) vm1 = vm.CreateQuickDummy(vm1Name, numScsiDisks=1, datastoreName=ds, diskSizeInMB=1, vmxVersion=vmxVersion, backingType=backingType) vm1DirName = vm1.config.files.snapshotDirectory print('Creating Snapshot S1 for %s' % vm1Name) vm.CreateSnapshot(vm1, 'S1', '', False, False) s1 = vm1.snapshot.currentSnapshot disks = vmconfig.CheckDevice(s1.config, vim.vm.Device.VirtualDisk) if len(disks) != 1: raise Exception('Failed to find parent disk from snapshot') parent = disks[0].backing vm2Name = '-'.join(['LinkedChild', suffix]) print('Creating %s VM on %s' % (vm2Name, ds)) task.WaitForTasks( [vm2.Destroy() for vm2 in folder.GetVmAll() if vm2.name == vm2Name]) vm2 = vm.CreateQuickDummy(vm2Name, datastoreName=ds, vmxVersion=vmxVersion) vm2DirName = vm2.config.files.snapshotDirectory configSpec = vim.vm.ConfigSpec() configSpec = vmconfig.AddScsiCtlr(configSpec) configSpec = vmconfig.AddScsiDisk(configSpec, datastorename=ds, capacity=1024, backingType=backingType) child = configSpec.deviceChange[1].device.backing child.parent = parent child.deltaDiskFormat = delta # this edit is expected to fail configSpec = vmconfig.AddFloppy( configSpec, type="image", backingName= "[] /these/are/not/the/floppy/images/you/are/looking/for.flp") floppy = configSpec.deviceChange[2].device floppy.backing = None print('Reconfigure %s (1) adding a disk backed by snapshot of %s and (2) ' 'adding floppy backed by non-existent image. Expecting a failure' % (vm2Name, vm1Name)) try: vm.Reconfigure(vm2, configSpec) except Exception as e: pass else: raise Exception( 'Expected exception during %s reconfigure. But it succeeded instead' % vm2Name) print('Destroying %s' % vm2Name) vm.Destroy(vm2) print('Destroying %s' % vm1Name) vm.Destroy(vm1) hostSystem = host.GetHostSystem(si) datastoreBrowser = hostSystem.GetDatastoreBrowser() try: task.WaitForTask(datastoreBrowser.Search(vm1DirName)) except vim.fault.FileNotFound: pass else: raise Exception( "Expected that '%s' will be gone but it still present" % vm1DirName) try: task.WaitForTask(datastoreBrowser.Search(vm2DirName)) except vim.fault.FileNotFound: pass else: raise Exception( "Expected that '%s' will be gone but it still present" % vm2DirName)