VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/testmanager/core/testboxstatus.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: 10.9 KB
Line 
1# -*- coding: utf-8 -*-
2# $Id: testboxstatus.py 61220 2016-05-27 01:16:02Z 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.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: 61220 $"
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
65 kasAllowNullAttributes = ['idTestSet', ];
66 kasValidValues_enmState = \
67 [
68 ksTestBoxState_Idle, ksTestBoxState_Testing, ksTestBoxState_GangGathering,
69 ksTestBoxState_GangGatheringTimedOut, ksTestBoxState_GangTesting, ksTestBoxState_GangCleanup,
70 ksTestBoxState_Rebooting, ksTestBoxState_Upgrading, ksTestBoxState_UpgradingAndRebooting,
71 ksTestBoxState_DoingSpecialCmd,
72 ];
73
74 def __init__(self):
75 ModelDataBase.__init__(self);
76
77 #
78 # Initialize with defaults.
79 # See the database for explanations of each of these fields.
80 #
81 self.idTestBox = None;
82 self.idGenTestBox = None;
83 self.tsUpdated = None;
84 self.enmState = self.ksTestBoxState_Idle;
85 self.idTestSet = None;
86
87 def initFromDbRow(self, aoRow):
88 """
89 Internal worker for initFromDbWithId and initFromDbWithGenId as well as
90 TestBoxStatusLogic.
91 """
92
93 if aoRow is None:
94 raise TMRowNotFound('TestBoxStatus not found.');
95
96 self.idTestBox = aoRow[0];
97 self.idGenTestBox = aoRow[1];
98 self.tsUpdated = aoRow[2];
99 self.enmState = aoRow[3];
100 self.idTestSet = aoRow[4];
101 return self;
102
103 def initFromDbWithId(self, oDb, idTestBox):
104 """
105 Initialize the object from the database.
106 """
107 oDb.execute('SELECT *\n'
108 'FROM TestBoxStatuses\n'
109 'WHERE idTestBox = %s\n'
110 , (idTestBox, ) );
111 return self.initFromDbRow(oDb.fetchOne());
112
113 def initFromDbWithGenId(self, oDb, idGenTestBox):
114 """
115 Initialize the object from the database.
116 """
117 oDb.execute('SELECT *\n'
118 'FROM TestBoxStatuses\n'
119 'WHERE idGenTestBox = %s\n'
120 , (idGenTestBox, ) );
121 return self.initFromDbRow(oDb.fetchOne());
122
123
124class TestBoxStatusLogic(ModelLogicBase):
125 """
126 TestBoxStatus logic.
127 """
128
129 ## The number of seconds between each time to call touchStatus() when
130 # returning CMD_IDLE.
131 kcSecIdleTouchStatus = 120;
132
133
134 def __init__(self, oDb):
135 ModelLogicBase.__init__(self, oDb);
136
137
138 def tryFetchStatus(self, idTestBox):
139 """
140 Attempts to fetch the status of the given testbox.
141
142 Returns a TestBoxStatusData object on success.
143 Returns None if no status was found.
144 Raises exception on other errors.
145 """
146 self._oDb.execute('SELECT *\n'
147 'FROM TestBoxStatuses\n'
148 'WHERE idTestBox = %s\n',
149 (idTestBox,));
150 if self._oDb.getRowCount() == 0:
151 return None;
152 oStatus = TestBoxStatusData();
153 return oStatus.initFromDbRow(self._oDb.fetchOne());
154
155 def tryFetchStatusAndConfig(self, idTestBox, sTestBoxUuid, sTestBoxAddr):
156 """
157 Tries to fetch the testbox status and current testbox config.
158
159 Returns (TestBoxStatusData, TestBoxData) on success, (None, None) if
160 not found. May throw an exception on database error.
161 """
162 self._oDb.execute('SELECT *\n'
163 'FROM TestBoxStatuses, TestBoxes\n'
164 'WHERE TestBoxStatuses.idTestBox = %s\n'
165 ' AND TestBoxes.idTestBox = %s\n'
166 ' AND TestBoxes.tsExpire = \'infinity\'::timestamp\n'
167 ' AND TestBoxes.uuidSystem = %s\n'
168 ' AND TestBoxes.ip = %s\n'
169 , (idTestBox,
170 idTestBox,
171 sTestBoxUuid,
172 sTestBoxAddr,
173 ));
174 cRows = self._oDb.getRowCount();
175 if cRows != 1:
176 if cRows != 0:
177 raise TMTooManyRows('tryFetchStatusForCommandReq got %s rows for idTestBox=%s' % (cRows, idTestBox));
178 return (None, None);
179 aoRow = self._oDb.fetchOne();
180 return (TestBoxStatusData().initFromDbRow(aoRow[0:5]), TestBoxData().initFromDbRow(aoRow[5:]));
181
182
183 def insertIdleStatus(self, idTestBox, idGenTestBox, fCommit = False):
184 """
185 Inserts an idle status for the specified testbox.
186 """
187 self._oDb.execute('INSERT INTO TestBoxStatuses (\n'
188 ' idTestBox,\n'
189 ' idGenTestBox,\n'
190 ' enmState,\n'
191 ' idTestSet)\n'
192 'VALUES ( %s,\n'
193 ' %s,\n'
194 ' \'idle\'::TestBoxState_T,\n'
195 ' NULL)\n',
196 (idTestBox, idGenTestBox) );
197 self._oDb.maybeCommit(fCommit);
198 return True;
199
200 def touchStatus(self, idTestBox, fCommit = False):
201 """
202 Touches the testbox status row, i.e. sets tsUpdated to the current time.
203 """
204 self._oDb.execute('UPDATE TestBoxStatuses\n'
205 'SET tsUpdated = CURRENT_TIMESTAMP\n'
206 'WHERE idTestBox = %s\n'
207 , (idTestBox,));
208 self._oDb.maybeCommit(fCommit);
209 return True;
210
211 def updateState(self, idTestBox, sNewState, idTestSet = None, fCommit = False):
212 """
213 Updates the testbox state.
214 """
215 self._oDb.execute('UPDATE TestBoxStatuses\n'
216 'SET enmState = %s,\n'
217 ' idTestSet = %s,\n'
218 ' tsUpdated = CURRENT_TIMESTAMP\n'
219 'WHERE idTestBox = %s\n',
220 (sNewState, idTestSet, idTestBox));
221 self._oDb.maybeCommit(fCommit);
222 return True;
223
224 def updateGangStatus(self, idTestSetGangLeader, sNewState, fCommit = False):
225 """
226 Update the state of all members of a gang.
227 """
228 self._oDb.execute('UPDATE TestBoxStatuses\n'
229 'SET enmState = %s,\n'
230 ' tsUpdated = CURRENT_TIMESTAMP\n'
231 'WHERE idTestBox IN (SELECT idTestBox\n'
232 ' FROM TestSets\n'
233 ' WHERE idTestSetGangLeader = %s)\n'
234 , (sNewState, idTestSetGangLeader,) );
235 self._oDb.maybeCommit(fCommit);
236 return True;
237
238 def isWholeGangDoneTesting(self, idTestSetGangLeader):
239 """
240 Checks if the whole gang is done testing.
241 """
242 self._oDb.execute('SELECT COUNT(*)\n'
243 'FROM TestBoxStatuses, TestSets\n'
244 'WHERE TestBoxStatuses.idTestSet = TestSets.idTestSet\n'
245 ' AND TestSets.idTestSetGangLeader = %s\n'
246 ' AND TestBoxStatuses.enmState IN (%s, %s)\n'
247 , ( idTestSetGangLeader,
248 TestBoxStatusData.ksTestBoxState_GangGathering,
249 TestBoxStatusData.ksTestBoxState_GangTesting));
250 return self._oDb.fetchOne()[0] == 0;
251
252 def isTheWholeGangThere(self, idTestSetGangLeader):
253 """
254 Checks if the whole gang is done testing.
255 """
256 self._oDb.execute('SELECT COUNT(*)\n'
257 'FROM TestBoxStatuses, TestSets\n'
258 'WHERE TestBoxStatuses.idTestSet = TestSets.idTestSet\n'
259 ' AND TestSets.idTestSetGangLeader = %s\n'
260 ' AND TestBoxStatuses.enmState IN (%s, %s)\n'
261 , ( idTestSetGangLeader,
262 TestBoxStatusData.ksTestBoxState_GangGathering,
263 TestBoxStatusData.ksTestBoxState_GangTesting));
264 return self._oDb.fetchOne()[0] == 0;
265
266 def timeSinceLastChangeInSecs(self, oStatusData):
267 """
268 Figures the time since the last status change.
269 """
270 tsNow = self._oDb.getCurrentTimestamp();
271 oDelta = tsNow - oStatusData.tsUpdated;
272 return oDelta.seconds + oDelta.days * 24 * 3600;
273
274
275#
276# Unit testing.
277#
278
279# pylint: disable=C0111
280class TestBoxStatusDataTestCase(ModelDataBaseTestCase):
281 def setUp(self):
282 self.aoSamples = [TestBoxStatusData(),];
283
284if __name__ == '__main__':
285 unittest.main();
286 # not reached.
287
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