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.opnfv_tests.openstack.tempest import tempest, conf_utils
14 from functest.utils.constants import CONST
15 from snaps.openstack.os_credentials import OSCreds
18 class OSTempestConfUtilsTesting(unittest.TestCase):
21 self.os_creds = OSCreds(
22 username='user', password='pass',
23 auth_url='http://foo.com:5000/v3', project_name='bar')
25 @mock.patch('snaps.openstack.utils.deploy_utils.create_project',
26 return_value=mock.Mock())
27 @mock.patch('snaps.openstack.utils.deploy_utils.create_user',
28 return_value=mock.Mock())
29 @mock.patch('snaps.openstack.utils.deploy_utils.create_network',
31 @mock.patch('snaps.openstack.utils.deploy_utils.create_image',
32 return_value=mock.Mock())
33 def test_create_tempest_resources_missing_network_dic(self, *mock_args):
34 tempest_resources = tempest.TempestResourcesManager(os_creds={})
35 with self.assertRaises(Exception) as context:
36 tempest_resources.create()
37 msg = 'Failed to create private network'
38 self.assertTrue(msg in context.exception)
40 @mock.patch('snaps.openstack.utils.deploy_utils.create_project',
41 return_value=mock.Mock())
42 @mock.patch('snaps.openstack.utils.deploy_utils.create_user',
43 return_value=mock.Mock())
44 @mock.patch('snaps.openstack.utils.deploy_utils.create_network',
45 return_value=mock.Mock())
46 @mock.patch('snaps.openstack.utils.deploy_utils.create_image',
48 def test_create_tempest_resources_missing_image(self, *mock_args):
49 tempest_resources = tempest.TempestResourcesManager(os_creds={})
51 with self.assertRaises(Exception) as context:
52 tempest_resources.create()
53 msg = 'Failed to create image'
54 self.assertTrue(msg in context.exception, msg=str(context.exception))
56 @mock.patch('snaps.openstack.utils.deploy_utils.create_project',
57 return_value=mock.Mock())
58 @mock.patch('snaps.openstack.utils.deploy_utils.create_user',
59 return_value=mock.Mock())
60 @mock.patch('snaps.openstack.utils.deploy_utils.create_network',
61 return_value=mock.Mock())
62 @mock.patch('snaps.openstack.utils.deploy_utils.create_image',
63 return_value=mock.Mock())
64 @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create',
66 def test_create_tempest_resources_missing_flavor(self, *mock_args):
67 tempest_resources = tempest.TempestResourcesManager(
68 os_creds=self.os_creds)
70 CONST.__setattr__('tempest_use_custom_flavors', 'True')
71 with self.assertRaises(Exception) as context:
72 tempest_resources.create()
73 msg = 'Failed to create flavor'
74 self.assertTrue(msg in context.exception, msg=str(context.exception))
76 CONST.__setattr__('tempest_use_custom_flavors', 'False')
77 with self.assertRaises(Exception) as context:
78 tempest_resources.create(use_custom_flavors=True)
79 msg = 'Failed to create flavor'
80 self.assertTrue(msg in context.exception, msg=str(context.exception))
82 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
84 @mock.patch('functest.utils.functest_utils.execute_command_raise')
85 @mock.patch('functest.utils.functest_utils.execute_command')
86 def test_create_rally_deployment(self, mock_exec, mock_exec_raise,
89 conf_utils.create_rally_deployment()
91 cmd = "rally deployment destroy opnfv-rally"
92 error_msg = "Deployment %s does not exist." % \
93 CONST.__getattribute__('rally_deployment_name')
94 mock_logger_info.assert_any_call("Creating Rally environment...")
95 mock_exec.assert_any_call(cmd, error_msg=error_msg, verbose=False)
97 cmd = "rally deployment create --fromenv --name="
98 cmd += CONST.__getattribute__('rally_deployment_name')
99 error_msg = "Problem while creating Rally deployment"
100 mock_exec_raise.assert_any_call(cmd, error_msg=error_msg)
102 cmd = "rally deployment check"
103 error_msg = ("OpenStack not responding or "
104 "faulty Rally deployment.")
105 mock_exec_raise.assert_any_call(cmd, error_msg=error_msg)
107 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
109 def test_create_verifier(self, mock_logger_debug):
110 mock_popen = mock.Mock()
111 attrs = {'poll.return_value': None,
112 'stdout.readline.return_value': '0'}
113 mock_popen.configure_mock(**attrs)
115 CONST.__setattr__('tempest_verifier_name', 'test_veifier_name')
116 with mock.patch('functest.utils.functest_utils.execute_command_raise',
117 side_effect=Exception), \
118 self.assertRaises(Exception):
119 conf_utils.create_verifier()
120 mock_logger_debug.assert_any_call("Tempest test_veifier_name"
123 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
124 'create_verifier', return_value=mock.Mock())
125 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
126 'create_rally_deployment', return_value=mock.Mock())
127 def test_get_verifier_id_missing_verifier(self, mock_rally, mock_tempest):
128 CONST.__setattr__('tempest_verifier_name', 'test_verifier_name')
129 with mock.patch('functest.opnfv_tests.openstack.tempest.'
130 'conf_utils.subprocess.Popen') as mock_popen, \
131 self.assertRaises(Exception):
132 mock_stdout = mock.Mock()
133 attrs = {'stdout.readline.return_value': ''}
134 mock_stdout.configure_mock(**attrs)
135 mock_popen.return_value = mock_stdout
136 conf_utils.get_verifier_id()
138 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
139 'create_verifier', return_value=mock.Mock())
140 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
141 'create_rally_deployment', return_value=mock.Mock())
142 def test_get_verifier_id_default(self, mock_rally, mock_tempest):
143 CONST.__setattr__('tempest_verifier_name', 'test_verifier_name')
144 with mock.patch('functest.opnfv_tests.openstack.tempest.'
145 'conf_utils.subprocess.Popen') as mock_popen:
146 mock_stdout = mock.Mock()
147 attrs = {'stdout.readline.return_value': 'test_deploy_id'}
148 mock_stdout.configure_mock(**attrs)
149 mock_popen.return_value = mock_stdout
151 self.assertEqual(conf_utils.get_verifier_id(),
154 def test_get_verifier_deployment_id_missing_rally(self):
155 CONST.__setattr__('tempest_verifier_name', 'test_deploy_name')
156 with mock.patch('functest.opnfv_tests.openstack.tempest.'
157 'conf_utils.subprocess.Popen') as mock_popen, \
158 self.assertRaises(Exception):
159 mock_stdout = mock.Mock()
160 attrs = {'stdout.readline.return_value': ''}
161 mock_stdout.configure_mock(**attrs)
162 mock_popen.return_value = mock_stdout
163 conf_utils.get_verifier_deployment_id(),
165 def test_get_verifier_deployment_id_default(self):
166 CONST.__setattr__('tempest_verifier_name', 'test_deploy_name')
167 with mock.patch('functest.opnfv_tests.openstack.tempest.'
168 'conf_utils.subprocess.Popen') as mock_popen:
169 mock_stdout = mock.Mock()
170 attrs = {'stdout.readline.return_value': 'test_deploy_id'}
171 mock_stdout.configure_mock(**attrs)
172 mock_popen.return_value = mock_stdout
174 self.assertEqual(conf_utils.get_verifier_deployment_id(),
177 def test_get_verifier_repo_dir_default(self):
178 with mock.patch('functest.opnfv_tests.openstack.tempest.'
179 'conf_utils.os.path.join',
180 return_value='test_verifier_repo_dir'), \
181 mock.patch('functest.opnfv_tests.openstack.tempest.'
182 'conf_utils.get_verifier_id') as m:
183 self.assertEqual(conf_utils.get_verifier_repo_dir(''),
184 'test_verifier_repo_dir')
185 self.assertTrue(m.called)
187 def test_get_verifier_deployment_dir_default(self):
188 with mock.patch('functest.opnfv_tests.openstack.tempest.'
189 'conf_utils.os.path.join',
190 return_value='test_verifier_repo_dir'), \
191 mock.patch('functest.opnfv_tests.openstack.tempest.'
192 'conf_utils.get_verifier_id') as m1, \
193 mock.patch('functest.opnfv_tests.openstack.tempest.'
194 'conf_utils.get_verifier_deployment_id') as m2:
195 self.assertEqual(conf_utils.get_verifier_deployment_dir('', ''),
196 'test_verifier_repo_dir')
197 self.assertTrue(m1.called)
198 self.assertTrue(m2.called)
200 def test_backup_tempest_config_default(self):
201 with mock.patch('functest.opnfv_tests.openstack.tempest.'
202 'conf_utils.os.path.exists',
203 return_value=False), \
204 mock.patch('functest.opnfv_tests.openstack.tempest.'
205 'conf_utils.os.makedirs') as m1, \
206 mock.patch('functest.opnfv_tests.openstack.tempest.'
207 'conf_utils.shutil.copyfile') as m2:
208 conf_utils.backup_tempest_config('test_conf_file')
209 self.assertTrue(m1.called)
210 self.assertTrue(m2.called)
212 with mock.patch('functest.opnfv_tests.openstack.tempest.'
213 'conf_utils.os.path.exists',
214 return_value=True), \
215 mock.patch('functest.opnfv_tests.openstack.tempest.'
216 'conf_utils.shutil.copyfile') as m2:
217 conf_utils.backup_tempest_config('test_conf_file')
218 self.assertTrue(m2.called)
220 def test_configure_tempest_default(self):
221 with mock.patch('functest.opnfv_tests.openstack.tempest.'
222 'conf_utils.configure_verifier',
223 return_value='test_conf_file'), \
224 mock.patch('functest.opnfv_tests.openstack.tempest.'
225 'conf_utils.configure_tempest_update_params') as m1:
226 conf_utils.configure_tempest('test_dep_dir')
227 self.assertTrue(m1.called)
229 def test_configure_tempest_defcore_default(self):
230 with mock.patch('functest.opnfv_tests.openstack.tempest.'
231 'conf_utils.configure_verifier',
232 return_value='test_conf_file'), \
233 mock.patch('functest.opnfv_tests.openstack.tempest.'
234 'conf_utils.configure_tempest_update_params'), \
235 mock.patch('functest.opnfv_tests.openstack.tempest.'
236 'conf_utils.ConfigParser.RawConfigParser.'
238 mock.patch('functest.opnfv_tests.openstack.tempest.'
239 'conf_utils.ConfigParser.RawConfigParser.'
241 mock.patch('functest.opnfv_tests.openstack.tempest.'
242 'conf_utils.ConfigParser.RawConfigParser.'
243 'write') as mwrite, \
244 mock.patch('__builtin__.open', mock.mock_open()), \
245 mock.patch('functest.opnfv_tests.openstack.tempest.'
246 'conf_utils.generate_test_accounts_file'), \
247 mock.patch('functest.opnfv_tests.openstack.tempest.'
248 'conf_utils.shutil.copyfile'):
249 conf_utils.configure_tempest_defcore(
250 'test_dep_dir', 'test_image_id', 'test_flavor_id',
251 'test_image_alt_id', 'test_flavor_alt_id', 'test_tenant_id')
252 mset.assert_any_call('compute', 'image_ref', 'test_image_id')
253 mset.assert_any_call('compute', 'image_ref_alt',
255 mset.assert_any_call('compute', 'flavor_ref', 'test_flavor_id')
256 mset.assert_any_call('compute', 'flavor_ref_alt',
257 'test_flavor_alt_id')
258 self.assertTrue(mread.called)
259 self.assertTrue(mwrite.called)
261 def test_generate_test_accounts_file_default(self):
262 with mock.patch("__builtin__.open", mock.mock_open()), \
263 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
264 'yaml.dump') as mock_dump:
265 conf_utils.generate_test_accounts_file('test_tenant_id')
266 self.assertTrue(mock_dump.called)
268 def _test_missing_param(self, params, image_id, flavor_id):
269 with mock.patch('functest.opnfv_tests.openstack.tempest.'
270 'conf_utils.ConfigParser.RawConfigParser.'
272 mock.patch('functest.opnfv_tests.openstack.tempest.'
273 'conf_utils.ConfigParser.RawConfigParser.'
275 mock.patch('functest.opnfv_tests.openstack.tempest.'
276 'conf_utils.ConfigParser.RawConfigParser.'
277 'write') as mwrite, \
278 mock.patch('__builtin__.open', mock.mock_open()), \
279 mock.patch('functest.opnfv_tests.openstack.tempest.'
280 'conf_utils.backup_tempest_config'), \
281 mock.patch('functest.utils.functest_utils.yaml.safe_load',
282 return_value={'validation': {'ssh_timeout': 300}}):
283 CONST.__setattr__('OS_ENDPOINT_TYPE', None)
284 conf_utils.configure_tempest_update_params(
285 'test_conf_file', image_id=image_id, flavor_id=flavor_id)
286 mset.assert_any_call(params[0], params[1], params[2])
287 self.assertTrue(mread.called)
288 self.assertTrue(mwrite.called)
290 def test_configure_tempest_update_params_missing_image_id(self):
291 self._test_missing_param(('compute', 'image_ref',
292 'test_image_id'), 'test_image_id',
295 def test_configure_tempest_update_params_missing_image_id_alt(self):
296 conf_utils.IMAGE_ID_ALT = 'test_image_id_alt'
297 self._test_missing_param(('compute', 'image_ref_alt',
298 'test_image_id_alt'), None, None)
300 def test_configure_tempest_update_params_missing_flavor_id(self):
301 CONST.__setattr__('tempest_use_custom_flavors', 'True')
302 self._test_missing_param(('compute', 'flavor_ref',
303 'test_flavor_id'), None,
306 def test_configure_tempest_update_params_missing_flavor_id_alt(self):
307 CONST.__setattr__('tempest_use_custom_flavors', 'True')
308 conf_utils.FLAVOR_ID_ALT = 'test_flavor_id_alt'
309 self._test_missing_param(('compute', 'flavor_ref_alt',
310 'test_flavor_id_alt'), None,
313 def test_configure_verifier_missing_temp_conf_file(self):
314 with mock.patch('functest.opnfv_tests.openstack.tempest.'
315 'conf_utils.os.path.isfile',
316 return_value=False), \
317 mock.patch('functest.opnfv_tests.openstack.tempest.'
318 'conf_utils.ft_utils.execute_command') as mexe, \
319 self.assertRaises(Exception) as context:
320 conf_utils.configure_verifier('test_dep_dir')
321 mexe.assert_any_call("rally verify configure-verifier")
322 msg = ("Tempest configuration file 'test_dep_dir/tempest.conf'"
324 self.assertTrue(msg in context)
326 def test_configure_verifier_default(self):
327 with mock.patch('functest.opnfv_tests.openstack.tempest.'
328 'conf_utils.os.path.isfile',
329 return_value=True), \
330 mock.patch('functest.opnfv_tests.openstack.tempest.'
331 'conf_utils.ft_utils.execute_command') as mexe:
332 self.assertEqual(conf_utils.configure_verifier('test_dep_dir'),
333 'test_dep_dir/tempest.conf')
334 mexe.assert_any_call("rally verify configure-verifier "
338 if __name__ == "__main__":
339 logging.disable(logging.CRITICAL)
340 unittest.main(verbosity=2)