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