def testSnapshotTreeDepth(self):
        """
        Test snapshot tree depth.
        """
        reporter.testStart('snapshotTreeDepth')

        try:
            oVM = self.createTestVM('test-snap', 1, None, 4)
            assert oVM is not None

            # modify the VM config, create and attach empty HDD
            oSession = self.openSession(oVM)
            sHddPath = os.path.join(self.sScratchPath, 'TestSnapEmpty.vdi')
            fRc = True
            fRc = fRc and oSession.createAndAttachHd(sHddPath, cb=1024*1024, sController='SATA Controller', fImmutable=False)
            fRc = fRc and oSession.saveSettings()

            # take 250 snapshots (snapshot tree depth limit)
            for i in range(1, 251):
                fRc = fRc and oSession.takeSnapshot('Snapshot ' + str(i))
            fRc = oSession.close() and fRc

            # unregister and re-register to test loading of settings
            sSettingsFile = oVM.settingsFilePath
            reporter.log('unregistering VM')
            oVM.unregister(vboxcon.CleanupMode_DetachAllReturnNone)
            oVBox = self.oVBoxMgr.getVirtualBox()
            reporter.log('opening VM %s, testing config reading' % (sSettingsFile))
            oVM = oVBox.openMachine(sSettingsFile)

            assert fRc is True
        except:
            reporter.errorXcpt()

        return reporter.testDone()[1] == 0
    def moveTo(self, sLocation, aoMediumAttachments):
        for oAttachment in aoMediumAttachments:
            try:
                oMedium = oAttachment.medium
                reporter.log('Move medium "%s" to "%s"' % (oMedium.name, sLocation,))
            except:
                reporter.errorXcpt('failed to get the medium from the IMediumAttachment "%s"' % (oAttachment))

            if self.oTstDrv.fpApiVer >= 5.3 and self.oTstDrv.uRevision > 124748:
                try:
                    oProgress = vboxwrappers.ProgressWrapper(oMedium.moveTo(sLocation), self.oTstDrv.oVBoxMgr, self.oTstDrv,
                                                             'move "%s"' % (oMedium.name,));
                except:
                    return reporter.errorXcpt('Medium::moveTo("%s") for medium "%s" failed' % (sLocation, oMedium.name,));
            else:
                try:
                    oProgress = vboxwrappers.ProgressWrapper(oMedium.setLocation(sLocation), self.oTstDrv.oVBoxMgr, self.oTstDrv,
                                                             'move "%s"' % (oMedium.name,));
                except:
                    return reporter.errorXcpt('Medium::setLocation("%s") for medium "%s" failed' % (sLocation, oMedium.name,));


            oProgress.wait()
            if oProgress.logResult() is False:
                return False
        return True
Example #3
0
    def testIGuest_additionsVersion(self, oGuest):
        """
        Returns False if no version string could be obtained, otherwise True
        even though errors are logged.
        """
        try:
            sVer = oGuest.additionsVersion
        except:
            reporter.errorXcpt("Getting the additions version failed.")
            return False
        reporter.log('IGuest::additionsVersion="%s"' % (sVer,))

        if sVer.strip() == "":
            reporter.error("IGuest::additionsVersion is empty.")
            return False

        if sVer != sVer.strip():
            reporter.error('IGuest::additionsVersion is contains spaces: "%s".' % (sVer,))

        asBits = sVer.split(".")
        if len(asBits) < 3:
            reporter.error(
                'IGuest::additionsVersion does not contain at least tree dot separated fields: "%s" (%d).'
                % (sVer, len(asBits))
            )

        ## @todo verify the format.
        return True
    def _sudoExecuteSync(self, asArgs, sInput):
        """
        Executes a sudo child process synchronously.
        Returns a tuple [True, 0] if the process executed successfully
        and returned 0, otherwise [False, rc] is returned.
        """
        reporter.log('Executing [sudo]: %s' % (asArgs, ));
        reporter.flushall();
        fRc = True;
        sOutput = '';
        sError = '';
        try:
            oProcess = utils.sudoProcessPopen(asArgs, stdout=subprocess.PIPE, stdin=subprocess.PIPE,
                                              stderr=subprocess.PIPE, shell = False, close_fds = False);

            sOutput, sError = oProcess.communicate(sInput);
            iExitCode  = oProcess.poll();

            if iExitCode is not 0:
                fRc = False;
        except:
            reporter.errorXcpt();
            fRc = False;
        reporter.log('Exit code [sudo]: %s (%s)' % (fRc, asArgs));
        return (fRc, str(sOutput), str(sError));
Example #5
0
    def testRunUnitTestsSet(self, sTestCasePattern, sTestCaseSubDir):
        """
        Run subset of the unit tests set.
        """

        # Open /dev/null for use as stdin further down.
        try:
            oDevNull = open(os.path.devnull, 'w+');
        except:
            oDevNull = None;

        # Determin the host OS specific exclusion lists.
        dTestCasesBuggyForHostOs = self.kdTestCasesBuggyPerOs.get(utils.getHostOs(), []);
        dTestCasesBuggyForHostOs.update(self.kdTestCasesBuggyPerOs.get(utils.getHostOsDotArch(), []));

        #
        # Process the file list and run everything looking like a testcase.
        #
        for sFilename in sorted(os.listdir(os.path.join(self.sUnitTestsPathBase, sTestCaseSubDir))):
            # Separate base and suffix and morph the base into something we
            # can use for reporting and array lookups.
            sName, sSuffix = os.path.splitext(sFilename);
            if sTestCaseSubDir != '.':
                sName = sTestCaseSubDir + '/' + sName;

            # Basic exclusion.
            if   not re.match(sTestCasePattern, sFilename) \
              or sSuffix in self.kasSuffixBlackList:
                reporter.log('"%s" is not a test case.' % (sFilename,))
                continue

            # Check if the testcase is black listed or buggy before executing it.
            if self._isExcluded(sName, self.kdTestCasesBlackList):
                # (No testStart/Done or accounting here!)
                reporter.log('%s: SKIPPED (blacklisted)' % (sName,));

            elif self._isExcluded(sName, self.kdTestCasesBuggy):
                reporter.testStart(sName);
                reporter.log('%s: Skipping, buggy in general.' % (sName,));
                reporter.testDone(fSkipped = True);
                self.cSkipped += 1;

            elif self._isExcluded(sName, dTestCasesBuggyForHostOs):
                reporter.testStart(sName);
                reporter.log('%s: Skipping, buggy on %s.' % (sName, utils.getHostOs(),));
                reporter.testDone(fSkipped = True);
                self.cSkipped += 1;

            else:
                sFullPath = os.path.normpath(os.path.join(self.sUnitTestsPathBase, os.path.join(sTestCaseSubDir, sFilename)));
                reporter.testStart(sName);
                try:
                    fSkipped = self._executeTestCase(sName, sFullPath, sTestCaseSubDir, oDevNull);
                except:
                    reporter.errorXcpt('!*!');
                    self.cFailed += 1;
                    fSkipped = False;
                reporter.testDone(fSkipped);
Example #6
0
 def interruptWaitEventsThreadProc(self):
     """ Thread procedure that's used for waking up the main thread. """
     time.sleep(2);
     try:
         rc2 = self.oVBoxMgr.interruptWaitEvents();
     except:
         reporter.errorXcpt();
     else:
         if rc2 is True:
             return True;
         reporter.error('interruptWaitEvents returned "%s" when called from other thread, expected True' % (rc2,));
     return False;
    def testImportOvaAsOvf(self, sOva):
        """
        Unpacks the OVA into a subdirectory in the scratch area and imports it as an OVF.
        """
        oVirtualBox = self.oTstDrv.oVBoxMgr.getVirtualBox();

        sTmpDir = os.path.join(self.oTstDrv.sScratchPath, os.path.split(sOva)[1] + '-ovf');
        sOvf    = os.path.join(sTmpDir, os.path.splitext(os.path.split(sOva)[1])[0] + '.ovf');

        #
        # Unpack
        #
        try:
            os.mkdir(sTmpDir, 0o755);
            oTarFile = tarfile.open(sOva, 'r:*');
            oTarFile.extractall(sTmpDir);
            oTarFile.close();
        except:
            return reporter.errorXcpt('Unpacking "%s" to "%s" for OVF style importing failed' % (sOvf, sTmpDir,));

        #
        # Import.
        #
        try:
            oAppliance2 = oVirtualBox.createAppliance();
        except:
            return reporter.errorXcpt('IVirtualBox::createAppliance failed (#2)');

        try:
            oProgress = vboxwrappers.ProgressWrapper(oAppliance2.read(sOvf), self.oTstDrv.oVBoxMgr, self.oTstDrv,
                                                     'read "%s"' % (sOvf,));
        except:
            return reporter.errorXcpt('IAppliance::read("%s") failed' % (sOvf,));
        oProgress.wait();
        if oProgress.logResult() is False:
            return False;

        try:
            oAppliance2.interpret();
        except:
            return reporter.errorXcpt('IAppliance::interpret() failed on "%s"' % (sOvf,));

        try:
            oProgress = vboxwrappers.ProgressWrapper(oAppliance2.importMachines([]),
                                                     self.oTstDrv.oVBoxMgr, self.oTstDrv, 'importMachines "%s"' % (sOvf,));
        except:
            return reporter.errorXcpt('IAppliance::importMachines failed on "%s"' % (sOvf,));
        oProgress.wait();
        if oProgress.logResult() is False:
            return False;

        return True;
 def _sudoExecuteSync(self, asArgs):
     """
     Executes a sudo child process synchronously.
     Returns True if the process executed successfully and returned 0,
     otherwise False is returned.
     """
     reporter.log('Executing [sudo]: %s' % (asArgs, ));
     try:
         iRc = utils.sudoProcessCall(asArgs, shell = False, close_fds = False);
     except:
         reporter.errorXcpt();
         return False;
     reporter.log('Exit code [sudo]: %s (%s)' % (iRc, asArgs));
     return iRc is 0;
    def cloneMedium(self, oSrcHd, oTgtHd):
        """
        Clones medium into target medium.
        """
        try:
            oProgressCom = oSrcHd.cloneTo(oTgtHd, (vboxcon.MediumVariant_Standard, ), None);
            oProgress = vboxwrappers.ProgressWrapper(oProgressCom, self.oVBoxMgr, self.oVBox.oTstDrv,
                                                     'clone base disk %s to %s' % (oSrcHd.name, oTgtHd.name));
            oProgress.wait(cMsTimeout = 60 * 1000);
            oProgress.logResult();
        except:
            reporter.errorXcpt('failed to clone medium %s to %s' % (oSrcHd.name, oTgtHd.name));
            return False;

        return True;
    def moveVMToLocation(self, sLocation, oVM):
        """
        Document me here, not with hashes above.
        """
        fRc = True
        try:

            ## @todo r=bird: Too much unncessary crap inside try clause.  Only oVM.moveTo needs to be here.
            ##               Though, you could make an argument for oVM.name too, perhaps.

            # move machine
            reporter.log('Moving machine "%s" to the "%s"' % (oVM.name, sLocation))
            sType = 'basic'
            oProgress = vboxwrappers.ProgressWrapper(oVM.moveTo(sLocation, sType), self.oTstDrv.oVBoxMgr, self.oTstDrv,
                                                     'moving machine "%s"' % (oVM.name,))

        except:
            return reporter.errorXcpt('Machine::moveTo("%s") for machine "%s" failed' % (sLocation, oVM.name,))

        oProgress.wait()
        if oProgress.logResult() is False:
            fRc = False
            reporter.log('Progress object returned False')
        else:
            fRc = True

        return fRc
 def _sudoExecuteSync(self, asArgs):
     """
     Executes a sudo child process synchronously.
     Returns a tuple [True, 0] if the process executed successfully
     and returned 0, otherwise [False, rc] is returned.
     """
     reporter.log('Executing [sudo]: %s' % (asArgs, ));
     reporter.flushall();
     iRc = 0;
     try:
         iRc = utils.sudoProcessCall(asArgs, shell = False, close_fds = False);
     except:
         reporter.errorXcpt();
         return (False, 0);
     reporter.log('Exit code [sudo]: %s (%s)' % (iRc, asArgs));
     return (iRc is 0, iRc);
    def _persistentVarUnset(self, sVar):
        """
        Unsets a persistent variable.

        Returns True on success, False + reporter.error on failure.

        May raise exception if the variable name is invalid or something
        unexpected happens.
        """
        sFull = self.__persistentVarCalcName(sVar);
        if os.path.exists(sFull):
            try:
                os.unlink(sFull);
            except:
                reporter.errorXcpt('Error unlinking "%s"' % (sFull,));
                return False;
        return True;
