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
17 from xtesting.core import testcase
19 from functest.opnfv_tests.openstack.rally import rally
22 class OSRallyTesting(unittest.TestCase):
23 # pylint: disable=too-many-public-methods
25 with mock.patch('os_client_config.get_config') as mock_get_config, \
26 mock.patch('shade.OpenStackCloud') as mock_shade, \
27 mock.patch('functest.core.tenantnetwork.NewProject') \
29 self.rally_base = rally.RallyBase()
30 self.rally_base.image = munch.Munch(name='foo')
31 self.rally_base.flavor = munch.Munch(name='foo')
32 self.rally_base.flavor_alt = munch.Munch(name='bar')
33 self.assertTrue(mock_get_config.called)
34 self.assertTrue(mock_shade.called)
35 self.assertTrue(mock_new_project.called)
37 def test_build_task_args_missing_floating_network(self):
38 os.environ['OS_AUTH_URL'] = ''
39 self.rally_base.ext_net = None
40 task_args = self.rally_base._build_task_args('test_file_name')
41 self.assertEqual(task_args['floating_network'], '')
43 def test_build_task_args_missing_net_id(self):
44 os.environ['OS_AUTH_URL'] = ''
45 self.rally_base.network = None
46 task_args = self.rally_base._build_task_args('test_file_name')
47 self.assertEqual(task_args['netid'], '')
50 def check_scenario_file(value):
51 yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
52 if yaml_file in value:
56 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists')
57 def test_prepare_test_list_missing_scenario_file(self, mock_func):
58 mock_func.side_effect = self.check_scenario_file
59 with self.assertRaises(Exception):
60 self.rally_base._prepare_test_list('test_file_name')
61 mock_func.assert_called()
64 def check_temp_dir(value):
65 yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
66 if yaml_file in value:
70 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists')
71 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.makedirs')
72 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
74 def test_prepare_test_list_missing_temp_dir(
75 self, mock_method, mock_os_makedirs, mock_path_exists):
76 mock_path_exists.side_effect = self.check_temp_dir
78 yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
79 ret_val = os.path.join(self.rally_base.TEMP_DIR, yaml_file)
80 self.assertEqual(self.rally_base._prepare_test_list('test_file_name'),
82 mock_path_exists.assert_called()
83 mock_method.assert_called()
84 mock_os_makedirs.assert_called()
86 def test_get_task_id_default(self):
87 cmd_raw = 'Task 1: started'
88 self.assertEqual(self.rally_base.get_task_id(cmd_raw),
91 def test_get_task_id_missing_id(self):
93 self.assertEqual(self.rally_base.get_task_id(cmd_raw),
96 def test_task_succeed_fail(self):
97 json_raw = json.dumps({})
98 self.assertEqual(self.rally_base.task_succeed(json_raw),
100 json_raw = json.dumps({'tasks': [{'status': 'crashed'}]})
101 self.assertEqual(self.rally_base.task_succeed(json_raw),
104 def test_task_succeed_success(self):
105 json_raw = json.dumps({'tasks': [{'status': 'finished',
107 self.assertEqual(self.rally_base.task_succeed(json_raw),
110 @mock.patch('six.moves.builtins.open', mock.mock_open())
111 @mock.patch('functest.opnfv_tests.openstack.rally.rally.yaml.safe_load',
112 return_value={'scenario': [
113 {'scenarios': ['test_scenario'],
114 'installers': ['test_installer'],
116 {'scenarios': ['other_scenario'],
117 'installers': ['test_installer'],
118 'tests': ['other_test']}]})
119 def test_excl_scenario_default(self, mock_func):
120 os.environ['INSTALLER_TYPE'] = 'test_installer'
121 os.environ['DEPLOY_SCENARIO'] = 'test_scenario'
122 self.assertEqual(self.rally_base.excl_scenario(), ['test'])
123 mock_func.assert_called()
125 @mock.patch('six.moves.builtins.open', mock.mock_open())
126 @mock.patch('functest.opnfv_tests.openstack.rally.rally.yaml.safe_load',
127 return_value={'scenario': [
128 {'scenarios': ['^os-[^-]+-featT-modeT$'],
129 'installers': ['test_installer'],
131 {'scenarios': ['^os-ctrlT-[^-]+-modeT$'],
132 'installers': ['test_installer'],
134 {'scenarios': ['^os-ctrlT-featT-[^-]+$'],
135 'installers': ['test_installer'],
137 {'scenarios': ['^os-'],
138 'installers': ['test_installer'],
140 {'scenarios': ['other_scenario'],
141 'installers': ['test_installer'],
142 'tests': ['test0a']},
143 {'scenarios': [''], # empty scenario
144 'installers': ['test_installer'],
145 'tests': ['test0b']}]})
146 def test_excl_scenario_regex(self, mock_func):
147 os.environ['INSTALLER_TYPE'] = 'test_installer'
148 os.environ['DEPLOY_SCENARIO'] = 'os-ctrlT-featT-modeT'
149 self.assertEqual(self.rally_base.excl_scenario(),
150 ['test1', 'test2', 'test3', 'test4'])
151 mock_func.assert_called()
153 @mock.patch('six.moves.builtins.open', side_effect=Exception)
154 def test_excl_scenario_exception(self, mock_open):
155 self.assertEqual(self.rally_base.excl_scenario(), [])
156 mock_open.assert_called()
158 @mock.patch('six.moves.builtins.open', mock.mock_open())
159 @mock.patch('functest.opnfv_tests.openstack.rally.rally.yaml.safe_load',
160 return_value={'functionality': [
161 {'functions': ['no_migration'], 'tests': ['test']}]})
162 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
163 '_migration_supported', return_value=False)
164 def test_excl_func_default(self, mock_func, mock_yaml_load):
165 os.environ['INSTALLER_TYPE'] = 'test_installer'
166 os.environ['DEPLOY_SCENARIO'] = 'test_scenario'
167 self.assertEqual(self.rally_base.excl_func(), ['test'])
168 mock_func.assert_called()
169 mock_yaml_load.assert_called()
171 @mock.patch('six.moves.builtins.open', side_effect=Exception)
172 def test_excl_func_exception(self, mock_open):
173 self.assertEqual(self.rally_base.excl_func(), [])
174 mock_open.assert_called()
176 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.stat')
177 def test_file_is_empty_default(self, mock_os_stat):
178 attrs = {'st_size': 10}
179 mock_os_stat.return_value.configure_mock(**attrs)
180 self.assertEqual(self.rally_base.file_is_empty('test_file_name'),
182 mock_os_stat.assert_called()
184 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.stat',
185 side_effect=Exception)
186 def test_file_is_empty_exception(self, mock_os_stat):
187 self.assertEqual(self.rally_base.file_is_empty('test_file_name'), True)
188 mock_os_stat.assert_called()
190 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
192 def test_run_task_missing_task_file(self, mock_path_exists):
193 with self.assertRaises(Exception):
194 self.rally_base._run_task('test_name')
195 mock_path_exists.assert_called()
197 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
199 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
200 '_prepare_test_list', return_value='test_file_name')
201 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
202 'file_is_empty', return_value=True)
203 @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.info')
204 def test_run_task_no_tests_for_scenario(self, mock_logger_info,
205 mock_file_empty, mock_prep_list,
207 self.rally_base._run_task('test_name')
208 mock_logger_info.assert_any_call('No tests for scenario \"%s\"',
210 mock_file_empty.assert_called()
211 mock_prep_list.assert_called()
212 mock_path_exists.assert_called()
214 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
215 '_prepare_test_list', return_value='test_file_name')
216 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
217 'file_is_empty', return_value=False)
218 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
219 '_build_task_args', return_value={})
220 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
221 'get_task_id', return_value=None)
222 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
224 @mock.patch('functest.opnfv_tests.openstack.rally.rally.subprocess.Popen')
225 @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.error')
226 def test_run_task_taskid_missing(self, mock_logger_error, *args):
227 # pylint: disable=unused-argument
228 with self.assertRaises(Exception):
229 self.rally_base._run_task('test_name')
230 text = 'Failed to retrieve task_id'
231 mock_logger_error.assert_any_call(text)
233 @mock.patch('six.moves.builtins.open', mock.mock_open())
234 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
235 '_prepare_test_list', return_value='test_file_name')
236 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
237 'file_is_empty', return_value=False)
238 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
239 '_build_task_args', return_value={})
240 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
241 'get_task_id', return_value='1')
242 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
243 'task_succeed', return_value=True)
244 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
246 @mock.patch('functest.opnfv_tests.openstack.rally.rally.subprocess.Popen')
247 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.makedirs')
248 @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.info')
249 @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.error')
250 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
252 def test_run_task_default(self, mock_save_res, *args):
253 # pylint: disable=unused-argument
254 self.rally_base._run_task('test_name')
255 mock_save_res.assert_called()
257 @mock.patch('six.moves.builtins.open', mock.mock_open())
258 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
259 'task_succeed', return_value=True)
260 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
262 @mock.patch('subprocess.check_output')
263 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.makedirs')
264 @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.info')
265 @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.debug')
266 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
268 def test_save_results(self, mock_summary, *args):
269 # pylint: disable=unused-argument
270 self.rally_base._save_results('test_name', '1234')
271 mock_summary.assert_called()
273 def test_prepare_env_testname_invalid(self):
274 self.rally_base.TESTS = ['test1', 'test2']
275 self.rally_base.test_name = 'test'
276 with self.assertRaises(Exception):
277 self.rally_base._prepare_env()
279 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
280 'get_external_network')
281 def test_prepare_env_flavor_alt_creation_failed(self, *args):
282 # pylint: disable=unused-argument
283 self.rally_base.TESTS = ['test1', 'test2']
284 self.rally_base.test_name = 'test1'
285 with mock.patch.object(self.rally_base.cloud,
286 'list_hypervisors') as mock_list_hyperv, \
287 mock.patch.object(self.rally_base, 'create_flavor_alt',
288 side_effect=Exception) \
289 as mock_create_flavor:
290 with self.assertRaises(Exception):
291 self.rally_base._prepare_env()
292 mock_list_hyperv.assert_called_once()
293 mock_create_flavor.assert_called_once()
295 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
297 def test_run_tests_all(self, mock_run_task):
298 self.rally_base.TESTS = ['test1', 'test2']
299 self.rally_base.test_name = 'all'
300 self.rally_base._run_tests()
301 mock_run_task.assert_any_call('test1')
302 mock_run_task.assert_any_call('test2')
304 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
306 def test_run_tests_default(self, mock_run_task):
307 self.rally_base.TESTS = ['test1', 'test2']
308 self.rally_base.test_name = 'test1'
309 self.rally_base._run_tests()
310 mock_run_task.assert_any_call('test1')
312 def test_clean_up_default(self):
313 with mock.patch.object(self.rally_base.orig_cloud,
314 'delete_flavor') as mock_delete_flavor:
315 self.rally_base.flavor_alt = mock.Mock()
316 self.rally_base.clean()
317 self.assertEqual(mock_delete_flavor.call_count, 1)
319 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
320 'create_rally_deployment')
321 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
323 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
325 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
327 def test_run_default(self, *args):
328 self.assertEqual(self.rally_base.run(), testcase.TestCase.EX_OK)
332 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
333 'create_rally_deployment', side_effect=Exception)
334 def test_run_exception_create_rally_dep(self, mock_create_rally_dep):
335 self.assertEqual(self.rally_base.run(), testcase.TestCase.EX_RUN_ERROR)
336 mock_create_rally_dep.assert_called()
338 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
339 'create_rally_deployment', return_value=mock.Mock())
340 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
341 '_prepare_env', side_effect=Exception)
342 def test_run_exception_prepare_env(self, mock_prep_env, *args):
343 # pylint: disable=unused-argument
344 self.assertEqual(self.rally_base.run(), testcase.TestCase.EX_RUN_ERROR)
345 mock_prep_env.assert_called()
347 def test_append_summary(self):
348 text = '{"tasks": [{"subtasks": [{"workloads": [{"full_duration": ' \
349 '1.23,"data": [{"error": []}]}]},{"workloads": ' \
350 '[{"full_duration": 2.78, "data": [{"error": ["err"]}]}]}]}]}'
351 self.rally_base._append_summary(text, "foo_test")
352 self.assertEqual(self.rally_base.summary[0]['test_name'], "foo_test")
353 self.assertEqual(self.rally_base.summary[0]['overall_duration'], 4.01)
354 self.assertEqual(self.rally_base.summary[0]['nb_tests'], 2)
355 self.assertEqual(self.rally_base.summary[0]['nb_success'], 1)
357 def test_is_successful_false(self):
358 with mock.patch('six.moves.builtins.super') as mock_super:
359 self.rally_base.summary = [{"task_status": True},
360 {"task_status": False}]
361 self.assertEqual(self.rally_base.is_successful(),
362 testcase.TestCase.EX_TESTCASE_FAILED)
363 mock_super(rally.RallyBase, self).is_successful.assert_not_called()
365 def test_is_successful_true(self):
366 with mock.patch('six.moves.builtins.super') as mock_super:
367 mock_super(rally.RallyBase, self).is_successful.return_value = 424
368 self.rally_base.summary = [{"task_status": True},
369 {"task_status": True}]
370 self.assertEqual(self.rally_base.is_successful(), 424)
371 mock_super(rally.RallyBase, self).is_successful.assert_called()
374 if __name__ == "__main__":
375 logging.disable(logging.CRITICAL)
376 unittest.main(verbosity=2)