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
20 class OSRallyTesting(unittest.TestCase):
23 self.nova_client = mock.Mock()
24 self.neutron_client = mock.Mock()
25 self.cinder_client = mock.Mock()
26 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
27 'os_utils.get_nova_client',
28 return_value=self.nova_client), \
29 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
30 'os_utils.get_neutron_client',
31 return_value=self.neutron_client), \
32 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
33 'os_utils.get_cinder_client',
34 return_value=self.cinder_client):
35 self.rally_base = rally.RallyBase()
36 self.rally_base.network_dict['net_id'] = 'test_net_id'
39 def test_build_task_args_missing_floating_network(self):
40 CONST.__setattr__('OS_AUTH_URL', None)
41 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
42 'os_utils.get_external_net',
44 task_args = self.rally_base._build_task_args('test_file_name')
45 self.assertEqual(task_args['floating_network'], '')
47 def test_build_task_args_missing_net_id(self):
48 CONST.__setattr__('OS_AUTH_URL', None)
49 self.rally_base.network_dict['net_id'] = ''
50 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
51 'os_utils.get_external_net',
52 return_value='test_floating_network'):
53 task_args = self.rally_base._build_task_args('test_file_name')
54 self.assertEqual(task_args['netid'], '')
56 def test_build_task_args_missing_auth_url(self):
57 CONST.__setattr__('OS_AUTH_URL', None)
58 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
59 'os_utils.get_external_net',
60 return_value='test_floating_network'):
61 task_args = self.rally_base._build_task_args('test_file_name')
62 self.assertEqual(task_args['request_url'], '')
64 def check_scenario_file(self, value):
65 yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
66 if yaml_file in value:
70 def test_prepare_test_list_missing_scenario_file(self):
71 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
73 side_effect=self.check_scenario_file), \
74 self.assertRaises(Exception):
75 self.rally_base._prepare_test_list('test_file_name')
77 def check_temp_dir(self, value):
78 yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
79 if yaml_file in value:
83 def test_prepare_test_list_missing_temp_dir(self):
84 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
86 side_effect=self.check_temp_dir), \
87 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
88 'os.makedirs') as mock_os_makedir, \
89 mock.patch.object(self.rally_base, 'apply_blacklist',
90 return_value=mock.Mock()) as mock_method:
91 yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
92 ret_val = os.path.join(self.rally_base.TEMP_DIR, yaml_file)
93 self.assertEqual(self.rally_base.
94 _prepare_test_list('test_file_name'),
96 self.assertTrue(mock_method.called)
97 self.assertTrue(mock_os_makedir.called)
99 def test_get_task_id_default(self):
100 cmd_raw = 'Task 1: started'
101 self.assertEqual(self.rally_base.get_task_id(cmd_raw),
104 def test_get_task_id_missing_id(self):
106 self.assertEqual(self.rally_base.get_task_id(cmd_raw),
109 def test_task_succeed_fail(self):
110 json_raw = json.dumps([None])
111 self.assertEqual(self.rally_base.task_succeed(json_raw),
113 json_raw = json.dumps([{'result': [{'error': ['test_error']}]}])
114 self.assertEqual(self.rally_base.task_succeed(json_raw),
117 def test_task_succeed_success(self):
118 json_raw = json.dumps('')
119 self.assertEqual(self.rally_base.task_succeed(json_raw),
123 if self.polling_iter == 0:
125 self.polling_iter -= 1
128 def test_get_cmd_output(self):
130 attrs = {'poll.side_effect': self.polling,
131 'stdout.readline.return_value': 'line'}
132 proc.configure_mock(**attrs)
133 self.assertEqual(self.rally_base.get_cmd_output(proc),
136 def test_excl_scenario_default(self):
137 CONST.__setattr__('INSTALLER_TYPE', 'test_installer')
138 CONST.__setattr__('DEPLOY_SCENARIO', 'test_scenario')
139 dic = {'scenario': [{'scenarios': ['test_scenario'],
140 'installers': ['test_installer'],
142 with mock.patch('__builtin__.open', mock.mock_open()), \
143 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
146 self.assertEqual(self.rally_base.excl_scenario(),
149 def test_excl_scenario_exception(self):
150 with mock.patch('__builtin__.open', side_effect=Exception):
151 self.assertEqual(self.rally_base.excl_scenario(),
154 def test_excl_func_default(self):
155 CONST.__setattr__('INSTALLER_TYPE', 'test_installer')
156 CONST.__setattr__('DEPLOY_SCENARIO', 'test_scenario')
157 dic = {'functionality': [{'functions': ['no_live_migration'],
159 with mock.patch('__builtin__.open', mock.mock_open()), \
160 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
163 mock.patch.object(self.rally_base, 'live_migration_supported',
165 self.assertEqual(self.rally_base.excl_func(),
168 def test_excl_func_exception(self):
169 with mock.patch('__builtin__.open', side_effect=Exception):
170 self.assertEqual(self.rally_base.excl_func(),
173 def test_file_is_empty_default(self):
174 mock_obj = mock.Mock()
175 attrs = {'st_size': 10}
176 mock_obj.configure_mock(**attrs)
177 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
179 return_value=mock_obj):
180 self.assertEqual(self.rally_base.file_is_empty('test_file_name'),
183 def test_file_is_empty_exception(self):
184 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
186 side_effect=Exception):
187 self.assertEqual(self.rally_base.file_is_empty('test_file_name'),
190 def test_run_task_missing_task_file(self):
191 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
193 return_value=False), \
194 self.assertRaises(Exception):
195 self.rally_base._run_task('test_name')
197 @mock.patch('functest.opnfv_tests.openstack.rally.rally.logger.info')
198 def test_run_task_no_tests_for_scenario(self, mock_logger_info):
199 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
201 return_value=True), \
202 mock.patch.object(self.rally_base, '_prepare_test_list',
203 return_value='test_file_name'), \
204 mock.patch.object(self.rally_base, 'file_is_empty',
206 self.rally_base._run_task('test_name')
207 str = 'No tests for scenario "test_name"'
208 mock_logger_info.assert_any_call(str)
210 @mock.patch('functest.opnfv_tests.openstack.rally.rally.logger.error')
211 def test_run_task_taskid_missing(self, mock_logger_error):
212 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
214 return_value=True), \
215 mock.patch.object(self.rally_base, '_prepare_test_list',
216 return_value='test_file_name'), \
217 mock.patch.object(self.rally_base, 'file_is_empty',
218 return_value=False), \
219 mock.patch.object(self.rally_base, '_build_task_args',
221 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
222 'subprocess.Popen'), \
223 mock.patch.object(self.rally_base, '_get_output',
224 return_value=mock.Mock()), \
225 mock.patch.object(self.rally_base, 'get_task_id',
226 return_value=None), \
227 mock.patch.object(self.rally_base, 'get_cmd_output',
229 self.rally_base._run_task('test_name')
230 str = 'Failed to retrieve task_id, validating task...'
231 mock_logger_error.assert_any_call(str)
233 @mock.patch('functest.opnfv_tests.openstack.rally.rally.logger.info')
234 @mock.patch('functest.opnfv_tests.openstack.rally.rally.logger.error')
235 def test_run_task_default(self, mock_logger_error,
238 attrs = {'read.return_value': 'json_result'}
239 popen.configure_mock(**attrs)
241 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
243 return_value=True), \
244 mock.patch.object(self.rally_base, '_prepare_test_list',
245 return_value='test_file_name'), \
246 mock.patch.object(self.rally_base, 'file_is_empty',
247 return_value=False), \
248 mock.patch.object(self.rally_base, '_build_task_args',
250 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
251 'subprocess.Popen'), \
252 mock.patch.object(self.rally_base, '_get_output',
253 return_value=mock.Mock()), \
254 mock.patch.object(self.rally_base, 'get_task_id',
256 mock.patch.object(self.rally_base, 'get_cmd_output',
258 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
260 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
262 return_value=popen), \
263 mock.patch('__builtin__.open', mock.mock_open()), \
264 mock.patch.object(self.rally_base, 'task_succeed',
266 self.rally_base._run_task('test_name')
267 str = 'Test scenario: "test_name" OK.\n'
268 mock_logger_info.assert_any_call(str)
270 def test_prepare_env_testname_invalid(self):
271 self.rally_base.TESTS = ['test1', 'test2']
272 self.rally_base.test_name = 'test'
273 with self.assertRaises(Exception):
274 self.rally_base._prepare_env()
276 def test_prepare_env_volume_creation_failed(self):
277 self.rally_base.TESTS = ['test1', 'test2']
278 self.rally_base.test_name = 'test1'
280 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
281 'os_utils.list_volume_types',
282 return_value=None), \
283 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
284 'os_utils.create_volume_type',
285 return_value=volume_type), \
286 self.assertRaises(Exception):
287 self.rally_base._prepare_env()
289 def test_prepare_env_image_missing(self):
290 self.rally_base.TESTS = ['test1', 'test2']
291 self.rally_base.test_name = 'test1'
292 volume_type = mock.Mock()
294 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
295 'os_utils.list_volume_types',
296 return_value=None), \
297 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
298 'os_utils.create_volume_type',
299 return_value=volume_type), \
300 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
301 'os_utils.get_or_create_image',
302 return_value=(True, image_id)), \
303 self.assertRaises(Exception):
304 self.rally_base._prepare_env()
306 def test_prepare_env_image_shared_network_creation_failed(self):
307 self.rally_base.TESTS = ['test1', 'test2']
308 self.rally_base.test_name = 'test1'
309 volume_type = mock.Mock()
310 image_id = 'image_id'
312 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
313 'os_utils.list_volume_types',
314 return_value=None), \
315 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
316 'os_utils.create_volume_type',
317 return_value=volume_type), \
318 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
319 'os_utils.get_or_create_image',
320 return_value=(True, image_id)), \
321 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
322 'os_utils.create_shared_network_full',
323 return_value=network_dict), \
324 self.assertRaises(Exception):
325 self.rally_base._prepare_env()
327 def test_run_tests_all(self):
328 self.rally_base.TESTS = ['test1', 'test2']
329 self.rally_base.test_name = 'all'
330 with mock.patch.object(self.rally_base, '_run_task',
331 return_value=mock.Mock()):
332 self.rally_base._run_tests()
333 self.rally_base._run_task.assert_any_call('test1')
334 self.rally_base._run_task.assert_any_call('test2')
336 def test_run_tests_default(self):
337 self.rally_base.TESTS = ['test1', 'test2']
338 self.rally_base.test_name = 'test1'
339 with mock.patch.object(self.rally_base, '_run_task',
340 return_value=mock.Mock()):
341 self.rally_base._run_tests()
342 self.rally_base._run_task.assert_any_call('test1')
344 def test_clean_up_default(self):
345 self.rally_base.volume_type = mock.Mock()
346 self.rally_base.cinder_client = mock.Mock()
347 self.rally_base.image_exists = False
348 self.rally_base.image_id = 1
349 self.rally_base.nova_client = mock.Mock()
350 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
351 'os_utils.delete_volume_type') as mock_vol_method, \
352 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
353 'os_utils.delete_glance_image') as mock_glance_method:
354 self.rally_base._clean_up()
355 mock_vol_method.assert_any_call(self.rally_base.cinder_client,
356 self.rally_base.volume_type)
357 mock_glance_method.assert_any_call(self.rally_base.nova_client,
360 def test_run_default(self):
361 with mock.patch.object(self.rally_base, '_prepare_env'), \
362 mock.patch.object(self.rally_base, '_run_tests'), \
363 mock.patch.object(self.rally_base, '_generate_report'), \
364 mock.patch.object(self.rally_base, '_clean_up'):
365 self.assertEqual(self.rally_base.run(),
366 testcase.TestCase.EX_OK)
368 def test_run_exception(self):
369 with mock.patch.object(self.rally_base, '_prepare_env',
370 side_effect=Exception):
371 self.assertEqual(self.rally_base.run(),
372 testcase.TestCase.EX_RUN_ERROR)
375 if __name__ == "__main__":
376 logging.disable(logging.CRITICAL)
377 unittest.main(verbosity=2)