Example #13
0
    def testInstallAdditions(self, oSession, oTxsSession, oTestVm):
        """
        Tests installing the guest additions
        """
        if oTestVm.isWindows():
            (fRc, oTxsSession) = self.testWindowsInstallAdditions(oSession, oTxsSession, oTestVm)
        else:
            reporter.error(
                "Guest Additions installation not implemented for %s yet! (%s)" % (oTestVm.sKind, oTestVm.sVmName)
            )
            fRc = False

        #
        # Verify installation of Guest Additions using commmon bits.
        #
        if fRc is True:
            #
            # Wait for the GAs to come up.
            #

            ## @todo need to signed up for a OnAdditionsStateChanged and wait runlevel to
            #  at least reach Userland.

            #
            # Check if the additions are operational.
            #
            try:
                oGuest = oSession.o.console.guest
            except:
                reporter.errorXcpt("Getting IGuest failed.")
                return (False, oTxsSession)

            # Check the additionsVersion attribute. It must not be empty.
            reporter.testStart("IGuest::additionsVersion")
            fRc = self.testIGuest_additionsVersion(oGuest)
            reporter.testDone()

            reporter.testStart("IGuest::additionsRunLevel")
            self.testIGuest_additionsRunLevel(oGuest, oTestVm)
            reporter.testDone()

            ## @todo test IAdditionsFacilities.

        return (fRc, oTxsSession)
    def testIt(self):
        """
        Execute the sub-testcase.
        """
        fRc = True;

        # Import a set of simple OVAs.
        # Note! Manifests generated by ovftool 4.0.0 does not include the ovf, while the ones b 4.1.0 does.
        for sOva in (
            # t1 is a plain VM without any disks, ovftool 4.0 export from fusion
            'tdAppliance1-t1.ova',
            # t2 is a plain VM with one disk. Both 4.0 and 4.1.0 exports.
            'tdAppliance1-t2.ova',
            'tdAppliance1-t2-ovftool-4.1.0.ova',
            # t3 is a VM with one gzipped disk and selecting SHA256 on the ovftool cmdline (--compress=9 --shaAlgorithm=sha256).
            'tdAppliance1-t3.ova',
            'tdAppliance1-t3-ovftool-4.1.0.ova',
            # t4 is a VM with with two gzipped disk, SHA256 and a (self) signed manifest (--privateKey=./tdAppliance1-t4.pem).
            'tdAppliance1-t4.ova',
            'tdAppliance1-t4-ovftool-4.1.0.ova',
            # t5 is a VM with with one gzipped disk, SHA1 and a manifest signed by a valid (2016) DigiCert code signing cert.
            'tdAppliance1-t5.ova',
            'tdAppliance1-t5-ovftool-4.1.0.ova',
            # t6 is a VM with with one gzipped disk, SHA1 and a manifest signed by a certificate issued by the t4 certificate,
            # thus it should be impossible to establish a trusted path to a root CA.
            'tdAppliance1-t6.ova',
            'tdAppliance1-t6-ovftool-4.1.0.ova',
            # t7 is based on tdAppliance1-t2-ovftool-4.1.0.ova and has modified to have an invalid InstanceID as well as an
            # extra readme file.  It was tarred up using bsdtar 2.4.12 on windows, so it uses a slightly different tar format and
            # have different file attributes.
            'tdAppliance1-t7-bad-instance.ova',
            ):
            reporter.testStart(sOva);
            try:
                fRc = self.testImportOva(os.path.join(g_ksValidationKitDir, 'tests', 'api', sOva)) and fRc;
                fRc = self.testImportOvaAsOvf(os.path.join(g_ksValidationKitDir, 'tests', 'api', sOva)) and fRc;
            except:
                reporter.errorXcpt();
                fRc = False;
            fRc = reporter.testDone() and fRc;

        ## @todo more stuff
        return fRc;
Example #15
0
    def testEventQueueWaiting(self):
        """
        Test event queue waiting.
        """
        reporter.testStart('waitForEvents');

        # Check return values and such.
        for cMsTimeout in (0, 1, 2, 3, 256, 1000, 0):
            iLoop = 0;
            while True:
                try:
                    rc = self.oVBoxMgr.waitForEvents(cMsTimeout);
                except:
                    reporter.errorXcpt();
                    break;
                if not isinstance(rc, types.IntType):
                    reporter.error('waitForEvents returns non-integer type');
                    break;
                if rc == 1:
                    break;
                if rc != 0:
                    reporter.error('waitForEvents returns "%s", expected 0 or 1' % (rc,));
                    break;
                iLoop += 1;
                if iLoop > 10240:
                    reporter.error('waitForEvents returns 0 (success) %u times. '
                                   'Expected 1 (timeout/interrupt) after a call or two.'
                                   % (iLoop,));
                    break;
            if reporter.testErrorCount() != 0:
                break;

        # Check that we get an exception when trying to call the method from
        # a different thread.
        reporter.log('If running a debug build, you will see an ignored assertion now. Please ignore it.')
        sVBoxAssertSaved = os.environ.get('VBOX_ASSERT', 'breakpoint');
        os.environ['VBOX_ASSERT'] = 'ignore';
        oThread = threading.Thread(target=self.testEventQueueWaitingThreadProc);
        oThread.start();
        oThread.join();
        os.environ['VBOX_ASSERT'] = sVBoxAssertSaved;

        return reporter.testDone()[1] == 0;
    def _executeSync(self, asArgs, fMaySkip = False):
        """
        Executes a child process synchronously.

        Returns True if the process executed successfully and returned 0.
        Returns None if fMaySkip is true and the child exits with RTEXITCODE_SKIPPED.
        Returns False for all other cases.
        """
        reporter.log('Executing: %s' % (asArgs, ));
        reporter.flushall();
        try:
            iRc = utils.processCall(asArgs, shell = False, close_fds = False);
        except:
            reporter.errorXcpt();
            return False;
        reporter.log('Exit code: %s (%s)' % (iRc, asArgs));
        if fMaySkip and iRc == rtexitcode.RTEXITCODE_SKIPPED:
            return None;
        return iRc is 0;
    def _persistentVarSet(self, sVar, sValue = ''):
        """
        Sets a persistent variable.

        Returns True on success, False + reporter.error on failure.

        May raise exception if the variable name is invalid or something
        unexpected happens.
        """
        sFull = self.__persistentVarCalcName(sVar);
        try:
            oFile = open(sFull, 'w');
            if len(sValue) > 0:
                oFile.write(sValue.encode('utf-8'));
            oFile.close();
        except:
            reporter.errorXcpt('Error creating "%s"' % (sFull,));
            return False;
        return True;
    def testImportOva(self, sOva):
        """ xxx """
        oVirtualBox = self.oTstDrv.oVBoxMgr.getVirtualBox();

        #
        # Import it as OVA.
        #
        try:
            oAppliance = oVirtualBox.createAppliance();
        except:
            return reporter.errorXcpt('IVirtualBox::createAppliance failed');

        try:
            oProgress = vboxwrappers.ProgressWrapper(oAppliance.read(sOva), self.oTstDrv.oVBoxMgr, self.oTstDrv,
                                                     'read "%s"' % (sOva,));
        except:
            return reporter.errorXcpt('IAppliance::read("%s") failed' % (sOva,));
        oProgress.wait();
        if oProgress.logResult() is False:
            return False;

        try:
            oAppliance.interpret();
        except:
            return reporter.errorXcpt('IAppliance::interpret() failed on "%s"' % (sOva,));

        #
        try:
            oProgress = vboxwrappers.ProgressWrapper(oAppliance.importMachines([]),
                                                     self.oTstDrv.oVBoxMgr, self.oTstDrv, 'importMachines "%s"' % (sOva,));
        except:
            return reporter.errorXcpt('IAppliance::importMachines failed on "%s"' % (sOva,));
        oProgress.wait();
        if oProgress.logResult() is False:
            return False;

        #
        # Export the
        #
        ## @todo do more with this OVA. Like untaring it and loading it as an OVF.  Export it and import it again.

        return True;
Example #19
0
    def testIGuest_additionsRunLevel(self, oGuest, oTestVm):
        """
        Do run level tests.
        """
        if oTestVm.isLoggedOntoDesktop():
            eExpectedRunLevel = vboxcon.AdditionsRunLevelType_Desktop;
        else:
            eExpectedRunLevel = vboxcon.AdditionsRunLevelType_Userland;

        ## @todo Insert wait for the desired run level.
        try:
            iLevel = oGuest.additionsRunLevel;
        except:
            reporter.errorXcpt('Getting the additions run level failed.');
            return False;
        reporter.log('IGuest::additionsRunLevel=%s' % (iLevel,));

        if iLevel != eExpectedRunLevel:
            reporter.error('Expected runlevel %d, found %d instead' % (eExpectedRunLevel, iLevel));
        return True;
    def _darwinUnmountDmg(self, fIgnoreError):
        """
        Umount any DMG on at the default mount point.
        """
        sMountPath = self._darwinDmgPath();
        if not os.path.exists(sMountPath):
            return True;

        # Unmount.
        fRc = self._executeSync(['hdiutil', 'detach', sMountPath ]);
        if not fRc and not fIgnoreError:
            reporter.error('Failed to unmount DMG at %s' % sMountPath);

        # Remove dir.
        try:
            os.rmdir(sMountPath);
        except:
            if not fIgnoreError:
                reporter.errorXcpt('Failed to remove directory %s' % sMountPath);
        return fRc;
    def testMediumTreeDepth(self):
        """
        Test medium tree depth.
        """
        reporter.testStart('mediumTreeDepth')

        try:
            oVM = self.createTestVM('test-medium', 1, None, 4)
            assert oVM is not None

            # create chain with 300 disk images (medium tree depth limit)
            fRc = True
            oSession = self.openSession(oVM)
            for i in range(1, 301):
                sHddPath = os.path.join(self.sScratchPath, 'Test' + str(i) + '.vdi')
                if i is 1:
                    oHd = oSession.createBaseHd(sHddPath, cb=1024*1024)
                else:
                    oHd = oSession.createDiffHd(oHd, sHddPath)
                if oHd is None:
                    fRc = False
                    break

            # modify the VM config, attach HDD
            fRc = fRc and oSession.attachHd(sHddPath, sController='SATA Controller', fImmutable=False, fForceResource=False)
            fRc = fRc and oSession.saveSettings()
            fRc = oSession.close() and fRc

            # unregister and re-register to test loading of settings
            sSettingsFile = oVM.settingsFilePath
            reporter.log('unregistering VM')
            oVM.unregister(vboxcon.CleanupMode_DetachAllReturnNone)
            oVBox = self.oVBoxMgr.getVirtualBox()
            reporter.log('opening VM %s, testing config reading' % (sSettingsFile))
            oVM = oVBox.openMachine(sSettingsFile)

            assert fRc is True
        except:
            reporter.errorXcpt()

        return reporter.testDone()[1] == 0
