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_generate_test_list_defcore_mode(self, mock_logger_debug):
54 self.tempestcommon.MODE = 'defcore'
55 with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
56 'shutil.copyfile') as m:
57 self.tempestcommon.generate_test_list('test_verifier_repo_dir')
58 self.assertTrue(m.called)
60 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.error')
61 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.debug')
62 def test_generate_test_list_custom_mode_missing_file(self,
65 self.tempestcommon.MODE = 'custom'
66 with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
67 'os.path.isfile', return_value=False), \
68 self.assertRaises(Exception) as context:
69 msg = "Tempest test list file %s NOT found."
70 self.tempestcommon.generate_test_list('test_verifier_repo_dir')
71 self.assertTrue((msg % conf_utils.TEMPEST_CUSTOM) in context)
73 def test_generate_test_list_custom_mode_default(self):
74 self.tempestcommon.MODE = 'custom'
75 with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
76 'shutil.copyfile') as m, \
77 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
78 'os.path.isfile', return_value=True):
79 self.tempestcommon.generate_test_list('test_verifier_repo_dir')
80 self.assertTrue(m.called)
82 def _test_generate_test_list_mode_default(self, mode):
83 self.tempestcommon.MODE = mode
84 if self.tempestcommon.MODE == 'smoke':
86 elif self.tempestcommon.MODE == 'full':
89 testr_mode = 'tempest.api.' + self.tempestcommon.MODE
90 conf_utils.TEMPEST_RAW_LIST = 'raw_list'
91 verifier_repo_dir = 'test_verifier_repo_dir'
92 with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
93 'ft_utils.execute_command') as m:
95 "testr list-tests {1} > {2};"
96 "cd -;".format(verifier_repo_dir,
98 conf_utils.TEMPEST_RAW_LIST))
99 self.tempestcommon.generate_test_list('test_verifier_repo_dir')
100 m.assert_any_call(cmd)
102 def test_generate_test_list_smoke_mode(self):
103 self._test_generate_test_list_mode_default('smoke')
105 def test_generate_test_list_full_mode(self):
106 self._test_generate_test_list_mode_default('full')
108 def test_parse_verifier_result_missing_verification_uuid(self):
109 self.tempestcommon.VERIFICATION_ID = None
110 with self.assertRaises(Exception):
111 self.tempestcommon.parse_verifier_result()
113 def test_apply_tempest_blacklist_no_blacklist(self):
114 with mock.patch('__builtin__.open', mock.mock_open()) as m, \
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_tempest_blacklist_default(self):
126 item_dict = {'scenarios': ['deploy_scenario'],
127 'installers': ['installer_type'],
129 with mock.patch('__builtin__.open', mock.mock_open()) as m, \
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=['\} 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 self.assertEqual(self.tempestcommon.run(),
160 testcase.TestCase.EX_RUN_ERROR)
162 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
163 'os.path.exists', return_value=False)
164 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
165 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
166 'TempestResourcesManager.create', side_effect=Exception)
167 def test_run_tempest_create_resources_ko(self, *args):
168 self.assertEqual(self.tempestcommon.run(),
169 testcase.TestCase.EX_RUN_ERROR)
171 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
172 'os.path.exists', return_value=False)
173 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
174 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
175 'TempestResourcesManager.create', return_value={})
176 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
177 'get_active_compute_cnt', side_effect=Exception)
178 def test_run_get_active_compute_cnt_ko(self, *args):
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 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
186 'TempestResourcesManager.create', return_value={})
187 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
188 'get_active_compute_cnt', return_value=2)
189 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
190 'conf_utils.configure_tempest', side_effect=Exception)
191 def test_run_configure_tempest_ko(self, *args):
192 self.assertEqual(self.tempestcommon.run(),
193 testcase.TestCase.EX_RUN_ERROR)
195 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
196 'os.path.exists', return_value=False)
197 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
198 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
199 'TempestResourcesManager.create', return_value={})
200 @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
201 'get_active_compute_cnt', return_value=2)
202 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
203 'conf_utils.configure_tempest')
204 def _test_run(self, status, *args):
205 self.assertEqual(self.tempestcommon.run(), status)
207 def test_run_missing_generate_test_list(self):
208 with mock.patch.object(self.tempestcommon, 'generate_test_list',
209 side_effect=Exception):
210 self._test_run(testcase.TestCase.EX_RUN_ERROR)
212 def test_run_apply_tempest_blacklist_ko(self):
213 with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
214 mock.patch.object(self.tempestcommon,
215 'apply_tempest_blacklist',
216 side_effect=Exception()):
217 self._test_run(testcase.TestCase.EX_RUN_ERROR)
219 def test_run_verifier_tests_ko(self, *args):
220 with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
221 mock.patch.object(self.tempestcommon,
222 'apply_tempest_blacklist'), \
223 mock.patch.object(self.tempestcommon, 'run_verifier_tests',
224 side_effect=Exception()), \
225 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
226 side_effect=Exception):
227 self._test_run(testcase.TestCase.EX_RUN_ERROR)
229 def test_run_parse_verifier_result_ko(self, *args):
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 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
235 side_effect=Exception):
236 self._test_run(testcase.TestCase.EX_RUN_ERROR)
238 def test_run(self, *args):
239 with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
240 mock.patch.object(self.tempestcommon,
241 'apply_tempest_blacklist'), \
242 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
243 mock.patch.object(self.tempestcommon, 'parse_verifier_result'):
244 self._test_run(testcase.TestCase.EX_OK)
247 if __name__ == "__main__":
248 logging.disable(logging.CRITICAL)
249 unittest.main(verbosity=2)