3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Apache License, Version 2.0
5 # which accompanies this distribution, and is available at
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # pylint: disable=missing-docstring,protected-access,invalid-name
16 from xtesting.core import testcase
18 from functest.opnfv_tests.openstack.rally import rally
21 class OSRallyTesting(unittest.TestCase):
22 # pylint: disable=too-many-public-methods
24 with mock.patch('os_client_config.make_shade') as mock_make_shade:
25 self.rally_base = rally.RallyBase()
26 self.assertTrue(mock_make_shade.called)
28 def test_build_task_args_missing_floating_network(self):
29 os.environ['OS_AUTH_URL'] = ''
30 self.rally_base.ext_net_name = ''
31 task_args = self.rally_base._build_task_args('test_file_name')
32 self.assertEqual(task_args['floating_network'], '')
34 def test_build_task_args_missing_net_id(self):
35 os.environ['OS_AUTH_URL'] = ''
36 self.rally_base.priv_net_id = ''
37 task_args = self.rally_base._build_task_args('test_file_name')
38 self.assertEqual(task_args['netid'], '')
41 def check_scenario_file(value):
42 yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
43 if yaml_file in value:
47 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists')
48 def test_prepare_test_list_missing_scenario_file(self, mock_func):
49 mock_func.side_effect = self.check_scenario_file
50 with self.assertRaises(Exception):
51 self.rally_base._prepare_test_list('test_file_name')
52 mock_func.assert_called()
55 def check_temp_dir(value):
56 yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
57 if yaml_file in value:
61 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists')
62 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.makedirs')
63 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
65 def test_prepare_test_list_missing_temp_dir(
66 self, mock_method, mock_os_makedirs, mock_path_exists):
67 mock_path_exists.side_effect = self.check_temp_dir
69 yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
70 ret_val = os.path.join(self.rally_base.TEMP_DIR, yaml_file)
71 self.assertEqual(self.rally_base._prepare_test_list('test_file_name'),
73 mock_path_exists.assert_called()
74 mock_method.assert_called()
75 mock_os_makedirs.assert_called()
77 def test_get_task_id_default(self):
78 cmd_raw = 'Task 1: started'
79 self.assertEqual(self.rally_base.get_task_id(cmd_raw),
82 def test_get_task_id_missing_id(self):
84 self.assertEqual(self.rally_base.get_task_id(cmd_raw),
87 def test_task_succeed_fail(self):
88 json_raw = json.dumps({})
89 self.assertEqual(self.rally_base.task_succeed(json_raw),
91 json_raw = json.dumps({'tasks': [{'status': 'crashed'}]})
92 self.assertEqual(self.rally_base.task_succeed(json_raw),
95 def test_task_succeed_success(self):
96 json_raw = json.dumps({'tasks': [{'status': 'finished',
98 self.assertEqual(self.rally_base.task_succeed(json_raw),
101 def test_get_cmd_output(self):
103 proc.stdout.__iter__ = mock.Mock(return_value=iter(['line1', 'line2']))
104 self.assertEqual(self.rally_base.get_cmd_output(proc),
107 @mock.patch('__builtin__.open', mock.mock_open())
108 @mock.patch('functest.opnfv_tests.openstack.rally.rally.yaml.safe_load',
109 return_value={'scenario': [
110 {'scenarios': ['test_scenario'],
111 'installers': ['test_installer'],
113 {'scenarios': ['other_scenario'],
114 'installers': ['test_installer'],
115 'tests': ['other_test']}]})
116 def test_excl_scenario_default(self, mock_func):
117 os.environ['INSTALLER_TYPE'] = 'test_installer'
118 os.environ['DEPLOY_SCENARIO'] = 'test_scenario'
119 self.assertEqual(self.rally_base.excl_scenario(), ['test'])
120 mock_func.assert_called()
122 @mock.patch('__builtin__.open', mock.mock_open())
123 @mock.patch('functest.opnfv_tests.openstack.rally.rally.yaml.safe_load',
124 return_value={'scenario': [
125 {'scenarios': ['^os-[^-]+-featT-modeT$'],
126 'installers': ['test_installer'],
128 {'scenarios': ['^os-ctrlT-[^-]+-modeT$'],
129 'installers': ['test_installer'],
131 {'scenarios': ['^os-ctrlT-featT-[^-]+$'],
132 'installers': ['test_installer'],
134 {'scenarios': ['^os-'],
135 'installers': ['test_installer'],
137 {'scenarios': ['other_scenario'],
138 'installers': ['test_installer'],
139 'tests': ['test0a']},
140 {'scenarios': [''], # empty scenario
141 'installers': ['test_installer'],
142 'tests': ['test0b']}]})
143 def test_excl_scenario_regex(self, mock_func):
144 os.environ['INSTALLER_TYPE'] = 'test_installer'
145 os.environ['DEPLOY_SCENARIO'] = 'os-ctrlT-featT-modeT'
146 self.assertEqual(self.rally_base.excl_scenario(),
147 ['test1', 'test2', 'test3', 'test4'])
148 mock_func.assert_called()
150 @mock.patch('__builtin__.open', side_effect=Exception)
151 def test_excl_scenario_exception(self, mock_open):
152 self.assertEqual(self.rally_base.excl_scenario(), [])
153 mock_open.assert_called()
155 @mock.patch('__builtin__.open', mock.mock_open())
156 @mock.patch('functest.opnfv_tests.openstack.rally.rally.yaml.safe_load',
157 return_value={'functionality': [
158 {'functions': ['no_migration'], 'tests': ['test']}]})
159 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
160 '_migration_supported', return_value=False)
161 def test_excl_func_default(self, mock_func, mock_yaml_load):
162 os.environ['INSTALLER_TYPE'] = 'test_installer'
163 os.environ['DEPLOY_SCENARIO'] = 'test_scenario'
164 self.assertEqual(self.rally_base.excl_func(), ['test'])
165 mock_func.assert_called()
166 mock_yaml_load.assert_called()
168 @mock.patch('__builtin__.open', side_effect=Exception)
169 def test_excl_func_exception(self, mock_open):
170 self.assertEqual(self.rally_base.excl_func(), [])
171 mock_open.assert_called()
173 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.stat')
174 def test_file_is_empty_default(self, mock_os_stat):
175 attrs = {'st_size': 10}
176 mock_os_stat.return_value.configure_mock(**attrs)
177 self.assertEqual(self.rally_base.file_is_empty('test_file_name'),
179 mock_os_stat.assert_called()
181 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.stat',
182 side_effect=Exception)
183 def test_file_is_empty_exception(self, mock_os_stat):
184 self.assertEqual(self.rally_base.file_is_empty('test_file_name'), True)
185 mock_os_stat.assert_called()
187 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
189 def test_run_task_missing_task_file(self, mock_path_exists):
190 with self.assertRaises(Exception):
191 self.rally_base._run_task('test_name')
192 mock_path_exists.assert_called()
194 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
196 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
197 '_prepare_test_list', return_value='test_file_name')
198 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
199 'file_is_empty', return_value=True)
200 @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.info')
201 def test_run_task_no_tests_for_scenario(self, mock_logger_info,
202 mock_file_empty, mock_prep_list,
204 self.rally_base._run_task('test_name')
205 mock_logger_info.assert_any_call('No tests for scenario \"%s\"',
207 mock_file_empty.assert_called()
208 mock_prep_list.assert_called()
209 mock_path_exists.assert_called()
211 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
212 '_prepare_test_list', return_value='test_file_name')
213 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
214 'file_is_empty', return_value=False)
215 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
216 '_build_task_args', return_value={})
217 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
218 'get_task_id', return_value=None)
219 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
220 'get_cmd_output', return_value='')
221 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
223 @mock.patch('functest.opnfv_tests.openstack.rally.rally.subprocess.Popen')
224 @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.error')
225 def test_run_task_taskid_missing(self, mock_logger_error, *args):
226 # pylint: disable=unused-argument
227 with self.assertRaises(Exception):
228 self.rally_base._run_task('test_name')
229 text = 'Failed to retrieve task_id, validating task...'
230 mock_logger_error.assert_any_call(text)
232 @mock.patch('__builtin__.open', mock.mock_open())
233 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
234 '_prepare_test_list', return_value='test_file_name')
235 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
236 'file_is_empty', return_value=False)
237 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
238 '_build_task_args', return_value={})
239 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
240 'get_task_id', return_value='1')
241 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
242 'get_cmd_output', return_value='')
243 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
244 'task_succeed', return_value=True)
245 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
247 @mock.patch('functest.opnfv_tests.openstack.rally.rally.subprocess.Popen')
248 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.makedirs')
249 @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.info')
250 @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.error')
251 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
253 def test_run_task_default(self, mock_save_res, *args):
254 # pylint: disable=unused-argument
255 self.rally_base._run_task('test_name')
256 mock_save_res.assert_called()
258 @mock.patch('__builtin__.open', mock.mock_open())
259 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
260 'task_succeed', return_value=True)
261 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
262 'get_cmd_output', return_value='')
263 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
265 @mock.patch('functest.opnfv_tests.openstack.rally.rally.subprocess.Popen')
266 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.makedirs')
267 @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.info')
268 @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.debug')
269 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
271 def test_save_results(self, mock_summary, *args):
272 # pylint: disable=unused-argument
273 self.rally_base._save_results('test_name', '1234')
274 mock_summary.assert_called()
276 def test_prepare_env_testname_invalid(self):
277 self.rally_base.TESTS = ['test1', 'test2']
278 self.rally_base.test_name = 'test'
279 with self.assertRaises(Exception):
280 self.rally_base._prepare_env()
282 @mock.patch('functest.utils.functest_utils.get_external_network')
283 def test_prepare_env_image_missing(self, *args):
284 # pylint: disable=unused-argument
285 self.rally_base.TESTS = ['test1', 'test2']
286 self.rally_base.test_name = 'test1'
287 with mock.patch.object(self.rally_base.cloud, 'list_hypervisors'), \
288 mock.patch.object(self.rally_base.cloud, 'create_image',
290 with self.assertRaises(Exception):
291 self.rally_base._prepare_env()
293 @mock.patch('functest.utils.functest_utils.get_external_network')
294 def test_prepare_env_network_creation_failed(self, *args):
295 # pylint: disable=unused-argument
296 self.rally_base.TESTS = ['test1', 'test2']
297 self.rally_base.test_name = 'test1'
298 with mock.patch.object(self.rally_base.cloud,
299 'list_hypervisors') as mock_list_hyperv, \
300 mock.patch.object(self.rally_base.cloud,
301 'create_image') as mock_create_img, \
302 mock.patch.object(self.rally_base.cloud, 'create_network',
303 return_value=None) as mock_create_net:
304 with self.assertRaises(Exception):
305 self.rally_base._prepare_env()
306 mock_create_net.assert_called()
307 mock_create_img.assert_called()
308 mock_list_hyperv.assert_called()
310 @mock.patch('functest.utils.functest_utils.get_external_network')
311 def test_prepare_env_subnet_creation_failed(self, *args):
312 # pylint: disable=unused-argument
313 self.rally_base.TESTS = ['test1', 'test2']
314 self.rally_base.test_name = 'test1'
315 with mock.patch.object(self.rally_base.cloud,
316 'list_hypervisors') as mock_list_hyperv, \
317 mock.patch.object(self.rally_base.cloud,
318 'create_image') as mock_create_img, \
319 mock.patch.object(self.rally_base.cloud,
320 'create_network') as mock_create_net, \
321 mock.patch.object(self.rally_base.cloud, 'create_subnet',
322 return_value=None) as mock_create_subnet:
323 with self.assertRaises(Exception):
324 self.rally_base._prepare_env()
325 mock_create_subnet.assert_called()
326 mock_create_net.assert_called()
327 mock_create_img.assert_called()
328 mock_list_hyperv.assert_called()
330 @mock.patch('functest.utils.functest_utils.get_external_network')
331 def test_prepare_env_router_creation_failed(self, *args):
332 # pylint: disable=unused-argument
333 self.rally_base.TESTS = ['test1', 'test2']
334 self.rally_base.test_name = 'test1'
335 with mock.patch.object(self.rally_base.cloud,
336 'list_hypervisors') as mock_list_hyperv, \
337 mock.patch.object(self.rally_base.cloud,
338 'create_image') as mock_create_img, \
339 mock.patch.object(self.rally_base.cloud,
340 'create_network') as mock_create_net, \
341 mock.patch.object(self.rally_base.cloud,
342 'create_subnet') as mock_create_subnet, \
343 mock.patch.object(self.rally_base.cloud, 'create_router',
344 return_value=None) as mock_create_router:
345 with self.assertRaises(Exception):
346 self.rally_base._prepare_env()
347 mock_create_router.assert_called()
348 mock_create_subnet.assert_called()
349 mock_create_net.assert_called()
350 mock_create_img.assert_called()
351 mock_list_hyperv.assert_called()
353 @mock.patch('functest.utils.functest_utils.get_external_network')
354 def test_prepare_env_flavor_creation_failed(self, *args):
355 # pylint: disable=unused-argument
356 self.rally_base.TESTS = ['test1', 'test2']
357 self.rally_base.test_name = 'test1'
358 with mock.patch.object(self.rally_base.cloud,
359 'list_hypervisors') as mock_list_hyperv, \
360 mock.patch.object(self.rally_base.cloud,
361 'create_image') as mock_create_img, \
362 mock.patch.object(self.rally_base.cloud,
363 'create_network') as mock_create_net, \
364 mock.patch.object(self.rally_base.cloud,
365 'create_subnet') as mock_create_subnet, \
366 mock.patch.object(self.rally_base.cloud,
367 'add_router_interface') as mock_add_router_if, \
368 mock.patch.object(self.rally_base.cloud,
369 'create_router') as mock_create_router, \
370 mock.patch.object(self.rally_base.cloud, 'create_flavor',
371 return_value=None) as mock_create_flavor:
372 with self.assertRaises(Exception):
373 self.rally_base._prepare_env()
374 mock_create_flavor.assert_called_once()
375 mock_add_router_if.assert_called()
376 mock_create_router.assert_called()
377 mock_create_subnet.assert_called()
378 mock_create_net.assert_called()
379 mock_create_img.assert_called()
380 mock_list_hyperv.assert_called()
382 @mock.patch('functest.utils.functest_utils.get_external_network')
383 def test_prepare_env_flavor_alt_creation_failed(self, *args):
384 # pylint: disable=unused-argument
385 self.rally_base.TESTS = ['test1', 'test2']
386 self.rally_base.test_name = 'test1'
387 with mock.patch.object(self.rally_base.cloud,
388 'list_hypervisors') as mock_list_hyperv, \
389 mock.patch.object(self.rally_base.cloud,
390 'create_image') as mock_create_img, \
391 mock.patch.object(self.rally_base.cloud,
392 'create_network') as mock_create_net, \
393 mock.patch.object(self.rally_base.cloud,
394 'create_subnet') as mock_create_subnet, \
395 mock.patch.object(self.rally_base.cloud,
396 'add_router_interface') as mock_add_router_if, \
397 mock.patch.object(self.rally_base.cloud,
398 'create_router') as mock_create_router, \
399 mock.patch.object(self.rally_base.cloud,
400 'set_flavor_specs') as mock_set_flavor_specs, \
401 mock.patch.object(self.rally_base.cloud, 'create_flavor',
402 side_effect=[mock.Mock(), None]) \
403 as mock_create_flavor:
404 with self.assertRaises(Exception):
405 self.rally_base._prepare_env()
406 self.assertEqual(mock_create_flavor.call_count, 2)
407 mock_set_flavor_specs.assert_called_once()
408 mock_add_router_if.assert_called()
409 mock_create_router.assert_called()
410 mock_create_subnet.assert_called()
411 mock_create_net.assert_called()
412 mock_create_img.assert_called()
413 mock_list_hyperv.assert_called()
415 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
417 def test_run_tests_all(self, mock_run_task):
418 self.rally_base.TESTS = ['test1', 'test2']
419 self.rally_base.test_name = 'all'
420 self.rally_base._run_tests()
421 mock_run_task.assert_any_call('test1')
422 mock_run_task.assert_any_call('test2')
424 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
426 def test_run_tests_default(self, mock_run_task):
427 self.rally_base.TESTS = ['test1', 'test2']
428 self.rally_base.test_name = 'test1'
429 self.rally_base._run_tests()
430 mock_run_task.assert_any_call('test1')
432 def test_clean_up_default(self):
433 with mock.patch.object(self.rally_base.cloud,
434 'delete_flavor') as mock_delete_flavor, \
435 mock.patch.object(self.rally_base.cloud,
436 'remove_router_interface') \
437 as mock_remove_router_if, \
438 mock.patch.object(self.rally_base.cloud,
439 'delete_router') as mock_delete_router, \
440 mock.patch.object(self.rally_base.cloud,
441 'delete_subnet') as mock_delete_subnet, \
442 mock.patch.object(self.rally_base.cloud,
443 'delete_network') as mock_delete_net, \
444 mock.patch.object(self.rally_base.cloud,
445 'delete_image') as mock_delete_img:
446 self.rally_base.flavor_alt = mock.Mock()
447 self.rally_base.flavor = mock.Mock()
448 self.rally_base.router = mock.Mock()
449 self.rally_base.subnet = mock.Mock()
450 self.rally_base.network = mock.Mock()
451 self.rally_base.image = mock.Mock()
452 self.rally_base._clean_up()
453 self.assertEqual(mock_delete_flavor.call_count, 2)
454 mock_remove_router_if.assert_called()
455 mock_delete_router.assert_called()
456 mock_delete_subnet.assert_called()
457 mock_delete_net.assert_called()
458 mock_delete_img.assert_called()
460 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
461 'create_rally_deployment')
462 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
464 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
466 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
468 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
470 def test_run_default(self, *args):
471 self.assertEqual(self.rally_base.run(), testcase.TestCase.EX_OK)
475 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
476 'create_rally_deployment', side_effect=Exception)
477 def test_run_exception_create_rally_dep(self, mock_create_rally_dep):
478 self.assertEqual(self.rally_base.run(), testcase.TestCase.EX_RUN_ERROR)
479 mock_create_rally_dep.assert_called()
481 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
482 'create_rally_deployment', return_value=mock.Mock())
483 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
484 '_prepare_env', side_effect=Exception)
485 def test_run_exception_prepare_env(self, mock_prep_env, *args):
486 # pylint: disable=unused-argument
487 self.assertEqual(self.rally_base.run(), testcase.TestCase.EX_RUN_ERROR)
488 mock_prep_env.assert_called()
490 def test_append_summary(self):
491 text = '{"tasks": [{"subtasks": [{"workloads": [{"full_duration": ' \
492 '1.23,"data": [{"error": []}]}]},{"workloads": ' \
493 '[{"full_duration": 2.78, "data": [{"error": ["err"]}]}]}]}]}'
494 self.rally_base._append_summary(text, "foo_test")
495 self.assertEqual(self.rally_base.summary[0]['test_name'], "foo_test")
496 self.assertEqual(self.rally_base.summary[0]['overall_duration'], 4.01)
497 self.assertEqual(self.rally_base.summary[0]['nb_tests'], 2)
498 self.assertEqual(self.rally_base.summary[0]['nb_success'], 1)
500 def test_is_successful_false(self):
501 with mock.patch('__builtin__.super') as mock_super:
502 self.rally_base.summary = [{"task_status": True},
503 {"task_status": False}]
504 self.assertEqual(self.rally_base.is_successful(),
505 testcase.TestCase.EX_TESTCASE_FAILED)
506 mock_super(rally.RallyBase, self).is_successful.assert_not_called()
508 def test_is_successful_true(self):
509 with mock.patch('__builtin__.super') as mock_super:
510 mock_super(rally.RallyBase, self).is_successful.return_value = 424
511 self.rally_base.summary = [{"task_status": True},
512 {"task_status": True}]
513 self.assertEqual(self.rally_base.is_successful(), 424)
514 mock_super(rally.RallyBase, self).is_successful.assert_called()
517 if __name__ == "__main__":
518 logging.disable(logging.CRITICAL)
519 unittest.main(verbosity=2)