Example #22
0
    def actionExecute(self):
        """
        Execute the testcase.
        """
        fRc = True;

        # Import a set of simple OVAs.
        # Note! Manifests generated by ovftool 4.0.0 does not include the ovf, while the ones b 4.1.0 does.
        for sOva in (
            # t1 is a plain VM without any disks, ovftool 4.0 export from fusion
            'tdAppliance1-t1.ova',
            # t2 is a plain VM with one disk. Both 4.0 and 4.1.0 exports.
            'tdAppliance1-t2.ova',
            'tdAppliance1-t2-ovftool-4.1.0.ova',
            # t3 is a VM with one gzipped disk and selecting SHA256 on the ovftool cmdline (--compress=9 --shaAlgorithm=sha256).
            'tdAppliance1-t3.ova',
            'tdAppliance1-t3-ovftool-4.1.0.ova',
            # t4 is a VM with with two gzipped disk, SHA256 and a (self) signed manifest (--privateKey=./tdAppliance1-t4.pem).
            'tdAppliance1-t4.ova',
            'tdAppliance1-t4-ovftool-4.1.0.ova',
            # t5 is a VM with with one gzipped disk, SHA1 and a manifest signed by a valid (2016) DigiCert code signing certificate.
            'tdAppliance1-t5.ova',
            'tdAppliance1-t5-ovftool-4.1.0.ova',
            # t6 is a VM with with one gzipped disk, SHA1 and a manifest signed by a certificate issued by the t4 certificate,
            # thus it should be impossible to establish a trusted path to a root CA.
            'tdAppliance1-t6.ova',
            'tdAppliance1-t6-ovftool-4.1.0.ova',
            ):
            reporter.testStart(sOva);
            try:
                fRc = self.testImportOva(os.path.join(g_ksValidationKitDir, 'tests', 'api', sOva)) and fRc;
                fRc = self.testImportOvaAsOvf(os.path.join(g_ksValidationKitDir, 'tests', 'api', sOva)) and fRc;
            except:
                reporter.errorXcpt();
                fRc = False;
            fRc = reporter.testDone() and fRc;

        ## @todo more stuff
        return fRc;
Example #23
0
    def testIGuest_additionsRunLevel(self, oGuest, oTestVm):
        """
        Do run level tests.
        """
        if oTestVm.isLoggedOntoDesktop():
            eExpectedRunLevel = vboxcon.AdditionsRunLevelType_Desktop
        else:
            eExpectedRunLevel = vboxcon.AdditionsRunLevelType_Userland

        ## @todo Insert wait for the desired run level.
        try:
            iLevel = oGuest.additionsRunLevel
        except:
            reporter.errorXcpt("Getting the additions run level failed.")
            return False
        reporter.log("IGuest::additionsRunLevel=%s" % (iLevel,))

        if iLevel != eExpectedRunLevel:
            pass
            ## @todo We really need that wait!!
            # reporter.error('Expected runlevel %d, found %d instead' % (eExpectedRunLevel, iLevel));
        return True
    def _persistentVarGet(self, sVar):
        """
        Gets the value of a persistent variable.

        Returns variable value on success.
        Returns None if the variable doesn't exist or if an
        error (reported) occured.

        May raise exception if the variable name is invalid or something
        unexpected happens.
        """
        sFull = self.__persistentVarCalcName(sVar);
        if not os.path.exists(sFull):
            return None;
        try:
            oFile = open(sFull, 'r');
            sValue = oFile.read().decode('utf-8');
            oFile.close();
        except:
            reporter.errorXcpt('Error creating "%s"' % (sFull,));
            return None;
        return sValue;
    def openMedium(self, sHd, fImmutable = False):
        """
        Opens medium in readonly mode.
        Returns Medium object on success and None on failure.  Error information is logged.
        """
        sFullName = self.oVBox.oTstDrv.getFullResourceName(sHd);
        try:
            oHd = self.oVBox.findHardDisk(sFullName);
        except:
            try:
                if self.fpApiVer >= 4.1:
                    oHd = self.oVBox.openMedium(sFullName, vboxcon.DeviceType_HardDisk, vboxcon.AccessMode_ReadOnly, False);
                elif self.fpApiVer >= 4.0:
                    oHd = self.oVBox.openMedium(sFullName, vboxcon.DeviceType_HardDisk, vboxcon.AccessMode_ReadOnly);
                else:
                    oHd = self.oVBox.openHardDisk(sFullName, vboxcon.AccessMode_ReadOnly, False, "", False, "");

            except:
                reporter.errorXcpt('failed to open hd "%s"' % (sFullName));
                return None;

        try:
            if fImmutable:
                oHd.type = vboxcon.MediumType_Immutable;
            else:
                oHd.type = vboxcon.MediumType_Normal;

        except:
            if fImmutable:
                reporter.errorXcpt('failed to set hd "%s" immutable' % (sHd));
            else:
                reporter.errorXcpt('failed to set hd "%s" normal' % (sHd));

            return None;

        return oHd;
Example #26
0
    def writeToDisk(self, sAltBase=None):
        """
        Writes out the files to disk.
        Returns True on success, False + error logging on failure.
        """

        # We only need to flip DOS slashes to unix ones, since windows & OS/2 can handle unix slashes.
        fDosToUnix = self.fDosStyle and os.path.sep != '\\'

        # The directories:
        for oDir in self.aoDirs:
            sPath = oDir.sPath
            if sAltBase:
                if fDosToUnix:
                    sPath = sAltBase + sPath[len(self.sBasePath):].replace(
                        '\\', os.path.sep)
                else:
                    sPath = sAltBase + sPath[len(self.sBasePath):]
            elif fDosToUnix:
                sPath = sPath.replace('\\', os.path.sep)

            try:
                os.mkdir(sPath, 0o770)
            except:
                return reporter.errorXcpt('mkdir(%s) failed' % (sPath, ))

        # The files:
        for oFile in self.aoFiles:
            sPath = oFile.sPath
            if sAltBase:
                if fDosToUnix:
                    sPath = sAltBase + sPath[len(self.sBasePath):].replace(
                        '\\', os.path.sep)
                else:
                    sPath = sAltBase + sPath[len(self.sBasePath):]
            elif fDosToUnix:
                sPath = sPath.replace('\\', os.path.sep)

            try:
                oOutFile = open(sPath, 'wb')
                # pylint: disable=consider-using-with
            except:
                return reporter.errorXcpt('open(%s, "wb") failed' % (sPath, ))
            try:
                if sys.version_info[0] < 3:
                    oOutFile.write(bytes(oFile.abContent))
                else:
                    oOutFile.write(oFile.abContent)
            except:
                try:
                    oOutFile.close()
                except:
                    pass
                return reporter.errorXcpt('%s: write(%s bytes) failed' % (
                    sPath,
                    oFile.cbContent,
                ))
            try:
                oOutFile.close()
            except:
                return reporter.errorXcpt('%s: close() failed' % (sPath, ))

        return True
Example #27
0
    def testImportOvaAsOvf(self, sOva):
        """
        Unpacks the OVA into a subdirectory in the scratch area and imports it as an OVF.
        """
        oVirtualBox = self.oTstDrv.oVBoxMgr.getVirtualBox()

        sTmpDir = os.path.join(self.oTstDrv.sScratchPath,
                               os.path.split(sOva)[1] + '-ovf')
        sOvf = os.path.join(
            sTmpDir,
            os.path.splitext(os.path.split(sOva)[1])[0] + '.ovf')

        #
        # Unpack
        #
        try:
            os.mkdir(sTmpDir, 0o755)
            oTarFile = tarfile.open(sOva, 'r:*')
            oTarFile.extractall(sTmpDir)
            oTarFile.close()
        except:
            return reporter.errorXcpt(
                'Unpacking "%s" to "%s" for OVF style importing failed' % (
                    sOvf,
                    sTmpDir,
                ))

        #
        # Import.
        #
        try:
            oAppliance2 = oVirtualBox.createAppliance()
        except:
            return reporter.errorXcpt(
                'IVirtualBox::createAppliance failed (#2)')

        try:
            oProgress = vboxwrappers.ProgressWrapper(oAppliance2.read(sOvf),
                                                     self.oTstDrv.oVBoxMgr,
                                                     self.oTstDrv,
                                                     'read "%s"' % (sOvf, ))
        except:
            return reporter.errorXcpt('IAppliance::read("%s") failed' %
                                      (sOvf, ))
        oProgress.wait()
        if oProgress.logResult() is False:
            return False

        try:
            oAppliance2.interpret()
        except:
            return reporter.errorXcpt(
                'IAppliance::interpret() failed on "%s"' % (sOvf, ))

        try:
            oProgress = vboxwrappers.ProgressWrapper(
                oAppliance2.importMachines([]), self.oTstDrv.oVBoxMgr,
                self.oTstDrv, 'importMachines "%s"' % (sOvf, ))
        except:
            return reporter.errorXcpt(
                'IAppliance::importMachines failed on "%s"' % (sOvf, ))
        oProgress.wait()
        if oProgress.logResult() is False:
            return False

        return True
    def getReconfiguredVm(self,
                          oTestDrv,
                          cCpus,
                          sVirtMode,
                          sParavirtMode=None):
        #
        # Do the standard reconfig in the base class first, it'll figure out
        # if we can run the VM as requested.
        #
        (fRc,
         oVM) = vboxtestvms.TestVm.getReconfiguredVm(self, oTestDrv, cCpus,
                                                     sVirtMode, sParavirtMode)

        #
        # Make sure there is no HD from the previous run attached nor taking
        # up storage on the host.
        #
        if fRc is True:
            fRc = self.detatchAndDeleteHd(oTestDrv)

        #
        # Check for ubuntu installer vs. AMD host CPU.
        #
        if fRc is True and (self.fInstVmFlags & self.kfUbuntuNewAmdBug):
            if self.isHostCpuAffectedByUbuntuNewAmdBug(oTestDrv):
                return (None, None)
                # (skip)

        #
        # Make adjustments to the default config, and adding a fresh HD.
        #
        if fRc is True:
            oSession = oTestDrv.openSession(oVM)
            if oSession is not None:
                if self.sHddControllerType == self.ksSataController:
                    fRc = fRc and oSession.setStorageControllerType(
                        vboxcon.StorageControllerType_IntelAhci,
                        self.sHddControllerType)
                    fRc = fRc and oSession.setStorageControllerPortCount(
                        self.sHddControllerType, 1)
                elif self.sHddControllerType == self.ksScsiController:
                    fRc = fRc and oSession.setStorageControllerType(
                        vboxcon.StorageControllerType_LsiLogic,
                        self.sHddControllerType)
                try:
                    sHddPath = os.path.join(
                        os.path.dirname(oVM.settingsFilePath),
                        '%s-%s-%s.vdi' % (
                            self.sVmName,
                            sVirtMode,
                            cCpus,
                        ))
                except:
                    reporter.errorXcpt()
                    sHddPath = None
                    fRc = False

                fRc = fRc and oSession.createAndAttachHd(
                    sHddPath,
                    cb=self.cGbHdd * 1024 * 1024 * 1024,
                    sController=self.sHddControllerType,
                    iPort=0,
                    fImmutable=False)

                # Set proper boot order
                fRc = fRc and oSession.setBootOrder(
                    1, vboxcon.DeviceType_HardDisk)
                fRc = fRc and oSession.setBootOrder(2, vboxcon.DeviceType_DVD)

                # Adjust memory if requested.
                if self.iOptRamAdjust != 0:
                    fRc = fRc and oSession.setRamSize(
                        oSession.o.machine.memorySize + self.iOptRamAdjust)

                # Set extra data
                for sExtraData in self.asExtraData:
                    try:
                        sKey, sValue = sExtraData.split(':')
                    except ValueError:
                        raise base.InvalidOption(
                            'Invalid extradata specified: %s' % sExtraData)
                    reporter.log('Set extradata: %s => %s' % (sKey, sValue))
                    fRc = fRc and oSession.setExtraData(sKey, sValue)

                # Other variations?

                # Save the settings.
                fRc = fRc and oSession.saveSettings()
                fRc = oSession.close() and fRc
            else:
                fRc = False
            if fRc is not True:
                oVM = None

        # Done.
        return (fRc, oVM)
