Enable tempest multinode tests
[functest.git] / functest / tests / unit / openstack / tempest / test_tempest.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 logging
9 import unittest
10
11 import mock
12
13 from functest.core import testcase
14 from functest.opnfv_tests.openstack.tempest import tempest
15 from functest.opnfv_tests.openstack.tempest import conf_utils
16 from functest.utils.constants import CONST
17
18 from snaps.openstack.os_credentials import OSCreds
19
20
21 class OSTempestTesting(unittest.TestCase):
22
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
28         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
29                         'conf_utils.get_verifier_id',
30                         return_value='test_deploy_id'), \
31             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
32                        'conf_utils.get_verifier_deployment_id',
33                        return_value='test_deploy_id'), \
34             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
35                        'conf_utils.get_verifier_repo_dir',
36                        return_value='test_verifier_repo_dir'), \
37             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
38                        'conf_utils.get_verifier_deployment_dir',
39                        return_value='test_verifier_deploy_dir'), \
40             mock.patch('snaps.openstack.tests.openstack_tests.get_credentials',
41                        return_value=os_creds):
42             self.tempestcommon = tempest.TempestCommon()
43             self.tempestsmoke_serial = tempest.TempestSmokeSerial()
44             self.tempestsmoke_parallel = tempest.TempestSmokeParallel()
45             self.tempestfull_parallel = tempest.TempestFullParallel()
46             self.tempestcustom = tempest.TempestCustom()
47             self.tempestdefcore = tempest.TempestDefcore()
48
49     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.debug')
50     def test_generate_test_list_defcore_mode(self, mock_logger_debug):
51         self.tempestcommon.MODE = 'defcore'
52         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
53                         'shutil.copyfile') as m:
54             self.tempestcommon.generate_test_list('test_verifier_repo_dir')
55             self.assertTrue(m.called)
56
57     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.error')
58     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.debug')
59     def test_generate_test_list_custom_mode_missing_file(self,
60                                                          mock_logger_debug,
61                                                          mock_logger_error):
62         self.tempestcommon.MODE = 'custom'
63         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
64                         'os.path.isfile', return_value=False), \
65                 self.assertRaises(Exception) as context:
66             msg = "Tempest test list file %s NOT found."
67             self.tempestcommon.generate_test_list('test_verifier_repo_dir')
68             self.assertTrue((msg % conf_utils.TEMPEST_CUSTOM) in context)
69
70     def test_generate_test_list_custom_mode_default(self):
71         self.tempestcommon.MODE = 'custom'
72         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
73                         'shutil.copyfile') as m, \
74             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
75                        'os.path.isfile', return_value=True):
76             self.tempestcommon.generate_test_list('test_verifier_repo_dir')
77             self.assertTrue(m.called)
78
79     def _test_generate_test_list_mode_default(self, mode):
80         self.tempestcommon.MODE = mode
81         if self.tempestcommon.MODE == 'smoke':
82             testr_mode = "smoke"
83         elif self.tempestcommon.MODE == 'full':
84             testr_mode = ""
85         else:
86             testr_mode = 'tempest.api.' + self.tempestcommon.MODE
87         conf_utils.TEMPEST_RAW_LIST = 'raw_list'
88         verifier_repo_dir = 'test_verifier_repo_dir'
89         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
90                         'ft_utils.execute_command') as m:
91             cmd = ("cd {0};"
92                    "testr list-tests {1} > {2};"
93                    "cd -;".format(verifier_repo_dir,
94                                   testr_mode,
95                                   conf_utils.TEMPEST_RAW_LIST))
96             self.tempestcommon.generate_test_list('test_verifier_repo_dir')
97             m.assert_any_call(cmd)
98
99     def test_generate_test_list_smoke_mode(self):
100         self._test_generate_test_list_mode_default('smoke')
101
102     def test_generate_test_list_full_mode(self):
103         self._test_generate_test_list_mode_default('full')
104
105     def test_parse_verifier_result_missing_verification_uuid(self):
106         self.tempestcommon.VERIFICATION_ID = None
107         with self.assertRaises(Exception):
108             self.tempestcommon.parse_verifier_result()
109
110     def test_apply_tempest_blacklist_no_blacklist(self):
111         with mock.patch('__builtin__.open', mock.mock_open()) as m, \
112             mock.patch.object(self.tempestcommon, 'read_file',
113                               return_value=['test1', 'test2']):
114             conf_utils.TEMPEST_BLACKLIST = Exception
115             CONST.__setattr__('INSTALLER_TYPE', 'installer_type')
116             CONST.__setattr__('DEPLOY_SCENARIO', 'deploy_scenario')
117             self.tempestcommon.apply_tempest_blacklist()
118             obj = m()
119             obj.write.assert_any_call('test1\n')
120             obj.write.assert_any_call('test2\n')
121
122     def test_apply_tempest_blacklist_default(self):
123         item_dict = {'scenarios': ['deploy_scenario'],
124                      'installers': ['installer_type'],
125                      'tests': ['test2']}
126         with mock.patch('__builtin__.open', mock.mock_open()) as m, \
127             mock.patch.object(self.tempestcommon, 'read_file',
128                               return_value=['test1', 'test2']), \
129             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
130                        'yaml.safe_load', return_value=item_dict):
131             CONST.__setattr__('INSTALLER_TYPE', 'installer_type')
132             CONST.__setattr__('DEPLOY_SCENARIO', 'deploy_scenario')
133             self.tempestcommon.apply_tempest_blacklist()
134             obj = m()
135             obj.write.assert_any_call('test1\n')
136             self.assertFalse(obj.write.assert_any_call('test2\n'))
137
138     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.info')
139     def test_run_verifier_tests_default(self, mock_logger_info):
140         with mock.patch('__builtin__.open', mock.mock_open()), \
141             mock.patch('__builtin__.iter', return_value=['\} tempest\.']), \
142             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
143                        'subprocess.Popen'):
144             conf_utils.TEMPEST_LIST = 'test_tempest_list'
145             cmd_line = ("rally verify start  --load-list "
146                         "test_tempest_list --detailed")
147             self.tempestcommon.run_verifier_tests()
148             mock_logger_info. \
149                 assert_any_call("Starting Tempest test suite: '%s'."
150                                 % cmd_line)
151
152     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
153                 'os.path.exists', return_value=False)
154     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs',
155                 side_effect=Exception)
156     def test_run_makedirs_ko(self, *args):
157         self.assertEqual(self.tempestcommon.run(),
158                          testcase.TestCase.EX_RUN_ERROR)
159
160     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
161                 'os.path.exists', return_value=False)
162     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
163     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
164                 'TempestResourcesManager.create', side_effect=Exception)
165     def test_run_tempest_create_resources_ko(self, *args):
166         self.assertEqual(self.tempestcommon.run(),
167                          testcase.TestCase.EX_RUN_ERROR)
168
169     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
170                 'os.path.exists', return_value=False)
171     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
172     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
173                 'TempestResourcesManager.create', return_value={})
174     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
175                 'get_active_compute_cnt', side_effect=Exception)
176     def test_run_get_active_compute_cnt_ko(self, *args):
177         self.assertEqual(self.tempestcommon.run(),
178                          testcase.TestCase.EX_RUN_ERROR)
179
180     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
181                 'os.path.exists', return_value=False)
182     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
183     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
184                 'TempestResourcesManager.create', return_value={})
185     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
186                 'get_active_compute_cnt', return_value=2)
187     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
188                 'conf_utils.configure_tempest', side_effect=Exception)
189     def test_run_configure_tempest_ko(self, *args):
190         self.assertEqual(self.tempestcommon.run(),
191                          testcase.TestCase.EX_RUN_ERROR)
192
193     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
194                 'os.path.exists', return_value=False)
195     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
196     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
197                 'TempestResourcesManager.create', return_value={})
198     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
199                 'get_active_compute_cnt', return_value=2)
200     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
201                 'conf_utils.configure_tempest')
202     def _test_run(self, status, *args):
203         self.assertEqual(self.tempestcommon.run(), status)
204
205     def test_run_missing_generate_test_list(self):
206         with mock.patch.object(self.tempestcommon, 'generate_test_list',
207                                side_effect=Exception):
208             self._test_run(testcase.TestCase.EX_RUN_ERROR)
209
210     def test_run_apply_tempest_blacklist_ko(self):
211         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
212                     mock.patch.object(self.tempestcommon,
213                                       'apply_tempest_blacklist',
214                                       side_effect=Exception()):
215             self._test_run(testcase.TestCase.EX_RUN_ERROR)
216
217     def test_run_verifier_tests_ko(self, *args):
218         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
219                 mock.patch.object(self.tempestcommon,
220                                   'apply_tempest_blacklist'), \
221                 mock.patch.object(self.tempestcommon, 'run_verifier_tests',
222                                   side_effect=Exception()), \
223                 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
224                                   side_effect=Exception):
225             self._test_run(testcase.TestCase.EX_RUN_ERROR)
226
227     def test_run_parse_verifier_result_ko(self, *args):
228         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
229                 mock.patch.object(self.tempestcommon,
230                                   'apply_tempest_blacklist'), \
231                 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
232                 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
233                                   side_effect=Exception):
234             self._test_run(testcase.TestCase.EX_RUN_ERROR)
235
236     def test_run(self, *args):
237         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
238                 mock.patch.object(self.tempestcommon,
239                                   'apply_tempest_blacklist'), \
240                 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
241                 mock.patch.object(self.tempestcommon, 'parse_verifier_result'):
242             self._test_run(testcase.TestCase.EX_OK)
243
244
245 if __name__ == "__main__":
246     logging.disable(logging.CRITICAL)
247     unittest.main(verbosity=2)