Add reporting to DB in OpenDaylightTesting
[functest.git] / testcases / Controllers / ODL / OpenDaylightTesting.py
1 #!/usr/bin/python
2
3 import argparse
4 import fileinput
5 import os
6 import re
7 import shutil
8 import sys
9
10 from robot import run
11 from robot.api import ExecutionResult, ResultVisitor
12 from robot.utils.robottime import timestamp_to_secs
13
14 import functest.utils.functest_logger as ft_logger
15 import functest.utils.functest_utils as ft_utils
16
17
18 class ODLResultVisitor(ResultVisitor):
19
20     def __init__(self):
21         self._data = []
22
23     def visit_test(self, test):
24         output = {}
25         output['name'] = test.name
26         output['parent'] = test.parent.name
27         output['status'] = test.status
28         output['startime'] = test.starttime
29         output['endtime'] = test.endtime
30         output['critical'] = test.critical
31         output['text'] = test.message
32         output['elapsedtime'] = test.elapsedtime
33         self._data.append(output)
34
35     def get_data(self):
36         return self._data
37
38
39 class ODLTestCases:
40
41     repos = "/home/opnfv/repos/"
42     odl_test_repo = repos + "odl_test/"
43     neutron_suite_dir = odl_test_repo + "csit/suites/openstack/neutron/"
44     basic_suite_dir = odl_test_repo + "csit/suites/integration/basic/"
45     res_dir = '/home/opnfv/functest/results/odl/'
46     logger = ft_logger.Logger("opendaylight").getLogger()
47
48     @classmethod
49     def copy_opnf_testcases(cls):
50         opnfv_testcases_dir = (os.path.dirname(os.path.abspath(__file__)) +
51                                "/custom_tests/neutron/")
52         files = [opnfv_testcases_dir + "001__reachability.robot",
53                  opnfv_testcases_dir + "040__delete_ports.robot",
54                  opnfv_testcases_dir + "050__delete_subnets.robot",
55                  opnfv_testcases_dir + "060__delete_networks.robot"]
56         for f in files:
57             try:
58                 shutil.copy(f, cls.neutron_suite_dir)
59             except IOError as e:
60                 cls.logger.error(
61                     "Cannot copy OPNFV's testcases to ODL directory", e)
62                 return False
63         return True
64
65     @classmethod
66     def set_robotframework_vars(cls, odlusername="admin", odlpassword="admin"):
67         odl_variables_files = cls.odl_test_repo + 'csit/variables/Variables.py'
68         try:
69             cls.logger.debug(cls.neutron_suite_dir + '__init__.robot')
70             for line in fileinput.input(odl_variables_files,
71                                         inplace=True):
72                 print re.sub("AUTH = .*",
73                              ("AUTH = [u'" + odlusername + "', u'" +
74                               odlpassword + "']"),
75                              line.rstrip())
76             return True
77         except Exception as e:
78             cls.logger.error("Cannot set ODL creds", e)
79             return False
80
81     @classmethod
82     def run(cls, **kwargs):
83         dirs = [cls.basic_suite_dir, cls.neutron_suite_dir]
84         try:
85             odlusername = kwargs['odlusername']
86             odlpassword = kwargs['odlpassword']
87             variables = ['KEYSTONE:' + kwargs['keystoneip'],
88                          'NEUTRON:' + kwargs['neutronip'],
89                          'OSUSERNAME:"' + kwargs['osusername'] + '"',
90                          'OSTENANTNAME:"' + kwargs['ostenantname'] + '"',
91                          'OSPASSWORD:"' + kwargs['ospassword'] + '"',
92                          'ODL_SYSTEM_IP:' + kwargs['odlip'],
93                          'PORT:' + kwargs['odlwebport'],
94                          'RESTCONFPORT:' + kwargs['odlrestconfport']]
95         except KeyError as e:
96             cls.logger.error("Cannot run ODL testcases. Please check", e)
97             return False
98         if (cls.copy_opnf_testcases() and
99                 cls.set_robotframework_vars(odlusername, odlpassword)):
100             try:
101                 os.makedirs(cls.res_dir)
102             except OSError:
103                 pass
104             stdout_file = cls.res_dir + 'stdout.txt'
105             with open(stdout_file, 'w') as stdout:
106                 result = run(*dirs, variable=variables,
107                              output=cls.res_dir + 'output.xml',
108                              log=cls.res_dir + 'log.html',
109                              report=cls.res_dir + 'report.html',
110                              stdout=stdout)
111
112             with open(stdout_file, 'r') as stdout:
113                 cls.logger.info("\n" + stdout.read())
114
115             return result
116         else:
117             return False
118
119     @classmethod
120     def push_to_db(cls):
121         result = ExecutionResult(cls.res_dir + 'output.xml')
122         visitor = ODLResultVisitor()
123         result.visit(visitor)
124         start_time = timestamp_to_secs(result.suite.starttime)
125         stop_time = timestamp_to_secs(result.suite.endtime)
126         details = {}
127         details['description'] = result.suite.name
128         details['tests'] = visitor.get_data()
129         if not ft_utils.push_results_to_db(
130                 "functest", "odl", None, start_time, stop_time,
131                 result.suite.status, details):
132             cls.logger.error("Cannot push ODL results to DB")
133             return False
134         else:
135             return True
136
137
138 if __name__ == '__main__':
139     parser = argparse.ArgumentParser()
140     parser.add_argument('-k', '--keystoneip',
141                         help='Keystone IP',
142                         default='127.0.0.1')
143     parser.add_argument('-n', '--neutronip',
144                         help='Neutron IP',
145                         default='127.0.0.1')
146     parser.add_argument('-a', '--osusername',
147                         help='Username for OpenStack',
148                         default='admin')
149     parser.add_argument('-b', '--ostenantname',
150                         help='Tenantname for OpenStack',
151                         default='admin')
152     parser.add_argument('-c', '--ospassword',
153                         help='Password for OpenStack',
154                         default='admin')
155     parser.add_argument('-o', '--odlip',
156                         help='OpenDaylight IP',
157                         default='127.0.0.1')
158     parser.add_argument('-w', '--odlwebport',
159                         help='OpenDaylight Web Portal Port',
160                         default='8080')
161     parser.add_argument('-r', '--odlrestconfport',
162                         help='OpenDaylight RESTConf Port',
163                         default='8181')
164     parser.add_argument('-d', '--odlusername',
165                         help='Username for ODL',
166                         default='admin')
167     parser.add_argument('-e', '--odlpassword',
168                         help='Password for ODL',
169                         default='admin')
170     parser.add_argument('-p', '--pushtodb',
171                         help='Push results to DB',
172                         action='store_true')
173
174     args = vars(parser.parse_args())
175     ODLTestCases.run(**args)
176     if args['pushtodb']:
177         sys.exit(not ODLTestCases.push_to_db())
178     sys.exit(os.EX_OK)