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 conf_utils
14 from functest.utils.constants import CONST
17 class OSTempestConfUtilsTesting(unittest.TestCase):
19 def test_create_tempest_resources_missing_network_dic(self):
20 with mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
21 'os_utils.get_keystone_client',
22 return_value=mock.Mock()), \
23 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
24 'os_utils.create_tenant',
25 return_value='test_tenant_id'), \
26 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
27 'os_utils.create_user',
28 return_value='test_user_id'), \
29 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
30 'os_utils.create_shared_network_full',
32 self.assertRaises(Exception) as context:
33 conf_utils.create_tempest_resources()
34 msg = 'Failed to create private network'
35 self.assertTrue(msg in context)
37 def test_create_tempest_resources_missing_image(self):
38 with mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
39 'os_utils.get_keystone_client',
40 return_value=mock.Mock()), \
41 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
42 'os_utils.create_tenant',
43 return_value='test_tenant_id'), \
44 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
45 'os_utils.create_user',
46 return_value='test_user_id'), \
47 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
48 'os_utils.create_shared_network_full',
49 return_value=mock.Mock()), \
50 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
51 'os_utils.get_or_create_image',
52 return_value=(mock.Mock(), None)), \
53 self.assertRaises(Exception) as context:
55 CONST.__setattr__('tempest_use_custom_images', True)
56 conf_utils.create_tempest_resources()
57 msg = 'Failed to create image'
58 self.assertTrue(msg in context)
60 CONST.__setattr__('tempest_use_custom_images', False)
61 conf_utils.create_tempest_resources(use_custom_images=True)
62 msg = 'Failed to create image'
63 self.assertTrue(msg in context)
65 def test_create_tempest_resources_missing_flavor(self):
66 with mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
67 'os_utils.get_keystone_client',
68 return_value=mock.Mock()), \
69 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
70 'os_utils.create_tenant',
71 return_value='test_tenant_id'), \
72 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
73 'os_utils.create_user',
74 return_value='test_user_id'), \
75 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
76 'os_utils.create_shared_network_full',
77 return_value=mock.Mock()), \
78 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
79 'os_utils.get_or_create_image',
80 return_value=(mock.Mock(), 'image_id')), \
81 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
82 'os_utils.get_or_create_flavor',
83 return_value=(mock.Mock(), None)), \
84 self.assertRaises(Exception) as context:
85 CONST.__setattr__('tempest_use_custom_images', True)
86 CONST.__setattr__('tempest_use_custom_flavors', True)
87 conf_utils.create_tempest_resources()
88 msg = 'Failed to create flavor'
89 self.assertTrue(msg in context)
91 CONST.__setattr__('tempest_use_custom_images', True)
92 CONST.__setattr__('tempest_use_custom_flavors', False)
93 conf_utils.create_tempest_resources(use_custom_flavors=False)
94 msg = 'Failed to create flavor'
95 self.assertTrue(msg in context)
97 def test_get_verifier_id_missing_verifier(self):
98 CONST.__setattr__('tempest_deployment_name', 'test_deploy_name')
99 with mock.patch('functest.opnfv_tests.openstack.tempest.'
100 'conf_utils.subprocess.Popen') as mock_popen, \
101 self.assertRaises(Exception):
102 mock_stdout = mock.Mock()
103 attrs = {'stdout.readline.return_value': ''}
104 mock_stdout.configure_mock(**attrs)
105 mock_popen.return_value = mock_stdout
106 conf_utils.get_verifier_id(),
108 def test_get_verifier_id_default(self):
109 CONST.__setattr__('tempest_deployment_name', 'test_deploy_name')
110 with mock.patch('functest.opnfv_tests.openstack.tempest.'
111 'conf_utils.subprocess.Popen') as mock_popen:
112 mock_stdout = mock.Mock()
113 attrs = {'stdout.readline.return_value': 'test_deploy_id'}
114 mock_stdout.configure_mock(**attrs)
115 mock_popen.return_value = mock_stdout
117 self.assertEqual(conf_utils.get_verifier_id(),
120 def test_get_verifier_deployment_id_missing_rally(self):
121 CONST.__setattr__('tempest_deployment_name', 'test_deploy_name')
122 with mock.patch('functest.opnfv_tests.openstack.tempest.'
123 'conf_utils.subprocess.Popen') as mock_popen, \
124 self.assertRaises(Exception):
125 mock_stdout = mock.Mock()
126 attrs = {'stdout.readline.return_value': ''}
127 mock_stdout.configure_mock(**attrs)
128 mock_popen.return_value = mock_stdout
129 conf_utils.get_verifier_deployment_id(),
131 def test_get_verifier_deployment_id_default(self):
132 CONST.__setattr__('tempest_deployment_name', 'test_deploy_name')
133 with mock.patch('functest.opnfv_tests.openstack.tempest.'
134 'conf_utils.subprocess.Popen') as mock_popen:
135 mock_stdout = mock.Mock()
136 attrs = {'stdout.readline.return_value': 'test_deploy_id'}
137 mock_stdout.configure_mock(**attrs)
138 mock_popen.return_value = mock_stdout
140 self.assertEqual(conf_utils.get_verifier_deployment_id(),
143 def test_get_verifier_repo_dir_default(self):
144 with mock.patch('functest.opnfv_tests.openstack.tempest.'
145 'conf_utils.os.path.join',
146 return_value='test_verifier_repo_dir'), \
147 mock.patch('functest.opnfv_tests.openstack.tempest.'
148 'conf_utils.get_verifier_id') as m:
149 self.assertEqual(conf_utils.get_verifier_repo_dir(''),
150 'test_verifier_repo_dir')
151 self.assertTrue(m.called)
153 def test_get_verifier_deployment_dir_default(self):
154 with mock.patch('functest.opnfv_tests.openstack.tempest.'
155 'conf_utils.os.path.join',
156 return_value='test_verifier_repo_dir'), \
157 mock.patch('functest.opnfv_tests.openstack.tempest.'
158 'conf_utils.get_verifier_id') as m1, \
159 mock.patch('functest.opnfv_tests.openstack.tempest.'
160 'conf_utils.get_verifier_deployment_id') as m2:
161 self.assertEqual(conf_utils.get_verifier_deployment_dir('', ''),
162 'test_verifier_repo_dir')
163 self.assertTrue(m1.called)
164 self.assertTrue(m2.called)
166 def test_get_repo_tag_default(self):
167 mock_popen = mock.Mock()
168 attrs = {'stdout.readline.return_value': 'test_tag'}
169 mock_popen.configure_mock(**attrs)
171 with mock.patch('functest.opnfv_tests.openstack.tempest.'
172 'conf_utils.subprocess.Popen',
173 return_value=mock_popen):
174 self.assertEqual(conf_utils.get_repo_tag('test_repo'),
177 def test_backup_tempest_config_default(self):
178 with mock.patch('functest.opnfv_tests.openstack.tempest.'
179 'conf_utils.os.path.exists',
180 return_value=False), \
181 mock.patch('functest.opnfv_tests.openstack.tempest.'
182 'conf_utils.os.makedirs') as m1, \
183 mock.patch('functest.opnfv_tests.openstack.tempest.'
184 'conf_utils.shutil.copyfile') as m2:
185 conf_utils.backup_tempest_config('test_conf_file')
186 self.assertTrue(m1.called)
187 self.assertTrue(m2.called)
189 with mock.patch('functest.opnfv_tests.openstack.tempest.'
190 'conf_utils.os.path.exists',
191 return_value=True), \
192 mock.patch('functest.opnfv_tests.openstack.tempest.'
193 'conf_utils.shutil.copyfile') as m2:
194 conf_utils.backup_tempest_config('test_conf_file')
195 self.assertTrue(m2.called)
197 def test_configure_tempest_default(self):
198 with mock.patch('functest.opnfv_tests.openstack.tempest.'
199 'conf_utils.configure_verifier',
200 return_value='test_conf_file'), \
201 mock.patch('functest.opnfv_tests.openstack.tempest.'
202 'conf_utils.configure_tempest_update_params') as m1, \
203 mock.patch('functest.opnfv_tests.openstack.tempest.'
204 'conf_utils.configure_tempest_multisite_params') as m2:
205 conf_utils.configure_tempest('test_dep_dir',
206 MODE='feature_multisite')
207 self.assertTrue(m1.called)
208 self.assertTrue(m2.called)
210 with mock.patch('functest.opnfv_tests.openstack.tempest.'
211 'conf_utils.configure_verifier',
212 return_value='test_conf_file'), \
213 mock.patch('functest.opnfv_tests.openstack.tempest.'
214 'conf_utils.configure_tempest_update_params') as m1:
215 conf_utils.configure_tempest('test_dep_dir')
216 self.assertTrue(m1.called)
217 self.assertTrue(m2.called)
219 def test_configure_tempest_defcore_default(self):
220 img_flavor_dict = {'image_id': 'test_image_id',
221 'flavor_id': 'test_flavor_id',
222 'image_id_alt': 'test_image_alt_id',
223 'flavor_id_alt': 'test_flavor_alt_id'}
224 with mock.patch('functest.opnfv_tests.openstack.tempest.'
225 'conf_utils.configure_verifier',
226 return_value='test_conf_file'), \
227 mock.patch('functest.opnfv_tests.openstack.tempest.'
228 'conf_utils.configure_tempest_update_params'), \
229 mock.patch('functest.opnfv_tests.openstack.tempest.'
230 'conf_utils.ConfigParser.RawConfigParser.'
232 mock.patch('functest.opnfv_tests.openstack.tempest.'
233 'conf_utils.ConfigParser.RawConfigParser.'
235 mock.patch('functest.opnfv_tests.openstack.tempest.'
236 'conf_utils.ConfigParser.RawConfigParser.'
237 'write') as mwrite, \
238 mock.patch('__builtin__.open', mock.mock_open()), \
239 mock.patch('functest.opnfv_tests.openstack.tempest.'
240 'conf_utils.shutil.copyfile'):
241 conf_utils.configure_tempest_defcore('test_dep_dir',
243 mset.assert_any_call('compute', 'image_ref', 'test_image_id')
244 mset.assert_any_call('compute', 'image_ref_alt',
246 mset.assert_any_call('compute', 'flavor_ref', 'test_flavor_id')
247 mset.assert_any_call('compute', 'flavor_ref_alt',
248 'test_flavor_alt_id')
249 self.assertTrue(mread.called)
250 self.assertTrue(mwrite.called)
252 def _test_missing_param(self, params, image_id, flavor_id):
253 with mock.patch('functest.opnfv_tests.openstack.tempest.'
254 'conf_utils.ConfigParser.RawConfigParser.'
256 mock.patch('functest.opnfv_tests.openstack.tempest.'
257 'conf_utils.ConfigParser.RawConfigParser.'
259 mock.patch('functest.opnfv_tests.openstack.tempest.'
260 'conf_utils.ConfigParser.RawConfigParser.'
261 'write') as mwrite, \
262 mock.patch('__builtin__.open', mock.mock_open()), \
263 mock.patch('functest.opnfv_tests.openstack.tempest.'
264 'conf_utils.backup_tempest_config'):
265 CONST.__setattr__('OS_ENDPOINT_TYPE', None)
267 configure_tempest_update_params('test_conf_file',
270 mset.assert_any_call(params[0], params[1], params[2])
271 self.assertTrue(mread.called)
272 self.assertTrue(mwrite.called)
274 def test_configure_tempest_update_params_missing_image_id(self):
275 CONST.__setattr__('tempest_use_custom_images', True)
276 self._test_missing_param(('compute', 'image_ref',
277 'test_image_id'), 'test_image_id',
280 def test_configure_tempest_update_params_missing_image_id_alt(self):
281 CONST.__setattr__('tempest_use_custom_images', True)
282 conf_utils.IMAGE_ID_ALT = 'test_image_id_alt'
283 self._test_missing_param(('compute', 'image_ref_alt',
284 'test_image_id_alt'), None, None)
286 def test_configure_tempest_update_params_missing_flavor_id(self):
287 CONST.__setattr__('tempest_use_custom_flavors', True)
288 self._test_missing_param(('compute', 'flavor_ref',
289 'test_flavor_id'), None,
292 def test_configure_tempest_update_params_missing_flavor_id_alt(self):
293 CONST.__setattr__('tempest_use_custom_flavors', True)
294 conf_utils.FLAVOR_ID_ALT = 'test_flavor_id_alt'
295 self._test_missing_param(('compute', 'flavor_ref_alt',
296 'test_flavor_id_alt'), None,
299 def test_configure_verifier_missing_temp_conf_file(self):
300 with mock.patch('functest.opnfv_tests.openstack.tempest.'
301 'conf_utils.os.path.isfile',
302 return_value=False), \
303 mock.patch('functest.opnfv_tests.openstack.tempest.'
304 'conf_utils.ft_utils.execute_command') as mexe, \
305 self.assertRaises(Exception) as context:
306 conf_utils.configure_verifier('test_dep_dir')
307 mexe.assert_any_call("rally verify configure-verifier")
308 msg = ("Tempest configuration file 'test_dep_dir/tempest.conf'"
310 self.assertTrue(msg in context)
312 def test_configure_verifier_default(self):
313 with mock.patch('functest.opnfv_tests.openstack.tempest.'
314 'conf_utils.os.path.isfile',
315 return_value=True), \
316 mock.patch('functest.opnfv_tests.openstack.tempest.'
317 'conf_utils.ft_utils.execute_command') as mexe:
318 self.assertEqual(conf_utils.configure_verifier('test_dep_dir'),
319 'test_dep_dir/tempest.conf')
320 mexe.assert_any_call("rally verify configure-verifier "
323 def test_configure_tempest_multisite_params_without_fuel(self):
324 conf_utils.CI_INSTALLER_TYPE = 'not_fuel'
325 with mock.patch('functest.opnfv_tests.openstack.tempest.'
326 'conf_utils.os_utils.get_endpoint',
327 return_value='kingbird_endpoint_url'), \
328 mock.patch('functest.opnfv_tests.openstack.tempest.'
329 'conf_utils.ConfigParser.RawConfigParser.'
331 mock.patch('functest.opnfv_tests.openstack.tempest.'
332 'conf_utils.ConfigParser.RawConfigParser.'
334 mock.patch('functest.opnfv_tests.openstack.tempest.'
335 'conf_utils.ConfigParser.RawConfigParser.'
336 'add_section') as msection, \
337 mock.patch('functest.opnfv_tests.openstack.tempest.'
338 'conf_utils.ConfigParser.RawConfigParser.'
339 'write') as mwrite, \
340 mock.patch('__builtin__.open', mock.mock_open()), \
341 mock.patch('functest.opnfv_tests.openstack.tempest.'
342 'conf_utils.backup_tempest_config'):
344 conf_utils.configure_tempest_multisite_params('test_conf_file')
345 msection.assert_any_call("kingbird")
346 mset.assert_any_call('service_available', 'kingbird', 'true')
347 mset.assert_any_call('kingbird', 'endpoint_type', 'publicURL')
348 mset.assert_any_call('kingbird', 'TIME_TO_SYNC', '120')
349 mset.assert_any_call('kingbird', 'endpoint_url',
350 'kingbird_endpoint_url')
351 self.assertTrue(mread.called)
352 self.assertTrue(mwrite.called)
354 def test_install_verifier_ext_default(self):
355 with mock.patch('functest.opnfv_tests.openstack.tempest.'
356 'conf_utils.get_repo_tag',
357 return_value='test_tag'), \
358 mock.patch('functest.opnfv_tests.openstack.tempest.'
359 'conf_utils.ft_utils.'
360 'execute_command_raise') as mexe:
361 conf_utils.install_verifier_ext('test_path')
362 cmd = ("rally verify add-verifier-ext --source test_path "
363 "--version test_tag")
364 error_msg = ("Problem while adding verifier extension from"
366 mexe.assert_called_once_with(cmd, error_msg=error_msg)
368 if __name__ == "__main__":
369 logging.disable(logging.CRITICAL)
370 unittest.main(verbosity=2)