From dd53db9a5ffae2f71b89bc8d9d82f64104384541 Mon Sep 17 00:00:00 2001 From: lanqinglong Date: Fri, 9 Oct 2015 21:19:49 +0800 Subject: Use new client script to run onos test JIRA:FUNCTEST-61 Add client.py modify some scripts Change-Id: Icc562ec5211dfea664f89086d259cc7fc011e3a4 Signed-off-by: lanqinglong --- .../Controllers/ONOS/Teston/CI/adapters/client.py | 35 ++++ .../ONOS/Teston/CI/adapters/connection.py | 79 ++++++-- .../ONOS/Teston/CI/adapters/environment.py | 38 ++-- .../Controllers/ONOS/Teston/CI/onosfunctest.py | 208 ++------------------- 4 files changed, 137 insertions(+), 223 deletions(-) create mode 100644 testcases/Controllers/ONOS/Teston/CI/adapters/client.py (limited to 'testcases') 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..25adcbe08 --- /dev/null +++ b/testcases/Controllers/ONOS/Teston/CI/adapters/client.py @@ -0,0 +1,35 @@ +""" +Description: + This file is used to run testcase + lanqinglong@huawei.com +""" +from environment import environment + +class client( environment ): + + def __init__( self ): + environment.__init__( self ) + self.loginfo = environment() + + def RunScript( self, testname ): + """ + Run ONOS Test Script + Parameters: + testname: ONOS Testcase Name + masterusername: The server username of running ONOS + masterpassword: The server password of running ONOS + """ + self.ChangeTestCasePara( testname, self.masterusername, self.masterpassword ) + runtest = "OnosSystemTest/TestON/bin/cli.py run " + testname + os.system(runtest) + print "Done!" + + def onosbasic(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 ) + self.SSHRelease( masterhandle ) \ No newline at end of file diff --git a/testcases/Controllers/ONOS/Teston/CI/adapters/connection.py b/testcases/Controllers/ONOS/Teston/CI/adapters/connection.py index 9890ecfae..1d96a49d5 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 is '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..01e72cd4a 100644 --- a/testcases/Controllers/ONOS/Teston/CI/adapters/environment.py +++ b/testcases/Controllers/ONOS/Teston/CI/adapters/environment.py @@ -14,12 +14,13 @@ 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 = '' def DownLoadCode( self, handle, codeurl ): @@ -30,7 +31,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 +93,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 +134,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 +144,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 +157,10 @@ class environment: password: onos&compute node password """ print "Now Changing " + testcase + " name&password" - if masterusername is 'root': + if self.masterusername is 'root': filepath = '/root/' else : - filepath = '/home/' + masterusername + '/' + filepath = '/home/' + self.masterusername + '/' filepath = filepath +"OnosSystemTest/TestON/tests/" + testcase + "/" + \ testcase + ".topo" line = open(filepath,'r').readlines() @@ -205,6 +206,11 @@ class environment: """ 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.ChangeOnosName(self.agentusername,self.agentpassword) diff --git a/testcases/Controllers/ONOS/Teston/CI/onosfunctest.py b/testcases/Controllers/ONOS/Teston/CI/onosfunctest.py index 72fa4ae1f..3286a15f8 100644 --- a/testcases/Controllers/ONOS/Teston/CI/onosfunctest.py +++ b/testcases/Controllers/ONOS/Teston/CI/onosfunctest.py @@ -7,197 +7,25 @@ CASE2 - Ovsdb test&Default configuration&Vm go online lanqinglong@huawei.com """ -import os -import os.path -import time -import pexpect -import re -import sys +from adapters.client import client -def SSHlogin(ipaddr,username,password): - login = pexpect.spawn('ssh %s@%s'%(username,ipaddr)) - index = 0 - while index != 2: - index = login.expect(['assword:','yes/no','#|$',pexpect.EOF]) - if index == 0: - login.sendline(password) - login.interact() - if index == 1: - login.sendline('yes') - print "Login Success!" - -def AddKarafUser(ipaddr,username,password): - print '\033[1;31;40m' - print "Now Adding karaf user to OC1..." - print "\033[0m" - login = pexpect.spawn("ssh -l %s -p 8101 %s"%(username,ipaddr)) - index = 0 - while index != 2: - index = login.expect(['assword:','yes/no',pexpect.EOF]) - if index == 0: - login.sendline(password) - login.sendline("logout") - index = login.expect(["closed",pexpect.EOF]) - if index == 0: - print "Add SSH Known Host Success!" - else: - print "Add SSH Known Host Failed! Please Check!" - login.interact() - if index == 1: - login.sendline('yes') - -def DownLoadCode(): - print '\033[1;31;40m' - print "Now loading test codes!Please wait in patient..." - print "\033[0m" - os.system("git clone https://github.com/sunyulin/OnosSystemTest.git") - time.sleep(1) - os.system("git clone https://gerrit.onosproject.org/onos") - time.sleep(1) - print "Done!" - -def CleanEnv(): - print '\033[1;31;40m' - print "Now Cleaning test environment" - print "\033[0m" - os.system("sudo apt-get install -y mininet") - os.system("OnosSystemTest/TestON/bin/cleanup.sh") - time.sleep(5) - print "Done!" - -def OnosPushKeys(cmd,password): - print '\033[1;31;40m' - print "Now Pushing Onos Keys:"+cmd - print "\033[0m" - Pushkeys = pexpect.spawn(cmd) - Result = 0 - while Result != 2: - Result = Pushkeys.expect(["yes","password",pexpect.EOF,pexpect.TIMEOUT]) - if (Result == 0): - Pushkeys.sendline("yes") - if (Result == 1): - Pushkeys.sendline(password) - if (Result == 3): - print("Push keys Error!") - print "Done!" - -def AddEnvIntoBashrc(name): - print '\033[1;31;40m' - print "Now Adding bash environment" - print "\033[0m" - fileopen = open("/etc/profile",'r') - findContext = 1 - while findContext: - findContext = fileopen.readline() - result = findContext.find('dev/bash_profile') - if result != -1: - break - fileopen.close - if result == -1: - envAdd = open("/etc/profile",'a+') - envAdd.writelines("\nsource /root/onos/tools/dev/bash_profile") - envAdd.close() - -def SetEnvVar(masterpass,agentpass): - print '\033[1;31;40m' - print "Now Setting test environment" - print "\033[0m" - os.environ["OCT"] = "10.1.0.1" - os.environ["OC1"] = "10.1.0.50" - os.environ["OC2"] = "10.1.0.51" - os.environ["OC3"] = "10.1.0.52" - os.environ["OCN"] = "10.1.0.53" - os.environ["OCN2"] = "10.1.0.54" - os.environ["localhost"] = "10.1.0.1" - os.system("sudo pip install configobj") - os.system("sudo apt-get install -y sshpass") - OnosPushKeys("onos-push-keys 10.1.0.1",masterpass) - OnosPushKeys("onos-push-keys 10.1.0.50",agentpass) - OnosPushKeys("onos-push-keys 10.1.0.53",agentpass) - OnosPushKeys("onos-push-keys 10.1.0.54",agentpass) - -def Gensshkey(): - print '\033[1;31;40m' - print "Now Generating SSH keys..." - print "\033[0m" - os.system("rm -rf ~/.ssh/*") - keysub = pexpect.spawn("ssh-keygen -t rsa") - Result = 0 - while Result != 2: - Result = keysub.expect(["Overwrite","Enter",pexpect.EOF,pexpect.TIMEOUT]) - if Result == 0: - keysub.sendline("y") - if Result == 1: - keysub.sendline("\n") - if Result == 3: - printf("Generate SSH key failed.") - print "Done!" - -def ChangeOnosName(user,password): - print '\033[1;31;40m' - print "Now Changing ONOS name&password" - print "\033[0m" - line = open("onos/tools/build/envDefaults",'r').readlines() - lenall = len(line)-1 - for i in range(lenall): - if "ONOS_USER=" in line[i]: - line[i]=line[i].replace("sdn",user) - if "ONOS_GROUP" in line[i]: - 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.writelines(line) - NewFile.close - print "Done!" - -def ChangeTestCasePara(testcase,user,password): - print '\033[1;31;40m' - print "Now Changing " + testcase + " name&password" - print "\033[0m" - filepath = "OnosSystemTest/TestON/tests/" + testcase + "/" + testcase + ".topo" - line = open(filepath,'r').readlines() - lenall = len(line)-1 - for i in range(lenall-2): - if ("localhost" in line[i]) or ("OCT" in line[i]): - line[i+1]=re.sub(">\w+",">"+user,line[i+1]) - line[i+2]=re.sub(">\w+",">"+password,line[i+2]) - if "OC1" in line [i] \ - or "OC2" in line [i] \ - or "OC3" in line [i] \ - or "OCN" in line [i] \ - or "OCN2" in line[i]: - line[i+1]=re.sub(">\w+",">root",line[i+1]) - line[i+2]=re.sub(">\w+",">root",line[i+2]) - NewFile = open(filepath,'w') - NewFile.writelines(line) - NewFile.close - print "Done!" - -def RunScript(testname,masterusername,masterpassword): - ChangeTestCasePara(testname,masterusername,masterpassword) - runtest = "OnosSystemTest/TestON/bin/cli.py run " + testname - os.system(runtest) - print "Done!" if __name__=="__main__": - #This is the compass run machine user&pass,you need to modify - masterusername = "root" - masterpassword = "root" - - #The config below you don't need to care - agentusername = "root" - agentpassword = "root" - - print "Test Begin....." - Gensshkey() - AddKarafUser("10.1.0.50","karaf","karaf") - AddEnvIntoBashrc("source onos/tools/dev/bash_profile") - SSHlogin("10.1.0.1",masterusername,masterpassword) - ChangeOnosName(agentusername,agentpassword) - DownLoadCode() - CleanEnv() - SetEnvVar(masterpassword,agentpassword) - RunScript("FUNCvirNetNB",masterusername,masterpassword) - RunScript("FUNCovsdbtest",masterusername,masterpassword) + main = client() + main.masterusername = "root" + main.masterpassword = "root" + main.agentusername = "root" + main.agentpassword = "root" + main.OCT = '189.42.8.99' + main.OC1 = '189.42.8.101' + main.OC2 = '189.42.8.102' + main.OC3 = '189.42.8.103' + main.OCN = '189.42.8.104' + main.OCN2 = '189.42.8.105' + main.localhost = main.OCT + main.onosbasic() + + #scripts to run + main.RunScript("FUNCvirNetNB") + main.RunScript("FUNCovsdbtest") -- cgit 1.2.3-korg