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