c8045fd12d9c6de8479507f9cfa3515d6554ed9c
[functest.git] / testcases / Controllers / ONOS / Teston / onosfunctest.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
29 parser = argparse.ArgumentParser()
30 parser.add_argument("-t", "--testcase", help="Testcase name")
31 args = parser.parse_args()
32
33
34 """ logging configuration """
35 logger = ft_logger.Logger("onos").getLogger()
36
37 # onos parameters
38 TEST_DB = ft_utils.get_functest_config("results.test_db_url")
39 ONOS_REPO_PATH = \
40     ft_utils.get_functest_config("general.directories.dir_repos")
41 ONOS_CONF_DIR = \
42     ft_utils.get_functest_config("general.directories.dir_functest_conf")
43
44 ONOSCI_PATH = ONOS_REPO_PATH + "/"
45 starttime = datetime.datetime.now()
46
47 HOME = os.environ['HOME'] + "/"
48 INSTALLER_TYPE = os.environ['INSTALLER_TYPE']
49 DEPLOY_SCENARIO = os.environ['DEPLOY_SCENARIO']
50 ONOSCI_PATH = ONOS_REPO_PATH + "/"
51 GLANCE_IMAGE_NAME = ft_utils.get_functest_config("onos_sfc.image_name")
52 GLANCE_IMAGE_FILENAME = \
53     ft_utils.get_functest_config("onos_sfc.image_file_name")
54 GLANCE_IMAGE_PATH = \
55     ft_utils.get_functest_config("general.directories.dir_functest_data") + \
56     "/" + GLANCE_IMAGE_FILENAME
57 SFC_PATH = ft_utils.FUNCTEST_REPO + "/" + \
58            ft_utils.get_functest_config("general.directories.dir_onos_sfc")
59
60
61 def RunScript(testname):
62     """
63     Run ONOS Test Script
64     Parameters:
65     testname: ONOS Testcase Name
66     """
67     runtest = ONOSCI_PATH + "onos/TestON/bin/cli.py run " + testname
68     logger.debug("Run script " + testname)
69     os.system(runtest)
70
71
72 def DownloadCodes(url="https://github.com/wuwenbin2/OnosSystemTest.git"):
73     """
74     Download Onos Teston codes
75     Parameters:
76     url: github url
77     """
78     downloadcode = "git clone " + url + " " + ONOSCI_PATH + "OnosSystemTest"
79     logger.debug("Download Onos Teston codes " + url)
80     os.system(downloadcode)
81
82
83 def GetResult():
84     LOGPATH = ONOSCI_PATH + "onos/TestON/logs"
85     cmd = "grep -rnh " + "Fail" + " " + LOGPATH
86     Resultbuffer = os.popen(cmd).read()
87     # duration = datetime.datetime.now() - starttime
88     time.sleep(2)
89
90     if re.search("\s+[1-9]+\s+", Resultbuffer):
91         logger.debug("Testcase Fails\n" + Resultbuffer)
92         # Result = "Failed"
93     else:
94         logger.debug("Testcases Success")
95         # Result = "Success"
96     # payload={'timestart': str(starttime),
97     #          'duration': str(duration),
98     #            'status': Result}
99     cmd = "grep -rnh 'Execution Time' " + LOGPATH
100     Resultbuffer = os.popen(cmd).read()
101     time1 = Resultbuffer[114:128]
102     time2 = Resultbuffer[28:42]
103     cmd = "grep -rnh 'Success Percentage' " + LOGPATH + "/FUNCvirNetNB_*"
104     Resultbuffer = os.popen(cmd).read()
105     if Resultbuffer.find('100%') >= 0:
106         result1 = 'Success'
107     else:
108         result1 = 'Failed'
109     cmd = "grep -rnh 'Success Percentage' " + LOGPATH + "/FUNCvirNetNBL3*"
110     Resultbuffer = os.popen(cmd).read()
111     if Resultbuffer.find('100%') >= 0:
112         result2 = 'Success'
113     else:
114         result2 = 'Failed'
115     status1 = []
116     status2 = []
117     cmd = "grep -rnh 'h3' " + LOGPATH + "/FUNCvirNetNB_*"
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         status1.append({'Case name:': res[i][0] + res[i][1],
125                         'Case result': res[i][2]})
126         i = i + 1
127     cmd = "grep -rnh 'h3' " + LOGPATH + "/FUNCvirNetNBL3*"
128     Resultbuffer = os.popen(cmd).read()
129     pattern = re.compile("<h3>([^-]+) - ([^-]+) - (\S*)</h3>")
130     # res = pattern.search(Resultbuffer).groups()
131     res = pattern.findall(Resultbuffer)
132     i = 0
133     for index in range(len(res)):
134         status2.append({'Case name:': res[i][0] + res[i][1],
135                         'Case result': res[i][2]})
136         i = i + 1
137     payload = {'timestart': str(starttime),
138                'FUNCvirNet': {'duration': time1,
139                               'result': result1,
140                               'status': status1},
141                'FUNCvirNetL3': {'duration': time2,
142                                 'result': result2,
143                                 'status': status2}}
144     return payload
145
146
147 def SetOnosIp():
148     cmd = "openstack catalog show network | grep publicURL"
149     cmd_output = os.popen(cmd).read()
150     OC1 = re.search(r"\d+\.\d+\.\d+\.\d+", cmd_output).group()
151     os.environ['OC1'] = OC1
152     time.sleep(2)
153     logger.debug("ONOS IP is " + OC1)
154
155
156 def SetOnosIpForJoid():
157     cmd = "env | grep SDN_CONTROLLER"
158     cmd_output = os.popen(cmd).read()
159     OC1 = re.search(r"\d+\.\d+\.\d+\.\d+", cmd_output).group()
160     os.environ['OC1'] = OC1
161     time.sleep(2)
162     logger.debug("ONOS IP is " + OC1)
163
164
165 def CleanOnosTest():
166     TESTONPATH = ONOSCI_PATH + "onos/"
167     cmd = "rm -rf " + TESTONPATH
168     os.system(cmd)
169     time.sleep(2)
170     logger.debug("Clean ONOS Teston")
171
172
173 def CreateImage():
174     glance_client = openstack_utils.get_glance_client()
175     image_id = openstack_utils.create_glance_image(glance_client,
176                                                    GLANCE_IMAGE_NAME,
177                                                    GLANCE_IMAGE_PATH)
178     EXIT_CODE = -1
179     if not image_id:
180         logger.error("Failed to create a Glance image...")
181         return(EXIT_CODE)
182     logger.debug("Image '%s' with ID=%s created successfully."
183                  % (GLANCE_IMAGE_NAME, image_id))
184
185
186 def SfcTest():
187     cmd = "python " + SFC_PATH + "Sfc.py"
188     logger.debug("Run sfc tests")
189     os.system(cmd)
190
191
192 def GetIp(type):
193     cmd = "openstack catalog show " + type + " | grep publicURL"
194     cmd_output = os.popen(cmd).read()
195     ip = re.search(r"\d+\.\d+\.\d+\.\d+", cmd_output).group()
196     return ip
197
198
199 def Replace(before, after):
200     file = "Sfc_fun.py"
201     cmd = "sed -i 's/" + before + "/" + after + "/g' " + SFC_PATH + file
202     os.system(cmd)
203
204
205 def SetSfcConf():
206     Replace("keystone_ip", GetIp("keystone"))
207     Replace("neutron_ip", GetIp("neutron"))
208     Replace("nova_ip", GetIp("nova"))
209     Replace("glance_ip", GetIp("glance"))
210     pwd = os.environ['OS_PASSWORD']
211     Replace("console", pwd)
212     creds_neutron = openstack_utils.get_credentials("neutron")
213     neutron_client = neutronclient.Client(**creds_neutron)
214     ext_net = openstack_utils.get_external_net(neutron_client)
215     Replace("admin_floating_net", ext_net)
216     logger.info("Modify configuration for SFC")
217
218
219 def OnosTest():
220     start_time = time.time()
221     stop_time = start_time
222     if INSTALLER_TYPE == "joid":
223         logger.debug("Installer is Joid")
224         SetOnosIpForJoid()
225     else:
226         SetOnosIp()
227     RunScript("FUNCvirNetNB")
228     RunScript("FUNCvirNetNBL3")
229     try:
230         logger.debug("Push ONOS results into DB")
231         # TODO check path result for the file
232         result = GetResult()
233         stop_time = time.time()
234
235         # ONOS success criteria = all tests OK
236         # i.e. FUNCvirNet & FUNCvirNetL3
237         status = "FAIL"
238         try:
239             if (result['FUNCvirNet']['result'] == "Success" and
240                     result['FUNCvirNetL3']['result'] == "Success"):
241                     status = "PASS"
242         except:
243             logger.error("Unable to set ONOS criteria")
244
245         ft_utils.push_results_to_db("functest",
246                                     "onos",
247                                     start_time,
248                                     stop_time,
249                                     status,
250                                     result)
251
252     except:
253         logger.error("Error pushing results into Database")
254
255     if status == "FAIL":
256         EXIT_CODE = -1
257         exit(EXIT_CODE)
258
259
260 def main():
261
262     if args.testcase == "sfc":
263         CreateImage()
264         SetSfcConf()
265         SfcTest()
266     else:
267         OnosTest()
268
269 if __name__ == '__main__':
270     main()