2d22baec9901bf759a56fcfcacf2b8cb2a97c397
[functest.git] / testcases / Controllers / ONOS / Teston / CI / 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 os
18 import time
19 import sys
20 import logging
21 import yaml
22 import datetime
23 import re
24 import argparse
25
26 parser = argparse.ArgumentParser()
27 parser.add_argument("-i", "--installer", help="Installer type")
28 args = parser.parse_args()
29 """ logging configuration """
30
31 logger = logging.getLogger('onos')
32 logger.setLevel(logging.DEBUG)
33
34 ch = logging.StreamHandler()
35
36
37 formatter = logging.Formatter('%(asctime)s - %(name)s'
38                               '- %(levelname)s - %(message)s')
39
40 ch.setFormatter(formatter)
41 logger.addHandler(ch)
42
43 with open("/home/opnfv/functest/conf/config_functest.yaml") as f:
44     functest_yaml = yaml.safe_load(f)
45 f.close()
46
47 # onos parameters
48 TEST_DB = functest_yaml.get("results").get("test_db_url")
49 ONOS_REPO_PATH = functest_yaml.get("general").get("directories").get("dir_repos")
50 ONOS_CONF_DIR = functest_yaml.get("general").get("directories").get("dir_functest_conf")
51 REPO_PATH = ONOS_REPO_PATH + '/functest/'
52 if not os.path.exists(REPO_PATH):
53     logger.error("Functest repository directory not found '%s'" % REPO_PATH)
54     exit(-1)
55 sys.path.append(REPO_PATH + "testcases/")
56 import functest_utils
57
58 ONOSCI_PATH= REPO_PATH+'testcases/Controllers/ONOS/Teston/CI/'
59 starttime = datetime.datetime.now()
60
61 HOME = os.environ['HOME'] + "/"
62
63 def RunScript(testname):
64     """
65     Run ONOS Test Script
66     Parameters:
67     testname: ONOS Testcase Name
68     """
69     runtest = ONOSCI_PATH + "OnosSystemTest/TestON/bin/cli.py run " + testname
70     logger.debug( "Run script " + testname )
71     os.system(runtest)
72
73 def DownloadCodes(url="https://github.com/sunyulin/OnosSystemTest.git"):
74     """
75     Download Onos Teston codes
76     Parameters:
77     url: github url
78     """
79     downloadcode = "git clone " + url + " " + ONOSCI_PATH + "OnosSystemTest"
80     logger.debug( "Download Onos Teston codes " + url)
81     os.system(downloadcode)
82
83 def GetResult():
84     LOGPATH = ONOSCI_PATH + "OnosSystemTest/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     payload={'FUNCvirNet':{'duration': time1,
116                            'result': result1},
117              'FUNCvirNetL3':{'duration': time2,
118                            'result': result2}}
119     return payload
120
121 def SetOnosIp():
122     cmd = "keystone catalog --service network | grep publicURL"
123     cmd_output = os.popen(cmd).read()
124     OC1=re.search(r"\d+\.\d+\.\d+\.\d+",cmd_output).group()
125     os.environ['OC1'] = OC1
126     time.sleep(2)
127     logger.debug( "ONOS IP is " + OC1)
128
129 def SetOnosIpForJoid():
130     cmd = "env | grep SDN_CONTROLLER"
131     cmd_output = os.popen(cmd).read()
132     OC1=re.search(r"\d+\.\d+\.\d+\.\d+",cmd_output).group()
133     os.environ['OC1'] = OC1
134     time.sleep(2)
135     logger.debug( "ONOS IP is " + OC1)
136
137 def CleanOnosTest():
138     TESTONPATH = ONOSCI_PATH + "OnosSystemTest/"
139     cmd = "rm -rf " + TESTONPATH
140     os.system(cmd)
141     time.sleep(2)
142     logger.debug( "Clean ONOS Teston" )
143
144 def main():
145
146     DownloadCodes()
147     if args.installer == "joid":
148         logger.debug( "Installer is Joid")
149         SetOnosIpForJoid()
150     else:
151         SetOnosIp()
152     RunScript("FUNCvirNetNB")
153     RunScript("FUNCvirNetNBL3")
154
155     try:
156         logger.debug("Push result into DB")
157         # TODO check path result for the file
158         git_version = functest_utils.get_git_branch(REPO_PATH)
159         pod_name = functest_utils.get_pod_name(logger)
160         result = GetResult()
161         functest_utils.push_results_to_db(TEST_DB,
162                                           "ONOS",
163                                           logger, pod_name, git_version,
164                                           payload=result)
165     except:
166         logger.error("Error pushing results into Database")
167
168     CleanOnosTest()
169
170
171 if __name__ == '__main__':
172     main()