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