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
15 from functest.core import testcase
16 from functest.opnfv_tests.openstack.rally import rally
17 from functest.utils.constants import CONST
19 from snaps.openstack.os_credentials import OSCreds
22 class OSRallyTesting(unittest.TestCase):
25 username='user', password='pass',
26 auth_url='http://foo.com:5000/v3', project_name='bar')
27 with mock.patch('snaps.openstack.tests.openstack_tests.'
28 'get_credentials', return_value=os_creds) as m:
29 self.rally_base = rally.RallyBase()
31 self.assertTrue(m.called)
33 def test_build_task_args_missing_floating_network(self):
34 CONST.__setattr__('OS_AUTH_URL', None)
35 self.rally_base.ext_net_name = ''
36 task_args = self.rally_base._build_task_args('test_file_name')
37 self.assertEqual(task_args['floating_network'], '')
39 def test_build_task_args_missing_net_id(self):
40 CONST.__setattr__('OS_AUTH_URL', None)
41 self.rally_base.priv_net_id = ''
42 task_args = self.rally_base._build_task_args('test_file_name')
43 self.assertEqual(task_args['netid'], '')
46 def check_scenario_file(value):
47 yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
48 if yaml_file in value:
52 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists')
53 def test_prepare_test_list_missing_scenario_file(self, mock_func):
54 mock_func.side_effect = self.check_scenario_file
55 with self.assertRaises(Exception):
56 self.rally_base._prepare_test_list('test_file_name')
57 mock_func.assert_called()
60 def check_temp_dir(value):
61 yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
62 if yaml_file in value:
66 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists')
67 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.makedirs')
68 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
70 def test_prepare_test_list_missing_temp_dir(
71 self, mock_method, mock_os_makedirs, mock_path_exists):
72 mock_path_exists.side_effect = self.check_temp_dir
74 yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
75 ret_val = os.path.join(self.rally_base.TEMP_DIR, yaml_file)
76 self.assertEqual(self.rally_base._prepare_test_list('test_file_name'),
78 mock_path_exists.assert_called()
79 mock_method.assert_called()
80 mock_os_makedirs.assert_called()
82 def test_get_task_id_default(self):
83 cmd_raw = 'Task 1: started'
84 self.assertEqual(self.rally_base.get_task_id(cmd_raw),
87 def test_get_task_id_missing_id(self):
89 self.assertEqual(self.rally_base.get_task_id(cmd_raw),
92 def test_task_succeed_fail(self):
93 json_raw = json.dumps([None])
94 self.assertEqual(self.rally_base.task_succeed(json_raw),
96 json_raw = json.dumps([{'result': [{'error': ['test_error']}]}])
97 self.assertEqual(self.rally_base.task_succeed(json_raw),
100 def test_task_succeed_success(self):
101 json_raw = json.dumps('')
102 self.assertEqual(self.rally_base.task_succeed(json_raw),
106 if self.polling_iter == 0:
108 self.polling_iter -= 1
111 def test_get_cmd_output(self):
113 attrs = {'poll.side_effect': self.polling,
114 'stdout.readline.return_value': 'line'}
115 proc.configure_mock(**attrs)
116 self.assertEqual(self.rally_base.get_cmd_output(proc),
119 @mock.patch('__builtin__.open', mock.mock_open())
120 @mock.patch('functest.opnfv_tests.openstack.rally.rally.yaml.safe_load',
121 return_value={'scenario': [
122 {'scenarios': ['test_scenario'],
123 'installers': ['test_installer'],
125 {'scenarios': ['other_scenario'],
126 'installers': ['test_installer'],
127 'tests': ['other_test']}]})
128 def test_excl_scenario_default(self, mock_func):
129 CONST.__setattr__('INSTALLER_TYPE', 'test_installer')
130 CONST.__setattr__('DEPLOY_SCENARIO', 'test_scenario')
131 self.assertEqual(self.rally_base.excl_scenario(), ['test'])
132 mock_func.assert_called()
134 @mock.patch('__builtin__.open', mock.mock_open())
135 @mock.patch('functest.opnfv_tests.openstack.rally.rally.yaml.safe_load',
136 return_value={'scenario': [
137 {'scenarios': ['^os-[^-]+-featT-modeT$'],
138 'installers': ['test_installer'],
140 {'scenarios': ['^os-ctrlT-[^-]+-modeT$'],
141 'installers': ['test_installer'],
143 {'scenarios': ['^os-ctrlT-featT-[^-]+$'],
144 'installers': ['test_installer'],
146 {'scenarios': ['^os-'],
147 'installers': ['test_installer'],
149 {'scenarios': ['other_scenario'],
150 'installers': ['test_installer'],
151 'tests': ['test0a']},
152 {'scenarios': [''], # empty scenario
153 'installers': ['test_installer'],
154 'tests': ['test0b']}]})
155 def test_excl_scenario_regex(self, mock_func):
156 CONST.__setattr__('INSTALLER_TYPE', 'test_installer')
157 CONST.__setattr__('DEPLOY_SCENARIO', 'os-ctrlT-featT-modeT')
158 self.assertEqual(self.rally_base.excl_scenario(),
159 ['test1', 'test2', 'test3', 'test4'])
160 mock_func.assert_called()
162 @mock.patch('__builtin__.open', side_effect=Exception)
163 def test_excl_scenario_exception(self, mock_open):
164 self.assertEqual(self.rally_base.excl_scenario(), [])
165 mock_open.assert_called()
167 @mock.patch('__builtin__.open', mock.mock_open())
168 @mock.patch('functest.opnfv_tests.openstack.rally.rally.yaml.safe_load',
169 return_value={'functionality': [
170 {'functions': ['no_migration'], 'tests': ['test']}]})
171 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
172 '_migration_supported', return_value=False)
173 def test_excl_func_default(self, mock_func, mock_yaml_load):
174 CONST.__setattr__('INSTALLER_TYPE', 'test_installer')
175 CONST.__setattr__('DEPLOY_SCENARIO', 'test_scenario')
176 self.assertEqual(self.rally_base.excl_func(), ['test'])
177 mock_func.assert_called()
178 mock_yaml_load.assert_called()
180 @mock.patch('__builtin__.open', side_effect=Exception)
181 def test_excl_func_exception(self, mock_open):
182 self.assertEqual(self.rally_base.excl_func(), [])
183 mock_open.assert_called()
185 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.stat')
186 def test_file_is_empty_default(self, mock_os_stat):
187 attrs = {'st_size': 10}
188 mock_os_stat.return_value.configure_mock(**attrs)
189 self.assertEqual(self.rally_base.file_is_empty('test_file_name'),
191 mock_os_stat.assert_called()
193 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.stat',
194 side_effect=Exception)
195 def test_file_is_empty_exception(self, mock_os_stat):
196 self.assertEqual(self.rally_base.file_is_empty('test_file_name'), True)
197 mock_os_stat.assert_called()
199 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
201 def test_run_task_missing_task_file(self, mock_path_exists):
202 with self.assertRaises(Exception):
203 self.rally_base._run_task('test_name')
204 mock_path_exists.assert_called()
206 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
208 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
209 '_prepare_test_list', return_value='test_file_name')
210 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
211 'file_is_empty', return_value=True)
212 @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.info')
213 def test_run_task_no_tests_for_scenario(self, mock_logger_info,
214 mock_file_empty, mock_prep_list,
216 self.rally_base._run_task('test_name')
217 mock_logger_info.assert_any_call('No tests for scenario \"%s\"',
219 mock_file_empty.assert_called()
220 mock_prep_list.assert_called()
221 mock_path_exists.assert_called()
223 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
224 '_prepare_test_list', return_value='test_file_name')
225 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
226 'file_is_empty', return_value=False)
227 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
228 '_build_task_args', return_value={})
229 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
231 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
232 'get_task_id', return_value=None)
233 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
234 'get_cmd_output', return_value='')
235 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
237 @mock.patch('functest.opnfv_tests.openstack.rally.rally.subprocess.Popen')
238 @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.error')
239 def test_run_task_taskid_missing(self, mock_logger_error, *args):
240 self.rally_base._run_task('test_name')
241 text = 'Failed to retrieve task_id, validating task...'
242 mock_logger_error.assert_any_call(text)
244 @mock.patch('__builtin__.open', mock.mock_open())
245 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
246 '_prepare_test_list', return_value='test_file_name')
247 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
248 'file_is_empty', return_value=False)
249 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
250 '_build_task_args', return_value={})
251 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
253 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
254 'get_task_id', return_value='1')
255 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
256 'get_cmd_output', return_value='')
257 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
258 'task_succeed', return_value=True)
259 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
261 @mock.patch('functest.opnfv_tests.openstack.rally.rally.subprocess.Popen')
262 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.makedirs')
263 @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.popen')
264 @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.info')
265 @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.error')
266 def test_run_task_default(self, mock_logger_error, mock_logger_info,
268 attrs = {'read.return_value': 'json_result'}
269 mock_popen.return_value.configure_mock(**attrs)
270 self.rally_base._run_task('test_name')
271 text = 'Test scenario: "test_name" OK.\n'
272 mock_logger_info.assert_any_call(text)
273 mock_logger_error.assert_not_called()
275 def test_prepare_env_testname_invalid(self):
276 self.rally_base.TESTS = ['test1', 'test2']
277 self.rally_base.test_name = 'test'
278 with self.assertRaises(Exception):
279 self.rally_base._prepare_env()
281 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
282 'get_active_compute_cnt')
283 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
284 'get_ext_net_name', return_value='test_net_name')
285 @mock.patch('snaps.openstack.utils.deploy_utils.create_image',
287 def test_prepare_env_image_missing(
288 self, mock_get_img, mock_get_net, mock_get_comp_cnt):
289 self.rally_base.TESTS = ['test1', 'test2']
290 self.rally_base.test_name = 'test1'
291 with self.assertRaises(Exception):
292 self.rally_base._prepare_env()
293 mock_get_img.assert_called()
294 mock_get_net.assert_called()
295 mock_get_comp_cnt.assert_called()
297 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
298 'get_active_compute_cnt')
299 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
300 'get_ext_net_name', return_value='test_net_name')
301 @mock.patch('snaps.openstack.utils.deploy_utils.create_image')
302 @mock.patch('snaps.openstack.utils.deploy_utils.create_network',
304 def test_prepare_env_network_creation_failed(
305 self, mock_create_net, mock_get_img, mock_get_net,
307 self.rally_base.TESTS = ['test1', 'test2']
308 self.rally_base.test_name = 'test1'
309 with self.assertRaises(Exception):
310 self.rally_base._prepare_env()
311 mock_create_net.assert_called()
312 mock_get_img.assert_called()
313 mock_get_net.assert_called()
314 mock_get_comp_cnt.assert_called()
316 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
317 'get_active_compute_cnt')
318 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
319 'get_ext_net_name', return_value='test_net_name')
320 @mock.patch('snaps.openstack.utils.deploy_utils.create_image')
321 @mock.patch('snaps.openstack.utils.deploy_utils.create_network')
322 @mock.patch('snaps.openstack.utils.deploy_utils.create_router',
324 def test_prepare_env_router_creation_failed(
325 self, mock_create_router, mock_create_net, mock_get_img,
326 mock_get_net, mock_get_comp_cnt):
327 self.rally_base.TESTS = ['test1', 'test2']
328 self.rally_base.test_name = 'test1'
329 with self.assertRaises(Exception):
330 self.rally_base._prepare_env()
331 mock_create_net.assert_called()
332 mock_get_img.assert_called()
333 mock_get_net.assert_called()
334 mock_create_router.assert_called()
335 mock_get_comp_cnt.assert_called()
337 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
338 'get_active_compute_cnt')
339 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
340 'get_ext_net_name', return_value='test_net_name')
341 @mock.patch('snaps.openstack.utils.deploy_utils.create_image')
342 @mock.patch('snaps.openstack.utils.deploy_utils.create_network')
343 @mock.patch('snaps.openstack.utils.deploy_utils.create_router')
344 @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create',
346 def test_prepare_env_flavor_creation_failed(
347 self, mock_create_flavor, mock_create_router, mock_create_net,
348 mock_get_img, mock_get_net, mock_get_comp_cnt):
349 self.rally_base.TESTS = ['test1', 'test2']
350 self.rally_base.test_name = 'test1'
351 with self.assertRaises(Exception):
352 self.rally_base._prepare_env()
353 mock_create_net.assert_called()
354 mock_get_img.assert_called()
355 mock_get_net.assert_called()
356 mock_create_router.assert_called()
357 mock_get_comp_cnt.assert_called()
358 mock_create_flavor.assert_called_once()
360 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
361 'get_active_compute_cnt')
362 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
363 'get_ext_net_name', return_value='test_net_name')
364 @mock.patch('snaps.openstack.utils.deploy_utils.create_image')
365 @mock.patch('snaps.openstack.utils.deploy_utils.create_network')
366 @mock.patch('snaps.openstack.utils.deploy_utils.create_router')
367 @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create',
368 side_effect=[mock.Mock, None])
369 def test_prepare_env_flavor_alt_creation_failed(
370 self, mock_create_flavor, mock_create_router, mock_create_net,
371 mock_get_img, mock_get_net, mock_get_comp_cnt):
372 self.rally_base.TESTS = ['test1', 'test2']
373 self.rally_base.test_name = 'test1'
374 with self.assertRaises(Exception):
375 self.rally_base._prepare_env()
376 mock_create_net.assert_called()
377 mock_get_img.assert_called()
378 mock_get_net.assert_called()
379 mock_create_router.assert_called()
380 mock_get_comp_cnt.assert_called()
381 self.assertEqual(mock_create_flavor.call_count, 2)
383 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
385 def test_run_tests_all(self, mock_run_task):
386 self.rally_base.TESTS = ['test1', 'test2']
387 self.rally_base.test_name = 'all'
388 self.rally_base._run_tests()
389 mock_run_task.assert_any_call('test1')
390 mock_run_task.assert_any_call('test2')
392 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
394 def test_run_tests_default(self, mock_run_task):
395 self.rally_base.TESTS = ['test1', 'test2']
396 self.rally_base.test_name = 'test1'
397 self.rally_base._run_tests()
398 mock_run_task.assert_any_call('test1')
400 def test_clean_up_default(self):
401 creator1 = mock.Mock()
402 creator2 = mock.Mock()
403 self.rally_base.creators = [creator1, creator2]
404 self.rally_base._clean_up()
405 self.assertTrue(creator1.clean.called)
406 self.assertTrue(creator2.clean.called)
408 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
410 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
412 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
414 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
416 def test_run_default(self, *args):
417 self.assertEqual(self.rally_base.run(), testcase.TestCase.EX_OK)
418 map(lambda m: m.assert_called(), args)
420 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
421 '_prepare_env', side_effect=Exception)
422 def test_run_exception(self, mock_prep_env):
423 self.assertEqual(self.rally_base.run(), testcase.TestCase.EX_RUN_ERROR)
424 mock_prep_env.assert_called()
427 if __name__ == "__main__":
428 logging.disable(logging.CRITICAL)
429 unittest.main(verbosity=2)