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