VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/testmanager/core/globalresource.py@ 62584

Last change on this file since 62584 was 62484, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.2 KB
Line 
1# -*- coding: utf-8 -*-
2# $Id: globalresource.py 62484 2016-07-22 18:35:33Z vboxsync $
3
4"""
5Test Manager - Global Resources.
6"""
7
8__copyright__ = \
9"""
10Copyright (C) 2012-2016 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: 62484 $"
30
31
32# Standard python imports.
33import unittest;
34
35# Validation Kit imports.
36from testmanager.core.base import ModelDataBase, ModelDataBaseTestCase, ModelLogicBase, TMRowNotFound;
37
38
39class GlobalResourceData(ModelDataBase):
40 """
41 Global resource data
42 """
43
44 ksIdAttr = 'idGlobalRsrc';
45
46 ksParam_idGlobalRsrc = 'GlobalResource_idGlobalRsrc'
47 ksParam_tsEffective = 'GlobalResource_tsEffective'
48 ksParam_tsExpire = 'GlobalResource_tsExpire'
49 ksParam_uidAuthor = 'GlobalResource_uidAuthor'
50 ksParam_sName = 'GlobalResource_sName'
51 ksParam_sDescription = 'GlobalResource_sDescription'
52 ksParam_fEnabled = 'GlobalResource_fEnabled'
53
54 kasAllowNullAttributes = ['idGlobalRsrc', 'tsEffective', 'tsExpire', 'uidAuthor', 'sDescription' ];
55 kcchMin_sName = 2;
56 kcchMax_sName = 64;
57
58 def __init__(self):
59 ModelDataBase.__init__(self);
60
61 #
62 # Initialize with defaults.
63 # See the database for explanations of each of these fields.
64 #
65 self.idGlobalRsrc = None;
66 self.tsEffective = None;
67 self.tsExpire = None;
68 self.uidAuthor = None;
69 self.sName = None;
70 self.sDescription = None;
71 self.fEnabled = False
72
73 def initFromDbRow(self, aoRow):
74 """
75 Reinitialize from a SELECT * FROM GlobalResources row.
76 Returns self. Raises exception if no row.
77 """
78 if aoRow is None:
79 raise TMRowNotFound('Global resource not found.')
80
81 self.idGlobalRsrc = aoRow[0]
82 self.tsEffective = aoRow[1]
83 self.tsExpire = aoRow[2]
84 self.uidAuthor = aoRow[3]
85 self.sName = aoRow[4]
86 self.sDescription = aoRow[5]
87 self.fEnabled = aoRow[6]
88 return self
89
90 def initFromDbWithId(self, oDb, idGlobalRsrc, tsNow = None, sPeriodBack = None):
91 """
92 Initialize the object from the database.
93 """
94 oDb.execute(self.formatSimpleNowAndPeriodQuery(oDb,
95 'SELECT *\n'
96 'FROM GlobalResources\n'
97 'WHERE idGlobalRsrc = %s\n'
98 , ( idGlobalRsrc,), tsNow, sPeriodBack));
99 aoRow = oDb.fetchOne()
100 if aoRow is None:
101 raise TMRowNotFound('idGlobalRsrc=%s not found (tsNow=%s sPeriodBack=%s)' % (idGlobalRsrc, tsNow, sPeriodBack,));
102 return self.initFromDbRow(aoRow);
103
104 def isEqual(self, oOther):
105 """
106 Compares two instances.
107 """
108 return self.idGlobalRsrc == oOther.idGlobalRsrc \
109 and str(self.tsEffective) == str(oOther.tsEffective) \
110 and str(self.tsExpire) == str(oOther.tsExpire) \
111 and self.uidAuthor == oOther.uidAuthor \
112 and self.sName == oOther.sName \
113 and self.sDescription == oOther.sDescription \
114 and self.fEnabled == oOther.fEnabled
115
116
117class GlobalResourceLogic(ModelLogicBase):
118 """
119 Global resource logic.
120 """
121
122 def fetchForListing(self, iStart, cMaxRows, tsNow):
123 """
124 Returns an array (list) of FailureReasonData items, empty list if none.
125 Raises exception on error.
126 """
127
128 if tsNow is None:
129 self._oDb.execute('SELECT *\n'
130 'FROM GlobalResources\n'
131 'WHERE tsExpire = \'infinity\'::TIMESTAMP\n'
132 'ORDER BY idGlobalRsrc DESC\n'
133 'LIMIT %s OFFSET %s\n'
134 , (cMaxRows, iStart,));
135 else:
136 self._oDb.execute('SELECT *\n'
137 'FROM GlobalResources\n'
138 'WHERE tsExpire > %s\n'
139 ' AND tsEffective <= %s\n'
140 'ORDER BY idGlobalRsrc DESC\n'
141 'LIMIT %s OFFSET %s\n'
142 , (tsNow, tsNow, cMaxRows, iStart,))
143
144 aoRows = []
145 for aoRow in self._oDb.fetchAll():
146 aoRows.append(GlobalResourceData().initFromDbRow(aoRow))
147 return aoRows
148
149 def getAll(self, tsEffective = None):
150 """
151 Gets all global resources.
152
153 Returns an array of GlobalResourceData instances on success (can be
154 empty). Raises exception on database error.
155 """
156 if tsEffective is not None:
157 self._oDb.execute('SELECT *\n'
158 'FROM GlobalResources\n'
159 'WHERE tsExpire > %s\n'
160 ' AND tsEffective <= %s\n'
161 , (tsEffective, tsEffective));
162 else:
163 self._oDb.execute('SELECT *\n'
164 'FROM GlobalResources\n'
165 'WHERE tsExpire = \'infinity\'::TIMESTAMP\n');
166 aaoRows = self._oDb.fetchAll();
167 aoRet = [];
168 for aoRow in aaoRows:
169 aoRet.append(GlobalResourceData().initFromDbRow(aoRow));
170
171 return aoRet;
172
173 def addGlobalResource(self, uidAuthor, oData):
174 """Add Global Resource DB record"""
175 self._oDb.execute('SELECT * FROM add_globalresource(%s, %s, %s, %s);',
176 (uidAuthor,
177 oData.sName,
178 oData.sDescription,
179 oData.fEnabled))
180 self._oDb.commit()
181 return True
182
183 def editGlobalResource(self, uidAuthor, idGlobalRsrc, oData):
184 """Modify Global Resource DB record"""
185 # Check if anything has been changed
186 oGlobalResourcesDataOld = self.getById(idGlobalRsrc)
187 if oGlobalResourcesDataOld.isEqual(oData):
188 # Nothing has been changed, do nothing
189 return True
190
191 self._oDb.execute('SELECT * FROM update_globalresource(%s, %s, %s, %s, %s);',
192 (uidAuthor,
193 idGlobalRsrc,
194 oData.sName,
195 oData.sDescription,
196 oData.fEnabled))
197 self._oDb.commit()
198 return True
199
200 def remove(self, uidAuthor, idGlobalRsrc):
201 """Delete Global Resource DB record"""
202 self._oDb.execute('SELECT * FROM del_globalresource(%s, %s);',
203 (uidAuthor, idGlobalRsrc))
204 self._oDb.commit()
205 return True
206
207 def getById(self, idGlobalRsrc):
208 """
209 Get global resource record by its id
210 """
211 self._oDb.execute('SELECT *\n'
212 'FROM GlobalResources\n'
213 'WHERE tsExpire = \'infinity\'::timestamp\n'
214 ' AND idGlobalRsrc=%s;', (idGlobalRsrc,))
215
216 aRows = self._oDb.fetchAll()
217 if len(aRows) not in (0, 1):
218 raise self._oDb.integrityException('Duplicate global resource entry with ID %u (current)' % (idGlobalRsrc,));
219 try:
220 return GlobalResourceData().initFromDbRow(aRows[0])
221 except IndexError:
222 raise TMRowNotFound('Global resource not found.')
223
224 def allocateResources(self, idTestBox, aoGlobalRsrcs, fCommit = False):
225 """
226 Allocates the given global resource.
227
228 Returns True of successfully allocated the resources, False if not.
229 May raise exception on DB error.
230 """
231 # Quit quickly if there is nothing to alloocate.
232 if len(aoGlobalRsrcs) == 0:
233 return True;
234
235 #
236 # Note! Someone else might have allocated the resources since the
237 # scheduler check that they were available. In such case we
238 # need too quietly rollback and return FALSE.
239 #
240 self._oDb.execute('SAVEPOINT allocateResources');
241
242 for oGlobalRsrc in aoGlobalRsrcs:
243 try:
244 self._oDb.execute('INSERT INTO GlobalResourceStatuses (idGlobalRsrc, idTestBox)\n'
245 'VALUES (%s, %s)', (oGlobalRsrc.idGlobalRsrc, idTestBox, ) );
246 except self._oDb.oXcptError:
247 self._oDb.execute('ROLLBACK TO SAVEPOINT allocateResources');
248 return False;
249
250 self._oDb.execute('RELEASE SAVEPOINT allocateResources');
251 self._oDb.maybeCommit(fCommit);
252 return True;
253
254 def freeGlobalResourcesByTestBox(self, idTestBox, fCommit = False):
255 """
256 Frees all global resources own by the given testbox.
257 Returns True. May raise exception on DB error.
258 """
259 self._oDb.execute('DELETE FROM GlobalResourceStatuses\n'
260 'WHERE idTestBox = %s\n', (idTestBox, ) );
261 self._oDb.maybeCommit(fCommit);
262 return True;
263
264#
265# Unit testing.
266#
267
268# pylint: disable=C0111
269class GlobalResourceDataTestCase(ModelDataBaseTestCase):
270 def setUp(self):
271 self.aoSamples = [GlobalResourceData(),];
272
273if __name__ == '__main__':
274 unittest.main();
275 # not reached.
276
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