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 = "(cd {0}; stestr list '{1}' >{2} 2>/dev/null)".format(
87 verifier_repo_dir, testr_mode, self.tempestcommon.list)
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 mock_stdout = mock.Mock()
273 attrs = {'stdout.readline.return_value': b'test_deploy_id'}
274 mock_stdout.configure_mock(**attrs)
275 mock_popen.return_value = mock_stdout
277 self.assertEqual(self.tempestcommon.get_verifier_id(),
280 def test_get_depl_id_default(self):
281 setattr(config.CONF, 'tempest_verifier_name', 'test_deploy_name')
282 with mock.patch('functest.opnfv_tests.openstack.tempest.'
283 'tempest.subprocess.Popen') as mock_popen:
284 mock_stdout = mock.Mock()
285 attrs = {'stdout.readline.return_value': b'test_deploy_id'}
286 mock_stdout.configure_mock(**attrs)
287 mock_popen.return_value = mock_stdout
289 self.assertEqual(rally.RallyBase.get_verifier_deployment_id(),
292 def test_get_verif_repo_dir_default(self):
293 with mock.patch('functest.opnfv_tests.openstack.tempest.'
294 'tempest.os.path.join',
295 return_value='test_verifier_repo_dir'):
296 self.assertEqual(self.tempestcommon.get_verifier_repo_dir(''),
297 'test_verifier_repo_dir')
299 def test_get_depl_dir_default(self):
300 with mock.patch('functest.opnfv_tests.openstack.tempest.'
301 'tempest.os.path.join',
302 return_value='test_verifier_repo_dir'):
304 self.tempestcommon.get_verifier_deployment_dir('', ''),
305 'test_verifier_repo_dir')
307 def _test_missing_param(self, params, image_id, flavor_id, alt=False):
308 with mock.patch('six.moves.configparser.RawConfigParser.'
310 mock.patch('six.moves.configparser.RawConfigParser.'
312 mock.patch('six.moves.configparser.RawConfigParser.'
313 'write') as mwrite, \
314 mock.patch('six.moves.builtins.open', mock.mock_open()), \
315 mock.patch('functest.utils.functest_utils.yaml.safe_load',
316 return_value={'validation': {'ssh_timeout': 300}}):
317 os.environ['OS_INTERFACE'] = ''
319 self.tempestcommon.configure_tempest_update_params(
320 'test_conf_file', image_id=image_id,
322 mset.assert_any_call(params[0], params[1], params[2])
324 self.tempestcommon.configure_tempest_update_params(
325 'test_conf_file', image_alt_id=image_id,
326 flavor_alt_id=flavor_id)
327 mset.assert_any_call(params[0], params[1], params[2])
328 self.assertTrue(mread.called)
329 self.assertTrue(mwrite.called)
331 def test_upd_missing_image_id(self):
332 self._test_missing_param(('compute', 'image_ref', 'test_image_id'),
333 'test_image_id', None)
335 def test_upd_missing_image_id_alt(self):
336 self._test_missing_param(
337 ('compute', 'image_ref_alt', 'test_image_id_alt'),
338 'test_image_id_alt', None, alt=True)
340 def test_upd_missing_flavor_id(self):
341 self._test_missing_param(('compute', 'flavor_ref', 'test_flavor_id'),
342 None, 'test_flavor_id')
344 def test_upd_missing_flavor_id_alt(self):
345 self._test_missing_param(
346 ('compute', 'flavor_ref_alt', 'test_flavor_id_alt'),
347 None, 'test_flavor_id_alt', alt=True)
349 def test_verif_missing_conf_file(self):
350 with mock.patch('functest.opnfv_tests.openstack.tempest.'
351 'tempest.os.path.isfile',
352 return_value=False), \
353 mock.patch('subprocess.check_output') as mexe, \
354 self.assertRaises(Exception) as context:
355 self.tempestcommon.configure_verifier('test_dep_dir')
356 mexe.assert_called_once_with("rally verify configure-verifier")
357 msg = ("Tempest configuration file 'test_dep_dir/tempest.conf'"
359 self.assertTrue(msg in context.exception)
361 def test_configure_verifier_default(self):
362 with mock.patch('functest.opnfv_tests.openstack.tempest.'
363 'tempest.os.path.isfile',
364 return_value=True), \
365 mock.patch('subprocess.check_output') as mexe:
367 self.tempestcommon.configure_verifier('test_dep_dir'),
368 'test_dep_dir/tempest.conf')
369 mexe.assert_called_once_with(
370 ['rally', 'verify', 'configure-verifier', '--reconfigure',
371 '--id', str(getattr(config.CONF, 'tempest_verifier_name'))])
373 def test_is_successful_false(self):
374 with mock.patch('six.moves.builtins.super') as mock_super:
375 self.tempestcommon.deny_skipping = True
376 self.tempestcommon.details = {"skipped_number": 2}
377 self.assertEqual(self.tempestcommon.is_successful(),
378 testcase.TestCase.EX_TESTCASE_FAILED)
379 mock_super(tempest.TempestCommon,
380 self).is_successful.assert_not_called()
382 def test_is_successful_true(self):
383 with mock.patch('six.moves.builtins.super') as mock_super:
384 self.tempestcommon.deny_skipping = False
385 self.tempestcommon.details = {"skipped_number": 2}
386 mock_super(tempest.TempestCommon,
387 self).is_successful.return_value = 567
388 self.assertEqual(self.tempestcommon.is_successful(), 567)
389 mock_super(tempest.TempestCommon,
390 self).is_successful.assert_called()
393 if __name__ == "__main__":
394 logging.disable(logging.CRITICAL)
395 unittest.main(verbosity=2)