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