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 check_scenario_file(self, value):
57 yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
58 if yaml_file in value:
62 def test_prepare_test_list_missing_scenario_file(self):
63 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
65 side_effect=self.check_scenario_file), \
66 self.assertRaises(Exception):
67 self.rally_base._prepare_test_list('test_file_name')
69 def check_temp_dir(self, value):
70 yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
71 if yaml_file in value:
75 def test_prepare_test_list_missing_temp_dir(self):
76 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
78 side_effect=self.check_temp_dir), \
79 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
80 'os.makedirs') as mock_os_makedir, \
81 mock.patch.object(self.rally_base, 'apply_blacklist',
82 return_value=mock.Mock()) as mock_method:
83 yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
84 ret_val = os.path.join(self.rally_base.TEMP_DIR, yaml_file)
85 self.assertEqual(self.rally_base.
86 _prepare_test_list('test_file_name'),
88 self.assertTrue(mock_method.called)
89 self.assertTrue(mock_os_makedir.called)
91 def test_get_task_id_default(self):
92 cmd_raw = 'Task 1: started'
93 self.assertEqual(self.rally_base.get_task_id(cmd_raw),
96 def test_get_task_id_missing_id(self):
98 self.assertEqual(self.rally_base.get_task_id(cmd_raw),
101 def test_task_succeed_fail(self):
102 json_raw = json.dumps([None])
103 self.assertEqual(self.rally_base.task_succeed(json_raw),
105 json_raw = json.dumps([{'result': [{'error': ['test_error']}]}])
106 self.assertEqual(self.rally_base.task_succeed(json_raw),
109 def test_task_succeed_success(self):
110 json_raw = json.dumps('')
111 self.assertEqual(self.rally_base.task_succeed(json_raw),
115 if self.polling_iter == 0:
117 self.polling_iter -= 1
120 def test_get_cmd_output(self):
122 attrs = {'poll.side_effect': self.polling,
123 'stdout.readline.return_value': 'line'}
124 proc.configure_mock(**attrs)
125 self.assertEqual(self.rally_base.get_cmd_output(proc),
128 def test_excl_scenario_default(self):
129 CONST.__setattr__('INSTALLER_TYPE', 'test_installer')
130 CONST.__setattr__('DEPLOY_SCENARIO', 'test_scenario')
131 dic = {'scenario': [{'scenarios': ['test_scenario'],
132 'installers': ['test_installer'],
134 with mock.patch('__builtin__.open', mock.mock_open()), \
135 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
138 self.assertEqual(self.rally_base.excl_scenario(),
141 def test_excl_scenario_exception(self):
142 with mock.patch('__builtin__.open', side_effect=Exception):
143 self.assertEqual(self.rally_base.excl_scenario(),
146 def test_excl_func_default(self):
147 CONST.__setattr__('INSTALLER_TYPE', 'test_installer')
148 CONST.__setattr__('DEPLOY_SCENARIO', 'test_scenario')
149 dic = {'functionality': [{'functions': ['no_live_migration'],
151 with mock.patch('__builtin__.open', mock.mock_open()), \
152 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
155 mock.patch.object(self.rally_base, 'live_migration_supported',
157 self.assertEqual(self.rally_base.excl_func(),
160 def test_excl_func_exception(self):
161 with mock.patch('__builtin__.open', side_effect=Exception):
162 self.assertEqual(self.rally_base.excl_func(),
165 def test_file_is_empty_default(self):
166 mock_obj = mock.Mock()
167 attrs = {'st_size': 10}
168 mock_obj.configure_mock(**attrs)
169 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
171 return_value=mock_obj):
172 self.assertEqual(self.rally_base.file_is_empty('test_file_name'),
175 def test_file_is_empty_exception(self):
176 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
178 side_effect=Exception):
179 self.assertEqual(self.rally_base.file_is_empty('test_file_name'),
182 def test_run_task_missing_task_file(self):
183 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
185 return_value=False), \
186 self.assertRaises(Exception):
187 self.rally_base._run_task('test_name')
189 @mock.patch('functest.opnfv_tests.openstack.rally.rally.logger.info')
190 def test_run_task_no_tests_for_scenario(self, mock_logger_info):
191 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
193 return_value=True), \
194 mock.patch.object(self.rally_base, '_prepare_test_list',
195 return_value='test_file_name'), \
196 mock.patch.object(self.rally_base, 'file_is_empty',
198 self.rally_base._run_task('test_name')
199 str = 'No tests for scenario "test_name"'
200 mock_logger_info.assert_any_call(str)
202 @mock.patch('functest.opnfv_tests.openstack.rally.rally.logger.error')
203 def test_run_task_taskid_missing(self, mock_logger_error):
204 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
206 return_value=True), \
207 mock.patch.object(self.rally_base, '_prepare_test_list',
208 return_value='test_file_name'), \
209 mock.patch.object(self.rally_base, 'file_is_empty',
210 return_value=False), \
211 mock.patch.object(self.rally_base, '_build_task_args',
213 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
214 'subprocess.Popen'), \
215 mock.patch.object(self.rally_base, '_get_output',
216 return_value=mock.Mock()), \
217 mock.patch.object(self.rally_base, 'get_task_id',
218 return_value=None), \
219 mock.patch.object(self.rally_base, 'get_cmd_output',
221 self.rally_base._run_task('test_name')
222 str = 'Failed to retrieve task_id, validating task...'
223 mock_logger_error.assert_any_call(str)
225 @mock.patch('functest.opnfv_tests.openstack.rally.rally.logger.info')
226 @mock.patch('functest.opnfv_tests.openstack.rally.rally.logger.error')
227 def test_run_task_default(self, mock_logger_error,
230 attrs = {'read.return_value': 'json_result'}
231 popen.configure_mock(**attrs)
233 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
235 return_value=True), \
236 mock.patch.object(self.rally_base, '_prepare_test_list',
237 return_value='test_file_name'), \
238 mock.patch.object(self.rally_base, 'file_is_empty',
239 return_value=False), \
240 mock.patch.object(self.rally_base, '_build_task_args',
242 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
243 'subprocess.Popen'), \
244 mock.patch.object(self.rally_base, '_get_output',
245 return_value=mock.Mock()), \
246 mock.patch.object(self.rally_base, 'get_task_id',
248 mock.patch.object(self.rally_base, 'get_cmd_output',
250 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
252 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
254 return_value=popen), \
255 mock.patch('__builtin__.open', mock.mock_open()), \
256 mock.patch.object(self.rally_base, 'task_succeed',
258 self.rally_base._run_task('test_name')
259 str = 'Test scenario: "test_name" OK.\n'
260 mock_logger_info.assert_any_call(str)
262 def test_prepare_env_testname_invalid(self):
263 self.rally_base.TESTS = ['test1', 'test2']
264 self.rally_base.test_name = 'test'
265 with self.assertRaises(Exception):
266 self.rally_base._prepare_env()
268 def test_prepare_env_volume_creation_failed(self):
269 self.rally_base.TESTS = ['test1', 'test2']
270 self.rally_base.test_name = 'test1'
272 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
273 'os_utils.list_volume_types',
274 return_value=None), \
275 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
276 'os_utils.create_volume_type',
277 return_value=volume_type), \
278 self.assertRaises(Exception):
279 self.rally_base._prepare_env()
281 def test_prepare_env_image_missing(self):
282 self.rally_base.TESTS = ['test1', 'test2']
283 self.rally_base.test_name = 'test1'
284 volume_type = mock.Mock()
286 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
287 'os_utils.list_volume_types',
288 return_value=None), \
289 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
290 'os_utils.create_volume_type',
291 return_value=volume_type), \
292 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
293 'os_utils.get_or_create_image',
294 return_value=(True, image_id)), \
295 self.assertRaises(Exception):
296 self.rally_base._prepare_env()
298 def test_prepare_env_image_shared_network_creation_failed(self):
299 self.rally_base.TESTS = ['test1', 'test2']
300 self.rally_base.test_name = 'test1'
301 volume_type = mock.Mock()
302 image_id = 'image_id'
304 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
305 'os_utils.list_volume_types',
306 return_value=None), \
307 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
308 'os_utils.create_volume_type',
309 return_value=volume_type), \
310 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
311 'os_utils.get_or_create_image',
312 return_value=(True, image_id)), \
313 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
314 'os_utils.create_shared_network_full',
315 return_value=network_dict), \
316 self.assertRaises(Exception):
317 self.rally_base._prepare_env()
319 def test_run_tests_all(self):
320 self.rally_base.TESTS = ['test1', 'test2']
321 self.rally_base.test_name = 'all'
322 with mock.patch.object(self.rally_base, '_run_task',
323 return_value=mock.Mock()):
324 self.rally_base._run_tests()
325 self.rally_base._run_task.assert_any_call('test1')
326 self.rally_base._run_task.assert_any_call('test2')
328 def test_run_tests_default(self):
329 self.rally_base.TESTS = ['test1', 'test2']
330 self.rally_base.test_name = 'test1'
331 with mock.patch.object(self.rally_base, '_run_task',
332 return_value=mock.Mock()):
333 self.rally_base._run_tests()
334 self.rally_base._run_task.assert_any_call('test1')
336 def test_clean_up_default(self):
337 self.rally_base.volume_type = mock.Mock()
338 self.rally_base.cinder_client = mock.Mock()
339 self.rally_base.image_exists = False
340 self.rally_base.image_id = 1
341 self.rally_base.nova_client = mock.Mock()
342 with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
343 'os_utils.delete_volume_type') as mock_vol_method, \
344 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
345 'os_utils.delete_glance_image') as mock_glance_method:
346 self.rally_base._clean_up()
347 mock_vol_method.assert_any_call(self.rally_base.cinder_client,
348 self.rally_base.volume_type)
349 mock_glance_method.assert_any_call(self.rally_base.nova_client,
352 def test_run_default(self):
353 with mock.patch.object(self.rally_base, '_prepare_env'), \
354 mock.patch.object(self.rally_base, '_run_tests'), \
355 mock.patch.object(self.rally_base, '_generate_report'), \
356 mock.patch.object(self.rally_base, '_clean_up'):
357 self.assertEqual(self.rally_base.run(),
358 testcase.TestCase.EX_OK)
360 def test_run_exception(self):
361 with mock.patch.object(self.rally_base, '_prepare_env',
362 side_effect=Exception):
363 self.assertEqual(self.rally_base.run(),
364 testcase.TestCase.EX_RUN_ERROR)
367 if __name__ == "__main__":
368 logging.disable(logging.CRITICAL)
369 unittest.main(verbosity=2)