Change 'testcases' directory structure
[functest.git] / testcases / Controllers / 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 os
19 import time
20 import pexpect
21 import re
22 import sys
23 import pxssh
24 from connection import connection
25
26
27 class environment(connection):
28
29     def __init__(self):
30         connection.__init__(self)
31         self.loginfo = connection()
32         self.masterhandle = ''
33         self.home = ''
34
35     def DownLoadCode(self, handle, codeurl):
36         """
37         Download Code use 'git clone'
38         parameters:
39         handle:  current working handle
40         codeurl: clone code url
41         """
42         print "Now loading test codes! Please wait in patient..."
43         originalfolder = sys.path[0]
44         print originalfolder
45         gitclone = handle
46         gitclone.sendline("git clone " + codeurl)
47         index = 0
48         # increment = 0
49         while index != 1 or index != 4:
50             index = gitclone.expect(['already exists',
51                                      'esolving deltas: 100%',
52                                      'eceiving objects',
53                                      'Already up-to-date',
54                                      'npacking objects: 100%', pexpect.EOF])
55
56             filefolder = self.home + '/' + codeurl.split('/')[-1].split('.')[0]
57             if index == 0:
58                 os.chdir(filefolder)
59                 os.system('git pull')
60                 os.chdir(originalfolder)
61                 self.loginfo.log('Download code success!')
62                 break
63             elif index == 1 or index == 4:
64                 self.loginfo.log('Download code success!')
65                 gitclone.sendline("mkdir onos")
66                 gitclone.prompt()
67                 gitclone.sendline("cp -rf " + filefolder + "/tools onos/")
68                 gitclone.prompt()
69                 break
70             elif index == 2:
71                 os.write(1, gitclone.before)
72                 sys.stdout.flush()
73             else:
74                 self.loginfo.log('Download code failed!')
75                 self.loginfo.log('Information before' + gitclone.before)
76                 break
77         gitclone.prompt()
78
79     def InstallDefaultSoftware(self, handle):
80         """
81         Install default software
82         parameters:
83         handle(input): current working handle
84         """
85         print "Now Cleaning test environment"
86         handle.sendline("sudo apt-get install -y mininet")
87         handle.prompt()
88         handle.sendline("sudo pip install configobj")
89         handle.prompt()
90         handle.sendline("sudo apt-get install -y sshpass")
91         handle.prompt()
92         handle.sendline("OnosSystemTest/TestON/bin/cleanup.sh")
93         handle.prompt()
94         time.sleep(5)
95         self.loginfo.log('Clean environment success!')
96
97     def OnosPushKeys(self, handle, cmd, password):
98         """
99         Using onos-push-keys to make ssh device without password
100         parameters:
101         handle(input): working handle
102         cmd(input): onos-push-keys xxx(xxx is device)
103         password(input): login in password
104         """
105         print "Now Pushing Onos Keys:" + cmd
106         Pushkeys = handle
107         Pushkeys.sendline(cmd)
108         Result = 0
109         while Result != 2:
110             Result = Pushkeys.expect(["(yes/no)", "assword:", "PEXPECT]#",
111                                       pexpect.EOF, pexpect.TIMEOUT])
112             if(Result == 0):
113                 Pushkeys.sendline("yes")
114             if(Result == 1):
115                 Pushkeys.sendline(password)
116             if(Result == 2):
117                 self.loginfo.log("ONOS Push keys Success!")
118                 break
119             if(Result == 3):
120                 self.loginfo.log("ONOS Push keys Error!")
121                 break
122             time.sleep(2)
123         Pushkeys.prompt()
124         print "Done!"
125
126     def SetOnosEnvVar(self, handle, masterpass, agentpass):
127         """
128         Setup onos pushkeys to all devices(3+2)
129         parameters:
130         handle(input): current working handle
131         masterpass: scripts running server's password
132         agentpass: onos cluster&compute node password
133         """
134         print "Now Setting test environment"
135         for host in self.hosts:
136             print "try to connect " + str(host)
137             result = self.CheckSshNoPasswd(host)
138             if not result:
139                 print ("ssh lgin failed,try to copy master publickey" +
140                        "to agent " + str(host))
141                 self.CopyPublicKey(host)
142         self.OnosPushKeys(handle, "onos-push-keys " + self.OCT, masterpass)
143         self.OnosPushKeys(handle, "onos-push-keys " + self.OC1, agentpass)
144         self.OnosPushKeys(handle, "onos-push-keys " + self.OC2, agentpass)
145         self.OnosPushKeys(handle, "onos-push-keys " + self.OC3, agentpass)
146         self.OnosPushKeys(handle, "onos-push-keys " + self.OCN, agentpass)
147         self.OnosPushKeys(handle, "onos-push-keys " + self.OCN2, agentpass)
148
149     def CheckSshNoPasswd(self, host):
150         """
151         Check master can connect agent with no password
152         """
153         login = pexpect.spawn("ssh " + str(host))
154         index = 4
155         while index == 4:
156             index = login.expect(['(yes/no)', '>|#|\$',
157                                   pexpect.EOF, pexpect.TIMEOUT])
158             if index == 0:
159                 login.sendline("yes")
160                 index = 4
161             if index == 1:
162                 self.loginfo.log("ssh connect to " + str(host) +
163                                  " success,no need to copy ssh public key")
164                 return True
165         login.interact()
166         return False
167
168     def ChangeOnosName(self, user, password):
169         """
170         Change onos name in envDefault file
171         Because some command depend on this
172         parameters:
173         user: onos&compute node user
174         password: onos&compute node password
175         """
176         print "Now Changing ONOS name&password"
177         filepath = self.home + '/onos/tools/build/envDefaults'
178         line = open(filepath, 'r').readlines()
179         lenall = len(line) - 1
180         for i in range(lenall):
181             if "ONOS_USER=" in line[i]:
182                 line[i] = line[i].replace("sdn", user)
183             if "ONOS_GROUP" in line[i]:
184                 line[i] = line[i].replace("sdn", user)
185             if "ONOS_PWD" in line[i]:
186                 line[i] = line[i].replace("rocks", password)
187         NewFile = open(filepath, 'w')
188         NewFile.writelines(line)
189         NewFile.close
190         print "Done!"
191
192     def ChangeTestCasePara(self, testcase, user, password):
193         """
194         When running test script, there's something need \
195         to change in every test folder's *.param & *.topo files
196         user: onos&compute node user
197         password: onos&compute node password
198         """
199         print "Now Changing " + testcase + " name&password"
200         if self.masterusername == 'root':
201             filepath = '/root/'
202         else:
203             filepath = '/home/' + self.masterusername + '/'
204         filepath = (filepath + "OnosSystemTest/TestON/tests/" +
205                     testcase + "/" + testcase + ".topo")
206         line = open(filepath, 'r').readlines()
207         lenall = len(line) - 1
208         for i in range(lenall - 2):
209             if("localhost" in line[i]) or ("OCT" in line[i]):
210                 line[i + 1] = re.sub(">\w+", ">" + user, line[i + 1])
211                 line[i + 2] = re.sub(">\w+", ">" + password, line[i + 2])
212             if ("OC1" in line[i] or "OC2" in line[i] or "OC3" in line[i] or
213                     "OCN" in line[i] or "OCN2" in line[i]):
214                 line[i + 1] = re.sub(">\w+", ">root", line[i + 1])
215                 line[i + 2] = re.sub(">\w+", ">root", line[i + 2])
216         NewFile = open(filepath, 'w')
217         NewFile.writelines(line)
218         NewFile.close
219
220     def SSHlogin(self, ipaddr, username, password):
221         """
222         SSH login provide a connection to destination.
223         parameters:
224         ipaddr:   ip address
225         username: login user name
226         password: login password
227         return: handle
228         """
229         login = pxssh.pxssh()
230         login.login(ipaddr, username, password, original_prompt='[$#>]')
231         # send command ls -l
232         login.sendline('ls -l')
233         # match prompt
234         login.prompt()
235         print("SSH login " + ipaddr + " success!")
236         return login
237
238     def SSHRelease(self, handle):
239         # Release ssh
240         handle.logout()
241
242     def CopyOnostoTestbin(self):
243         sourcefile = self.cipath + '/dependencies/onos'
244         destifile = self.home + '/onos/tools/test/bin/'
245         os.system('pwd')
246         runcommand = 'cp ' + sourcefile + ' ' + destifile
247         os.system(runcommand)
248
249     def CopyPublicKey(self, host):
250         output = os.popen('cat /root/.ssh/id_rsa.pub')
251         publickey = output.read().strip('\n')
252         tmphandle = self.SSHlogin(self.installer_master,
253                                   self.installer_master_username,
254                                   self.installer_master_password)
255         tmphandle.sendline("ssh " + host + " -T \'echo " +
256                            str(publickey) + ">>/root/.ssh/authorized_keys\'")
257         tmphandle.prompt()
258         self.SSHRelease(tmphandle)
259         print "Add OCT PublicKey to " + host + " success"
260
261     def OnosEnvSetup(self, handle):
262         """
263         Onos Environment Setup function
264         """
265         self.Gensshkey(handle)
266         self.home = self.GetEnvValue(handle, 'HOME')
267         self.AddKnownHost(handle, self.OC1, "karaf", "karaf")
268         self.AddKnownHost(handle, self.OC2, "karaf", "karaf")
269         self.AddKnownHost(handle, self.OC3, "karaf", "karaf")
270         self.DownLoadCode(handle,
271                           'https://github.com/sunyulin/OnosSystemTest.git')
272         # self.DownLoadCode(handle, 'https://gerrit.onosproject.org/onos')
273         if self.masterusername == 'root':
274             filepath = '/root/'
275         else:
276             filepath = '/home/' + self.masterusername + '/'
277         self.OnosRootPathChange(filepath)
278         self.CopyOnostoTestbin()
279         self.ChangeOnosName(self.agentusername, self.agentpassword)
280         self.InstallDefaultSoftware(handle)
281         self.SetOnosEnvVar(handle, self.masterpassword, self.agentpassword)