CMS 3D CMS Logo

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