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=missing-docstring
15 from xtesting.core import testcase
17 from functest.opnfv_tests.openstack.rally import rally
18 from functest.opnfv_tests.openstack.tempest import tempest
19 from functest.utils import config
22 class OSTempestTesting(unittest.TestCase):
23 # pylint: disable=too-many-public-methods
26 with mock.patch('os_client_config.get_config'), \
27 mock.patch('shade.OpenStackCloud'), \
28 mock.patch('functest.core.tenantnetwork.NewProject'), \
29 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
30 'RallyBase.create_rally_deployment'), \
31 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
32 'TempestCommon.create_verifier'), \
33 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
34 'TempestCommon.get_verifier_id',
35 return_value='test_deploy_id'), \
36 mock.patch('functest.opnfv_tests.openstack.rally.rally.'
37 'RallyBase.get_verifier_deployment_id',
38 return_value='test_deploy_id'), \
39 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
40 'TempestCommon.get_verifier_repo_dir',
41 return_value='test_verifier_repo_dir'), \
42 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
43 'TempestCommon.get_verifier_deployment_dir',
44 return_value='test_verifier_deploy_dir'), \
45 mock.patch('os_client_config.make_shade'):
46 self.tempestcommon = tempest.TempestCommon()
48 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.error')
49 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.debug')
50 def test_gen_tl_cm_missing_file(self, mock_logger_debug,
52 # pylint: disable=unused-argument
53 self.tempestcommon.mode = 'custom'
54 with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
55 'os.path.isfile', return_value=False), \
56 self.assertRaises(Exception) as context:
57 msg = "Tempest test list file %s NOT found."
58 self.tempestcommon.generate_test_list()
60 (msg % self.tempestcommon.tempest_custom) in context.exception)
62 @mock.patch('subprocess.check_output')
63 @mock.patch('os.remove')
64 def test_gen_tl_cm_default(self, *args):
65 self.tempestcommon.mode = 'custom'
66 with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
67 'shutil.copyfile') as mock_copyfile, \
68 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
69 'os.path.isfile', return_value=True):
70 self.tempestcommon.generate_test_list()
71 self.assertTrue(mock_copyfile.called)
72 args[0].assert_called_once_with('/etc/tempest.conf')
74 @mock.patch('os.remove')
75 @mock.patch('shutil.copyfile')
76 @mock.patch('subprocess.check_output')
77 def _test_gen_tl_mode_default(self, mode, *args):
79 testr_mode = r'^tempest\.(api|scenario).*\[.*\bsmoke\b.*\]$'
81 testr_mode = r'^tempest\.'
83 testr_mode = self.tempestcommon.mode
84 verifier_repo_dir = 'test_verifier_repo_dir'
85 self.tempestcommon.verifier_repo_dir = verifier_repo_dir
86 cmd = (f"(cd {verifier_repo_dir}; stestr list '{testr_mode}' > "
87 f"{self.tempestcommon.list} 2>/dev/null)")
88 self.tempestcommon.generate_test_list(mode=testr_mode)
89 args[0].assert_called_once_with(cmd, shell=True)
90 args[2].assert_called_once_with('/etc/tempest.conf')
92 def test_gen_tl_smoke_mode(self):
93 self._test_gen_tl_mode_default('smoke')
95 def test_gen_tl_full_mode(self):
96 self._test_gen_tl_mode_default('full')
98 def test_verif_res_missing_verif_id(self):
99 self.tempestcommon.verification_id = None
100 with self.assertRaises(Exception):
101 self.tempestcommon.parse_verifier_result()
103 def test_backup_config_default(self):
104 with mock.patch('os.path.exists', return_value=False), \
105 mock.patch('os.makedirs') as mock_makedirs, \
106 mock.patch('shutil.copyfile') as mock_copyfile:
107 self.tempestcommon.backup_tempest_config(
108 'test_conf_file', res_dir='test_dir')
109 self.assertTrue(mock_makedirs.called)
110 self.assertTrue(mock_copyfile.called)
112 with mock.patch('os.path.exists', return_value=True), \
113 mock.patch('shutil.copyfile') as mock_copyfile:
114 self.tempestcommon.backup_tempest_config(
115 'test_conf_file', res_dir='test_dir')
116 self.assertTrue(mock_copyfile.called)
118 @mock.patch("os.rename")
119 @mock.patch("os.remove")
120 @mock.patch("os.path.exists", return_value=True)
121 def test_apply_missing_blacklist(self, *args):
122 with mock.patch('six.moves.builtins.open',
123 mock.mock_open()) as mock_open, \
124 mock.patch.object(self.tempestcommon, 'read_file',
125 return_value=['test1', 'test2']):
126 self.tempestcommon.tempest_blacklist = Exception
127 os.environ['DEPLOY_SCENARIO'] = 'deploy_scenario'
128 self.tempestcommon.apply_tempest_blacklist(
129 self.tempestcommon.tempest_blacklist)
131 obj.write.assert_any_call('test1\n')
132 obj.write.assert_any_call('test2\n')
133 args[0].assert_called_once_with(self.tempestcommon.raw_list)
134 args[1].assert_called_once_with(self.tempestcommon.raw_list)
135 args[2].assert_called_once_with(
136 self.tempestcommon.list, self.tempestcommon.raw_list)
138 @mock.patch("os.rename")
139 @mock.patch("os.remove")
140 @mock.patch("os.path.exists", return_value=True)
141 def test_apply_blacklist_default(self, *args):
142 item_dict = {'scenarios': ['deploy_scenario'],
144 with mock.patch('six.moves.builtins.open',
145 mock.mock_open()) as mock_open, \
146 mock.patch.object(self.tempestcommon, 'read_file',
147 return_value=['test1', 'test2']), \
148 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
149 'yaml.safe_load', return_value=item_dict):
150 os.environ['DEPLOY_SCENARIO'] = 'deploy_scenario'
151 self.tempestcommon.apply_tempest_blacklist(
152 self.tempestcommon.tempest_blacklist)
154 obj.write.assert_any_call('test1\n')
155 self.assertFalse(obj.write.assert_any_call('test2\n'))
156 args[0].assert_called_once_with(self.tempestcommon.raw_list)
157 args[1].assert_called_once_with(self.tempestcommon.raw_list)
158 args[2].assert_called_once_with(
159 self.tempestcommon.list, self.tempestcommon.raw_list)
161 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
163 @mock.patch('six.moves.builtins.open', mock.mock_open())
164 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.info')
165 def test_run_verifier_tests_default(self, *args):
166 self.tempestcommon.tempest_list = 'test_tempest_list'
167 cmd = ["rally", "verify", "start", "--load-list",
168 self.tempestcommon.tempest_list]
169 with self.assertRaises(Exception):
170 self.tempestcommon.run_verifier_tests()
171 args[0].assert_any_call("Starting Tempest test suite: '%s'.", cmd)
173 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
174 'os.path.exists', return_value=False)
175 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs',
176 side_effect=Exception)
177 def test_run_makedirs_ko(self, *args):
178 # pylint: disable=unused-argument
179 self.assertEqual(self.tempestcommon.run(),
180 testcase.TestCase.EX_RUN_ERROR)
182 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
183 'os.path.exists', return_value=False)
184 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
185 def test_run_create_resources_ko(self, *args):
186 # pylint: disable=unused-argument
187 self.assertEqual(self.tempestcommon.run(),
188 testcase.TestCase.EX_RUN_ERROR)
190 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
191 'os.path.exists', return_value=False)
192 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
193 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
194 'TempestCommon.configure', 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 'TempestCommon.configure')
205 def _test_run(self, status, *args):
206 # pylint: disable=unused-argument
207 self.assertEqual(self.tempestcommon.run(), status)
209 def test_run_missing_gen_test_list(self):
210 with mock.patch.object(self.tempestcommon, 'generate_test_list',
211 side_effect=Exception):
212 self._test_run(testcase.TestCase.EX_RUN_ERROR)
214 def test_run_apply_blacklist_ko(self):
215 with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
216 mock.patch.object(self.tempestcommon,
217 'apply_tempest_blacklist',
218 side_effect=Exception()):
219 self._test_run(testcase.TestCase.EX_RUN_ERROR)
221 def test_run_verifier_tests_ko(self):
222 with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
223 mock.patch.object(self.tempestcommon,
224 'apply_tempest_blacklist'), \
225 mock.patch.object(self.tempestcommon, 'run_verifier_tests',
226 side_effect=Exception()), \
227 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
228 side_effect=Exception):
229 self._test_run(testcase.TestCase.EX_RUN_ERROR)
231 def test_run_verif_result_ko(self):
232 with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
233 mock.patch.object(self.tempestcommon,
234 'apply_tempest_blacklist'), \
235 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
236 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
237 side_effect=Exception):
238 self._test_run(testcase.TestCase.EX_RUN_ERROR)
240 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.TempestCommon.'
241 'run', return_value=testcase.TestCase.EX_OK)
242 def test_run(self, *args):
243 with mock.patch.object(self.tempestcommon, 'update_rally_regex'), \
244 mock.patch.object(self.tempestcommon, 'generate_test_list'), \
245 mock.patch.object(self.tempestcommon,
246 'apply_tempest_blacklist'), \
247 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
248 mock.patch.object(self.tempestcommon,
249 'parse_verifier_result'):
250 self._test_run(testcase.TestCase.EX_OK)
251 args[0].assert_called_once_with()
253 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.debug')
254 def test_create_verifier(self, mock_logger_debug):
255 mock_popen = mock.Mock()
256 attrs = {'poll.return_value': None,
257 'stdout.readline.return_value': '0'}
258 mock_popen.configure_mock(**attrs)
260 setattr(config.CONF, 'tempest_verifier_name', 'test_verifier_name')
261 with mock.patch('subprocess.Popen', side_effect=Exception), \
262 self.assertRaises(Exception):
263 self.tempestcommon.create_verifier()
264 mock_logger_debug.assert_any_call("Tempest test_verifier_name"
267 def test_get_verifier_id_default(self):
268 setattr(config.CONF, 'tempest_verifier_name', 'test_verifier_name')
270 with mock.patch('functest.opnfv_tests.openstack.tempest.'
271 'tempest.subprocess.Popen') as mock_popen:
272 attrs = {'return_value.__enter__.return_value.'
273 'stdout.readline.return_value': b'test_deploy_id'}
274 mock_popen.configure_mock(**attrs)
276 self.assertEqual(self.tempestcommon.get_verifier_id(),
279 def test_get_depl_id_default(self):
280 setattr(config.CONF, 'tempest_verifier_name', 'test_deploy_name')
281 with mock.patch('functest.opnfv_tests.openstack.tempest.'
282 'tempest.subprocess.Popen') as mock_popen:
283 attrs = {'return_value.__enter__.return_value.'
284 'stdout.readline.return_value': b'test_deploy_id'}
285 mock_popen.configure_mock(**attrs)
287 self.assertEqual(rally.RallyBase.get_verifier_deployment_id(),
290 def test_get_verif_repo_dir_default(self):
291 with mock.patch('functest.opnfv_tests.openstack.tempest.'
292 'tempest.os.path.join',
293 return_value='test_verifier_repo_dir'):
294 self.assertEqual(self.tempestcommon.get_verifier_repo_dir(''),
295 'test_verifier_repo_dir')
297 def test_get_depl_dir_default(self):
298 with mock.patch('functest.opnfv_tests.openstack.tempest.'
299 'tempest.os.path.join',
300 return_value='test_verifier_repo_dir'):
302 self.tempestcommon.get_verifier_deployment_dir('', ''),
303 'test_verifier_repo_dir')
305 def _test_missing_param(self, params, image_id, flavor_id, alt=False):
306 with mock.patch('six.moves.configparser.RawConfigParser.'
308 mock.patch('six.moves.configparser.RawConfigParser.'
310 mock.patch('six.moves.configparser.RawConfigParser.'
311 'write') as mwrite, \
312 mock.patch('six.moves.builtins.open', mock.mock_open()), \
313 mock.patch('functest.utils.functest_utils.yaml.safe_load',
314 return_value={'validation': {'ssh_timeout': 300}}):
315 os.environ['OS_INTERFACE'] = ''
317 self.tempestcommon.configure_tempest_update_params(
318 'test_conf_file', image_id=image_id,
320 mset.assert_any_call(params[0], params[1], params[2])
322 self.tempestcommon.configure_tempest_update_params(
323 'test_conf_file', image_alt_id=image_id,
324 flavor_alt_id=flavor_id)
325 mset.assert_any_call(params[0], params[1], params[2])
326 self.assertTrue(mread.called)
327 self.assertTrue(mwrite.called)
329 def test_upd_missing_image_id(self):
330 self._test_missing_param(('compute', 'image_ref', 'test_image_id'),
331 'test_image_id', None)
333 def test_upd_missing_image_id_alt(self):
334 self._test_missing_param(
335 ('compute', 'image_ref_alt', 'test_image_id_alt'),
336 'test_image_id_alt', None, alt=True)
338 def test_upd_missing_flavor_id(self):
339 self._test_missing_param(('compute', 'flavor_ref', 'test_flavor_id'),
340 None, 'test_flavor_id')
342 def test_upd_missing_flavor_id_alt(self):
343 self._test_missing_param(
344 ('compute', 'flavor_ref_alt', 'test_flavor_id_alt'),
345 None, 'test_flavor_id_alt', alt=True)
347 def test_verif_missing_conf_file(self):
348 with mock.patch('functest.opnfv_tests.openstack.tempest.'
349 'tempest.os.path.isfile',
350 return_value=False), \
351 mock.patch('subprocess.check_output') as mexe, \
352 self.assertRaises(Exception) as context:
353 self.tempestcommon.configure_verifier('test_dep_dir')
354 mexe.assert_called_once_with("rally verify configure-verifier")
355 msg = ("Tempest configuration file 'test_dep_dir/tempest.conf'"
357 self.assertTrue(msg in context.exception)
359 def test_configure_verifier_default(self):
360 with mock.patch('functest.opnfv_tests.openstack.tempest.'
361 'tempest.os.path.isfile',
362 return_value=True), \
363 mock.patch('subprocess.check_output') as mexe:
365 self.tempestcommon.configure_verifier('test_dep_dir'),
366 'test_dep_dir/tempest.conf')
367 mexe.assert_called_once_with(
368 ['rally', 'verify', 'configure-verifier', '--reconfigure',
369 '--id', str(getattr(config.CONF, 'tempest_verifier_name'))])
371 def test_is_successful_false(self):
372 with mock.patch('six.moves.builtins.super') as mock_super:
373 self.tempestcommon.deny_skipping = True
374 self.tempestcommon.details = {"skipped_number": 2}
375 self.assertEqual(self.tempestcommon.is_successful(),
376 testcase.TestCase.EX_TESTCASE_FAILED)
377 mock_super(tempest.TempestCommon,
378 self).is_successful.assert_not_called()
380 def test_is_successful_true(self):
381 with mock.patch('six.moves.builtins.super') as mock_super:
382 self.tempestcommon.deny_skipping = False
383 self.tempestcommon.details = {"skipped_number": 2}
384 mock_super(tempest.TempestCommon,
385 self).is_successful.return_value = 567
386 self.assertEqual(self.tempestcommon.is_successful(), 567)
387 mock_super(tempest.TempestCommon,
388 self).is_successful.assert_called()
391 if __name__ == "__main__":
392 logging.disable(logging.CRITICAL)
393 unittest.main(verbosity=2)