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(),
341 mock.patch.object(self.test, 'parse_results'):
342 self._test_main(testcase.TestCase.EX_OK, *args)
344 @mock.patch('os.remove')
345 @mock.patch('robot.run', return_value=1)
346 @mock.patch('os.makedirs')
347 def test_testcases_in_failure(self, *args):
348 with mock.patch.object(self.test, 'set_robotframework_vars',
349 return_value=True), \
350 mock.patch.object(odl, 'open', mock.mock_open(),
352 mock.patch.object(self.test, 'parse_results'):
353 self._test_main(testcase.TestCase.EX_OK, *args)
355 @mock.patch('os.remove', side_effect=OSError)
356 @mock.patch('robot.run')
357 @mock.patch('os.makedirs')
358 def test_remove_oserror(self, *args):
359 with mock.patch.object(self.test, 'set_robotframework_vars',
360 return_value=True), \
361 mock.patch.object(odl, 'open', mock.mock_open(),
363 mock.patch.object(self.test, 'parse_results'):
364 self._test_main(testcase.TestCase.EX_OK, *args)
367 class ODLRunTesting(ODLTesting):
369 """The class testing ODLTests.run()."""
370 # pylint: disable=missing-docstring
372 def _test_no_env_var(self, var):
373 with mock.patch('functest.utils.openstack_utils.get_endpoint',
374 return_value="http://{}:9696".format(
375 ODLTesting._neutron_ip)):
377 self.assertEqual(self.test.run(),
378 testcase.TestCase.EX_RUN_ERROR)
380 def _test_run(self, status=testcase.TestCase.EX_OK,
381 exception=None, **kwargs):
382 odlip = kwargs['odlip'] if 'odlip' in kwargs else '127.0.0.3'
383 odlwebport = kwargs['odlwebport'] if 'odlwebport' in kwargs else '8080'
384 odlrestconfport = (kwargs['odlrestconfport']
385 if 'odlrestconfport' in kwargs else '8181')
387 with mock.patch('functest.utils.openstack_utils.get_endpoint',
388 return_value="http://{}:9696".format(
389 ODLTesting._neutron_ip)):
391 self.test.main = mock.Mock(side_effect=exception)
393 self.test.main = mock.Mock(return_value=status)
394 self.assertEqual(self.test.run(), status)
395 self.test.main.assert_called_once_with(
396 odl.ODLTests.default_suites,
397 neutronip=self._neutron_ip,
398 odlip=odlip, odlpassword=self._odl_password,
399 odlrestconfport=odlrestconfport,
400 odlusername=self._odl_username, odlwebport=odlwebport,
401 osauthurl=self._os_auth_url,
402 ospassword=self._os_password, ostenantname=self._os_tenantname,
403 osusername=self._os_username)
405 def _test_multiple_suites(self, suites,
406 status=testcase.TestCase.EX_OK, **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')
411 with mock.patch('functest.utils.openstack_utils.get_endpoint',
412 return_value="http://{}:9696".format(
413 ODLTesting._neutron_ip)):
414 self.test.main = mock.Mock(return_value=status)
415 self.assertEqual(self.test.run(suites=suites), status)
416 self.test.main.assert_called_once_with(
418 neutronip=self._neutron_ip,
419 odlip=odlip, odlpassword=self._odl_password,
420 odlrestconfport=odlrestconfport,
421 odlusername=self._odl_username, odlwebport=odlwebport,
422 osauthurl=self._os_auth_url,
423 ospassword=self._os_password, ostenantname=self._os_tenantname,
424 osusername=self._os_username)
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_main_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_main_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='8282')
484 def test_apex_no_controller_ip(self):
485 with mock.patch('functest.utils.openstack_utils.get_endpoint',
486 return_value="http://{}:9696".format(
487 ODLTesting._neutron_ip)):
488 os.environ["INSTALLER_TYPE"] = "apex"
489 self.assertEqual(self.test.run(),
490 testcase.TestCase.EX_RUN_ERROR)
493 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
494 os.environ["INSTALLER_TYPE"] = "apex"
495 self._test_run(testcase.TestCase.EX_OK,
496 odlip=self._sdn_controller_ip, odlwebport='8081',
497 odlrestconfport='8081')
499 def test_netvirt_no_controller_ip(self):
500 with mock.patch('functest.utils.openstack_utils.get_endpoint',
501 return_value="http://{}:9696".format(
502 ODLTesting._neutron_ip)):
503 os.environ["INSTALLER_TYPE"] = "netvirt"
504 self.assertEqual(self.test.run(),
505 testcase.TestCase.EX_RUN_ERROR)
507 def test_netvirt(self):
508 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
509 os.environ["INSTALLER_TYPE"] = "netvirt"
510 self._test_run(testcase.TestCase.EX_OK,
511 odlip=self._sdn_controller_ip, odlwebport='8081',
512 odlrestconfport='8081')
514 def test_joid_no_controller_ip(self):
515 with mock.patch('functest.utils.openstack_utils.get_endpoint',
516 return_value="http://{}:9696".format(
517 ODLTesting._neutron_ip)):
518 os.environ["INSTALLER_TYPE"] = "joid"
519 self.assertEqual(self.test.run(),
520 testcase.TestCase.EX_RUN_ERROR)
523 os.environ["SDN_CONTROLLER"] = self._sdn_controller_ip
524 os.environ["INSTALLER_TYPE"] = "joid"
525 self._test_run(testcase.TestCase.EX_OK,
526 odlip=self._sdn_controller_ip, odlwebport='8080')
528 def test_compass(self):
529 os.environ["INSTALLER_TYPE"] = "compass"
530 self._test_run(testcase.TestCase.EX_OK,
531 odlip=self._neutron_ip, odlwebport='8181')
534 class ODLArgParserTesting(ODLTesting):
536 """The class testing ODLParser."""
537 # pylint: disable=missing-docstring
540 self.parser = odl.ODLParser()
541 super(ODLArgParserTesting, self).setUp()
543 def test_default(self):
544 self.assertEqual(self.parser.parse_args(), self.defaultargs)
546 def test_basic(self):
547 self.defaultargs['neutronip'] = self._neutron_ip
548 self.defaultargs['odlip'] = self._sdn_controller_ip
550 self.parser.parse_args(
551 ["--neutronip={}".format(self._neutron_ip),
552 "--odlip={}".format(self._sdn_controller_ip)]),
555 @mock.patch('sys.stderr', new_callable=StringIO.StringIO)
556 def test_fail(self, mock_method):
557 self.defaultargs['foo'] = 'bar'
558 with self.assertRaises(SystemExit):
559 self.parser.parse_args(["--foo=bar"])
560 self.assertTrue(mock_method.getvalue().startswith("usage:"))
562 def _test_arg(self, arg, value):
563 self.defaultargs[arg] = value
565 self.parser.parse_args(["--{}={}".format(arg, value)]),
568 def test_odlusername(self):
569 self._test_arg('odlusername', 'foo')
571 def test_odlpassword(self):
572 self._test_arg('odlpassword', 'foo')
574 def test_osauthurl(self):
575 self._test_arg('osauthurl', 'http://127.0.0.4:5000/v2')
577 def test_neutronip(self):
578 self._test_arg('neutronip', '127.0.0.4')
580 def test_osusername(self):
581 self._test_arg('osusername', 'foo')
583 def test_ostenantname(self):
584 self._test_arg('ostenantname', 'foo')
586 def test_ospassword(self):
587 self._test_arg('ospassword', 'foo')
589 def test_odlip(self):
590 self._test_arg('odlip', '127.0.0.4')
592 def test_odlwebport(self):
593 self._test_arg('odlwebport', '80')
595 def test_odlrestconfport(self):
596 self._test_arg('odlrestconfport', '80')
598 def test_pushtodb(self):
599 self.defaultargs['pushtodb'] = True
600 self.assertEqual(self.parser.parse_args(["--{}".format('pushtodb')]),
603 def test_multiple_args(self):
604 self.defaultargs['neutronip'] = self._neutron_ip
605 self.defaultargs['odlip'] = self._sdn_controller_ip
607 self.parser.parse_args(
608 ["--neutronip={}".format(self._neutron_ip),
609 "--odlip={}".format(self._sdn_controller_ip)]),
613 if __name__ == "__main__":
614 unittest.main(verbosity=2)