Merge "change cloudify cli version from 3.3 to 3.3.1"
[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 import functest.utils.functest_logger as ft_logger
25 import functest.utils.functest_utils as functest_utils
26
27 parser = argparse.ArgumentParser()
28 parser.add_argument("-i", "--installer", help="Installer type")
29 args = parser.parse_args()
30 """ logging configuration """
31 logger = ft_logger.Logger("onos").getLogger()
32
33 with open(os.environ["CONFIG_FUNCTEST_YAML"]) as f:
34     functest_yaml = yaml.safe_load(f)
35 f.close()
36
37 # onos parameters
38 TEST_DB = functest_yaml.get("results").get("test_db_url")
39 ONOS_REPO_PATH = functest_yaml.get("general").get("directories").get(
40     "dir_repos")
41 ONOS_CONF_DIR = functest_yaml.get("general").get("directories").get(
42     "dir_functest_conf")
43 REPO_PATH = ONOS_REPO_PATH + '/functest/'
44 if not os.path.exists(REPO_PATH):
45     logger.error("Functest repository directory not found '%s'" % REPO_PATH)
46     exit(-1)
47
48 ONOSCI_PATH = REPO_PATH + 'testcases/Controllers/ONOS/Teston/'
49 starttime = datetime.datetime.now()
50
51 HOME = os.environ['HOME'] + "/"
52
53
54 def RunScript(testname):
55     """
56     Run ONOS Test Script
57     Parameters:
58     testname: ONOS Testcase Name
59     """
60     runtest = ONOSCI_PATH + "OnosSystemTest/TestON/bin/cli.py run " + testname
61     logger.debug("Run script " + testname)
62     os.system(runtest)
63
64
65 def DownloadCodes(url="https://github.com/sunyulin/OnosSystemTest.git"):
66     """
67     Download Onos Teston codes
68     Parameters:
69     url: github url
70     """
71     downloadcode = "git clone " + url + " " + ONOSCI_PATH + "OnosSystemTest"
72     logger.debug("Download Onos Teston codes " + url)
73     os.system(downloadcode)
74
75
76 def GetResult():
77     LOGPATH = ONOSCI_PATH + "OnosSystemTest/TestON/logs"
78     cmd = "grep -rnh " + "Fail" + " " + LOGPATH
79     Resultbuffer = os.popen(cmd).read()
80     # duration = datetime.datetime.now() - starttime
81     time.sleep(2)
82
83     if re.search("\s+[1-9]+\s+", Resultbuffer):
84         logger.debug("Testcase Fails\n" + Resultbuffer)
85         # Result = "Failed"
86     else:
87         logger.debug("Testcases Success")
88         # Result = "Success"
89     # payload={'timestart': str(starttime),
90     #          'duration': str(duration),
91     #            'status': Result}
92     cmd = "grep -rnh 'Execution Time' " + LOGPATH
93     Resultbuffer = os.popen(cmd).read()
94     time1 = Resultbuffer[114:128]
95     time2 = Resultbuffer[28:42]
96     cmd = "grep -rnh 'Success Percentage' " + LOGPATH + "/FUNCvirNetNB_*"
97     Resultbuffer = os.popen(cmd).read()
98     if Resultbuffer.find('100%') >= 0:
99         result1 = 'Success'
100     else:
101         result1 = 'Failed'
102     cmd = "grep -rnh 'Success Percentage' " + LOGPATH + "/FUNCvirNetNBL3*"
103     Resultbuffer = os.popen(cmd).read()
104     if Resultbuffer.find('100%') >= 0:
105         result2 = 'Success'
106     else:
107         result2 = 'Failed'
108     status1 = []
109     status2 = []
110     cmd = "grep -rnh 'h3' " + LOGPATH + "/FUNCvirNetNB_*"
111     Resultbuffer = os.popen(cmd).read()
112     pattern = re.compile("<h3>([^-]+) - ([^-]+) - (\S*)</h3>")
113     # res = pattern.search(Resultbuffer).groups()
114     res = pattern.findall(Resultbuffer)
115     i = 0
116     for index in range(len(res)):
117         status1.append({'Case name:': res[i][0] + res[i][1],
118                         'Case result': res[i][2]})
119         i = i + 1
120     cmd = "grep -rnh 'h3' " + LOGPATH + "/FUNCvirNetNBL3*"
121     Resultbuffer = os.popen(cmd).read()
122     pattern = re.compile("<h3>([^-]+) - ([^-]+) - (\S*)</h3>")
123     # res = pattern.search(Resultbuffer).groups()
124     res = pattern.findall(Resultbuffer)
125     i = 0
126     for index in range(len(res)):
127         status2.append({'Case name:': res[i][0] + res[i][1],
128                         'Case result': res[i][2]})
129         i = i + 1
130     payload = {'timestart': str(starttime),
131                'FUNCvirNet': {'duration': time1,
132                               'result': result1,
133                               'status': status1},
134                'FUNCvirNetL3': {'duration': time2,
135                                 'result': result2,
136                                 'status': status2}}
137     return payload
138
139
140 def SetOnosIp():
141     cmd = "openstack catalog show network | grep publicURL"
142     cmd_output = os.popen(cmd).read()
143     OC1 = re.search(r"\d+\.\d+\.\d+\.\d+", cmd_output).group()
144     os.environ['OC1'] = OC1
145     time.sleep(2)
146     logger.debug("ONOS IP is " + OC1)
147
148
149 def SetOnosIpForJoid():
150     cmd = "env | grep SDN_CONTROLLER"
151     cmd_output = os.popen(cmd).read()
152     OC1 = re.search(r"\d+\.\d+\.\d+\.\d+", cmd_output).group()
153     os.environ['OC1'] = OC1
154     time.sleep(2)
155     logger.debug("ONOS IP is " + OC1)
156
157
158 def CleanOnosTest():
159     TESTONPATH = ONOSCI_PATH + "OnosSystemTest/"
160     cmd = "rm -rf " + TESTONPATH
161     os.system(cmd)
162     time.sleep(2)
163     logger.debug("Clean ONOS Teston")
164
165
166 def main():
167     start_time = time.time()
168     stop_time = start_time
169     DownloadCodes()
170     if args.installer == "joid":
171         logger.debug("Installer is Joid")
172         SetOnosIpForJoid()
173     else:
174         SetOnosIp()
175     RunScript("FUNCvirNetNB")
176     RunScript("FUNCvirNetNBL3")
177
178     try:
179         logger.debug("Push ONOS results into DB")
180         # TODO check path result for the file
181         result = GetResult()
182         stop_time = time.time()
183
184         # ONOS success criteria = all tests OK
185         # i.e. FUNCvirNet & FUNCvirNetL3
186         status = "failed"
187         try:
188             if (result['FUNCvirNet']['result'] == "Success" and
189                     result['FUNCvirNetL3']['result'] == "Success"):
190                     status = "passed"
191         except:
192             logger.error("Unable to set ONOS criteria")
193
194         functest_utils.push_results_to_db("functest",
195                                           "onos",
196                                           logger,
197                                           start_time,
198                                           stop_time,
199                                           status,
200                                           result)
201
202     except:
203         logger.error("Error pushing results into Database")
204
205     CleanOnosTest()
206
207
208 if __name__ == '__main__':
209     main()