CMS 3D CMS Logo

conditionUploadTest.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import cx_Oracle
4 import subprocess
5 import json
6 import os
7 import shutil
8 import datetime
9 
10 # Requirement 1: a conddb key for the authentication with valid permission on writing on prep CMS_CONDITIONS account
11 # this could be dropped introducing a specific entry in the .netrc
12 # Requirement 2: an entry "Dropbox" in the .netrc for the authentication
13 
14 class DB:
15  def __init__(self, serviceName, schemaName ):
16  self.serviceName = serviceName
17  self.schemaName = schemaName
18  self.connStr = None
19 
20  def connect( self ):
21  command = "cmscond_authentication_manager -s %s --list_conn | grep '%s@%s'" %(self.serviceName,self.schemaName,self.serviceName)
22  pipe = subprocess.Popen( command, shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
23  out = pipe.communicate()[0]
24  srvconn = '%s@%s' %(self.schemaName,self.serviceName)
25  rowpwd = out.split(srvconn)[1].split(self.schemaName)[1]
26  pwd = ''
27  for c in rowpwd:
28  if c != ' ' and c != '\n':
29  pwd += c
30  self.connStr = '%s/%s@%s' %(self.schemaName,pwd,self.serviceName)
31 
32  def setSynchronizationType( self, tag, synchType ):
33  db = cx_Oracle.connect(self.connStr)
34  cursor = db.cursor()
35  db.begin()
36  cursor.execute('UPDATE TAG SET SYNCHRONIZATION =:SYNCH WHERE NAME =:NAME',(synchType,tag,))
37  db.commit()
38 
39  def getLastInsertedSince( self, tag, snapshot ):
40  db = cx_Oracle.connect(self.connStr)
41  cursor = db.cursor()
42  cursor.execute('SELECT SINCE, INSERTION_TIME FROM IOV WHERE TAG_NAME =:TAG_NAME AND INSERTION_TIME >:TIME ORDER BY INSERTION_TIME DESC',(tag,snapshot))
43  row = cursor.fetchone()
44  return row
45 
46  def removeTag( self, tag ):
47  db = cx_Oracle.connect(self.connStr)
48  cursor = db.cursor()
49  db.begin()
50  cursor.execute('DELETE FROM IOV WHERE TAG_NAME =:TAG_NAME',(tag,))
51  cursor.execute('DELETE FROM TAG_LOG WHERE TAG_NAME=:TAG_NAME',(tag,))
52  cursor.execute('DELETE FROM TAG WHERE NAME=:NAME',(tag,))
53  db.commit()
54 
55 def makeBaseFile( inputTag, startingSince ):
56  cwd = os.getcwd()
57  baseFile = '%s_%s.db' %(inputTag,startingSince)
58  baseFilePath = os.path.join(cwd,baseFile)
59  if os.path.exists( baseFile ):
60  os.remove( baseFile )
61  command = "conddb_import -c sqlite_file:%s -f oracle://cms_orcon_adg/CMS_CONDITIONS -i %s -t %s -b %s" %(baseFile,inputTag,inputTag,startingSince)
62  pipe = subprocess.Popen( command, shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
63  out = pipe.communicate()[0]
64  if not os.path.exists( baseFile ):
65  msg = 'ERROR: base file has not been created: %s' %out
66  raise Exception( msg )
67  return baseFile
68 
69 
70 def makeMetadataFile( inputTag, destTag, since, description ):
71  cwd = os.getcwd()
72  metadataFile = os.path.join(cwd,'%s.txt') %destTag
73  if os.path.exists( metadataFile ):
74  os.remove( metadataFile )
75  metadata = {}
76  metadata[ "destinationDatabase" ] = "oracle://cms_orcoff_prep/CMS_CONDITIONS"
77  tagList = {}
78  tagList[ destTag ] = { "dependencies": {}, "synchronizeTo": "any" }
79  metadata[ "destinationTags" ] = tagList
80  metadata[ "inputTag" ] = inputTag
81  metadata[ "since" ] = since
82  metadata[ "userText" ] = description
83  fileName = destTag+".txt"
84  with open( fileName, "w" ) as file:
85  file.write(json.dumps(metadata,file,indent=4,sort_keys=True))
86 
87 def uploadFile( fileName, logFileName ):
88  command = "uploadConditions.py %s" %fileName
89  pipe = subprocess.Popen( command, shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
90  out = pipe.communicate()[0]
91  lines = out.split('\n')
92  ret = False
93  for line in lines:
94  if line.startswith('upload ended with code:'):
95  returnCode = line.split('upload ended with code:')[1].strip()
96  if returnCode == '0':
97  ret = True
98  break
99  with open(logFileName,'a') as logFile:
100  logFile.write(out)
101  return ret
102 
104  def __init__(self, db):
105  self.db = db
106  self.errors = 0
107  self.logFileName = 'conditionUploadTest.log'
108 
109  def log( self, msg ):
110  print msg
111  with open(self.logFileName,'a') as logFile:
112  logFile.write(msg)
113  logFile.write('\n')
114 
115  def upload( self, inputTag, baseFile, destTag, synchro, destSince, success, expectedAction ):
116  insertedSince = None
117  destFile = '%s.db' %destTag
118  metaDestFile = '%s.txt' %destTag
119  shutil.copyfile( baseFile, destFile )
120  self.log( '# ---------------------------------------------------------------------------')
121  self.log( '# Testing tag %s with synch=%s, destSince=%s - expecting ret=%s action=%s' %(destTag,synchro,destSince,success,expectedAction))
122 
123  descr = 'Testing conditionsUpload with synch:%s - expected action: %s' %(synchro,expectedAction)
124  makeMetadataFile( inputTag, destTag, destSince, descr )
125  beforeUpload = datetime.datetime.utcnow()
126  ret = uploadFile( destFile, self.logFileName )
127  if ret != success:
128  self.log( 'ERROR: the return value for the upload of tag %s with sychro %s was %s, while the expected result is %s' %(destTag,synchro,ret,success))
129  self.errors += 1
130  else:
131  row = self.db.getLastInsertedSince( destTag, beforeUpload )
132  if ret == True:
133  if expectedAction == 'CREATE' or expectedAction == 'INSERT' or expectedAction == 'APPEND':
134  if destSince != row[0]:
135  self.log( 'ERROR: the since inserted is %s, expected value is %s - expected action: %s' %(row[0],destSince,expectedAction))
136  self.errors += 1
137  else:
138  self.log( '# OK: Found expected value for last since inserted: %s timestamp: %s' %(row[0],row[1]))
139  insertedSince = row[0]
140  elif expectedAction == 'SYNCHRONIZE':
141  if destSince == row[0]:
142  self.log( 'ERROR: the since inserted %s has not been synchronized with the FCSR - expected action: %s' %(row[0],expectedAction))
143  self.errors += 1
144  else:
145  self.log( '# OK: Found synchronized value for the last since inserted: %s timestamp: %s' %(row[0],row[1]))
146  insertedSince = row[0]
147  else:
148  self.log( 'ERROR: found an appended since %s - expected action: %s' %(row[0],expectedAction))
149  self.errors += 1
150  else:
151  if not row is None:
152  self.log( 'ERROR: found new insered since: %s timestamp: %s' %(row[0],row[1]))
153  self.errors += 1
154  if expectedAction != 'FAIL':
155  self.log( 'ERROR: Upload failed. Expected value: %s' %(destSince))
156  self.errors += 1
157  else:
158  self.log( '# OK: Upload failed as expected.')
159  os.remove( destFile )
160  os.remove( metaDestFile )
161  return insertedSince
162 
163 
164 def main():
165  print 'Testing...'
166  serviceName = 'cms_orcoff_prep'
167  schemaName = 'CMS_CONDITIONS'
168  db = DB(serviceName,schemaName)
169  db.connect()
170  inputTag = 'runinfo_31X_mc'
171  bfile0 = makeBaseFile( inputTag,1)
172  bfile1 = makeBaseFile( inputTag,100)
173  test = UploadTest( db )
174  # test with synch=any
175  tag = 'test_CondUpload_any'
176  test.upload( inputTag, bfile0, tag, 'any', 1, True, 'CREATE' )
177  test.upload( inputTag, bfile1, tag, 'any', 1, False, 'FAIL' )
178  test.upload( inputTag, bfile0, tag, 'any', 200, True, 'APPEND' )
179  test.upload( inputTag, bfile0, tag, 'any', 100, True, 'INSERT')
180  test.upload( inputTag, bfile0, tag, 'any', 200, True, 'INSERT')
181  db.removeTag( tag )
182  # test with synch=validation
183  tag = 'test_CondUpload_validation'
184  test.upload( inputTag, bfile0, tag, 'validation', 1, True, 'CREATE')
185  db.setSynchronizationType( tag, 'validation' )
186  test.upload( inputTag, bfile0, tag, 'validation', 1, True, 'INSERT')
187  test.upload( inputTag, bfile0, tag, 'validation', 200, True, 'APPEND')
188  test.upload( inputTag, bfile0, tag, 'validation', 100, True, 'INSERT')
189  db.removeTag( tag )
190  # test with synch=mc
191  tag = 'test_CondUpload_mc'
192  test.upload( inputTag, bfile1, tag, 'mc', 1, False, 'FAIL')
193  test.upload( inputTag, bfile0, tag, 'mc', 1, True, 'CREATE')
194  db.setSynchronizationType( tag, 'mc' )
195  test.upload( inputTag, bfile0, tag, 'mc', 1, False, 'FAIL')
196  test.upload( inputTag, bfile0, tag, 'mc', 200, False, 'FAIL')
197  db.removeTag( tag )
198  # test with synch=hlt
199  tag = 'test_CondUpload_hlt'
200  test.upload( inputTag, bfile0, tag, 'hlt', 1, True, 'CREATE')
201  db.setSynchronizationType( tag, 'hlt' )
202  test.upload( inputTag, bfile0, tag, 'hlt', 200, True, 'SYNCHRONIZE')
203  fcsr = test.upload( inputTag, bfile0, tag, 'hlt', 100, True, 'SYNCHRONIZE')
204  if not fcsr is None:
205  since = fcsr + 200
206  test.upload( inputTag, bfile0, tag, 'hlt', since, True, 'APPEND')
207  since = fcsr + 100
208  test.upload( inputTag, bfile0, tag, 'hlt', since, True, 'INSERT')
209  db.removeTag( tag )
210  # test with synch=express
211  tag = 'test_CondUpload_express'
212  test.upload( inputTag, bfile0, tag, 'express', 1, True, 'CREATE')
213  db.setSynchronizationType( tag, 'express' )
214  test.upload( inputTag, bfile0, tag, 'express', 200, True, 'SYNCHRONIZE')
215  fcsr = test.upload( inputTag, bfile0, tag, 'express', 100, True, 'SYNCHRONIZE')
216  if not fcsr is None:
217  since = fcsr + 200
218  test.upload( inputTag, bfile0, tag, 'express', since, True, 'APPEND')
219  since = fcsr + 100
220  test.upload( inputTag, bfile0, tag, 'express', since, True, 'INSERT')
221  db.removeTag( tag )
222  # test with synch=prompt
223  tag = 'test_CondUpload_prompt'
224  test.upload( inputTag, bfile0, tag, 'prompt', 1, True, 'CREATE')
225  db.setSynchronizationType( tag, 'prompt' )
226  test.upload( inputTag, bfile0, tag, 'prompt', 200, True, 'SYNCHRONIZE')
227  fcsr = test.upload( inputTag, bfile0, tag, 'prompt', 100, True, 'SYNCHRONIZE')
228  if not fcsr is None:
229  since = fcsr + 200
230  test.upload( inputTag, bfile0, tag, 'prompt', since, True, 'APPEND')
231  since = fcsr + 100
232  test.upload( inputTag, bfile0, tag, 'prompt', since, True, 'INSERT')
233  db.removeTag( tag )
234  # test with synch=pcl
235  tag = 'test_CondUpload_pcl'
236  test.upload( inputTag, bfile0, tag, 'pcl', 1, True, 'CREATE')
237  db.setSynchronizationType( tag, 'pcl' )
238  test.upload( inputTag, bfile0, tag, 'pcl', 200, False, 'FAIL')
239  if not fcsr is None:
240  since = fcsr + 200
241  test.upload( inputTag, bfile0, tag, 'pcl', since, True, 'APPEND')
242  since = fcsr + 100
243  test.upload( inputTag, bfile0, tag, 'pcl', since, True, 'INSERT')
244  db.removeTag( tag )
245  # test with synch=offline
246  tag = 'test_CondUpload_offline'
247  test.upload( inputTag, bfile0, tag, 'offline', 1, True, 'CREATE')
248  db.setSynchronizationType( tag, 'offline' )
249  test.upload( inputTag, bfile0, tag, 'offline', 1000, True, 'APPEND')
250  test.upload( inputTag, bfile0, tag, 'offline', 500, False, 'FAIL' )
251  test.upload( inputTag, bfile0, tag, 'offline', 1000, False, 'FAIL' )
252  test.upload( inputTag, bfile0, tag, 'offline', 2000, True, 'APPEND' )
253  db.removeTag( tag )
254  # test with synch=runmc
255  tag = 'test_CondUpload_runmc'
256  test.upload( inputTag, bfile0, tag, 'runmc', 1, True, 'CREATE')
257  db.setSynchronizationType( tag, 'runmc' )
258  test.upload( inputTag, bfile0, tag, 'runmc', 1000, True, 'APPEND')
259  test.upload( inputTag, bfile0, tag, 'runmc', 500, False, 'FAIL' )
260  test.upload( inputTag, bfile0, tag, 'runmc', 1000, False, 'FAIL' )
261  test.upload( inputTag, bfile0, tag, 'runmc', 2000, True, 'APPEND' )
262  db.removeTag( tag )
263  os.remove( bfile0 )
264  os.remove( bfile1 )
265  print 'Done. Errors: %s' %test.errors
266 
267 if __name__ == '__main__':
268  main()
def upload(self, inputTag, baseFile, destTag, synchro, destSince, success, expectedAction)
def setSynchronizationType(self, tag, synchType)
def uploadFile(fileName, logFileName)
def makeMetadataFile(inputTag, destTag, since, description)
def __init__(self, serviceName, schemaName)
def makeBaseFile(inputTag, startingSince)
def getLastInsertedSince(self, tag, snapshot)
Definition: main.py:1
double split
Definition: MVATrainer.cc:139