Merge "Delete functest.utils.functest_logger"
[functest.git] / functest / opnfv_tests / sdn / onos / teston / adapters / environment.py
1 """
2 Description:
3     This file is used to setup the running environment
4     Include Download code,setup environment variable
5             Set onos running config
6             Set user name/password
7             Onos-push-keys and so on
8     lanqinglong@huawei.com
9
10 #
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
15 #
16 """
17
18 import logging
19 import pexpect
20 import pxssh
21 import re
22 import os
23 import sys
24 import time
25
26 from connection import Connection
27
28
29 class Environment(Connection):
30
31     logger = logging.getLogger(__name__)
32
33     def __init__(self):
34         Connection.__init__(self)
35         self.loginfo = Connection()
36         self.masterhandle = ''
37         self.home = ''
38
39     def DownLoadCode(self, handle, codeurl):
40         """
41         Download Code use 'git clone'
42         parameters:
43         handle:  current working handle
44         codeurl: clone code url
45         """
46         self.logger.info("Now loading test codes! Please wait in patient...")
47         originalfolder = sys.path[0]
48         self.logger.info(originalfolder)
49         gitclone = handle
50         gitclone.sendline("git clone " + codeurl)
51         index = 0
52         # increment = 0
53         while index != 1 or index != 4:
54             index = gitclone.expect(['already exists',
55                                      'esolving deltas: 100%',
56                                      'eceiving objects',
57                                      'Already up-to-date',
58                                      'npacking objects: 100%', pexpect.EOF])
59
60             filefolder = self.home + '/' + codeurl.split('/')[-1].split('.')[0]
61             if index == 0:
62                 os.chdir(filefolder)
63                 os.system('git pull')
64                 os.chdir(originalfolder)
65                 self.loginfo.log('Download code success!')
66                 break
67             elif index == 1 or index == 4:
68                 self.loginfo.log('Download code success!')
69                 gitclone.sendline("mkdir onos")
70                 gitclone.prompt()
71                 gitclone.sendline("cp -rf " + filefolder + "/tools onos/")
72                 gitclone.prompt()
73                 break
74             elif index == 2:
75                 os.write(1, gitclone.before)
76                 sys.stdout.flush()
77             else:
78                 self.loginfo.log('Download code failed!')
79                 self.loginfo.log('Information before' + gitclone.before)
80                 break
81         gitclone.prompt()
82
83     def InstallDefaultSoftware(self, handle):
84         """
85         Install default software
86         parameters:
87         handle(input): current working handle
88         """
89         self.logger.info("Now Cleaning test environment")
90         handle.sendline("sudo apt-get install -y mininet")
91         handle.prompt()
92         handle.sendline("sudo pip install configobj")
93         handle.prompt()
94         handle.sendline("sudo apt-get install -y sshpass")
95         handle.prompt()
96         handle.sendline("OnosSystemTest/TestON/bin/cleanup.sh")
97         handle.prompt()
98         time.sleep(5)
99         self.loginfo.log('Clean environment success!')
100
101     def OnosPushKeys(self, handle, cmd, password):
102         """
103         Using onos-push-keys to make ssh device without password
104         parameters:
105         handle(input): working handle
106         cmd(input): onos-push-keys xxx(xxx is device)
107         password(input): login in password
108         """
109         self.logger.info("Now Pushing Onos Keys:" + cmd)
110         Pushkeys = handle
111         Pushkeys.sendline(cmd)
112         Result = 0
113         while Result != 2:
114             Result = Pushkeys.expect(["(yes/no)", "assword:", "PEXPECT]#",
115                                       pexpect.EOF, pexpect.TIMEOUT])
116             if(Result == 0):
117                 Pushkeys.sendline("yes")
118             if(Result == 1):
119                 Pushkeys.sendline(password)
120             if(Result == 2):
121                 self.loginfo.log("ONOS Push keys Success!")
122                 break
123             if(Result == 3):
124                 self.loginfo.log("ONOS Push keys Error!")
125                 break
126             time.sleep(2)
127         Pushkeys.prompt()
128         self.logger.info("Done!")
129
130     def SetOnosEnvVar(self, handle, masterpass, agentpass):
131         """
132         Setup onos pushkeys to all devices(3+2)
133         parameters:
134         handle(input): current working handle
135         masterpass: scripts running server's password
136         agentpass: onos cluster&compute node password
137         """
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)
142             if not result:
143                 self.logger.info(
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)
153
154     def CheckSshNoPasswd(self, host):
155         """
156         Check master can connect agent with no password
157         """
158         login = pexpect.spawn("ssh " + str(host))
159         index = 4
160         while index == 4:
161             index = login.expect(['(yes/no)', '>|#|\$',
162                                   pexpect.EOF, pexpect.TIMEOUT])
163             if index == 0:
164                 login.sendline("yes")
165                 index = 4
166             if index == 1:
167                 self.loginfo.log("ssh connect to " + str(host) +
168                                  " success,no need to copy ssh public key")
169                 return True
170         login.interact()
171         return False
172
173     def ChangeOnosName(self, user, password):
174         """
175         Change onos name in envDefault file
176         Because some command depend on this
177         parameters:
178         user: onos&compute node user
179         password: onos&compute node password
180         """
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)
194         NewFile.close
195         self.logger.info("Done!")
196
197     def ChangeTestCasePara(self, testcase, user, password):
198         """
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
203         """
204         self.logger.info("Now Changing " + testcase + " name&password")
205         if self.masterusername == 'root':
206             filepath = '/root/'
207         else:
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)
223         NewFile.close
224
225     def SSHlogin(self, ipaddr, username, password):
226         """
227         SSH login provide a connection to destination.
228         parameters:
229         ipaddr:   ip address
230         username: login user name
231         password: login password
232         return: handle
233         """
234         login = pxssh.pxssh()
235         login.login(ipaddr, username, password, original_prompt='[$#>]')
236         # send command ls -l
237         login.sendline('ls -l')
238         # match prompt
239         login.prompt()
240         self.logger.info("SSH login " + ipaddr + " success!")
241         return login
242
243     def SSHRelease(self, handle):
244         # Release ssh
245         handle.logout()
246
247     def CopyOnostoTestbin(self):
248         sourcefile = self.cipath + '/dependencies/onos'
249         destifile = self.home + '/onos/tools/test/bin/'
250         os.system('pwd')
251         runcommand = 'cp ' + sourcefile + ' ' + destifile
252         os.system(runcommand)
253
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\'")
262         tmphandle.prompt()
263         self.SSHRelease(tmphandle)
264         self.logger.info("Add OCT PublicKey to " + host + " success")
265
266     def OnosEnvSetup(self, handle):
267         """
268         Onos Environment Setup function
269         """
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':
279             filepath = '/root/'
280         else:
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)