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."""
18 from keystoneauth1.exceptions import auth_plugins
20 from robot.errors import DataError, RobotError
21 from robot.result import testcase as result_testcase
22 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
35 logging.disable(logging.CRITICAL)
38 self.visitor = odl.ODLResultVisitor()
41 self.assertFalse(self.visitor.get_data())
44 data = {'name': 'foo',
47 'starttime': "20161216 16:00:00.000",
48 'endtime': "20161216 16:00:01.000",
50 'text': 'Hello, World!',
52 test = result_testcase.TestCase(name=data['name'],
53 status=data['status'],
55 starttime=data['starttime'],
56 endtime=data['endtime'])
57 test.parent = mock.Mock()
58 config = {'name': data['parent'],
59 'criticality.test_is_critical.return_value': data[
61 test.parent.configure_mock(**config)
62 self.visitor.visit_test(test)
63 self.assertEqual(self.visitor.get_data(), [data])
66 class ODLTesting(unittest.TestCase):
68 """The super class which testing classes could inherit."""
69 # pylint: disable=missing-docstring
71 logging.disable(logging.CRITICAL)
73 _keystone_ip = "127.0.0.1"
74 _neutron_ip = "127.0.0.2"
75 _sdn_controller_ip = "127.0.0.3"
76 _os_auth_url = "http://{}:5000/v2.0".format(_keystone_ip)
77 _os_tenantname = "admin"
78 _os_username = "admin"
79 _os_password = "admin"
81 _odl_restconfport = "8181"
82 _odl_username = "admin"
83 _odl_password = "admin"
86 for var in ("INSTALLER_TYPE", "SDN_CONTROLLER", "SDN_CONTROLLER_IP"):
89 os.environ["OS_AUTH_URL"] = self._os_auth_url
90 os.environ["OS_USERNAME"] = self._os_username
91 os.environ["OS_PASSWORD"] = self._os_password
92 os.environ["OS_TENANT_NAME"] = self._os_tenantname
93 self.test = odl.ODLTests(case_name='odl', project_name='functest')
94 self.defaultargs = {'odlusername': self._odl_username,
95 'odlpassword': self._odl_password,
96 'neutronip': self._keystone_ip,
97 'osauthurl': self._os_auth_url,
98 'osusername': self._os_username,
99 'ostenantname': self._os_tenantname,
100 'ospassword': self._os_password,
101 'odlip': self._keystone_ip,
102 'odlwebport': self._odl_webport,
103 'odlrestconfport': self._odl_restconfport,
107 class ODLParseResultTesting(ODLTesting):
109 """The class testing ODLTests.parse_results()."""
110 # pylint: disable=missing-docstring
112 _config = {'name': 'dummy', 'starttime': '20161216 16:00:00.000',
113 'endtime': '20161216 16:00:01.000'}
115 @mock.patch('robot.api.ExecutionResult', side_effect=DataError)
116 def test_raises_exc(self, mock_method):
117 with self.assertRaises(DataError):
118 self.test.parse_results()
119 mock_method.assert_called_once_with(
120 os.path.join(odl.ODLTests.res_dir, 'output.xml'))
122 def _test_result(self, config, result):
124 suite.configure_mock(**config)
125 with mock.patch('robot.api.ExecutionResult',
126 return_value=mock.Mock(suite=suite)):
127 self.test.parse_results()
128 self.assertEqual(self.test.result, result)
129 self.assertEqual(self.test.start_time,
130 timestamp_to_secs(config['starttime']))
131 self.assertEqual(self.test.stop_time,
132 timestamp_to_secs(config['endtime']))
133 self.assertEqual(self.test.details,
134 {'description': config['name'], 'tests': []})
136 def test_null_passed(self):
137 self._config.update({'statistics.critical.passed': 0,
138 'statistics.critical.total': 20})
139 self._test_result(self._config, 0)
141 def test_no_test(self):
142 self._config.update({'statistics.critical.passed': 20,
143 'statistics.critical.total': 0})
144 self._test_result(self._config, 0)
146 def test_half_success(self):
147 self._config.update({'statistics.critical.passed': 10,
148 'statistics.critical.total': 20})
149 self._test_result(self._config, 50)
151 def test_success(self):
152 self._config.update({'statistics.critical.passed': 20,
153 'statistics.critical.total': 20})
154 self._test_result(self._config, 100)
157 class ODLRobotTesting(ODLTesting):
159 """The class testing ODLTests.set_robotframework_vars()."""
160 # pylint: disable=missing-docstring
162 @mock.patch('fileinput.input', side_effect=Exception())
163 def test_set_vars_ko(self, mock_method):
164 self.assertFalse(self.test.set_robotframework_vars())
165 mock_method.assert_called_once_with(
166 os.path.join(odl.ODLTests.odl_test_repo,
167 'csit/variables/Variables.py'), inplace=True)
169 @mock.patch('fileinput.input', return_value=[])
170 def test_set_vars_empty(self, mock_method):
171 self.assertTrue(self.test.set_robotframework_vars())
172 mock_method.assert_called_once_with(
173 os.path.join(odl.ODLTests.odl_test_repo,
174 'csit/variables/Variables.py'), inplace=True)
176 @mock.patch('sys.stdout', new_callable=StringIO.StringIO)
177 def _test_set_vars(self, msg1, msg2, *args):
178 line = mock.MagicMock()
179 line.__iter__.return_value = [msg1]
180 with mock.patch('fileinput.input', return_value=line) as mock_method:
181 self.assertTrue(self.test.set_robotframework_vars())
182 mock_method.assert_called_once_with(
183 os.path.join(odl.ODLTests.odl_test_repo,
184 'csit/variables/Variables.py'), inplace=True)
185 self.assertEqual(args[0].getvalue(), "{}\n".format(msg2))
187 def test_set_vars_auth_default(self):
188 self._test_set_vars("AUTH = []",
189 "AUTH = [u'admin', u'admin']")
191 def test_set_vars_auth1(self):
192 self._test_set_vars("AUTH1 = []", "AUTH1 = []")
194 @mock.patch('sys.stdout', new_callable=StringIO.StringIO)
195 def test_set_vars_auth_foo(self, *args):
196 line = mock.MagicMock()
197 line.__iter__.return_value = ["AUTH = []"]
198 with mock.patch('fileinput.input', return_value=line) as mock_method:
199 self.assertTrue(self.test.set_robotframework_vars('foo', 'bar'))
200 mock_method.assert_called_once_with(
201 os.path.join(odl.ODLTests.odl_test_repo,
202 'csit/variables/Variables.py'), inplace=True)
203 self.assertEqual(args[0].getvalue(),
204 "AUTH = [u'{}', u'{}']\n".format('foo', 'bar'))
207 class ODLMainTesting(ODLTesting):
209 """The class testing ODLTests.main()."""
210 # pylint: disable=missing-docstring
212 def _get_main_kwargs(self, key=None):
213 kwargs = {'odlusername': self._odl_username,
214 'odlpassword': self._odl_password,
215 'neutronip': self._neutron_ip,
216 'osauthurl': self._os_auth_url,
217 'osusername': self._os_username,
218 'ostenantname': self._os_tenantname,
219 'ospassword': self._os_password,
220 'odlip': self._sdn_controller_ip,
221 'odlwebport': self._odl_webport,
222 'odlrestconfport': self._odl_restconfport}
227 def _test_main(self, status, *args):
228 kwargs = self._get_main_kwargs()
229 self.assertEqual(self.test.main(**kwargs), status)
231 args[0].assert_called_once_with(
232 odl.ODLTests.res_dir)
234 variable = ['KEYSTONE:{}'.format(self._keystone_ip),
235 'NEUTRON:{}'.format(self._neutron_ip),
236 'OS_AUTH_URL:"{}"'.format(self._os_auth_url),
237 'OSUSERNAME:"{}"'.format(self._os_username),
238 'OSTENANTNAME:"{}"'.format(self._os_tenantname),
239 'OSPASSWORD:"{}"'.format(self._os_password),
240 'ODL_SYSTEM_IP:{}'.format(self._sdn_controller_ip),
241 'PORT:{}'.format(self._odl_webport),
242 'RESTCONFPORT:{}'.format(self._odl_restconfport)]
243 args[1].assert_called_once_with(
244 odl.ODLTests.basic_suite_dir,
245 odl.ODLTests.neutron_suite_dir,
247 output=os.path.join(odl.ODLTests.res_dir, 'output.xml'),
252 args[2].assert_called_with(
253 os.path.join(odl.ODLTests.res_dir, 'stdout.txt'))
255 def _test_no_keyword(self, key):
256 kwargs = self._get_main_kwargs(key)
257 self.assertEqual(self.test.main(**kwargs),
258 testcase.TestCase.EX_RUN_ERROR)
260 def test_no_odlusername(self):
261 self._test_no_keyword('odlusername')
263 def test_no_odlpassword(self):
264 self._test_no_keyword('odlpassword')
266 def test_no_neutronip(self):
267 self._test_no_keyword('neutronip')
269 def test_no_osauthurl(self):
270 self._test_no_keyword('osauthurl')
272 def test_no_osusername(self):
273 self._test_no_keyword('osusername')
275 def test_no_ostenantname(self):
276 self._test_no_keyword('ostenantname')
278 def test_no_ospassword(self):
279 self._test_no_keyword('ospassword')
281 def test_no_odlip(self):
282 self._test_no_keyword('odlip')
284 def test_no_odlwebport(self):
285 self._test_no_keyword('odlwebport')
287 def test_no_odlrestconfport(self):
288 self._test_no_keyword('odlrestconfport')
290 def test_set_vars_ko(self):
291 with mock.patch.object(self.test, 'set_robotframework_vars',
292 return_value=False) as mock_object:
293 self._test_main(testcase.TestCase.EX_RUN_ERROR)
294 mock_object.assert_called_once_with(
295 self._odl_username, self._odl_password)
297 @mock.patch('os.makedirs', side_effect=Exception)
298 def test_makedirs_exc(self, mock_method):
299 with mock.patch.object(self.test, 'set_robotframework_vars',
300 return_value=True), \
301 self.assertRaises(Exception):
302 self._test_main(testcase.TestCase.EX_RUN_ERROR,
305 @mock.patch('os.makedirs', side_effect=OSError)
306 def test_makedirs_oserror(self, mock_method):
307 with mock.patch.object(self.test, 'set_robotframework_vars',
309 self._test_main(testcase.TestCase.EX_RUN_ERROR,
312 @mock.patch('robot.run', side_effect=RobotError)
313 @mock.patch('os.makedirs')
314 def test_run_ko(self, *args):
315 with mock.patch.object(self.test, 'set_robotframework_vars',
316 return_value=True), \
317 mock.patch.object(odl, 'open', mock.mock_open(),
319 self.assertRaises(RobotError):
320 self._test_main(testcase.TestCase.EX_RUN_ERROR, *args)
322 @mock.patch('robot.run')
323 @mock.patch('os.makedirs')
324 def test_parse_results_ko(self, *args):
325 with mock.patch.object(self.test, 'set_robotframework_vars',
326 return_value=True), \
327 mock.patch.object(odl, 'open', mock.mock_open(),
329 mock.patch.object(self.test, 'parse_results',
330 side_effect=RobotError):
331 self._test_main(testcase.TestCase.EX_RUN_ERROR, *args)
333 @mock.patch('os.remove', side_effect=Exception)
334 @mock.patch('robot.run')
335 @mock.patch('os.makedirs')
336 def test_remove_exc(self, *args):
337 with mock.patch.object(self.test, 'set_robotframework_vars',
338 return_value=True), \
339 mock.patch.object(self.test, 'parse_results'), \
340 self.assertRaises(Exception):
341 self._test_main(testcase.TestCase.EX_OK, *args)
343 @mock.patch('os.remove')
344 @mock.patch('robot.run')
345 @mock.patch('os.makedirs')
346 def test_ok(self, *args):
347 with mock.patch.object(self.test, 'set_robotframework_vars',
348 return_value=True), \
349 mock.patch.object(odl, 'open', mock.mock_open(),
351 mock.patch.object(self.test, 'parse_results'):
352 self._test_main(testcase.TestCase.EX_OK, *args)
354 @mock.patch('os.remove')
355 @mock.patch('robot.run')
356 @mock.patch('os.makedirs', side_effect=OSError(errno.EEXIST, ''))
357 def test_makedirs_oserror17(self, *args):
358 with mock.patch.object(self.test, 'set_robotframework_vars',
359 return_value=True), \
360 mock.patch.object(odl, 'open', mock.mock_open(),
361 create=True) as mock_open, \
362 mock.patch.object(self.test, 'parse_results'):
363 self._test_main(testcase.TestCase.EX_OK, *args)
364 mock_open.assert_called_once_with(
365 os.path.join(odl.ODLTests.res_dir, 'stdout.txt'), 'w+')
367 @mock.patch('os.remove')
368 @mock.patch('robot.run', return_value=1)
369 @mock.patch('os.makedirs')
370 def test_testcases_in_failure(self, *args):
371 with mock.patch.object(self.test, 'set_robotframework_vars',
372 return_value=True), \
373 mock.patch.object(odl, 'open', mock.mock_open(),
374 create=True) as mock_open, \
375 mock.patch.object(self.test, 'parse_results'):
376 self._test_main(testcase.TestCase.EX_OK, *args)
377 mock_open.assert_called_once_with(
378 os.path.join(odl.ODLTests.res_dir, 'stdout.txt'), 'w+')
380 @mock.patch('os.remove', side_effect=OSError)
381 @mock.patch('robot.run')
382 @mock.patch('os.makedirs')
383 def test_remove_oserror(self, *args):
384 with mock.patch.object(self.test, 'set_robotframework_vars',
385 return_value=True), \
386 mock.patch.object(odl, 'open', mock.mock_open(),
387 create=True) as mock_open, \
388 mock.patch.object(self.test, 'parse_results'):
389 self._test_main(testcase.TestCase.EX_OK, *args)
390 mock_open.assert_called_once_with(
391 os.path.join(odl.ODLTests.res_dir, 'stdout.txt'), 'w+')
394 class ODLRunTesting(ODLTesting):
396 """The class testing ODLTests.run()."""
397 # pylint: disable=missing-docstring
399 def _test_no_env_var(self, var):
400 with mock.patch('functest.utils.openstack_utils.get_endpoint',
401 return_value="http://{}:9696".format(
402 ODLTesting._neutron_ip)):
404 self.assertEqual(self.test.run(),
405 testcase.TestCase.EX_RUN_ERROR)
407 def _test_run(self, status=testcase.TestCase.EX_OK,
408 exception=None, **kwargs):
409 odlip = kwargs['odlip'] if 'odlip' in kwargs else '127.0.0.3'
410 odlwebport = kwargs['odlwebport'] if 'odlwebport' in kwargs else '8080'
411 odlrestconfport = (kwargs['odlrestconfport']
412 if 'odlrestconfport' in kwargs else '8181')
414 with mock.patch('functest.utils.openstack_utils.get_endpoint',
415 return_value="http://{}:9696".format(
416 ODLTesting._neutron_ip)):
418 self.test.main = mock.Mock(side_effect=exception)
420 self.test.main = mock.Mock(return_value=status)
421 self.assertEqual(self.test.run(), status)
422 self.test.main.assert_called_once_with(
423 odl.ODLTests.default_suites,
424 neutronip=self._neutron_ip,
425 odlip=odlip, odlpassword=self._odl_password,
426 odlrestconfport=odlrestconfport,
427 odlusername=self._odl_username, odlwebport=odlwebport,
428 osauthurl=self._os_auth_url,
429 ospassword=self._os_password, ostenantname=self._os_tenantname,
430 osusername=self._os_username)
432 def _test_multiple_suites(self, suites,
433 status=testcase.TestCase.EX_OK, **kwargs):
434 odlip = kwargs['odlip'] if 'odlip' in kwargs else '127.0.0.3'
435 odlwebport = kwargs['odlwebport'] if 'odlwebport' in kwargs else '8080'
436 odlrestconfport = (kwargs['odlrestconfport']
437 if 'odlrestconfport' in kwargs else '8181')
438 with mock.patch('functest.utils.openstack_utils.get_endpoint',
439 return_value="http://{}:9696".format(
440 ODLTesting._neutron_ip)):
441 self.test.main = mock.Mock(return_value=status)
442 self.assertEqual(self.test.run(suites=suites), status)
443 self.test.main.assert_called_once_with(
445 neutronip=self._neutron_ip,
446 odlip=odlip, odlpassword=self._odl_password,
447 odlrestconfport=odlrestconfport,
448 odlusername=self._odl_username, odlwebport=odlwebport,
449 osauthurl=self._os_auth_url,
450 ospassword=self._os_password, ostenantname=self._os_tenantname,
451 osusername=self._os_username)
454 with mock.patch('functest.utils.openstack_utils.get_endpoint',
455 side_effect=auth_plugins.MissingAuthPlugin()):
456 self.assertEqual(self.test.run(),
457 testcase.TestCase.EX_RUN_ERROR)
459 def test_no_os_auth_url(self):
460 self._test_no_env_var("OS_AUTH_URL")
462 def test_no_os_username(self):
463 self._test_no_env_var("OS_USERNAME")
465 def test_no_os_password(self):
466 self._test_no_env_var("OS_PASSWORD")
468 def test_no_os_tenant_name(self):
469 self._test_no_env_var("OS_TENANT_NAME")
471 def test_main_false(self):
472 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
473 self._test_run(testcase.TestCase.EX_RUN_ERROR,
474 odlip=self._sdn_controller_ip,
475 odlwebport=self._odl_webport)
477 def test_main_exc(self):
478 with self.assertRaises(Exception):
479 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
480 self._test_run(status=testcase.TestCase.EX_RUN_ERROR,
481 exception=Exception(),
482 odlip=self._sdn_controller_ip,
483 odlwebport=self._odl_webport)
485 def test_no_sdn_controller_ip(self):
486 with mock.patch('functest.utils.openstack_utils.get_endpoint',
487 return_value="http://{}:9696".format(
488 ODLTesting._neutron_ip)):
489 self.assertEqual(self.test.run(),
490 testcase.TestCase.EX_RUN_ERROR)
492 def test_without_installer_type(self):
493 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
494 self._test_run(testcase.TestCase.EX_OK,
495 odlip=self._sdn_controller_ip,
496 odlwebport=self._odl_webport)
498 def test_suites(self):
499 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
500 self._test_multiple_suites(
501 [odl.ODLTests.basic_suite_dir],
502 testcase.TestCase.EX_OK,
503 odlip=self._sdn_controller_ip,
504 odlwebport=self._odl_webport)
507 os.environ["INSTALLER_TYPE"] = "fuel"
508 self._test_run(testcase.TestCase.EX_OK,
509 odlip=self._neutron_ip, odlwebport='8282')
511 def test_apex_no_controller_ip(self):
512 with mock.patch('functest.utils.openstack_utils.get_endpoint',
513 return_value="http://{}:9696".format(
514 ODLTesting._neutron_ip)):
515 os.environ["INSTALLER_TYPE"] = "apex"
516 self.assertEqual(self.test.run(),
517 testcase.TestCase.EX_RUN_ERROR)
520 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
521 os.environ["INSTALLER_TYPE"] = "apex"
522 self._test_run(testcase.TestCase.EX_OK,
523 odlip=self._sdn_controller_ip, odlwebport='8081',
524 odlrestconfport='8081')
526 def test_netvirt_no_controller_ip(self):
527 with mock.patch('functest.utils.openstack_utils.get_endpoint',
528 return_value="http://{}:9696".format(
529 ODLTesting._neutron_ip)):
530 os.environ["INSTALLER_TYPE"] = "netvirt"
531 self.assertEqual(self.test.run(),
532 testcase.TestCase.EX_RUN_ERROR)
534 def test_netvirt(self):
535 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
536 os.environ["INSTALLER_TYPE"] = "netvirt"
537 self._test_run(testcase.TestCase.EX_OK,
538 odlip=self._sdn_controller_ip, odlwebport='8081',
539 odlrestconfport='8081')
541 def test_joid_no_controller_ip(self):
542 with mock.patch('functest.utils.openstack_utils.get_endpoint',
543 return_value="http://{}:9696".format(
544 ODLTesting._neutron_ip)):
545 os.environ["INSTALLER_TYPE"] = "joid"
546 self.assertEqual(self.test.run(),
547 testcase.TestCase.EX_RUN_ERROR)
550 os.environ["SDN_CONTROLLER"] = self._sdn_controller_ip
551 os.environ["INSTALLER_TYPE"] = "joid"
552 self._test_run(testcase.TestCase.EX_OK,
553 odlip=self._sdn_controller_ip, odlwebport='8080')
555 def test_compass(self):
556 os.environ["INSTALLER_TYPE"] = "compass"
557 self._test_run(testcase.TestCase.EX_OK,
558 odlip=self._neutron_ip, odlwebport='8181')
561 class ODLArgParserTesting(ODLTesting):
563 """The class testing ODLParser."""
564 # pylint: disable=missing-docstring
567 self.parser = odl.ODLParser()
568 super(ODLArgParserTesting, self).setUp()
570 def test_default(self):
571 self.assertEqual(self.parser.parse_args(), self.defaultargs)
573 def test_basic(self):
574 self.defaultargs['neutronip'] = self._neutron_ip
575 self.defaultargs['odlip'] = self._sdn_controller_ip
577 self.parser.parse_args(
578 ["--neutronip={}".format(self._neutron_ip),
579 "--odlip={}".format(self._sdn_controller_ip)]),
582 @mock.patch('sys.stderr', new_callable=StringIO.StringIO)
583 def test_fail(self, mock_method):
584 self.defaultargs['foo'] = 'bar'
585 with self.assertRaises(SystemExit):
586 self.parser.parse_args(["--foo=bar"])
587 self.assertTrue(mock_method.getvalue().startswith("usage:"))
589 def _test_arg(self, arg, value):
590 self.defaultargs[arg] = value
592 self.parser.parse_args(["--{}={}".format(arg, value)]),
595 def test_odlusername(self):
596 self._test_arg('odlusername', 'foo')
598 def test_odlpassword(self):
599 self._test_arg('odlpassword', 'foo')
601 def test_osauthurl(self):
602 self._test_arg('osauthurl', 'http://127.0.0.4:5000/v2')
604 def test_neutronip(self):
605 self._test_arg('neutronip', '127.0.0.4')
607 def test_osusername(self):
608 self._test_arg('osusername', 'foo')
610 def test_ostenantname(self):
611 self._test_arg('ostenantname', 'foo')
613 def test_ospassword(self):
614 self._test_arg('ospassword', 'foo')
616 def test_odlip(self):
617 self._test_arg('odlip', '127.0.0.4')
619 def test_odlwebport(self):
620 self._test_arg('odlwebport', '80')
622 def test_odlrestconfport(self):
623 self._test_arg('odlrestconfport', '80')
625 def test_pushtodb(self):
626 self.defaultargs['pushtodb'] = True
627 self.assertEqual(self.parser.parse_args(["--{}".format('pushtodb')]),
630 def test_multiple_args(self):
631 self.defaultargs['neutronip'] = self._neutron_ip
632 self.defaultargs['odlip'] = self._sdn_controller_ip
634 self.parser.parse_args(
635 ["--neutronip={}".format(self._neutron_ip),
636 "--odlip={}".format(self._sdn_controller_ip)]),
640 if __name__ == "__main__":
641 unittest.main(verbosity=2)