Merge "Remove Rally requests scenario"
[functest-xtesting.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
22     def setUp(self):
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'
37             self.polling_iter = 2
38
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',
43                         return_value=None):
44             task_args = self.rally_base._build_task_args('test_file_name')
45             self.assertEqual(task_args['floating_network'], '')
46
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'], '')
55
56     def check_scenario_file(self, value):
57         yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
58         if yaml_file in value:
59             return False
60         return True
61
62     def test_prepare_test_list_missing_scenario_file(self):
63         with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
64                         'os.path.exists',
65                         side_effect=self.check_scenario_file), \
66                 self.assertRaises(Exception):
67             self.rally_base._prepare_test_list('test_file_name')
68
69     def check_temp_dir(self, value):
70         yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
71         if yaml_file in value:
72             return True
73         return False
74
75     def test_prepare_test_list_missing_temp_dir(self):
76         with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
77                         'os.path.exists',
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'),
87                              ret_val)
88             self.assertTrue(mock_method.called)
89             self.assertTrue(mock_os_makedir.called)
90
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),
94                          '1')
95
96     def test_get_task_id_missing_id(self):
97         cmd_raw = ''
98         self.assertEqual(self.rally_base.get_task_id(cmd_raw),
99                          None)
100
101     def test_task_succeed_fail(self):
102         json_raw = json.dumps([None])
103         self.assertEqual(self.rally_base.task_succeed(json_raw),
104                          False)
105         json_raw = json.dumps([{'result': [{'error': ['test_error']}]}])
106         self.assertEqual(self.rally_base.task_succeed(json_raw),
107                          False)
108
109     def test_task_succeed_success(self):
110         json_raw = json.dumps('')
111         self.assertEqual(self.rally_base.task_succeed(json_raw),
112                          True)
113
114     def polling(self):
115         if self.polling_iter == 0:
116             return "something"
117         self.polling_iter -= 1
118         return None
119
120     def test_get_cmd_output(self):
121         proc = mock.Mock()
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),
126                          'lineline')
127
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'],
133                              'tests': ['test']}]}
134         with mock.patch('__builtin__.open', mock.mock_open()), \
135             mock.patch('functest.opnfv_tests.openstack.rally.rally.'
136                        'yaml.safe_load',
137                        return_value=dic):
138                 self.assertEqual(self.rally_base.excl_scenario(),
139                                  ['test'])
140
141     def test_excl_scenario_exception(self):
142         with mock.patch('__builtin__.open', side_effect=Exception):
143                 self.assertEqual(self.rally_base.excl_scenario(),
144                                  [])
145
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'],
150                                   'tests': ['test']}]}
151         with mock.patch('__builtin__.open', mock.mock_open()), \
152             mock.patch('functest.opnfv_tests.openstack.rally.rally.'
153                        'yaml.safe_load',
154                        return_value=dic), \
155             mock.patch.object(self.rally_base, 'live_migration_supported',
156                               return_value=False):
157                 self.assertEqual(self.rally_base.excl_func(),
158                                  ['test'])
159
160     def test_excl_func_exception(self):
161         with mock.patch('__builtin__.open', side_effect=Exception):
162                 self.assertEqual(self.rally_base.excl_func(),
163                                  [])
164
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.'
170                         'os.stat',
171                         return_value=mock_obj):
172             self.assertEqual(self.rally_base.file_is_empty('test_file_name'),
173                              False)
174
175     def test_file_is_empty_exception(self):
176         with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
177                         'os.stat',
178                         side_effect=Exception):
179             self.assertEqual(self.rally_base.file_is_empty('test_file_name'),
180                              True)
181
182     def test_run_task_missing_task_file(self):
183         with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
184                         'os.path.exists',
185                         return_value=False), \
186                 self.assertRaises(Exception):
187             self.rally_base._run_task('test_name')
188
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.'
192                         'os.path.exists',
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',
197                               return_value=True):
198             self.rally_base._run_task('test_name')
199             str = 'No tests for scenario "test_name"'
200             mock_logger_info.assert_any_call(str)
201
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.'
205                         'os.path.exists',
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',
212                               return_value={}), \
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',
220                               return_value=''):
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)
224
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,
228                               mock_logger_info):
229         popen = mock.Mock()
230         attrs = {'read.return_value': 'json_result'}
231         popen.configure_mock(**attrs)
232
233         with mock.patch('functest.opnfv_tests.openstack.rally.rally.'
234                         'os.path.exists',
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',
241                               return_value={}), \
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',
247                               return_value='1'), \
248             mock.patch.object(self.rally_base, 'get_cmd_output',
249                               return_value=''), \
250             mock.patch('functest.opnfv_tests.openstack.rally.rally.'
251                        'os.makedirs'), \
252             mock.patch('functest.opnfv_tests.openstack.rally.rally.'
253                        'os.popen',
254                        return_value=popen), \
255             mock.patch('__builtin__.open', mock.mock_open()), \
256             mock.patch.object(self.rally_base, 'task_succeed',
257                               return_value=True):
258             self.rally_base._run_task('test_name')
259             str = 'Test scenario: "test_name" OK.\n'
260             mock_logger_info.assert_any_call(str)
261
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()
267
268     def test_prepare_env_volume_creation_failed(self):
269         self.rally_base.TESTS = ['test1', 'test2']
270         self.rally_base.test_name = 'test1'
271         volume_type = None
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()
280
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()
285         image_id = None
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()
297
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'
303         network_dict = None
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()
318
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')
327
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')
335
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,
350                                                1)
351
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)
359
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)
365
366
367 if __name__ == "__main__":
368     logging.disable(logging.CRITICAL)
369     unittest.main(verbosity=2)