Move rally and tempest out of functest-core
[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')
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_migration'], 'tests': ['test']}]})
171     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
172                 '_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     def test_file_is_empty_default(self, mock_os_stat):
187         attrs = {'st_size': 10}
188         mock_os_stat.return_value.configure_mock(**attrs)
189         self.assertEqual(self.rally_base.file_is_empty('test_file_name'),
190                          False)
191         mock_os_stat.assert_called()
192
193     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.stat',
194                 side_effect=Exception)
195     def test_file_is_empty_exception(self, mock_os_stat):
196         self.assertEqual(self.rally_base.file_is_empty('test_file_name'), True)
197         mock_os_stat.assert_called()
198
199     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
200                 return_value=False)
201     def test_run_task_missing_task_file(self, mock_path_exists):
202         with self.assertRaises(Exception):
203             self.rally_base._run_task('test_name')
204         mock_path_exists.assert_called()
205
206     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
207                 return_value=True)
208     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
209                 '_prepare_test_list', return_value='test_file_name')
210     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
211                 'file_is_empty', return_value=True)
212     @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.info')
213     def test_run_task_no_tests_for_scenario(self, mock_logger_info,
214                                             mock_file_empty, mock_prep_list,
215                                             mock_path_exists):
216         self.rally_base._run_task('test_name')
217         mock_logger_info.assert_any_call('No tests for scenario \"%s\"',
218                                          'test_name')
219         mock_file_empty.assert_called()
220         mock_prep_list.assert_called()
221         mock_path_exists.assert_called()
222
223     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
224                 '_prepare_test_list', return_value='test_file_name')
225     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
226                 'file_is_empty', return_value=False)
227     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
228                 '_build_task_args', return_value={})
229     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
230                 '_get_output')
231     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
232                 'get_task_id', return_value=None)
233     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
234                 'get_cmd_output', return_value='')
235     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
236                 return_value=True)
237     @mock.patch('functest.opnfv_tests.openstack.rally.rally.subprocess.Popen')
238     @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.error')
239     def test_run_task_taskid_missing(self, mock_logger_error, *args):
240         self.rally_base._run_task('test_name')
241         text = 'Failed to retrieve task_id, validating task...'
242         mock_logger_error.assert_any_call(text)
243
244     @mock.patch('__builtin__.open', mock.mock_open())
245     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
246                 '_prepare_test_list', return_value='test_file_name')
247     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
248                 'file_is_empty', return_value=False)
249     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
250                 '_build_task_args', return_value={})
251     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
252                 '_get_output')
253     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
254                 'get_task_id', return_value='1')
255     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
256                 'get_cmd_output', return_value='')
257     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
258                 'task_succeed', return_value=True)
259     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.path.exists',
260                 return_value=True)
261     @mock.patch('functest.opnfv_tests.openstack.rally.rally.subprocess.Popen')
262     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.makedirs')
263     @mock.patch('functest.opnfv_tests.openstack.rally.rally.os.popen')
264     @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.info')
265     @mock.patch('functest.opnfv_tests.openstack.rally.rally.LOGGER.error')
266     def test_run_task_default(self, mock_logger_error, mock_logger_info,
267                               mock_popen, *args):
268         attrs = {'read.return_value': 'json_result'}
269         mock_popen.return_value.configure_mock(**attrs)
270         self.rally_base._run_task('test_name')
271         text = 'Test scenario: "test_name" OK.\n'
272         mock_logger_info.assert_any_call(text)
273         mock_logger_error.assert_not_called()
274
275     def test_prepare_env_testname_invalid(self):
276         self.rally_base.TESTS = ['test1', 'test2']
277         self.rally_base.test_name = 'test'
278         with self.assertRaises(Exception):
279             self.rally_base._prepare_env()
280
281     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
282                 'get_active_compute_cnt')
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(
288             self, mock_get_img, mock_get_net, mock_get_comp_cnt):
289         self.rally_base.TESTS = ['test1', 'test2']
290         self.rally_base.test_name = 'test1'
291         with self.assertRaises(Exception):
292             self.rally_base._prepare_env()
293         mock_get_img.assert_called()
294         mock_get_net.assert_called()
295         mock_get_comp_cnt.assert_called()
296
297     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
298                 'get_active_compute_cnt')
299     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
300                 'get_ext_net_name', return_value='test_net_name')
301     @mock.patch('snaps.openstack.utils.deploy_utils.create_image')
302     @mock.patch('snaps.openstack.utils.deploy_utils.create_network',
303                 return_value=None)
304     def test_prepare_env_network_creation_failed(
305             self, mock_create_net, mock_get_img, mock_get_net,
306             mock_get_comp_cnt):
307         self.rally_base.TESTS = ['test1', 'test2']
308         self.rally_base.test_name = 'test1'
309         with self.assertRaises(Exception):
310             self.rally_base._prepare_env()
311         mock_create_net.assert_called()
312         mock_get_img.assert_called()
313         mock_get_net.assert_called()
314         mock_get_comp_cnt.assert_called()
315
316     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
317                 'get_active_compute_cnt')
318     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
319                 'get_ext_net_name', return_value='test_net_name')
320     @mock.patch('snaps.openstack.utils.deploy_utils.create_image')
321     @mock.patch('snaps.openstack.utils.deploy_utils.create_network')
322     @mock.patch('snaps.openstack.utils.deploy_utils.create_router',
323                 return_value=None)
324     def test_prepare_env_router_creation_failed(
325             self, mock_create_router, mock_create_net, mock_get_img,
326             mock_get_net, mock_get_comp_cnt):
327         self.rally_base.TESTS = ['test1', 'test2']
328         self.rally_base.test_name = 'test1'
329         with self.assertRaises(Exception):
330             self.rally_base._prepare_env()
331         mock_create_net.assert_called()
332         mock_get_img.assert_called()
333         mock_get_net.assert_called()
334         mock_create_router.assert_called()
335         mock_get_comp_cnt.assert_called()
336
337     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
338                 'get_active_compute_cnt')
339     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
340                 'get_ext_net_name', return_value='test_net_name')
341     @mock.patch('snaps.openstack.utils.deploy_utils.create_image')
342     @mock.patch('snaps.openstack.utils.deploy_utils.create_network')
343     @mock.patch('snaps.openstack.utils.deploy_utils.create_router')
344     @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create',
345                 return_value=None)
346     def test_prepare_env_flavor_creation_failed(
347             self, mock_create_flavor, mock_create_router, mock_create_net,
348             mock_get_img, mock_get_net, mock_get_comp_cnt):
349         self.rally_base.TESTS = ['test1', 'test2']
350         self.rally_base.test_name = 'test1'
351         with self.assertRaises(Exception):
352             self.rally_base._prepare_env()
353         mock_create_net.assert_called()
354         mock_get_img.assert_called()
355         mock_get_net.assert_called()
356         mock_create_router.assert_called()
357         mock_get_comp_cnt.assert_called()
358         mock_create_flavor.assert_called_once()
359
360     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
361                 'get_active_compute_cnt')
362     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
363                 'get_ext_net_name', return_value='test_net_name')
364     @mock.patch('snaps.openstack.utils.deploy_utils.create_image')
365     @mock.patch('snaps.openstack.utils.deploy_utils.create_network')
366     @mock.patch('snaps.openstack.utils.deploy_utils.create_router')
367     @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create',
368                 side_effect=[mock.Mock, None])
369     def test_prepare_env_flavor_alt_creation_failed(
370             self, mock_create_flavor, mock_create_router, mock_create_net,
371             mock_get_img, mock_get_net, mock_get_comp_cnt):
372         self.rally_base.TESTS = ['test1', 'test2']
373         self.rally_base.test_name = 'test1'
374         with self.assertRaises(Exception):
375             self.rally_base._prepare_env()
376         mock_create_net.assert_called()
377         mock_get_img.assert_called()
378         mock_get_net.assert_called()
379         mock_create_router.assert_called()
380         mock_get_comp_cnt.assert_called()
381         self.assertEqual(mock_create_flavor.call_count, 2)
382
383     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
384                 '_run_task')
385     def test_run_tests_all(self, mock_run_task):
386         self.rally_base.TESTS = ['test1', 'test2']
387         self.rally_base.test_name = 'all'
388         self.rally_base._run_tests()
389         mock_run_task.assert_any_call('test1')
390         mock_run_task.assert_any_call('test2')
391
392     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
393                 '_run_task')
394     def test_run_tests_default(self, mock_run_task):
395         self.rally_base.TESTS = ['test1', 'test2']
396         self.rally_base.test_name = 'test1'
397         self.rally_base._run_tests()
398         mock_run_task.assert_any_call('test1')
399
400     def test_clean_up_default(self):
401         creator1 = mock.Mock()
402         creator2 = mock.Mock()
403         self.rally_base.creators = [creator1, creator2]
404         self.rally_base._clean_up()
405         self.assertTrue(creator1.clean.called)
406         self.assertTrue(creator2.clean.called)
407
408     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
409                 'create_rally_deployment')
410     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
411                 '_prepare_env')
412     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
413                 '_run_tests')
414     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
415                 '_generate_report')
416     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
417                 '_clean_up')
418     def test_run_default(self, *args):
419         self.assertEqual(self.rally_base.run(), testcase.TestCase.EX_OK)
420         map(lambda m: m.assert_called(), args)
421
422     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
423                 'create_rally_deployment', side_effect=Exception)
424     def test_run_exception_create_rally_dep(self, mock_create_rally_dep):
425         self.assertEqual(self.rally_base.run(), testcase.TestCase.EX_RUN_ERROR)
426         mock_create_rally_dep.assert_called()
427
428     @mock.patch('functest.opnfv_tests.openstack.rally.rally.RallyBase.'
429                 '_prepare_env', side_effect=Exception)
430     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
431                 'create_rally_deployment', return_value=mock.Mock())
432     def test_run_exception_prepare_env(self, mock_create_rally_dep,
433                                        mock_prep_env):
434         self.assertEqual(self.rally_base.run(), testcase.TestCase.EX_RUN_ERROR)
435         mock_prep_env.assert_called()
436
437
438 if __name__ == "__main__":
439     logging.disable(logging.CRITICAL)
440     unittest.main(verbosity=2)