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.tempest import tempest
18 from functest.opnfv_tests.openstack.tempest import conf_utils
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.tempest.tempest.'
30 'conf_utils.create_rally_deployment'), \
31 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
32 'conf_utils.create_verifier'), \
33 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
34 'conf_utils.get_verifier_id',
35 return_value='test_deploy_id'), \
36 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
37 'conf_utils.get_verifier_deployment_id',
38 return_value='test_deploy_id'), \
39 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
40 'conf_utils.get_verifier_repo_dir',
41 return_value='test_verifier_repo_dir'), \
42 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
43 'conf_utils.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 % conf_utils.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 cmd = "(cd {0}; stestr list '{1}' >{2} 2>/dev/null)".format(
86 verifier_repo_dir, testr_mode, self.tempestcommon.list)
87 self.tempestcommon.generate_test_list(mode=testr_mode)
88 args[0].assert_called_once_with(cmd, shell=True)
89 args[2].assert_called_once_with('/etc/tempest.conf')
91 def test_gen_tl_smoke_mode(self):
92 self._test_gen_tl_mode_default('smoke')
94 def test_gen_tl_full_mode(self):
95 self._test_gen_tl_mode_default('full')
97 def test_verif_res_missing_verif_id(self):
98 self.tempestcommon.verification_id = None
99 with self.assertRaises(Exception):
100 self.tempestcommon.parse_verifier_result()
102 def test_backup_config_default(self):
103 with mock.patch('os.path.exists', return_value=False), \
104 mock.patch('os.makedirs') as mock_makedirs, \
105 mock.patch('shutil.copyfile') as mock_copyfile:
106 self.tempestcommon.backup_tempest_config(
107 'test_conf_file', res_dir='test_dir')
108 self.assertTrue(mock_makedirs.called)
109 self.assertTrue(mock_copyfile.called)
111 with mock.patch('os.path.exists', return_value=True), \
112 mock.patch('shutil.copyfile') as mock_copyfile:
113 self.tempestcommon.backup_tempest_config(
114 'test_conf_file', res_dir='test_dir')
115 self.assertTrue(mock_copyfile.called)
117 @mock.patch("os.rename")
118 @mock.patch("os.remove")
119 @mock.patch("os.path.exists", return_value=True)
120 def test_apply_missing_blacklist(self, *args):
121 with mock.patch('six.moves.builtins.open',
122 mock.mock_open()) as mock_open, \
123 mock.patch.object(self.tempestcommon, 'read_file',
124 return_value=['test1', 'test2']):
125 conf_utils.TEMPEST_BLACKLIST = Exception
126 os.environ['DEPLOY_SCENARIO'] = 'deploy_scenario'
127 self.tempestcommon.apply_tempest_blacklist()
129 obj.write.assert_any_call('test1\n')
130 obj.write.assert_any_call('test2\n')
131 args[0].assert_called_once_with(self.tempestcommon.raw_list)
132 args[1].assert_called_once_with(self.tempestcommon.raw_list)
133 args[2].assert_called_once_with(
134 self.tempestcommon.list, self.tempestcommon.raw_list)
136 @mock.patch("os.rename")
137 @mock.patch("os.remove")
138 @mock.patch("os.path.exists", return_value=True)
139 def test_apply_blacklist_default(self, *args):
140 item_dict = {'scenarios': ['deploy_scenario'],
142 with mock.patch('six.moves.builtins.open',
143 mock.mock_open()) as mock_open, \
144 mock.patch.object(self.tempestcommon, 'read_file',
145 return_value=['test1', 'test2']), \
146 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
147 'yaml.safe_load', return_value=item_dict):
148 os.environ['DEPLOY_SCENARIO'] = 'deploy_scenario'
149 self.tempestcommon.apply_tempest_blacklist()
151 obj.write.assert_any_call('test1\n')
152 self.assertFalse(obj.write.assert_any_call('test2\n'))
153 args[0].assert_called_once_with(self.tempestcommon.raw_list)
154 args[1].assert_called_once_with(self.tempestcommon.raw_list)
155 args[2].assert_called_once_with(
156 self.tempestcommon.list, self.tempestcommon.raw_list)
158 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.info')
159 def test_run_verifier_tests_default(self, mock_logger_info):
160 with mock.patch('six.moves.builtins.open', mock.mock_open()), \
161 mock.patch('six.moves.builtins.iter',
162 return_value=[r'\} tempest\.']), \
163 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
165 conf_utils.TEMPEST_LIST = 'test_tempest_list'
166 cmd = ["rally", "verify", "start", "--load-list",
167 conf_utils.TEMPEST_LIST]
168 with self.assertRaises(Exception):
169 self.tempestcommon.run_verifier_tests()
171 assert_any_call("Starting Tempest test suite: '%s'.", cmd)
173 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
175 def test_generate_report(self, mock_popen):
176 self.tempestcommon.verification_id = "1234"
177 html_file = os.path.join(
179 getattr(config.CONF, 'dir_results'),
180 self.tempestcommon.case_name),
181 "tempest-report.html")
182 cmd = ["rally", "verify", "report", "--type", "html", "--uuid",
183 "1234", "--to", html_file]
184 self.tempestcommon.generate_report()
185 mock_popen.assert_called_once_with(cmd, stdout=mock.ANY,
188 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
189 'os.path.exists', return_value=False)
190 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs',
191 side_effect=Exception)
192 def test_run_makedirs_ko(self, *args):
193 # pylint: disable=unused-argument
194 self.assertEqual(self.tempestcommon.run(),
195 testcase.TestCase.EX_RUN_ERROR)
197 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
198 'os.path.exists', return_value=False)
199 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
200 def test_run_create_resources_ko(self, *args):
201 # pylint: disable=unused-argument
202 self.assertEqual(self.tempestcommon.run(),
203 testcase.TestCase.EX_RUN_ERROR)
205 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
206 'os.path.exists', return_value=False)
207 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
208 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
209 'TempestCommon.configure', side_effect=Exception)
210 def test_run_configure_tempest_ko(self, *args):
211 # pylint: disable=unused-argument
212 self.assertEqual(self.tempestcommon.run(),
213 testcase.TestCase.EX_RUN_ERROR)
215 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
216 'os.path.exists', return_value=False)
217 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
218 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
219 'TempestCommon.configure')
220 def _test_run(self, status, *args):
221 # pylint: disable=unused-argument
222 self.assertEqual(self.tempestcommon.run(), status)
224 def test_run_missing_gen_test_list(self):
225 with mock.patch.object(self.tempestcommon, 'generate_test_list',
226 side_effect=Exception):
227 self._test_run(testcase.TestCase.EX_RUN_ERROR)
229 def test_run_apply_blacklist_ko(self):
230 with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
232 self.tempestcommon, 'apply_tempest_blacklist',
233 side_effect=Exception()):
234 self._test_run(testcase.TestCase.EX_RUN_ERROR)
236 def test_run_verifier_tests_ko(self):
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 side_effect=Exception()), \
242 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
243 side_effect=Exception):
244 self._test_run(testcase.TestCase.EX_RUN_ERROR)
246 def test_run_verif_result_ko(self):
247 with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
248 mock.patch.object(self.tempestcommon,
249 'apply_tempest_blacklist'), \
250 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
251 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
252 side_effect=Exception):
253 self._test_run(testcase.TestCase.EX_RUN_ERROR)
255 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.TempestCommon.'
256 'run', return_value=testcase.TestCase.EX_OK)
257 def test_run(self, *args):
258 with mock.patch.object(self.tempestcommon, 'update_rally_regex'), \
259 mock.patch.object(self.tempestcommon, 'generate_test_list'), \
260 mock.patch.object(self.tempestcommon,
261 'apply_tempest_blacklist'), \
262 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
263 mock.patch.object(self.tempestcommon,
264 'parse_verifier_result'), \
265 mock.patch.object(self.tempestcommon, 'generate_report'):
266 self._test_run(testcase.TestCase.EX_OK)
267 args[0].assert_called_once_with()
270 if __name__ == "__main__":
271 logging.disable(logging.CRITICAL)
272 unittest.main(verbosity=2)