1 | # -*- coding: utf-8 -*-
|
---|
2 | # $Id: systemlog.py 69111 2017-10-17 14:26:02Z vboxsync $
|
---|
3 |
|
---|
4 | """
|
---|
5 | Test Manager - SystemLog.
|
---|
6 | """
|
---|
7 |
|
---|
8 | __copyright__ = \
|
---|
9 | """
|
---|
10 | Copyright (C) 2012-2017 Oracle Corporation
|
---|
11 |
|
---|
12 | This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
13 | available from http://www.virtualbox.org. This file is free software;
|
---|
14 | you can redistribute it and/or modify it under the terms of the GNU
|
---|
15 | General Public License (GPL) as published by the Free Software
|
---|
16 | Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
17 | VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
18 | hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
19 |
|
---|
20 | The contents of this file may alternatively be used under the terms
|
---|
21 | of the Common Development and Distribution License Version 1.0
|
---|
22 | (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
23 | VirtualBox OSE distribution, in which case the provisions of the
|
---|
24 | CDDL are applicable instead of those of the GPL.
|
---|
25 |
|
---|
26 | You may elect to license modified versions of this file under the
|
---|
27 | terms and conditions of either the GPL or the CDDL or both.
|
---|
28 | """
|
---|
29 | __version__ = "$Revision: 69111 $"
|
---|
30 |
|
---|
31 |
|
---|
32 | # Standard python imports.
|
---|
33 | import unittest;
|
---|
34 |
|
---|
35 | # Validation Kit imports.
|
---|
36 | from testmanager.core.base import ModelDataBase, ModelDataBaseTestCase, ModelLogicBase, TMExceptionBase;
|
---|
37 |
|
---|
38 |
|
---|
39 | class SystemLogData(ModelDataBase): # pylint: disable=R0902
|
---|
40 | """
|
---|
41 | SystemLog Data.
|
---|
42 | """
|
---|
43 |
|
---|
44 | ## @name Event Constants
|
---|
45 | # @{
|
---|
46 | ksEvent_CmdNacked = 'CmdNack ';
|
---|
47 | ksEvent_TestBoxUnknown = 'TBoxUnkn';
|
---|
48 | ksEvent_TestSetAbandoned = 'TSetAbdd';
|
---|
49 | ksEvent_UserAccountUnknown = 'TAccUnkn';
|
---|
50 | ksEvent_XmlResultMalformed = 'XmlRMalf';
|
---|
51 | ksEvent_SchedQueueRecreate = 'SchQRecr';
|
---|
52 | ## @}
|
---|
53 |
|
---|
54 | ## Valid event types.
|
---|
55 | kasEvents = \
|
---|
56 | [ \
|
---|
57 | ksEvent_CmdNacked,
|
---|
58 | ksEvent_TestBoxUnknown,
|
---|
59 | ksEvent_TestSetAbandoned,
|
---|
60 | ksEvent_UserAccountUnknown,
|
---|
61 | ksEvent_XmlResultMalformed,
|
---|
62 | ksEvent_SchedQueueRecreate,
|
---|
63 | ];
|
---|
64 |
|
---|
65 | ksParam_tsCreated = 'tsCreated';
|
---|
66 | ksParam_sEvent = 'sEvent';
|
---|
67 | ksParam_sLogText = 'sLogText';
|
---|
68 |
|
---|
69 | kasValidValues_sEvent = kasEvents;
|
---|
70 |
|
---|
71 | def __init__(self):
|
---|
72 | ModelDataBase.__init__(self);
|
---|
73 |
|
---|
74 | #
|
---|
75 | # Initialize with defaults.
|
---|
76 | # See the database for explanations of each of these fields.
|
---|
77 | #
|
---|
78 | self.tsCreated = None;
|
---|
79 | self.sEvent = None;
|
---|
80 | self.sLogText = None;
|
---|
81 |
|
---|
82 | def initFromDbRow(self, aoRow):
|
---|
83 | """
|
---|
84 | Internal worker for initFromDbWithId and initFromDbWithGenId as well as
|
---|
85 | SystemLogLogic.
|
---|
86 | """
|
---|
87 |
|
---|
88 | if aoRow is None:
|
---|
89 | raise TMExceptionBase('SystemLog row not found.');
|
---|
90 |
|
---|
91 | self.tsCreated = aoRow[0];
|
---|
92 | self.sEvent = aoRow[1];
|
---|
93 | self.sLogText = aoRow[2];
|
---|
94 | return self;
|
---|
95 |
|
---|
96 |
|
---|
97 | class SystemLogLogic(ModelLogicBase):
|
---|
98 | """
|
---|
99 | SystemLog logic.
|
---|
100 | """
|
---|
101 |
|
---|
102 | def __init__(self, oDb):
|
---|
103 | ModelLogicBase.__init__(self, oDb);
|
---|
104 |
|
---|
105 | def fetchForListing(self, iStart, cMaxRows, tsNow, aiSortColumns = None):
|
---|
106 | """
|
---|
107 | Fetches SystemLog entries.
|
---|
108 |
|
---|
109 | Returns an array (list) of SystemLogData items, empty list if none.
|
---|
110 | Raises exception on error.
|
---|
111 | """
|
---|
112 | _ = aiSortColumns;
|
---|
113 | if tsNow is None:
|
---|
114 | self._oDb.execute('SELECT *\n'
|
---|
115 | 'FROM SystemLog\n'
|
---|
116 | 'ORDER BY tsCreated DESC\n'
|
---|
117 | 'LIMIT %s OFFSET %s\n',
|
---|
118 | (cMaxRows, iStart));
|
---|
119 | else:
|
---|
120 | self._oDb.execute('SELECT *\n'
|
---|
121 | 'FROM SystemLog\n'
|
---|
122 | 'WHERE tsCreated <= %s\n'
|
---|
123 | 'ORDER BY tsCreated DESC\n'
|
---|
124 | 'LIMIT %s OFFSET %s\n',
|
---|
125 | (tsNow, cMaxRows, iStart));
|
---|
126 | aoRows = [];
|
---|
127 | for _ in range(self._oDb.getRowCount()):
|
---|
128 | oData = SystemLogData();
|
---|
129 | oData.initFromDbRow(self._oDb.fetchOne());
|
---|
130 | aoRows.append(oData);
|
---|
131 | return aoRows;
|
---|
132 |
|
---|
133 | def addEntry(self, sEvent, sLogText, cHoursRepeat = 0, fCommit = False):
|
---|
134 | """
|
---|
135 | Adds an entry to the SystemLog table.
|
---|
136 | Raises exception on problem.
|
---|
137 | """
|
---|
138 | if sEvent not in SystemLogData.kasEvents:
|
---|
139 | raise TMExceptionBase('Unknown event type "%s"' % (sEvent,));
|
---|
140 |
|
---|
141 | # Check the repeat restriction first.
|
---|
142 | if cHoursRepeat > 0:
|
---|
143 | self._oDb.execute('SELECT COUNT(*) as Stuff\n'
|
---|
144 | 'FROM SystemLog\n'
|
---|
145 | 'WHERE tsCreated >= (current_timestamp - interval \'%s hours\')\n'
|
---|
146 | ' AND sEvent = %s\n'
|
---|
147 | ' AND sLogText = %s\n',
|
---|
148 | (cHoursRepeat,
|
---|
149 | sEvent,
|
---|
150 | sLogText));
|
---|
151 | aRow = self._oDb.fetchOne();
|
---|
152 | if aRow[0] > 0:
|
---|
153 | return None;
|
---|
154 |
|
---|
155 | # Insert it.
|
---|
156 | self._oDb.execute('INSERT INTO SystemLog (sEvent, sLogText)\n'
|
---|
157 | 'VALUES (%s, %s)\n',
|
---|
158 | (sEvent, sLogText));
|
---|
159 |
|
---|
160 | if fCommit:
|
---|
161 | self._oDb.commit();
|
---|
162 | return True;
|
---|
163 |
|
---|
164 | #
|
---|
165 | # Unit testing.
|
---|
166 | #
|
---|
167 |
|
---|
168 | # pylint: disable=C0111
|
---|
169 | class SystemLogDataTestCase(ModelDataBaseTestCase):
|
---|
170 | def setUp(self):
|
---|
171 | self.aoSamples = [SystemLogData(),];
|
---|
172 |
|
---|
173 | if __name__ == '__main__':
|
---|
174 | unittest.main();
|
---|
175 | # not reached.
|
---|
176 |
|
---|