Merge "Wait for refstack-client to finish"
[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 = os.path.join(
70         constants.CONST.__getattribute__('dir_repos'), 'odl_test')
71     neutron_suite_dir = os.path.join(odl_test_repo,
72                                      "csit/suites/openstack/neutron")
73     basic_suite_dir = os.path.join(odl_test_repo,
74                                    "csit/suites/integration/basic")
75     default_suites = [basic_suite_dir, neutron_suite_dir]
76     res_dir = os.path.join(
77         constants.CONST.__getattribute__('dir_results'), 'odl')
78     __logger = logging.getLogger(__name__)
79
80     @classmethod
81     def set_robotframework_vars(cls, odlusername="admin", odlpassword="admin"):
82         """Set credentials in csit/variables/Variables.py.
83
84         Returns:
85             True if credentials are set.
86             False otherwise.
87         """
88         odl_variables_files = os.path.join(cls.odl_test_repo,
89                                            'csit/variables/Variables.py')
90         try:
91             for line in fileinput.input(odl_variables_files,
92                                         inplace=True):
93                 print(re.sub("AUTH = .*",
94                              ("AUTH = [u'" + odlusername + "', u'" +
95                               odlpassword + "']"),
96                              line.rstrip()))
97             return True
98         except Exception as ex:  # pylint: disable=broad-except
99             cls.__logger.error("Cannot set ODL creds: %s", str(ex))
100             return False
101
102     def parse_results(self):
103         """Parse output.xml and get the details in it."""
104         xml_file = os.path.join(self.res_dir, 'output.xml')
105         result = robot.api.ExecutionResult(xml_file)
106         visitor = ODLResultVisitor()
107         result.visit(visitor)
108         try:
109             self.result = 100 * (
110                 result.suite.statistics.critical.passed /
111                 result.suite.statistics.critical.total)
112         except ZeroDivisionError:
113             self.__logger.error("No test has been run")
114         self.start_time = timestamp_to_secs(result.suite.starttime)
115         self.stop_time = timestamp_to_secs(result.suite.endtime)
116         self.details = {}
117         self.details['description'] = result.suite.name
118         self.details['tests'] = visitor.get_data()
119
120     def run_suites(self, suites=None, **kwargs):
121         """Run the test suites
122
123         It has been designed to be called in any context.
124         It requires the following keyword arguments:
125
126            * odlusername,
127            * odlpassword,
128            * osauthurl,
129            * neutronip,
130            * osusername,
131            * ostenantname,
132            * ospassword,
133            * odlip,
134            * odlwebport,
135            * odlrestconfport.
136
137         Here are the steps:
138            * set all RobotFramework_variables,
139            * create the output directories if required,
140            * get the results in output.xml,
141            * delete temporary files.
142
143         Args:
144             kwargs: Arbitrary keyword arguments.
145
146         Returns:
147             EX_OK if all suites ran well.
148             EX_RUN_ERROR otherwise.
149         """
150         try:
151             if not suites:
152                 suites = self.default_suites
153             odlusername = kwargs['odlusername']
154             odlpassword = kwargs['odlpassword']
155             osauthurl = kwargs['osauthurl']
156             keystoneip = urllib.parse.urlparse(osauthurl).hostname
157             variables = ['KEYSTONE:' + keystoneip,
158                          'NEUTRON:' + kwargs['neutronip'],
159                          'OS_AUTH_URL:"' + osauthurl + '"',
160                          'OSUSERNAME:"' + kwargs['osusername'] + '"',
161                          'OSTENANTNAME:"' + kwargs['ostenantname'] + '"',
162                          'OSPASSWORD:"' + kwargs['ospassword'] + '"',
163                          'ODL_SYSTEM_IP:' + kwargs['odlip'],
164                          'PORT:' + kwargs['odlwebport'],
165                          'RESTCONFPORT:' + kwargs['odlrestconfport']]
166         except KeyError as ex:
167             self.__logger.error("Cannot run ODL testcases. Please check "
168                                 "%s", str(ex))
169             return self.EX_RUN_ERROR
170         if self.set_robotframework_vars(odlusername, odlpassword):
171             try:
172                 os.makedirs(self.res_dir)
173             except OSError as ex:
174                 if ex.errno != errno.EEXIST:
175                     self.__logger.exception(
176                         "Cannot create %s", self.res_dir)
177                     return self.EX_RUN_ERROR
178             output_dir = os.path.join(self.res_dir, 'output.xml')
179             stream = StringIO()
180             robot.run(*suites, variable=variables, output=output_dir,
181                       log='NONE', report='NONE', stdout=stream)
182             self.__logger.info("\n" + stream.getvalue())
183             self.__logger.info("ODL results were successfully generated")
184             try:
185                 self.parse_results()
186                 self.__logger.info("ODL results were successfully parsed")
187             except RobotError as ex:
188                 self.__logger.error("Run tests before publishing: %s",
189                                     ex.message)
190                 return self.EX_RUN_ERROR
191             return self.EX_OK
192         else:
193             return self.EX_RUN_ERROR
194
195     def run(self, **kwargs):
196         """Run suites in OPNFV environment
197
198         It basically check env vars to call main() with the keywords
199         required.
200
201         Args:
202             kwargs: Arbitrary keyword arguments.
203
204         Returns:
205             EX_OK if all suites ran well.
206             EX_RUN_ERROR otherwise.
207         """
208         try:
209             suites = self.default_suites
210             try:
211                 suites = kwargs["suites"]
212             except KeyError:
213                 pass
214             neutron_url = op_utils.get_endpoint(service_type='network')
215             kwargs = {'neutronip': urllib.parse.urlparse(neutron_url).hostname}
216             kwargs['odlip'] = kwargs['neutronip']
217             kwargs['odlwebport'] = '8080'
218             kwargs['odlrestconfport'] = '8181'
219             kwargs['odlusername'] = 'admin'
220             kwargs['odlpassword'] = 'admin'
221             installer_type = None
222             if 'INSTALLER_TYPE' in os.environ:
223                 installer_type = os.environ['INSTALLER_TYPE']
224             kwargs['osusername'] = os.environ['OS_USERNAME']
225             kwargs['ostenantname'] = os.environ['OS_TENANT_NAME']
226             kwargs['osauthurl'] = os.environ['OS_AUTH_URL']
227             kwargs['ospassword'] = os.environ['OS_PASSWORD']
228             if installer_type == 'fuel':
229                 kwargs['odlwebport'] = '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