Line Code
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
#!/usr/bin/env python3

import sqlite3
import subprocess
import json
import os
import shutil
import datetime

fileName = 'copy_test.db'

class DB:
    def __init__(self):
        pass

    def setSynchronizationType( self, tag, synchType ):
        db = sqlite3.connect(fileName)
        cursor = db.cursor()
        cursor.execute('UPDATE TAG SET SYNCHRONIZATION =? WHERE NAME =?',(synchType,tag,))
        db.commit()

    def getLastInsertedSince( self, tag, snapshot ):
        db = sqlite3.connect(fileName)
        cursor = db.cursor()
        cursor.execute('SELECT SINCE, INSERTION_TIME FROM IOV WHERE TAG_NAME =? AND INSERTION_TIME >? ORDER BY INSERTION_TIME DESC',(tag,snapshot))
        row = cursor.fetchone()
        return row

def prepareFile( inputTag, sourceTag, startingSince ):
    command = "conddb --yes copy %s %s --destdb %s -f %s" %(inputTag,sourceTag,fileName,startingSince)
    pipe = subprocess.Popen( command, shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
    out = pipe.communicate()[0]

def copy( sourceTag, destTag, since, logFileName ):
    command = "conddb --yes --db %s copy %s %s -f %s --synchronize" %(fileName,sourceTag,destTag,since)
    pipe = subprocess.Popen( command, shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
    out = pipe.communicate()[0]
    lines = out.split('\n')
    ret = pipe.returncode
    for line in lines:
        print(line)
    with open(logFileName,'a') as logFile:
        logFile.write(out)
    return ret==0

class CopyTest:
    def __init__(self, db):
        self.db = db
        self.errors = 0
        self.logFileName = 'conddbCopyTest.log'

    def log( self, msg ):
        print(msg)
        with open(self.logFileName,'a') as logFile:
            logFile.write(msg)
            logFile.write('\n')

    def execute( self, sourceTag, baseFile, destTag, synchro, destSince, success, expectedAction ):
        insertedSince = None
        metaDestFile = '%s.txt' %destTag
        #shutil.copyfile( baseFile, destFile )
        self.log( '# ---------------------------------------------------------------------------')
        self.log( '# Testing tag %s with synch=%s, destSince=%s - expecting ret=%s action=%s' %(destTag,synchro,destSince,success,expectedAction))
    
        descr = 'Testing conditionsUpload with synch:%s - expected action: %s' %(synchro,expectedAction)
        beforeUpload = datetime.datetime.utcnow()
        ret = copy( sourceTag, destTag, destSince, self.logFileName )
        if ret != success:
            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))
            self.errors += 1
        else:
            row = self.db.getLastInsertedSince( destTag, beforeUpload )
            if ret == True:
                if expectedAction == 'CREATE' or expectedAction == 'INSERT' or expectedAction == 'APPEND':
                    if destSince != row[0]:
                        self.log( 'ERROR: the since inserted is %s, expected value is %s - expected action: %s' %(row[0],destSince,expectedAction))
                        self.errors += 1
                    else:
                        self.log( '# OK: Found expected value for last since inserted: %s timestamp: %s' %(row[0],row[1]))
                        insertedSince = row[0]
                elif expectedAction == 'SYNCHRONIZE':
                    if destSince == row[0]:
                        self.log( 'ERROR: the since inserted %s has not been synchronized with the FCSR - expected action: %s' %(row[0],expectedAction))
                        self.errors += 1
                    else:
                        self.log( '# OK: Found synchronized value for the last since inserted: %s timestamp: %s' %(row[0],row[1]))
                        insertedSince = row[0]
                else:
                    self.log( 'ERROR: found an appended since %s - expected action: %s' %(row[0],expectedAction))
                    self.errors += 1
            else:
                if not row is None:
                    self.log( 'ERROR: found new insered since: %s timestamp: %s' %(row[0],row[1]))
                    self.errors += 1
                if expectedAction != 'FAIL':
                    self.log( 'ERROR: Upload failed. Expected value: %s' %(destSince))
                    self.errors += 1
                else:
                    self.log( '# OK: Upload failed as expected.')
        return insertedSince


