3 # Copyright (c) 2016 Orange and others.
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
10 """Define classes required to run ODL suites.
12 It has been designed for any context. But helpers are given for
13 running test suites in OPNFV environment.
19 from __future__ import division
28 from six.moves import urllib
29 from snaps.openstack.utils import keystone_utils
31 from functest.core import robotframework
32 from functest.opnfv_tests.openstack.snaps import snaps_utils
33 from functest.utils import config
34 from functest.utils import env
36 __author__ = "Cedric Ollivier <cedric.ollivier@orange.com>"
39 class ODLTests(robotframework.RobotFramework):
40 """ODL test runner."""
42 odl_test_repo = getattr(config.CONF, 'dir_repo_odl_test')
43 neutron_suite_dir = os.path.join(
44 odl_test_repo, "csit/suites/openstack/neutron")
45 basic_suite_dir = os.path.join(
46 odl_test_repo, "csit/suites/integration/basic")
47 default_suites = [basic_suite_dir, neutron_suite_dir]
48 odl_variables_file = os.path.join(
49 odl_test_repo, 'csit/variables/Variables.robot')
50 __logger = logging.getLogger(__name__)
52 def __init__(self, **kwargs):
53 super(ODLTests, self).__init__(**kwargs)
54 self.res_dir = os.path.join(
55 getattr(config.CONF, 'dir_results'), 'odl')
56 self.xml_file = os.path.join(self.res_dir, 'output.xml')
59 def set_robotframework_vars(cls, odlusername="admin", odlpassword="admin"):
60 """Set credentials in csit/variables/Variables.robot.
63 True if credentials are set.
68 for line in fileinput.input(cls.odl_variables_file,
70 print(re.sub("@{AUTH}.*",
71 "@{{AUTH}} {} {}".format(
72 odlusername, odlpassword),
75 except Exception: # pylint: disable=broad-except
76 cls.__logger.exception("Cannot set ODL creds:")
79 def run_suites(self, suites=None, **kwargs):
80 """Run the test suites
82 It has been designed to be called in any context.
83 It requires the following keyword arguments:
97 * set all RobotFramework_variables,
98 * create the output directories if required,
99 * get the results in output.xml,
100 * delete temporary files.
103 kwargs: Arbitrary keyword arguments.
106 EX_OK if all suites ran well.
107 EX_RUN_ERROR otherwise.
111 suites = self.default_suites
112 odlusername = kwargs['odlusername']
113 odlpassword = kwargs['odlpassword']
114 osauthurl = kwargs['osauthurl']
115 keystoneurl = "{}://{}".format(
116 urllib.parse.urlparse(osauthurl).scheme,
117 urllib.parse.urlparse(osauthurl).netloc)
118 variable = ['KEYSTONEURL:' + keystoneurl,
119 'NEUTRONURL:' + kwargs['neutronurl'],
120 'OS_AUTH_URL:"' + osauthurl + '"',
121 'OSUSERNAME:"' + kwargs['osusername'] + '"',
122 ('OSUSERDOMAINNAME:"' +
123 kwargs['osuserdomainname'] + '"'),
124 'OSTENANTNAME:"' + kwargs['osprojectname'] + '"',
125 ('OSPROJECTDOMAINNAME:"' +
126 kwargs['osprojectdomainname'] + '"'),
127 'OSPASSWORD:"' + kwargs['ospassword'] + '"',
128 'ODL_SYSTEM_IP:' + kwargs['odlip'],
129 'PORT:' + kwargs['odlwebport'],
130 'RESTCONFPORT:' + kwargs['odlrestconfport']]
132 self.__logger.exception("Cannot run ODL testcases. Please check")
133 return self.EX_RUN_ERROR
134 if not os.path.isfile(self.odl_variables_file):
135 self.__logger.info("Skip writting ODL creds")
137 if not self.set_robotframework_vars(odlusername, odlpassword):
138 return self.EX_RUN_ERROR
139 return super(ODLTests, self).run(variable=variable, suites=suites)
141 def run(self, **kwargs):
142 """Run suites in OPNFV environment
144 It basically checks env vars to call main() with the keywords
148 kwargs: Arbitrary keyword arguments.
151 EX_OK if all suites ran well.
152 EX_RUN_ERROR otherwise.
155 suites = self.default_suites
157 suites = kwargs["suites"]
160 snaps_creds = snaps_utils.get_credentials()
161 kwargs = {'neutronurl': keystone_utils.get_endpoint(
162 snaps_creds, 'network')}
163 kwargs['odlip'] = urllib.parse.urlparse(
164 kwargs['neutronurl']).hostname
165 kwargs['odlwebport'] = '8080'
166 kwargs['odlrestconfport'] = '8181'
167 kwargs['odlusername'] = 'admin'
168 kwargs['odlpassword'] = 'admin'
169 installer_type = env.get('INSTALLER_TYPE')
170 kwargs['osusername'] = os.environ['OS_USERNAME']
171 kwargs['osuserdomainname'] = os.environ.get(
172 'OS_USER_DOMAIN_NAME', 'Default')
173 kwargs['osprojectname'] = os.environ['OS_PROJECT_NAME']
174 kwargs['osprojectdomainname'] = os.environ.get(
175 'OS_PROJECT_DOMAIN_NAME', 'Default')
176 kwargs['osauthurl'] = os.environ['OS_AUTH_URL']
177 kwargs['ospassword'] = os.environ['OS_PASSWORD']
178 if installer_type == 'fuel':
179 kwargs['odlwebport'] = '8181'
180 kwargs['odlrestconfport'] = '8282'
181 elif installer_type == 'apex' or installer_type == 'netvirt':
182 kwargs['odlip'] = env.get('SDN_CONTROLLER_IP')
183 kwargs['odlwebport'] = '8081'
184 kwargs['odlrestconfport'] = '8081'
185 elif installer_type == 'compass':
186 kwargs['odlrestconfport'] = '8080'
187 elif installer_type == 'daisy':
188 kwargs['odlip'] = env.get('SDN_CONTROLLER_IP')
189 kwargs['odlwebport'] = '8181'
190 kwargs['odlrestconfport'] = '8087'
192 kwargs['odlip'] = env.get('SDN_CONTROLLER_IP')
193 assert kwargs['odlip']
194 except KeyError as ex:
195 self.__logger.error("Cannot run ODL testcases. "
196 "Please check env var: "
198 return self.EX_RUN_ERROR
199 except Exception: # pylint: disable=broad-except
200 self.__logger.exception("Cannot run ODL testcases.")
201 return self.EX_RUN_ERROR
203 return self.run_suites(suites, **kwargs)
206 class ODLParser(object): # pylint: disable=too-few-public-methods
207 """Parser to run ODL test suites."""
210 self.parser = argparse.ArgumentParser()
211 self.parser.add_argument(
212 '-n', '--neutronurl', help='Neutron Endpoint',
213 default='http://127.0.0.1:9696')
214 self.parser.add_argument(
215 '-k', '--osauthurl', help='OS_AUTH_URL as defined by OpenStack',
216 default='http://127.0.0.1:5000/v3')
217 self.parser.add_argument(
218 '-a', '--osusername', help='Username for OpenStack',
220 self.parser.add_argument(
221 '-f', '--osuserdomainname', help='User domain name for OpenStack',
223 self.parser.add_argument(
224 '-b', '--osprojectname', help='Projet name for OpenStack',
226 self.parser.add_argument(
227 '-g', '--osprojectdomainname',
228 help='Project domain name for OpenStack',
230 self.parser.add_argument(
231 '-c', '--ospassword', help='Password for OpenStack',
233 self.parser.add_argument(
234 '-o', '--odlip', help='OpenDaylight IP',
236 self.parser.add_argument(
237 '-w', '--odlwebport', help='OpenDaylight Web Portal Port',
239 self.parser.add_argument(
240 '-r', '--odlrestconfport', help='OpenDaylight RESTConf Port',
242 self.parser.add_argument(
243 '-d', '--odlusername', help='Username for ODL',
245 self.parser.add_argument(
246 '-e', '--odlpassword', help='Password for ODL',
248 self.parser.add_argument(
249 '-p', '--pushtodb', help='Push results to DB',
252 def parse_args(self, argv=None):
255 It can call sys.exit if arguments are incorrect.
258 the arguments from cmdline
262 return vars(self.parser.parse_args(argv))
267 logging.basicConfig()
270 args = parser.parse_args(sys.argv[1:])
272 result = odl.run_suites(ODLTests.default_suites, **args)
273 if result != robotframework.RobotFramework.EX_OK:
276 return odl.push_to_db()
278 except Exception: # pylint: disable=broad-except
279 return robotframework.RobotFramework.EX_RUN_ERROR