file/dir renaming for consistency
[functest.git] / functest / opnfv_tests / sdn / onos / teston / onos.py
1 """
2 Description: This test is to run onos Teston VTN scripts
3
4 List of test cases:
5 CASE1 - Northbound NBI test network/subnet/ports
6 CASE2 - Ovsdb test&Default configuration&Vm go online
7
8 lanqinglong@huawei.com
9 #
10 # All rights reserved. This program and the accompanying materials
11 # are made available under the terms of the Apache License, Version 2.0
12 # which accompanies this distribution, and is available at
13 # http://www.apache.org/licenses/LICENSE-2.0
14 #
15 """
16
17 import datetime
18 import os
19 import re
20 import time
21
22 import argparse
23 from neutronclient.v2_0 import client as neutronclient
24
25 import functest.utils.functest_logger as ft_logger
26 import functest.utils.functest_utils as ft_utils
27 import functest.utils.openstack_utils as openstack_utils
28 import functest.utils.functest_constants as ft_constants
29
30
31 parser = argparse.ArgumentParser()
32 parser.add_argument("-t", "--testcase", help="Testcase name")
33 args = parser.parse_args()
34
35
36 """ logging configuration """
37 logger = ft_logger.Logger("onos").getLogger()
38
39 # onos parameters
40 ONOSCI_PATH = ft_constants.REPOS_DIR + "/"
41 starttime = datetime.datetime.now()
42
43 INSTALLER_TYPE = ft_constants.CI_INSTALLER_TYPE
44 ONOS_SFC_IMAGE_NAME = ft_constants.ONOS_SFC_IMAGE_NAME
45 ONOS_SFC_IMAGE_PATH = os.path.join(ft_constants.FUNCTEST_DATA_DIR,
46                                    ft_constants.ONOS_SFC_IMAGE_FILENAME)
47 ONOS_SFC_PATH = os.path.join(ft_constants.FUNCTEST_REPO_DIR,
48                              ft_constants.ONOS_SFC_RELATIVE_PATH)
49
50
51 def RunScript(testname):
52     """
53     Run ONOS Test Script
54     Parameters:
55     testname: ONOS Testcase Name
56     """
57     runtest = ONOSCI_PATH + "onos/TestON/bin/cli.py run " + testname
58     logger.debug("Run script " + testname)
59     os.system(runtest)
60
61
62 def DownloadCodes(url="https://github.com/wuwenbin2/OnosSystemTest.git"):
63     """
64     Download Onos Teston codes
65     Parameters:
66     url: github url
67     """
68     downloadcode = "git clone " + url + " " + ONOSCI_PATH + "OnosSystemTest"
69     logger.debug("Download Onos Teston codes " + url)
70     os.system(downloadcode)
71
72
73 def GetResult():
74     LOGPATH = ONOSCI_PATH + "onos/TestON/logs"
75     cmd = "grep -rnh " + "Fail" + " " + LOGPATH
76     Resultbuffer = os.popen(cmd).read()
77     # duration = datetime.datetime.now() - starttime
78     time.sleep(2)
79
80     if re.search("\s+[1-9]+\s+", Resultbuffer):
81         logger.debug("Testcase Fails\n" + Resultbuffer)
82         # Result = "Failed"
83     else:
84         logger.debug("Testcases Success")
85         # Result = "Success"
86     # payload={'timestart': str(starttime),
87     #          'duration': str(duration),
88     #            'status': Result}
89     cmd = "grep -rnh 'Execution Time' " + LOGPATH
90     Resultbuffer = os.popen(cmd).read()
91     time1 = Resultbuffer[114:128]
92     time2 = Resultbuffer[28:42]
93     cmd = "grep -rnh 'Success Percentage' " + LOGPATH + "/FUNCvirNetNB_*"
94     Resultbuffer = os.popen(cmd).read()
95     if Resultbuffer.find('100%') >= 0:
96         result1 = 'Success'
97     else:
98         result1 = 'Failed'
99     cmd = "grep -rnh 'Success Percentage' " + LOGPATH + "/FUNCvirNetNBL3*"
100     Resultbuffer = os.popen(cmd).read()
101     if Resultbuffer.find('100%') >= 0:
102         result2 = 'Success'
103     else:
104         result2 = 'Failed'
105     status1 = []
106     status2 = []
107     cmd = "grep -rnh 'h3' " + LOGPATH + "/FUNCvirNetNB_*"
108     Resultbuffer = os.popen(cmd).read()
109     pattern = re.compile("<h3>([^-]+) - ([^-]+) - (\S*)</h3>")
110     # res = pattern.search(Resultbuffer).groups()
111     res = pattern.findall(Resultbuffer)
112     i = 0
113     for index in range(len(res)):
114         status1.append({'Case name:': res[i][0] + res[i][1],
115                         'Case result': res[i][2]})
116         i = i + 1
117     cmd = "grep -rnh 'h3' " + LOGPATH + "/FUNCvirNetNBL3*"
118     Resultbuffer = os.popen(cmd).read()
119     pattern = re.compile("<h3>([^-]+) - ([^-]+) - (\S*)</h3>")
120     # res = pattern.search(Resultbuffer).groups()
121     res = pattern.findall(Resultbuffer)
122     i = 0
123     for index in range(len(res)):
124         status2.append({'Case name:': res[i][0] + res[i][1],
125                         'Case result': res[i][2]})
126         i = i + 1
127     payload = {'timestart': str(starttime),
128                'FUNCvirNet': {'duration': time1,
129                               'result': result1,
130                               'status': status1},
131                'FUNCvirNetL3': {'duration': time2,
132                                 'result': result2,
133                                 'status': status2}}
134     return payload
135
136
137 def SetOnosIp():
138     cmd = "openstack catalog show network | grep publicURL"
139     cmd_output = os.popen(cmd).read()
140     OC1 = re.search(r"\d+\.\d+\.\d+\.\d+", cmd_output).group()
141     os.environ['OC1'] = OC1
142     time.sleep(2)
143     logger.debug("ONOS IP is " + OC1)
144
145
146 def SetOnosIpForJoid():
147     cmd = "env | grep SDN_CONTROLLER"
148     cmd_output = os.popen(cmd).read()
149     OC1 = re.search(r"\d+\.\d+\.\d+\.\d+", cmd_output).group()
150     os.environ['OC1'] = OC1
151     time.sleep(2)
152     logger.debug("ONOS IP is " + OC1)
153
154
155 def CleanOnosTest():
156     TESTONPATH = ONOSCI_PATH + "onos/"
157     cmd = "rm -rf " + TESTONPATH
158     os.system(cmd)
159     time.sleep(2)
160     logger.debug("Clean ONOS Teston")
161
162
163 def CreateImage():
164     glance_client = openstack_utils.get_glance_client()
165     image_id = openstack_utils.create_glance_image(glance_client,
166                                                    ONOS_SFC_IMAGE_NAME,
167                                                    ONOS_SFC_IMAGE_PATH)
168     EXIT_CODE = -1
169     if not image_id:
170         logger.error("Failed to create a Glance image...")
171         return(EXIT_CODE)
172     logger.debug("Image '%s' with ID=%s created successfully."
173                  % (ONOS_SFC_IMAGE_NAME, image_id))
174
175
176 def SfcTest():
177     cmd = "python " + ONOS_SFC_PATH + "/Sfc.py"
178     logger.debug("Run sfc tests")
179     os.system(cmd)
180
181
182 def GetIp(type):
183     cmd = "openstack catalog show " + type + " | grep publicURL"
184     cmd_output = os.popen(cmd).read()
185     ip = re.search(r"\d+\.\d+\.\d+\.\d+", cmd_output).group()
186     return ip
187
188
189 def Replace(before, after):
190     file = "/Sfc_fun.py"
191     cmd = "sed -i 's/" + before + "/" + after + "/g' " + ONOS_SFC_PATH + file
192     os.system(cmd)
193
194
195 def SetSfcConf():
196     Replace("keystone_ip", GetIp("keystone"))
197     Replace("neutron_ip", GetIp("neutron"))
198     Replace("nova_ip", GetIp("nova"))
199     Replace("glance_ip", GetIp("glance"))
200     pwd = ft_constants.OS_PASSWORD
201     Replace("console", pwd)
202     creds_neutron = openstack_utils.get_credentials("neutron")
203     neutron_client = neutronclient.Client(**creds_neutron)
204     ext_net = openstack_utils.get_external_net(neutron_client)
205     Replace("admin_floating_net", ext_net)
206     logger.info("Modify configuration for SFC")
207
208
209 def OnosTest():
210     start_time = time.time()
211     stop_time = start_time
212     if INSTALLER_TYPE == "joid":
213         logger.debug("Installer is Joid")
214         SetOnosIpForJoid()
215     else:
216         SetOnosIp()
217     RunScript("FUNCvirNetNB")
218     RunScript("FUNCvirNetNBL3")
219     try:
220         logger.debug("Push ONOS results into DB")
221         # TODO check path result for the file
222         result = GetResult()
223         stop_time = time.time()
224
225         # ONOS success criteria = all tests OK
226         # i.e. FUNCvirNet & FUNCvirNetL3
227         status = "FAIL"
228         try:
229             if (result['FUNCvirNet']['result'] == "Success" and
230                     result['FUNCvirNetL3']['result'] == "Success"):
231                     status = "PASS"
232         except:
233             logger.error("Unable to set ONOS criteria")
234
235         ft_utils.push_results_to_db("functest",
236                                     "onos",
237                                     start_time,
238                                     stop_time,
239                                     status,
240                                     result)
241
242     except:
243         logger.error("Error pushing results into Database")
244
245     if status == "FAIL":
246         EXIT_CODE = -1
247         exit(EXIT_CODE)
248
249
250 def main():
251
252     if args.testcase == "sfc":
253         CreateImage()
254         SetSfcConf()
255         SfcTest()
256     else:
257         OnosTest()
258
259
260 if __name__ == '__main__':
261     main()