diff options
Diffstat (limited to 'testcases/Controllers/ONOS/Teston/CI/adapters')
4 files changed, 197 insertions, 37 deletions
diff --git a/testcases/Controllers/ONOS/Teston/CI/adapters/client.py b/testcases/Controllers/ONOS/Teston/CI/adapters/client.py new file mode 100644 index 000000000..32e3749f0 --- /dev/null +++ b/testcases/Controllers/ONOS/Teston/CI/adapters/client.py @@ -0,0 +1,69 @@ +""" +Description: + This file is used to run testcase + lanqinglong@huawei.com +""" +from environment import environment +import os +import time +import pexpect +import re + +class client( environment ): + + def __init__( self ): + environment.__init__( self ) + self.loginfo = environment() + self.testcase = '' + + def RunScript( self, handle, testname, timeout=300 ): + """ + Run ONOS Test Script + Parameters: + testname: ONOS Testcase Name + masterusername: The server username of running ONOS + masterpassword: The server password of running ONOS + """ + self.testcase = testname + self.ChangeTestCasePara( testname, self.masterusername, self.masterpassword ) + runhandle = handle + runtest = self.home + "/OnosSystemTest/TestON/bin/cli.py run " + testname + runhandle.sendline(runtest) + circletime = 0 + lastshowscreeninfo = '' + while True: + Result = runhandle.expect(["PEXPECT]#", pexpect.EOF, pexpect.TIMEOUT]) + curshowscreeninfo = runhandle.before + if (len(lastshowscreeninfo) != len(curshowscreeninfo)): + print str(curshowscreeninfo)[len(lastshowscreeninfo)::] + lastshowscreeninfo = curshowscreeninfo + if Result == 0: + print "Done!" + return + time.sleep(1) + circletime += 1 + if circletime > timeout: + break + self.loginfo.log( "Timeout when running the test, please check!" ) + + def onosstart( self ): + #This is the compass run machine user&pass,you need to modify + + print "Test Begin....." + self.OnosConnectionSet() + masterhandle = self.SSHlogin(self.localhost, self.masterusername, + self.masterpassword) + self.OnosEnvSetup( masterhandle ) + return masterhandle + + def onosclean( self, handle ): + self.SSHRelease( handle ) + self.loginfo.log('Release onos handle Successful') + + def push_results_to_db( self, payload, pushornot = 1): + url = self.Result_DB + "/results" + params = {"project_name": "functest", "case_name": "ONOS-" + self.testcase, + "pod_name": 'huawei-build-2', "details": payload} + headers = {'Content-Type': 'application/json'} + r = requests.post(url, data=json.dumps(params), headers=headers) + self.loginfo.log('Pushing result via Northbound, info:' + r ) diff --git a/testcases/Controllers/ONOS/Teston/CI/adapters/connection.py b/testcases/Controllers/ONOS/Teston/CI/adapters/connection.py index 9890ecfae..f7f283ed0 100644 --- a/testcases/Controllers/ONOS/Teston/CI/adapters/connection.py +++ b/testcases/Controllers/ONOS/Teston/CI/adapters/connection.py @@ -13,12 +13,13 @@ import re import sys from foundation import foundation -class connection: +class connection( foundation ): def __init__( self ): + foundation.__init__( self ) self.loginfo = foundation() - def AddKnownHost( self, ipaddr, username, password ): + def AddKnownHost( self, handle, ipaddr, username, password ): """ Add an user to known host,so that onos can login in with onos $ipaddr. parameters: @@ -27,7 +28,8 @@ class connection: password: login password """ print( "Now Adding an user to known hosts " + ipaddr ) - login = pexpect.spawn( "ssh -l %s -p 8101 %s"%( username, ipaddr ) ) + login = handle + login.sendline( "ssh -l %s -p 8101 %s"%( username, ipaddr ) ) index = 0 while index != 2: index = login.expect( ['assword:', 'yes/no', pexpect.EOF, \ @@ -38,38 +40,64 @@ class connection: index = login.expect( ["closed", pexpect.EOF] ) if index == 0: self.loginfo.log( "Add SSH Known Host Success!" ) + break else: self.loginfo.log( "Add SSH Known Host Failed! Please Check!" ) - #login.interact() + break + login.prompt( ) if index == 1: login.sendline('yes') - def Gensshkey( self ): + def GetEnvValue( self, handle, envname): + """ + os.getenv only returns current user value + GetEnvValue returns a environment value of + current handle + eg: GetEnvValue(handle,'HOME') + """ + envhandle = handle + envhandle.sendline( 'echo $' + envname ) + envhandle.prompt( ) + reg = envname + '\r\n(.*)\r' + envaluereg = re.compile( reg ) + envalue = envaluereg.search( envhandle.before ) + if envalue: + return envalue.groups()[0] + else: + return None + + def Gensshkey( self, handle ): """ Generate ssh keys, used for some server have no sshkey. """ print "Now Generating SSH keys..." #Here file name may be id_rsa or id_ecdsa or others #So here will have a judgement - filelist = os.listdir( '~/.ssh' ) + keysub = handle + filepath = self.GetEnvValue( keysub, 'HOME' ) + '/.ssh' + filelist = os.listdir( filepath ) for item in filelist: if 'id' in item: self.loginfo.log("SSH keys are exsit in ssh directory.") return True - keysub = pexpect.spawn("ssh-keygen -t rsa") + keysub.sendline("ssh-keygen -t rsa") Result = 0 while Result != 2: Result = keysub.expect( ["Overwrite", "Enter", pexpect.EOF, \ - pexpect.TIMEOUT]) + 'PEXPECT]#', pexpect.TIMEOUT]) if Result == 0: keysub.sendline("y") - if Result == 1: + if Result == 1 or Result == 2: keysub.sendline("\n") if Result == 3: + self.loginfo.log( "Generate SSH key success." ) + keysub.prompt() + break + if Result == 4: self.loginfo.log("Generate SSH key failed.") - - self.loginfo.log( "Generate SSH key success." ) + keysub.prompt() + break def GetRootAuth( self, password ): """ @@ -126,16 +154,33 @@ class connection: envAdd.close( ) self.loginfo.log( "Add env to bashrc success!" ) + def OnosRootPathChange( self, onospath ): + """ + Change ONOS root path in file:bash_profile + onospath: path of onos root + """ + print "Now Changing ONOS Root Path" + filepath = onospath + '/onos/tools/dev/bash_profile' + line = open(filepath, 'r').readlines() + lenall = len(line) - 1 + for i in range(lenall): + if "export ONOS_ROOT" in line[i]: + line[i] = 'export ONOS_ROOT=' + onospath + 'onos\n' + NewFile = open(filepath, 'w') + NewFile.writelines(line) + NewFile.close + print "Done!" + def OnosConnectionSet (self): """ Intergrate for ONOS connection setup """ - self.Gensshkey() - self.AddKnownHost( self.OC1, "karaf", "karaf" ) - self.AddKnownHost( self.OC2, "karaf", "karaf" ) - self.AddKnownHost( self.OC3, "karaf", "karaf" ) - currentpath = os.getcwd() - filepath = os.path.join( currentpath, "onos/tools/dev/bash_profile" ) + if self.masterusername == 'root': + filepath = '/root/' + else : + filepath = '/home/' + self.masterusername + '/' + self.OnosRootPathChange( filepath ) + filepath = os.path.join( filepath, "onos/tools/dev/bash_profile" ) self.AddEnvIntoBashrc("source " + filepath + "\n") self.AddEnvIntoBashrc("export OCT=" + self.OCT) self.AddEnvIntoBashrc("export OC1=" + self.OC1) diff --git a/testcases/Controllers/ONOS/Teston/CI/adapters/environment.py b/testcases/Controllers/ONOS/Teston/CI/adapters/environment.py index 0578d62f5..bdd0e122e 100644 --- a/testcases/Controllers/ONOS/Teston/CI/adapters/environment.py +++ b/testcases/Controllers/ONOS/Teston/CI/adapters/environment.py @@ -14,13 +14,15 @@ import pexpect import re import sys import pxssh -from foundation import foundation +from connection import connection -class environment: +class environment( connection ): def __init__( self ): - self.loginfo = foundation( ) + connection.__init__( self ) + self.loginfo = connection( ) self.masterhandle = '' + self.home = '' def DownLoadCode( self, handle, codeurl ): """ @@ -30,7 +32,7 @@ class environment: codeurl: clone code url """ print "Now loading test codes! Please wait in patient..." - originalfolder = os.getcwd() + originalfolder = self.home gitclone = handle gitclone.sendline( "git clone " + codeurl ) index = 0 @@ -92,16 +94,19 @@ class environment: Pushkeys.sendline( cmd ) Result = 0 while Result != 2: - Result = Pushkeys.expect( ["yes", "password", "#|$", pexpect.EOF, \ - pexpect.TIMEOUT]) + Result = Pushkeys.expect( ["(yes/no)", "assword:", "PEXPECT]#", \ + pexpect.EOF, pexpect.TIMEOUT]) if ( Result == 0 ): Pushkeys.sendline( "yes" ) if ( Result == 1 ): Pushkeys.sendline( password ) if ( Result == 2 ): self.loginfo.log( "ONOS Push keys Success!" ) + break if ( Result == 3 ): self.loginfo.log( "ONOS Push keys Error!" ) + break + time.sleep(2) Pushkeys.prompt( ) print "Done!" @@ -130,12 +135,9 @@ class environment: password: onos&compute node password """ print "Now Changing ONOS name&password" - if masterusername is 'root': - filepath = '/root/' - else : - filepath = '/home/' +masterusername + '/' - line = open(filepath + "onos/tools/build/envDefaults", 'r').readlines() - lenall = len(line)-1 + filepath = self.home + '/onos/tools/build/envDefaults' + line = open(filepath, 'r').readlines() + lenall = len(line) - 1 for i in range(lenall): if "ONOS_USER=" in line[i]: line[i]=line[i].replace("sdn",user) @@ -143,12 +145,12 @@ class environment: line[i]=line[i].replace("sdn",user) if "ONOS_PWD" in line[i]: line[i]=line[i].replace("rocks",password) - NewFile = open("onos/tools/build/envDefaults",'w') + NewFile = open(filepath, 'w') NewFile.writelines(line) NewFile.close print "Done!" - def ChangeTestCasePara(testcase,user,password): + def ChangeTestCasePara(self, testcase, user, password): """ When running test script, there's something need \ to change in every test folder's *.param & *.topo files @@ -156,10 +158,10 @@ class environment: password: onos&compute node password """ print "Now Changing " + testcase + " name&password" - if masterusername is 'root': + if self.masterusername == 'root': filepath = '/root/' else : - filepath = '/home/' + masterusername + '/' + filepath = '/home/' + self.masterusername + '/' filepath = filepath +"OnosSystemTest/TestON/tests/" + testcase + "/" + \ testcase + ".topo" line = open(filepath,'r').readlines() @@ -201,12 +203,24 @@ class environment: #Release ssh handle.logout() + def CopyOnostoTestbin( self ): + sourcefile = os.curdir + '/dependencies/onos' + destifile = self.home + '/onos/tools/test/bin/' + runcommand = 'cp ' + sourcefile + ' ' + destifile + os.system( runcommand ) + def OnosEnvSetup( self, handle ): """ Onos Environment Setup function """ + self.Gensshkey( handle ) + self.home = self.GetEnvValue( handle, 'HOME' ) + self.AddKnownHost( handle, self.OC1, "karaf", "karaf" ) + self.AddKnownHost( handle, self.OC2, "karaf", "karaf" ) + self.AddKnownHost( handle, self.OC3, "karaf", "karaf" ) self.DownLoadCode( handle, 'https://github.com/sunyulin/OnosSystemTest.git' ) self.DownLoadCode( handle, 'https://gerrit.onosproject.org/onos' ) + self.CopyOnostoTestbin() self.ChangeOnosName(self.agentusername,self.agentpassword) self.InstallDefaultSoftware( handle ) - self.SetOnosEnvVar(handle, self.masterpassword,self.agentpassword)
\ No newline at end of file + self.SetOnosEnvVar(handle, self.masterpassword,self.agentpassword) diff --git a/testcases/Controllers/ONOS/Teston/CI/adapters/foundation.py b/testcases/Controllers/ONOS/Teston/CI/adapters/foundation.py index 2d6b87684..c909d36be 100644 --- a/testcases/Controllers/ONOS/Teston/CI/adapters/foundation.py +++ b/testcases/Controllers/ONOS/Teston/CI/adapters/foundation.py @@ -7,11 +7,16 @@ Description: import logging import os import time +import yaml class foundation: def __init__(self): - self.dir = os.path.join( os.getcwd(), 'log' ) + + currentpath = os.getcwd() + self.logdir = os.path.join( currentpath, 'log' ) + self.workhome = currentpath[0:currentpath.rfind('testcases')-1] + self.Result_DB = '' def log (self, loginfo): """ @@ -20,7 +25,7 @@ class foundation: loginfo(input): record info """ filename = time.strftime( '%Y-%m-%d-%H-%M-%S' ) + '.log' - filepath = os.path.join( self.dir, filename ) + filepath = os.path.join( self.logdir, filename ) logging.basicConfig( level=logging.INFO, format = '%(asctime)s %(filename)s:%(message)s', datefmt = '%d %b %Y %H:%M:%S', @@ -29,4 +34,31 @@ class foundation: filelog = logging.FileHandler( filepath ) logging.getLogger( 'Functest' ).addHandler( filelog ) print loginfo - logging.info(loginfo)
\ No newline at end of file + logging.info(loginfo) + + def getdefaultpara( self ): + """ + Get Default Parameters value + """ + with open(self.workhome + "testcases/config_functest.yaml") as f: + functest_yaml = yaml.safe_load(f) + f.close() + + self.Result_DB = str(functest_yaml.get("results").get("test_db_url")) + self.masterusername = str(functest_yaml.get("ONOS").get("general").\ + get('onosbench_username')) + self.masterpassword = str(functest_yaml.get("ONOS").get("general").\ + get("onosbench_password")) + self.agentusername = str(functest_yaml.get("ONOS").get("general").\ + get("onoscli_username")) + self.agentpassword = str(functest_yaml.get("ONOS").get("general").\ + get("onoscli_password")) + self.runtimeout = functest_yaml.get("ONOS").get("general").get("runtimeout") + self.OCT = str(functest_yaml.get("ONOS").get("environment").get("OCT")) + self.OC1 = str(functest_yaml.get("ONOS").get("environment").get("OC1")) + self.OC2 = str(functest_yaml.get("ONOS").get("environment").get("OC2")) + self.OC3 = str(functest_yaml.get("ONOS").get("environment").get("OC3")) + self.OCN = str(functest_yaml.get("ONOS").get("environment").get("OCN")) + self.OCN2 = str(functest_yaml.get("ONOS").get("environment").get("OCN2")) + self.localhost = self.OCT + return True
\ No newline at end of file |