def main():
    print('Testing...')
    bfile0 = fileName
    bfile1 = fileName
    db = DB()
    inputTag = 'runinfo_31X_mc'
    inputTag0  ='runinfo_0'
    inputTag1 = 'runinfo_1'
    prepareFile( inputTag,inputTag0,1)
    prepareFile( inputTag,inputTag1,100)
    test = CopyTest( db )
    # test with synch=any
    tag = 'test_CondUpload_any'
    test.execute( inputTag0, bfile0, tag, 'any', 1, True, 'CREATE' )
    test.execute( inputTag0, bfile0, tag, 'any', 200, True, 'APPEND' )  
    test.execute( inputTag0, bfile0, tag, 'any', 100, True, 'INSERT')  
    test.execute( inputTag0, bfile0, tag, 'any', 200, True, 'INSERT')  
    # test with synch=validation
    tag = 'test_CondUpload_validation'
    test.execute( inputTag0, bfile0, tag, 'validation', 1, True, 'CREATE')  
    db.setSynchronizationType( tag, 'validation' ) 
    test.execute( inputTag0, bfile0, tag, 'validation', 1, True, 'INSERT')  
    test.execute( inputTag0, bfile0, tag, 'validation', 200, True, 'APPEND')  
    test.execute( inputTag0, bfile0, tag, 'validation', 100, True, 'INSERT')  
    # test with synch=mc
    tag = 'test_CondUpload_mc'
    test.execute( inputTag0, bfile0, tag, 'mc', 1, True, 'CREATE')  
    db.setSynchronizationType( tag, 'mc' ) 
    test.execute( inputTag1, bfile1, tag, 'mc', 1, False, 'FAIL')  
    test.execute( inputTag0, bfile0, tag, 'mc', 1, False, 'FAIL')  
    test.execute( inputTag0, bfile0, tag, 'mc', 200, False, 'FAIL') 
    # test with synch=hlt
    tag = 'test_CondUpload_hlt'
    test.execute( inputTag0, bfile0, tag, 'hlt', 1, True, 'CREATE')  
    db.setSynchronizationType( tag, 'hlt' ) 
    test.execute( inputTag0, bfile0, tag, 'hlt', 200, True, 'SYNCHRONIZE')  
    fcsr = test.execute( inputTag0, bfile0, tag, 'hlt', 100, True, 'SYNCHRONIZE')  
    if not fcsr is None:
        since = fcsr + 200
        test.execute( inputTag0, bfile0, tag, 'hlt', since, True, 'APPEND')  
        since = fcsr + 100
        test.execute( inputTag0, bfile0, tag, 'hlt', since, True, 'INSERT')  
    # test with synch=express
    tag = 'test_CondUpload_express'
    test.execute( inputTag0, bfile0, tag, 'express', 1, True, 'CREATE')  
    db.setSynchronizationType( tag, 'express' ) 
    test.execute( inputTag0, bfile0, tag, 'express', 200, True, 'SYNCHRONIZE')  
    fcsr = test.execute( inputTag0, bfile0, tag, 'express', 100, True, 'SYNCHRONIZE')  
    if not fcsr is None:
        since = fcsr + 200
        test.execute( inputTag0, bfile0, tag, 'express', since, True, 'APPEND')  
        since = fcsr + 100
        test.execute( inputTag0, bfile0, tag, 'express', since, True, 'INSERT')  
    # test with synch=prompt
    tag = 'test_CondUpload_prompt'
    test.execute( inputTag0, bfile0, tag, 'prompt', 1, True, 'CREATE')  
    db.setSynchronizationType( tag, 'prompt' ) 
    test.execute( inputTag0, bfile0, tag, 'prompt', 200, True, 'SYNCHRONIZE')  
    fcsr = test.execute( inputTag0, bfile0, tag, 'prompt', 100, True, 'SYNCHRONIZE')  
    if not fcsr is None:
        since = fcsr + 200
        test.execute( inputTag0, bfile0, tag, 'prompt', since, True, 'APPEND')  
        since = fcsr + 100
        test.execute( inputTag0, bfile0, tag, 'prompt', since, True, 'INSERT')  
    # test with synch=pcl
    tag = 'test_CondUpload_pcl'
    test.execute( inputTag0, bfile0, tag, 'pcl', 1, True, 'CREATE')  
    db.setSynchronizationType( tag, 'pcl' ) 
    test.execute( inputTag0, bfile0, tag, 'pcl', 200, False, 'FAIL')  
    if not fcsr is None:
        since = fcsr + 200
        test.execute( inputTag0, bfile0, tag, 'pcl', since, True, 'APPEND')  
        since = fcsr + 100
        test.execute( inputTag0, bfile0, tag, 'pcl', since, True, 'INSERT')  
    # test with synch=offline
    tag = 'test_CondUpload_offline'
    test.execute( inputTag0, bfile0, tag, 'offline', 1, True, 'CREATE')  
    db.setSynchronizationType( tag, 'offline' ) 
    test.execute( inputTag0, bfile0, tag, 'offline', 1000, True, 'APPEND')
    test.execute( inputTag0, bfile0, tag, 'offline', 500, False, 'FAIL' ) 
    test.execute( inputTag0, bfile0, tag, 'offline', 1000, False, 'FAIL' ) 
    test.execute( inputTag0, bfile0, tag, 'offline', 2000, True, 'APPEND' ) 
    # test with synch=runmc
    tag = 'test_CondUpload_runmc'
    test.execute( inputTag0, bfile0, tag, 'runmc', 1, True, 'CREATE')  
    db.setSynchronizationType( tag, 'runmc' ) 
    test.execute( inputTag0, bfile0, tag, 'runmc', 1000, True, 'APPEND')
    test.execute( inputTag0, bfile0, tag, 'runmc', 500, False, 'FAIL' ) 
    test.execute( inputTag0, bfile0, tag, 'runmc', 1000, False, 'FAIL' ) 
    test.execute( inputTag0, bfile0, tag, 'runmc', 2000, True, 'APPEND' )
    os.remove( fileName )
    print('Done. Errors: %s' %test.errors)

    
if __name__ == '__main__':
    main()