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 @mock.patch('robot.api.ExecutionResult', side_effect=DataError)
113 def test_raises_exc(self, mock_method):
114 with self.assertRaises(DataError):
115 self.test.parse_results()
116 mock_method.assert_called_once_with(
117 os.path.join(odl.ODLTests.res_dir, 'output.xml'))
120 config = {'name': 'dummy', 'starttime': '20161216 16:00:00.000',
121 'endtime': '20161216 16:00:01.000', 'status': 'PASS'}
123 suite.configure_mock(**config)
124 with mock.patch('robot.api.ExecutionResult',
125 return_value=mock.Mock(suite=suite)):
126 self.test.parse_results()
127 self.assertEqual(self.test.result, config['status'])
128 self.assertEqual(self.test.start_time,
129 timestamp_to_secs(config['starttime']))
130 self.assertEqual(self.test.stop_time,
131 timestamp_to_secs(config['endtime']))
132 self.assertEqual(self.test.details,
133 {'description': config['name'], 'tests': []})
136 class ODLRobotTesting(ODLTesting):
138 """The class testing ODLTests.set_robotframework_vars()."""
139 # pylint: disable=missing-docstring
141 @mock.patch('fileinput.input', side_effect=Exception())
142 def test_set_vars_ko(self, mock_method):
143 self.assertFalse(self.test.set_robotframework_vars())
144 mock_method.assert_called_once_with(
145 os.path.join(odl.ODLTests.odl_test_repo,
146 'csit/variables/Variables.py'), inplace=True)
148 @mock.patch('fileinput.input', return_value=[])
149 def test_set_vars_empty(self, mock_method):
150 self.assertTrue(self.test.set_robotframework_vars())
151 mock_method.assert_called_once_with(
152 os.path.join(odl.ODLTests.odl_test_repo,
153 'csit/variables/Variables.py'), inplace=True)
155 @mock.patch('sys.stdout', new_callable=StringIO.StringIO)
156 def _test_set_vars(self, msg1, msg2, *args):
157 line = mock.MagicMock()
158 line.__iter__.return_value = [msg1]
159 with mock.patch('fileinput.input', return_value=line) as mock_method:
160 self.assertTrue(self.test.set_robotframework_vars())
161 mock_method.assert_called_once_with(
162 os.path.join(odl.ODLTests.odl_test_repo,
163 'csit/variables/Variables.py'), inplace=True)
164 self.assertEqual(args[0].getvalue(), "{}\n".format(msg2))
166 def test_set_vars_auth_default(self):
167 self._test_set_vars("AUTH = []",
168 "AUTH = [u'admin', u'admin']")
170 def test_set_vars_auth1(self):
171 self._test_set_vars("AUTH1 = []", "AUTH1 = []")
173 @mock.patch('sys.stdout', new_callable=StringIO.StringIO)
174 def test_set_vars_auth_foo(self, *args):
175 line = mock.MagicMock()
176 line.__iter__.return_value = ["AUTH = []"]
177 with mock.patch('fileinput.input', return_value=line) as mock_method:
178 self.assertTrue(self.test.set_robotframework_vars('foo', 'bar'))
179 mock_method.assert_called_once_with(
180 os.path.join(odl.ODLTests.odl_test_repo,
181 'csit/variables/Variables.py'), inplace=True)
182 self.assertEqual(args[0].getvalue(),
183 "AUTH = [u'{}', u'{}']\n".format('foo', 'bar'))
186 class ODLMainTesting(ODLTesting):
188 """The class testing ODLTests.main()."""
189 # pylint: disable=missing-docstring
191 def _get_main_kwargs(self, key=None):
192 kwargs = {'odlusername': self._odl_username,
193 'odlpassword': self._odl_password,
194 'neutronip': self._neutron_ip,
195 'osauthurl': self._os_auth_url,
196 'osusername': self._os_username,
197 'ostenantname': self._os_tenantname,
198 'ospassword': self._os_password,
199 'odlip': self._sdn_controller_ip,
200 'odlwebport': self._odl_webport,
201 'odlrestconfport': self._odl_restconfport}
206 def _test_main(self, status, *args):
207 kwargs = self._get_main_kwargs()
208 self.assertEqual(self.test.main(**kwargs), status)
210 args[0].assert_called_once_with(
211 odl.ODLTests.res_dir)
213 variable = ['KEYSTONE:{}'.format(self._keystone_ip),
214 'NEUTRON:{}'.format(self._neutron_ip),
215 'OS_AUTH_URL:"{}"'.format(self._os_auth_url),
216 'OSUSERNAME:"{}"'.format(self._os_username),
217 'OSTENANTNAME:"{}"'.format(self._os_tenantname),
218 'OSPASSWORD:"{}"'.format(self._os_password),
219 'ODL_SYSTEM_IP:{}'.format(self._sdn_controller_ip),
220 'PORT:{}'.format(self._odl_webport),
221 'RESTCONFPORT:{}'.format(self._odl_restconfport)]
222 args[1].assert_called_once_with(
223 odl.ODLTests.basic_suite_dir,
224 odl.ODLTests.neutron_suite_dir,
226 output=os.path.join(odl.ODLTests.res_dir, 'output.xml'),
231 args[2].assert_called_with(
232 os.path.join(odl.ODLTests.res_dir, 'stdout.txt'))
234 def _test_no_keyword(self, key):
235 kwargs = self._get_main_kwargs(key)
236 self.assertEqual(self.test.main(**kwargs),
237 testcase.TestCase.EX_RUN_ERROR)
239 def test_no_odlusername(self):
240 self._test_no_keyword('odlusername')
242 def test_no_odlpassword(self):
243 self._test_no_keyword('odlpassword')
245 def test_no_neutronip(self):
246 self._test_no_keyword('neutronip')
248 def test_no_osauthurl(self):
249 self._test_no_keyword('osauthurl')
251 def test_no_osusername(self):
252 self._test_no_keyword('osusername')
254 def test_no_ostenantname(self):
255 self._test_no_keyword('ostenantname')
257 def test_no_ospassword(self):
258 self._test_no_keyword('ospassword')
260 def test_no_odlip(self):
261 self._test_no_keyword('odlip')
263 def test_no_odlwebport(self):
264 self._test_no_keyword('odlwebport')
266 def test_no_odlrestconfport(self):
267 self._test_no_keyword('odlrestconfport')
269 def test_set_vars_ko(self):
270 with mock.patch.object(self.test, 'set_robotframework_vars',
271 return_value=False) as mock_object:
272 self._test_main(testcase.TestCase.EX_RUN_ERROR)
273 mock_object.assert_called_once_with(
274 self._odl_username, self._odl_password)
276 @mock.patch('os.makedirs', side_effect=Exception)
277 def test_makedirs_exc(self, mock_method):
278 with mock.patch.object(self.test, 'set_robotframework_vars',
279 return_value=True), \
280 self.assertRaises(Exception):
281 self._test_main(testcase.TestCase.EX_RUN_ERROR,
284 @mock.patch('os.makedirs', side_effect=OSError)
285 def test_makedirs_oserror(self, mock_method):
286 with mock.patch.object(self.test, 'set_robotframework_vars',
288 self._test_main(testcase.TestCase.EX_RUN_ERROR,
291 @mock.patch('robot.run', side_effect=RobotError)
292 @mock.patch('os.makedirs')
293 def test_run_ko(self, *args):
294 with mock.patch.object(self.test, 'set_robotframework_vars',
295 return_value=True), \
296 mock.patch.object(odl, 'open', mock.mock_open(),
298 self.assertRaises(RobotError):
299 self._test_main(testcase.TestCase.EX_RUN_ERROR, *args)
301 @mock.patch('robot.run')
302 @mock.patch('os.makedirs')
303 def test_parse_results_ko(self, *args):
304 with mock.patch.object(self.test, 'set_robotframework_vars',
305 return_value=True), \
306 mock.patch.object(odl, 'open', mock.mock_open(),
308 mock.patch.object(self.test, 'parse_results',
309 side_effect=RobotError):
310 self._test_main(testcase.TestCase.EX_RUN_ERROR, *args)
312 @mock.patch('os.remove', side_effect=Exception)
313 @mock.patch('robot.run')
314 @mock.patch('os.makedirs')
315 def test_remove_exc(self, *args):
316 with mock.patch.object(self.test, 'set_robotframework_vars',
317 return_value=True), \
318 mock.patch.object(self.test, 'parse_results'), \
319 self.assertRaises(Exception):
320 self._test_main(testcase.TestCase.EX_OK, *args)
322 @mock.patch('os.remove')
323 @mock.patch('robot.run')
324 @mock.patch('os.makedirs')
325 def test_ok(self, *args):
326 with mock.patch.object(self.test, 'set_robotframework_vars',
327 return_value=True), \
328 mock.patch.object(odl, 'open', mock.mock_open(),
330 mock.patch.object(self.test, 'parse_results'):
331 self._test_main(testcase.TestCase.EX_OK, *args)
333 @mock.patch('os.remove')
334 @mock.patch('robot.run')
335 @mock.patch('os.makedirs', side_effect=OSError(errno.EEXIST, ''))
336 def test_makedirs_oserror17(self, *args):
337 with mock.patch.object(self.test, 'set_robotframework_vars',
338 return_value=True), \
339 mock.patch.object(odl, 'open', mock.mock_open(),
340 create=True) as mock_open, \
341 mock.patch.object(self.test, 'parse_results'):
342 self._test_main(testcase.TestCase.EX_OK, *args)
343 mock_open.assert_called_once_with(
344 os.path.join(odl.ODLTests.res_dir, 'stdout.txt'), 'w+')
346 @mock.patch('os.remove')
347 @mock.patch('robot.run', return_value=1)
348 @mock.patch('os.makedirs')
349 def test_testcases_in_failure(self, *args):
350 with mock.patch.object(self.test, 'set_robotframework_vars',
351 return_value=True), \
352 mock.patch.object(odl, 'open', mock.mock_open(),
353 create=True) as mock_open, \
354 mock.patch.object(self.test, 'parse_results'):
355 self._test_main(testcase.TestCase.EX_OK, *args)
356 mock_open.assert_called_once_with(
357 os.path.join(odl.ODLTests.res_dir, 'stdout.txt'), 'w+')
359 @mock.patch('os.remove', side_effect=OSError)
360 @mock.patch('robot.run')
361 @mock.patch('os.makedirs')
362 def test_remove_oserror(self, *args):
363 with mock.patch.object(self.test, 'set_robotframework_vars',
364 return_value=True), \
365 mock.patch.object(odl, 'open', mock.mock_open(),
366 create=True) as mock_open, \
367 mock.patch.object(self.test, 'parse_results'):
368 self._test_main(testcase.TestCase.EX_OK, *args)
369 mock_open.assert_called_once_with(
370 os.path.join(odl.ODLTests.res_dir, 'stdout.txt'), 'w+')
373 class ODLRunTesting(ODLTesting):
375 """The class testing ODLTests.run()."""
376 # pylint: disable=missing-docstring
378 def _test_no_env_var(self, var):
379 with mock.patch('functest.utils.openstack_utils.get_endpoint',
380 return_value="http://{}:9696".format(
381 ODLTesting._neutron_ip)):
383 self.assertEqual(self.test.run(),
384 testcase.TestCase.EX_RUN_ERROR)
386 def _test_run(self, status=testcase.TestCase.EX_OK,
387 exception=None, **kwargs):
388 odlip = kwargs['odlip'] if 'odlip' in kwargs else '127.0.0.3'
389 odlwebport = kwargs['odlwebport'] if 'odlwebport' in kwargs else '8080'
390 odlrestconfport = (kwargs['odlrestconfport']
391 if 'odlrestconfport' in kwargs else '8181')
393 with mock.patch('functest.utils.openstack_utils.get_endpoint',
394 return_value="http://{}:9696".format(
395 ODLTesting._neutron_ip)):
397 self.test.main = mock.Mock(side_effect=exception)
399 self.test.main = mock.Mock(return_value=status)
400 self.assertEqual(self.test.run(), status)
401 self.test.main.assert_called_once_with(
402 odl.ODLTests.default_suites,
403 neutronip=self._neutron_ip,
404 odlip=odlip, odlpassword=self._odl_password,
405 odlrestconfport=odlrestconfport,
406 odlusername=self._odl_username, odlwebport=odlwebport,
407 osauthurl=self._os_auth_url,
408 ospassword=self._os_password, ostenantname=self._os_tenantname,
409 osusername=self._os_username)
411 def _test_multiple_suites(self, suites,
412 status=testcase.TestCase.EX_OK, **kwargs):
413 odlip = kwargs['odlip'] if 'odlip' in kwargs else '127.0.0.3'
414 odlwebport = kwargs['odlwebport'] if 'odlwebport' in kwargs else '8080'
415 odlrestconfport = (kwargs['odlrestconfport']
416 if 'odlrestconfport' in kwargs else '8181')
417 with mock.patch('functest.utils.openstack_utils.get_endpoint',
418 return_value="http://{}:9696".format(
419 ODLTesting._neutron_ip)):
420 self.test.main = mock.Mock(return_value=status)
421 self.assertEqual(self.test.run(suites=suites), status)
422 self.test.main.assert_called_once_with(
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)
433 with mock.patch('functest.utils.openstack_utils.get_endpoint',
434 side_effect=auth_plugins.MissingAuthPlugin()):
435 self.assertEqual(self.test.run(),
436 testcase.TestCase.EX_RUN_ERROR)
438 def test_no_os_auth_url(self):
439 self._test_no_env_var("OS_AUTH_URL")
441 def test_no_os_username(self):
442 self._test_no_env_var("OS_USERNAME")
444 def test_no_os_password(self):
445 self._test_no_env_var("OS_PASSWORD")
447 def test_no_os_tenant_name(self):
448 self._test_no_env_var("OS_TENANT_NAME")
450 def test_main_false(self):
451 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
452 self._test_run(testcase.TestCase.EX_RUN_ERROR,
453 odlip=self._sdn_controller_ip,
454 odlwebport=self._odl_webport)
456 def test_main_exc(self):
457 with self.assertRaises(Exception):
458 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
459 self._test_run(status=testcase.TestCase.EX_RUN_ERROR,
460 exception=Exception(),
461 odlip=self._sdn_controller_ip,
462 odlwebport=self._odl_webport)
464 def test_no_sdn_controller_ip(self):
465 with mock.patch('functest.utils.openstack_utils.get_endpoint',
466 return_value="http://{}:9696".format(
467 ODLTesting._neutron_ip)):
468 self.assertEqual(self.test.run(),
469 testcase.TestCase.EX_RUN_ERROR)
471 def test_without_installer_type(self):
472 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
473 self._test_run(testcase.TestCase.EX_OK,
474 odlip=self._sdn_controller_ip,
475 odlwebport=self._odl_webport)
477 def test_suites(self):
478 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
479 self._test_multiple_suites(
480 [odl.ODLTests.basic_suite_dir],
481 testcase.TestCase.EX_OK,
482 odlip=self._sdn_controller_ip,
483 odlwebport=self._odl_webport)
486 os.environ["INSTALLER_TYPE"] = "fuel"
487 self._test_run(testcase.TestCase.EX_OK,
488 odlip=self._neutron_ip, odlwebport='8282')
490 def test_apex_no_controller_ip(self):
491 with mock.patch('functest.utils.openstack_utils.get_endpoint',
492 return_value="http://{}:9696".format(
493 ODLTesting._neutron_ip)):
494 os.environ["INSTALLER_TYPE"] = "apex"
495 self.assertEqual(self.test.run(),
496 testcase.TestCase.EX_RUN_ERROR)
499 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
500 os.environ["INSTALLER_TYPE"] = "apex"
501 self._test_run(testcase.TestCase.EX_OK,
502 odlip=self._sdn_controller_ip, odlwebport='8081',
503 odlrestconfport='8081')
505 def test_netvirt_no_controller_ip(self):
506 with mock.patch('functest.utils.openstack_utils.get_endpoint',
507 return_value="http://{}:9696".format(
508 ODLTesting._neutron_ip)):
509 os.environ["INSTALLER_TYPE"] = "netvirt"
510 self.assertEqual(self.test.run(),
511 testcase.TestCase.EX_RUN_ERROR)
513 def test_netvirt(self):
514 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
515 os.environ["INSTALLER_TYPE"] = "netvirt"
516 self._test_run(testcase.TestCase.EX_OK,
517 odlip=self._sdn_controller_ip, odlwebport='8081',
518 odlrestconfport='8081')
520 def test_joid_no_controller_ip(self):
521 with mock.patch('functest.utils.openstack_utils.get_endpoint',
522 return_value="http://{}:9696".format(
523 ODLTesting._neutron_ip)):
524 os.environ["INSTALLER_TYPE"] = "joid"
525 self.assertEqual(self.test.run(),
526 testcase.TestCase.EX_RUN_ERROR)
529 os.environ["SDN_CONTROLLER"] = self._sdn_controller_ip
530 os.environ["INSTALLER_TYPE"] = "joid"
531 self._test_run(testcase.TestCase.EX_OK,
532 odlip=self._sdn_controller_ip, odlwebport='8080')
534 def test_compass(self):
535 os.environ["INSTALLER_TYPE"] = "compass"
536 self._test_run(testcase.TestCase.EX_OK,
537 odlip=self._neutron_ip, odlwebport='8181')
540 class ODLArgParserTesting(ODLTesting):
542 """The class testing ODLParser."""
543 # pylint: disable=missing-docstring
546 self.parser = odl.ODLParser()
547 super(ODLArgParserTesting, self).setUp()
549 def test_default(self):
550 self.assertEqual(self.parser.parse_args(), self.defaultargs)
552 def test_basic(self):
553 self.defaultargs['neutronip'] = self._neutron_ip
554 self.defaultargs['odlip'] = self._sdn_controller_ip
556 self.parser.parse_args(
557 ["--neutronip={}".format(self._neutron_ip),
558 "--odlip={}".format(self._sdn_controller_ip)]),
561 @mock.patch('sys.stderr', new_callable=StringIO.StringIO)
562 def test_fail(self, mock_method):
563 self.defaultargs['foo'] = 'bar'
564 with self.assertRaises(SystemExit):
565 self.parser.parse_args(["--foo=bar"])
566 self.assertTrue(mock_method.getvalue().startswith("usage:"))
568 def _test_arg(self, arg, value):
569 self.defaultargs[arg] = value
571 self.parser.parse_args(["--{}={}".format(arg, value)]),
574 def test_odlusername(self):
575 self._test_arg('odlusername', 'foo')
577 def test_odlpassword(self):
578 self._test_arg('odlpassword', 'foo')
580 def test_osauthurl(self):
581 self._test_arg('osauthurl', 'http://127.0.0.4:5000/v2')
583 def test_neutronip(self):
584 self._test_arg('neutronip', '127.0.0.4')
586 def test_osusername(self):
587 self._test_arg('osusername', 'foo')
589 def test_ostenantname(self):
590 self._test_arg('ostenantname', 'foo')
592 def test_ospassword(self):
593 self._test_arg('ospassword', 'foo')
595 def test_odlip(self):
596 self._test_arg('odlip', '127.0.0.4')
598 def test_odlwebport(self):
599 self._test_arg('odlwebport', '80')
601 def test_odlrestconfport(self):
602 self._test_arg('odlrestconfport', '80')
604 def test_pushtodb(self):
605 self.defaultargs['pushtodb'] = True
606 self.assertEqual(self.parser.parse_args(["--{}".format('pushtodb')]),
609 def test_multiple_args(self):
610 self.defaultargs['neutronip'] = self._neutron_ip
611 self.defaultargs['odlip'] = self._sdn_controller_ip
613 self.parser.parse_args(
614 ["--neutronip={}".format(self._neutron_ip),
615 "--odlip={}".format(self._sdn_controller_ip)]),
619 if __name__ == "__main__":
620 unittest.main(verbosity=2)