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 functest.core import testcase
18 from functest.opnfv_tests.openstack.rally import rally
19 from functest.utils.constants import CONST
21 from snaps.openstack.os_credentials import OSCreds
24 class OSRallyTesting(unittest.TestCase):
25 # pylint: disable=too-many-public-methods
28 username='user', password='pass',
29 auth_url='http://foo.com:5000/v3', project_name='bar')
30 with mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
32 return_value=os_creds) as mock_get_creds:
33 self.rally_base = rally.RallyBase()
34 self.assertTrue(mock_get_creds.called)
36 def test_build_task_args_missing_floating_network(self):
37 CONST.__setattr__('OS_AUTH_URL', None)
38 self.rally_base.ext_net_name = ''
39 task_args = self.rally_base._build_task_args('test_file_name')
40 self.assertEqual(task_args['floating_network'], '')
42 def test_build_task_args_missing_net_id(self):
43 CONST.__setattr__('OS_AUTH_URL', None)
44 self.rally_base.priv_net_id = ''
45 task_args = self.rally_base._build_task_args('test_file_name')
46 self.assertEqual(task_args['netid'], '')
49 def check_scenario_file(value):
50 yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
51 if yaml_file in value:
55 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists')
56 def test_prepare_test_list_missing_scenario_file(self, mock_func):
57 mock_func.side_effect = self.check_scenario_file
58 with self.assertRaises(Exception):
59 self.rally_base._prepare_test_list('test_file_name')
60 mock_func.assert_called()
63 def check_temp_dir(value):
64 yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
65 if yaml_file in value:
69 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists')
70 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.makedirs')
71 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
73 def test_prepare_test_list_missing_temp_dir(
74 self, mock_method, mock_os_makedirs, mock_path_exists):
75 mock_path_exists.side_effect = self.check_temp_dir
77 yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
78 ret_val = os.path.join(self.rally_base.TEMP_DIR, yaml_file)
79 self.assertEqual(self.rally_base._prepare_test_list('test_file_name'),
81 mock_path_exists.assert_called()
82 mock_method.assert_called()
83 mock_os_makedirs.assert_called()
85 def test_get_task_id_default(self):
86 cmd_raw = 'Task 1: started'
87 self.assertEqual(self.rally_base.get_task_id(cmd_raw),
90 def test_get_task_id_missing_id(self):
92 self.assertEqual(self.rally_base.get_task_id(cmd_raw),
95 def test_task_succeed_fail(self):
96 json_raw = json.dumps([None])
97 self.assertEqual(self.rally_base.task_succeed(json_raw),
99 json_raw = json.dumps([{'result': [{'error': ['test_error']}]}])
100 self.assertEqual(self.rally_base.task_succeed(json_raw),
103 def test_task_succeed_success(self):
104 json_raw = json.dumps('')
105 self.assertEqual(self.rally_base.task_succeed(json_raw),
108 def test_get_cmd_output(self):
110 proc.stdout.__iter__ = mock.Mock(return_value=iter(['line1', 'line2']))
111 self.assertEqual(self.rally_base.get_cmd_output(proc),
114 @mock.patch('__builtin__.open', mock.mock_open())
115 @mock.patch('functest.opnfv_tests.openstack.rally.rally.yaml.safe_load',
116 return_value={'scenario': [
117 {'scenarios': ['test_scenario'],
118 'installers': ['test_installer'],
120 {'scenarios': ['other_scenario'],
121 'installers': ['test_installer'],
122 'tests': ['other_test']}]})
123 def test_excl_scenario_default(self, mock_func):
124 CONST.__setattr__('INSTALLER_TYPE', 'test_installer')
125 CONST.__setattr__('DEPLOY_SCENARIO', 'test_scenario')
126 self.assertEqual(self.rally_base.excl_scenario(), ['test'])
127 mock_func.assert_called()
129 @mock.patch('__builtin__.open', mock.mock_open())
130 @mock.patch('functest.opnfv_tests.openstack.rally.rally.yaml.safe_load',
131 return_value={'scenario': [
132 {'scenarios': ['^os-[^-]+-featT-modeT$'],
133 'installers': ['test_installer'],
135 {'scenarios': ['^os-ctrlT-[^-]+-modeT$'],
136 'installers': ['test_installer'],
138 {'scenarios': ['^os-ctrlT-featT-[^-]+$'],
139 'installers': ['test_installer'],
141 {'scenarios': ['^os-'],
142 'installers': ['test_installer'],
144 {'scenarios': ['other_scenario'],
145 'installers': ['test_installer'],
146 'tests': ['test0a']},
147 {'scenarios': [''], # empty scenario
148 'installers': ['test_installer'],
149 'tests': ['test0b']}]})
150 def test_excl_scenario_regex(self, mock_func):
151 CONST.__setattr__('INSTALLER_TYPE', 'test_installer')
152 CONST.__setattr__('DEPLOY_SCENARIO', 'os-ctrlT-featT-modeT')
153 self.assertEqual(self.rally_base.excl_scenario(),
154 ['test1', 'test2', 'test3', 'test4'])
155 mock_func.assert_called()
157 @mock.patch('__builtin__.open', side_effect=Exception)
158 def test_excl_scenario_exception(self, mock_open):
159 self.assertEqual(self.rally_base.excl_scenario(), [])
160 mock_open.assert_called()
162 @mock.patch('__builtin__.open', mock.mock_open())
163 @mock.patch('functest.opnfv_tests.openstack.rally.rally.yaml.safe_load',
164 return_value={'functionality': [
165 {'functions': ['no_migration'], 'tests': ['test']}]})
166 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
167 '_migration_supported', return_value=False)
168 def test_excl_func_default(self, mock_func, mock_yaml_load):
169 CONST.__setattr__('INSTALLER_TYPE', 'test_installer')
170 CONST.__setattr__('DEPLOY_SCENARIO', 'test_scenario')
171 self.assertEqual(self.rally_base.excl_func(), ['test'])
172 mock_func.assert_called()
173 mock_yaml_load.assert_called()
175 @mock.patch('__builtin__.open', side_effect=Exception)
176 def test_excl_func_exception(self, mock_open):
177 self.assertEqual(self.rally_base.excl_func(), [])
178 mock_open.assert_called()
180 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.stat')
181 def test_file_is_empty_default(self, mock_os_stat):
182 attrs = {'st_size': 10}
183 mock_os_stat.return_value.configure_mock(**attrs)
184 self.assertEqual(self.rally_base.file_is_empty('test_file_name'),
186 mock_os_stat.assert_called()
188 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.stat',
189 side_effect=Exception)
190 def test_file_is_empty_exception(self, mock_os_stat):
191 self.assertEqual(self.rally_base.file_is_empty('test_file_name'), True)
192 mock_os_stat.assert_called()
194 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
196 def test_run_task_missing_task_file(self, mock_path_exists):
197 with self.assertRaises(Exception):
198 self.rally_base._run_task('test_name')
199 mock_path_exists.assert_called()
201 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
203 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
204 '_prepare_test_list', return_value='test_file_name')
205 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
206 'file_is_empty', return_value=True)
207 @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.info')
208 def test_run_task_no_tests_for_scenario(self, mock_logger_info,
209 mock_file_empty, mock_prep_list,
211 self.rally_base._run_task('test_name')
212 mock_logger_info.assert_any_call('No tests for scenario \"%s\"',
214 mock_file_empty.assert_called()
215 mock_prep_list.assert_called()
216 mock_path_exists.assert_called()
218 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
219 '_prepare_test_list', return_value='test_file_name')
220 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
221 'file_is_empty', return_value=False)
222 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
223 '_build_task_args', return_value={})
224 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
226 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
227 'get_task_id', return_value=None)
228 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
229 'get_cmd_output', return_value='')
230 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
232 @mock.patch('functest.opnfv_tests.openstack.rally.rally.subprocess.Popen')
233 @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.error')
234 def test_run_task_taskid_missing(self, mock_logger_error, *args):
235 # pylint: disable=unused-argument
236 self.rally_base._run_task('test_name')
237 text = 'Failed to retrieve task_id, validating task...'
238 mock_logger_error.assert_any_call(text)
240 @mock.patch('__builtin__.open', mock.mock_open())
241 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
242 '_prepare_test_list', return_value='test_file_name')
243 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
244 'file_is_empty', return_value=False)
245 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
246 '_build_task_args', return_value={})
247 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
249 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
250 'get_task_id', return_value='1')
251 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
252 'get_cmd_output', return_value='')
253 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
254 'task_succeed', return_value=True)
255 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
257 @mock.patch('functest.opnfv_tests.openstack.rally.rally.subprocess.Popen')
258 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.makedirs')
259 @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.info')
260 @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.error')
261 def test_run_task_default(self, mock_logger_error, mock_logger_info,
263 # pylint: disable=unused-argument
264 self.rally_base._run_task('test_name')
265 text = 'Test scenario: "test_name" OK.\n'
266 mock_logger_info.assert_any_call(text)
267 mock_logger_error.assert_not_called()
269 def test_prepare_env_testname_invalid(self):
270 self.rally_base.TESTS = ['test1', 'test2']
271 self.rally_base.test_name = 'test'
272 with self.assertRaises(Exception):
273 self.rally_base._prepare_env()
275 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
276 'get_active_compute_cnt')
277 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
278 'get_ext_net_name', return_value='test_net_name')
279 @mock.patch('snaps.openstack.utils.deploy_utils.create_image',
281 def test_prepare_env_image_missing(
282 self, mock_get_img, mock_get_net, mock_get_comp_cnt):
283 self.rally_base.TESTS = ['test1', 'test2']
284 self.rally_base.test_name = 'test1'
285 with self.assertRaises(Exception):
286 self.rally_base._prepare_env()
287 mock_get_img.assert_called()
288 mock_get_net.assert_called()
289 mock_get_comp_cnt.assert_called()
291 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
292 'get_active_compute_cnt')
293 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
294 'get_ext_net_name', return_value='test_net_name')
295 @mock.patch('snaps.openstack.utils.deploy_utils.create_image')
296 @mock.patch('snaps.openstack.utils.deploy_utils.create_network',
298 def test_prepare_env_network_creation_failed(
299 self, mock_create_net, mock_get_img, mock_get_net,
301 self.rally_base.TESTS = ['test1', 'test2']
302 self.rally_base.test_name = 'test1'
303 with self.assertRaises(Exception):
304 self.rally_base._prepare_env()
305 mock_create_net.assert_called()
306 mock_get_img.assert_called()
307 mock_get_net.assert_called()
308 mock_get_comp_cnt.assert_called()
310 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
311 'get_active_compute_cnt')
312 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
313 'get_ext_net_name', return_value='test_net_name')
314 @mock.patch('snaps.openstack.utils.deploy_utils.create_image')
315 @mock.patch('snaps.openstack.utils.deploy_utils.create_network')
316 @mock.patch('snaps.openstack.utils.deploy_utils.create_router',
318 def test_prepare_env_router_creation_failed(self, *args):
319 self.rally_base.TESTS = ['test1', 'test2']
320 self.rally_base.test_name = 'test1'
321 with self.assertRaises(Exception):
322 self.rally_base._prepare_env()
326 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
327 'get_active_compute_cnt')
328 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
329 'get_ext_net_name', return_value='test_net_name')
330 @mock.patch('snaps.openstack.utils.deploy_utils.create_image')
331 @mock.patch('snaps.openstack.utils.deploy_utils.create_network')
332 @mock.patch('snaps.openstack.utils.deploy_utils.create_router')
333 @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create',
335 def test_prepare_env_flavor_creation_failed(self, mock_create_flavor,
337 self.rally_base.TESTS = ['test1', 'test2']
338 self.rally_base.test_name = 'test1'
339 with self.assertRaises(Exception):
340 self.rally_base._prepare_env()
343 mock_create_flavor.assert_called_once()
345 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
346 'get_active_compute_cnt')
347 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
348 'get_ext_net_name', return_value='test_net_name')
349 @mock.patch('snaps.openstack.utils.deploy_utils.create_image')
350 @mock.patch('snaps.openstack.utils.deploy_utils.create_network')
351 @mock.patch('snaps.openstack.utils.deploy_utils.create_router')
352 @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create',
353 side_effect=[mock.Mock, None])
354 def test_prepare_env_flavor_alt_creation_failed(self, mock_create_flavor,
356 self.rally_base.TESTS = ['test1', 'test2']
357 self.rally_base.test_name = 'test1'
358 with self.assertRaises(Exception):
359 self.rally_base._prepare_env()
362 self.assertEqual(mock_create_flavor.call_count, 2)
364 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
366 def test_run_tests_all(self, mock_run_task):
367 self.rally_base.TESTS = ['test1', 'test2']
368 self.rally_base.test_name = 'all'
369 self.rally_base._run_tests()
370 mock_run_task.assert_any_call('test1')
371 mock_run_task.assert_any_call('test2')
373 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
375 def test_run_tests_default(self, mock_run_task):
376 self.rally_base.TESTS = ['test1', 'test2']
377 self.rally_base.test_name = 'test1'
378 self.rally_base._run_tests()
379 mock_run_task.assert_any_call('test1')
381 def test_clean_up_default(self):
382 creator1 = mock.Mock()
383 creator2 = mock.Mock()
384 self.rally_base.creators = [creator1, creator2]
385 self.rally_base._clean_up()
386 self.assertTrue(creator1.clean.called)
387 self.assertTrue(creator2.clean.called)
389 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
390 'create_rally_deployment')
391 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
393 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
395 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
397 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
399 def test_run_default(self, *args):
400 self.assertEqual(self.rally_base.run(), testcase.TestCase.EX_OK)
404 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
405 'create_rally_deployment', side_effect=Exception)
406 def test_run_exception_create_rally_dep(self, mock_create_rally_dep):
407 self.assertEqual(self.rally_base.run(), testcase.TestCase.EX_RUN_ERROR)
408 mock_create_rally_dep.assert_called()
410 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
411 'create_rally_deployment', return_value=mock.Mock())
412 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
413 '_prepare_env', side_effect=Exception)
414 def test_run_exception_prepare_env(self, mock_prep_env, *args):
415 # pylint: disable=unused-argument
416 self.assertEqual(self.rally_base.run(), testcase.TestCase.EX_RUN_ERROR)
417 mock_prep_env.assert_called()
419 def test_append_summary(self):
420 text = '[{"result":[{"error":[]},{"error":["err"]}],' \
421 '"full_duration": 17.312026}]'
422 self.rally_base._append_summary(text, "foo_test")
423 self.assertEqual(self.rally_base.summary[0]['test_name'], "foo_test")
424 self.assertEqual(self.rally_base.summary[0]['overall_duration'],
426 self.assertEqual(self.rally_base.summary[0]['nb_tests'], 2)
427 self.assertEqual(self.rally_base.summary[0]['nb_success'], 1)
430 if __name__ == "__main__":
431 logging.disable(logging.CRITICAL)
432 unittest.main(verbosity=2)