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

import cx_Oracle
import subprocess
import json
import os
import shutil
import datetime

# Requirement 1: a conddb key for the authentication with valid permission on writing on prep CMS_CONDITIONS account 
#                this could be dropped introducing a specific entry in the .netrc 
# Requirement 2: an entry "Dropbox" in the .netrc for the authentication

class DB:
    def __init__(self, serviceName, schemaName ):
        self.serviceName = serviceName
        self.schemaName = schemaName
        self.connStr = None

    def connect( self ):
        command = "cmscond_authentication_manager -s %s --list_conn | grep '%s@%s'" %(self.serviceName,self.schemaName,self.serviceName)
        pipe = subprocess.Popen( command, shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
        out = pipe.communicate()[0]
        srvconn = '%s@%s' %(self.schemaName,self.serviceName)
        rowpwd = out.split(srvconn)[1].split(self.schemaName)[1]
        pwd = ''
        for c in rowpwd:
            if c != ' ' and c != '\n':
                pwd += c
        self.connStr =  '%s/%s@%s' %(self.schemaName,pwd,self.serviceName)

    def setSynchronizationType( self, tag, synchType ):
        db = cx_Oracle.connect(self.connStr)
        cursor = db.cursor()
        db.begin()
        cursor.execute('UPDATE TAG SET SYNCHRONIZATION =:SYNCH WHERE NAME =:NAME',(synchType,tag,))
        db.commit()

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

    def removeTag( self, tag ):
        db = cx_Oracle.connect(self.connStr)
        cursor = db.cursor()
        db.begin()
        cursor.execute('DELETE FROM IOV WHERE TAG_NAME =:TAG_NAME',(tag,))
        cursor.execute('DELETE FROM TAG_LOG WHERE TAG_NAME=:TAG_NAME',(tag,))
        cursor.execute('DELETE FROM TAG WHERE NAME=:NAME',(tag,))
        db.commit()

def makeBaseFile( inputTag, startingSince ):
    cwd = os.getcwd()
    baseFile = '%s_%s.db' %(inputTag,startingSince)
    baseFilePath = os.path.join(cwd,baseFile)
    if os.path.exists( baseFile ):
        os.remove( baseFile )
    command = "conddb_import -c sqlite_file:%s -f oracle://cms_orcon_adg/CMS_CONDITIONS -i %s -t %s -b %s" %(baseFile,inputTag,inputTag,startingSince)
    pipe = subprocess.Popen( command, shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
    out = pipe.communicate()[0]
    if not os.path.exists( baseFile ):
        msg = 'ERROR: base file has not been created: %s' %out
        raise Exception( msg )
    return baseFile
        

def makeMetadataFile( inputTag, destTag, since, description ):
    cwd = os.getcwd()
    metadataFile = os.path.join(cwd,'%s.txt') %destTag
    if os.path.exists( metadataFile ):
        os.remove( metadataFile )
    metadata = {}
    metadata[ "destinationDatabase" ] = "oracle://cms_orcoff_prep/CMS_CONDITIONS"
    tagList = {}
    tagList[ destTag ] = { "dependencies": {}, "synchronizeTo": "any" }
    metadata[ "destinationTags" ] = tagList
    metadata[ "inputTag" ] = inputTag
    metadata[ "since" ] = since
    metadata[ "userText" ] = description
    fileName = destTag+".txt"
    with open( fileName, "w" ) as file:
        file.write(json.dumps(metadata,file,indent=4,sort_keys=True))

def uploadFile( fileName, logFileName ):
    command = "uploadConditions.py %s" %fileName
    pipe = subprocess.Popen( command, shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
    out = pipe.communicate()[0]
    lines = out.split('\n')
    ret = False
    for line in lines:
        if line.startswith('upload ended with code:'):
            returnCode = line.split('upload ended with code:')[1].strip()
            if returnCode == '0':
                ret = True
            break
    with open(logFileName,'a') as logFile:
        logFile.write(out)
    return ret

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

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

    def upload( self, inputTag, baseFile, destTag, synchro, destSince, success, expectedAction ):
        insertedSince = None
        destFile = '%s.db' %destTag
        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)
        makeMetadataFile( inputTag, destTag, destSince, descr )
        beforeUpload = datetime.datetime.utcnow()
        ret = uploadFile( destFile, self.logFileName )
        if ret != success:
            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))
            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.')
        os.remove( destFile )
        os.remove( metaDestFile )
        return insertedSince


