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