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
21 class OSTempestTesting(unittest.TestCase):
22 # pylint: disable=too-many-public-methods
25 with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
26 'conf_utils.get_verifier_id',
27 return_value='test_deploy_id'), \
28 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
29 'conf_utils.get_verifier_deployment_id',
30 return_value='test_deploy_id'), \
31 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
32 'conf_utils.get_verifier_repo_dir',
33 return_value='test_verifier_repo_dir'), \
34 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
35 'conf_utils.get_verifier_deployment_dir',
36 return_value='test_verifier_deploy_dir'), \
37 mock.patch('os_client_config.make_shade'):
38 self.tempestcommon = tempest.TempestCommon()
39 self.tempestsmoke_serial = tempest.TempestSmokeSerial()
40 self.tempestsmoke_parallel = tempest.TempestSmokeParallel()
41 self.tempestfull_parallel = tempest.TempestFullParallel()
42 self.tempestcustom = tempest.TempestCustom()
43 self.tempestdefcore = tempest.TempestDefcore()
44 self.tempestneutrontrunk = tempest.TempestNeutronTrunk()
46 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.error')
47 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.debug')
48 def test_gen_tl_cm_missing_file(self, mock_logger_debug,
50 # pylint: disable=unused-argument
51 self.tempestcommon.mode = 'custom'
52 with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
53 'os.path.isfile', return_value=False), \
54 self.assertRaises(Exception) as context:
55 msg = "Tempest test list file %s NOT found."
56 self.tempestcommon.generate_test_list()
58 (msg % conf_utils.TEMPEST_CUSTOM) in context.exception)
60 def test_gen_tl_cm_default(self):
61 self.tempestcommon.mode = 'custom'
62 with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
63 'shutil.copyfile') as mock_copyfile, \
64 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
65 'os.path.isfile', return_value=True):
66 self.tempestcommon.generate_test_list()
67 self.assertTrue(mock_copyfile.called)
69 @mock.patch('subprocess.check_output')
70 def _test_gen_tl_mode_default(self, mode, mock_exec=None):
71 self.tempestcommon.mode = mode
72 if self.tempestcommon.mode == 'smoke':
73 testr_mode = r"'^tempest\.(api|scenario).*\[.*\bsmoke\b.*\]$'"
74 elif self.tempestcommon.mode == 'full':
75 testr_mode = r"'^tempest\.'"
77 testr_mode = self.tempestcommon.mode
78 verifier_repo_dir = 'test_verifier_repo_dir'
79 cmd = "(cd {0}; stestr list {1} >{2} 2>/dev/null)".format(
80 verifier_repo_dir, testr_mode, self.tempestcommon.list)
81 self.tempestcommon.generate_test_list()
82 mock_exec.assert_called_once_with(cmd, shell=True)
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_gen_tl_neutron_trunk_mode(self):
91 self._test_gen_tl_mode_default('neutron_trunk')
93 def test_verif_res_missing_verif_id(self):
94 self.tempestcommon.verification_id = None
95 with self.assertRaises(Exception):
96 self.tempestcommon.parse_verifier_result()
98 def test_backup_config_default(self):
99 with mock.patch('os.path.exists', return_value=False), \
100 mock.patch('os.makedirs') as mock_makedirs, \
101 mock.patch('shutil.copyfile') as mock_copyfile:
102 self.tempestcommon.backup_tempest_config(
103 'test_conf_file', res_dir='test_dir')
104 self.assertTrue(mock_makedirs.called)
105 self.assertTrue(mock_copyfile.called)
107 with mock.patch('os.path.exists', return_value=True), \
108 mock.patch('shutil.copyfile') as mock_copyfile:
109 self.tempestcommon.backup_tempest_config(
110 'test_conf_file', res_dir='test_dir')
111 self.assertTrue(mock_copyfile.called)
113 @mock.patch("os.rename")
114 @mock.patch("os.remove")
115 @mock.patch("os.path.exists", return_value=True)
116 def test_apply_missing_blacklist(self, *args):
117 with mock.patch('__builtin__.open', mock.mock_open()) as mock_open, \
118 mock.patch.object(self.tempestcommon, 'read_file',
119 return_value=['test1', 'test2']):
120 conf_utils.TEMPEST_BLACKLIST = Exception
121 os.environ['INSTALLER_TYPE'] = 'installer_type'
122 os.environ['DEPLOY_SCENARIO'] = 'deploy_scenario'
123 self.tempestcommon.apply_tempest_blacklist()
125 obj.write.assert_any_call('test1\n')
126 obj.write.assert_any_call('test2\n')
127 args[0].assert_called_once_with(self.tempestcommon.raw_list)
128 args[1].assert_called_once_with(self.tempestcommon.raw_list)
129 args[2].assert_called_once_with(
130 self.tempestcommon.list, self.tempestcommon.raw_list)
132 @mock.patch("os.rename")
133 @mock.patch("os.remove")
134 @mock.patch("os.path.exists", return_value=True)
135 def test_apply_blacklist_default(self, *args):
136 item_dict = {'scenarios': ['deploy_scenario'],
137 'installers': ['installer_type'],
139 with mock.patch('__builtin__.open', mock.mock_open()) as mock_open, \
140 mock.patch.object(self.tempestcommon, 'read_file',
141 return_value=['test1', 'test2']), \
142 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
143 'yaml.safe_load', return_value=item_dict):
144 os.environ['INSTALLER_TYPE'] = 'installer_type'
145 os.environ['DEPLOY_SCENARIO'] = 'deploy_scenario'
146 self.tempestcommon.apply_tempest_blacklist()
148 obj.write.assert_any_call('test1\n')
149 self.assertFalse(obj.write.assert_any_call('test2\n'))
150 args[0].assert_called_once_with(self.tempestcommon.raw_list)
151 args[1].assert_called_once_with(self.tempestcommon.raw_list)
152 args[2].assert_called_once_with(
153 self.tempestcommon.list, self.tempestcommon.raw_list)
155 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.info')
156 def test_run_verifier_tests_default(self, mock_logger_info):
157 with mock.patch('__builtin__.open', mock.mock_open()), \
158 mock.patch('__builtin__.iter', return_value=[r'\} tempest\.']), \
159 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
161 conf_utils.TEMPEST_LIST = 'test_tempest_list'
162 cmd = ["rally", "verify", "start", "--load-list",
163 conf_utils.TEMPEST_LIST]
164 with self.assertRaises(Exception):
165 self.tempestcommon.run_verifier_tests()
167 assert_any_call("Starting Tempest test suite: '%s'.", cmd)
169 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
171 def test_generate_report(self, mock_popen):
172 self.tempestcommon.verification_id = "1234"
173 html_file = os.path.join(tempest.TempestCommon.TEMPEST_RESULTS_DIR,
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 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
186 'TempestResourcesManager.cleanup')
187 def test_run_makedirs_ko(self, *args):
188 # pylint: disable=unused-argument
189 self.assertEqual(self.tempestcommon.run(),
190 testcase.TestCase.EX_RUN_ERROR)
192 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
193 'os.path.exists', return_value=False)
194 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
195 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
196 'TempestResourcesManager.create', side_effect=Exception)
197 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
198 'TempestResourcesManager.cleanup')
199 def test_run_create_resources_ko(self, *args):
200 # pylint: disable=unused-argument
201 self.assertEqual(self.tempestcommon.run(),
202 testcase.TestCase.EX_RUN_ERROR)
204 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
205 'os.path.exists', return_value=False)
206 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
207 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
208 'TempestResourcesManager.create', return_value={})
209 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
210 'TempestResourcesManager.cleanup')
211 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
212 'TempestCommon.configure', side_effect=Exception)
213 def test_run_configure_tempest_ko(self, *args):
214 # pylint: disable=unused-argument
215 self.assertEqual(self.tempestcommon.run(),
216 testcase.TestCase.EX_RUN_ERROR)
218 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
219 'os.path.exists', return_value=False)
220 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
221 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
222 'TempestResourcesManager.create', return_value={})
223 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
224 'TempestResourcesManager.cleanup')
225 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
226 'TempestCommon.configure')
227 def _test_run(self, status, *args):
228 # pylint: disable=unused-argument
229 self.assertEqual(self.tempestcommon.run(), status)
231 def test_run_missing_gen_test_list(self):
232 with mock.patch.object(self.tempestcommon, 'generate_test_list',
233 side_effect=Exception):
234 self._test_run(testcase.TestCase.EX_RUN_ERROR)
236 def test_run_apply_blacklist_ko(self):
237 with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
239 self.tempestcommon, 'apply_tempest_blacklist',
240 side_effect=Exception()):
241 self._test_run(testcase.TestCase.EX_RUN_ERROR)
243 def test_run_verifier_tests_ko(self):
244 with 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 side_effect=Exception()), \
249 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
250 side_effect=Exception):
251 self._test_run(testcase.TestCase.EX_RUN_ERROR)
253 def test_run_verif_result_ko(self):
254 with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
255 mock.patch.object(self.tempestcommon,
256 'apply_tempest_blacklist'), \
257 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
258 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
259 side_effect=Exception):
260 self._test_run(testcase.TestCase.EX_RUN_ERROR)
263 with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
264 mock.patch.object(self.tempestcommon,
265 'apply_tempest_blacklist'), \
266 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
267 mock.patch.object(self.tempestcommon,
268 'parse_verifier_result'), \
269 mock.patch.object(self.tempestcommon, 'generate_report'):
270 self._test_run(testcase.TestCase.EX_OK)
273 if __name__ == "__main__":
274 logging.disable(logging.CRITICAL)
275 unittest.main(verbosity=2)