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 the classes required to fully cover odl."""
16 from keystoneauth1.exceptions import auth_plugins
18 from robot.errors import RobotError
20 from six.moves import urllib
22 from functest.core import testcase
23 from functest.opnfv_tests.sdn.odl import odl
25 __author__ = "Cedric Ollivier <cedric.ollivier@orange.com>"
28 class ODLTesting(unittest.TestCase):
30 """The super class which testing classes could inherit."""
31 # pylint: disable=missing-docstring
33 logging.disable(logging.CRITICAL)
35 _keystone_ip = "127.0.0.1"
36 _neutron_url = "http://127.0.0.2:9696"
37 _sdn_controller_ip = "127.0.0.3"
38 _os_auth_url = "http://{}:5000/v3".format(_keystone_ip)
39 _os_projectname = "admin"
40 _os_username = "admin"
41 _os_password = "admin"
43 _odl_restconfport = "8181"
44 _odl_username = "admin"
45 _odl_password = "admin"
46 _os_userdomainname = 'Default'
47 _os_projectdomainname = 'Default'
50 for var in ("INSTALLER_TYPE", "SDN_CONTROLLER", "SDN_CONTROLLER_IP"):
53 os.environ["OS_AUTH_URL"] = self._os_auth_url
54 os.environ["OS_USERNAME"] = self._os_username
55 os.environ["OS_USER_DOMAIN_NAME"] = self._os_userdomainname
56 os.environ["OS_PASSWORD"] = self._os_password
57 os.environ["OS_PROJECT_NAME"] = self._os_projectname
58 os.environ["OS_PROJECT_DOMAIN_NAME"] = self._os_projectdomainname
59 os.environ["OS_PASSWORD"] = self._os_password
60 self.test = odl.ODLTests(case_name='odl', project_name='functest')
61 self.defaultargs = {'odlusername': self._odl_username,
62 'odlpassword': self._odl_password,
63 'neutronurl': "http://{}:9696".format(
65 'osauthurl': self._os_auth_url,
66 'osusername': self._os_username,
67 'osuserdomainname': self._os_userdomainname,
68 'osprojectname': self._os_projectname,
69 'osprojectdomainname': self._os_projectdomainname,
70 'ospassword': self._os_password,
71 'odlip': self._keystone_ip,
72 'odlwebport': self._odl_webport,
73 'odlrestconfport': self._odl_restconfport,
77 class ODLRobotTesting(ODLTesting):
79 """The class testing ODLTests.set_robotframework_vars()."""
80 # pylint: disable=missing-docstring
82 @mock.patch('fileinput.input', side_effect=Exception())
83 def test_set_vars_ko(self, mock_method):
84 self.assertFalse(self.test.set_robotframework_vars())
85 mock_method.assert_called_once_with(
86 os.path.join(odl.ODLTests.odl_test_repo,
87 'csit/variables/Variables.robot'), inplace=True)
89 @mock.patch('fileinput.input', return_value=[])
90 def test_set_vars_empty(self, mock_method):
91 self.assertTrue(self.test.set_robotframework_vars())
92 mock_method.assert_called_once_with(
93 os.path.join(odl.ODLTests.odl_test_repo,
94 'csit/variables/Variables.robot'), inplace=True)
96 @mock.patch('sys.stdout', new_callable=six.StringIO)
97 def _test_set_vars(self, msg1, msg2, *args):
98 line = mock.MagicMock()
99 line.__iter__.return_value = [msg1]
100 with mock.patch('fileinput.input', return_value=line) as mock_method:
101 self.assertTrue(self.test.set_robotframework_vars())
102 mock_method.assert_called_once_with(
103 os.path.join(odl.ODLTests.odl_test_repo,
104 'csit/variables/Variables.robot'), inplace=True)
105 self.assertEqual(args[0].getvalue(), "{}\n".format(msg2))
107 def test_set_vars_auth_default(self):
110 "@{AUTH} admin admin")
112 def test_set_vars_auth1(self):
117 @mock.patch('sys.stdout', new_callable=six.StringIO)
118 def test_set_vars_auth_foo(self, *args):
119 line = mock.MagicMock()
120 line.__iter__.return_value = ["@{AUTH} "]
121 with mock.patch('fileinput.input', return_value=line) as mock_method:
122 self.assertTrue(self.test.set_robotframework_vars('foo', 'bar'))
123 mock_method.assert_called_once_with(
124 os.path.join(odl.ODLTests.odl_test_repo,
125 'csit/variables/Variables.robot'), inplace=True)
131 class ODLMainTesting(ODLTesting):
133 """The class testing ODLTests.run_suites()."""
134 # pylint: disable=missing-docstring
136 def _get_run_suites_kwargs(self, key=None):
137 kwargs = {'odlusername': self._odl_username,
138 'odlpassword': self._odl_password,
139 'neutronurl': self._neutron_url,
140 'osauthurl': self._os_auth_url,
141 'osusername': self._os_username,
142 'osuserdomainname': self._os_userdomainname,
143 'osprojectname': self._os_projectname,
144 'osprojectdomainname': self._os_projectdomainname,
145 'ospassword': self._os_password,
146 'odlip': self._sdn_controller_ip,
147 'odlwebport': self._odl_webport,
148 'odlrestconfport': self._odl_restconfport}
153 def _test_run_suites(self, status, *args):
154 kwargs = self._get_run_suites_kwargs()
155 self.assertEqual(self.test.run_suites(**kwargs), status)
157 args[0].assert_called_once_with(self.test.res_dir)
160 'KEYSTONEURL:{}://{}'.format(
161 urllib.parse.urlparse(self._os_auth_url).scheme,
162 urllib.parse.urlparse(self._os_auth_url).netloc),
163 'NEUTRONURL:{}'.format(self._neutron_url),
164 'OS_AUTH_URL:"{}"'.format(self._os_auth_url),
165 'OSUSERNAME:"{}"'.format(self._os_username),
166 'OSUSERDOMAINNAME:"{}"'.format(self._os_userdomainname),
167 'OSTENANTNAME:"{}"'.format(self._os_projectname),
168 'OSPROJECTDOMAINNAME:"{}"'.format(self._os_projectdomainname),
169 'OSPASSWORD:"{}"'.format(self._os_password),
170 'ODL_SYSTEM_IP:{}'.format(self._sdn_controller_ip),
171 'PORT:{}'.format(self._odl_webport),
172 'RESTCONFPORT:{}'.format(self._odl_restconfport)]
173 args[1].assert_called_once_with(
174 odl.ODLTests.basic_suite_dir,
175 odl.ODLTests.neutron_suite_dir,
177 output=os.path.join(self.test.res_dir, 'output.xml'),
182 def _test_no_keyword(self, key):
183 kwargs = self._get_run_suites_kwargs(key)
184 self.assertEqual(self.test.run_suites(**kwargs),
185 testcase.TestCase.EX_RUN_ERROR)
187 def test_no_odlusername(self):
188 self._test_no_keyword('odlusername')
190 def test_no_odlpassword(self):
191 self._test_no_keyword('odlpassword')
193 def test_no_neutronurl(self):
194 self._test_no_keyword('neutronurl')
196 def test_no_osauthurl(self):
197 self._test_no_keyword('osauthurl')
199 def test_no_osusername(self):
200 self._test_no_keyword('osusername')
202 def test_no_osprojectname(self):
203 self._test_no_keyword('osprojectname')
205 def test_no_ospassword(self):
206 self._test_no_keyword('ospassword')
208 def test_no_odlip(self):
209 self._test_no_keyword('odlip')
211 def test_no_odlwebport(self):
212 self._test_no_keyword('odlwebport')
214 def test_no_odlrestconfport(self):
215 self._test_no_keyword('odlrestconfport')
217 def test_set_vars_ko(self):
218 with mock.patch.object(self.test, 'set_robotframework_vars',
219 return_value=False) as mock_object:
220 self._test_run_suites(testcase.TestCase.EX_RUN_ERROR)
221 mock_object.assert_called_once_with(
222 self._odl_username, self._odl_password)
224 @mock.patch('robot.run', side_effect=RobotError)
225 @mock.patch('os.makedirs')
226 def test_run_ko(self, *args):
227 with mock.patch.object(self.test, 'set_robotframework_vars',
228 return_value=True), \
229 self.assertRaises(RobotError):
230 self._test_run_suites(testcase.TestCase.EX_RUN_ERROR, *args)
232 @mock.patch('robot.run')
233 @mock.patch('os.makedirs')
234 def test_parse_results_ko(self, *args):
235 with mock.patch.object(self.test, 'set_robotframework_vars',
236 return_value=True), \
237 mock.patch.object(self.test, 'parse_results',
238 side_effect=RobotError):
239 self._test_run_suites(testcase.TestCase.EX_RUN_ERROR, *args)
241 @mock.patch('robot.run')
242 @mock.patch('os.makedirs')
243 def test_ok(self, *args):
244 with mock.patch.object(self.test, 'set_robotframework_vars',
245 return_value=True), \
246 mock.patch.object(self.test, 'parse_results'):
247 self._test_run_suites(testcase.TestCase.EX_OK, *args)
249 @mock.patch('robot.run', return_value=1)
250 @mock.patch('os.makedirs')
251 def test_testcases_in_failure(self, *args):
252 with mock.patch.object(self.test, 'set_robotframework_vars',
253 return_value=True), \
254 mock.patch.object(self.test, 'parse_results'):
255 self._test_run_suites(testcase.TestCase.EX_OK, *args)
258 class ODLRunTesting(ODLTesting):
260 """The class testing ODLTests.run()."""
261 # pylint: disable=missing-docstring
263 def _test_no_env_var(self, var):
264 with mock.patch('functest.utils.openstack_utils.get_endpoint',
265 return_value=ODLTesting._neutron_url):
267 self.assertEqual(self.test.run(),
268 testcase.TestCase.EX_RUN_ERROR)
270 def _test_run(self, status=testcase.TestCase.EX_OK,
271 exception=None, **kwargs):
272 odlip = kwargs['odlip'] if 'odlip' in kwargs else '127.0.0.3'
273 odlwebport = kwargs['odlwebport'] if 'odlwebport' in kwargs else '8080'
274 odlrestconfport = (kwargs['odlrestconfport']
275 if 'odlrestconfport' in kwargs else '8181')
277 with mock.patch('functest.utils.openstack_utils.get_endpoint',
278 return_value=ODLTesting._neutron_url):
280 self.test.run_suites = mock.Mock(side_effect=exception)
282 self.test.run_suites = mock.Mock(return_value=status)
283 self.assertEqual(self.test.run(), status)
284 self.test.run_suites.assert_called_once_with(
285 odl.ODLTests.default_suites,
286 neutronurl=self._neutron_url,
287 odlip=odlip, odlpassword=self._odl_password,
288 odlrestconfport=odlrestconfport,
289 odlusername=self._odl_username, odlwebport=odlwebport,
290 osauthurl=self._os_auth_url,
291 ospassword=self._os_password,
292 osprojectname=self._os_projectname,
293 osusername=self._os_username,
294 osprojectdomainname=self._os_projectdomainname,
295 osuserdomainname=self._os_userdomainname)
297 def _test_multiple_suites(self, suites,
298 status=testcase.TestCase.EX_OK, **kwargs):
299 odlip = kwargs['odlip'] if 'odlip' in kwargs else '127.0.0.3'
300 odlwebport = kwargs['odlwebport'] if 'odlwebport' in kwargs else '8080'
301 odlrestconfport = (kwargs['odlrestconfport']
302 if 'odlrestconfport' in kwargs else '8181')
303 with mock.patch('functest.utils.openstack_utils.get_endpoint',
304 return_value=ODLTesting._neutron_url):
305 self.test.run_suites = mock.Mock(return_value=status)
306 self.assertEqual(self.test.run(suites=suites), status)
307 self.test.run_suites.assert_called_once_with(
309 neutronurl=self._neutron_url,
310 odlip=odlip, odlpassword=self._odl_password,
311 odlrestconfport=odlrestconfport,
312 odlusername=self._odl_username, odlwebport=odlwebport,
313 osauthurl=self._os_auth_url,
314 ospassword=self._os_password,
315 osprojectname=self._os_projectname,
316 osusername=self._os_username,
317 osprojectdomainname=self._os_projectdomainname,
318 osuserdomainname=self._os_userdomainname)
321 with mock.patch('functest.utils.openstack_utils.get_endpoint',
322 side_effect=auth_plugins.MissingAuthPlugin()):
323 self.assertEqual(self.test.run(),
324 testcase.TestCase.EX_RUN_ERROR)
326 def test_no_os_auth_url(self):
327 self._test_no_env_var("OS_AUTH_URL")
329 def test_no_os_username(self):
330 self._test_no_env_var("OS_USERNAME")
332 def test_no_os_password(self):
333 self._test_no_env_var("OS_PASSWORD")
335 def test_no_os__name(self):
336 self._test_no_env_var("OS_PROJECT_NAME")
338 def test_run_suites_false(self):
339 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
340 self._test_run(testcase.TestCase.EX_RUN_ERROR,
341 odlip=self._sdn_controller_ip,
342 odlwebport=self._odl_webport)
344 def test_run_suites_exc(self):
345 with self.assertRaises(Exception):
346 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
347 self._test_run(status=testcase.TestCase.EX_RUN_ERROR,
348 exception=Exception(),
349 odlip=self._sdn_controller_ip,
350 odlwebport=self._odl_webport)
352 def test_no_sdn_controller_ip(self):
353 with mock.patch('functest.utils.openstack_utils.get_endpoint',
354 return_value=ODLTesting._neutron_url):
355 self.assertEqual(self.test.run(),
356 testcase.TestCase.EX_RUN_ERROR)
358 def test_without_installer_type(self):
359 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
360 self._test_run(testcase.TestCase.EX_OK,
361 odlip=self._sdn_controller_ip,
362 odlwebport=self._odl_webport)
364 def test_suites(self):
365 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
366 self._test_multiple_suites(
367 [odl.ODLTests.basic_suite_dir],
368 testcase.TestCase.EX_OK,
369 odlip=self._sdn_controller_ip,
370 odlwebport=self._odl_webport)
373 os.environ["INSTALLER_TYPE"] = "fuel"
374 self._test_run(testcase.TestCase.EX_OK,
375 odlip=urllib.parse.urlparse(self._neutron_url).hostname,
377 odlrestconfport='8282')
379 def test_apex_no_controller_ip(self):
380 with mock.patch('functest.utils.openstack_utils.get_endpoint',
381 return_value=ODLTesting._neutron_url):
382 os.environ["INSTALLER_TYPE"] = "apex"
383 self.assertEqual(self.test.run(),
384 testcase.TestCase.EX_RUN_ERROR)
387 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
388 os.environ["INSTALLER_TYPE"] = "apex"
389 self._test_run(testcase.TestCase.EX_OK,
390 odlip=self._sdn_controller_ip, odlwebport='8081',
391 odlrestconfport='8081')
393 def test_netvirt_no_controller_ip(self):
394 with mock.patch('functest.utils.openstack_utils.get_endpoint',
395 return_value=ODLTesting._neutron_url):
396 os.environ["INSTALLER_TYPE"] = "netvirt"
397 self.assertEqual(self.test.run(),
398 testcase.TestCase.EX_RUN_ERROR)
400 def test_netvirt(self):
401 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
402 os.environ["INSTALLER_TYPE"] = "netvirt"
403 self._test_run(testcase.TestCase.EX_OK,
404 odlip=self._sdn_controller_ip, odlwebport='8081',
405 odlrestconfport='8081')
407 def test_joid_no_controller_ip(self):
408 with mock.patch('functest.utils.openstack_utils.get_endpoint',
409 return_value=ODLTesting._neutron_url):
410 os.environ["INSTALLER_TYPE"] = "joid"
411 self.assertEqual(self.test.run(),
412 testcase.TestCase.EX_RUN_ERROR)
415 os.environ["SDN_CONTROLLER"] = self._sdn_controller_ip
416 os.environ["INSTALLER_TYPE"] = "joid"
417 self._test_run(testcase.TestCase.EX_OK,
418 odlip=self._sdn_controller_ip, odlwebport='8080')
420 def test_compass(self):
421 os.environ["INSTALLER_TYPE"] = "compass"
422 self._test_run(testcase.TestCase.EX_OK,
423 odlip=urllib.parse.urlparse(self._neutron_url).hostname,
424 odlrestconfport='8080')
426 def test_daisy_no_controller_ip(self):
427 with mock.patch('functest.utils.openstack_utils.get_endpoint',
428 return_value=ODLTesting._neutron_url):
429 os.environ["INSTALLER_TYPE"] = "daisy"
430 self.assertEqual(self.test.run(),
431 testcase.TestCase.EX_RUN_ERROR)
433 def test_daisy(self):
434 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
435 os.environ["INSTALLER_TYPE"] = "daisy"
436 self._test_run(testcase.TestCase.EX_OK,
437 odlip=self._sdn_controller_ip, odlwebport='8181',
438 odlrestconfport='8087')
441 class ODLArgParserTesting(ODLTesting):
443 """The class testing ODLParser."""
444 # pylint: disable=missing-docstring
447 self.parser = odl.ODLParser()
448 super(ODLArgParserTesting, self).setUp()
450 def test_default(self):
451 self.assertEqual(self.parser.parse_args(), self.defaultargs)
453 def test_basic(self):
454 self.defaultargs['neutronurl'] = self._neutron_url
455 self.defaultargs['odlip'] = self._sdn_controller_ip
457 self.parser.parse_args(
458 ["--neutronurl={}".format(self._neutron_url),
459 "--odlip={}".format(self._sdn_controller_ip)]),
462 @mock.patch('sys.stderr', new_callable=six.StringIO)
463 def test_fail(self, mock_method):
464 self.defaultargs['foo'] = 'bar'
465 with self.assertRaises(SystemExit):
466 self.parser.parse_args(["--foo=bar"])
467 self.assertTrue(mock_method.getvalue().startswith("usage:"))
469 def _test_arg(self, arg, value):
470 self.defaultargs[arg] = value
472 self.parser.parse_args(["--{}={}".format(arg, value)]),
475 def test_odlusername(self):
476 self._test_arg('odlusername', 'foo')
478 def test_odlpassword(self):
479 self._test_arg('odlpassword', 'foo')
481 def test_osauthurl(self):
482 self._test_arg('osauthurl', 'http://127.0.0.4:5000/v2')
484 def test_neutronurl(self):
485 self._test_arg('neutronurl', 'http://127.0.0.4:9696')
487 def test_osusername(self):
488 self._test_arg('osusername', 'foo')
490 def test_osuserdomainname(self):
491 self._test_arg('osuserdomainname', 'domain')
493 def test_osprojectname(self):
494 self._test_arg('osprojectname', 'foo')
496 def test_osprojectdomainname(self):
497 self._test_arg('osprojectdomainname', 'domain')
499 def test_ospassword(self):
500 self._test_arg('ospassword', 'foo')
502 def test_odlip(self):
503 self._test_arg('odlip', '127.0.0.4')
505 def test_odlwebport(self):
506 self._test_arg('odlwebport', '80')
508 def test_odlrestconfport(self):
509 self._test_arg('odlrestconfport', '80')
511 def test_pushtodb(self):
512 self.defaultargs['pushtodb'] = True
513 self.assertEqual(self.parser.parse_args(["--{}".format('pushtodb')]),
516 def test_multiple_args(self):
517 self.defaultargs['neutronurl'] = self._neutron_url
518 self.defaultargs['odlip'] = self._sdn_controller_ip
520 self.parser.parse_args(
521 ["--neutronurl={}".format(self._neutron_url),
522 "--odlip={}".format(self._sdn_controller_ip)]),
526 if __name__ == "__main__":
527 logging.disable(logging.CRITICAL)
528 unittest.main(verbosity=2)