VirtualBox

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

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

testmanager: failiure reason fixes, some exception throwing cleanups, delinting with pylint 1.5.5.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.3 KB
Line 
1# -*- coding: utf-8 -*-
2# $Id: testcaseargs.py 61220 2016-05-27 01:16:02Z 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.215389.xyz. 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: 61220 $"
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
67 kasAllowNullAttributes = [ 'idTestCase', 'idTestCaseArgs', 'tsEffective', 'tsExpire', 'uidAuthor', 'idGenTestCaseArgs',
68 'cSecTimeout', 'sTestBoxReqExpr', 'sBuildReqExpr', ];
69
70 def __init__(self):
71 ModelDataBase.__init__(self);
72
73 #
74 # Initialize with defaults.
75 # See the database for explanations of each of these fields.
76 #
77 self.idTestCase = None;
78 self.idTestCaseArgs = None;
79 self.tsEffective = None;
80 self.tsExpire = None;
81 self.uidAuthor = None;
82 self.idGenTestCaseArgs = None;
83 self.sArgs = '';
84 self.cSecTimeout = None;
85 self.sTestBoxReqExpr = None;
86 self.sBuildReqExpr = None;
87 self.cGangMembers = 1;
88
89 def initFromDbRow(self, aoRow):
90 """
91 Re-initializes the object from a SELECT * FROM TestCaseArgs row.
92 Returns self. Raises exception if aoRow is None.
93 """
94 if aoRow is None:
95 raise TMRowNotFound('TestBoxStatus not found.');
96
97 self.idTestCase = aoRow[0];
98 self.idTestCaseArgs = aoRow[1];
99 self.tsEffective = aoRow[2];
100 self.tsExpire = aoRow[3];
101 self.uidAuthor = aoRow[4];
102 self.idGenTestCaseArgs = aoRow[5];
103 self.sArgs = aoRow[6];
104 self.cSecTimeout = aoRow[7];
105 self.sTestBoxReqExpr = aoRow[8];
106 self.sBuildReqExpr = aoRow[9];
107 self.cGangMembers = aoRow[10];
108 return self;
109
110 def initFromDbWithId(self, oDb, idTestCaseArgs, tsNow = None, sPeriodBack = None):
111 """
112 Initialize from the database.
113 """
114 oDb.execute(self.formatSimpleNowAndPeriodQuery(oDb,
115 'SELECT *\n'
116 'FROM TestCaseArgs\n'
117 'WHERE idTestCaseArgs = %s\n'
118 , ( idTestCaseArgs,), tsNow, sPeriodBack));
119 aoRow = oDb.fetchOne()
120 if aoRow is None:
121 raise TMRowNotFound('idTestCaseArgs=%s not found (tsNow=%s sPeriodBack=%s)'
122 % (idTestCaseArgs, tsNow, sPeriodBack,));
123 return self.initFromDbRow(aoRow);
124
125 def initFromDbWithGenId(self, oDb, idGenTestCaseArgs):
126 """
127 Initialize from the database, given the generation ID of a row.
128 """
129 oDb.execute('SELECT * FROM TestCaseArgs WHERE idGenTestCaseArgs = %s', (idGenTestCaseArgs,));
130 return self.initFromDbRow(oDb.fetchOne());
131
132 def initFromValues(self, sArgs, cSecTimeout = None, sTestBoxReqExpr = None, sBuildReqExpr = None, # pylint: disable=R0913
133 cGangMembers = 1, idTestCase = None, idTestCaseArgs = None, tsEffective = None, tsExpire = None,
134 uidAuthor = None, idGenTestCaseArgs = None):
135 """
136 Reinitialize from values.
137 Returns self.
138 """
139 self.idTestCase = idTestCase;
140 self.idTestCaseArgs = idTestCaseArgs;
141 self.tsEffective = tsEffective;
142 self.tsExpire = tsExpire;
143 self.uidAuthor = uidAuthor;
144 self.idGenTestCaseArgs = idGenTestCaseArgs;
145 self.sArgs = sArgs;
146 self.cSecTimeout = utils.parseIntervalSeconds(cSecTimeout);
147 self.sTestBoxReqExpr = sTestBoxReqExpr;
148 self.sBuildReqExpr = sBuildReqExpr;
149 self.cGangMembers = cGangMembers;
150 return self;
151
152 def getAttributeParamNullValues(self, sAttr):
153 aoNilValues = ModelDataBase.getAttributeParamNullValues(self, sAttr);
154 if sAttr == 'cSecTimeout':
155 aoNilValues.insert(0, ''); # Prettier NULL value for cSecTimeout.
156 elif sAttr == 'sArgs':
157 aoNilValues = []; # No NULL value here, thank you.
158 return aoNilValues;
159
160 def _validateAndConvertAttribute(self, sAttr, sParam, oValue, aoNilValues, fAllowNull, oDb):
161 if sAttr == 'cSecTimeout' and oValue not in aoNilValues: # Allow human readable interval formats.
162 return utils.parseIntervalSeconds(oValue);
163
164 (oValue, sError) = ModelDataBase._validateAndConvertAttribute(self, sAttr, sParam, oValue, aoNilValues, fAllowNull, oDb);
165 if sError is None:
166 if sAttr == 'sTestBoxReqExpr':
167 sError = TestCaseData.validateTestBoxReqExpr(oValue);
168 elif sAttr == 'sBuildReqExpr':
169 sError = TestCaseData.validateBuildReqExpr(oValue);
170 return (oValue, sError);
171
172
173
174
175class TestCaseArgsDataEx(TestCaseArgsData):
176 """
177 Complete data set.
178 """
179
180 def __init__(self):
181 TestCaseArgsData.__init__(self);
182 self.oTestCase = None;
183 self.aoTestCasePreReqs = [];
184 self.aoGlobalRsrc = [];
185
186 def initFromDbRow(self, aoRow):
187 raise TMExceptionBase('Do not call me: %s' % (aoRow,))
188
189 def initFromDbWithId(self, oDb, idTestCaseArgs, tsNow = None, sPeriodBack = None):
190 _ = oDb; _ = idTestCaseArgs; _ = tsNow; _ = sPeriodBack;
191 raise TMExceptionBase('Not supported.');
192
193 def initFromDbWithGenId(self, oDb, idGenTestCaseArgs):
194 _ = oDb; _ = idGenTestCaseArgs;
195 raise TMExceptionBase('Use initFromDbWithGenIdEx...');
196
197 def initFromDbWithGenIdEx(self, oDb, idGenTestCaseArgs, tsConfigEff = None, tsRsrcEff = None):
198 """
199 Initialize from the database, given the ID of a row.
200 """
201
202 oDb.execute('SELECT *, CURRENT_TIMESTAMP FROM TestCaseArgs WHERE idGenTestCaseArgs = %s', (idGenTestCaseArgs,));
203 aoRow = oDb.fetchOne();
204 TestCaseArgsData.initFromDbRow(self, aoRow);
205
206 tsNow = aoRow[11];
207 if tsConfigEff is None: tsConfigEff = tsNow;
208 if tsRsrcEff is None: tsRsrcEff = tsNow;
209
210 self.oTestCase = TestCaseData().initFromDbWithId(oDb, self.idTestCase, tsConfigEff);
211 self.aoTestCasePreReqs = TestCaseDependencyLogic(oDb).getTestCaseDeps(self.idTestCase, tsConfigEff);
212 self.aoGlobalRsrc = TestCaseGlobalRsrcDepLogic(oDb).getTestCaseDeps(self.idTestCase, tsRsrcEff);
213
214 return self;
215
216 def convertFromParamNull(self):
217 raise TMExceptionBase('Not implemented');
218
219 def convertToParamNull(self):
220 raise TMExceptionBase('Not implemented');
221
222 def isEqual(self, oOther):
223 raise TMExceptionBase('Not implemented');
224
225 def matchesTestBoxProps(self, oTestBoxData):
226 """
227 Checks if the all of the testbox related test requirements matches the
228 given testbox.
229
230 Returns True or False according to the expression, None on exception or
231 non-boolean expression result.
232 """
233 return TestCaseData.matchesTestBoxPropsEx(oTestBoxData, self.oTestCase.sTestBoxReqExpr) \
234 and TestCaseData.matchesTestBoxPropsEx(oTestBoxData, self.sTestBoxReqExpr);
235
236 def matchesBuildProps(self, oBuildDataEx):
237 """
238 Checks if the all of the build related test requirements matches the
239 given build.
240
241 Returns True or False according to the expression, None on exception or
242 non-boolean expression result.
243 """
244 return TestCaseData.matchesBuildPropsEx(oBuildDataEx, self.oTestCase.sBuildReqExpr) \
245 and TestCaseData.matchesBuildPropsEx(oBuildDataEx, self.sBuildReqExpr);
246
247
248class TestCaseArgsLogic(ModelLogicBase):
249 """
250 TestCaseArgs database logic.
251 """
252
253 def __init__(self, oDb):
254 ModelLogicBase.__init__(self, oDb);
255
256
257 def areResourcesFree(self, oDataEx):
258 """
259 Checks if all global resources are currently still in existance and free.
260 Returns True/False. May raise exception on database error.
261 """
262
263 # Create a set of global resource IDs.
264 if len(oDataEx.aoGlobalRsrc) == 0:
265 return True;
266 asIdRsrcs = [str(oDep.idGlobalRsrc) for oDep, _ in oDataEx.aoGlobalRsrc];
267
268 # A record in the resource status table means it's allocated.
269 self._oDb.execute('SELECT COUNT(*)\n'
270 'FROM GlobalResourceStatuses\n'
271 'WHERE GlobalResourceStatuses.idGlobalRsrc IN (' + ', '.join(asIdRsrcs) + ')\n');
272 if self._oDb.fetchOne()[0] == 0:
273 # Check for disabled or deleted resources (we cannot allocate them).
274 self._oDb.execute('SELECT COUNT(*)\n'
275 'FROM GlobalResources\n'
276 'WHERE GlobalResources.idGlobalRsrc IN (' + ', '.join(asIdRsrcs) + ')\n'
277 ' AND GlobalResources.tsExpire = \'infinity\'::TIMESTAMP\n'
278 ' AND GlobalResources.fEnabled = TRUE\n');
279 if self._oDb.fetchOne()[0] == len(oDataEx.aoGlobalRsrc):
280 return True;
281 return False;
282
283 def getAll(self):
284 """Get list of objects of type TestCaseArgsData"""
285 self._oDb.execute('SELECT *\n'
286 'FROM TestCaseArgs\n'
287 'WHERE tsExpire = \'infinity\'::TIMESTAMP')
288 aaoRows = self._oDb.fetchAll()
289 aoRet = []
290 for aoRow in aaoRows:
291 aoRet.append(TestCaseArgsData().initFromDbRow(aoRow))
292
293 return aoRet
294
295 def getTestCaseArgs(self, idTestCase, tsNow = None, aiWhiteList = None):
296 """Get list of testcase's arguments variations"""
297 if aiWhiteList is None:
298 if tsNow is None:
299 self._oDb.execute('SELECT *\n'
300 'FROM TestCaseArgs\n'
301 'WHERE idTestCase = %s\n'
302 ' AND tsExpire = \'infinity\'::TIMESTAMP\n'
303 'ORDER BY TestCaseArgs.idTestCaseArgs\n'
304 , (idTestCase,));
305 else:
306 self._oDb.execute('SELECT *\n'
307 'FROM TestCaseArgs\n'
308 'WHERE idTestCase = %s\n'
309 ' AND tsExpire > %s\n'
310 ' AND tsEffective <= %s\n'
311 'ORDER BY TestCaseArgs.idTestCaseArgs\n'
312 , (idTestCase, tsNow, tsNow));
313 else:
314 sWhiteList = ','.join((str(x) for x in aiWhiteList));
315 if tsNow is None:
316 self._oDb.execute('SELECT *\n'
317 'FROM TestCaseArgs\n'
318 'WHERE idTestCase = %s\n'
319 ' AND tsExpire = \'infinity\'::TIMESTAMP\n'
320 ' AND idTestCaseArgs IN (' + sWhiteList + ')\n'
321 'ORDER BY TestCaseArgs.idTestCaseArgs\n'
322 , (idTestCase,));
323 else:
324 self._oDb.execute('SELECT *\n'
325 'FROM TestCaseArgs\n'
326 'WHERE idTestCase = %s\n'
327 ' AND tsExpire > %s\n'
328 ' AND tsEffective <= %s\n'
329 ' AND idTestCaseArgs IN (' + sWhiteList + ')\n'
330 'ORDER BY TestCaseArgs.idTestCaseArgs\n'
331 , (idTestCase, tsNow, tsNow));
332
333 aaoRows = self._oDb.fetchAll()
334 aoRet = []
335 for aoRow in aaoRows:
336 aoRet.append(TestCaseArgsData().initFromDbRow(aoRow))
337
338 return aoRet
339
340 def addTestCaseArgs(self, oTestCaseArgsData):
341 """Add Test Case Args record into DB"""
342 pass
343
344
345#
346# Unit testing.
347#
348
349# pylint: disable=C0111
350class TestCaseArgsDataTestCase(ModelDataBaseTestCase):
351 def setUp(self):
352 self.aoSamples = [TestCaseArgsData(),];
353
354if __name__ == '__main__':
355 unittest.main();
356 # not reached.
357
Note: See TracBrowser for help on using the repository browser.

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