-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathTestInit.py
309 lines (257 loc) · 10.4 KB
/
TestInit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env python
"""
_TestInit__
A set of initialization steps used in many tests.
Test can call the methods from this class to
initialize their default environment so to
minimize code duplication.
This class is not a test but an auxilary class and
is based on the WMCore.WMInit class.
"""
from __future__ import print_function
import logging
import os
import shutil
import tempfile
import threading
import traceback
from _mysql_exceptions import OperationalError
from WMCore.Agent.Configuration import Configuration
from WMCore.Agent.Configuration import loadConfigurationFile
from WMCore.WMException import WMException
hasDatabase = True
try:
from WMCore.WMInit import WMInit
except ImportError as ex:
print(str(ex))
print("NOTE: TestInit is being loaded without database support")
hasDatabase = False
# Sorry for the global, but I think this should go here
trashDatabases = False # delete databases after every test?
class TestInitException(WMException):
"""
_TestInitException_
You should see this only if something is wrong setting up the database connection
Still, just in case...
"""
def deleteDatabaseAfterEveryTest(areYouSerious):
"""
this method handles whether or not TestInit will be vicious to databases
"""
# python is idiotic for its scoping system
global trashDatabases
if areYouSerious == "I'm Serious":
print("We are going to trash databases after every test")
trashDatabases = True
else:
#"I'm glad you weren't serious"
print("We are not going to trash databases after every test")
trashDatabases = False
class TestInit(object):
"""
A set of initialization steps used in many tests.
Test can call the methods from this class to
initialize their default environment so to
minimize code duplication.
"""
def __init__(self, testClassName = "Unknown Class"):
self.testClassName = testClassName
self.testDir = None
self.currModules = []
global hasDatabase
self.hasDatabase = hasDatabase
if self.hasDatabase:
self.init = WMInit()
self.deleteTmp = True
def __del__(self):
if self.deleteTmp:
self.delWorkDir()
self.attemptToCloseDBConnections()
def delWorkDir(self):
if self.testDir != None:
try:
shutil.rmtree( self.testDir )
except:
# meh, if it fails, I guess something weird happened
pass
def setLogging(self, logLevel = logging.INFO):
"""
Sets logging parameters
"""
# remove old logging instances.
return
logger1 = logging.getLogger()
logger2 = logging.getLogger(self.testClassName)
for logger in [logger1, logger2]:
for handler in logger.handlers:
handler.close()
logger.removeHandler(handler)
self.init.setLogging(self.testClassName, self.testClassName,
logExists = False, logLevel = logLevel)
def generateWorkDir(self, config = None, deleteOnDestruction = True, setTmpDir = False):
self.testDir = tempfile.mkdtemp()
if config:
config.section_("General")
config.General.workDir = self.testDir
os.environ['TESTDIR'] = self.testDir
if os.getenv('WMCORE_KEEP_DIRECTORIES', False):
deleteOnDestruction = True
logging.info("Generated testDir - %s" % self.testDir)
if setTmpDir:
os.environ['TMPDIR'] = self.testDir
self.deleteTmp = deleteOnDestruction
return self.testDir
def getBackendFromDbURL(self, dburl):
dialectPart = dburl.split(":")[0]
if dialectPart == 'mysql':
return 'MySQL'
elif dialectPart == 'oracle':
return 'Oracle'
elif dialectPart == 'http':
return 'CouchDB'
else:
raise RuntimeError("Unrecognized dialect %s" % dialectPart)
def setDatabaseConnection(self, connectUrl=None, socket=None, destroyAllDatabase = False):
"""
Set up the database connection by retrieving the environment
parameters.
The destroyAllDatabase option is for testing ONLY. Never flip that switch
on in any other instance where you don't know what you're doing.
"""
if not self.hasDatabase:
return
config = self.getConfiguration(connectUrl=connectUrl, socket=socket)
self.coreConfig = config
self.init.setDatabaseConnection(config.CoreDatabase.connectUrl,
config.CoreDatabase.dialect,
config.CoreDatabase.socket)
if trashDatabases or destroyAllDatabase:
self.clearDatabase()
# Have to check whether or not database is empty
# If the database is not empty when we go to set the schema, abort!
try:
result = self.init.checkDatabaseContents()
except OperationalError:
logging.debug("Error checking DB contents, assume DB does not exist")
return
if len(result) > 0:
msg = "Database not empty, cannot set schema !\n"
msg += str(result)
logging.error(msg)
raise TestInitException(msg)
return
def setSchema(self, customModules = [], useDefault = True, params = None):
"""
Creates the schema in the database for the default
tables/services: trigger, message service, threadpool.
Developers can add their own modules to it using the array
customModules which should follow the proper naming convention.
if useDefault is set to False, it will not instantiate the
schemas in the defaultModules array.
"""
if not self.hasDatabase:
return
defaultModules = ["WMCore.WMBS"]
if not useDefault:
defaultModules = []
# filter for unique modules
modules = []
for module in (defaultModules + customModules):
if module not in modules:
modules.append(module)
try:
self.init.setSchema(modules, params = params)
except Exception as ex:
print(traceback.format_exc())
raise ex
# store the list of modules we've added to the DB
# again filter for unique modules
modules = []
for module in (defaultModules + customModules + self.currModules):
if module not in modules:
modules.append(module)
self.currModules = modules
return
def getDBInterface(self):
"shouldbe called after connection is made"
if not self.hasDatabase:
return
myThread = threading.currentThread()
return myThread.dbi
def getConfiguration(self, configurationFile = None, connectUrl = None, socket=None):
"""
Loads (if available) your configuration file and augments
it with the standard settings used in multiple tests.
"""
if configurationFile != None:
config = loadConfigurationFile(configurationFile)
else:
config = Configuration()
# some general settings that would come from the general default
# config file
config.Agent.contact = "[email protected]"
config.Agent.teamName = "Lakers"
config.Agent.agentName = "Lebron James"
config.Agent.hostName = "testhost.laker.world"
config.section_("General")
# If you need a testDir, call testInit.generateWorkDir
# config.General.workDir = os.getenv("TESTDIR")
config.General.ReqMgr2ServiceURL = "http://localhost/reqmgr2"
config.section_("CoreDatabase")
if connectUrl:
config.CoreDatabase.connectUrl = connectUrl
config.CoreDatabase.dialect = self.getBackendFromDbURL(connectUrl)
config.CoreDatabase.socket = socket or os.getenv("DBSOCK")
else:
if os.getenv('DATABASE') == None:
raise RuntimeError("You must set the DATABASE environment variable to run tests")
config.CoreDatabase.connectUrl = os.getenv("DATABASE")
config.CoreDatabase.dialect = self.getBackendFromDbURL(os.getenv("DATABASE"))
config.CoreDatabase.socket = os.getenv("DBSOCK")
if os.getenv("DBHOST"):
print("****WARNING: the DBHOST environment variable will be deprecated soon***")
print("****WARNING: UPDATE YOUR ENVIRONMENT OR TESTS WILL FAIL****")
# after this you can augment it with whatever you need.
couchurl = os.getenv("COUCHURL")
config.section_("ACDC")
config.ACDC.couchurl = couchurl
config.ACDC.database = "wmagent_acdc_t"
config.component_("JobStateMachine")
config.JobStateMachine.couchurl = couchurl
config.JobStateMachine.couchDBName = "wmagent_job_test"
config.JobStateMachine.jobSummaryDBName = "job_summary"
config.JobStateMachine.summaryStatsDBName = "stat_summary_test"
config.component_("JobAccountant")
config.JobAccountant.pollInterval = 60
config.JobAccountant.componentDir = os.getcwd()
config.JobAccountant.logLevel = 'SQLDEBUG'
config.component_("TaskArchiver")
config.TaskArchiver.localWMStatsURL = "%s/%s" % (config.JobStateMachine.couchurl, config.JobStateMachine.jobSummaryDBName)
return config
def clearDatabase(self, modules = []):
"""
Database deletion. Global, ignore modules.
"""
if not self.hasDatabase:
return
self.init.clearDatabase()
return
def attemptToCloseDBConnections(self):
return
myThread = threading.currentThread()
print("Closing DB")
try:
if not myThread.transaction \
and not myThread.transaction.conn \
and not myThread.transaction.conn.closed:
myThread.transaction.conn.close()
myThread.transaction.conn = None
print("Connection Closed")
except Exception as e:
print("tried to close DBI but failed: %s" % e)
try:
if hasattr(myThread, "dbFactory"):
del myThread.dbFactory
print("dbFactory removed")
except Exception as e:
print("tried to delete factory but failed %s" % e)