summaryrefslogtreecommitdiffstats
path: root/testcases/Controllers/ONOS/Teston/CI/onosfunctest.py
blob: 72fa4ae1f1c547fc51cc56ac080c07ca2f3897b8 (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
"""
Description: This test is to run onos Teston VTN scripts

List of test cases:
CASE1 - Northbound NBI test network/subnet/ports
CASE2 - Ovsdb test&Default configuration&Vm go online

lanqinglong@huawei.com
"""
import os
import os.path
import time
import pexpect
import re
import sys

def SSHlogin(ipaddr,username,password):
    login = pexpect.spawn('ssh %s@%s'%(username,ipaddr))
    index = 0
    while index != 2:
        index = login.expect(['assword:','yes/no','#|$',pexpect.EOF])
        if index == 0:
            login.sendline(password)
            login.interact()
        if index == 1:
            login.sendline('yes')
    print "Login Success!"

def AddKarafUser(ipaddr,username,password):
    print '\033[1;31;40m'
    print "Now Adding karaf user to OC1..."
    print "\033[0m"
    login = pexpect.spawn("ssh -l %s -p 8101 %s"%(username,ipaddr))
    index = 0
    while index != 2:
        index = login.expect(['assword:','yes/no',pexpect.EOF])
        if index == 0:
            login.sendline(password)
            login.sendline("logout")
            index = login.expect(["closed",pexpect.EOF])
            if index == 0:
                print "Add SSH Known Host Success!"
            else:
                print "Add SSH Known Host Failed! Please Check!"
            login.interact()
        if index == 1:
            login.sendline('yes')

def DownLoadCode():
    print '\033[1;31;40m'
    print "Now loading test codes!Please wait in patient..."
    print "\033[0m"
    os.system("git clone https://github.com/sunyulin/OnosSystemTest.git")
    time.sleep(1)
    os.system("git clone https://gerrit.onosproject.org/onos")
    time.sleep(1)
    print "Done!"

def CleanEnv():
    print '\033[1;31;40m'
    print "Now Cleaning test environment"
    print "\033[0m"
    os.system("sudo apt-get install -y mininet")
    os.system("OnosSystemTest/TestON/bin/cleanup.sh")
    time.sleep(5)
    print "Done!"

def OnosPushKeys(cmd,password):
    print '\033[1;31;40m'
    print "Now Pushing Onos Keys:"+cmd
    print "\033[0m"
    Pushkeys = pexpect.spawn(cmd)
    Result = 0
    while Result != 2:
        Result = Pushkeys.expect(["yes","password",pexpect.EOF,pexpect.TIMEOUT])
        if (Result == 0):
            Pushkeys.sendline("yes")
        if (Result == 1):
            Pushkeys.sendline(password)
        if (Result == 3):
            print("Push keys Error!")
    print "Done!"

def AddEnvIntoBashrc(name):
    print '\033[1;31;40m'
    print "Now Adding bash environment"
    print "\033[0m"
    fileopen = open("/etc/profile",'r')
    findContext = 1
    while findContext:
        findContext = fileopen.readline()
        result = findContext.find('dev/bash_profile')
        if result != -1:
            break
    fileopen.close
    if result == -1:
        envAdd = open("/etc/profile",'a+')
        envAdd.writelines("\nsource /root/onos/tools/dev/bash_profile")
        envAdd.close()

def SetEnvVar(masterpass,agentpass):
    print '\033[1;31;40m'
    print "Now Setting test environment"
    print "\033[0m"
    os.environ["OCT"] = "10.1.0.1"
    os.environ["OC1"] = "10.1.0.50"
    os.environ["OC2"] = "10.1.0.51"
    os.environ["OC3"] = "10.1.0.52"
    os.environ["OCN"] = "10.1.0.53"
    os.environ["OCN2"] = "10.1.0.54"
    os.environ["localhost"] = "10.1.0.1"
    os.system("sudo pip install configobj")
    os.system("sudo apt-get install -y sshpass")
    OnosPushKeys("onos-push-keys 10.1.0.1",masterpass)
    OnosPushKeys("onos-push-keys 10.1.0.50",agentpass)
    OnosPushKeys("onos-push-keys 10.1.0.53",agentpass)
    OnosPushKeys("onos-push-keys 10.1.0.54",agentpass)

def Gensshkey():
    print '\033[1;31;40m'
    print "Now Generating SSH keys..."
    print "\033[0m"
    os.system("rm -rf ~/.ssh/*")
    keysub = pexpect.spawn("ssh-keygen -t rsa")
    Result = 0
    while Result != 2:
        Result = keysub.expect(["Overwrite","Enter",pexpect.EOF,pexpect.TIMEOUT])
        if Result == 0:
            keysub.sendline("y")
        if Result == 1:
            keysub.sendline("\n")
        if Result == 3:
            printf("Generate SSH key failed.")
    print "Done!"

def ChangeOnosName(user,password):
    print '\033[1;31;40m'
    print "Now Changing ONOS name&password"
    print "\033[0m"
    line = open("onos/tools/build/envDefaults",'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("onos/tools/build/envDefaults",'w')
    NewFile.writelines(line)
    NewFile.close
    print "Done!"

def ChangeTestCasePara(testcase,user,password):
    print '\033[1;31;40m'
    print "Now Changing " + testcase +  " name&password"
    print "\033[0m"
    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
    print "Done!"

def RunScript(testname,masterusername,masterpassword):
    ChangeTestCasePara(testname,masterusername,masterpassword)
    runtest = "OnosSystemTest/TestON/bin/cli.py run " + testname
    os.system(runtest)
    print "Done!"

if __name__=="__main__":

    #This is the compass run machine user&pass,you need to modify
    masterusername = "root"
    masterpassword = "root"

    #The config below you don't need to care
    agentusername = "root"
    agentpassword = "root"

    print "Test Begin....."
    Gensshkey()
    AddKarafUser("10.1.0.50","karaf","karaf")
    AddEnvIntoBashrc("source onos/tools/dev/bash_profile")
    SSHlogin("10.1.0.1",masterusername,masterpassword)
    ChangeOnosName(agentusername,agentpassword)
    DownLoadCode()
    CleanEnv()
    SetEnvVar(masterpassword,agentpassword)
    RunScript("FUNCvirNetNB",masterusername,masterpassword)
    RunScript("FUNCovsdbtest",masterusername,masterpassword)