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