def _cleanup(self): """Unregister test HD and VM if they are registered.""" # Do machine first to detach any HDs try: machine = VirtualMachine.find(self.testVMname) except: pass else: machine.eject() try: harddisk = HardDisk.find(self.testHDpath) except: pass else: harddisk.close() try: machine = VirtualMachine.find(self.cloneVMname) except Exception as e: pass else: machine.eject() machine.delete() try: clonedisk = HardDisk.find(self.cloneHDpath) except: pass else: clonedisk.close() try: os.remove(self.cloneHDpath) except: pass
def testRegister(self): """Test VirtualMachine.register() and related functions""" machine = VirtualMachine.open(self.testVMpath) machine.register() self.assertEqual(True, machine.isRegistered()) m2 = VirtualMachine.find(machine.name) self.assertEqual(machine.id, m2.id) machine.unregister() self.assertEqual(False, machine.isRegistered())
def testGetAll(self): """Test VirtualMachine.getAll()""" # Make sure we have at least one vm machine = VirtualMachine.open(self.testVMpath) machine.register() vms = VirtualMachine.getAll() self.assertTrue(len(vms) > 0) self.assertTrue(isinstance(vms[0], VirtualMachine)) machine.unregister()
def testGet(self): """Test VirtualMachine.get() method""" machine = VirtualMachine.open(self.testVMpath) # Should fail since not registered yet self.assertRaises( VirtualBoxObjectNotFoundException, VirtualMachine.get, machine.id) machine.register() m2 = VirtualMachine.get(machine.id) self.assertNotEqual(None, m2) self.assertEqual(machine.id, m2.id) machine.unregister()
def testSetAttr(self): """Set setting of VirtualMachine attributes""" machine = VirtualMachine.open(self.testVMpath) machine.register() with machine.lock() as session: mutableMachine = session.getMachine() # Double memory and make sure it persists newMemorySize = machine.memorySize * 2 mutableMachine.memorySize = newMemorySize session.saveSettings() self.assertEqual(newMemorySize, mutableMachine.memorySize) machine.unregister() machine2 = VirtualMachine.open(self.testVMpath) self.assertEqual(newMemorySize, machine2.memorySize)
def invoke(cls, args): """Invoke the command. Return exit code for program.""" if len(args) < 1: raise Exception("Missing source VM name argument") srcVM = VirtualMachine.find(args.pop(0)) if len(args) < 1: raise Exception("Missing target VM name argument") targetName = args.pop(0) message("Cloning %s to %s" % (srcVM, targetName)) cloneVM = srcVM.clone(targetName) # Now clone and attach disks disks = srcVM.getHardDrives() for disk in disks: # Generate new HD filename by prefixing new VM name. # Not the greatest, but not sure what the best way is. targetFilename = os.path.join(disk.dirname(), "%s-%s" % (targetName, disk.name)) # Todo: Need to resolve file already existing here. message("Cloning disk %s to %s (%d bytes)" % (disk, os.path.basename(targetFilename), disk.size)) progress = disk.clone(targetFilename, wait=False) show_progress(progress) cloneHD = HardDisk.find(targetFilename) message("Attaching %s to %s" % (cloneHD, cloneVM)) cloneVM.attachMedium(cloneHD) return 0
def invoke(cls, args): """Invoke the command. Return exit code for program.""" if len(args) == 0: raise Exception("Missing virtual machine name argument") vm = VirtualMachine.find(args.pop(0)) vm.resume() return 0
def invoke(cls, args): """Invoke the command. Return exit code for program.""" mode = "gui" if len(args) == 0: raise Exception("Missing virtual machine name argument"); vm = VirtualMachine.find(args.pop(0)) vm.powerOn(type=mode)
def invoke(cls, args): """Invoke the command. Return exit code for program.""" if len(args) < 1: raise Exception("Missing virtual machine argument") vm = VirtualMachine.find(args.pop(0)) if len(args) < 1: raise Exception("Missing target directory argument") targetDir = args.pop(0) verboseMsg("Backing up %s to %s" % (vm, targetDir)) if vm.isRunning(): verboseMsg("Pausing VM...") # Must wait until paused or will have race condition for lock # on disks. vm.pause(wait=True) atexit.register(vm.resume) # Todo: Backup settings file in some way. # Todo: Want to back up devices than hard drives? disks = vm.getHardDrives() for disk in disks: targetFilename = os.path.join(targetDir, disk.basename()) # Todo: Need to resolve file already existing here. verboseMsg("Backing up disk %s to %s (%d bytes)" % (disk, targetFilename, disk.size)) progress = disk.clone(targetFilename, wait=False) show_progress(progress) # Remove newly created clone from registry clone = HardDisk.find(targetFilename) clone.close()
def invoke(cls, args): """Invoke the command. Return exit code for program.""" mode = "gui" if len(args) == 0: raise Exception("Missing virtual machine name argument") vm = VirtualMachine.find(args.pop(0)) vm.powerOn(type=mode)
def invoke(cls, args): if len(args) == 0: vms = VirtualMachine.getAll() verboseMsg("Registered VMs:") for vm in vms: try: print "\t%s" % vm except Exception as e: errorMsg("Could not display VM: %s" % str(e)) else: for vmName in args: try: vm = VirtualMachine.find(vmName) print_vm(vm) except Exception as e: errorMsg("Could not display information about VM \"%s\": %s" % (vmName, str(e)))
def testAttachDevice(self): """Test VirtualMachine.attachDevice()""" from pyVBox import DVD machine = VirtualMachine.open(self.testVMpath) machine.register() machine.attachDevice(DVD) machine.unregister()
def invoke(cls, args): """Invoke the command. Return exit code for program.""" if len(args) == 0: raise Exception("Missing VM name") vm = VirtualMachine.find(args.pop(0)) snapshot = vm.getCurrentSnapshot() progress = vm.deleteSnapshot(snapshot, wait=False) show_progress(progress)
def invoke(cls, args): if len(args) == 0: vms = VirtualMachine.getAll() verboseMsg("Registered VMs:") for vm in vms: try: print "\t%s" % vm except Exception as e: errorMsg("Could not display VM: %s" % str(e)) else: for vmName in args: try: vm = VirtualMachine.find(vmName) print_vm(vm) except Exception as e: errorMsg( "Could not display information about VM \"%s\": %s" % (vmName, str(e)))
def invoke(cls, args): """Invoke the command. Return exit code for program.""" if len(args) == 0: raise Exception("Missing virtual machine name argument") for name in args: vm = VirtualMachine.find(name) verboseMsg("Ejecting %s" % vm) vm.eject() return 0
def testGetOSType(self): """Test getOSType() method""" machine = VirtualMachine.open(self.testVMpath) osType = machine.getOSType() self.assertNotEqual(None, osType) self.assertNotEqual(None, osType.familyId) self.assertNotEqual(None, osType.familyDescription) self.assertNotEqual(None, osType.id) self.assertNotEqual(None, osType.description)
def invoke(cls, args): """Invoke the command. Return exit code for program.""" if len(args) == 0: raise Exception("Missing virtual machine name argument"); for name in args: vm = VirtualMachine.find(name) verboseMsg("Ejecting %s" % vm) vm.eject() return 0
def testCreate(self): """Test VirtualMachine.create() method""" # If VM already exists and we don't specify forceOverwrite=True # this will raise a VirtualBoxFileError machine = VirtualMachine.create("CreateTestVM", "Ubuntu", forceOverwrite=True) # Clean up machine.unregister() machine.delete()
def invoke(cls, args): """Invoke the command. Return exit code for program.""" vms = VirtualMachine.getAll() for vm in vms: try: print vm.name except VirtualBoxException as e: if verbosityLevel > 1: errorMsg("Unknown machine: %s"%e) return 0
def testEject(self): """Test VirtualMachine.eject()""" machine = VirtualMachine.open(self.testVMpath) machine.register() self.assertEqual(True, machine.isRegistered()) harddisk = HardDisk.open(self.testHDpath) machine.attachMedium(harddisk) machine.eject() self.assertEqual(False, machine.isRegistered()) harddisk.close()
def invoke(cls, args): """Invoke the command. Return exit code for program.""" vms = VirtualMachine.getAll() for vm in vms: try: print vm.name except VirtualBoxException as e: if verbosityLevel > 1: errorMsg("Unknown machine: %s" % e) return 0
def invoke(cls, args): """Invoke the command. Return exit code for program.""" if len(args) == 0: raise Exception("Missing VM name") vm = VirtualMachine.find(args.pop(0)) if len(args) == 0: raise Exception("Missing snapshot name") name = args.pop(0) description = None if len(args) > 0: description = args.pop(0) vm.takeSnapshot(name, description)
def invoke(cls, args): """Invoke the command. Return exit code for program.""" if len(args) == 0: raise Exception("Missing virtual machine name argument"); for filename in args: vm = VirtualMachine.find(args.pop(0)) if vm.isRegistered(): verboseMsg("Unregistering VM %s" % vm) vm.unregister() else: errorMsg("VM \"%s\" is not registered." % vm) return 0
def invoke(cls, args): """Invoke the command. Return exit code for program.""" if len(args) == 0: raise Exception("Missing virtual machine name argument") for filename in args: vm = VirtualMachine.find(args.pop(0)) if vm.isRegistered(): verboseMsg("Unregistering VM %s" % vm) vm.unregister() else: errorMsg("VM \"%s\" is not registered." % vm) return 0
def invoke(cls, args): """Invoke the command. Return exit code for program.""" if len(args) < 1: raise Exception("Missing virtual machine argument") vm = VirtualMachine.find(args.pop(0)) if len(args) < 1: raise Exception("Missing hard disk filenames") for hd in args: hd = cls.harddisk(hd) verboseMsg("Attaching %s" % hd) vm.attachMedium(hd) return 0
def testGetStorageControllers(self): """Test VirtualMachine methods for getting StorageControllers""" machine = VirtualMachine.open(self.testVMpath) controllers = machine.getStorageControllers() self.assertNotEqual(None, controllers) for controller in controllers: c = machine.getStorageControllerByName(controller.name) self.assertNotEqual(None, c) c = machine.getStorageControllerByInstance(controller.instance) self.assertNotEqual(None, c) self.assertEqual(True, machine.doesStorageControllerExist(controller.name))
def testLock(self): """Test VirtualMachine.lock()""" machine = VirtualMachine.open(self.testVMpath) machine.register() self.assertTrue(machine.isUnlocked()) with machine.lock() as session: self.assertNotEqual(session, None) self.assertTrue(machine.isLocked()) m2 = session.getMachine() self.assertNotEqual(m2, None) self.assertTrue(machine.isUnlocked()) machine.unregister()
def testAttachMedium(self): """Test VirtualMachine.attachMedium() and related functions""" machine = VirtualMachine.open(self.testVMpath) machine.register() harddisk = HardDisk.open(self.testHDpath) machine.attachMedium(harddisk) mediums = machine.getAttachedMediums() self.assertEqual(1, len(mediums)) self.assertEqual(mediums[0].deviceType, HardDisk) machine.detachMedium(harddisk) machine.unregister() harddisk.close()
def testPowerOn(self): """Test powering on a VM.""" machine = VirtualMachine.open(self.testVMpath) machine.register() harddisk = HardDisk.open(self.testHDpath) machine.attachMedium(harddisk) machine.powerOn(type="vrdp") machine.waitUntilRunning() sleep(5) machine.powerOff(wait=True) machine.detachMedium(harddisk) harddisk.close() machine.unregister()
def testGetHardDrives(self): """Test VirtualMachine.getHardDrives() method""" machine = VirtualMachine.open(self.testVMpath) machine.register() # Attach something so it's interesting harddisk = HardDisk.open(self.testHDpath) machine.attachMedium(harddisk) mediums = machine.getAttachedMediums() self.assertEqual(1, len(mediums)) self.assertEqual(mediums[0].deviceType, HardDisk) hds = machine.getHardDrives() self.assertNotEqual(hds, None) self.assertTrue(isinstance(hds, list)) self.assertEqual(len(hds), 1) machine.detachMedium(harddisk) machine.unregister()
def testStorageController(self): """Test StorageController attributes""" machine = VirtualMachine.open(self.testVMpath) controllers = machine.getStorageControllers() # Should be an IDE controller and a SATA controller self.assertTrue(len(controllers) == 2) for controller in controllers: self.assertNotEqual(None, controller.name) self.assertNotEqual(None, controller.bus) self.assertNotEqual(None, controller.controllerType) self.assertNotEqual(None, controller.instance) self.assertNotEqual(None, controller.maxDevicesPerPortCount) self.assertNotEqual(None, controller.maxPortCount) self.assertNotEqual(None, controller.minPortCount) self.assertNotEqual(None, controller.portCount) self.assertNotEqual(None, str(controller))
def testMediumAttachment(self): """Test MediumAttachment attributes""" machine = VirtualMachine.open(self.testVMpath) attachments = machine.getMediumAttachments() # Should be one attached DVD drive self.assertTrue(len(attachments) == 1) for attachment in attachments: self.assertNotEqual(None, attachment.controller) # attachment.medium should be None in this case for a # empty removable devices self.assertEqual(None, attachment.medium) self.assertNotEqual(None, attachment.port) self.assertNotEqual(None, attachment.device) self.assertNotEqual(None, attachment.type) self.assertTrue(isinstance(attachment.type, Device)) self.assertTrue(isinstance(attachment.type, DVD)) self.assertNotEqual(None, attachment.passthrough)
def testSnapshot(self): """Test taking snapshot of a VM.""" return # Issue: https://github.com/von/pyVBox/issues/5 snapshotName = "Test Snapshot" machine = VirtualMachine.open(self.testVMpath) machine.register() self.assertEqual(None, machine.getCurrentSnapshot()) # This sleep seems to keep takeSnapshot() from hanging # at least all of the time. # Issue: https://github.com/von/pyVBox/issues/5 sleep(2) machine.takeSnapshot(snapshotName) snapshot = machine.getCurrentSnapshot() self.assertNotEqual(snapshot, None) self.assertEqual(snapshotName, snapshot.name) machine.deleteSnapshot(snapshot) self.assertEqual(None, machine.getCurrentSnapshot()) machine.unregister()
def testAddStorageController(self): """Test adding and removing of StorageController to a VirtualMachine""" # Currently the removeStorageController() method is failing with # an 'Operation aborted' and the test VM fails to boot if I leave # the added storage controllers, which messes up subsequent tests. # Issue: https://github.com/von/pyVBox/issues/2 controllerName="TestController" machine = VirtualMachine.open(self.testVMpath) machine.register() controller = machine.addStorageController(Constants.StorageBus_SCSI, name=controllerName) self.assertNotEqual(None, controller) self.assertEqual(controllerName, controller.name) self.assertEqual(True, machine.doesStorageControllerExist(controller.name)) machine.removeStorageController(controller.name) # Trying to use controller.name after remove fails. # Not sure if that's a bug or not. self.assertEqual(False, machine.doesStorageControllerExist(controllerName))
def invoke(cls, args): """Invoke the command. Return exit code for program.""" mode = "gui" # or "vrdp" if len(args) < 1: raise Exception("Missing virtual machine filename argument") vm = VirtualMachine.open(args.pop(0)) atexit.register(vm.eject) if not vm.isRegistered(): vm.register() for hd in args: hd = HardDisk.find(hd) vm.attachMedium(hd) verboseMsg("Starting VM in %s mode" % mode) vm.powerOn(type=mode) # Wait until machine is running or we have a race condition # where it still might be down when we call waitUntilDown() vm.waitUntilRunning() verboseMsg("VM started. Waiting until power down...") vm.waitUntilDown() verboseMsg("VM powered down.") # Let atexit clean up return 0
def testClone(self): """Test VirtualMachine.clone() method""" machine = VirtualMachine.open(self.testVMpath) newMachine = machine.clone(self.cloneVMname) self.assertEqual(self.cloneVMname, newMachine.name) self.assertEqual(machine.description, newMachine.description) self.assertEqual(machine.CPUCount, newMachine.CPUCount) self.assertEqual(machine.memorySize, newMachine.memorySize) self.assertEqual(machine.VRAMSize, newMachine.VRAMSize) self.assertEqual(machine.accelerate3DEnabled, newMachine.accelerate3DEnabled) self.assertEqual(machine.accelerate2DVideoEnabled, newMachine.accelerate2DVideoEnabled) self.assertEqual(machine.monitorCount, newMachine.monitorCount) with newMachine.lock() as session: controllers = machine.getStorageControllers() newControllers = newMachine.getStorageControllers() self.assertEqual(len(controllers), len(newControllers)) # Todo: compare individual controllers # Clean up newMachine.unregister() newMachine.delete()