CMS 3D CMS Logo

conddbCopyTest.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 from __future__ import print_function
4 import sqlite3
5 import subprocess
6 import json
7 import os
8 import shutil
9 import datetime
10 
11 fileName = 'copy_test.db'
12 
13 class DB:
14  def __init__(self):
15  pass
16 
17  def setSynchronizationType( self, tag, synchType ):
18  db = sqlite3.connect(fileName)
19  cursor = db.cursor()
20  cursor.execute('UPDATE TAG SET SYNCHRONIZATION =? WHERE NAME =?',(synchType,tag,))
21  db.commit()
22 
23  def getLastInsertedSince( self, tag, snapshot ):
24  db = sqlite3.connect(fileName)
25  cursor = db.cursor()
26  cursor.execute('SELECT SINCE, INSERTION_TIME FROM IOV WHERE TAG_NAME =? AND INSERTION_TIME >? ORDER BY INSERTION_TIME DESC',(tag,snapshot))
27  row = cursor.fetchone()
28  return row
29 
30 def prepareFile( inputTag, sourceTag, startingSince ):
31  command = "conddb --yes copy %s %s --destdb %s -f %s" %(inputTag,sourceTag,fileName,startingSince)
32  pipe = subprocess.Popen( command, shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
33  out = pipe.communicate()[0]
34 
35 def copy( sourceTag, destTag, since, logFileName ):
36  command = "conddb --yes --db %s copy %s %s -f %s --synchronize" %(fileName,sourceTag,destTag,since)
37  pipe = subprocess.Popen( command, shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
38  out = pipe.communicate()[0]
39  lines = out.split('\n')
40  ret = pipe.returncode
41  for line in lines:
42  print(line)
43  with open(logFileName,'a') as logFile:
44  logFile.write(out)
45  return ret==0
46 
47 class CopyTest:
48  def __init__(self, db):
49  self.db = db
50  self.errors = 0
51  self.logFileName = 'conddbCopyTest.log'
52 
53  def log( self, msg ):
54  print(msg)
55  with open(self.logFileName,'a') as logFile:
56  logFile.write(msg)
57  logFile.write('\n')
58 
59  def execute( self, sourceTag, baseFile, destTag, synchro, destSince, success, expectedAction ):
60  insertedSince = None
61  metaDestFile = '%s.txt' %destTag
62  #shutil.copyfile( baseFile, destFile )
63  self.log( '# ---------------------------------------------------------------------------')
64  self.log( '# Testing tag %s with synch=%s, destSince=%s - expecting ret=%s action=%s' %(destTag,synchro,destSince,success,expectedAction))
65 
66  descr = 'Testing conditionsUpload with synch:%s - expected action: %s' %(synchro,expectedAction)
67  beforeUpload = datetime.datetime.utcnow()
68  ret = copy( sourceTag, destTag, destSince, self.logFileName )
69  if ret != success:
70  self.log( 'ERROR: the return value for the copy of tag %s with sychro %s was %s, while the expected result is %s' %(destTag,synchro,ret,success))
71  self.errors += 1
72  else:
73  row = self.db.getLastInsertedSince( destTag, beforeUpload )
74  if ret == True:
75  if expectedAction == 'CREATE' or expectedAction == 'INSERT' or expectedAction == 'APPEND':
76  if destSince != row[0]:
77  self.log( 'ERROR: the since inserted is %s, expected value is %s - expected action: %s' %(row[0],destSince,expectedAction))
78  self.errors += 1
79  else:
80  self.log( '# OK: Found expected value for last since inserted: %s timestamp: %s' %(row[0],row[1]))
81  insertedSince = row[0]
82  elif expectedAction == 'SYNCHRONIZE':
83  if destSince == row[0]:
84  self.log( 'ERROR: the since inserted %s has not been synchronized with the FCSR - expected action: %s' %(row[0],expectedAction))
85  self.errors += 1
86  else:
87  self.log( '# OK: Found synchronized value for the last since inserted: %s timestamp: %s' %(row[0],row[1]))
88  insertedSince = row[0]
89  else:
90  self.log( 'ERROR: found an appended since %s - expected action: %s' %(row[0],expectedAction))
91  self.errors += 1
92  else:
93  if not row is None:
94  self.log( 'ERROR: found new insered since: %s timestamp: %s' %(row[0],row[1]))
95  self.errors += 1
96  if expectedAction != 'FAIL':
97  self.log( 'ERROR: Upload failed. Expected value: %s' %(destSince))
98  self.errors += 1
99  else:
100  self.log( '# OK: Upload failed as expected.')
101  return insertedSince
102 
103 
104 def main():
105  print('Testing...')
106  bfile0 = fileName
107  bfile1 = fileName
108  db = DB()
109  inputTag = 'runinfo_31X_mc'
110  inputTag0 ='runinfo_0'
111  inputTag1 = 'runinfo_1'
112  prepareFile( inputTag,inputTag0,1)
113  prepareFile( inputTag,inputTag1,100)
114  test = CopyTest( db )
115  # test with synch=any
116  tag = 'test_CondUpload_any'
117  test.execute( inputTag0, bfile0, tag, 'any', 1, True, 'CREATE' )
118  test.execute( inputTag0, bfile0, tag, 'any', 200, True, 'APPEND' )
119  test.execute( inputTag0, bfile0, tag, 'any', 100, True, 'INSERT')
120  test.execute( inputTag0, bfile0, tag, 'any', 200, True, 'INSERT')
121  # test with synch=validation
122  tag = 'test_CondUpload_validation'
123  test.execute( inputTag0, bfile0, tag, 'validation', 1, True, 'CREATE')
124  db.setSynchronizationType( tag, 'validation' )
125  test.execute( inputTag0, bfile0, tag, 'validation', 1, True, 'INSERT')
126  test.execute( inputTag0, bfile0, tag, 'validation', 200, True, 'APPEND')
127  test.execute( inputTag0, bfile0, tag, 'validation', 100, True, 'INSERT')
128  # test with synch=mc
129  tag = 'test_CondUpload_mc'
130  test.execute( inputTag0, bfile0, tag, 'mc', 1, True, 'CREATE')
131  db.setSynchronizationType( tag, 'mc' )
132  test.execute( inputTag1, bfile1, tag, 'mc', 1, False, 'FAIL')
133  test.execute( inputTag0, bfile0, tag, 'mc', 1, False, 'FAIL')
134  test.execute( inputTag0, bfile0, tag, 'mc', 200, False, 'FAIL')
135  # test with synch=hlt
136  tag = 'test_CondUpload_hlt'
137  test.execute( inputTag0, bfile0, tag, 'hlt', 1, True, 'CREATE')
138  db.setSynchronizationType( tag, 'hlt' )
139  test.execute( inputTag0, bfile0, tag, 'hlt', 200, True, 'SYNCHRONIZE')
140  fcsr = test.execute( inputTag0, bfile0, tag, 'hlt', 100, True, 'SYNCHRONIZE')
141  if not fcsr is None:
142  since = fcsr + 200
143  test.execute( inputTag0, bfile0, tag, 'hlt', since, True, 'APPEND')
144  since = fcsr + 100
145  test.execute( inputTag0, bfile0, tag, 'hlt', since, True, 'INSERT')
146  # test with synch=express
147  tag = 'test_CondUpload_express'
148  test.execute( inputTag0, bfile0, tag, 'express', 1, True, 'CREATE')
149  db.setSynchronizationType( tag, 'express' )
150  test.execute( inputTag0, bfile0, tag, 'express', 200, True, 'SYNCHRONIZE')
151  fcsr = test.execute( inputTag0, bfile0, tag, 'express', 100, True, 'SYNCHRONIZE')
152  if not fcsr is None:
153  since = fcsr + 200
154  test.execute( inputTag0, bfile0, tag, 'express', since, True, 'APPEND')
155  since = fcsr + 100
156  test.execute( inputTag0, bfile0, tag, 'express', since, True, 'INSERT')
157  # test with synch=prompt
158  tag = 'test_CondUpload_prompt'
159  test.execute( inputTag0, bfile0, tag, 'prompt', 1, True, 'CREATE')
160  db.setSynchronizationType( tag, 'prompt' )
161  test.execute( inputTag0, bfile0, tag, 'prompt', 200, True, 'SYNCHRONIZE')
162  fcsr = test.execute( inputTag0, bfile0, tag, 'prompt', 100, True, 'SYNCHRONIZE')
163  if not fcsr is None:
164  since = fcsr + 200
165  test.execute( inputTag0, bfile0, tag, 'prompt', since, True, 'APPEND')
166  since = fcsr + 100
167  test.execute( inputTag0, bfile0, tag, 'prompt', since, True, 'INSERT')
168  # test with synch=pcl
169  tag = 'test_CondUpload_pcl'
170  test.execute( inputTag0, bfile0, tag, 'pcl', 1, True, 'CREATE')
171  db.setSynchronizationType( tag, 'pcl' )
172  test.execute( inputTag0, bfile0, tag, 'pcl', 200, False, 'FAIL')
173  if not fcsr is None:
174  since = fcsr + 200
175  test.execute( inputTag0, bfile0, tag, 'pcl', since, True, 'APPEND')
176  since = fcsr + 100
177  test.execute( inputTag0, bfile0, tag, 'pcl', since, True, 'INSERT')
178  # test with synch=offline
179  tag = 'test_CondUpload_offline'
180  test.execute( inputTag0, bfile0, tag, 'offline', 1, True, 'CREATE')
181  db.setSynchronizationType( tag, 'offline' )
182  test.execute( inputTag0, bfile0, tag, 'offline', 1000, True, 'APPEND')
183  test.execute( inputTag0, bfile0, tag, 'offline', 500, False, 'FAIL' )
184  test.execute( inputTag0, bfile0, tag, 'offline', 1000, False, 'FAIL' )
185  test.execute( inputTag0, bfile0, tag, 'offline', 2000, True, 'APPEND' )
186  # test with synch=runmc
187  tag = 'test_CondUpload_runmc'
188  test.execute( inputTag0, bfile0, tag, 'runmc', 1, True, 'CREATE')
189  db.setSynchronizationType( tag, 'runmc' )
190  test.execute( inputTag0, bfile0, tag, 'runmc', 1000, True, 'APPEND')
191  test.execute( inputTag0, bfile0, tag, 'runmc', 500, False, 'FAIL' )
192  test.execute( inputTag0, bfile0, tag, 'runmc', 1000, False, 'FAIL' )
193  test.execute( inputTag0, bfile0, tag, 'runmc', 2000, True, 'APPEND' )
194  os.remove( fileName )
195  print('Done. Errors: %s' %test.errors)
196 
197 
198 if __name__ == '__main__':
199  main()
conddbCopyTest.CopyTest.log
def log(self, msg)
Definition: conddbCopyTest.py:53
conddbCopyTest.CopyTest
Definition: conddbCopyTest.py:47
conddbCopyTest.CopyTest.logFileName
logFileName
Definition: conddbCopyTest.py:51
conddbCopyTest.CopyTest.__init__
def __init__(self, db)
Definition: conddbCopyTest.py:48
conddbCopyTest.main
def main()
Definition: conddbCopyTest.py:104
conddbCopyTest.DB
Definition: conddbCopyTest.py:13
conddbCopyTest.copy
def copy(sourceTag, destTag, since, logFileName)
Definition: conddbCopyTest.py:35
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
conddbCopyTest.DB.getLastInsertedSince
def getLastInsertedSince(self, tag, snapshot)
Definition: conddbCopyTest.py:23
conddbCopyTest.CopyTest.db
db
Definition: conddbCopyTest.py:49
conddbCopyTest.CopyTest.execute
def execute(self, sourceTag, baseFile, destTag, synchro, destSince, success, expectedAction)
Definition: conddbCopyTest.py:59
main
Definition: main.py:1
conddbCopyTest.prepareFile
def prepareFile(inputTag, sourceTag, startingSince)
Definition: conddbCopyTest.py:30
conddbCopyTest.DB.setSynchronizationType
def setSynchronizationType(self, tag, synchType)
Definition: conddbCopyTest.py:17
conddbCopyTest.CopyTest.errors
errors
Definition: conddbCopyTest.py:50
conddbCopyTest.DB.__init__
def __init__(self)
Definition: conddbCopyTest.py:14