Example #29
0
    def testEventQueueInterrupt(self):
        """
        Test interrupting an event queue wait.
        """
        reporter.testStart('interruptWait')

        # interrupt ourselves first and check the return value.
        for i in range(0, 10):
            try:
                rc = self.oTstDrv.oVBoxMgr.interruptWaitEvents()
            except:
                reporter.errorXcpt()
                break
            if rc is not True:
                reporter.error(
                    'interruptWaitEvents returned "%s" expected True' % (rc, ))
                break

        if reporter.testErrorCount() == 0:
            #
            # Interrupt a waitForEvents call.
            #
            # This test ASSUMES that no other events are posted to the thread's
            # event queue once we've drained it.  Also ASSUMES the box is
            # relatively fast and not too busy because we're timing sensitive.
            #
            for i in range(0, 4):
                # Try quiesce the event queue.
                for _ in range(1, 100):
                    self.oTstDrv.oVBoxMgr.waitForEvents(0)

                # Create a thread that will interrupt us in 2 seconds.
                try:
                    oThread = threading.Thread(
                        target=self.interruptWaitEventsThreadProc)
                    oThread.setDaemon(False)
                except:
                    reporter.errorXcpt()
                    break

                cMsTimeout = 20000
                if i == 2:
                    cMsTimeout = -1
                elif i == 3:
                    cMsTimeout = -999999

                # Do the wait.
                oThread.start()
                msNow = base.timestampMilli()
                try:
                    rc = self.oTstDrv.oVBoxMgr.waitForEvents(cMsTimeout)
                except:
                    reporter.errorXcpt()
                else:
                    msElapsed = base.timestampMilli() - msNow

                    # Check the return code and elapsed time.
                    if not isinstance(rc, int):
                        reporter.error(
                            'waitForEvents returns non-integer type after %u ms, expected 1'
                            % (msElapsed, ))
                    elif rc != 1:
                        reporter.error(
                            'waitForEvents returned "%s" after %u ms, expected 1'
                            % (rc, msElapsed))
                    if msElapsed > 15000:
                        reporter.error(
                            'waitForEvents after %u ms, expected just above 2-3 seconds'
                            % (msElapsed, ))
                    elif msElapsed < 100:
                        reporter.error(
                            'waitForEvents after %u ms, expected more than 100 ms.'
                            % (msElapsed, ))

                oThread.join()
                oThread = None
                if reporter.testErrorCount() != 0:
                    break
                reporter.log('Iteration %u was successful...' % (i + 1, ))
        return reporter.testDone()[1] == 0
    def actionExecute(self, oTestDrv, fnCallback): # pylint: disable=R0914
        """
        For base.TestDriver.actionExecute.  Calls the callback function for
        each of the VMs and basic configuration variations (virt-mode and cpu
        count).

        Returns True if all fnCallback calls returned True, otherwise False.

        The callback can return True, False or None. The latter is for when the
        test is skipped.  (True is for success, False is for failure.)
        """

        self._removeUnsupportedVirtModes(oTestDrv);
        cMaxCpus = oTestDrv.getHostCpuCount();

        #
        # The test loop.
        #
        fRc = True;
        for oTestVm in self.aoTestVms:
            if oTestVm.fSkip and self.fIgnoreSkippedVm:
                reporter.log2('Ignoring VM %s (fSkip = True).' % (oTestVm.sVmName,));
                continue;
            reporter.testStart(oTestVm.sVmName);
            if oTestVm.fSkip:
                reporter.testDone(fSkipped = True);
                continue;

            # Intersect the supported modes and the ones being testing.
            asVirtModesSup = [sMode for sMode in oTestVm.asVirtModesSup if sMode in self.asVirtModes];

            # Ditto for CPUs.
            acCpusSup      = [cCpus for cCpus in oTestVm.acCpusSup      if cCpus in self.acCpus];

            # Ditto for paravirtualization modes, except if not specified we got a less obvious default.
            if self.asParavirtModes is not None  and  oTestDrv.fpApiVer >= 5.0:
                asParavirtModes = [sPvMode for sPvMode in oTestVm.asParavirtModesSup if sPvMode in self.asParavirtModes];
                assert None not in asParavirtModes;
            elif oTestDrv.fpApiVer >= 5.0:
                asParavirtModes = (oTestVm.asParavirtModesSup[0],);
                assert asParavirtModes[0] is not None;
            else:
                asParavirtModes = (None,);

            for cCpus in acCpusSup:
                if cCpus == 1:
                    reporter.testStart('1 cpu');
                else:
                    reporter.testStart('%u cpus' % (cCpus));
                    if cCpus > cMaxCpus:
                        reporter.testDone(fSkipped = True);
                        continue;

                cTests = 0;
                for sVirtMode in asVirtModesSup:
                    if sVirtMode == 'raw' and cCpus > 1:
                        continue;
                    reporter.testStart('%s' % ( g_dsVirtModeDescs[sVirtMode], ) );
                    cStartTests = cTests;

                    for sParavirtMode in asParavirtModes:
                        if sParavirtMode is not None:
                            assert oTestDrv.fpApiVer >= 5.0;
                            reporter.testStart('%s' % ( sParavirtMode, ) );

                        # Reconfigure the VM.
                        try:
                            (rc2, oVM) = oTestVm.getReconfiguredVm(oTestDrv, cCpus, sVirtMode, sParavirtMode = sParavirtMode);
                        except KeyboardInterrupt:
                            raise;
                        except:
                            reporter.errorXcpt(cFrames = 9);
                            rc2 = False;
                        if rc2 is True:
                            # Do the testing.
                            try:
                                rc2 = fnCallback(oVM, oTestVm);
                            except KeyboardInterrupt:
                                raise;
                            except:
                                reporter.errorXcpt(cFrames = 9);
                                rc2 = False;
                            if rc2 is False:
                                reporter.maybeErr(reporter.testErrorCount() == 0, 'fnCallback failed');
                        elif rc2 is False:
                            reporter.log('getReconfiguredVm failed');
                        if rc2 is False:
                            fRc = False;

                        cTests = cTests + (rc2 is not None);
                        if sParavirtMode is not None:
                            reporter.testDone(fSkipped = (rc2 is None));

                    reporter.testDone(fSkipped = cTests == cStartTests);

                reporter.testDone(fSkipped = cTests == 0);

            _, cErrors = reporter.testDone();
            if cErrors > 0:
                fRc = False;
        return fRc;
Example #31
0
    def actionExecute(self, oTestDrv, fnCallback): # pylint: disable=R0914
        """
        For base.TestDriver.actionExecute.  Calls the callback function for
        each of the VMs and basic configuration variations (virt-mode and cpu
        count).

        Returns True if all fnCallback calls returned True, otherwise False.

        The callback can return True, False or None. The latter is for when the
        test is skipped.  (True is for success, False is for failure.)
        """

        self._removeUnsupportedVirtModes(oTestDrv);
        cMaxCpus = oTestDrv.getHostCpuCount();

        #
        # The test loop.
        #
        fRc = True;
        for oTestVm in self.aoTestVms:
            if oTestVm.fSkip and self.fIgnoreSkippedVm:
                reporter.log2('Ignoring VM %s (fSkip = True).' % (oTestVm.sVmName,));
                continue;
            reporter.testStart(oTestVm.sVmName);
            if oTestVm.fSkip:
                reporter.testDone(fSkipped = True);
                continue;

            # Intersect the supported modes and the ones being testing.
            asVirtModesSup = [sMode for sMode in oTestVm.asVirtModesSup if sMode in self.asVirtModes];

            # Ditto for CPUs.
            acCpusSup      = [cCpus for cCpus in oTestVm.acCpusSup      if cCpus in self.acCpus];

            # Ditto for paravirtualization modes, except if not specified we got a less obvious default.
            if self.asParavirtModes is not None  and  oTestDrv.fpApiVer >= 5.0:
                asParavirtModes = [sPvMode for sPvMode in oTestVm.asParavirtModesSup if sPvMode in self.asParavirtModes];
                assert None not in asParavirtModes;
            elif oTestDrv.fpApiVer >= 5.0:
                asParavirtModes = (oTestVm.asParavirtModesSup[0],);
                assert asParavirtModes[0] is not None;
            else:
                asParavirtModes = (None,);

            for cCpus in acCpusSup:
                if cCpus == 1:
                    reporter.testStart('1 cpu');
                else:
                    reporter.testStart('%u cpus' % (cCpus));
                    if cCpus > cMaxCpus:
                        reporter.testDone(fSkipped = True);
                        continue;

                cTests = 0;
                for sVirtMode in asVirtModesSup:
                    if sVirtMode == 'raw' and cCpus > 1:
                        continue;
                    reporter.testStart('%s' % ( g_dsVirtModeDescs[sVirtMode], ) );
                    cStartTests = cTests;

                    for sParavirtMode in asParavirtModes:
                        if sParavirtMode is not None:
                            assert oTestDrv.fpApiVer >= 5.0;
                            reporter.testStart('%s' % ( sParavirtMode, ) );

                        # Reconfigure the VM.
                        try:
                            (rc2, oVM) = oTestVm.getReconfiguredVm(oTestDrv, cCpus, sVirtMode, sParavirtMode = sParavirtMode);
                        except KeyboardInterrupt:
                            raise;
                        except:
                            reporter.errorXcpt(cFrames = 9);
                            rc2 = False;
                        if rc2 is True:
                            # Do the testing.
                            try:
                                rc2 = fnCallback(oVM, oTestVm);
                            except KeyboardInterrupt:
                                raise;
                            except:
                                reporter.errorXcpt(cFrames = 9);
                                rc2 = False;
                            if rc2 is False:
                                reporter.maybeErr(reporter.testErrorCount() == 0, 'fnCallback failed');
                        elif rc2 is False:
                            reporter.log('getReconfiguredVm failed');
                        if rc2 is False:
                            fRc = False;

                        cTests = cTests + (rc2 is not None);
                        if sParavirtMode is not None:
                            reporter.testDone(fSkipped = (rc2 is None));

                    reporter.testDone(fSkipped = cTests == cStartTests);

                reporter.testDone(fSkipped = cTests == 0);

            _, cErrors = reporter.testDone();
            if cErrors > 0:
                fRc = False;
        return fRc;
    def getReconfiguredVm(self, oTestDrv, cCpus, sVirtMode, sParavirtMode = None):
        #
        # Do the standard reconfig in the base class first, it'll figure out
        # if we can run the VM as requested.
        #
        (fRc, oVM) = vboxtestvms.TestVm.getReconfiguredVm(self, oTestDrv, cCpus, sVirtMode, sParavirtMode);

        #
        # Make sure there is no HD from the previous run attached nor taking
        # up storage on the host.
        #
        if fRc is True:
            fRc = self.detatchAndDeleteHd(oTestDrv);

        #
        # Check for ubuntu installer vs. AMD host CPU.
        #
        if fRc is True and (self.fInstVmFlags & self.kfUbuntuNewAmdBug):
            if self.isHostCpuAffectedByUbuntuNewAmdBug(oTestDrv):
                return (None, None); # (skip)

        #
        # Make adjustments to the default config, and adding a fresh HD.
        #
        if fRc is True:
            oSession = oTestDrv.openSession(oVM);
            if oSession is not None:
                if self.sHddControllerType == self.ksSataController:
                    fRc = fRc and oSession.setStorageControllerType(vboxcon.StorageControllerType_IntelAhci,
                                                                    self.sHddControllerType);
                    fRc = fRc and oSession.setStorageControllerPortCount(self.sHddControllerType, 1);
                elif self.sHddControllerType == self.ksScsiController:
                    fRc = fRc and oSession.setStorageControllerType(vboxcon.StorageControllerType_LsiLogic,
                                                                    self.sHddControllerType);
                try:
                    sHddPath = os.path.join(os.path.dirname(oVM.settingsFilePath),
                                            '%s-%s-%s.vdi' % (self.sVmName, sVirtMode, cCpus,));
                except:
                    reporter.errorXcpt();
                    sHddPath = None;
                    fRc = False;

                fRc = fRc and oSession.createAndAttachHd(sHddPath,
                                                         cb = self.cGbHdd * 1024*1024*1024,
                                                         sController = self.sHddControllerType,
                                                         iPort = 0,
                                                         fImmutable = False);

                # Set proper boot order
                fRc = fRc and oSession.setBootOrder(1, vboxcon.DeviceType_HardDisk)
                fRc = fRc and oSession.setBootOrder(2, vboxcon.DeviceType_DVD)

                # Adjust memory if requested.
                if self.iOptRamAdjust != 0:
                    fRc = fRc and oSession.setRamSize(oSession.o.machine.memorySize + self.iOptRamAdjust);

                # Set extra data
                for sExtraData in self.asExtraData:
                    try:
                        sKey, sValue = sExtraData.split(':')
                    except ValueError:
                        raise base.InvalidOption('Invalid extradata specified: %s' % sExtraData)
                    reporter.log('Set extradata: %s => %s' % (sKey, sValue))
                    fRc = fRc and oSession.setExtraData(sKey, sValue)

                # Other variations?

                # Save the settings.
                fRc = fRc and oSession.saveSettings()
                fRc = oSession.close() and fRc;
            else:
                fRc = False;
            if fRc is not True:
                oVM = None;

        # Done.
        return (fRc, oVM)
