Merge "Use decorators for mock patching"
[functest.git] / functest / tests / unit / openstack / rally / test_rally.py
1 #!/usr/bin/env python
2
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
7
8 import json
9 import logging
10 import os
11 import unittest
12
13 import mock
14
15 from functest.core import testcase
16 from functest.opnfv_tests.openstack.rally import rally
17 from functest.utils.constants import CONST
18
19
20 class OSRallyTesting(unittest.TestCase):
21     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os_utils.'
22                 'get_nova_client', return_value=mock.Mock())
23     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os_utils.'
24                 'get_neutron_client', return_value=mock.Mock())
25     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os_utils.'
26                 'get_cinder_client', return_value=mock.Mock())
27     def setUp(self, mock_func1, mock_func2, mock_func3):
28         self.rally_base = rally.RallyBase()
29         self.rally_base.network_dict['net_id'] = 'test_net_id'
30         self.polling_iter = 2
31         mock_func1.assert_called()
32         mock_func2.assert_called()
33         mock_func3.assert_called()
34
35     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os_utils.'
36                 'get_external_net', return_value=None)
37     def test_build_task_args_missing_floating_network(self, mock_func):
38         CONST.__setattr__('OS_AUTH_URL', None)
39         task_args = self.rally_base._build_task_args('test_file_name')
40         self.assertEqual(task_args['floating_network'], '')
41         mock_func.assert_called()
42
43     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os_utils.'
44                 'get_external_net', return_value='test_floating_network')
45     def test_build_task_args_missing_net_id(self, mock_func):
46         CONST.__setattr__('OS_AUTH_URL', None)
47         self.rally_base.network_dict['net_id'] = ''
48         task_args = self.rally_base._build_task_args('test_file_name')
49         self.assertEqual(task_args['netid'], '')
50         mock_func.assert_called()
51
52     @staticmethod
53     def check_scenario_file(value):
54         yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
55         if yaml_file in value:
56             return False
57         return True
58
59     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists')
60     def test_prepare_test_list_missing_scenario_file(self, mock_func):
61         mock_func.side_effect = self.check_scenario_file
62         with self.assertRaises(Exception):
63             self.rally_base._prepare_test_list('test_file_name')
64         mock_func.assert_called()
65
66     @staticmethod
67     def check_temp_dir(value):
68         yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
69         if yaml_file in value:
70             return True
71         return False
72
73     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists')
74     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.makedirs')
75     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
76                 'apply_blacklist', return_value=mock.Mock())
77     def test_prepare_test_list_missing_temp_dir(
78             self, mock_method, mock_os_makedirs, mock_path_exists):
79         mock_path_exists.side_effect = self.check_temp_dir
80
81         yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
82         ret_val = os.path.join(self.rally_base.TEMP_DIR, yaml_file)
83         self.assertEqual(self.rally_base._prepare_test_list('test_file_name'),
84                          ret_val)
85         mock_path_exists.assert_called()
86         mock_method.assert_called()
87         mock_os_makedirs.assert_called()
88
89     def test_get_task_id_default(self):
90         cmd_raw = 'Task 1: started'
91         self.assertEqual(self.rally_base.get_task_id(cmd_raw),
92                          '1')
93
94     def test_get_task_id_missing_id(self):
95         cmd_raw = ''
96         self.assertEqual(self.rally_base.get_task_id(cmd_raw),
97                          None)
98
99     def test_task_succeed_fail(self):
100         json_raw = json.dumps([None])
101         self.assertEqual(self.rally_base.task_succeed(json_raw),
102                          False)
103         json_raw = json.dumps([{'result': [{'error': ['test_error']}]}])
104         self.assertEqual(self.rally_base.task_succeed(json_raw),
105                          False)
106
107     def test_task_succeed_success(self):
108         json_raw = json.dumps('')
109         self.assertEqual(self.rally_base.task_succeed(json_raw),
110                          True)
111
112     def polling(self):
113         if self.polling_iter == 0:
114             return "something"
115         self.polling_iter -= 1
116         return None
117
118     def test_get_cmd_output(self):
119         proc = mock.Mock()
120         attrs = {'poll.side_effect': self.polling,
121                  'stdout.readline.return_value': 'line'}
122         proc.configure_mock(**attrs)
123         self.assertEqual(self.rally_base.get_cmd_output(proc),
124                          'lineline')
125
126     @mock.patch('__builtin__.open', mock.mock_open())
127     @mock.patch('functest.opnfv_tests.openstack.rally.rally.yaml.safe_load',
128                 return_value={'scenario': [
129                     {'scenarios': ['test_scenario'],
130                      'installers': ['test_installer'],
131                      'tests': ['test']},
132                     {'scenarios': ['other_scenario'],
133                      'installers': ['test_installer'],
134                      'tests': ['other_test']}]})
135     def test_excl_scenario_default(self, mock_func):
136         CONST.__setattr__('INSTALLER_TYPE', 'test_installer')
137         CONST.__setattr__('DEPLOY_SCENARIO', 'test_scenario')
138         self.assertEqual(self.rally_base.excl_scenario(), ['test'])
139         mock_func.assert_called()
140
141     @mock.patch('__builtin__.open', mock.mock_open())
142     @mock.patch('functest.opnfv_tests.openstack.rally.rally.yaml.safe_load',
143                 return_value={'scenario': [
144                     {'scenarios': ['^os-[^-]+-featT-modeT$'],
145                      'installers': ['test_installer'],
146                      'tests': ['test1']},
147                     {'scenarios': ['^os-ctrlT-[^-]+-modeT$'],
148                      'installers': ['test_installer'],
149                      'tests': ['test2']},
150                     {'scenarios': ['^os-ctrlT-featT-[^-]+$'],
151                      'installers': ['test_installer'],
152                      'tests': ['test3']},
153                     {'scenarios': ['^os-'],
154                      'installers': ['test_installer'],
155                      'tests': ['test4']},
156                     {'scenarios': ['other_scenario'],
157                      'installers': ['test_installer'],
158                      'tests': ['test0a']},
159                     {'scenarios': [''],  # empty scenario
160                      'installers': ['test_installer'],
161                      'tests': ['test0b']}]})
162     def test_excl_scenario_regex(self, mock_func):
163         CONST.__setattr__('INSTALLER_TYPE', 'test_installer')
164         CONST.__setattr__('DEPLOY_SCENARIO', 'os-ctrlT-featT-modeT')
165         self.assertEqual(self.rally_base.excl_scenario(),
166                          ['test1', 'test2', 'test3', 'test4'])
167         mock_func.assert_called()
168
169     @mock.patch('__builtin__.open', side_effect=Exception)
170     def test_excl_scenario_exception(self, mock_open):
171         self.assertEqual(self.rally_base.excl_scenario(), [])
172         mock_open.assert_called()
173
174     @mock.patch('__builtin__.open', mock.mock_open())
175     @mock.patch('functest.opnfv_tests.openstack.rally.rally.yaml.safe_load',
176                 return_value={'functionality': [
177                     {'functions': ['no_live_migration'], 'tests': ['test']}]})
178     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
179                 'live_migration_supported', return_value=False)
180     def test_excl_func_default(self, mock_func, mock_yaml_load):
181         CONST.__setattr__('INSTALLER_TYPE', 'test_installer')
182         CONST.__setattr__('DEPLOY_SCENARIO', 'test_scenario')
183         self.assertEqual(self.rally_base.excl_func(), ['test'])
184         mock_func.assert_called()
185         mock_yaml_load.assert_called()
186
187     @mock.patch('__builtin__.open', side_effect=Exception)
188     def test_excl_func_exception(self, mock_open):
189         self.assertEqual(self.rally_base.excl_func(), [])
190         mock_open.assert_called()
191
192     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.stat',
193                 return_value=mock.Mock())
194     def test_file_is_empty_default(self, mock_os_stat):
195         attrs = {'st_size': 10}
196         mock_os_stat.return_value.configure_mock(**attrs)
197         self.assertEqual(self.rally_base.file_is_empty('test_file_name'),
198                          False)
199         mock_os_stat.assert_called()
200
201     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.stat',
202                 side_effect=Exception)
203     def test_file_is_empty_exception(self, mock_os_stat):
204         self.assertEqual(self.rally_base.file_is_empty('test_file_name'), True)
205         mock_os_stat.assert_called()
206
207     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
208                 return_value=False)
209     def test_run_task_missing_task_file(self, mock_path_exists):
210         with self.assertRaises(Exception):
211             self.rally_base._run_task('test_name')
212         mock_path_exists.assert_called()
213
214     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
215                 return_value=True)
216     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
217                 '_prepare_test_list', return_value='test_file_name')
218     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
219                 'file_is_empty', return_value=True)
220     @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.info')
221     def test_run_task_no_tests_for_scenario(self, mock_logger_info,
222                                             mock_file_empty, mock_prep_list,
223                                             mock_path_exists):
224         self.rally_base._run_task('test_name')
225         mock_logger_info.assert_any_call('No tests for scenario \"%s\"',
226                                          'test_name')
227         mock_file_empty.assert_called()
228         mock_prep_list.assert_called()
229         mock_path_exists.assert_called()
230
231     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
232                 '_prepare_test_list', return_value='test_file_name')
233     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
234                 'file_is_empty', return_value=False)
235     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
236                 '_build_task_args', return_value={})
237     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
238                 '_get_output', return_value=mock.Mock())
239     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
240                 'get_task_id', return_value=None)
241     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
242                 'get_cmd_output', return_value='')
243     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
244                 return_value=True)
245     @mock.patch('functest.opnfv_tests.openstack.rally.rally.subprocess.Popen')
246     @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.error')
247     def test_run_task_taskid_missing(self, mock_logger_error, *args):
248         self.rally_base._run_task('test_name')
249         text = 'Failed to retrieve task_id, validating task...'
250         mock_logger_error.assert_any_call(text)
251
252     @mock.patch('__builtin__.open', mock.mock_open())
253     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
254                 '_prepare_test_list', return_value='test_file_name')
255     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
256                 'file_is_empty', return_value=False)
257     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
258                 '_build_task_args', return_value={})
259     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
260                 '_get_output', return_value=mock.Mock())
261     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
262                 'get_task_id', return_value='1')
263     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
264                 'get_cmd_output', return_value='')
265     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
266                 'task_succeed', return_value=True)
267     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
268                 return_value=True)
269     @mock.patch('functest.opnfv_tests.openstack.rally.rally.subprocess.Popen')
270     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.makedirs')
271     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.popen',
272                 return_value=mock.Mock())
273     @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.info')
274     @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.error')
275     def test_run_task_default(self, mock_logger_error, mock_logger_info,
276                               mock_popen, *args):
277         attrs = {'read.return_value': 'json_result'}
278         mock_popen.return_value.configure_mock(**attrs)
279         self.rally_base._run_task('test_name')
280         text = 'Test scenario: "test_name" OK.\n'
281         mock_logger_info.assert_any_call(text)
282         mock_logger_error.assert_not_called()
283
284     def test_prepare_env_testname_invalid(self):
285         self.rally_base.TESTS = ['test1', 'test2']
286         self.rally_base.test_name = 'test'
287         with self.assertRaises(Exception):
288             self.rally_base._prepare_env()
289
290     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os_utils.'
291                 'list_volume_types', return_value=None)
292     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os_utils.'
293                 'create_volume_type', return_value=None)
294     def test_prepare_env_volume_creation_failed(self, mock_list, mock_create):
295         self.rally_base.TESTS = ['test1', 'test2']
296         self.rally_base.test_name = 'test1'
297         with self.assertRaises(Exception):
298             self.rally_base._prepare_env()
299         mock_list.assert_called()
300         mock_create.assert_called()
301
302     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os_utils.'
303                 'list_volume_types', return_value=None)
304     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os_utils.'
305                 'create_volume_type', return_value=mock.Mock())
306     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os_utils.'
307                 'get_or_create_image', return_value=(True, None))
308     def test_prepare_env_image_missing(self, mock_get_img, mock_create_vt,
309                                        mock_list_vt):
310         self.rally_base.TESTS = ['test1', 'test2']
311         self.rally_base.test_name = 'test1'
312         with self.assertRaises(Exception):
313             self.rally_base._prepare_env()
314         mock_get_img.assert_called()
315         mock_create_vt.assert_called()
316         mock_list_vt.assert_called()
317
318     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os_utils.'
319                 'list_volume_types', return_value=None)
320     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os_utils.'
321                 'create_volume_type', return_value=mock.Mock())
322     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os_utils.'
323                 'get_or_create_image', return_value=(True, 'image_id'))
324     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os_utils.'
325                 'create_shared_network_full', return_value=None)
326     def test_prepare_env_image_shared_network_creation_failed(
327             self, mock_create_net, mock_get_img, mock_create_vt, mock_list_vt):
328         self.rally_base.TESTS = ['test1', 'test2']
329         self.rally_base.test_name = 'test1'
330         with self.assertRaises(Exception):
331             self.rally_base._prepare_env()
332         mock_create_net.assert_called()
333         mock_get_img.assert_called()
334         mock_create_vt.assert_called()
335         mock_list_vt.assert_called()
336
337     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
338                 '_run_task', return_value=mock.Mock())
339     def test_run_tests_all(self, mock_run_task):
340         self.rally_base.TESTS = ['test1', 'test2']
341         self.rally_base.test_name = 'all'
342         self.rally_base._run_tests()
343         mock_run_task.assert_any_call('test1')
344         mock_run_task.assert_any_call('test2')
345
346     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
347                 '_run_task', return_value=mock.Mock())
348     def test_run_tests_default(self, mock_run_task):
349         self.rally_base.TESTS = ['test1', 'test2']
350         self.rally_base.test_name = 'test1'
351         self.rally_base._run_tests()
352         mock_run_task.assert_any_call('test1')
353
354     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os_utils.'
355                 'delete_volume_type')
356     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os_utils.'
357                 'delete_glance_image')
358     def test_clean_up_default(self, mock_glance_method, mock_vol_method):
359         self.rally_base.volume_type = mock.Mock()
360         self.rally_base.cinder_client = mock.Mock()
361         self.rally_base.image_exists = False
362         self.rally_base.image_id = 1
363         self.rally_base.nova_client = mock.Mock()
364         self.rally_base._clean_up()
365         mock_vol_method.assert_any_call(self.rally_base.cinder_client,
366                                         self.rally_base.volume_type)
367         mock_glance_method.assert_any_call(self.rally_base.nova_client,
368                                            1)
369
370     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
371                 '_prepare_env')
372     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
373                 '_run_tests')
374     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
375                 '_generate_report')
376     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
377                 '_clean_up')
378     def test_run_default(self, *args):
379         self.assertEqual(self.rally_base.run(), testcase.TestCase.EX_OK)
380         map(lambda m: m.assert_called(), args)
381
382     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
383                 '_prepare_env', side_effect=Exception)
384     def test_run_exception(self, mock_prep_env):
385         self.assertEqual(self.rally_base.run(), testcase.TestCase.EX_RUN_ERROR)
386         mock_prep_env.assert_called()
387
388
389 if __name__ == "__main__":
390     logging.disable(logging.CRITICAL)
391     unittest.main(verbosity=2)