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
13 from functest.core import testcase
14 from functest.opnfv_tests.openstack.tempest import tempest
15 from functest.opnfv_tests.openstack.tempest import conf_utils
16 from functest.utils.constants import CONST
18 from snaps.openstack.os_credentials import OSCreds
21 class OSTempestTesting(unittest.TestCase):
25 username='user', password='pass',
26 auth_url='http://foo.com:5000/v3', project_name='bar')
28 with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
29 'conf_utils.get_verifier_id',
30 return_value='test_deploy_id'), \
31 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
32 'conf_utils.get_verifier_deployment_id',
33 return_value='test_deploy_id'), \
34 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
35 'conf_utils.get_verifier_repo_dir',
36 return_value='test_verifier_repo_dir'), \
37 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
38 'conf_utils.get_verifier_deployment_dir',
39 return_value='test_verifier_deploy_dir'), \
40 mock.patch('snaps.openstack.tests.openstack_tests.get_credentials',
41 return_value=os_creds):
42 self.tempestcommon = tempest.TempestCommon()
43 self.tempestsmoke_serial = tempest.TempestSmokeSerial()
44 self.tempestsmoke_parallel = tempest.TempestSmokeParallel()
45 self.tempestfull_parallel = tempest.TempestFullParallel()
46 self.tempestcustom = tempest.TempestCustom()
47 self.tempestdefcore = tempest.TempestDefcore()
49 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.debug')
50 def test_generate_test_list_defcore_mode(self, mock_logger_debug):
51 self.tempestcommon.MODE = 'defcore'
52 with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
53 'shutil.copyfile') as m:
54 self.tempestcommon.generate_test_list('test_verifier_repo_dir')
55 self.assertTrue(m.called)
57 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.error')
58 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.debug')
59 def test_generate_test_list_custom_mode_missing_file(self,
62 self.tempestcommon.MODE = 'custom'
63 with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
64 'os.path.isfile', return_value=False), \
65 self.assertRaises(Exception) as context:
66 msg = "Tempest test list file %s NOT found."
67 self.tempestcommon.generate_test_list('test_verifier_repo_dir')
68 self.assertTrue((msg % conf_utils.TEMPEST_CUSTOM) in context)
70 def test_generate_test_list_custom_mode_default(self):
71 self.tempestcommon.MODE = 'custom'
72 with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
73 'shutil.copyfile') as m, \
74 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
75 'os.path.isfile', return_value=True):
76 self.tempestcommon.generate_test_list('test_verifier_repo_dir')
77 self.assertTrue(m.called)
79 def _test_generate_test_list_mode_default(self, mode):
80 self.tempestcommon.MODE = mode
81 if self.tempestcommon.MODE == 'smoke':
83 elif self.tempestcommon.MODE == 'full':
86 testr_mode = 'tempest.api.' + self.tempestcommon.MODE
87 conf_utils.TEMPEST_RAW_LIST = 'raw_list'
88 verifier_repo_dir = 'test_verifier_repo_dir'
89 with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
90 'ft_utils.execute_command') as m:
92 "testr list-tests {1} > {2};"
93 "cd -;".format(verifier_repo_dir,
95 conf_utils.TEMPEST_RAW_LIST))
96 self.tempestcommon.generate_test_list('test_verifier_repo_dir')
97 m.assert_any_call(cmd)
99 def test_generate_test_list_smoke_mode(self):
100 self._test_generate_test_list_mode_default('smoke')
102 def test_generate_test_list_full_mode(self):
103 self._test_generate_test_list_mode_default('full')
105 def test_parse_verifier_result_missing_verification_uuid(self):
106 self.tempestcommon.VERIFICATION_ID = None
107 with self.assertRaises(Exception):
108 self.tempestcommon.parse_verifier_result()
110 def test_apply_tempest_blacklist_no_blacklist(self):
111 with mock.patch('__builtin__.open', mock.mock_open()) as m, \
112 mock.patch.object(self.tempestcommon, 'read_file',
113 return_value=['test1', 'test2']):
114 conf_utils.TEMPEST_BLACKLIST = Exception
115 CONST.__setattr__('INSTALLER_TYPE', 'installer_type')
116 CONST.__setattr__('DEPLOY_SCENARIO', 'deploy_scenario')
117 self.tempestcommon.apply_tempest_blacklist()
119 obj.write.assert_any_call('test1\n')
120 obj.write.assert_any_call('test2\n')
122 def test_apply_tempest_blacklist_default(self):
123 item_dict = {'scenarios': ['deploy_scenario'],
124 'installers': ['installer_type'],
126 with mock.patch('__builtin__.open', mock.mock_open()) as m, \
127 mock.patch.object(self.tempestcommon, 'read_file',
128 return_value=['test1', 'test2']), \
129 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
130 'yaml.safe_load', return_value=item_dict):
131 CONST.__setattr__('INSTALLER_TYPE', 'installer_type')
132 CONST.__setattr__('DEPLOY_SCENARIO', 'deploy_scenario')
133 self.tempestcommon.apply_tempest_blacklist()
135 obj.write.assert_any_call('test1\n')
136 self.assertFalse(obj.write.assert_any_call('test2\n'))
138 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.info')
139 def test_run_verifier_tests_default(self, mock_logger_info):
140 with mock.patch('__builtin__.open', mock.mock_open()), \
141 mock.patch('__builtin__.iter', return_value=['\} tempest\.']), \
142 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
144 conf_utils.TEMPEST_LIST = 'test_tempest_list'
145 cmd_line = ("rally verify start --load-list "
146 "test_tempest_list --detailed")
147 self.tempestcommon.run_verifier_tests()
149 assert_any_call("Starting Tempest test suite: '%s'."
152 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
153 'os.path.exists', return_value=False)
154 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs',
155 side_effect=Exception)
156 def test_run_makedirs_ko(self, *args):
157 self.assertEqual(self.tempestcommon.run(),
158 testcase.TestCase.EX_RUN_ERROR)
160 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
161 'os.path.exists', return_value=False)
162 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
163 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
164 'TempestResourcesManager.create', side_effect=Exception)
165 def test_run_tempest_create_resources_ko(self, *args):
166 self.assertEqual(self.tempestcommon.run(),
167 testcase.TestCase.EX_RUN_ERROR)
169 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
170 'os.path.exists', return_value=False)
171 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
172 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
173 'TempestResourcesManager.create', return_value={})
174 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
175 'conf_utils.configure_tempest', side_effect=Exception)
176 def test_run_configure_tempest_ko(self, *args):
177 self.assertEqual(self.tempestcommon.run(),
178 testcase.TestCase.EX_RUN_ERROR)
180 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
181 'os.path.exists', return_value=False)
182 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
183 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
184 'TempestResourcesManager.create', return_value={})
185 @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
186 'conf_utils.configure_tempest')
187 def _test_run(self, status, *args):
188 self.assertEqual(self.tempestcommon.run(), status)
190 def test_run_missing_generate_test_list(self):
191 with mock.patch.object(self.tempestcommon, 'generate_test_list',
192 side_effect=Exception):
193 self._test_run(testcase.TestCase.EX_RUN_ERROR)
195 def test_run_apply_tempest_blacklist_ko(self):
196 with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
197 mock.patch.object(self.tempestcommon,
198 'apply_tempest_blacklist',
199 side_effect=Exception()):
200 self._test_run(testcase.TestCase.EX_RUN_ERROR)
202 def test_run_verifier_tests_ko(self, *args):
203 with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
204 mock.patch.object(self.tempestcommon,
205 'apply_tempest_blacklist'), \
206 mock.patch.object(self.tempestcommon, 'run_verifier_tests',
207 side_effect=Exception()), \
208 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
209 side_effect=Exception):
210 self._test_run(testcase.TestCase.EX_RUN_ERROR)
212 def test_run_parse_verifier_result_ko(self, *args):
213 with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
214 mock.patch.object(self.tempestcommon,
215 'apply_tempest_blacklist'), \
216 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
217 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
218 side_effect=Exception):
219 self._test_run(testcase.TestCase.EX_RUN_ERROR)
221 def test_run(self, *args):
222 with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
223 mock.patch.object(self.tempestcommon,
224 'apply_tempest_blacklist'), \
225 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
226 mock.patch.object(self.tempestcommon, 'parse_verifier_result'):
227 self._test_run(testcase.TestCase.EX_OK)
230 if __name__ == "__main__":
231 logging.disable(logging.CRITICAL)
232 unittest.main(verbosity=2)