Example #33
0
    def _unattendedConfigure(self, oIUnattended,
                             oTestDrv):  # type: (Any, vbox.TestDriver) -> bool
        """
        Configures the unattended install object.

        The ISO attribute has been set and detectIsoOS has been done, the rest of the
        setup is done here.

        Returns True on success, False w/ errors logged on failure.
        """

        #
        # Make it install the TXS.
        #
        try:
            oIUnattended.installTestExecService = True
        except:
            return reporter.errorXcpt()
        try:
            oIUnattended.validationKitIsoPath = oTestDrv.sVBoxValidationKitIso
        except:
            return reporter.errorXcpt()
        oTestDrv.processPendingEvents()

        #
        # Install GAs?
        #
        if self.fOptInstallAdditions:
            if (self.fInstVmFlags & self.kfNoGAs) == 0:
                try:
                    oIUnattended.installGuestAdditions = True
                except:
                    return reporter.errorXcpt()
                try:
                    oIUnattended.additionsIsoPath = oTestDrv.getGuestAdditionsIso(
                    )
                except:
                    return reporter.errorXcpt()
                oTestDrv.processPendingEvents()
            else:
                reporter.log(
                    "Warning! Ignoring request to install Guest Additions as kfNoGAs is set!"
                )

        #
        # Product key?
        #
        if self.fInstVmFlags & UnattendedVm.kfKeyFile:
            sKeyFile = ''
            sKey = ''
            try:
                sKeyFile = oIUnattended.isoPath + '.key'
                oFile = utils.openNoInherit(sKeyFile)
                for sLine in oFile:
                    sLine = sLine.strip()
                    if sLine and not sLine.startswith(
                            ';') and not sLine.startswith(
                                '#') and not sLine.startswith('//'):
                        sKey = sLine
                        break
                oFile.close()
            except:
                return reporter.errorXcpt('sKeyFile=%s' % (sKeyFile, ))
            if not sKey:
                return reporter.error('No key in keyfile (%s)!' % (sKeyFile, ))
            try:
                oIUnattended.productKey = sKey
            except:
                return reporter.errorXcpt()

        return True
Example #34
0
    def _executeTestCase(self, sName, sFullPath, sTestCaseSubDir, oDevNull):  # pylint: disable=R0914
        """
        Executes a test case.
        """

        fSkipped = False

        #
        # If hardening is enabled, some test cases and their dependencies
        # needs to be copied to and execute from the sVBoxInstallRoot
        # directory in order to work. They also have to be executed as
        # root, i.e. via sudo.
        #
        fHardened = False
        asFilesToRemove = []
        # Stuff to clean up.
        asDirsToRemove = []
        # Ditto.
        if    sName in self.kasHardened \
          and self.sUnitTestsPathBase != self.sVBoxInstallRoot:

            sDstDir = os.path.join(self.sVBoxInstallRoot, sTestCaseSubDir)
            if not os.path.exists(sDstDir):
                self._hardenedMkDir(sDstDir)
                asDirsToRemove.append(sDstDir)

            sDst = os.path.join(sDstDir, os.path.basename(sFullPath))
            self._hardenedCopyFile(sFullPath, sDst, 0755)
            asFilesToRemove.append(sDst)

            # Copy any associated .dll/.so/.dylib.
            for sSuff in ['.dll', '.so', '.dylib']:
                sSrc = os.path.splitext(sFullPath)[0] + sSuff
                if os.path.exists(sSrc):
                    sDst = os.path.join(sDstDir, os.path.basename(sSrc))
                    self._hardenedCopyFile(sSrc, sDst, 0644)
                    asFilesToRemove.append(sDst)

            # Copy any associated .r0, .rc and .gc modules.
            offDriver = sFullPath.rfind('Driver')
            if offDriver > 0:
                for sSuff in ['.r0', 'RC.rc', 'RC.gc']:
                    sSrc = sFullPath[:offDriver] + sSuff
                    if os.path.exists(sSrc):
                        sDst = os.path.join(sDstDir, os.path.basename(sSrc))
                        self._hardenedCopyFile(sSrc, sDst, 0644)
                        asFilesToRemove.append(sDst)

            sFullPath = os.path.join(sDstDir, os.path.basename(sFullPath))
            fHardened = True

        #
        # Set up arguments and environment.
        #
        asArgs = [
            sFullPath,
        ]
        if sName in self.kdArguments:
            asArgs.extend(self.kdArguments[sName])

        os.environ['IPRT_TEST_OMIT_TOP_TEST'] = '1'
        os.environ['IPRT_TEST_FILE'] = sXmlFile = os.path.join(
            self.sScratchPath, 'result.xml')
        if os.path.exists(sXmlFile):
            try:
                os.unlink(sXmlFile)
            except:
                self._hardenedDeleteFile(sXmlFile)

        #
        # Execute the test case.
        #
        # Windows is confusing output.  Trying a few things to get rid of this.
        # First, flush both stderr and stdout before running the child.  Second,
        # assign the child stderr to stdout.  If this doesn't help, we'll have
        # to capture the child output.
        #
        reporter.log('*** Executing %s%s...' %
                     (asArgs, ' [hardened]' if fHardened else ''))
        try:
            sys.stdout.flush()
        except:
            pass
        try:
            sys.stderr.flush()
        except:
            pass
        if not self.fDryRun:
            try:
                if fHardened:
                    oChild = utils.sudoProcessPopen(asArgs,
                                                    stdin=oDevNull,
                                                    stdout=sys.stdout,
                                                    stderr=sys.stdout)
                else:
                    oChild = subprocess.Popen(asArgs,
                                              stdin=oDevNull,
                                              stdout=sys.stdout,
                                              stderr=sys.stdout)
            except:
                if sName in [
                        'tstAsmStructsRC',  # 32-bit, may fail to start on 64-bit linux. Just ignore.
                ]:
                    reporter.logXcpt()
                    fSkipped = True
                else:
                    reporter.errorXcpt()
                iRc = 1023
                oChild = None

            if oChild is not None:
                self.pidFileAdd(oChild.pid, fSudo=fHardened)
                iRc = oChild.wait()
                self.pidFileRemove(oChild.pid)
        else:
            iRc = 0

        #
        # Clean up
        #
        for sPath in asFilesToRemove:
            self._hardenedDeleteFile(sPath)
        for sPath in asDirsToRemove:
            self._hardenedRemoveDir(sPath)

        #
        # Report.
        #
        if os.path.exists(sXmlFile):
            reporter.addSubXmlFile(sXmlFile)
            if fHardened:
                self._hardenedDeleteFile(sXmlFile)
            else:
                os.unlink(sXmlFile)

        if iRc == 0:
            reporter.log('*** %s: exit code %d' % (sFullPath, iRc))
            self.cPassed += 1

        elif iRc == 4:  # RTEXITCODE_SKIPPED
            reporter.log('*** %s: exit code %d (RTEXITCODE_SKIPPED)' %
                         (sFullPath, iRc))
            fSkipped = True
            self.cSkipped += 1

        elif fSkipped:
            reporter.log('*** %s: exit code %d (Skipped)' % (sFullPath, iRc))
            self.cSkipped += 1

        else:
            sName = self.kdExitCodeNames.get(iRc, '')
            if iRc in self.kdExitCodeNamesWin and utils.getHostOs() == 'win':
                sName = self.kdExitCodeNamesWin[iRc]
            if sName != '':
                sName = ' (%s)' % (sName)

            if iRc != 1:
                reporter.testFailure('Exit status: %d%s' % (iRc, sName))
                reporter.log('!*! %s: exit code %d%s' %
                             (sFullPath, iRc, sName))
            else:
                reporter.error('!*! %s: exit code %d%s' %
                               (sFullPath, iRc, sName))
            self.cFailed += 1

        return fSkipped
Example #35
0
    def testRunUnitTestsSet(self, sTestCasePattern, sTestCaseSubDir):
        """
        Run subset of the unit tests set.
        """

        # Open /dev/null for use as stdin further down.
        try:
            oDevNull = open(os.path.devnull, 'w+')
        except:
            oDevNull = None

        # Determin the host OS specific exclusion lists.
        dTestCasesBuggyForHostOs = self.kdTestCasesBuggyPerOs.get(
            utils.getHostOs(), [])

        #
        # Process the file list and run everything looking like a testcase.
        #
        for sFilename in sorted(
                os.listdir(
                    os.path.join(self.sUnitTestsPathBase, sTestCaseSubDir))):
            # Separate base and suffix and morph the base into something we
            # can use for reporting and array lookups.
            sName, sSuffix = os.path.splitext(sFilename)
            if sTestCaseSubDir != '.':
                sName = sTestCaseSubDir + '/' + sName

            # Basic exclusion.
            if   not re.match(sTestCasePattern, sFilename) \
              or sSuffix in self.kasSuffixBlackList:
                reporter.log('"%s" is not a test case.' % (sFilename, ))
                continue

            # Check if the testcase is black listed or buggy before executing it.
            if self._isExcluded(sName, self.kdTestCasesBlackList):
                # (No testStart/Done or accounting here!)
                reporter.log('%s: SKIPPED (blacklisted)' % (sName, ))

            elif self._isExcluded(sName, self.kdTestCasesBuggy):
                reporter.testStart(sName)
                reporter.log('%s: Skipping, buggy in general.' % (sName, ))
                reporter.testDone(fSkipped=True)
                self.cSkipped += 1

            elif self._isExcluded(sName, dTestCasesBuggyForHostOs):
                reporter.testStart(sName)
                reporter.log('%s: Skipping, buggy on %s.' % (
                    sName,
                    utils.getHostOs(),
                ))
                reporter.testDone(fSkipped=True)
                self.cSkipped += 1

            else:
                sFullPath = os.path.normpath(
                    os.path.join(self.sUnitTestsPathBase,
                                 os.path.join(sTestCaseSubDir, sFilename)))
                reporter.testStart(sName)
                try:
                    fSkipped = self._executeTestCase(sName, sFullPath,
                                                     sTestCaseSubDir, oDevNull)
                except:
                    reporter.errorXcpt('!*!')
                    self.cFailed += 1
                    fSkipped = False
                reporter.testDone(fSkipped)
