VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/testmanager/core/testboxstatus.py@ 61502

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

testmanager: Testboxes can now be members of more than one scheduling group.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.8 KB
Line 
1# -*- coding: utf-8 -*-
2# $Id: testboxstatus.py 61502 2016-06-06 17:53:01Z vboxsync $
3
4"""
5Test Manager - TestBoxStatus.
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: 61502 $"
30
31
32# Standard python imports.
33import unittest;
34
35# Validation Kit imports.
36from testmanager.core.base import ModelDataBase, ModelDataBaseTestCase, ModelLogicBase, TMTooManyRows, TMRowNotFound;
37from testmanager.core.testbox import TestBoxData;
38
39
40class TestBoxStatusData(ModelDataBase):
41 """
42 TestBoxStatus Data.
43 """
44
45 ## @name TestBoxState_T
46 # @{
47 ksTestBoxState_Idle = 'idle';
48 ksTestBoxState_Testing = 'testing';
49 ksTestBoxState_GangGathering = 'gang-gathering';
50 ksTestBoxState_GangGatheringTimedOut = 'gang-gathering-timedout';
51 ksTestBoxState_GangTesting = 'gang-testing';
52 ksTestBoxState_GangCleanup = 'gang-cleanup';
53 ksTestBoxState_Rebooting = 'rebooting';
54 ksTestBoxState_Upgrading = 'upgrading';
55 ksTestBoxState_UpgradingAndRebooting = 'upgrading-and-rebooting';
56 ksTestBoxState_DoingSpecialCmd = 'doing-special-cmd';
57 ## @}
58
59 ksParam_idTestBox = 'TestBoxStatus_idTestBox';
60 ksParam_idGenTestBox = 'TestBoxStatus_idGenTestBox'
61 ksParam_tsUpdated = 'TestBoxStatus_tsUpdated';
62 ksParam_enmState = 'TestBoxStatus_enmState';
63 ksParam_idTestSet = 'TestBoxStatus_idTestSet';
64 ksParam_iWorkItem = 'TestBoxStatus_iWorkItem';
65
66 kasAllowNullAttributes = ['idTestSet', ];
67 kasValidValues_enmState = \
68 [
69 ksTestBoxState_Idle, ksTestBoxState_Testing, ksTestBoxState_GangGathering,
70 ksTestBoxState_GangGatheringTimedOut, ksTestBoxState_GangTesting, ksTestBoxState_GangCleanup,
71 ksTestBoxState_Rebooting, ksTestBoxState_Upgrading, ksTestBoxState_UpgradingAndRebooting,
72 ksTestBoxState_DoingSpecialCmd,
73 ];
74
75 kcDbColumns = 6;
76
77 def __init__(self):
78 ModelDataBase.__init__(self);
79
80 #
81 # Initialize with defaults.
82 # See the database for explanations of each of these fields.
83 #
84 self.idTestBox = None;
85 self.idGenTestBox = None;
86 self.tsUpdated = None;
87 self.enmState = self.ksTestBoxState_Idle;
88 self.idTestSet = None;
89 self.iWorkItem = None;
90
91 def initFromDbRow(self, aoRow):
92 """
93 Internal worker for initFromDbWithId and initFromDbWithGenId as well as
94 TestBoxStatusLogic.
95 """
96
97 if aoRow is None:
98 raise TMRowNotFound('TestBoxStatus not found.');
99
100 self.idTestBox = aoRow[0];
101 self.idGenTestBox = aoRow[1];
102 self.tsUpdated = aoRow[2];
103 self.enmState = aoRow[3];
104 self.idTestSet = aoRow[4];
105 self.iWorkItem = aoRow[5];
106 return self;
107
108 def initFromDbWithId(self, oDb, idTestBox):
109 """
110 Initialize the object from the database.
111 """
112 oDb.execute('SELECT *\n'
113 'FROM TestBoxStatuses\n'
114 'WHERE idTestBox = %s\n'
115 , (idTestBox, ) );
116 return self.initFromDbRow(oDb.fetchOne());
117
118 def initFromDbWithGenId(self, oDb, idGenTestBox):
119 """
120 Initialize the object from the database.
121 """
122 oDb.execute('SELECT *\n'
123 'FROM TestBoxStatuses\n'
124 'WHERE idGenTestBox = %s\n'
125 , (idGenTestBox, ) );
126 return self.initFromDbRow(oDb.fetchOne());
127
128
129class TestBoxStatusLogic(ModelLogicBase):
130 """
131 TestBoxStatus logic.
132 """
133
134 ## The number of seconds between each time to call touchStatus() when
135 # returning CMD_IDLE.
136 kcSecIdleTouchStatus = 120;
137
138
139 def __init__(self, oDb):
140 ModelLogicBase.__init__(self, oDb);
141
142
143 def tryFetchStatus(self, idTestBox):
144 """
145 Attempts to fetch the status of the given testbox.
146
147 Returns a TestBoxStatusData object on success.
148 Returns None if no status was found.
149 Raises exception on other errors.
150 """
151 self._oDb.execute('SELECT *\n'
152 'FROM TestBoxStatuses\n'
153 'WHERE idTestBox = %s\n',
154 (idTestBox,));
155 if self._oDb.getRowCount() == 0:
156 return None;
157 oStatus = TestBoxStatusData();
158 return oStatus.initFromDbRow(self._oDb.fetchOne());
159
160 def tryFetchStatusAndConfig(self, idTestBox, sTestBoxUuid, sTestBoxAddr):
161 """
162 Tries to fetch the testbox status and current testbox config.
163
164 Returns (TestBoxStatusData, TestBoxData) on success, (None, None) if
165 not found. May throw an exception on database error.
166 """
167 self._oDb.execute('SELECT TestBoxStatuses.*,\n'
168 ' TestBoxesWithStrings.*,\n'
169 'FROM TestBoxStatuses,\n'
170 ' TestBoxesWithStrings\n'
171 'WHERE TestBoxStatuses.idTestBox = %s\n'
172 ' AND TestBoxesWithStrings.idTestBox = %s\n'
173 ' AND TestBoxesWithStrings.tsExpire = \'infinity\'::TIMESTAMP\n'
174 ' AND TestBoxesWithStrings.uuidSystem = %s\n'
175 ' AND TestBoxesWithStrings.ip = %s\n'
176 , (idTestBox,
177 idTestBox,
178 sTestBoxUuid,
179 sTestBoxAddr,
180 ));
181 cRows = self._oDb.getRowCount();
182 if cRows != 1:
183 if cRows != 0:
184 raise TMTooManyRows('tryFetchStatusForCommandReq got %s rows for idTestBox=%s' % (cRows, idTestBox));
185 return (None, None);
186 aoRow = self._oDb.fetchOne();
187 return (TestBoxStatusData().initFromDbRow(aoRow[:TestBoxStatusData.kcDbColumns]),
188 TestBoxData().initFromDbRow(aoRow[TestBoxStatusData.kcDbColumns:]));
189
190
191 def insertIdleStatus(self, idTestBox, idGenTestBox, fCommit = False):
192 """
193 Inserts an idle status for the specified testbox.
194 """
195 self._oDb.execute('INSERT INTO TestBoxStatuses (\n'
196 ' idTestBox,\n'
197 ' idGenTestBox,\n'
198 ' enmState,\n'
199 ' idTestSet,\n'
200 ' iWorkItem)\n'
201 'VALUES ( %s,\n'
202 ' %s,\n'
203 ' \'idle\'::TestBoxState_T,\n'
204 ' NULL,\n',
205 ' 0)\n',
206 (idTestBox, idGenTestBox) );
207 self._oDb.maybeCommit(fCommit);
208 return True;
209
210 def touchStatus(self, idTestBox, fCommit = False):
211 """
212 Touches the testbox status row, i.e. sets tsUpdated to the current time.
213 """
214 self._oDb.execute('UPDATE TestBoxStatuses\n'
215 'SET tsUpdated = CURRENT_TIMESTAMP\n'
216 'WHERE idTestBox = %s\n'
217 , (idTestBox,));
218 self._oDb.maybeCommit(fCommit);
219 return True;
220
221 def updateState(self, idTestBox, sNewState, idTestSet = None, fCommit = False):
222 """
223 Updates the testbox state.
224 """
225 self._oDb.execute('UPDATE TestBoxStatuses\n'
226 'SET enmState = %s,\n'
227 ' idTestSet = %s,\n'
228 ' tsUpdated = CURRENT_TIMESTAMP\n'
229 'WHERE idTestBox = %s\n',
230 (sNewState, idTestSet, idTestBox));
231 self._oDb.maybeCommit(fCommit);
232 return True;
233
234 def updateGangStatus(self, idTestSetGangLeader, sNewState, fCommit = False):
235 """
236 Update the state of all members of a gang.
237 """
238 self._oDb.execute('UPDATE TestBoxStatuses\n'
239 'SET enmState = %s,\n'
240 ' tsUpdated = CURRENT_TIMESTAMP\n'
241 'WHERE idTestBox IN (SELECT idTestBox\n'
242 ' FROM TestSets\n'
243 ' WHERE idTestSetGangLeader = %s)\n'
244 , (sNewState, idTestSetGangLeader,) );
245 self._oDb.maybeCommit(fCommit);
246 return True;
247
248 def updateWorkItem(self, idTestBox, iWorkItem, fCommit = False):
249 """
250 Updates the testbox state.
251 """
252 self._oDb.execute('UPDATE TestBoxStatuses\n'
253 'SET iWorkItem = %s\n'
254 'WHERE idTestBox = %s\n'
255 , ( iWorkItem, idTestBox,));
256 self._oDb.maybeCommit(fCommit);
257 return True;
258
259 def isWholeGangDoneTesting(self, idTestSetGangLeader):
260 """
261 Checks if the whole gang is done testing.
262 """
263 self._oDb.execute('SELECT COUNT(*)\n'
264 'FROM TestBoxStatuses, TestSets\n'
265 'WHERE TestBoxStatuses.idTestSet = TestSets.idTestSet\n'
266 ' AND TestSets.idTestSetGangLeader = %s\n'
267 ' AND TestBoxStatuses.enmState IN (%s, %s)\n'
268 , ( idTestSetGangLeader,
269 TestBoxStatusData.ksTestBoxState_GangGathering,
270 TestBoxStatusData.ksTestBoxState_GangTesting));
271 return self._oDb.fetchOne()[0] == 0;
272
273 def isTheWholeGangThere(self, idTestSetGangLeader):
274 """
275 Checks if the whole gang is done testing.
276 """
277 self._oDb.execute('SELECT COUNT(*)\n'
278 'FROM TestBoxStatuses, TestSets\n'
279 'WHERE TestBoxStatuses.idTestSet = TestSets.idTestSet\n'
280 ' AND TestSets.idTestSetGangLeader = %s\n'
281 ' AND TestBoxStatuses.enmState IN (%s, %s)\n'
282 , ( idTestSetGangLeader,
283 TestBoxStatusData.ksTestBoxState_GangGathering,
284 TestBoxStatusData.ksTestBoxState_GangTesting));
285 return self._oDb.fetchOne()[0] == 0;
286
287 def timeSinceLastChangeInSecs(self, oStatusData):
288 """
289 Figures the time since the last status change.
290 """
291 tsNow = self._oDb.getCurrentTimestamp();
292 oDelta = tsNow - oStatusData.tsUpdated;
293 return oDelta.seconds + oDelta.days * 24 * 3600;
294
295
296#
297# Unit testing.
298#
299
300# pylint: disable=C0111
301class TestBoxStatusDataTestCase(ModelDataBaseTestCase):
302 def setUp(self):
303 self.aoSamples = [TestBoxStatusData(),];
304
305if __name__ == '__main__':
306 unittest.main();
307 # not reached.
308
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