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
11 # All rights reserved. This program and the accompanying materials
12 # are made available under the terms of the Apache License, Version 2.0
13 # which accompanies this distribution, and is available at
14 # http://www.apache.org/licenses/LICENSE-2.0
25 from connection import Connection
26 import functest.utils.functest_logger as ft_logger
29 class Environment(Connection):
31 logger = ft_logger.Logger("environment").getLogger()
34 Connection.__init__(self)
35 self.loginfo = Connection()
36 self.masterhandle = ''
39 def DownLoadCode(self, handle, codeurl):
41 Download Code use 'git clone'
43 handle: current working handle
44 codeurl: clone code url
46 self.logger.info("Now loading test codes! Please wait in patient...")
47 originalfolder = sys.path[0]
48 self.logger.info(originalfolder)
50 gitclone.sendline("git clone " + codeurl)
53 while index != 1 or index != 4:
54 index = gitclone.expect(['already exists',
55 'esolving deltas: 100%',
58 'npacking objects: 100%', pexpect.EOF])
60 filefolder = self.home + '/' + codeurl.split('/')[-1].split('.')[0]
64 os.chdir(originalfolder)
65 self.loginfo.log('Download code success!')
67 elif index == 1 or index == 4:
68 self.loginfo.log('Download code success!')
69 gitclone.sendline("mkdir onos")
71 gitclone.sendline("cp -rf " + filefolder + "/tools onos/")
75 os.write(1, gitclone.before)
78 self.loginfo.log('Download code failed!')
79 self.loginfo.log('Information before' + gitclone.before)
83 def InstallDefaultSoftware(self, handle):
85 Install default software
87 handle(input): current working handle
89 self.logger.info("Now Cleaning test environment")
90 handle.sendline("sudo apt-get install -y mininet")
92 handle.sendline("sudo pip install configobj")
94 handle.sendline("sudo apt-get install -y sshpass")
96 handle.sendline("OnosSystemTest/TestON/bin/cleanup.sh")
99 self.loginfo.log('Clean environment success!')
101 def OnosPushKeys(self, handle, cmd, password):
103 Using onos-push-keys to make ssh device without password
105 handle(input): working handle
106 cmd(input): onos-push-keys xxx(xxx is device)
107 password(input): login in password
109 self.logger.info("Now Pushing Onos Keys:" + cmd)
111 Pushkeys.sendline(cmd)
114 Result = Pushkeys.expect(["(yes/no)", "assword:", "PEXPECT]#",
115 pexpect.EOF, pexpect.TIMEOUT])
117 Pushkeys.sendline("yes")
119 Pushkeys.sendline(password)
121 self.loginfo.log("ONOS Push keys Success!")
124 self.loginfo.log("ONOS Push keys Error!")
128 self.logger.info("Done!")
130 def SetOnosEnvVar(self, handle, masterpass, agentpass):
132 Setup onos pushkeys to all devices(3+2)
134 handle(input): current working handle
135 masterpass: scripts running server's password
136 agentpass: onos cluster&compute node password
138 self.logger.info("Now Setting test environment")
139 for host in self.hosts:
140 self.logger.info("try to connect " + str(host))
141 result = self.CheckSshNoPasswd(host)
144 "ssh login failed,try to copy master publickey" +
145 "to agent " + str(host))
146 self.CopyPublicKey(host)
147 self.OnosPushKeys(handle, "onos-push-keys " + self.OCT, masterpass)
148 self.OnosPushKeys(handle, "onos-push-keys " + self.OC1, agentpass)
149 self.OnosPushKeys(handle, "onos-push-keys " + self.OC2, agentpass)
150 self.OnosPushKeys(handle, "onos-push-keys " + self.OC3, agentpass)
151 self.OnosPushKeys(handle, "onos-push-keys " + self.OCN, agentpass)
152 self.OnosPushKeys(handle, "onos-push-keys " + self.OCN2, agentpass)
154 def CheckSshNoPasswd(self, host):
156 Check master can connect agent with no password
158 login = pexpect.spawn("ssh " + str(host))
161 index = login.expect(['(yes/no)', '>|#|\$',
162 pexpect.EOF, pexpect.TIMEOUT])
164 login.sendline("yes")
167 self.loginfo.log("ssh connect to " + str(host) +
168 " success,no need to copy ssh public key")
173 def ChangeOnosName(self, user, password):
175 Change onos name in envDefault file
176 Because some command depend on this
178 user: onos&compute node user
179 password: onos&compute node password
181 self.logger.info("Now Changing ONOS name&password")
182 filepath = self.home + '/onos/tools/build/envDefaults'
183 line = open(filepath, 'r').readlines()
184 lenall = len(line) - 1
185 for i in range(lenall):
186 if "ONOS_USER=" in line[i]:
187 line[i] = line[i].replace("sdn", user)
188 if "ONOS_GROUP" in line[i]:
189 line[i] = line[i].replace("sdn", user)
190 if "ONOS_PWD" in line[i]:
191 line[i] = line[i].replace("rocks", password)
192 NewFile = open(filepath, 'w')
193 NewFile.writelines(line)
195 self.logger.info("Done!")
197 def ChangeTestCasePara(self, testcase, user, password):
199 When running test script, there's something need
200 to change in every test folder's *.param & *.topo files
201 user: onos&compute node user
202 password: onos&compute node password
204 self.logger.info("Now Changing " + testcase + " name&password")
205 if self.masterusername == 'root':
208 filepath = '/home/' + self.masterusername + '/'
209 filepath = (filepath + "OnosSystemTest/TestON/tests/" +
210 testcase + "/" + testcase + ".topo")
211 line = open(filepath, 'r').readlines()
212 lenall = len(line) - 1
213 for i in range(lenall - 2):
214 if("localhost" in line[i]) or ("OCT" in line[i]):
215 line[i + 1] = re.sub(">\w+", ">" + user, line[i + 1])
216 line[i + 2] = re.sub(">\w+", ">" + password, line[i + 2])
217 if ("OC1" in line[i] or "OC2" in line[i] or "OC3" in line[i] or
218 "OCN" in line[i] or "OCN2" in line[i]):
219 line[i + 1] = re.sub(">\w+", ">root", line[i + 1])
220 line[i + 2] = re.sub(">\w+", ">root", line[i + 2])
221 NewFile = open(filepath, 'w')
222 NewFile.writelines(line)
225 def SSHlogin(self, ipaddr, username, password):
227 SSH login provide a connection to destination.
230 username: login user name
231 password: login password
234 login = pxssh.pxssh()
235 login.login(ipaddr, username, password, original_prompt='[$#>]')
237 login.sendline('ls -l')
240 self.logger.info("SSH login " + ipaddr + " success!")
243 def SSHRelease(self, handle):
247 def CopyOnostoTestbin(self):
248 sourcefile = self.cipath + '/dependencies/onos'
249 destifile = self.home + '/onos/tools/test/bin/'
251 runcommand = 'cp ' + sourcefile + ' ' + destifile
252 os.system(runcommand)
254 def CopyPublicKey(self, host):
255 output = os.popen('cat /root/.ssh/id_rsa.pub')
256 publickey = output.read().strip('\n')
257 tmphandle = self.SSHlogin(self.installer_master,
258 self.installer_master_username,
259 self.installer_master_password)
260 tmphandle.sendline("ssh " + host + " -T \'echo " +
261 str(publickey) + ">>/root/.ssh/authorized_keys\'")
263 self.SSHRelease(tmphandle)
264 self.logger.info("Add OCT PublicKey to " + host + " success")
266 def OnosEnvSetup(self, handle):
268 Onos Environment Setup function
270 self.Gensshkey(handle)
271 self.home = self.GetEnvValue(handle, 'HOME')
272 self.AddKnownHost(handle, self.OC1, "karaf", "karaf")
273 self.AddKnownHost(handle, self.OC2, "karaf", "karaf")
274 self.AddKnownHost(handle, self.OC3, "karaf", "karaf")
275 self.DownLoadCode(handle,
276 'https://github.com/wuwenbin2/OnosSystemTest.git')
277 # self.DownLoadCode(handle, 'https://gerrit.onosproject.org/onos')
278 if self.masterusername == 'root':
281 filepath = '/home/' + self.masterusername + '/'
282 self.OnosRootPathChange(filepath)
283 self.CopyOnostoTestbin()
284 self.ChangeOnosName(self.agentusername, self.agentpassword)
285 self.InstallDefaultSoftware(handle)
286 self.SetOnosEnvVar(handle, self.masterpassword, self.agentpassword)