Example #36
0
    def testEventQueueInterrupt(self):
        """
        Test interrupting an event queue wait.
        """
        reporter.testStart('interruptWait');

        # interrupt ourselves first and check the return value.
        for i in range(0, 10):
            try:
                rc = self.oVBoxMgr.interruptWaitEvents();
            except:
                reporter.errorXcpt();
                break;
            if rc is not True:
                reporter.error('interruptWaitEvents returned "%s" expected True' % (rc,));
                break

        if reporter.testErrorCount() == 0:
            #
            # Interrupt a waitForEvents call.
            #
            # This test ASSUMES that no other events are posted to the thread's
            # event queue once we've drained it.  Also ASSUMES the box is
            # relatively fast and not too busy because we're timing sensitive.
            #
            for i in range(0, 4):
                # Try quiesce the event queue.
                for _ in range(1, 100):
                    self.oVBoxMgr.waitForEvents(0);

                # Create a thread that will interrupt us in 2 seconds.
                try:
                    oThread = threading.Thread(target=self.interruptWaitEventsThreadProc);
                    oThread.setDaemon(False);
                except:
                    reporter.errorXcpt();
                    break;

                cMsTimeout = 20000;
                if i == 2:
                    cMsTimeout = -1;
                elif i == 3:
                    cMsTimeout = -999999;

                # Do the wait.
                oThread.start();
                msNow = base.timestampMilli();
                try:
                    rc = self.oVBoxMgr.waitForEvents(cMsTimeout);
                except:
                    reporter.errorXcpt();
                else:
                    msElapsed = base.timestampMilli() - msNow;

                    # Check the return code and elapsed time.
                    if not isinstance(rc, types.IntType):
                        reporter.error('waitForEvents returns non-integer type after %u ms, expected 1' % (msElapsed,));
                    elif rc != 1:
                        reporter.error('waitForEvents returned "%s" after %u ms, expected 1' % (rc, msElapsed));
                    if msElapsed > 15000:
                        reporter.error('waitForEvents after %u ms, expected just above 2-3 seconds' % (msElapsed,));
                    elif msElapsed < 100:
                        reporter.error('waitForEvents after %u ms, expected more than 100 ms.' % (msElapsed,));

                oThread.join();
                oThread = None;
                if reporter.testErrorCount() != 0:
                    break;
                reporter.log('Iteration %u was successful...' % (i + 1,));
        return reporter.testDone()[1] == 0;
Example #37
0
    def testMediumMove(self):
        """
        Test medium moving.
        """
        reporter.testStart('medium moving')

        try:
            oVM = self.createTestVM('test-medium-move', 1, None, 4)
            assert oVM is not None

            # create virtual disk image vdi
            fRc = True
            c = 0
            oSession = self.openSession(oVM)
            for i in self.suffixes:
                sHddPath = os.path.join(self.sScratchPath, 'Test' + str(c) + i)
                oHd = oSession.createBaseHd(sHddPath, cb=1024 * 1024)
                if oHd is None:
                    fRc = False
                    break

                # attach HDD, IDE controller exists by default, but we use SATA just in case
                sController = 'SATA Controller'
                fRc = fRc and oSession.attachHd(sHddPath,
                                                sController,
                                                iPort=c,
                                                iDevice=0,
                                                fImmutable=False,
                                                fForceResource=False)
                fRc = fRc and oSession.saveSettings()

            #create temporary subdirectory in the current working directory
            sOrigLoc = self.sScratchPath
            sNewLoc = os.path.join(sOrigLoc, 'newLocation/')

            os.makedirs(sNewLoc, 0775)

            aListOfAttach = oVM.getMediumAttachmentsOfController(sController)
            #case 1. Only path without file name
            sLocation = sNewLoc
            self.setLocation(sLocation, aListOfAttach)

            #case 2. Only path without file name without '\' or '/' at the end
            sLocation = sOrigLoc
            self.setLocation(sLocation, aListOfAttach)

            #case 3. Path with file name
            sLocation = sNewLoc + 'newName.vdi'
            self.setLocation(sLocation, aListOfAttach)

            #case 4. Only file name
            sLocation = 'onlyMediumName'
            self.setLocation(sLocation, aListOfAttach)

            #case 5. Move snapshot
            fRc = fRc and oSession.takeSnapshot('Snapshot1')
            sLocation = sOrigLoc
            #get fresh attachments after snapshot
            aListOfAttach = oVM.getMediumAttachmentsOfController(sController)
            self.setLocation(sLocation, aListOfAttach)

            fRc = oSession.close() and fRc

            assert fRc is True
        except:
            reporter.errorXcpt()

        return reporter.testDone()[1] == 0
Example #38
0
    def testOneCfg(
            self,
            sVmName,
            eStorageController,
            sHostIoCache,
            sDiskFormat,  # pylint: disable=R0913,R0915
            sDiskVariant,
            sDiskPath,
            cCpus,
            sIoTest,
            sVirtMode,
            sTestSet):
        """
        Runs the specified VM thru test #1.

        Returns a success indicator on the general test execution. This is not
        the actual test result.
        """
        oVM = self.getVmByName(sVmName)

        dTestSet = self.kdTestSets.get(sTestSet)
        cbDisk = dTestSet.get('DiskSizeGb') * 1024 * 1024 * 1024
        fHwVirt = sVirtMode != 'raw'
        fNestedPaging = sVirtMode == 'hwvirt-np'

        fRc = True
        if sDiskFormat == 'iSCSI':
            sDiskPath = self.asIscsiTargets[0]
        elif self.fUseScratch:
            sDiskPath = self.sScratchPath
        else:
            # If requested recreate the storage space to start with a clean config
            # for benchmarks
            if self.fRecreateStorCfg:
                sMountPoint = self.prepareStorage(self.oStorCfg)
                if sMountPoint is not None:
                    # Create a directory where every normal user can write to.
                    self.oStorCfg.mkDirOnVolume(sMountPoint, 'test', 0777)
                    sDiskPath = sMountPoint + '/test'
                else:
                    fRc = False
                    reporter.testFailure('Failed to prepare storage for VM')

        if not fRc:
            return fRc

        lstDisks = []
        # List of disks we have to delete afterwards.

        for iDiffLvl in range(self.cDiffLvls + 1):

            if iDiffLvl == 0:
                reporter.testStart('Base')
            else:
                reporter.testStart('Diff %u' % (iDiffLvl))

            # Reconfigure the VM
            oSession = self.openSession(oVM)
            if oSession is not None:
                # Attach HD
                fRc = oSession.ensureControllerAttached(
                    _ControllerTypeToName(eStorageController))
                fRc = fRc and oSession.setStorageControllerType(
                    eStorageController,
                    _ControllerTypeToName(eStorageController))

                if sHostIoCache == 'hostiocache':
                    fRc = fRc and oSession.setStorageControllerHostIoCache(
                        _ControllerTypeToName(eStorageController), True)
                elif sHostIoCache == 'no-hostiocache':
                    fRc = fRc and oSession.setStorageControllerHostIoCache(
                        _ControllerTypeToName(eStorageController), False)

                iDevice = 0
                if eStorageController == vboxcon.StorageControllerType_PIIX3 or \
                   eStorageController == vboxcon.StorageControllerType_PIIX4:
                    iDevice = 1
                    # Master is for the OS.

                oHdParent = None
                if iDiffLvl > 0:
                    oHdParent = lstDisks[0]
                oHd = self.createHd(oSession, sDiskFormat, sDiskVariant,
                                    iDiffLvl, oHdParent, sDiskPath, cbDisk)
                if oHd is not None:
                    lstDisks.insert(0, oHd)
                    try:
                        if oSession.fpApiVer >= 4.0:
                            oSession.o.machine.attachDevice(_ControllerTypeToName(eStorageController), \
                                                            0, iDevice, vboxcon.DeviceType_HardDisk, oHd)
                        else:
                            oSession.o.machine.attachDevice(_ControllerTypeToName(eStorageController), \
                                                            0, iDevice, vboxcon.DeviceType_HardDisk, oHd.id)
                    except:
                        reporter.errorXcpt('attachDevice("%s",%s,%s,HardDisk,"%s") failed on "%s"' \
                                           % (_ControllerTypeToName(eStorageController), 1, 0, oHd.id, oSession.sName) )
                        fRc = False
                    else:
                        reporter.log('attached "%s" to %s' %
                                     (sDiskPath, oSession.sName))
                else:
                    fRc = False
                fRc = fRc and oSession.enableVirtEx(fHwVirt)
                fRc = fRc and oSession.enableNestedPaging(fNestedPaging)
                fRc = fRc and oSession.setCpuCount(cCpus)
                fRc = fRc and oSession.saveSettings()
                fRc = oSession.close() and fRc and True
                # pychecker hack.
                oSession = None
            else:
                fRc = False

            # Start up.
            if fRc is True:
                self.logVmInfo(oVM)
                oSession, oTxsSession = self.startVmAndConnectToTxsViaTcp(
                    sVmName, fCdWait=False, fNatForwardingForTxs=True)
                if oSession is not None:
                    self.addTask(oSession)

                    # Fudge factor - Allow the guest to finish starting up.
                    self.sleep(5)

                    # Prepare the storage on the guest
                    lstBinaryPaths = [
                        '/bin', '/sbin', '/usr/bin', '/usr/sbin'
                    ]
                    oExecVm = remoteexecutor.RemoteExecutor(
                        oTxsSession, lstBinaryPaths, '${SCRATCH}')
                    oStorCfgVm = storagecfg.StorageCfg(oExecVm, 'linux', self.getGuestDisk(oSession, oTxsSession, \
                                                                                           eStorageController))

                    sMountPoint = self.prepareStorage(oStorCfgVm)
                    if sMountPoint is not None:
                        self.testBenchmark('linux', sIoTest, sMountPoint, oExecVm, dTestSet, \
                                           cMsTimeout = 2 * 3600 * 1000)
                        # 2 hours max (Benchmark and QCOW takes some time)
                        self.cleanupStorage(oStorCfgVm)
                    else:
                        reporter.testFailure(
                            'Failed to prepare storage for the guest benchmark'
                        )

                    # cleanup.
                    self.removeTask(oTxsSession)
                    self.terminateVmBySession(oSession)
                else:
                    fRc = False

                # Remove disk
                oSession = self.openSession(oVM)
                if oSession is not None:
                    try:
                        oSession.o.machine.detachDevice(
                            _ControllerTypeToName(eStorageController), 0,
                            iDevice)

                        # Remove storage controller if it is not an IDE controller.
                        if     eStorageController is not vboxcon.StorageControllerType_PIIX3 \
                           and eStorageController is not vboxcon.StorageControllerType_PIIX4:
                            oSession.o.machine.removeStorageController(
                                _ControllerTypeToName(eStorageController))

                        oSession.saveSettings()
                        oSession.saveSettings()
                        oSession.close()
                        oSession = None
                    except:
                        reporter.errorXcpt(
                            'failed to detach/delete disk %s from storage controller'
                            % (sDiskPath))
                else:
                    fRc = False

            reporter.testDone()

        # Delete all disks
        for oHd in lstDisks:
            self.oVBox.deleteHdByMedium(oHd)

        # Cleanup storage area
        if sDiskFormat != 'iSCSI' and not self.fUseScratch and self.fRecreateStorCfg:
            self.cleanupStorage(self.oStorCfg)

        return fRc
    def testOneCfg(self, sVmName, eStorageController, sDiskFormat, sDiskVariant, # pylint: disable=R0913
                   sDiskPath, cCpus, sIoTest, sVirtMode, sTestSet):
        """
        Runs the specified VM thru test #1.

        Returns a success indicator on the general test execution. This is not
        the actual test result.
        """
        oVM = self.getVmByName(sVmName);

        dTestSet      = self.kdTestSets.get(sTestSet);
        cbDisk        = dTestSet.get('DiskSizeGb') * 1024*1024*1024;
        fHwVirt       = sVirtMode != 'raw';
        fNestedPaging = sVirtMode == 'hwvirt-np';

        fRc = True;
        if sDiskFormat == 'iSCSI':
            sDiskPath = self.asIscsiTargets[0];
        elif self.fUseScratch:
            sDiskPath = self.sScratchPath;
        else:
            # If requested recreate the storage space to start with a clean config
            # for benchmarks
            if self.fRecreateStorCfg:
                sMountPoint = self.prepareStorage(self.oStorCfg);
                if sMountPoint is not None:
                    # Create a directory where every normal user can write to.
                    self.oStorCfg.mkDirOnVolume(sMountPoint, 'test', 0777);
                    sDiskPath = sMountPoint + '/test';
                else:
                    fRc = False;
                    reporter.testFailure('Failed to prepare storage for VM');

        if not fRc:
            return fRc;

        # Reconfigure the VM
        oSession = self.openSession(oVM);
        if oSession is not None:
            # Attach HD
            fRc = oSession.ensureControllerAttached(_ControllerTypeToName(eStorageController));
            fRc = fRc and oSession.setStorageControllerType(eStorageController, _ControllerTypeToName(eStorageController));

            iDevice = 0;
            if eStorageController == vboxcon.StorageControllerType_PIIX3 or \
               eStorageController == vboxcon.StorageControllerType_PIIX4:
                iDevice = 1; # Master is for the OS.

            if sDiskFormat == "iSCSI":
                listNames = [];
                listValues = [];
                listValues = self.asIscsiTargets[0].split('|');
                listNames.append('TargetAddress');
                listNames.append('TargetName');
                listNames.append('LUN');

                if self.fpApiVer >= 5.0:
                    oHd = oSession.oVBox.createMedium(sDiskFormat, sDiskPath, vboxcon.AccessMode_ReadWrite, \
                                                      vboxcon.DeviceType_HardDisk);
                else:
                    oHd = oSession.oVBox.createHardDisk(sDiskFormat, sDiskPath);
                oHd.type = vboxcon.MediumType_Normal;
                oHd.setProperties(listNames, listValues);

                # Attach it.
                if fRc is True:
                    try:
                        if oSession.fpApiVer >= 4.0:
                            oSession.o.machine.attachDevice(_ControllerTypeToName(eStorageController), \
                                                            0, iDevice, vboxcon.DeviceType_HardDisk, oHd);
                        else:
                            oSession.o.machine.attachDevice(_ControllerTypeToName(eStorageController), \
                                                            0, iDevice, vboxcon.DeviceType_HardDisk, oHd.id);
                    except:
                        reporter.errorXcpt('attachDevice("%s",%s,%s,HardDisk,"%s") failed on "%s"' \
                                           % (_ControllerTypeToName(eStorageController), 1, 0, oHd.id, oSession.sName) );
                        fRc = False;
                    else:
                        reporter.log('attached "%s" to %s' % (sDiskPath, oSession.sName));
            else:
                tMediumVariant = self.convDiskToMediumVariant(sDiskVariant);
                fRc = fRc and oSession.createAndAttachHd(sDiskPath + '/test.disk', sDiskFormat, \
                                                         _ControllerTypeToName(eStorageController), \
                                                         cb = cbDisk, iPort = 0, iDevice = iDevice, \
                                                         fImmutable = False, cMsTimeout = 3600 * 1000, \
                                                         tMediumVariant = tMediumVariant);
            fRc = fRc and oSession.enableVirtEx(fHwVirt);
            fRc = fRc and oSession.enableNestedPaging(fNestedPaging);
            fRc = fRc and oSession.setCpuCount(cCpus);
            fRc = fRc and oSession.saveSettings();
            fRc = oSession.close() and fRc and True; # pychecker hack.
            oSession = None;
        else:
            fRc = False;

        # Start up.
        if fRc is True:
            self.logVmInfo(oVM);
            oSession, oTxsSession = self.startVmAndConnectToTxsViaTcp(sVmName, fCdWait = False, fNatForwardingForTxs = True);
            if oSession is not None:
                self.addTask(oSession);

                # Fudge factor - Allow the guest to finish starting up.
                self.sleep(5);

                # Prepare the storage on the guest
                lstBinaryPaths = ['/bin', '/sbin', '/usr/bin', '/usr/sbin' ];
                oExecVm = remoteexecutor.RemoteExecutor(oTxsSession, lstBinaryPaths, '${SCRATCH}');
                oStorCfgVm = storagecfg.StorageCfg(oExecVm, 'linux', self.getGuestDisk(oSession, oTxsSession, \
                                                                                       eStorageController));

                sMountPoint = self.prepareStorage(oStorCfgVm);
                if sMountPoint is not None:
                    self.testBenchmark('linux', sIoTest, sMountPoint, oExecVm, dTestSet);
                    self.cleanupStorage(oStorCfgVm);
                else:
                    reporter.testFailure('Failed to prepare storage for the guest benchmark');

                # cleanup.
                self.removeTask(oTxsSession);
                self.terminateVmBySession(oSession)
            else:
                fRc = False;

            # Remove disk
            oSession = self.openSession(oVM);
            if oSession is not None:
                try:
                    oSession.o.machine.detachDevice(_ControllerTypeToName(eStorageController), 0, iDevice);

                    # Remove storage controller if it is not an IDE controller.
                    if     eStorageController is not vboxcon.StorageControllerType_PIIX3 \
                       and eStorageController is not vboxcon.StorageControllerType_PIIX4:
                        oSession.o.machine.removeStorageController(_ControllerTypeToName(eStorageController));

                    oSession.saveSettings();
                    self.oVBox.deleteHdByLocation(sDiskPath + '/test.disk');
                    oSession.saveSettings();
                    oSession.close();
                    oSession = None;
                except:
                    reporter.errorXcpt('failed to detach/delete disk %s from storage controller' % (sDiskPath));
            else:
                fRc = False;

            # Cleanup storage area
            if sDiskFormat != 'iSCSI' and not self.fUseScratch and self.fRecreateStorCfg:
                self.cleanupStorage(self.oStorCfg);

        return fRc;
