3 This file is used to setup the running environment
4 Include Download code,setup environment variable
5 Set onos running config
7 Onos-push-keys and so on
17 from connection import connection
19 class environment( connection ):
22 connection.__init__( self )
23 self.loginfo = connection( )
24 self.masterhandle = ''
27 def DownLoadCode( self, handle, codeurl ):
29 Download Code use 'git clone'
31 handle: current working handle
32 codeurl: clone code url
34 print "Now loading test codes! Please wait in patient..."
35 originalfolder = self.home
37 gitclone.sendline( "git clone " + codeurl )
39 while index != 1 or index != 4:
40 index = gitclone.expect ( ['already exists', 'resolving deltas: 100%', \
41 'Receiving objects', 'Already up-to-date', \
44 filefolder = originalfolder + '/' + codeurl.split('/')[-1].split('.')[0]
46 os.chdir( filefolder )
47 os.system( 'git pull' )
48 os.chdir( originalfolder )
49 self.loginfo.log( 'Download code success!' )
52 self.loginfo.log( 'Download code success!' )
60 self.loginfo.log( 'Download code failed!' )
61 self.loginfo.log( 'Information before' + gitclone.before )
66 def InstallDefaultSoftware( self, handle ):
68 Install default software
70 handle(input): current working handle
72 print "Now Cleaning test environment"
73 handle.sendline("sudo apt-get install -y mininet")
75 handle.sendline("sudo pip install configobj")
77 handle.sendline("sudo apt-get install -y sshpass")
79 handle.sendline("OnosSystemTest/TestON/bin/cleanup.sh")
82 self.loginfo.log( 'Clean environment success!' )
84 def OnosPushKeys(self, handle, cmd, password):
86 Using onos-push-keys to make ssh device without password
88 handle(input): working handle
89 cmd(input): onos-push-keys xxx(xxx is device)
90 password(input): login in password
92 print "Now Pushing Onos Keys:"+cmd
94 Pushkeys.sendline( cmd )
97 Result = Pushkeys.expect( ["(yes/no)", "assword:", "PEXPECT]#", \
98 pexpect.EOF, pexpect.TIMEOUT])
100 Pushkeys.sendline( "yes" )
102 Pushkeys.sendline( password )
104 self.loginfo.log( "ONOS Push keys Success!" )
107 self.loginfo.log( "ONOS Push keys Error!" )
113 def SetOnosEnvVar( self, handle, masterpass, agentpass):
115 Setup onos pushkeys to all devices(3+2)
117 handle(input): current working handle
118 masterpass: scripts running server's password
119 agentpass: onos cluster&compute node password
121 print "Now Setting test environment"
122 self.OnosPushKeys( handle, "onos-push-keys " + self.OCT, masterpass)
123 self.OnosPushKeys( handle, "onos-push-keys " + self.OC1, agentpass)
124 self.OnosPushKeys( handle, "onos-push-keys " + self.OC2, agentpass)
125 self.OnosPushKeys( handle, "onos-push-keys " + self.OC3, agentpass)
126 self.OnosPushKeys( handle, "onos-push-keys " + self.OCN, agentpass)
127 self.OnosPushKeys( handle, "onos-push-keys " + self.OCN2, agentpass)
129 def ChangeOnosName( self, user, password):
131 Change onos name in envDefault file
132 Because some command depend on this
134 user: onos&compute node user
135 password: onos&compute node password
137 print "Now Changing ONOS name&password"
138 filepath = self.home + '/onos/tools/build/envDefaults'
139 line = open(filepath, 'r').readlines()
140 lenall = len(line) - 1
141 for i in range(lenall):
142 if "ONOS_USER=" in line[i]:
143 line[i]=line[i].replace("sdn",user)
144 if "ONOS_GROUP" in line[i]:
145 line[i]=line[i].replace("sdn",user)
146 if "ONOS_PWD" in line[i]:
147 line[i]=line[i].replace("rocks",password)
148 NewFile = open(filepath, 'w')
149 NewFile.writelines(line)
153 def ChangeTestCasePara(self, testcase, user, password):
155 When running test script, there's something need \
156 to change in every test folder's *.param & *.topo files
157 user: onos&compute node user
158 password: onos&compute node password
160 print "Now Changing " + testcase + " name&password"
161 if self.masterusername is 'root':
164 filepath = '/home/' + self.masterusername + '/'
165 filepath = filepath +"OnosSystemTest/TestON/tests/" + testcase + "/" + \
167 line = open(filepath,'r').readlines()
169 for i in range(lenall-2):
170 if ("localhost" in line[i]) or ("OCT" in line[i]):
171 line[i+1]=re.sub(">\w+",">"+user,line[i+1])
172 line[i+2]=re.sub(">\w+",">"+password,line[i+2])
173 if "OC1" in line [i] \
174 or "OC2" in line [i] \
175 or "OC3" in line [i] \
176 or "OCN" in line [i] \
177 or "OCN2" in line[i]:
178 line[i+1]=re.sub(">\w+",">root",line[i+1])
179 line[i+2]=re.sub(">\w+",">root",line[i+2])
180 NewFile = open(filepath,'w')
181 NewFile.writelines(line)
184 def SSHlogin ( self, ipaddr, username, password ) :
186 SSH login provide a connection to destination.
189 username: login user name
190 password: login password
193 login = pxssh.pxssh( )
194 login.login ( ipaddr, username, password, original_prompt='[$#>]')
196 login.sendline ('ls -l')
199 print ("SSH login " + ipaddr + " success!")
202 def SSHRelease( self, handle ):
206 def CopyOnostoTestbin( self ):
207 sourcefile = os.curdir + '/dependencies/onos'
208 destifile = self.home + '/onos/tools/test/bin/'
209 runcommand = 'cp ' + sourcefile + ' ' + destifile
210 os.system( runcommand )
212 def OnosEnvSetup( self, handle ):
214 Onos Environment Setup function
216 self.Gensshkey( handle )
217 self.home = self.GetEnvValue( handle, 'HOME' )
218 self.AddKnownHost( handle, self.OC1, "karaf", "karaf" )
219 self.AddKnownHost( handle, self.OC2, "karaf", "karaf" )
220 self.AddKnownHost( handle, self.OC3, "karaf", "karaf" )
221 self.DownLoadCode( handle, 'https://github.com/sunyulin/OnosSystemTest.git' )
222 self.DownLoadCode( handle, 'https://gerrit.onosproject.org/onos' )
223 self.CopyOnostoTestbin()
224 self.ChangeOnosName(self.agentusername,self.agentpassword)
225 self.InstallDefaultSoftware( handle )
226 self.SetOnosEnvVar(handle, self.masterpassword,self.agentpassword)