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 model
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 = model.TestCase(
53 name=data['name'], status=data['status'], message=data['text'],
54 starttime=data['starttime'], endtime=data['endtime'])
55 test.parent = mock.Mock()
56 config = {'name': data['parent'],
57 'criticality.test_is_critical.return_value': data[
59 test.parent.configure_mock(**config)
60 self.visitor.visit_test(test)
61 self.assertEqual(self.visitor.get_data(), [data])
64 class ODLTesting(unittest.TestCase):
66 """The super class which testing classes could inherit."""
67 # pylint: disable=missing-docstring
69 logging.disable(logging.CRITICAL)
71 _keystone_ip = "127.0.0.1"
72 _neutron_ip = "127.0.0.2"
73 _sdn_controller_ip = "127.0.0.3"
74 _os_auth_url = "http://{}:5000/v2.0".format(_keystone_ip)
75 _os_tenantname = "admin"
76 _os_username = "admin"
77 _os_password = "admin"
79 _odl_restconfport = "8181"
80 _odl_username = "admin"
81 _odl_password = "admin"
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_PASSWORD"] = self._os_password
90 os.environ["OS_TENANT_NAME"] = self._os_tenantname
91 self.test = odl.ODLTests(case_name='odl', project_name='functest')
92 self.defaultargs = {'odlusername': self._odl_username,
93 'odlpassword': self._odl_password,
94 'neutronip': self._keystone_ip,
95 'osauthurl': self._os_auth_url,
96 'osusername': self._os_username,
97 'ostenantname': self._os_tenantname,
98 'ospassword': self._os_password,
99 'odlip': self._keystone_ip,
100 'odlwebport': self._odl_webport,
101 'odlrestconfport': self._odl_restconfport,
105 class ODLParseResultTesting(ODLTesting):
107 """The class testing ODLTests.parse_results()."""
108 # pylint: disable=missing-docstring
110 _config = {'name': 'dummy', 'starttime': '20161216 16:00:00.000',
111 'endtime': '20161216 16:00:01.000'}
113 @mock.patch('robot.api.ExecutionResult', side_effect=DataError)
114 def test_raises_exc(self, mock_method):
115 with self.assertRaises(DataError):
116 self.test.parse_results()
117 mock_method.assert_called_once_with(
118 os.path.join(odl.ODLTests.res_dir, 'output.xml'))
120 def _test_result(self, config, result):
122 suite.configure_mock(**config)
123 with mock.patch('robot.api.ExecutionResult',
124 return_value=mock.Mock(suite=suite)):
125 self.test.parse_results()
126 self.assertEqual(self.test.result, result)
127 self.assertEqual(self.test.start_time,
128 timestamp_to_secs(config['starttime']))
129 self.assertEqual(self.test.stop_time,
130 timestamp_to_secs(config['endtime']))
131 self.assertEqual(self.test.details,
132 {'description': config['name'], 'tests': []})
134 def test_null_passed(self):
135 self._config.update({'statistics.critical.passed': 0,
136 'statistics.critical.total': 20})
137 self._test_result(self._config, 0)
139 def test_no_test(self):
140 self._config.update({'statistics.critical.passed': 20,
141 'statistics.critical.total': 0})
142 self._test_result(self._config, 0)
144 def test_half_success(self):
145 self._config.update({'statistics.critical.passed': 10,
146 'statistics.critical.total': 20})
147 self._test_result(self._config, 50)
149 def test_success(self):
150 self._config.update({'statistics.critical.passed': 20,
151 'statistics.critical.total': 20})
152 self._test_result(self._config, 100)
155 class ODLRobotTesting(ODLTesting):
157 """The class testing ODLTests.set_robotframework_vars()."""
158 # pylint: disable=missing-docstring
160 @mock.patch('fileinput.input', side_effect=Exception())
161 def test_set_vars_ko(self, mock_method):
162 self.assertFalse(self.test.set_robotframework_vars())
163 mock_method.assert_called_once_with(
164 os.path.join(odl.ODLTests.odl_test_repo,
165 'csit/variables/Variables.py'), inplace=True)
167 @mock.patch('fileinput.input', return_value=[])
168 def test_set_vars_empty(self, mock_method):
169 self.assertTrue(self.test.set_robotframework_vars())
170 mock_method.assert_called_once_with(
171 os.path.join(odl.ODLTests.odl_test_repo,
172 'csit/variables/Variables.py'), inplace=True)
174 @mock.patch('sys.stdout', new_callable=StringIO.StringIO)
175 def _test_set_vars(self, msg1, msg2, *args):
176 line = mock.MagicMock()
177 line.__iter__.return_value = [msg1]
178 with mock.patch('fileinput.input', return_value=line) as mock_method:
179 self.assertTrue(self.test.set_robotframework_vars())
180 mock_method.assert_called_once_with(
181 os.path.join(odl.ODLTests.odl_test_repo,
182 'csit/variables/Variables.py'), inplace=True)
183 self.assertEqual(args[0].getvalue(), "{}\n".format(msg2))
185 def test_set_vars_auth_default(self):
186 self._test_set_vars("AUTH = []",
187 "AUTH = [u'admin', u'admin']")
189 def test_set_vars_auth1(self):
190 self._test_set_vars("AUTH1 = []", "AUTH1 = []")
192 @mock.patch('sys.stdout', new_callable=StringIO.StringIO)
193 def test_set_vars_auth_foo(self, *args):
194 line = mock.MagicMock()
195 line.__iter__.return_value = ["AUTH = []"]
196 with mock.patch('fileinput.input', return_value=line) as mock_method:
197 self.assertTrue(self.test.set_robotframework_vars('foo', 'bar'))
198 mock_method.assert_called_once_with(
199 os.path.join(odl.ODLTests.odl_test_repo,
200 'csit/variables/Variables.py'), inplace=True)
201 self.assertEqual(args[0].getvalue(),
202 "AUTH = [u'{}', u'{}']\n".format('foo', 'bar'))
205 class ODLMainTesting(ODLTesting):
207 """The class testing ODLTests.main()."""
208 # pylint: disable=missing-docstring
210 def _get_main_kwargs(self, key=None):
211 kwargs = {'odlusername': self._odl_username,
212 'odlpassword': self._odl_password,
213 'neutronip': self._neutron_ip,
214 'osauthurl': self._os_auth_url,
215 'osusername': self._os_username,
216 'ostenantname': self._os_tenantname,
217 'ospassword': self._os_password,
218 'odlip': self._sdn_controller_ip,
219 'odlwebport': self._odl_webport,
220 'odlrestconfport': self._odl_restconfport}
225 def _test_main(self, status, *args):
226 kwargs = self._get_main_kwargs()
227 self.assertEqual(self.test.main(**kwargs), status)
229 args[0].assert_called_once_with(
230 odl.ODLTests.res_dir)
232 variable = ['KEYSTONE:{}'.format(self._keystone_ip),
233 'NEUTRON:{}'.format(self._neutron_ip),
234 'OS_AUTH_URL:"{}"'.format(self._os_auth_url),
235 'OSUSERNAME:"{}"'.format(self._os_username),
236 'OSTENANTNAME:"{}"'.format(self._os_tenantname),
237 'OSPASSWORD:"{}"'.format(self._os_password),
238 'ODL_SYSTEM_IP:{}'.format(self._sdn_controller_ip),
239 'PORT:{}'.format(self._odl_webport),
240 'RESTCONFPORT:{}'.format(self._odl_restconfport)]
241 args[1].assert_called_once_with(
242 odl.ODLTests.basic_suite_dir,
243 odl.ODLTests.neutron_suite_dir,
245 output=os.path.join(odl.ODLTests.res_dir, 'output.xml'),
250 args[2].assert_called_with(
251 os.path.join(odl.ODLTests.res_dir, 'stdout.txt'))
253 def _test_no_keyword(self, key):
254 kwargs = self._get_main_kwargs(key)
255 self.assertEqual(self.test.main(**kwargs),
256 testcase.TestCase.EX_RUN_ERROR)
258 def test_no_odlusername(self):
259 self._test_no_keyword('odlusername')
261 def test_no_odlpassword(self):
262 self._test_no_keyword('odlpassword')
264 def test_no_neutronip(self):
265 self._test_no_keyword('neutronip')
267 def test_no_osauthurl(self):
268 self._test_no_keyword('osauthurl')
270 def test_no_osusername(self):
271 self._test_no_keyword('osusername')
273 def test_no_ostenantname(self):
274 self._test_no_keyword('ostenantname')
276 def test_no_ospassword(self):
277 self._test_no_keyword('ospassword')
279 def test_no_odlip(self):
280 self._test_no_keyword('odlip')
282 def test_no_odlwebport(self):
283 self._test_no_keyword('odlwebport')
285 def test_no_odlrestconfport(self):
286 self._test_no_keyword('odlrestconfport')
288 def test_set_vars_ko(self):
289 with mock.patch.object(self.test, 'set_robotframework_vars',
290 return_value=False) as mock_object:
291 self._test_main(testcase.TestCase.EX_RUN_ERROR)
292 mock_object.assert_called_once_with(
293 self._odl_username, self._odl_password)
295 @mock.patch('os.makedirs', side_effect=Exception)
296 def test_makedirs_exc(self, mock_method):
297 with mock.patch.object(self.test, 'set_robotframework_vars',
298 return_value=True), \
299 self.assertRaises(Exception):
300 self._test_main(testcase.TestCase.EX_RUN_ERROR,
303 @mock.patch('os.makedirs', side_effect=OSError)
304 def test_makedirs_oserror(self, mock_method):
305 with mock.patch.object(self.test, 'set_robotframework_vars',
307 self._test_main(testcase.TestCase.EX_RUN_ERROR,
310 @mock.patch('robot.run', side_effect=RobotError)
311 @mock.patch('os.makedirs')
312 def test_run_ko(self, *args):
313 with mock.patch.object(self.test, 'set_robotframework_vars',
314 return_value=True), \
315 mock.patch.object(odl, 'open', mock.mock_open(),
317 self.assertRaises(RobotError):
318 self._test_main(testcase.TestCase.EX_RUN_ERROR, *args)
320 @mock.patch('robot.run')
321 @mock.patch('os.makedirs')
322 def test_parse_results_ko(self, *args):
323 with mock.patch.object(self.test, 'set_robotframework_vars',
324 return_value=True), \
325 mock.patch.object(odl, 'open', mock.mock_open(),
327 mock.patch.object(self.test, 'parse_results',
328 side_effect=RobotError):
329 self._test_main(testcase.TestCase.EX_RUN_ERROR, *args)
331 @mock.patch('os.remove', side_effect=Exception)
332 @mock.patch('robot.run')
333 @mock.patch('os.makedirs')
334 def test_remove_exc(self, *args):
335 with mock.patch.object(self.test, 'set_robotframework_vars',
336 return_value=True), \
337 mock.patch.object(self.test, 'parse_results'), \
338 self.assertRaises(Exception):
339 self._test_main(testcase.TestCase.EX_OK, *args)
341 @mock.patch('os.remove')
342 @mock.patch('robot.run')
343 @mock.patch('os.makedirs')
344 def test_ok(self, *args):
345 with mock.patch.object(self.test, 'set_robotframework_vars',
346 return_value=True), \
347 mock.patch.object(odl, 'open', mock.mock_open(),
349 mock.patch.object(self.test, 'parse_results'):
350 self._test_main(testcase.TestCase.EX_OK, *args)
352 @mock.patch('os.remove')
353 @mock.patch('robot.run')
354 @mock.patch('os.makedirs', side_effect=OSError(errno.EEXIST, ''))
355 def test_makedirs_oserror17(self, *args):
356 with mock.patch.object(self.test, 'set_robotframework_vars',
357 return_value=True), \
358 mock.patch.object(odl, 'open', mock.mock_open(),
359 create=True) as mock_open, \
360 mock.patch.object(self.test, 'parse_results'):
361 self._test_main(testcase.TestCase.EX_OK, *args)
362 mock_open.assert_called_once_with(
363 os.path.join(odl.ODLTests.res_dir, 'stdout.txt'), 'w+')
365 @mock.patch('os.remove')
366 @mock.patch('robot.run', return_value=1)
367 @mock.patch('os.makedirs')
368 def test_testcases_in_failure(self, *args):
369 with mock.patch.object(self.test, 'set_robotframework_vars',
370 return_value=True), \
371 mock.patch.object(odl, 'open', mock.mock_open(),
372 create=True) as mock_open, \
373 mock.patch.object(self.test, 'parse_results'):
374 self._test_main(testcase.TestCase.EX_OK, *args)
375 mock_open.assert_called_once_with(
376 os.path.join(odl.ODLTests.res_dir, 'stdout.txt'), 'w+')
378 @mock.patch('os.remove', side_effect=OSError)
379 @mock.patch('robot.run')
380 @mock.patch('os.makedirs')
381 def test_remove_oserror(self, *args):
382 with mock.patch.object(self.test, 'set_robotframework_vars',
383 return_value=True), \
384 mock.patch.object(odl, 'open', mock.mock_open(),
385 create=True) as mock_open, \
386 mock.patch.object(self.test, 'parse_results'):
387 self._test_main(testcase.TestCase.EX_OK, *args)
388 mock_open.assert_called_once_with(
389 os.path.join(odl.ODLTests.res_dir, 'stdout.txt'), 'w+')
392 class ODLRunTesting(ODLTesting):
394 """The class testing ODLTests.run()."""
395 # pylint: disable=missing-docstring
397 def _test_no_env_var(self, var):
398 with mock.patch('functest.utils.openstack_utils.get_endpoint',
399 return_value="http://{}:9696".format(
400 ODLTesting._neutron_ip)):
402 self.assertEqual(self.test.run(),
403 testcase.TestCase.EX_RUN_ERROR)
405 def _test_run(self, status=testcase.TestCase.EX_OK,
406 exception=None, **kwargs):
407 odlip = kwargs['odlip'] if 'odlip' in kwargs else '127.0.0.3'
408 odlwebport = kwargs['odlwebport'] if 'odlwebport' in kwargs else '8080'
409 odlrestconfport = (kwargs['odlrestconfport']
410 if 'odlrestconfport' in kwargs else '8181')
412 with mock.patch('functest.utils.openstack_utils.get_endpoint',
413 return_value="http://{}:9696".format(
414 ODLTesting._neutron_ip)):
416 self.test.main = mock.Mock(side_effect=exception)
418 self.test.main = mock.Mock(return_value=status)
419 self.assertEqual(self.test.run(), status)
420 self.test.main.assert_called_once_with(
421 odl.ODLTests.default_suites,
422 neutronip=self._neutron_ip,
423 odlip=odlip, odlpassword=self._odl_password,
424 odlrestconfport=odlrestconfport,
425 odlusername=self._odl_username, odlwebport=odlwebport,
426 osauthurl=self._os_auth_url,
427 ospassword=self._os_password, ostenantname=self._os_tenantname,
428 osusername=self._os_username)
430 def _test_multiple_suites(self, suites,
431 status=testcase.TestCase.EX_OK, **kwargs):
432 odlip = kwargs['odlip'] if 'odlip' in kwargs else '127.0.0.3'
433 odlwebport = kwargs['odlwebport'] if 'odlwebport' in kwargs else '8080'
434 odlrestconfport = (kwargs['odlrestconfport']
435 if 'odlrestconfport' in kwargs else '8181')
436 with mock.patch('functest.utils.openstack_utils.get_endpoint',
437 return_value="http://{}:9696".format(
438 ODLTesting._neutron_ip)):
439 self.test.main = mock.Mock(return_value=status)
440 self.assertEqual(self.test.run(suites=suites), status)
441 self.test.main.assert_called_once_with(
443 neutronip=self._neutron_ip,
444 odlip=odlip, odlpassword=self._odl_password,
445 odlrestconfport=odlrestconfport,
446 odlusername=self._odl_username, odlwebport=odlwebport,
447 osauthurl=self._os_auth_url,
448 ospassword=self._os_password, ostenantname=self._os_tenantname,
449 osusername=self._os_username)
452 with mock.patch('functest.utils.openstack_utils.get_endpoint',
453 side_effect=auth_plugins.MissingAuthPlugin()):
454 self.assertEqual(self.test.run(),
455 testcase.TestCase.EX_RUN_ERROR)
457 def test_no_os_auth_url(self):
458 self._test_no_env_var("OS_AUTH_URL")
460 def test_no_os_username(self):
461 self._test_no_env_var("OS_USERNAME")
463 def test_no_os_password(self):
464 self._test_no_env_var("OS_PASSWORD")
466 def test_no_os_tenant_name(self):
467 self._test_no_env_var("OS_TENANT_NAME")
469 def test_main_false(self):
470 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
471 self._test_run(testcase.TestCase.EX_RUN_ERROR,
472 odlip=self._sdn_controller_ip,
473 odlwebport=self._odl_webport)
475 def test_main_exc(self):
476 with self.assertRaises(Exception):
477 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
478 self._test_run(status=testcase.TestCase.EX_RUN_ERROR,
479 exception=Exception(),
480 odlip=self._sdn_controller_ip,
481 odlwebport=self._odl_webport)
483 def test_no_sdn_controller_ip(self):
484 with mock.patch('functest.utils.openstack_utils.get_endpoint',
485 return_value="http://{}:9696".format(
486 ODLTesting._neutron_ip)):
487 self.assertEqual(self.test.run(),
488 testcase.TestCase.EX_RUN_ERROR)
490 def test_without_installer_type(self):
491 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
492 self._test_run(testcase.TestCase.EX_OK,
493 odlip=self._sdn_controller_ip,
494 odlwebport=self._odl_webport)
496 def test_suites(self):
497 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
498 self._test_multiple_suites(
499 [odl.ODLTests.basic_suite_dir],
500 testcase.TestCase.EX_OK,
501 odlip=self._sdn_controller_ip,
502 odlwebport=self._odl_webport)
505 os.environ["INSTALLER_TYPE"] = "fuel"
506 self._test_run(testcase.TestCase.EX_OK,
507 odlip=self._neutron_ip, odlwebport='8282')
509 def test_apex_no_controller_ip(self):
510 with mock.patch('functest.utils.openstack_utils.get_endpoint',
511 return_value="http://{}:9696".format(
512 ODLTesting._neutron_ip)):
513 os.environ["INSTALLER_TYPE"] = "apex"
514 self.assertEqual(self.test.run(),
515 testcase.TestCase.EX_RUN_ERROR)
518 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
519 os.environ["INSTALLER_TYPE"] = "apex"
520 self._test_run(testcase.TestCase.EX_OK,
521 odlip=self._sdn_controller_ip, odlwebport='8081',
522 odlrestconfport='8081')
524 def test_netvirt_no_controller_ip(self):
525 with mock.patch('functest.utils.openstack_utils.get_endpoint',
526 return_value="http://{}:9696".format(
527 ODLTesting._neutron_ip)):
528 os.environ["INSTALLER_TYPE"] = "netvirt"
529 self.assertEqual(self.test.run(),
530 testcase.TestCase.EX_RUN_ERROR)
532 def test_netvirt(self):
533 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
534 os.environ["INSTALLER_TYPE"] = "netvirt"
535 self._test_run(testcase.TestCase.EX_OK,
536 odlip=self._sdn_controller_ip, odlwebport='8081',
537 odlrestconfport='8081')
539 def test_joid_no_controller_ip(self):
540 with mock.patch('functest.utils.openstack_utils.get_endpoint',
541 return_value="http://{}:9696".format(
542 ODLTesting._neutron_ip)):
543 os.environ["INSTALLER_TYPE"] = "joid"
544 self.assertEqual(self.test.run(),
545 testcase.TestCase.EX_RUN_ERROR)
548 os.environ["SDN_CONTROLLER"] = self._sdn_controller_ip
549 os.environ["INSTALLER_TYPE"] = "joid"
550 self._test_run(testcase.TestCase.EX_OK,
551 odlip=self._sdn_controller_ip, odlwebport='8080')
553 def test_compass(self):
554 os.environ["INSTALLER_TYPE"] = "compass"
555 self._test_run(testcase.TestCase.EX_OK,
556 odlip=self._neutron_ip, odlwebport='8181')
559 class ODLArgParserTesting(ODLTesting):
561 """The class testing ODLParser."""
562 # pylint: disable=missing-docstring
565 self.parser = odl.ODLParser()
566 super(ODLArgParserTesting, self).setUp()
568 def test_default(self):
569 self.assertEqual(self.parser.parse_args(), self.defaultargs)
571 def test_basic(self):
572 self.defaultargs['neutronip'] = self._neutron_ip
573 self.defaultargs['odlip'] = self._sdn_controller_ip
575 self.parser.parse_args(
576 ["--neutronip={}".format(self._neutron_ip),
577 "--odlip={}".format(self._sdn_controller_ip)]),
580 @mock.patch('sys.stderr', new_callable=StringIO.StringIO)
581 def test_fail(self, mock_method):
582 self.defaultargs['foo'] = 'bar'
583 with self.assertRaises(SystemExit):
584 self.parser.parse_args(["--foo=bar"])
585 self.assertTrue(mock_method.getvalue().startswith("usage:"))
587 def _test_arg(self, arg, value):
588 self.defaultargs[arg] = value
590 self.parser.parse_args(["--{}={}".format(arg, value)]),
593 def test_odlusername(self):
594 self._test_arg('odlusername', 'foo')
596 def test_odlpassword(self):
597 self._test_arg('odlpassword', 'foo')
599 def test_osauthurl(self):
600 self._test_arg('osauthurl', 'http://127.0.0.4:5000/v2')
602 def test_neutronip(self):
603 self._test_arg('neutronip', '127.0.0.4')
605 def test_osusername(self):
606 self._test_arg('osusername', 'foo')
608 def test_ostenantname(self):
609 self._test_arg('ostenantname', 'foo')
611 def test_ospassword(self):
612 self._test_arg('ospassword', 'foo')
614 def test_odlip(self):
615 self._test_arg('odlip', '127.0.0.4')
617 def test_odlwebport(self):
618 self._test_arg('odlwebport', '80')
620 def test_odlrestconfport(self):
621 self._test_arg('odlrestconfport', '80')
623 def test_pushtodb(self):
624 self.defaultargs['pushtodb'] = True
625 self.assertEqual(self.parser.parse_args(["--{}".format('pushtodb')]),
628 def test_multiple_args(self):
629 self.defaultargs['neutronip'] = self._neutron_ip
630 self.defaultargs['odlip'] = self._sdn_controller_ip
632 self.parser.parse_args(
633 ["--neutronip={}".format(self._neutron_ip),
634 "--odlip={}".format(self._sdn_controller_ip)]),
638 if __name__ == "__main__":
639 unittest.main(verbosity=2)