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_live_migration'], 'tests': ['test']}]})
171 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
172 'live_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_ext_net_name', return_value='test_net_name')
283 @mock.patch('snaps.openstack.utils.deploy_utils.create_image',
285 def test_prepare_env_image_missing(self, mock_get_img, mock_get_net):
286 self.rally_base.TESTS = ['test1', 'test2']
287 self.rally_base.test_name = 'test1'
288 with self.assertRaises(Exception):
289 self.rally_base._prepare_env()
290 mock_get_img.assert_called()
291 mock_get_net.assert_called()
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):
300 self.rally_base.TESTS = ['test1', 'test2']
301 self.rally_base.test_name = 'test1'
302 with self.assertRaises(Exception):
303 self.rally_base._prepare_env()
304 mock_create_net.assert_called()
305 mock_get_img.assert_called()
306 mock_get_net.assert_called()
308 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
309 'get_ext_net_name', return_value='test_net_name')
310 @mock.patch('snaps.openstack.utils.deploy_utils.create_image')
311 @mock.patch('snaps.openstack.utils.deploy_utils.create_network')
312 @mock.patch('snaps.openstack.utils.deploy_utils.create_router',
314 def test_prepare_env_router_creation_failed(
315 self, mock_create_router, mock_create_net, mock_get_img,
317 self.rally_base.TESTS = ['test1', 'test2']
318 self.rally_base.test_name = 'test1'
319 with self.assertRaises(Exception):
320 self.rally_base._prepare_env()
321 mock_create_net.assert_called()
322 mock_get_img.assert_called()
323 mock_get_net.assert_called()
324 mock_create_router.assert_called()
326 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
327 'get_ext_net_name', return_value='test_net_name')
328 @mock.patch('snaps.openstack.utils.deploy_utils.create_image')
329 @mock.patch('snaps.openstack.utils.deploy_utils.create_network')
330 @mock.patch('snaps.openstack.utils.deploy_utils.create_router')
331 @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create',
333 def test_prepare_env_flavor_creation_failed(
334 self, mock_create_flavor, mock_create_router, mock_create_net,
335 mock_get_img, mock_get_net):
336 self.rally_base.TESTS = ['test1', 'test2']
337 self.rally_base.test_name = 'test1'
338 with self.assertRaises(Exception):
339 self.rally_base._prepare_env()
340 mock_create_net.assert_called()
341 mock_get_img.assert_called()
342 mock_get_net.assert_called()
343 mock_create_router.assert_called()
344 mock_create_flavor.assert_called_once()
346 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
347 'get_ext_net_name', return_value='test_net_name')
348 @mock.patch('snaps.openstack.utils.deploy_utils.create_image')
349 @mock.patch('snaps.openstack.utils.deploy_utils.create_network')
350 @mock.patch('snaps.openstack.utils.deploy_utils.create_router')
351 @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create',
352 side_effect=[mock.Mock, None])
353 def test_prepare_env_flavor_alt_creation_failed(
354 self, mock_create_flavor, mock_create_router, mock_create_net,
355 mock_get_img, mock_get_net):
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()
360 mock_create_net.assert_called()
361 mock_get_img.assert_called()
362 mock_get_net.assert_called()
363 mock_create_router.assert_called()
364 self.assertEqual(mock_create_flavor.call_count, 2)
366 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
368 def test_run_tests_all(self, mock_run_task):
369 self.rally_base.TESTS = ['test1', 'test2']
370 self.rally_base.test_name = 'all'
371 self.rally_base._run_tests()
372 mock_run_task.assert_any_call('test1')
373 mock_run_task.assert_any_call('test2')
375 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
377 def test_run_tests_default(self, mock_run_task):
378 self.rally_base.TESTS = ['test1', 'test2']
379 self.rally_base.test_name = 'test1'
380 self.rally_base._run_tests()
381 mock_run_task.assert_any_call('test1')
383 def test_clean_up_default(self):
384 creator1 = mock.Mock()
385 creator2 = mock.Mock()
386 self.rally_base.creators = [creator1, creator2]
387 self.rally_base._clean_up()
388 self.assertTrue(creator1.clean.called)
389 self.assertTrue(creator2.clean.called)
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)
401 map(lambda m: m.assert_called(), args)
403 @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
404 '_prepare_env', side_effect=Exception)
405 def test_run_exception(self, mock_prep_env):
406 self.assertEqual(self.rally_base.run(), testcase.TestCase.EX_RUN_ERROR)
407 mock_prep_env.assert_called()
410 if __name__ == "__main__":
411 logging.disable(logging.CRITICAL)
412 unittest.main(verbosity=2)