Dedicated flavors for rally tests
[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 from snaps.openstack.os_credentials import OSCreds
20
21
22 class OSRallyTesting(unittest.TestCase):
23     def setUp(self):
24         os_creds = OSCreds(
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()
30             self.polling_iter = 2
31         self.assertTrue(m.called)
32
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'], '')
38
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'], '')
44
45     @staticmethod
46     def check_scenario_file(value):
47         yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
48         if yaml_file in value:
49             return False
50         return True
51
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()
58
59     @staticmethod
60     def check_temp_dir(value):
61         yaml_file = 'opnfv-{}.yaml'.format('test_file_name')
62         if yaml_file in value:
63             return True
64         return False
65
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.'
69                 'apply_blacklist', return_value=mock.Mock())
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
73
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'),
77                          ret_val)
78         mock_path_exists.assert_called()
79         mock_method.assert_called()
80         mock_os_makedirs.assert_called()
81
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),
85                          '1')
86
87     def test_get_task_id_missing_id(self):
88         cmd_raw = ''
89         self.assertEqual(self.rally_base.get_task_id(cmd_raw),
90                          None)
91
92     def test_task_succeed_fail(self):
93         json_raw = json.dumps([None])
94         self.assertEqual(self.rally_base.task_succeed(json_raw),
95                          False)
96         json_raw = json.dumps([{'result': [{'error': ['test_error']}]}])
97         self.assertEqual(self.rally_base.task_succeed(json_raw),
98                          False)
99
100     def test_task_succeed_success(self):
101         json_raw = json.dumps('')
102         self.assertEqual(self.rally_base.task_succeed(json_raw),
103                          True)
104
105     def polling(self):
106         if self.polling_iter == 0:
107             return "something"
108         self.polling_iter -= 1
109         return None
110
111     def test_get_cmd_output(self):
112         proc = mock.Mock()
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),
117                          'lineline')
118
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'],
124                      'tests': ['test']},
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()
133
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'],
139                      'tests': ['test1']},
140                     {'scenarios': ['^os-ctrlT-[^-]+-modeT$'],
141                      'installers': ['test_installer'],
142                      'tests': ['test2']},
143                     {'scenarios': ['^os-ctrlT-featT-[^-]+$'],
144                      'installers': ['test_installer'],
145                      'tests': ['test3']},
146                     {'scenarios': ['^os-'],
147                      'installers': ['test_installer'],
148                      'tests': ['test4']},
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()
161
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()
166
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()
179
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()
184
185     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.stat',
186                 return_value=mock.Mock())
187     def test_file_is_empty_default(self, mock_os_stat):
188         attrs = {'st_size': 10}
189         mock_os_stat.return_value.configure_mock(**attrs)
190         self.assertEqual(self.rally_base.file_is_empty('test_file_name'),
191                          False)
192         mock_os_stat.assert_called()
193
194     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.stat',
195                 side_effect=Exception)
196     def test_file_is_empty_exception(self, mock_os_stat):
197         self.assertEqual(self.rally_base.file_is_empty('test_file_name'), True)
198         mock_os_stat.assert_called()
199
200     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
201                 return_value=False)
202     def test_run_task_missing_task_file(self, mock_path_exists):
203         with self.assertRaises(Exception):
204             self.rally_base._run_task('test_name')
205         mock_path_exists.assert_called()
206
207     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
208                 return_value=True)
209     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
210                 '_prepare_test_list', return_value='test_file_name')
211     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
212                 'file_is_empty', return_value=True)
213     @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.info')
214     def test_run_task_no_tests_for_scenario(self, mock_logger_info,
215                                             mock_file_empty, mock_prep_list,
216                                             mock_path_exists):
217         self.rally_base._run_task('test_name')
218         mock_logger_info.assert_any_call('No tests for scenario \"%s\"',
219                                          'test_name')
220         mock_file_empty.assert_called()
221         mock_prep_list.assert_called()
222         mock_path_exists.assert_called()
223
224     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
225                 '_prepare_test_list', return_value='test_file_name')
226     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
227                 'file_is_empty', return_value=False)
228     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
229                 '_build_task_args', return_value={})
230     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
231                 '_get_output', return_value=mock.Mock())
232     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
233                 'get_task_id', return_value=None)
234     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
235                 'get_cmd_output', return_value='')
236     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
237                 return_value=True)
238     @mock.patch('functest.opnfv_tests.openstack.rally.rally.subprocess.Popen')
239     @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.error')
240     def test_run_task_taskid_missing(self, mock_logger_error, *args):
241         self.rally_base._run_task('test_name')
242         text = 'Failed to retrieve task_id, validating task...'
243         mock_logger_error.assert_any_call(text)
244
245     @mock.patch('__builtin__.open', mock.mock_open())
246     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
247                 '_prepare_test_list', return_value='test_file_name')
248     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
249                 'file_is_empty', return_value=False)
250     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
251                 '_build_task_args', return_value={})
252     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
253                 '_get_output', return_value=mock.Mock())
254     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
255                 'get_task_id', return_value='1')
256     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
257                 'get_cmd_output', return_value='')
258     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
259                 'task_succeed', return_value=True)
260     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
261                 return_value=True)
262     @mock.patch('functest.opnfv_tests.openstack.rally.rally.subprocess.Popen')
263     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.makedirs')
264     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.popen',
265                 return_value=mock.Mock())
266     @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.info')
267     @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.error')
268     def test_run_task_default(self, mock_logger_error, mock_logger_info,
269                               mock_popen, *args):
270         attrs = {'read.return_value': 'json_result'}
271         mock_popen.return_value.configure_mock(**attrs)
272         self.rally_base._run_task('test_name')
273         text = 'Test scenario: "test_name" OK.\n'
274         mock_logger_info.assert_any_call(text)
275         mock_logger_error.assert_not_called()
276
277     def test_prepare_env_testname_invalid(self):
278         self.rally_base.TESTS = ['test1', 'test2']
279         self.rally_base.test_name = 'test'
280         with self.assertRaises(Exception):
281             self.rally_base._prepare_env()
282
283     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
284                 'get_ext_net_name', return_value='test_net_name')
285     @mock.patch('snaps.openstack.utils.deploy_utils.create_image',
286                 return_value=None)
287     def test_prepare_env_image_missing(self, mock_get_img, mock_get_net):
288         self.rally_base.TESTS = ['test1', 'test2']
289         self.rally_base.test_name = 'test1'
290         with self.assertRaises(Exception):
291             self.rally_base._prepare_env()
292         mock_get_img.assert_called()
293         mock_get_net.assert_called()
294
295     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
296                 'get_ext_net_name', return_value='test_net_name')
297     @mock.patch('snaps.openstack.utils.deploy_utils.create_image',
298                 return_value=mock.Mock())
299     @mock.patch('snaps.openstack.utils.deploy_utils.create_network',
300                 return_value=None)
301     def test_prepare_env_network_creation_failed(
302             self, mock_create_net, mock_get_img, mock_get_net):
303         self.rally_base.TESTS = ['test1', 'test2']
304         self.rally_base.test_name = 'test1'
305         with self.assertRaises(Exception):
306             self.rally_base._prepare_env()
307         mock_create_net.assert_called()
308         mock_get_img.assert_called()
309         mock_get_net.assert_called()
310
311     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
312                 'get_ext_net_name', return_value='test_net_name')
313     @mock.patch('snaps.openstack.utils.deploy_utils.create_image',
314                 return_value=mock.Mock())
315     @mock.patch('snaps.openstack.utils.deploy_utils.create_network',
316                 return_value=mock.Mock())
317     @mock.patch('snaps.openstack.utils.deploy_utils.create_router',
318                 return_value=None)
319     def test_prepare_env_router_creation_failed(
320             self, mock_create_router, mock_create_net, mock_get_img,
321             mock_get_net):
322         self.rally_base.TESTS = ['test1', 'test2']
323         self.rally_base.test_name = 'test1'
324         with self.assertRaises(Exception):
325             self.rally_base._prepare_env()
326         mock_create_net.assert_called()
327         mock_get_img.assert_called()
328         mock_get_net.assert_called()
329         mock_create_router.assert_called()
330
331     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
332                 'get_ext_net_name', return_value='test_net_name')
333     @mock.patch('snaps.openstack.utils.deploy_utils.create_image',
334                 return_value=mock.Mock())
335     @mock.patch('snaps.openstack.utils.deploy_utils.create_network',
336                 return_value=mock.Mock())
337     @mock.patch('snaps.openstack.utils.deploy_utils.create_router',
338                 return_value=mock.Mock())
339     @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create',
340                 return_value=None)
341     def test_prepare_env_flavor_creation_failed(
342             self, mock_create_flavor, mock_create_router, mock_create_net,
343             mock_get_img, mock_get_net):
344         self.rally_base.TESTS = ['test1', 'test2']
345         self.rally_base.test_name = 'test1'
346         with self.assertRaises(Exception):
347             self.rally_base._prepare_env()
348         mock_create_net.assert_called()
349         mock_get_img.assert_called()
350         mock_get_net.assert_called()
351         mock_create_router.assert_called()
352         mock_create_flavor.assert_called_once()
353
354     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
355                 'get_ext_net_name', return_value='test_net_name')
356     @mock.patch('snaps.openstack.utils.deploy_utils.create_image',
357                 return_value=mock.Mock())
358     @mock.patch('snaps.openstack.utils.deploy_utils.create_network',
359                 return_value=mock.Mock())
360     @mock.patch('snaps.openstack.utils.deploy_utils.create_router',
361                 return_value=mock.Mock())
362     @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create',
363                 side_effect=[mock.Mock, None])
364     def test_prepare_env_flavor_alt_creation_failed(
365             self, mock_create_flavor, mock_create_router, mock_create_net,
366             mock_get_img, mock_get_net):
367         self.rally_base.TESTS = ['test1', 'test2']
368         self.rally_base.test_name = 'test1'
369         with self.assertRaises(Exception):
370             self.rally_base._prepare_env()
371         mock_create_net.assert_called()
372         mock_get_img.assert_called()
373         mock_get_net.assert_called()
374         mock_create_router.assert_called()
375         self.assertEqual(mock_create_flavor.call_count, 2)
376
377     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
378                 '_run_task', return_value=mock.Mock())
379     def test_run_tests_all(self, mock_run_task):
380         self.rally_base.TESTS = ['test1', 'test2']
381         self.rally_base.test_name = 'all'
382         self.rally_base._run_tests()
383         mock_run_task.assert_any_call('test1')
384         mock_run_task.assert_any_call('test2')
385
386     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
387                 '_run_task', return_value=mock.Mock())
388     def test_run_tests_default(self, mock_run_task):
389         self.rally_base.TESTS = ['test1', 'test2']
390         self.rally_base.test_name = 'test1'
391         self.rally_base._run_tests()
392         mock_run_task.assert_any_call('test1')
393
394     def test_clean_up_default(self):
395         creator1 = mock.Mock()
396         creator2 = mock.Mock()
397         self.rally_base.creators = [creator1, creator2]
398         self.rally_base._clean_up()
399         self.assertTrue(creator1.clean.called)
400         self.assertTrue(creator2.clean.called)
401
402     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
403                 '_prepare_env')
404     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
405                 '_run_tests')
406     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
407                 '_generate_report')
408     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
409                 '_clean_up')
410     def test_run_default(self, *args):
411         self.assertEqual(self.rally_base.run(), testcase.TestCase.EX_OK)
412         map(lambda m: m.assert_called(), args)
413
414     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
415                 '_prepare_env', side_effect=Exception)
416     def test_run_exception(self, mock_prep_env):
417         self.assertEqual(self.rally_base.run(), testcase.TestCase.EX_RUN_ERROR)
418         mock_prep_env.assert_called()
419
420
421 if __name__ == "__main__":
422     logging.disable(logging.CRITICAL)
423     unittest.main(verbosity=2)