def main():
    print('Testing...')
    serviceName = 'cms_orcoff_prep'
    schemaName = 'CMS_CONDITIONS'
    db = DB(serviceName,schemaName)
    db.connect()
    inputTag = 'runinfo_31X_mc'
    bfile0 = makeBaseFile( inputTag,1)
    bfile1 = makeBaseFile( inputTag,100)
    test = UploadTest( db )
    # test with synch=any
    tag = 'test_CondUpload_any'
    test.upload( inputTag, bfile0, tag, 'any', 1, True, 'CREATE' )
    test.upload( inputTag, bfile1, tag, 'any', 1, False, 'FAIL' )  
    test.upload( inputTag, bfile0, tag, 'any', 200, True, 'APPEND' )  
    test.upload( inputTag, bfile0, tag, 'any', 100, True, 'INSERT')  
    test.upload( inputTag, bfile0, tag, 'any', 200, True, 'INSERT')  
    db.removeTag( tag )
    # test with synch=validation
    tag = 'test_CondUpload_validation'
    test.upload( inputTag, bfile0, tag, 'validation', 1, True, 'CREATE')  
    db.setSynchronizationType( tag, 'validation' ) 
    test.upload( inputTag, bfile0, tag, 'validation', 1, True, 'INSERT')  
    test.upload( inputTag, bfile0, tag, 'validation', 200, True, 'APPEND')  
    test.upload( inputTag, bfile0, tag, 'validation', 100, True, 'INSERT')  
    db.removeTag( tag )
    # test with synch=mc
    tag = 'test_CondUpload_mc'
    test.upload( inputTag, bfile1, tag, 'mc', 1, False, 'FAIL')  
    test.upload( inputTag, bfile0, tag, 'mc', 1, True, 'CREATE')  
    db.setSynchronizationType( tag, 'mc' ) 
    test.upload( inputTag, bfile0, tag, 'mc', 1, False, 'FAIL')  
    test.upload( inputTag, bfile0, tag, 'mc', 200, False, 'FAIL') 
    db.removeTag( tag )
    # test with synch=hlt
    tag = 'test_CondUpload_hlt'
    test.upload( inputTag, bfile0, tag, 'hlt', 1, True, 'CREATE')  
    db.setSynchronizationType( tag, 'hlt' ) 
    test.upload( inputTag, bfile0, tag, 'hlt', 200, True, 'SYNCHRONIZE')  
    fcsr = test.upload( inputTag, bfile0, tag, 'hlt', 100, True, 'SYNCHRONIZE')  
    if not fcsr is None:
        since = fcsr + 200
        test.upload( inputTag, bfile0, tag, 'hlt', since, True, 'APPEND')  
        since = fcsr + 100
        test.upload( inputTag, bfile0, tag, 'hlt', since, True, 'INSERT')  
    db.removeTag( tag )
    # test with synch=express
    tag = 'test_CondUpload_express'
    test.upload( inputTag, bfile0, tag, 'express', 1, True, 'CREATE')  
    db.setSynchronizationType( tag, 'express' ) 
    test.upload( inputTag, bfile0, tag, 'express', 200, True, 'SYNCHRONIZE')  
    fcsr = test.upload( inputTag, bfile0, tag, 'express', 100, True, 'SYNCHRONIZE')  
    if not fcsr is None:
        since = fcsr + 200
        test.upload( inputTag, bfile0, tag, 'express', since, True, 'APPEND')  
        since = fcsr + 100
        test.upload( inputTag, bfile0, tag, 'express', since, True, 'INSERT')  
    db.removeTag( tag )
    # test with synch=prompt
    tag = 'test_CondUpload_prompt'
    test.upload( inputTag, bfile0, tag, 'prompt', 1, True, 'CREATE')  
    db.setSynchronizationType( tag, 'prompt' ) 
    test.upload( inputTag, bfile0, tag, 'prompt', 200, True, 'SYNCHRONIZE')  
    fcsr = test.upload( inputTag, bfile0, tag, 'prompt', 100, True, 'SYNCHRONIZE')  
    if not fcsr is None:
        since = fcsr + 200
        test.upload( inputTag, bfile0, tag, 'prompt', since, True, 'APPEND')  
        since = fcsr + 100
        test.upload( inputTag, bfile0, tag, 'prompt', since, True, 'INSERT')  
    db.removeTag( tag )
    # test with synch=pcl
    tag = 'test_CondUpload_pcl'
    test.upload( inputTag, bfile0, tag, 'pcl', 1, True, 'CREATE')  
    db.setSynchronizationType( tag, 'pcl' ) 
    test.upload( inputTag, bfile0, tag, 'pcl', 200, False, 'FAIL')  
    if not fcsr is None:
        since = fcsr + 200
        test.upload( inputTag, bfile0, tag, 'pcl', since, True, 'APPEND')  
        since = fcsr + 100
        test.upload( inputTag, bfile0, tag, 'pcl', since, True, 'INSERT')  
    db.removeTag( tag )
    # test with synch=offline
    tag = 'test_CondUpload_offline'
    test.upload( inputTag, bfile0, tag, 'offline', 1, True, 'CREATE')  
    db.setSynchronizationType( tag, 'offline' ) 
    test.upload( inputTag, bfile0, tag, 'offline', 1000, True, 'APPEND')
    test.upload( inputTag, bfile0, tag, 'offline', 500, False, 'FAIL' ) 
    test.upload( inputTag, bfile0, tag, 'offline', 1000, False, 'FAIL' ) 
    test.upload( inputTag, bfile0, tag, 'offline', 2000, True, 'APPEND' ) 
    db.removeTag( tag )
    # test with synch=runmc
    tag = 'test_CondUpload_runmc'
    test.upload( inputTag, bfile0, tag, 'runmc', 1, True, 'CREATE')  
    db.setSynchronizationType( tag, 'runmc' ) 
    test.upload( inputTag, bfile0, tag, 'runmc', 1000, True, 'APPEND')
    test.upload( inputTag, bfile0, tag, 'runmc', 500, False, 'FAIL' ) 
    test.upload( inputTag, bfile0, tag, 'runmc', 1000, False, 'FAIL' ) 
    test.upload( inputTag, bfile0, tag, 'runmc', 2000, True, 'APPEND' )
    db.removeTag( tag )
    os.remove( bfile0 )
    os.remove( bfile1 )
    print('Done. Errors: %s' %test.errors)
    
if __name__ == '__main__':
    main()