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('functest.opnfv_tests.openstack.tempest.tempest.'
27 'conf_utils.get_verifier_id',
28 return_value='test_deploy_id'), \
29 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
30 'conf_utils.get_verifier_deployment_id',
31 return_value='test_deploy_id'), \
32 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
33 'conf_utils.get_verifier_repo_dir',
34 return_value='test_verifier_repo_dir'), \
35 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
36 'conf_utils.get_verifier_deployment_dir',
37 return_value='test_verifier_deploy_dir'), \
38 mock.patch('os_client_config.make_shade'):
39 self.tempestcommon = tempest.TempestCommon()
41 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.error')
42 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.debug')
43 def test_gen_tl_cm_missing_file(self, mock_logger_debug,
45 # pylint: disable=unused-argument
46 self.tempestcommon.mode = 'custom'
47 with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
48 'os.path.isfile', return_value=False), \
49 self.assertRaises(Exception) as context:
50 msg = "Tempest test list file %s NOT found."
51 self.tempestcommon.generate_test_list()
53 (msg % conf_utils.TEMPEST_CUSTOM) in context.exception)
55 @mock.patch('subprocess.check_output')
56 @mock.patch('os.remove')
57 def test_gen_tl_cm_default(self, *args):
58 self.tempestcommon.mode = 'custom'
59 with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
60 'shutil.copyfile') as mock_copyfile, \
61 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
62 'os.path.isfile', return_value=True):
63 self.tempestcommon.generate_test_list()
64 self.assertTrue(mock_copyfile.called)
65 args[0].assert_called_once_with('/etc/tempest.conf')
67 @mock.patch('os.remove')
68 @mock.patch('shutil.copyfile')
69 @mock.patch('subprocess.check_output')
70 def _test_gen_tl_mode_default(self, mode, *args):
72 testr_mode = r'^tempest\.(api|scenario).*\[.*\bsmoke\b.*\]$'
74 testr_mode = r'^tempest\.'
76 testr_mode = self.tempestcommon.mode
77 verifier_repo_dir = 'test_verifier_repo_dir'
78 cmd = "(cd {0}; stestr list '{1}' >{2} 2>/dev/null)".format(
79 verifier_repo_dir, testr_mode, self.tempestcommon.list)
80 self.tempestcommon.generate_test_list(mode=testr_mode)
81 args[0].assert_called_once_with(cmd, shell=True)
82 args[2].assert_called_once_with('/etc/tempest.conf')
84 def test_gen_tl_smoke_mode(self):
85 self._test_gen_tl_mode_default('smoke')
87 def test_gen_tl_full_mode(self):
88 self._test_gen_tl_mode_default('full')
90 def test_verif_res_missing_verif_id(self):
91 self.tempestcommon.verification_id = None
92 with self.assertRaises(Exception):
93 self.tempestcommon.parse_verifier_result()
95 def test_backup_config_default(self):
96 with mock.patch('os.path.exists', return_value=False), \
97 mock.patch('os.makedirs') as mock_makedirs, \
98 mock.patch('shutil.copyfile') as mock_copyfile:
99 self.tempestcommon.backup_tempest_config(
100 'test_conf_file', res_dir='test_dir')
101 self.assertTrue(mock_makedirs.called)
102 self.assertTrue(mock_copyfile.called)
104 with mock.patch('os.path.exists', return_value=True), \
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_copyfile.called)
110 @mock.patch("os.rename")
111 @mock.patch("os.remove")
112 @mock.patch("os.path.exists", return_value=True)
113 def test_apply_missing_blacklist(self, *args):
114 with mock.patch('six.moves.builtins.open',
115 mock.mock_open()) as mock_open, \
116 mock.patch.object(self.tempestcommon, 'read_file',
117 return_value=['test1', 'test2']):
118 conf_utils.TEMPEST_BLACKLIST = Exception
119 os.environ['DEPLOY_SCENARIO'] = 'deploy_scenario'
120 self.tempestcommon.apply_tempest_blacklist()
122 obj.write.assert_any_call('test1\n')
123 obj.write.assert_any_call('test2\n')
124 args[0].assert_called_once_with(self.tempestcommon.raw_list)
125 args[1].assert_called_once_with(self.tempestcommon.raw_list)
126 args[2].assert_called_once_with(
127 self.tempestcommon.list, self.tempestcommon.raw_list)
129 @mock.patch("os.rename")
130 @mock.patch("os.remove")
131 @mock.patch("os.path.exists", return_value=True)
132 def test_apply_blacklist_default(self, *args):
133 item_dict = {'scenarios': ['deploy_scenario'],
135 with mock.patch('six.moves.builtins.open',
136 mock.mock_open()) as mock_open, \
137 mock.patch.object(self.tempestcommon, 'read_file',
138 return_value=['test1', 'test2']), \
139 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
140 'yaml.safe_load', return_value=item_dict):
141 os.environ['DEPLOY_SCENARIO'] = 'deploy_scenario'
142 self.tempestcommon.apply_tempest_blacklist()
144 obj.write.assert_any_call('test1\n')
145 self.assertFalse(obj.write.assert_any_call('test2\n'))
146 args[0].assert_called_once_with(self.tempestcommon.raw_list)
147 args[1].assert_called_once_with(self.tempestcommon.raw_list)
148 args[2].assert_called_once_with(
149 self.tempestcommon.list, self.tempestcommon.raw_list)
151 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.info')
152 def test_run_verifier_tests_default(self, mock_logger_info):
153 with mock.patch('six.moves.builtins.open', mock.mock_open()), \
154 mock.patch('six.moves.builtins.iter',
155 return_value=[r'\} tempest\.']), \
156 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
158 conf_utils.TEMPEST_LIST = 'test_tempest_list'
159 cmd = ["rally", "verify", "start", "--load-list",
160 conf_utils.TEMPEST_LIST]
161 with self.assertRaises(Exception):
162 self.tempestcommon.run_verifier_tests()
164 assert_any_call("Starting Tempest test suite: '%s'.", cmd)
166 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
168 def test_generate_report(self, mock_popen):
169 self.tempestcommon.verification_id = "1234"
170 html_file = os.path.join(
172 getattr(config.CONF, 'dir_results'),
173 self.tempestcommon.case_name),
174 "tempest-report.html")
175 cmd = ["rally", "verify", "report", "--type", "html", "--uuid",
176 "1234", "--to", html_file]
177 self.tempestcommon.generate_report()
178 mock_popen.assert_called_once_with(cmd, stdout=mock.ANY,
181 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
182 'os.path.exists', return_value=False)
183 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs',
184 side_effect=Exception)
185 def test_run_makedirs_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 def test_run_create_resources_ko(self, *args):
194 # pylint: disable=unused-argument
195 self.assertEqual(self.tempestcommon.run(),
196 testcase.TestCase.EX_RUN_ERROR)
198 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
199 'os.path.exists', return_value=False)
200 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
201 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
202 'TempestCommon.configure', side_effect=Exception)
203 def test_run_configure_tempest_ko(self, *args):
204 # pylint: disable=unused-argument
205 self.assertEqual(self.tempestcommon.run(),
206 testcase.TestCase.EX_RUN_ERROR)
208 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
209 'os.path.exists', return_value=False)
210 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
211 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
212 'TempestCommon.configure')
213 def _test_run(self, status, *args):
214 # pylint: disable=unused-argument
215 self.assertEqual(self.tempestcommon.run(), status)
217 def test_run_missing_gen_test_list(self):
218 with mock.patch.object(self.tempestcommon, 'generate_test_list',
219 side_effect=Exception):
220 self._test_run(testcase.TestCase.EX_RUN_ERROR)
222 def test_run_apply_blacklist_ko(self):
223 with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
225 self.tempestcommon, 'apply_tempest_blacklist',
226 side_effect=Exception()):
227 self._test_run(testcase.TestCase.EX_RUN_ERROR)
229 def test_run_verifier_tests_ko(self):
230 with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
231 mock.patch.object(self.tempestcommon,
232 'apply_tempest_blacklist'), \
233 mock.patch.object(self.tempestcommon, 'run_verifier_tests',
234 side_effect=Exception()), \
235 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
236 side_effect=Exception):
237 self._test_run(testcase.TestCase.EX_RUN_ERROR)
239 def test_run_verif_result_ko(self):
240 with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
241 mock.patch.object(self.tempestcommon,
242 'apply_tempest_blacklist'), \
243 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
244 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
245 side_effect=Exception):
246 self._test_run(testcase.TestCase.EX_RUN_ERROR)
248 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.TempestCommon.'
249 'run', return_value=testcase.TestCase.EX_OK)
250 def test_run(self, *args):
251 with mock.patch.object(self.tempestcommon, 'update_rally_regex'), \
252 mock.patch.object(self.tempestcommon, 'generate_test_list'), \
253 mock.patch.object(self.tempestcommon,
254 'apply_tempest_blacklist'), \
255 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
256 mock.patch.object(self.tempestcommon,
257 'parse_verifier_result'), \
258 mock.patch.object(self.tempestcommon, 'generate_report'):
259 self._test_run(testcase.TestCase.EX_OK)
260 args[0].assert_called_once_with()
263 if __name__ == "__main__":
264 logging.disable(logging.CRITICAL)
265 unittest.main(verbosity=2)