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 functest.core import testcase
16 from functest.opnfv_tests.openstack.tempest import tempest
17 from functest.opnfv_tests.openstack.tempest import conf_utils
18 from functest.utils.constants import CONST
20 from snaps.openstack.os_credentials import OSCreds
23 class OSTempestTesting(unittest.TestCase):
27 username='user', password='pass',
28 auth_url='http://foo.com:5000/v3', project_name='bar')
30 with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
31 'conf_utils.get_verifier_id',
32 return_value='test_deploy_id'), \
33 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
34 'conf_utils.get_verifier_deployment_id',
35 return_value='test_deploy_id'), \
36 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
37 'conf_utils.get_verifier_repo_dir',
38 return_value='test_verifier_repo_dir'), \
39 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
40 'conf_utils.get_verifier_deployment_dir',
41 return_value='test_verifier_deploy_dir'), \
42 mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
44 return_value=os_creds):
45 self.tempestcommon = tempest.TempestCommon()
46 self.tempestsmoke_serial = tempest.TempestSmokeSerial()
47 self.tempestsmoke_parallel = tempest.TempestSmokeParallel()
48 self.tempestfull_parallel = tempest.TempestFullParallel()
49 self.tempestcustom = tempest.TempestCustom()
50 self.tempestdefcore = tempest.TempestDefcore()
52 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.debug')
53 def test_gen_tl_defcore_mode(self, mock_logger_debug):
54 # pylint: disable=unused-argument
55 self.tempestcommon.mode = 'defcore'
56 with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
57 'shutil.copyfile') as mock_copyfile:
58 self.tempestcommon.generate_test_list('test_verifier_repo_dir')
59 self.assertTrue(mock_copyfile.called)
61 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.error')
62 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.debug')
63 def test_gen_tl_cm_missing_file(self, mock_logger_debug,
65 # pylint: disable=unused-argument
66 self.tempestcommon.mode = 'custom'
67 with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
68 'os.path.isfile', return_value=False), \
69 self.assertRaises(Exception) as context:
70 msg = "Tempest test list file %s NOT found."
71 self.tempestcommon.generate_test_list('test_verifier_repo_dir')
72 self.assertTrue((msg % conf_utils.TEMPEST_CUSTOM) in context)
74 def test_gen_tl_cm_default(self):
75 self.tempestcommon.mode = 'custom'
76 with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
77 'shutil.copyfile') as mock_copyfile, \
78 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
79 'os.path.isfile', return_value=True):
80 self.tempestcommon.generate_test_list('test_verifier_repo_dir')
81 self.assertTrue(mock_copyfile.called)
83 def _test_gen_tl_mode_default(self, mode):
84 self.tempestcommon.mode = mode
85 if self.tempestcommon.mode == 'smoke':
87 elif self.tempestcommon.mode == 'full':
90 testr_mode = 'tempest.api.' + self.tempestcommon.mode
91 conf_utils.TEMPEST_RAW_LIST = 'raw_list'
92 verifier_repo_dir = 'test_verifier_repo_dir'
93 with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
94 'ft_utils.execute_command') as mock_exec:
96 "testr list-tests {1} > {2};"
97 "cd -;".format(verifier_repo_dir, testr_mode,
98 conf_utils.TEMPEST_RAW_LIST))
99 self.tempestcommon.generate_test_list('test_verifier_repo_dir')
100 mock_exec.assert_any_call(cmd)
102 def test_gen_tl_smoke_mode(self):
103 self._test_gen_tl_mode_default('smoke')
105 def test_gen_tl_full_mode(self):
106 self._test_gen_tl_mode_default('full')
108 def test_verif_res_missing_verif_id(self):
109 self.tempestcommon.verification_id = None
110 with self.assertRaises(Exception):
111 self.tempestcommon.parse_verifier_result()
113 def test_apply_missing_blacklist(self):
114 with mock.patch('__builtin__.open', mock.mock_open()) as mock_open, \
115 mock.patch.object(self.tempestcommon, 'read_file',
116 return_value=['test1', 'test2']):
117 conf_utils.TEMPEST_BLACKLIST = Exception
118 CONST.__setattr__('INSTALLER_TYPE', 'installer_type')
119 CONST.__setattr__('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')
125 def test_apply_blacklist_default(self):
126 item_dict = {'scenarios': ['deploy_scenario'],
127 'installers': ['installer_type'],
129 with mock.patch('__builtin__.open', mock.mock_open()) as mock_open, \
130 mock.patch.object(self.tempestcommon, 'read_file',
131 return_value=['test1', 'test2']), \
132 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
133 'yaml.safe_load', return_value=item_dict):
134 CONST.__setattr__('INSTALLER_TYPE', 'installer_type')
135 CONST.__setattr__('DEPLOY_SCENARIO', 'deploy_scenario')
136 self.tempestcommon.apply_tempest_blacklist()
138 obj.write.assert_any_call('test1\n')
139 self.assertFalse(obj.write.assert_any_call('test2\n'))
141 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.info')
142 def test_run_verifier_tests_default(self, mock_logger_info):
143 with mock.patch('__builtin__.open', mock.mock_open()), \
144 mock.patch('__builtin__.iter', return_value=[r'\} tempest\.']), \
145 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
147 conf_utils.TEMPEST_LIST = 'test_tempest_list'
148 cmd = ["rally", "verify", "start", "--load-list",
149 conf_utils.TEMPEST_LIST]
150 self.tempestcommon.run_verifier_tests()
152 assert_any_call("Starting Tempest test suite: '%s'.", cmd)
154 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
155 'os.path.exists', return_value=False)
156 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs',
157 side_effect=Exception)
158 def test_run_makedirs_ko(self, *args):
159 # pylint: disable=unused-argument
160 self.assertEqual(self.tempestcommon.run(),
161 testcase.TestCase.EX_RUN_ERROR)
163 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
164 'os.path.exists', return_value=False)
165 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
166 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
167 'TempestResourcesManager.create', side_effect=Exception)
168 def test_run_create_resources_ko(self, *args):
169 # pylint: disable=unused-argument
170 self.assertEqual(self.tempestcommon.run(),
171 testcase.TestCase.EX_RUN_ERROR)
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 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
177 'TempestResourcesManager.create', return_value={})
178 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
179 'get_active_compute_cnt', side_effect=Exception)
180 def test_run_get_active_comp_cnt_ko(self, *args):
181 # pylint: disable=unused-argument
182 self.assertEqual(self.tempestcommon.run(),
183 testcase.TestCase.EX_RUN_ERROR)
185 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
186 'os.path.exists', return_value=False)
187 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
188 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
189 'TempestResourcesManager.create', return_value={})
190 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
191 'get_active_compute_cnt', return_value=2)
192 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
193 'conf_utils.configure_tempest', side_effect=Exception)
194 def test_run_configure_tempest_ko(self, *args):
195 # pylint: disable=unused-argument
196 self.assertEqual(self.tempestcommon.run(),
197 testcase.TestCase.EX_RUN_ERROR)
199 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
200 'os.path.exists', return_value=False)
201 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
202 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
203 'TempestResourcesManager.create', return_value={})
204 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
205 'get_active_compute_cnt', return_value=2)
206 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
207 'conf_utils.configure_tempest')
208 def _test_run(self, status, *args):
209 # pylint: disable=unused-argument
210 self.assertEqual(self.tempestcommon.run(), status)
212 def test_run_missing_gen_test_list(self):
213 with mock.patch.object(self.tempestcommon, 'generate_test_list',
214 side_effect=Exception):
215 self._test_run(testcase.TestCase.EX_RUN_ERROR)
217 def test_run_apply_blacklist_ko(self):
218 with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
219 mock.patch.object(self.tempestcommon,
220 'apply_tempest_blacklist',
221 side_effect=Exception()):
222 self._test_run(testcase.TestCase.EX_RUN_ERROR)
224 def test_run_verifier_tests_ko(self):
225 with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
226 mock.patch.object(self.tempestcommon,
227 'apply_tempest_blacklist'), \
228 mock.patch.object(self.tempestcommon, 'run_verifier_tests',
229 side_effect=Exception()), \
230 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
231 side_effect=Exception):
232 self._test_run(testcase.TestCase.EX_RUN_ERROR)
234 def test_run_verif_result_ko(self):
235 with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
236 mock.patch.object(self.tempestcommon,
237 'apply_tempest_blacklist'), \
238 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
239 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
240 side_effect=Exception):
241 self._test_run(testcase.TestCase.EX_RUN_ERROR)
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 mock.patch.object(self.tempestcommon, 'parse_verifier_result'):
249 self._test_run(testcase.TestCase.EX_OK)
252 if __name__ == "__main__":
253 logging.disable(logging.CRITICAL)
254 unittest.main(verbosity=2)