Example #40
0
    def test1OneCfg(self, sVmName, eStorageController, sDiskFormat, sDiskPath1, sDiskPath2, \
                    sGuestFs, cCpus, fHwVirt, fNestedPaging):
        """
        Runs the specified VM thru test #1.

        Returns a success indicator on the general test execution. This is not
        the actual test result.
        """
        oVM = self.getVmByName(sVmName);

        # Reconfigure the VM
        fRc = True;
        oSession = self.openSession(oVM);
        if oSession is not None:
            # Attach HD
            fRc = oSession.ensureControllerAttached(_ControllerTypeToName(eStorageController));
            fRc = fRc and oSession.setStorageControllerType(eStorageController, _ControllerTypeToName(eStorageController));

            if sDiskFormat == "iSCSI":
                listNames = [];
                listValues = [];
                listValues = sDiskPath1.split('|');
                listNames.append('TargetAddress');
                listNames.append('TargetName');
                listNames.append('LUN');

                if self.fpApiVer >= 5.0:
                    oHd = oSession.oVBox.createMedium(sDiskFormat, sDiskPath1, vboxcon.AccessMode_ReadWrite, \
                                                      vboxcon.DeviceType_HardDisk);
                else:
                    oHd = oSession.oVBox.createHardDisk(sDiskFormat, sDiskPath1);
                oHd.type = vboxcon.MediumType_Normal;
                oHd.setProperties(listNames, listValues);

                # Attach it.
                if fRc is True:
                    try:
                        if oSession.fpApiVer >= 4.0:
                            oSession.o.machine.attachDevice(_ControllerTypeToName(eStorageController), \
                                                            1, 0, vboxcon.DeviceType_HardDisk, oHd);
                        else:
                            oSession.o.machine.attachDevice(_ControllerTypeToName(eStorageController), \
                                                            1, 0, vboxcon.DeviceType_HardDisk, oHd.id);
                    except:
                        reporter.errorXcpt('attachDevice("%s",%s,%s,HardDisk,"%s") failed on "%s"' \
                                           % (_ControllerTypeToName(eStorageController), 1, 0, oHd.id, oSession.sName) );
                        fRc = False;
                    else:
                        reporter.log('attached "%s" to %s' % (sDiskPath1, oSession.sName));
            else:
                fRc = fRc and oSession.createAndAttachHd(sDiskPath1, sDiskFormat, _ControllerTypeToName(eStorageController), \
                                                         cb = 10*1024*1024*1024, iPort = 1, fImmutable = False);
                fRc = fRc and oSession.createAndAttachHd(sDiskPath2, sDiskFormat, _ControllerTypeToName(eStorageController), \
                                                         cb = 10*1024*1024*1024, iPort = 2, fImmutable = False);
            fRc = fRc and oSession.enableVirtEx(fHwVirt);
            fRc = fRc and oSession.enableNestedPaging(fNestedPaging);
            fRc = fRc and oSession.setCpuCount(cCpus);
            fRc = fRc and oSession.saveSettings();
            fRc = oSession.close() and fRc and True; # pychecker hack.
            oSession = None;
        else:
            fRc = False;

        # Start up.
        if fRc is True:
            self.logVmInfo(oVM);
            oSession, oTxsSession = self.startVmAndConnectToTxsViaTcp(sVmName, fCdWait = False, fNatForwardingForTxs = True);
            if oSession is not None:
                self.addTask(oTxsSession);

                # Fudge factor - Allow the guest to finish starting up.
                self.sleep(5);

                fRc = self.test1RunTestProgs(oSession, oTxsSession, fRc, 'stress testing', sGuestFs);

                # cleanup.
                self.removeTask(oTxsSession);
                self.terminateVmBySession(oSession)

                # Remove disk
                oSession = self.openSession(oVM);
                if oSession is not None:
                    try:
                        oSession.o.machine.detachDevice(_ControllerTypeToName(eStorageController), 1, 0);
                        oSession.o.machine.detachDevice(_ControllerTypeToName(eStorageController), 2, 0);

                        # Remove storage controller if it is not an IDE controller.
                        if     eStorageController is not vboxcon.StorageControllerType_PIIX3 \
                           and eStorageController is not vboxcon.StorageControllerType_PIIX4:
                            oSession.o.machine.removeStorageController(_ControllerTypeToName(eStorageController));

                        oSession.saveSettings();
                        oSession.oVBox.deleteHdByLocation(sDiskPath1);
                        oSession.oVBox.deleteHdByLocation(sDiskPath2);
                        oSession.saveSettings();
                        oSession.close();
                        oSession = None;
                    except:
                        reporter.errorXcpt('failed to detach/delete disks %s and %s from storage controller' % \
                                           (sDiskPath1, sDiskPath2));
                else:
                    fRc = False;
            else:
                fRc = False;
        return fRc;
    def _executeTestCase(self, sName, sFullPath, sTestCaseSubDir, oDevNull): # pylint: disable=R0914
        """
        Executes a test case.
        """

        fSkipped = False;

        #
        # If hardening is enabled, some test cases and their dependencies
        # needs to be copied to and execute from the sVBoxInstallRoot
        # directory in order to work. They also have to be executed as
        # root, i.e. via sudo.
        #
        fHardened       = False;
        asFilesToRemove = []; # Stuff to clean up.
        asDirsToRemove  = []; # Ditto.
        if    sName in self.kasHardened \
          and self.sUnitTestsPathBase != self.sVBoxInstallRoot:

            sDstDir = os.path.join(self.sVBoxInstallRoot, sTestCaseSubDir);
            if not os.path.exists(sDstDir):
                self._hardenedMkDir(sDstDir);
                asDirsToRemove.append(sDstDir);

            sDst = os.path.join(sDstDir, os.path.basename(sFullPath));
            self._hardenedCopyFile(sFullPath, sDst, 0755);
            asFilesToRemove.append(sDst);

            # Copy any associated .dll/.so/.dylib.
            for sSuff in [ '.dll', '.so', '.dylib' ]:
                sSrc = os.path.splitext(sFullPath)[0] + sSuff;
                if os.path.exists(sSrc):
                    sDst = os.path.join(sDstDir, os.path.basename(sSrc));
                    self._hardenedCopyFile(sSrc, sDst, 0644);
                    asFilesToRemove.append(sDst);

            # Copy any associated .r0, .rc and .gc modules.
            offDriver = sFullPath.rfind('Driver')
            if offDriver > 0:
                for sSuff in [ '.r0', 'RC.rc', 'RC.gc' ]:
                    sSrc = sFullPath[:offDriver] + sSuff;
                    if os.path.exists(sSrc):
                        sDst = os.path.join(sDstDir, os.path.basename(sSrc));
                        self._hardenedCopyFile(sSrc, sDst, 0644);
                        asFilesToRemove.append(sDst);

            sFullPath = os.path.join(sDstDir, os.path.basename(sFullPath));
            fHardened = True;

        #
        # Set up arguments and environment.
        #
        asArgs = [sFullPath,]
        if sName in self.kdArguments:
            asArgs.extend(self.kdArguments[sName]);

        os.environ['IPRT_TEST_OMIT_TOP_TEST'] = '1';
        os.environ['IPRT_TEST_FILE'] = sXmlFile = os.path.join(self.sScratchPath, 'result.xml');
        if os.path.exists(sXmlFile):
            try:    os.unlink(sXmlFile);
            except: self._hardenedDeleteFile(sXmlFile);

        #
        # Execute the test case.
        #
        # Windows is confusing output.  Trying a few things to get rid of this.
        # First, flush both stderr and stdout before running the child.  Second,
        # assign the child stderr to stdout.  If this doesn't help, we'll have
        # to capture the child output.
        #
        reporter.log('*** Executing %s%s...' % (asArgs, ' [hardened]' if fHardened else ''));
        try:    sys.stdout.flush();
        except: pass;
        try:    sys.stderr.flush();
        except: pass;
        if not self.fDryRun:
            try:
                if fHardened:
                    oChild = utils.sudoProcessPopen(asArgs, stdin = oDevNull, stdout = sys.stdout, stderr = sys.stdout);
                else:
                    oChild = subprocess.Popen(      asArgs, stdin = oDevNull, stdout = sys.stdout, stderr = sys.stdout);
            except:
                if sName in [ 'tstAsmStructsRC',    # 32-bit, may fail to start on 64-bit linux. Just ignore.
                              ]:
                    reporter.logXcpt();
                    fSkipped = True;
                else:
                    reporter.errorXcpt();
                iRc    = 1023;
                oChild = None;

            if oChild is not None:
                self.pidFileAdd(oChild.pid, fSudo = fHardened);
                iRc = oChild.wait();
                self.pidFileRemove(oChild.pid);
        else:
            iRc = 0;

        #
        # Clean up
        #
        for sPath in asFilesToRemove:
            self._hardenedDeleteFile(sPath);
        for sPath in asDirsToRemove:
            self._hardenedRemoveDir(sPath);

        #
        # Report.
        #
        if os.path.exists(sXmlFile):
            reporter.addSubXmlFile(sXmlFile);
            if fHardened:
                self._hardenedDeleteFile(sXmlFile);
            else:
                os.unlink(sXmlFile);

        if iRc == 0:
            reporter.log('*** %s: exit code %d' % (sFullPath, iRc));
            self.cPassed += 1

        elif iRc == 4: # RTEXITCODE_SKIPPED
            reporter.log('*** %s: exit code %d (RTEXITCODE_SKIPPED)' % (sFullPath, iRc));
            fSkipped = True;
            self.cSkipped += 1;

        elif fSkipped:
            reporter.log('*** %s: exit code %d (Skipped)' % (sFullPath, iRc));
            self.cSkipped += 1;

        else:
            sName = self.kdExitCodeNames.get(iRc, '');
            if iRc in self.kdExitCodeNamesWin and utils.getHostOs() == 'win':
                sName = self.kdExitCodeNamesWin[iRc];
            if sName != '':
                sName = ' (%s)' % (sName);

            if iRc != 1:
                reporter.testFailure('Exit status: %d%s' % (iRc, sName));
                reporter.log(  '!*! %s: exit code %d%s' % (sFullPath, iRc, sName));
            else:
                reporter.error('!*! %s: exit code %d%s' % (sFullPath, iRc, sName));
            self.cFailed += 1

        return fSkipped;
