# -*- coding: utf-8 -*-
# $Id$
"""
Test Manager - TestSet.
"""
__copyright__ = \
"""
Copyright (C) 2012-2014 Oracle Corporation
This file is part of VirtualBox Open Source Edition (OSE), as
available from http://www.virtualbox.org. This file is free software;
General Public License (GPL) as published by the Free Software
Foundation, in version 2 as it comes in the "COPYING" file of the
VirtualBox OSE distribution. VirtualBox OSE is distributed in the
hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
The contents of this file may alternatively be used under the terms
of the Common Development and Distribution License Version 1.0
(CDDL) only, as it comes in the "COPYING.CDDL" file of the
VirtualBox OSE distribution, in which case the provisions of the
CDDL are applicable instead of those of the GPL.
You may elect to license modified versions of this file under the
terms and conditions of either the GPL or the CDDL or both.
"""
# Standard python imports.
import os;
import zipfile;
import unittest;
# Validation Kit imports.
from testmanager.core.base import ModelDataBase, ModelDataBaseTestCase, ModelLogicBase, TMExceptionBase;
"""
TestSet Data.
"""
## @name TestStatus_T
# @{
## @}
## List of bad statuses.
];
#
# Initialize with defaults.
# See the database for explanations of each of these fields.
#
self.idBuildCategory = None;
self.idBuildTestSuite = None;
self.idGenTestBox = None;
self.idTestGroup = None;
self.idGenTestCase = None;
self.idTestCase = None;
self.idGenTestCaseArgs = None;
self.idTestCaseArgs = None;
self.idTestResult = None;
self.sBaseFilename = None;
self.idTestSetGangLeader = None;
"""
Internal worker for initFromDbWithId and initFromDbWithGenId as well as
TestBoxSetLogic.
"""
if aoRow is None:
raise TMExceptionBase('TestSet not found.');
return self;
"""
Initialize the object from the database.
"""
'FROM TestSets\n'
'WHERE idTestSet = %s\n'
, (idTestSet, ) );
if aoRow is None:
"""
Opens a file.
Returns (oFile, cbFile, fIsStream) on success.
Returns (None, sErrorMsg, None) on failure.
Will not raise exceptions, unless the class instance is invalid.
"""
# Try raw file first.
try:
# Try the zip archive next.
try:
# Construct a meaningful error message.
try:
return (None, 'Code not reachable!', None);
"""
Creates a new file.
Returns oFile on success.
Returns sErrorMsg on failure.
"""
# Try raw file first.
try:
return oFile;
"""
TestSet logic.
"""
"""
Attempts to fetch a test set.
Returns a TestSetData object on success.
Returns None if no status was found.
Raises exception on other errors.
"""
'FROM TestSets\n'
'WHERE idTestSet = %s\n',
(idTestSet,));
return None;
oData = TestSetData();
"""
Gets the string table id for the given string, adding it if new.
"""
## @todo move this and make a stored procedure for it.
'FROM TestResultStrTab\n'
'WHERE sValue = %s'
, (sString,));
'VALUES (%s)\n'
'RETURNING idStr\n'
, (sString,));
if fCommit:
"""
Completes the testset.
Returns the test set ID of the gang leader, None if no gang involvement.
Raises exceptions on database errors and invalid input.
"""
#
# Get the basic test set data and check if there is anything to do here.
#
if oData.idTestResult is None:
#
# Close open sub test results, count these as errors.
# Note! No need to propagate error counts here. Only one tree line will
# have open sets, and it will go all the way to the root.
#
'FROM TestResults\n'
'WHERE idTestSet = %s\n'
' AND enmStatus = %s\n'
' AND idTestResult <> %s\n'
'ORDER BY idTestResult DESC\n'
'SET enmStatus = \'failure\',\n'
' tsElapsed = CURRENT_TIMESTAMP - tsCreated,\n'
' cErrors = cErrors + 1\n'
'WHERE idTestResult = %s\n'
, (aoRow[0],));
'VALUES ( %s, %s, \'failure\'::TestResultMsgLevel_T)\n'
#
# If it's a success result, check it against error counters.
#
'FROM TestResults\n'
'WHERE idTestSet = %s\n'
' AND cErrors > 0\n'
, (idTestSet,));
if cErrors > 0:
#
# If it's an pure 'failure', check for timeouts and propagate it.
#
'FROM TestResults\n'
'WHERE idTestSet = %s\n'
' AND enmStatus = %s\n'
#
# Complete the top level test result and then the test set.
#
'SET cErrors = (SELECT COALESCE(SUM(cErrors), 0)\n'
' FROM TestResults\n'
' WHERE idTestResultParent = %s)\n'
'WHERE idTestResult = %s\n'
'RETURNING cErrors\n'
'SET cErrors = 1\n'
'WHERE idTestResult = %s\n'
, (oData.idTestResult,));
'SET enmStatus = %s,\n'
' tsElapsed = CURRENT_TIMESTAMP - tsCreated\n'
'WHERE idTestResult = %s\n'
'SET enmStatus = %s,\n'
' tsDone = CURRENT_TIMESTAMP\n'
'WHERE idTestSet = %s\n'
return oData.idTestSetGangLeader;
"""
Completes the testset as abandoned if necessary.
See scenario #9:
file://../../docs/AutomaticTestingRevamp.html#cleaning-up-abandond-testcase
Returns True if successfully completed as abandond, False if it's already
completed, and raises exceptions under exceptional circumstances.
"""
#
# Get the basic test set data and check if there is anything to do here.
#
if oData is None:
return False;
return False;
if oData.idTestResult is not None:
#
# Clean up test results, adding a message why they failed.
#
'SET enmStatus = \'failure\',\n'
' tsElapsed = CURRENT_TIMESTAMP - tsCreated,\n'
' cErrors = cErrors + 1\n'
'WHERE idTestSet = %s\n'
' AND enmStatus = \'running\'::TestStatus_T\n'
, (idTestSet,));
'VALUES ( %s, %s, \'failure\'::TestResultMsgLevel_T)\n'
#
# Complete the testset.
#
'SET enmStatus = \'failure\',\n'
' tsDone = CURRENT_TIMESTAMP\n'
'WHERE idTestSet = %s\n'
' AND enmStatus = \'running\'::TestStatus_T\n'
, (idTestSet,));
return True;
"""
Completes the testset with a gang-gathering timeout.
Raises exceptions on database errors and invalid input.
"""
#
# Get the basic test set data and check if there is anything to do here.
#
if oData.idTestResult is None:
#
# Complete the top level test result and then the test set.
#
'SET enmStatus = \'failure\',\n'
' tsElapsed = CURRENT_TIMESTAMP - tsCreated,\n'
' cErrors = cErrors + 1\n'
'WHERE idTestSet = %s\n'
' AND enmStatus = \'running\'::TestStatus_T\n'
, (idTestSet,));
'VALUES ( %s, %s, \'failure\'::TestResultMsgLevel_T)\n'
'SET enmStatus = \'failure\',\n'
' tsDone = CURRENT_TIMESTAMP\n'
'WHERE idTestSet = %s\n'
, (idTestSet,));
return True;
def createFile(self, oTestSet, sName, sMime, sKind, sDesc, cbFile, fCommit = False): # pylint: disable=R0914
"""
Creates a file and associating with the current test result record in
the test set.
Returns file object that the file content can be written to.
Raises exception on database error, I/O errors, if there are too many
files in the test set or if they take up too much disk space.
The caller (testboxdisp.py) is expected to do basic input validation,
so we skip that and get on with the bits only we can do.
"""
#
# Furhter input and limit checks.
#
raise TMExceptionBase('Cannot create files on a test set with status "%s".' % (oTestSet.enmStatus,));
'FROM TestResultFiles,\n'
' TestResults,\n'
' TestResultStrTab\n'
'WHERE TestResults.idTestSet = %s\n'
' AND TestResultFiles.idTestResult = TestResults.idTestResult\n'
' AND TestResultStrTab.idStr = TestResultFiles.idStrFile\n'
dFiles = {}
cbTotalFiles = 0;
try:
except:
raise TMExceptionBase('Will exceed total upload limit: %u bytes + %u bytes > %s MiB.' \
#
# Create a new file.
#
'FROM TestResults\n'
'WHERE idTestSet = %s\n'
' AND enmStatus = \'running\'::TestStatus_T\n'
'ORDER BY idTestResult\n'
'LIMIT 1\n'
raise TMExceptionBase('No open test results - someone committed a capital offence or we ran into a race.');
# Note! There is in theory a race here, but that's something the
# test driver doing parallel upload with non-unique names
# should worry about. The TD should always avoid this path.
break;
sName = None;
if sName is None:
self._oDb.execute('INSERT INTO TestResultFiles(idTestResult, idStrFile, idStrDescription, idStrKind, idStrMime)\n'
'VALUES (%s, %s, %s, %s, %s)\n'
, ( idTestResult,
));
return oFile;
"""
Returns an array of TestBoxData object representing the gang for the given testset.
"""
'FROM TestBoxes, TestSets\n'
'WHERE TestSets.idTestSetGangLeader = %s\n'
' AND TestSets.idGenTestBox = TestBoxes.idGenTestBox\n'
'ORDER BY iGangMemberNo ASC\n'
, (idTestSetGangLeader,));
aoTestBoxes = [];
return aoTestBoxes;
"""
Gets the TestResultFileEx corresponding to idTestResultFile.
Raises an exception if the file wasn't found, doesn't belong to
idTestSet, and on DB error.
"""
' StrTabFile.sValue AS sFile,\n'
' StrTabDesc.sValue AS sDescription,\n'
' StrTabKind.sValue AS sKind,\n'
' StrTabMime.sValue AS sMime\n'
'FROM TestResultFiles,\n'
' TestResultStrTab AS StrTabFile,\n'
' TestResultStrTab AS StrTabDesc,\n'
' TestResultStrTab AS StrTabKind,\n'
' TestResultStrTab AS StrTabMime,\n'
' TestResults\n'
'WHERE TestResultFiles.idTestResultFile = %s\n'
' AND TestResultFiles.idStrFile = StrTabFile.idStr\n'
' AND TestResultFiles.idStrDescription = StrTabDesc.idStr\n'
' AND TestResultFiles.idStrKind = StrTabKind.idStr\n'
' AND TestResultFiles.idStrMime = StrTabMime.idStr\n'
' AND TestResults.idTestResult = TestResultFiles.idTestResult\n'
' AND TestResults.idTestSet = %s\n'
, ( idTestResultFile, idTestSet, ));
"""
Get TestSet table record by its id
"""
'FROM TestSets\n'
'WHERE idTestSet=%s\n',
(idTestSet,))
raise TMExceptionBase('Found more than one test sets with the same credentials. Database structure is corrupted.')
try:
except IndexError:
return None
"""
Returns a list of TestSetData objects of orphaned test sets.
A test set is orphaned if tsDone is NULL and the testbox has created
one or more newer testsets.
"""
'FROM TestSets,\n'
' (SELECT idTestSet, idTestBox FROM TestSets WHERE tsDone is NULL) AS t\n'
'WHERE TestSets.idTestSet = t.idTestSet\n'
' AND EXISTS(SELECT 1 FROM TestSets st\n'
' WHERE st.idTestBox = t.idTestBox AND st.idTestSet > t.idTestSet)\n'
' AND NOT EXISTS(SELECT 1 FROM TestBoxStatuses tbs\n'
' WHERE tbs.idTestBox = t.idTestBox AND tbs.idTestSet = t.idTestSet)\n'
'ORDER by TestSets.idTestBox, TestSets.idTestSet'
);
aoRet = [];
return aoRet;
#
# Unit testing.
#
# pylint: disable=C0111
if __name__ == '__main__':
# not reached.