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."""
17 from keystoneauth1.exceptions import auth_plugins
19 from robot.errors import DataError, RobotError
20 from robot.result import model
21 from robot.utils.robottime import timestamp_to_secs
24 from functest.core import testcase
25 from functest.opnfv_tests.sdn.odl import odl
27 __author__ = "Cedric Ollivier <cedric.ollivier@orange.com>"
30 class ODLVisitorTesting(unittest.TestCase):
32 """The class testing ODLResultVisitor."""
33 # pylint: disable=missing-docstring
36 self.visitor = odl.ODLResultVisitor()
39 self.assertFalse(self.visitor.get_data())
42 data = {'name': 'foo',
45 'starttime': "20161216 16:00:00.000",
46 'endtime': "20161216 16:00:01.000",
48 'text': 'Hello, World!',
50 test = model.TestCase(
51 name=data['name'], status=data['status'], message=data['text'],
52 starttime=data['starttime'], endtime=data['endtime'])
53 test.parent = mock.Mock()
54 config = {'name': data['parent'],
55 'criticality.test_is_critical.return_value': data[
57 test.parent.configure_mock(**config)
58 self.visitor.visit_test(test)
59 self.assertEqual(self.visitor.get_data(), [data])
62 class ODLTesting(unittest.TestCase):
64 """The super class which testing classes could inherit."""
65 # pylint: disable=missing-docstring
67 logging.disable(logging.CRITICAL)
69 _keystone_ip = "127.0.0.1"
70 _neutron_ip = "127.0.0.2"
71 _sdn_controller_ip = "127.0.0.3"
72 _os_auth_url = "http://{}:5000/v3".format(_keystone_ip)
73 _os_tenantname = "admin"
74 _os_username = "admin"
75 _os_password = "admin"
77 _odl_restconfport = "8181"
78 _odl_username = "admin"
79 _odl_password = "admin"
80 _os_userdomainname = 'Default'
81 _os_projectdomainname = 'Default'
84 for var in ("INSTALLER_TYPE", "SDN_CONTROLLER", "SDN_CONTROLLER_IP"):
87 os.environ["OS_AUTH_URL"] = self._os_auth_url
88 os.environ["OS_USERNAME"] = self._os_username
89 os.environ["OS_USER_DOMAIN_NAME"] = self._os_userdomainname
90 os.environ["OS_PASSWORD"] = self._os_password
91 os.environ["OS_TENANT_NAME"] = self._os_tenantname
92 os.environ["OS_PROJECT_DOMAIN_NAME"] = self._os_projectdomainname
93 os.environ["OS_PASSWORD"] = self._os_password
94 self.test = odl.ODLTests(case_name='odl', project_name='functest')
95 self.defaultargs = {'odlusername': self._odl_username,
96 'odlpassword': self._odl_password,
97 'neutronip': self._keystone_ip,
98 'osauthurl': self._os_auth_url,
99 'osusername': self._os_username,
100 'osuserdomainname': self._os_userdomainname,
101 'ostenantname': self._os_tenantname,
102 'osprojectdomainname': self._os_projectdomainname,
103 'ospassword': self._os_password,
104 'odlip': self._keystone_ip,
105 'odlwebport': self._odl_webport,
106 'odlrestconfport': self._odl_restconfport,
110 class ODLParseResultTesting(ODLTesting):
112 """The class testing ODLTests.parse_results()."""
113 # pylint: disable=missing-docstring
115 _config = {'name': 'dummy', 'starttime': '20161216 16:00:00.000',
116 'endtime': '20161216 16:00:01.000'}
118 @mock.patch('robot.api.ExecutionResult', side_effect=DataError)
119 def test_raises_exc(self, mock_method):
120 with self.assertRaises(DataError):
121 self.test.parse_results()
122 mock_method.assert_called_once_with(
123 os.path.join(odl.ODLTests.res_dir, 'output.xml'))
125 def _test_result(self, config, result):
127 suite.configure_mock(**config)
128 with mock.patch('robot.api.ExecutionResult',
129 return_value=mock.Mock(suite=suite)):
130 self.test.parse_results()
131 self.assertEqual(self.test.result, result)
132 self.assertEqual(self.test.start_time,
133 timestamp_to_secs(config['starttime']))
134 self.assertEqual(self.test.stop_time,
135 timestamp_to_secs(config['endtime']))
136 self.assertEqual(self.test.details,
137 {'description': config['name'], 'tests': []})
139 def test_null_passed(self):
140 self._config.update({'statistics.critical.passed': 0,
141 'statistics.critical.total': 20})
142 self._test_result(self._config, 0)
144 def test_no_test(self):
145 self._config.update({'statistics.critical.passed': 20,
146 'statistics.critical.total': 0})
147 self._test_result(self._config, 0)
149 def test_half_success(self):
150 self._config.update({'statistics.critical.passed': 10,
151 'statistics.critical.total': 20})
152 self._test_result(self._config, 50)
154 def test_success(self):
155 self._config.update({'statistics.critical.passed': 20,
156 'statistics.critical.total': 20})
157 self._test_result(self._config, 100)
160 class ODLRobotTesting(ODLTesting):
162 """The class testing ODLTests.set_robotframework_vars()."""
163 # pylint: disable=missing-docstring
165 @mock.patch('fileinput.input', side_effect=Exception())
166 def test_set_vars_ko(self, mock_method):
167 self.assertFalse(self.test.set_robotframework_vars())
168 mock_method.assert_called_once_with(
169 os.path.join(odl.ODLTests.odl_test_repo,
170 'csit/variables/Variables.py'), inplace=True)
172 @mock.patch('fileinput.input', return_value=[])
173 def test_set_vars_empty(self, mock_method):
174 self.assertTrue(self.test.set_robotframework_vars())
175 mock_method.assert_called_once_with(
176 os.path.join(odl.ODLTests.odl_test_repo,
177 'csit/variables/Variables.py'), inplace=True)
179 @mock.patch('sys.stdout', new_callable=six.StringIO)
180 def _test_set_vars(self, msg1, msg2, *args):
181 line = mock.MagicMock()
182 line.__iter__.return_value = [msg1]
183 with mock.patch('fileinput.input', return_value=line) as mock_method:
184 self.assertTrue(self.test.set_robotframework_vars())
185 mock_method.assert_called_once_with(
186 os.path.join(odl.ODLTests.odl_test_repo,
187 'csit/variables/Variables.py'), inplace=True)
188 self.assertEqual(args[0].getvalue(), "{}\n".format(msg2))
190 def test_set_vars_auth_default(self):
191 self._test_set_vars("AUTH = []",
192 "AUTH = [u'admin', u'admin']")
194 def test_set_vars_auth1(self):
195 self._test_set_vars("AUTH1 = []", "AUTH1 = []")
197 @mock.patch('sys.stdout', new_callable=six.StringIO)
198 def test_set_vars_auth_foo(self, *args):
199 line = mock.MagicMock()
200 line.__iter__.return_value = ["AUTH = []"]
201 with mock.patch('fileinput.input', return_value=line) as mock_method:
202 self.assertTrue(self.test.set_robotframework_vars('foo', 'bar'))
203 mock_method.assert_called_once_with(
204 os.path.join(odl.ODLTests.odl_test_repo,
205 'csit/variables/Variables.py'), inplace=True)
206 self.assertEqual(args[0].getvalue(),
207 "AUTH = [u'{}', u'{}']\n".format('foo', 'bar'))
210 class ODLMainTesting(ODLTesting):
212 """The class testing ODLTests.run_suites()."""
213 # pylint: disable=missing-docstring
215 def _get_run_suites_kwargs(self, key=None):
216 kwargs = {'odlusername': self._odl_username,
217 'odlpassword': self._odl_password,
218 'neutronip': self._neutron_ip,
219 'osauthurl': self._os_auth_url,
220 'osusername': self._os_username,
221 'osuserdomainname': self._os_userdomainname,
222 'ostenantname': self._os_tenantname,
223 'osprojectdomainname': self._os_projectdomainname,
224 'ospassword': self._os_password,
225 'odlip': self._sdn_controller_ip,
226 'odlwebport': self._odl_webport,
227 'odlrestconfport': self._odl_restconfport}
232 def _test_run_suites(self, status, *args):
233 kwargs = self._get_run_suites_kwargs()
234 self.assertEqual(self.test.run_suites(**kwargs), status)
236 args[0].assert_called_once_with(
237 odl.ODLTests.res_dir)
239 variable = ['KEYSTONE:{}'.format(self._keystone_ip),
240 'NEUTRON:{}'.format(self._neutron_ip),
241 'OS_AUTH_URL:"{}"'.format(self._os_auth_url),
242 'OSUSERNAME:"{}"'.format(self._os_username),
243 'OSUSERDOMAINNAME:"{}"'.format(
244 self._os_userdomainname),
245 'OSTENANTNAME:"{}"'.format(self._os_tenantname),
246 'OSPROJECTDOMAINNAME:"{}"'.format(
247 self._os_projectdomainname),
248 'OSPASSWORD:"{}"'.format(self._os_password),
249 'ODL_SYSTEM_IP:{}'.format(self._sdn_controller_ip),
250 'PORT:{}'.format(self._odl_webport),
251 'RESTCONFPORT:{}'.format(self._odl_restconfport)]
252 args[1].assert_called_once_with(
253 odl.ODLTests.basic_suite_dir,
254 odl.ODLTests.neutron_suite_dir,
256 output=os.path.join(odl.ODLTests.res_dir, 'output.xml'),
261 args[2].assert_called_with(
262 os.path.join(odl.ODLTests.res_dir, 'stdout.txt'))
264 def _test_no_keyword(self, key):
265 kwargs = self._get_run_suites_kwargs(key)
266 self.assertEqual(self.test.run_suites(**kwargs),
267 testcase.TestCase.EX_RUN_ERROR)
269 def test_no_odlusername(self):
270 self._test_no_keyword('odlusername')
272 def test_no_odlpassword(self):
273 self._test_no_keyword('odlpassword')
275 def test_no_neutronip(self):
276 self._test_no_keyword('neutronip')
278 def test_no_osauthurl(self):
279 self._test_no_keyword('osauthurl')
281 def test_no_osusername(self):
282 self._test_no_keyword('osusername')
284 def test_no_ostenantname(self):
285 self._test_no_keyword('ostenantname')
287 def test_no_ospassword(self):
288 self._test_no_keyword('ospassword')
290 def test_no_odlip(self):
291 self._test_no_keyword('odlip')
293 def test_no_odlwebport(self):
294 self._test_no_keyword('odlwebport')
296 def test_no_odlrestconfport(self):
297 self._test_no_keyword('odlrestconfport')
299 def test_set_vars_ko(self):
300 with mock.patch.object(self.test, 'set_robotframework_vars',
301 return_value=False) as mock_object:
302 self._test_run_suites(testcase.TestCase.EX_RUN_ERROR)
303 mock_object.assert_called_once_with(
304 self._odl_username, self._odl_password)
306 @mock.patch('os.makedirs', side_effect=Exception)
307 def test_makedirs_exc(self, mock_method):
308 with mock.patch.object(self.test, 'set_robotframework_vars',
309 return_value=True), \
310 self.assertRaises(Exception):
311 self._test_run_suites(testcase.TestCase.EX_RUN_ERROR,
314 @mock.patch('os.makedirs', side_effect=OSError)
315 def test_makedirs_oserror(self, mock_method):
316 with mock.patch.object(self.test, 'set_robotframework_vars',
318 self._test_run_suites(testcase.TestCase.EX_RUN_ERROR,
321 @mock.patch('robot.run', side_effect=RobotError)
322 @mock.patch('os.makedirs')
323 def test_run_ko(self, *args):
324 with mock.patch.object(self.test, 'set_robotframework_vars',
325 return_value=True), \
326 self.assertRaises(RobotError):
327 self._test_run_suites(testcase.TestCase.EX_RUN_ERROR, *args)
329 @mock.patch('robot.run')
330 @mock.patch('os.makedirs')
331 def test_parse_results_ko(self, *args):
332 with mock.patch.object(self.test, 'set_robotframework_vars',
333 return_value=True), \
334 mock.patch.object(self.test, 'parse_results',
335 side_effect=RobotError):
336 self._test_run_suites(testcase.TestCase.EX_RUN_ERROR, *args)
338 @mock.patch('robot.run')
339 @mock.patch('os.makedirs')
340 def test_ok(self, *args):
341 with mock.patch.object(self.test, 'set_robotframework_vars',
342 return_value=True), \
343 mock.patch.object(self.test, 'parse_results'):
344 self._test_run_suites(testcase.TestCase.EX_OK, *args)
346 @mock.patch('robot.run')
347 @mock.patch('os.makedirs', side_effect=OSError(errno.EEXIST, ''))
348 def test_makedirs_oserror17(self, *args):
349 with mock.patch.object(self.test, 'set_robotframework_vars',
350 return_value=True), \
351 mock.patch.object(self.test, 'parse_results'):
352 self._test_run_suites(testcase.TestCase.EX_OK, *args)
354 @mock.patch('robot.run', return_value=1)
355 @mock.patch('os.makedirs')
356 def test_testcases_in_failure(self, *args):
357 with mock.patch.object(self.test, 'set_robotframework_vars',
358 return_value=True), \
359 mock.patch.object(self.test, 'parse_results'):
360 self._test_run_suites(testcase.TestCase.EX_OK, *args)
363 class ODLRunTesting(ODLTesting):
365 """The class testing ODLTests.run()."""
366 # pylint: disable=missing-docstring
368 def _test_no_env_var(self, var):
369 with mock.patch('functest.utils.openstack_utils.get_endpoint',
370 return_value="http://{}:9696".format(
371 ODLTesting._neutron_ip)):
373 self.assertEqual(self.test.run(),
374 testcase.TestCase.EX_RUN_ERROR)
376 def _test_run(self, status=testcase.TestCase.EX_OK,
377 exception=None, **kwargs):
378 odlip = kwargs['odlip'] if 'odlip' in kwargs else '127.0.0.3'
379 odlwebport = kwargs['odlwebport'] if 'odlwebport' in kwargs else '8080'
380 odlrestconfport = (kwargs['odlrestconfport']
381 if 'odlrestconfport' in kwargs else '8181')
383 with mock.patch('functest.utils.openstack_utils.get_endpoint',
384 return_value="http://{}:9696".format(
385 ODLTesting._neutron_ip)):
387 self.test.run_suites = mock.Mock(side_effect=exception)
389 self.test.run_suites = mock.Mock(return_value=status)
390 self.assertEqual(self.test.run(), status)
391 self.test.run_suites.assert_called_once_with(
392 odl.ODLTests.default_suites,
393 neutronip=self._neutron_ip,
394 odlip=odlip, odlpassword=self._odl_password,
395 odlrestconfport=odlrestconfport,
396 odlusername=self._odl_username, odlwebport=odlwebport,
397 osauthurl=self._os_auth_url,
398 ospassword=self._os_password, ostenantname=self._os_tenantname,
399 osusername=self._os_username,
400 osprojectdomainname=self._os_projectdomainname,
401 osuserdomainname=self._os_userdomainname)
403 def _test_multiple_suites(self, suites,
404 status=testcase.TestCase.EX_OK, **kwargs):
405 odlip = kwargs['odlip'] if 'odlip' in kwargs else '127.0.0.3'
406 odlwebport = kwargs['odlwebport'] if 'odlwebport' in kwargs else '8080'
407 odlrestconfport = (kwargs['odlrestconfport']
408 if 'odlrestconfport' in kwargs else '8181')
409 with mock.patch('functest.utils.openstack_utils.get_endpoint',
410 return_value="http://{}:9696".format(
411 ODLTesting._neutron_ip)):
412 self.test.run_suites = mock.Mock(return_value=status)
413 self.assertEqual(self.test.run(suites=suites), status)
414 self.test.run_suites.assert_called_once_with(
416 neutronip=self._neutron_ip,
417 odlip=odlip, odlpassword=self._odl_password,
418 odlrestconfport=odlrestconfport,
419 odlusername=self._odl_username, odlwebport=odlwebport,
420 osauthurl=self._os_auth_url,
421 ospassword=self._os_password, ostenantname=self._os_tenantname,
422 osusername=self._os_username,
423 osprojectdomainname=self._os_projectdomainname,
424 osuserdomainname=self._os_userdomainname)
427 with mock.patch('functest.utils.openstack_utils.get_endpoint',
428 side_effect=auth_plugins.MissingAuthPlugin()):
429 self.assertEqual(self.test.run(),
430 testcase.TestCase.EX_RUN_ERROR)
432 def test_no_os_auth_url(self):
433 self._test_no_env_var("OS_AUTH_URL")
435 def test_no_os_username(self):
436 self._test_no_env_var("OS_USERNAME")
438 def test_no_os_password(self):
439 self._test_no_env_var("OS_PASSWORD")
441 def test_no_os_tenant_name(self):
442 self._test_no_env_var("OS_TENANT_NAME")
444 def test_run_suites_false(self):
445 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
446 self._test_run(testcase.TestCase.EX_RUN_ERROR,
447 odlip=self._sdn_controller_ip,
448 odlwebport=self._odl_webport)
450 def test_run_suites_exc(self):
451 with self.assertRaises(Exception):
452 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
453 self._test_run(status=testcase.TestCase.EX_RUN_ERROR,
454 exception=Exception(),
455 odlip=self._sdn_controller_ip,
456 odlwebport=self._odl_webport)
458 def test_no_sdn_controller_ip(self):
459 with mock.patch('functest.utils.openstack_utils.get_endpoint',
460 return_value="http://{}:9696".format(
461 ODLTesting._neutron_ip)):
462 self.assertEqual(self.test.run(),
463 testcase.TestCase.EX_RUN_ERROR)
465 def test_without_installer_type(self):
466 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
467 self._test_run(testcase.TestCase.EX_OK,
468 odlip=self._sdn_controller_ip,
469 odlwebport=self._odl_webport)
471 def test_suites(self):
472 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
473 self._test_multiple_suites(
474 [odl.ODLTests.basic_suite_dir],
475 testcase.TestCase.EX_OK,
476 odlip=self._sdn_controller_ip,
477 odlwebport=self._odl_webport)
480 os.environ["INSTALLER_TYPE"] = "fuel"
481 self._test_run(testcase.TestCase.EX_OK,
482 odlip=self._neutron_ip, odlwebport='8181',
483 odlrestconfport='8282')
485 def test_apex_no_controller_ip(self):
486 with mock.patch('functest.utils.openstack_utils.get_endpoint',
487 return_value="http://{}:9696".format(
488 ODLTesting._neutron_ip)):
489 os.environ["INSTALLER_TYPE"] = "apex"
490 self.assertEqual(self.test.run(),
491 testcase.TestCase.EX_RUN_ERROR)
494 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
495 os.environ["INSTALLER_TYPE"] = "apex"
496 self._test_run(testcase.TestCase.EX_OK,
497 odlip=self._sdn_controller_ip, odlwebport='8081',
498 odlrestconfport='8081')
500 def test_netvirt_no_controller_ip(self):
501 with mock.patch('functest.utils.openstack_utils.get_endpoint',
502 return_value="http://{}:9696".format(
503 ODLTesting._neutron_ip)):
504 os.environ["INSTALLER_TYPE"] = "netvirt"
505 self.assertEqual(self.test.run(),
506 testcase.TestCase.EX_RUN_ERROR)
508 def test_netvirt(self):
509 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
510 os.environ["INSTALLER_TYPE"] = "netvirt"
511 self._test_run(testcase.TestCase.EX_OK,
512 odlip=self._sdn_controller_ip, odlwebport='8081',
513 odlrestconfport='8081')
515 def test_joid_no_controller_ip(self):
516 with mock.patch('functest.utils.openstack_utils.get_endpoint',
517 return_value="http://{}:9696".format(
518 ODLTesting._neutron_ip)):
519 os.environ["INSTALLER_TYPE"] = "joid"
520 self.assertEqual(self.test.run(),
521 testcase.TestCase.EX_RUN_ERROR)
524 os.environ["SDN_CONTROLLER"] = self._sdn_controller_ip
525 os.environ["INSTALLER_TYPE"] = "joid"
526 self._test_run(testcase.TestCase.EX_OK,
527 odlip=self._sdn_controller_ip, odlwebport='8080')
529 def test_compass(self):
530 os.environ["INSTALLER_TYPE"] = "compass"
531 self._test_run(testcase.TestCase.EX_OK,
532 odlip=self._neutron_ip, odlrestconfport='8080')
534 def test_daisy_no_controller_ip(self):
535 with mock.patch('functest.utils.openstack_utils.get_endpoint',
536 return_value="http://{}:9696".format(
537 ODLTesting._neutron_ip)):
538 os.environ["INSTALLER_TYPE"] = "daisy"
539 self.assertEqual(self.test.run(),
540 testcase.TestCase.EX_RUN_ERROR)
542 def test_daisy(self):
543 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
544 os.environ["INSTALLER_TYPE"] = "daisy"
545 self._test_run(testcase.TestCase.EX_OK,
546 odlip=self._sdn_controller_ip, odlwebport='8181',
547 odlrestconfport='8087')
550 class ODLArgParserTesting(ODLTesting):
552 """The class testing ODLParser."""
553 # pylint: disable=missing-docstring
556 self.parser = odl.ODLParser()
557 super(ODLArgParserTesting, self).setUp()
559 def test_default(self):
560 self.assertEqual(self.parser.parse_args(), self.defaultargs)
562 def test_basic(self):
563 self.defaultargs['neutronip'] = self._neutron_ip
564 self.defaultargs['odlip'] = self._sdn_controller_ip
566 self.parser.parse_args(
567 ["--neutronip={}".format(self._neutron_ip),
568 "--odlip={}".format(self._sdn_controller_ip)]),
571 @mock.patch('sys.stderr', new_callable=six.StringIO)
572 def test_fail(self, mock_method):
573 self.defaultargs['foo'] = 'bar'
574 with self.assertRaises(SystemExit):
575 self.parser.parse_args(["--foo=bar"])
576 self.assertTrue(mock_method.getvalue().startswith("usage:"))
578 def _test_arg(self, arg, value):
579 self.defaultargs[arg] = value
581 self.parser.parse_args(["--{}={}".format(arg, value)]),
584 def test_odlusername(self):
585 self._test_arg('odlusername', 'foo')
587 def test_odlpassword(self):
588 self._test_arg('odlpassword', 'foo')
590 def test_osauthurl(self):
591 self._test_arg('osauthurl', 'http://127.0.0.4:5000/v2')
593 def test_neutronip(self):
594 self._test_arg('neutronip', '127.0.0.4')
596 def test_osusername(self):
597 self._test_arg('osusername', 'foo')
599 def test_osuserdomainname(self):
600 self._test_arg('osuserdomainname', 'domain')
602 def test_ostenantname(self):
603 self._test_arg('ostenantname', 'foo')
605 def test_osprojectdomainname(self):
606 self._test_arg('osprojectdomainname', 'domain')
608 def test_ospassword(self):
609 self._test_arg('ospassword', 'foo')
611 def test_odlip(self):
612 self._test_arg('odlip', '127.0.0.4')
614 def test_odlwebport(self):
615 self._test_arg('odlwebport', '80')
617 def test_odlrestconfport(self):
618 self._test_arg('odlrestconfport', '80')
620 def test_pushtodb(self):
621 self.defaultargs['pushtodb'] = True
622 self.assertEqual(self.parser.parse_args(["--{}".format('pushtodb')]),
625 def test_multiple_args(self):
626 self.defaultargs['neutronip'] = self._neutron_ip
627 self.defaultargs['odlip'] = self._sdn_controller_ip
629 self.parser.parse_args(
630 ["--neutronip={}".format(self._neutron_ip),
631 "--odlip={}".format(self._sdn_controller_ip)]),
635 if __name__ == "__main__":
636 logging.disable(logging.CRITICAL)
637 unittest.main(verbosity=2)