Merge "Remove redundant tempest cleanup utility"
[functest.git] / functest / opnfv_tests / sdn / odl / odl.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016 Orange and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 """Define classes required to run ODL suites.
11
12 It has been designed for any context. But helpers are given for
13 running test suites in OPNFV environment.
14
15 Example:
16         $ python odl.py
17 """
18
19 from __future__ import division
20
21 import argparse
22 import errno
23 import fileinput
24 import logging
25 import os
26 import re
27 import sys
28
29 import robot.api
30 from robot.errors import RobotError
31 import robot.run
32 from robot.utils.robottime import timestamp_to_secs
33 from six import StringIO
34 from six.moves import urllib
35
36 from functest.core import testcase
37 from functest.utils import constants
38 import functest.utils.openstack_utils as op_utils
39
40 __author__ = "Cedric Ollivier <cedric.ollivier@orange.com>"
41
42
43 class ODLResultVisitor(robot.api.ResultVisitor):
44     """Visitor to get result details."""
45
46     def __init__(self):
47         self._data = []
48
49     def visit_test(self, test):
50         output = {}
51         output['name'] = test.name
52         output['parent'] = test.parent.name
53         output['status'] = test.status
54         output['starttime'] = test.starttime
55         output['endtime'] = test.endtime
56         output['critical'] = test.critical
57         output['text'] = test.message
58         output['elapsedtime'] = test.elapsedtime
59         self._data.append(output)
60
61     def get_data(self):
62         """Get the details of the result."""
63         return self._data
64
65
66 class ODLTests(testcase.TestCase):
67     """ODL test runner."""
68
69     odl_test_repo = constants.CONST.__getattribute__('dir_repo_odl_test')
70     neutron_suite_dir = os.path.join(odl_test_repo,
71                                      "csit/suites/openstack/neutron")
72     basic_suite_dir = os.path.join(odl_test_repo,
73                                    "csit/suites/integration/basic")
74     default_suites = [basic_suite_dir, neutron_suite_dir]
75     res_dir = os.path.join(
76         constants.CONST.__getattribute__('dir_results'), 'odl')
77     __logger = logging.getLogger(__name__)
78
79     @classmethod
80     def set_robotframework_vars(cls, odlusername="admin", odlpassword="admin"):
81         """Set credentials in csit/variables/Variables.py.
82
83         Returns:
84             True if credentials are set.
85             False otherwise.
86         """
87         odl_variables_files = os.path.join(cls.odl_test_repo,
88                                            'csit/variables/Variables.py')
89         try:
90             for line in fileinput.input(odl_variables_files,
91                                         inplace=True):
92                 print(re.sub("AUTH = .*",
93                              ("AUTH = [u'" + odlusername + "', u'" +
94                               odlpassword + "']"),
95                              line.rstrip()))
96             return True
97         except Exception as ex:  # pylint: disable=broad-except
98             cls.__logger.error("Cannot set ODL creds: %s", str(ex))
99             return False
100
101     def parse_results(self):
102         """Parse output.xml and get the details in it."""
103         xml_file = os.path.join(self.res_dir, 'output.xml')
104         result = robot.api.ExecutionResult(xml_file)
105         visitor = ODLResultVisitor()
106         result.visit(visitor)
107         try:
108             self.result = 100 * (
109                 result.suite.statistics.critical.passed /
110                 result.suite.statistics.critical.total)
111         except ZeroDivisionError:
112             self.__logger.error("No test has been run")
113         self.start_time = timestamp_to_secs(result.suite.starttime)
114         self.stop_time = timestamp_to_secs(result.suite.endtime)
115         self.details = {}
116         self.details['description'] = result.suite.name
117         self.details['tests'] = visitor.get_data()
118
119     def run_suites(self, suites=None, **kwargs):
120         """Run the test suites
121
122         It has been designed to be called in any context.
123         It requires the following keyword arguments:
124
125            * odlusername,
126            * odlpassword,
127            * osauthurl,
128            * neutronip,
129            * osusername,
130            * ostenantname,
131            * ospassword,
132            * odlip,
133            * odlwebport,
134            * odlrestconfport.
135
136         Here are the steps:
137            * set all RobotFramework_variables,
138            * create the output directories if required,
139            * get the results in output.xml,
140            * delete temporary files.
141
142         Args:
143             kwargs: Arbitrary keyword arguments.
144
145         Returns:
146             EX_OK if all suites ran well.
147             EX_RUN_ERROR otherwise.
148         """
149         try:
150             if not suites:
151                 suites = self.default_suites
152             odlusername = kwargs['odlusername']
153             odlpassword = kwargs['odlpassword']
154             osauthurl = kwargs['osauthurl']
155             keystoneip = urllib.parse.urlparse(osauthurl).hostname
156             variables = ['KEYSTONE:' + keystoneip,
157                          'NEUTRON:' + kwargs['neutronip'],
158                          'OS_AUTH_URL:"' + osauthurl + '"',
159                          'OSUSERNAME:"' + kwargs['osusername'] + '"',
160                          'OSTENANTNAME:"' + kwargs['ostenantname'] + '"',
161                          'OSPASSWORD:"' + kwargs['ospassword'] + '"',
162                          'ODL_SYSTEM_IP:' + kwargs['odlip'],
163                          'PORT:' + kwargs['odlwebport'],
164                          'RESTCONFPORT:' + kwargs['odlrestconfport']]
165         except KeyError as ex:
166             self.__logger.error("Cannot run ODL testcases. Please check "
167                                 "%s", str(ex))
168             return self.EX_RUN_ERROR
169         if self.set_robotframework_vars(odlusername, odlpassword):
170             try:
171                 os.makedirs(self.res_dir)
172             except OSError as ex:
173                 if ex.errno != errno.EEXIST:
174                     self.__logger.exception(
175                         "Cannot create %s", self.res_dir)
176                     return self.EX_RUN_ERROR
177             output_dir = os.path.join(self.res_dir, 'output.xml')
178             stream = StringIO()
179             robot.run(*suites, variable=variables, output=output_dir,
180                       log='NONE', report='NONE', stdout=stream)
181             self.__logger.info("\n" + stream.getvalue())
182             self.__logger.info("ODL results were successfully generated")
183             try:
184                 self.parse_results()
185                 self.__logger.info("ODL results were successfully parsed")
186             except RobotError as ex:
187                 self.__logger.error("Run tests before publishing: %s",
188                                     ex.message)
189                 return self.EX_RUN_ERROR
190             return self.EX_OK
191         else:
192             return self.EX_RUN_ERROR
193
194     def run(self, **kwargs):
195         """Run suites in OPNFV environment
196
197         It basically check env vars to call main() with the keywords
198         required.
199
200         Args:
201             kwargs: Arbitrary keyword arguments.
202
203         Returns:
204             EX_OK if all suites ran well.
205             EX_RUN_ERROR otherwise.
206         """
207         try:
208             suites = self.default_suites
209             try:
210                 suites = kwargs["suites"]
211             except KeyError:
212                 pass
213             neutron_url = op_utils.get_endpoint(service_type='network')
214             kwargs = {'neutronip': urllib.parse.urlparse(neutron_url).hostname}
215             kwargs['odlip'] = kwargs['neutronip']
216             kwargs['odlwebport'] = '8080'
217             kwargs['odlrestconfport'] = '8181'
218             kwargs['odlusername'] = 'admin'
219             kwargs['odlpassword'] = 'admin'
220             installer_type = None
221             if 'INSTALLER_TYPE' in os.environ:
222                 installer_type = os.environ['INSTALLER_TYPE']
223             kwargs['osusername'] = os.environ['OS_USERNAME']
224             kwargs['ostenantname'] = os.environ['OS_TENANT_NAME']
225             kwargs['osauthurl'] = os.environ['OS_AUTH_URL']
226             kwargs['ospassword'] = os.environ['OS_PASSWORD']
227             if installer_type == 'fuel':
228                 kwargs['odlwebport'] = '8181'
229                 kwargs['odlrestconfport'] = '8282'
230             elif installer_type == 'apex' or installer_type == 'netvirt':
231                 kwargs['odlip'] = os.environ['SDN_CONTROLLER_IP']
232                 kwargs['odlwebport'] = '8081'
233                 kwargs['odlrestconfport'] = '8081'
234             elif installer_type == 'joid':
235                 kwargs['odlip'] = os.environ['SDN_CONTROLLER']
236             elif installer_type == 'compass':
237                 kwargs['odlrestconfport'] = '8080'
238             elif installer_type == 'daisy':
239                 kwargs['odlip'] = os.environ['SDN_CONTROLLER_IP']
240                 kwargs['odlwebport'] = '8181'
241                 kwargs['odlrestconfport'] = '8087'
242             else:
243                 kwargs['odlip'] = os.environ['SDN_CONTROLLER_IP']
244         except KeyError as ex:
245             self.__logger.error("Cannot run ODL testcases. "
246                                 "Please check env var: "
247                                 "%s", str(ex))
248             return self.EX_RUN_ERROR
249         except Exception:  # pylint: disable=broad-except
250             self.__logger.exception("Cannot run ODL testcases.")
251             return self.EX_RUN_ERROR
252
253         return self.run_suites(suites, **kwargs)
254
255
256 class ODLParser(object):  # pylint: disable=too-few-public-methods
257     """Parser to run ODL test suites."""
258
259     def __init__(self):
260         self.parser = argparse.ArgumentParser()
261         self.parser.add_argument(
262             '-n', '--neutronip', help='Neutron IP',
263             default='127.0.0.1')
264         self.parser.add_argument(
265             '-k', '--osauthurl', help='OS_AUTH_URL as defined by OpenStack',
266             default='http://127.0.0.1:5000/v2.0')
267         self.parser.add_argument(
268             '-a', '--osusername', help='Username for OpenStack',
269             default='admin')
270         self.parser.add_argument(
271             '-b', '--ostenantname', help='Tenantname for OpenStack',
272             default='admin')
273         self.parser.add_argument(
274             '-c', '--ospassword', help='Password for OpenStack',
275             default='admin')
276         self.parser.add_argument(
277             '-o', '--odlip', help='OpenDaylight IP',
278             default='127.0.0.1')
279         self.parser.add_argument(
280             '-w', '--odlwebport', help='OpenDaylight Web Portal Port',
281             default='8080')
282         self.parser.add_argument(
283             '-r', '--odlrestconfport', help='OpenDaylight RESTConf Port',
284             default='8181')
285         self.parser.add_argument(
286             '-d', '--odlusername', help='Username for ODL',
287             default='admin')
288         self.parser.add_argument(
289             '-e', '--odlpassword', help='Password for ODL',
290             default='admin')
291         self.parser.add_argument(
292             '-p', '--pushtodb', help='Push results to DB',
293             action='store_true')
294
295     def parse_args(self, argv=None):
296         """Parse arguments.
297
298         It can call sys.exit if arguments are incorrect.
299
300         Returns:
301             the arguments from cmdline
302         """
303         if not argv:
304             argv = []
305         return vars(self.parser.parse_args(argv))
306
307
308 def main():
309     """Entry point"""
310     logging.basicConfig()
311     odl = ODLTests()
312     parser = ODLParser()
313     args = parser.parse_args(sys.argv[1:])
314     try:
315         result = odl.run_suites(ODLTests.default_suites, **args)
316         if result != testcase.TestCase.EX_OK:
317             return result
318         if args['pushtodb']:
319             return odl.push_to_db()
320         else:
321             return result
322     except Exception:  # pylint: disable=broad-except
323         return testcase.TestCase.EX_RUN_ERROR