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 testAttachDevice(self): """Test VirtualMachine.attachDevice()""" from pyVBox import DVD machine = VirtualMachine.open(self.testVMpath) machine.register() machine.attachDevice(DVD) machine.unregister()
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 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 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 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 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 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 invoke(cls, args): """Invoke the command. Return exit code for program.""" if len(args) == 0: raise Exception("Missing virtual machine filename argument") for filename in args: vm = VirtualMachine.open(args.pop(0)) if vm.isRegistered(): errorMsg("VM \"%s\" is already registered." % vm) else: verboseMsg("Registering VM %s" % vm) vm.register() 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 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 invoke(cls, args): """Invoke the command. Return exit code for program.""" if len(args) == 0: raise Exception("Missing virtual machine filename argument"); for filename in args: vm = VirtualMachine.open(args.pop(0)) if vm.isRegistered(): errorMsg("VM \"%s\" is already registered." % vm) else: verboseMsg("Registering VM %s" % vm) vm.register() return 0
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 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 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 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()
def causeVirtualBoxException(cls): """Should translate VirtualBox exception into a VirtualBoxException""" with ExceptionHandler(): VirtualMachine.open(cls.bogusVMpath)
def testOpen(self): """Test VirtualMachine.open()""" machine = VirtualMachine.open(self.testVMpath) self.assertNotEqual(None, machine.id) self.assertNotEqual(None, machine.name) self.assertEqual(True, machine.isDown())