Example #42
0
    def createVMWithDefaults(self, sGuestType):
        sName = 'testvm_%s' % (sGuestType)
        # create VM manually, because the self.createTestVM() makes registration inside
        # the method, but IMachine::applyDefault() must be called before machine is registered
        try:
            if self.oTstDrv.fpApiVer >= 4.2:  # Introduces grouping (third parameter, empty for now).
                oVM = self.oTstDrv.oVBox.createMachine(
                    "", sName, [], self.oTstDrv.tryFindGuestOsId(sGuestType),
                    "")
            elif self.oTstDrv.fpApiVer >= 4.0:
                oVM = self.oTstDrv.oVBox.createMachine(
                    "", sName, self.oTstDrv.tryFindGuestOsId(sGuestType), "",
                    False)
            elif self.oTstDrv.fpApiVer >= 3.2:
                oVM = self.oTstDrv.oVBox.createMachine(
                    sName, self.oTstDrv.tryFindGuestOsId(sGuestType), "", "",
                    False)
            else:
                oVM = self.oTstDrv.oVBox.createMachine(
                    sName, self.oTstDrv.tryFindGuestOsId(sGuestType), "", "")
            try:
                oVM.saveSettings()
            except:
                reporter.logXcpt()
                if self.oTstDrv.fpApiVer >= 4.0:
                    try:
                        if self.oTstDrv.fpApiVer >= 4.3:
                            oProgress = oVM.deleteConfig([])
                        else:
                            oProgress = oVM.delete(None)
                        self.oTstDrv.waitOnProgress(oProgress)
                    except:
                        reporter.logXcpt()
                else:
                    try:
                        oVM.deleteSettings()
                    except:
                        reporter.logXcpt()
                raise
        except:
            reporter.errorXcpt('failed to create vm "%s"' % (sName))
            return None

        if oVM is None:
            return False

        # apply settings
        fRc = True
        try:
            if self.oTstDrv.fpApiVer >= 6.1:
                oVM.applyDefaults('')
                oVM.saveSettings()
            self.oTstDrv.oVBox.registerMachine(oVM)
        except:
            reporter.logXcpt()
            fRc = False

        # Some errors from applyDefaults can be observed only after further settings saving.
        # Change and save the size of the VM RAM as simple setting change.
        oSession = self.oTstDrv.openSession(oVM)
        if oSession is None:
            fRc = False

        if fRc:
            try:
                oSession.memorySize = 4096
                oSession.saveSettings(True)
            except:
                reporter.logXcpt()
                fRc = False

        # delete VM
        try:
            oVM.unregister(vboxcon.CleanupMode_DetachAllReturnNone)
        except:
            reporter.logXcpt()

        if self.oTstDrv.fpApiVer >= 4.0:
            try:
                if self.oTstDrv.fpApiVer >= 4.3:
                    oProgress = oVM.deleteConfig([])
                else:
                    oProgress = oVM.delete(None)
                self.oTstDrv.waitOnProgress(oProgress)

            except:
                reporter.logXcpt()

        else:
            try:
                oVM.deleteSettings()
            except:
                reporter.logXcpt()

        return fRc
    def testMediumMove(self):
        """
        Test medium moving.
        """
        reporter.testStart('medium moving')

        try: ## @todo r=bird: Bad 'ing style.
            oVM = self.oTstDrv.createTestVM('test-medium-move', 1, None, 4)
            assert oVM is not None

            # create hard disk images, one for each file-based backend, using the first applicable extension
            fRc = True
            oSession = self.oTstDrv.openSession(oVM)
            aoDskFmts = self.oTstDrv.oVBoxMgr.getArray(self.oTstDrv.oVBox.systemProperties, 'mediumFormats')
            asFiles = []
            for oDskFmt in aoDskFmts:
                aoDskFmtCaps = self.oTstDrv.oVBoxMgr.getArray(oDskFmt, 'capabilities')
                if vboxcon.MediumFormatCapabilities_File not in aoDskFmtCaps \
                    or vboxcon.MediumFormatCapabilities_CreateDynamic not in aoDskFmtCaps:
                    continue
                (asExts, aTypes) = oDskFmt.describeFileExtensions()
                for i in range(0, len(asExts)): #pylint: disable=consider-using-enumerate
                    if aTypes[i] is vboxcon.DeviceType_HardDisk:
                        sExt = '.' + asExts[i]
                        break
                if sExt is None:
                    fRc = False
                    break
                sFile = 'Test' + str(len(asFiles)) + sExt
                sHddPath = os.path.join(self.oTstDrv.sScratchPath, sFile)
                oHd = oSession.createBaseHd(sHddPath, sFmt=oDskFmt.id, cb=1024*1024)
                if oHd is None:
                    fRc = False
                    break

                # attach HDD, IDE controller exists by default, but we use SATA just in case
                sController='SATA Controller'
                fRc = fRc and oSession.attachHd(sHddPath, sController, iPort = len(asFiles),
                                                fImmutable=False, fForceResource=False)
                if fRc:
                    asFiles.append(sFile)

            fRc = fRc and oSession.saveSettings()

            #create temporary subdirectory in the current working directory
            sOrigLoc = self.oTstDrv.sScratchPath
            sNewLoc = os.path.join(sOrigLoc, 'newLocation')
            os.mkdir(sNewLoc, 0o775)

            aoMediumAttachments = oVM.getMediumAttachmentsOfController(sController)
            #case 1. Only path without file name, with trailing separator
            fRc = self.moveTo(sNewLoc + os.sep, aoMediumAttachments) and fRc
            fRc = self.checkLocation(sNewLoc, aoMediumAttachments, asFiles) and fRc

            #case 2. Only path without file name, without trailing separator
            fRc = self.moveTo(sOrigLoc, aoMediumAttachments) and fRc
            fRc = self.checkLocation(sOrigLoc, aoMediumAttachments, asFiles) and fRc

            #case 3. Path with file name
            #The case supposes that user has passed a destination path with a file name but hasn't added an extension/suffix
            #to this destination file. User supposes that the extension would be added automatically and to be the same as
            #for the original file. Difficult case, apparently this case should follow mv(1) logic
            #and the file name is processed as folder name (aka mv(1) logic).
            #Be discussed.
            fRc = self.moveTo(os.path.join(sNewLoc, 'newName'), aoMediumAttachments) and fRc
            asNewFiles = ['newName' + os.path.splitext(s)[1] for s in asFiles]
            fRc = self.checkLocation(os.path.join(sNewLoc, 'newName'), aoMediumAttachments, asFiles) and fRc

            #after the case the destination path must be corrected
            sNewLoc = os.path.join(sNewLoc, 'newName')

            #case 4. Only file name
            fRc = self.moveTo('onlyMediumName', aoMediumAttachments) and fRc
            asNewFiles = ['onlyMediumName' + os.path.splitext(s)[1] for s in asFiles]
            if self.oTstDrv.fpApiVer >= 5.3:
                fRc = self.checkLocation(sNewLoc, aoMediumAttachments, asNewFiles) and fRc
            else:
                fRc = self.checkLocation(sNewLoc, aoMediumAttachments,
                                         [s.replace('.hdd', '.parallels') for s in asNewFiles]) and fRc

            #case 5. Move all files from a snapshot
            fRc = fRc and oSession.takeSnapshot('Snapshot1')
            if fRc:
                aoMediumAttachments = oVM.getMediumAttachmentsOfController(sController)
                asSnapFiles = [os.path.basename(o.medium.name) for o in aoMediumAttachments]
                fRc = self.moveTo(sOrigLoc, aoMediumAttachments) and fRc
                fRc = self.checkLocation(sOrigLoc, aoMediumAttachments, asSnapFiles) and fRc

            fRc = oSession.close() and fRc
        except:
            reporter.errorXcpt()

        return reporter.testDone()[1] == 0