VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/testmanager/core/testcaseargs.py@ 61255

Last change on this file since 61255 was 61255, checked in by vboxsync, 9 years ago

testmanager: two database changes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.6 KB
Line 
1# -*- coding: utf-8 -*-
2# $Id: testcaseargs.py 61255 2016-05-28 03:52:35Z vboxsync $
3
4"""
5Test Manager - Test Case Arguments Variations.
6"""
7
8__copyright__ = \
9"""
10Copyright (C) 2012-2015 Oracle Corporation
11
12This file is part of VirtualBox Open Source Edition (OSE), as
13available from http://www.virtualbox.org. This file is free software;
14you can redistribute it and/or modify it under the terms of the GNU
15General Public License (GPL) as published by the Free Software
16Foundation, in version 2 as it comes in the "COPYING" file of the
17VirtualBox OSE distribution. VirtualBox OSE is distributed in the
18hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
19
20The contents of this file may alternatively be used under the terms
21of the Common Development and Distribution License Version 1.0
22(CDDL) only, as it comes in the "COPYING.CDDL" file of the
23VirtualBox OSE distribution, in which case the provisions of the
24CDDL are applicable instead of those of the GPL.
25
26You may elect to license modified versions of this file under the
27terms and conditions of either the GPL or the CDDL or both.
28"""
29__version__ = "$Revision: 61255 $"
30
31
32# Standard python imports.
33import unittest;
34import sys;
35
36# Validation Kit imports.
37from common import utils;
38from testmanager.core.base import ModelDataBase, ModelDataBaseTestCase, ModelLogicBase, TMExceptionBase, \
39 TMRowNotFound;
40from testmanager.core.testcase import TestCaseData, TestCaseDependencyLogic, TestCaseGlobalRsrcDepLogic;
41
42# Python 3 hacks:
43if sys.version_info[0] >= 3:
44 long = int; # pylint: disable=W0622,C0103
45
46
47class TestCaseArgsData(ModelDataBase):
48 """
49 Test case argument variation.
50 """
51
52 ksIdAttr = 'idTestCaseArgs';
53 ksIdGenAttr = 'idGenTestCaseArgs';
54
55 ksParam_idTestCase = 'TestCaseArgs_idTestCase';
56 ksParam_idTestCaseArgs = 'TestCaseArgs_idTestCaseArgs';
57 ksParam_tsEffective = 'TestCaseArgs_tsEffective';
58 ksParam_tsExpire = 'TestCaseArgs_tsExpire';
59 ksParam_uidAuthor = 'TestCaseArgs_uidAuthor';
60 ksParam_idGenTestCaseArgs = 'TestCaseArgs_idGenTestCaseArgs';
61 ksParam_sArgs = 'TestCaseArgs_sArgs';
62 ksParam_cSecTimeout = 'TestCaseArgs_cSecTimeout';
63 ksParam_sTestBoxReqExpr = 'TestCaseArgs_sTestBoxReqExpr';
64 ksParam_sBuildReqExpr = 'TestCaseArgs_sBuildReqExpr';
65 ksParam_cGangMembers = 'TestCaseArgs_cGangMembers';
66 ksParam_sSubName = 'TestCaseArgs_sSubName';
67
68 kcDbColumns = 12;
69
70 kasAllowNullAttributes = [ 'idTestCase', 'idTestCaseArgs', 'tsEffective', 'tsExpire', 'uidAuthor', 'idGenTestCaseArgs',
71 'cSecTimeout', 'sTestBoxReqExpr', 'sBuildReqExpr', 'sSubName', ];
72
73 def __init__(self):
74 ModelDataBase.__init__(self);
75
76 #
77 # Initialize with defaults.
78 # See the database for explanations of each of these fields.
79 #
80 self.idTestCase = None;
81 self.idTestCaseArgs = None;
82 self.tsEffective = None;
83 self.tsExpire = None;
84 self.uidAuthor = None;
85 self.idGenTestCaseArgs = None;
86 self.sArgs = '';
87 self.cSecTimeout = None;
88 self.sTestBoxReqExpr = None;
89 self.sBuildReqExpr = None;
90 self.cGangMembers = 1;
91 self.sSubName = None;
92
93 def initFromDbRow(self, aoRow):
94 """
95 Re-initializes the object from a SELECT * FROM TestCaseArgs row.
96 Returns self. Raises exception if aoRow is None.
97 """
98 if aoRow is None:
99 raise TMRowNotFound('TestBoxStatus not found.');
100
101 self.idTestCase = aoRow[0];
102 self.idTestCaseArgs = aoRow[1];
103 self.tsEffective = aoRow[2];
104 self.tsExpire = aoRow[3];
105 self.uidAuthor = aoRow[4];
106 self.idGenTestCaseArgs = aoRow[5];
107 self.sArgs = aoRow[6];
108 self.cSecTimeout = aoRow[7];
109 self.sTestBoxReqExpr = aoRow[8];
110 self.sBuildReqExpr = aoRow[9];
111 self.cGangMembers = aoRow[10];
112 self.sSubName = aoRow[11];
113 return self;
114
115 def initFromDbWithId(self, oDb, idTestCaseArgs, tsNow = None, sPeriodBack = None):
116 """
117 Initialize from the database.
118 """
119 oDb.execute(self.formatSimpleNowAndPeriodQuery(oDb,
120 'SELECT *\n'
121 'FROM TestCaseArgs\n'
122 'WHERE idTestCaseArgs = %s\n'
123 , ( idTestCaseArgs,), tsNow, sPeriodBack));
124 aoRow = oDb.fetchOne()
125 if aoRow is None:
126 raise TMRowNotFound('idTestCaseArgs=%s not found (tsNow=%s sPeriodBack=%s)'
127 % (idTestCaseArgs, tsNow, sPeriodBack,));
128 return self.initFromDbRow(aoRow);
129
130 def initFromDbWithGenId(self, oDb, idGenTestCaseArgs):
131 """
132 Initialize from the database, given the generation ID of a row.
133 """
134 oDb.execute('SELECT * FROM TestCaseArgs WHERE idGenTestCaseArgs = %s', (idGenTestCaseArgs,));
135 return self.initFromDbRow(oDb.fetchOne());
136
137 def initFromValues(self, sArgs, cSecTimeout = None, sTestBoxReqExpr = None, sBuildReqExpr = None, # pylint: disable=R0913
138 cGangMembers = 1, idTestCase = None, idTestCaseArgs = None, tsEffective = None, tsExpire = None,
139 uidAuthor = None, idGenTestCaseArgs = None, sSubName = None):
140 """
141 Reinitialize from values.
142 Returns self.
143 """
144 self.idTestCase = idTestCase;
145 self.idTestCaseArgs = idTestCaseArgs;
146 self.tsEffective = tsEffective;
147 self.tsExpire = tsExpire;
148 self.uidAuthor = uidAuthor;
149 self.idGenTestCaseArgs = idGenTestCaseArgs;
150 self.sArgs = sArgs;
151 self.cSecTimeout = utils.parseIntervalSeconds(cSecTimeout);
152 self.sTestBoxReqExpr = sTestBoxReqExpr;
153 self.sBuildReqExpr = sBuildReqExpr;
154 self.cGangMembers = cGangMembers;
155 self.sSubName = sSubName;
156 return self;
157
158 def getAttributeParamNullValues(self, sAttr):
159 aoNilValues = ModelDataBase.getAttributeParamNullValues(self, sAttr);
160 if sAttr == 'cSecTimeout':
161 aoNilValues.insert(0, ''); # Prettier NULL value for cSecTimeout.
162 elif sAttr == 'sArgs':
163 aoNilValues = []; # No NULL value here, thank you.
164 return aoNilValues;
165
166 def _validateAndConvertAttribute(self, sAttr, sParam, oValue, aoNilValues, fAllowNull, oDb):
167 if sAttr == 'cSecTimeout' and oValue not in aoNilValues: # Allow human readable interval formats.
168 return utils.parseIntervalSeconds(oValue);
169
170 (oValue, sError) = ModelDataBase._validateAndConvertAttribute(self, sAttr, sParam, oValue, aoNilValues, fAllowNull, oDb);
171 if sError is None:
172 if sAttr == 'sTestBoxReqExpr':
173 sError = TestCaseData.validateTestBoxReqExpr(oValue);
174 elif sAttr == 'sBuildReqExpr':
175 sError = TestCaseData.validateBuildReqExpr(oValue);
176 return (oValue, sError);
177
178
179
180
181class TestCaseArgsDataEx(TestCaseArgsData):
182 """
183 Complete data set.
184 """
185
186 def __init__(self):
187 TestCaseArgsData.__init__(self);
188 self.oTestCase = None;
189 self.aoTestCasePreReqs = [];
190 self.aoGlobalRsrc = [];
191
192 def initFromDbRow(self, aoRow):
193 raise TMExceptionBase('Do not call me: %s' % (aoRow,))
194
195 def initFromDbWithId(self, oDb, idTestCaseArgs, tsNow = None, sPeriodBack = None):
196 _ = oDb; _ = idTestCaseArgs; _ = tsNow; _ = sPeriodBack;
197 raise TMExceptionBase('Not supported.');
198
199 def initFromDbWithGenId(self, oDb, idGenTestCaseArgs):
200 _ = oDb; _ = idGenTestCaseArgs;
201 raise TMExceptionBase('Use initFromDbWithGenIdEx...');
202
203 def initFromDbWithGenIdEx(self, oDb, idGenTestCaseArgs, tsConfigEff = None, tsRsrcEff = None):
204 """
205 Initialize from the database, given the ID of a row.
206 """
207
208 oDb.execute('SELECT *, CURRENT_TIMESTAMP FROM TestCaseArgs WHERE idGenTestCaseArgs = %s', (idGenTestCaseArgs,));
209 aoRow = oDb.fetchOne();
210 TestCaseArgsData.initFromDbRow(self, aoRow);
211
212 tsNow = aoRow[11];
213 if tsConfigEff is None: tsConfigEff = tsNow;
214 if tsRsrcEff is None: tsRsrcEff = tsNow;
215
216 self.oTestCase = TestCaseData().initFromDbWithId(oDb, self.idTestCase, tsConfigEff);
217 self.aoTestCasePreReqs = TestCaseDependencyLogic(oDb).getTestCaseDeps(self.idTestCase, tsConfigEff);
218 self.aoGlobalRsrc = TestCaseGlobalRsrcDepLogic(oDb).getTestCaseDeps(self.idTestCase, tsRsrcEff);
219
220 return self;
221
222 def convertFromParamNull(self):
223 raise TMExceptionBase('Not implemented');
224
225 def convertToParamNull(self):
226 raise TMExceptionBase('Not implemented');
227
228 def isEqual(self, oOther):
229 raise TMExceptionBase('Not implemented');
230
231 def matchesTestBoxProps(self, oTestBoxData):
232 """
233 Checks if the all of the testbox related test requirements matches the
234 given testbox.
235
236 Returns True or False according to the expression, None on exception or
237 non-boolean expression result.
238 """
239 return TestCaseData.matchesTestBoxPropsEx(oTestBoxData, self.oTestCase.sTestBoxReqExpr) \
240 and TestCaseData.matchesTestBoxPropsEx(oTestBoxData, self.sTestBoxReqExpr);
241
242 def matchesBuildProps(self, oBuildDataEx):
243 """
244 Checks if the all of the build related test requirements matches the
245 given build.
246
247 Returns True or False according to the expression, None on exception or
248 non-boolean expression result.
249 """
250 return TestCaseData.matchesBuildPropsEx(oBuildDataEx, self.oTestCase.sBuildReqExpr) \
251 and TestCaseData.matchesBuildPropsEx(oBuildDataEx, self.sBuildReqExpr);
252
253
254class TestCaseArgsLogic(ModelLogicBase):
255 """
256 TestCaseArgs database logic.
257 """
258
259 def __init__(self, oDb):
260 ModelLogicBase.__init__(self, oDb);
261
262
263 def areResourcesFree(self, oDataEx):
264 """
265 Checks if all global resources are currently still in existance and free.
266 Returns True/False. May raise exception on database error.
267 """
268
269 # Create a set of global resource IDs.
270 if len(oDataEx.aoGlobalRsrc) == 0:
271 return True;
272 asIdRsrcs = [str(oDep.idGlobalRsrc) for oDep, _ in oDataEx.aoGlobalRsrc];
273
274 # A record in the resource status table means it's allocated.
275 self._oDb.execute('SELECT COUNT(*)\n'
276 'FROM GlobalResourceStatuses\n'
277 'WHERE GlobalResourceStatuses.idGlobalRsrc IN (' + ', '.join(asIdRsrcs) + ')\n');
278 if self._oDb.fetchOne()[0] == 0:
279 # Check for disabled or deleted resources (we cannot allocate them).
280 self._oDb.execute('SELECT COUNT(*)\n'
281 'FROM GlobalResources\n'
282 'WHERE GlobalResources.idGlobalRsrc IN (' + ', '.join(asIdRsrcs) + ')\n'
283 ' AND GlobalResources.tsExpire = \'infinity\'::TIMESTAMP\n'
284 ' AND GlobalResources.fEnabled = TRUE\n');
285 if self._oDb.fetchOne()[0] == len(oDataEx.aoGlobalRsrc):
286 return True;
287 return False;
288
289 def getAll(self):
290 """Get list of objects of type TestCaseArgsData"""
291 self._oDb.execute('SELECT *\n'
292 'FROM TestCaseArgs\n'
293 'WHERE tsExpire = \'infinity\'::TIMESTAMP')
294 aaoRows = self._oDb.fetchAll()
295 aoRet = []
296 for aoRow in aaoRows:
297 aoRet.append(TestCaseArgsData().initFromDbRow(aoRow))
298
299 return aoRet
300
301 def getTestCaseArgs(self, idTestCase, tsNow = None, aiWhiteList = None):
302 """Get list of testcase's arguments variations"""
303 if aiWhiteList is None:
304 if tsNow is None:
305 self._oDb.execute('SELECT *\n'
306 'FROM TestCaseArgs\n'
307 'WHERE idTestCase = %s\n'
308 ' AND tsExpire = \'infinity\'::TIMESTAMP\n'
309 'ORDER BY TestCaseArgs.idTestCaseArgs\n'
310 , (idTestCase,));
311 else:
312 self._oDb.execute('SELECT *\n'
313 'FROM TestCaseArgs\n'
314 'WHERE idTestCase = %s\n'
315 ' AND tsExpire > %s\n'
316 ' AND tsEffective <= %s\n'
317 'ORDER BY TestCaseArgs.idTestCaseArgs\n'
318 , (idTestCase, tsNow, tsNow));
319 else:
320 sWhiteList = ','.join((str(x) for x in aiWhiteList));
321 if tsNow is None:
322 self._oDb.execute('SELECT *\n'
323 'FROM TestCaseArgs\n'
324 'WHERE idTestCase = %s\n'
325 ' AND tsExpire = \'infinity\'::TIMESTAMP\n'
326 ' AND idTestCaseArgs IN (' + sWhiteList + ')\n'
327 'ORDER BY TestCaseArgs.idTestCaseArgs\n'
328 , (idTestCase,));
329 else:
330 self._oDb.execute('SELECT *\n'
331 'FROM TestCaseArgs\n'
332 'WHERE idTestCase = %s\n'
333 ' AND tsExpire > %s\n'
334 ' AND tsEffective <= %s\n'
335 ' AND idTestCaseArgs IN (' + sWhiteList + ')\n'
336 'ORDER BY TestCaseArgs.idTestCaseArgs\n'
337 , (idTestCase, tsNow, tsNow));
338
339 aaoRows = self._oDb.fetchAll()
340 aoRet = []
341 for aoRow in aaoRows:
342 aoRet.append(TestCaseArgsData().initFromDbRow(aoRow))
343
344 return aoRet
345
346 def addTestCaseArgs(self, oTestCaseArgsData):
347 """Add Test Case Args record into DB"""
348 pass
349
350
351#
352# Unit testing.
353#
354
355# pylint: disable=C0111
356class TestCaseArgsDataTestCase(ModelDataBaseTestCase):
357 def setUp(self):
358 self.aoSamples = [TestCaseArgsData(),];
359
360if __name__ == '__main__':
361 unittest.main();
362 # not reached.
363
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette