64b8ac1dea37a04437e0aad9c6596f8ce13a5d05
[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
25 """ logging configuration """
26
27 logger = logging.getLogger('onos')
28 logger.setLevel(logging.DEBUG)
29
30 ch = logging.StreamHandler()
31
32
33 formatter = logging.Formatter('%(asctime)s - %(name)s'
34                               '- %(levelname)s - %(message)s')
35
36 ch.setFormatter(formatter)
37 logger.addHandler(ch)
38
39 with open("/home/opnfv/functest/conf/config_functest.yaml") as f:
40     functest_yaml = yaml.safe_load(f)
41 f.close()
42
43 # onos parameters
44 TEST_DB = functest_yaml.get("results").get("test_db_url")
45 ONOS_REPO_PATH = functest_yaml.get("general").get("directories").get("dir_repos")
46 ONOS_CONF_DIR = functest_yaml.get("general").get("directories").get("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 sys.path.append(REPO_PATH + "testcases/")
52 import functest_utils
53
54 ONOSCI_PATH= REPO_PATH+'testcases/Controllers/ONOS/Teston/CI/'
55 starttime = datetime.datetime.now()
56
57 HOME = os.environ['HOME'] + "/"
58
59 def RunScript(testname):
60     """
61     Run ONOS Test Script
62     Parameters:
63     testname: ONOS Testcase Name
64     """
65     runtest = ONOSCI_PATH + "OnosSystemTest/TestON/bin/cli.py run " + testname
66     logger.debug( "Run script " + testname )
67     os.system(runtest)
68
69 def DownloadCodes(url="https://github.com/sunyulin/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 def GetResult():
80     LOGPATH = ONOSCI_PATH + "OnosSystemTest/TestON/logs"
81     cmd = "grep -rnh " + "Fail" + " " + LOGPATH
82     Resultbuffer = os.popen(cmd).read()
83     duration = datetime.datetime.now() - starttime
84     time.sleep(2)
85
86     if re.search("\s+[1-9]+\s+", Resultbuffer):
87         logger.debug("Testcase Fails\n" + Resultbuffer)
88         Result = "Failed"
89     else:
90         logger.debug("Testcases Success")
91         Result = "Success"
92     payload={'timestart': str(starttime),
93               'duration': str(duration),
94                 'status': Result}
95     return payload
96
97 def SetOnosIp():
98     cmd = "keystone catalog --service network | grep publicURL"
99     cmd_output = os.popen(cmd).read()
100     print cmd_output
101     OC1=re.search(r"\d+\.\d+\.\d+\.\d+",cmd_output).group()
102     os.environ['OC1'] = OC1
103     time.sleep(2)
104     logger.debug( "ONOS IP is " + OC1)
105
106 def CleanOnosTest():
107     TESTONPATH = ONOSCI_PATH + "OnosSystemTest/"
108     cmd = "rm -rf " + TESTONPATH
109     os.system(cmd)
110     time.sleep(2)
111     logger.debug( "Clean ONOS Teston" )
112
113 def main():
114
115     DownloadCodes()
116     SetOnosIp()
117     RunScript("FUNCvirNetNB")
118     RunScript("FUNCvirNetNBL3")
119
120     try:
121         logger.debug("Push result into DB")
122         # TODO check path result for the file
123         git_version = functest_utils.get_git_branch(REPO_PATH)
124         pod_name = functest_utils.get_pod_name(logger)
125         result = GetResult()
126         functest_utils.push_results_to_db(TEST_DB,
127                                           "ONOS",
128                                           logger, pod_name, git_version,
129                                           payload=result)
130     except:
131         logger.error("Error pushing results into Database")
132
133     CleanOnosTest()
134
135
136 if __name__ == '__main__':
137     main()