aboutsummaryrefslogtreecommitdiffstats
path: root/testcases/Controllers/ONOS/Teston/CI/adapters/environment.py
blob: ea1a836da9879e45b09336c007d1b3b7293adb89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"""
Description:
    This file is used to setup the running environment
    Include Download code,setup environment variable
            Set onos running config
            Set user name/password
            Onos-push-keys and so on
    lanqinglong@huawei.com
"""

import os
import time
import pexpect
import re
import sys
import pxssh
from connection import connection

class environment( connection ):

    def __init__( self ):
        connection.__init__( self )
        self.loginfo = connection( )
        self.masterhandle = ''
        self.home = ''

    def DownLoadCode( self, handle, codeurl ):
        """
        Download Code use 'git clone'
        parameters:
        handle:  current working handle
        codeurl: clone code url
        """
        print "Now loading test codes! Please wait in patient..."
        originalfolder = self.home
        gitclone = handle
        gitclone.sendline( "git clone " + codeurl )
        index = 0
        while index != 1 or index != 4:
            index = gitclone.expect ( ['already exists', 'resolving deltas: 100%', \
                                    'Receiving objects', 'Already up-to-date', \
                                    pexpect.EOF] )

            filefolder = originalfolder + '/' + codeurl.split('/')[-1].split('.')[0]
            if index == 0 :
                os.chdir( filefolder )
                os.system( 'git pull' )
                os.chdir( originalfolder )
                self.loginfo.log( 'Download code success!' )
                break
            elif index == 1 :
                self.loginfo.log( 'Download code success!' )
                break
            elif index == 2 :
                increment += 1
                if increment == 20:
                    print '\n'
                print '.'
            else :
                self.loginfo.log( 'Download code failed!' )
                self.loginfo.log( 'Information before' + gitclone.before )
                break
            time.sleep(5)
        gitclone.prompt( )

    def InstallDefaultSoftware( self, handle ):
        """
        Install default software
        parameters:
        handle(input): current working handle
        """
        print "Now Cleaning test environment"
        handle.sendline("sudo apt-get install -y mininet")
        handle.prompt( )
        handle.sendline("sudo pip install configobj")
        handle.prompt( )
        handle.sendline("sudo apt-get install -y sshpass")
        handle.prompt( )
        handle.sendline("OnosSystemTest/TestON/bin/cleanup.sh")
        handle.prompt( )
        time.sleep(5)
        self.loginfo.log( 'Clean environment success!' )

    def OnosPushKeys(self, handle, cmd, password):
        """
        Using onos-push-keys to make ssh device without password
        parameters:
        handle(input): working handle
        cmd(input): onos-push-keys xxx(xxx is device)
        password(input): login in password
        """
        print "Now Pushing Onos Keys:"+cmd
        Pushkeys = handle
        Pushkeys.sendline( cmd )
        Result = 0
        while Result != 2:
            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!"

    def SetOnosEnvVar( self, handle, masterpass, agentpass):
        """
        Setup onos pushkeys to all devices(3+2)
        parameters:
        handle(input): current working handle
        masterpass: scripts running server's password
        agentpass: onos cluster&compute node password
        """
        print "Now Setting test environment"
        self.OnosPushKeys( handle, "onos-push-keys " + self.OCT, masterpass)
        self.OnosPushKeys( handle, "onos-push-keys " + self.OC1, agentpass)
        self.OnosPushKeys( handle, "onos-push-keys " + self.OC2, agentpass)
        self.OnosPushKeys( handle, "onos-push-keys " + self.OC3, agentpass)
        self.OnosPushKeys( handle, "onos-push-keys " + self.OCN, agentpass)
        self.OnosPushKeys( handle, "onos-push-keys " + self.OCN2, agentpass)

    def ChangeOnosName( self, user, password):
        """
        Change onos name in envDefault file
        Because some command depend on this
        parameters:
        user: onos&compute node user
        password: onos&compute node password
        """
        print "Now Changing ONOS name&password"
        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)
           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(filepath, 'w')
        NewFile.writelines(line)
        NewFile.close
        print "Done!"

    def ChangeTestCasePara(self, testcase, user, password):
        """
        When running test script, there's something need \
        to change in every test folder's *.param & *.topo files
        user: onos&compute node user
        password: onos&compute node password
        """
        print "Now Changing " + testcase +  " name&password"
        if self.masterusername is 'root':
            filepath = '/root/'
        else :
            filepath = '/home/' + self.masterusername + '/'
        filepath = 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

    def SSHlogin ( self, ipaddr, username, password ) :
        """
        SSH login provide a connection to destination.
        parameters:
        ipaddr:   ip address
        username: login user name
        password: login password
        return: handle
        """
        login = pxssh.pxssh( )
        login.login ( ipaddr, username, password, original_prompt='[$#>]')
        #send command ls -l
        login.sendline ('ls -l')
        #match prompt
        login.prompt()
        print ("SSH login " + ipaddr + " success!")
        return login

    def SSHRelease( self, handle ):
        #Release ssh
        handle.logout()

    def CopyOnostoTestbin( self ):
        sourcefile = os.curdir + '/dependencies/onos'
        destifile = self.home + '/onos/tools/test/bin/'
        runcommand = 'cp ' + sourcefile + ' ' + destifile
        os.system( runcommand )

    def OnosEnvSetup( self, handle ):
        """
        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.CopyOnostoTestbin()
        self.ChangeOnosName(self.agentusername,self.agentpassword)
        self.InstallDefaultSoftware( handle )
        self.SetOnosEnvVar(handle, self.masterpassword,self.agentpassword)