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.create_shared_network_full',
23 self.assertRaises(Exception) as context:
24 conf_utils.create_tempest_resources()
25 msg = 'Failed to create private network'
26 self.assertTrue(msg in context)
28 def test_create_tempest_resources_missing_image(self):
29 with mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
30 'os_utils.create_shared_network_full',
31 return_value=mock.Mock()), \
32 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
33 'os_utils.get_or_create_image',
34 return_value=(mock.Mock(), None)), \
35 self.assertRaises(Exception) as context:
37 CONST.__setattr__('tempest_use_custom_images', True)
38 conf_utils.create_tempest_resources()
39 msg = 'Failed to create image'
40 self.assertTrue(msg in context)
42 CONST.__setattr__('tempest_use_custom_images', False)
43 conf_utils.create_tempest_resources(use_custom_images=True)
44 msg = 'Failed to create image'
45 self.assertTrue(msg in context)
47 def test_create_tempest_resources_missing_flavor(self):
48 with mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
49 'os_utils.create_shared_network_full',
50 return_value=mock.Mock()), \
51 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
52 'os_utils.get_or_create_image',
53 return_value=(mock.Mock(), 'image_id')), \
54 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
55 'os_utils.get_or_create_flavor',
56 return_value=(mock.Mock(), None)), \
57 self.assertRaises(Exception) as context:
58 CONST.__setattr__('tempest_use_custom_images', True)
59 CONST.__setattr__('tempest_use_custom_flavors', True)
60 conf_utils.create_tempest_resources()
61 msg = 'Failed to create flavor'
62 self.assertTrue(msg in context)
64 CONST.__setattr__('tempest_use_custom_images', True)
65 CONST.__setattr__('tempest_use_custom_flavors', False)
66 conf_utils.create_tempest_resources(use_custom_flavors=False)
67 msg = 'Failed to create flavor'
68 self.assertTrue(msg in context)
70 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
72 def create_tenant_user_and_tenant_ok(self, mock_logger_error):
73 with mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
74 'os_utils.get_keystone_client',
75 return_value=mock.Mock()), \
76 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
77 'os_utils.create_tenant',
78 return_value='test_tenant_id'):
79 conf_utils.create_tenant_user()
80 mock_logger_error.assert_not_called()
82 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
84 def create_tenant_user_and_user_ok(self, mock_logger_error):
85 with mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
86 'os_utils.get_keystone_client',
87 return_value=mock.Mock()), \
88 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
89 'os_utils.create_user',
90 return_value='test_user_id'):
91 conf_utils.create_tenant_user()
92 mock_logger_error.assert_not_called()
94 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
96 def create_tenant_user_and_tenant_failed(self, mock_logger_error):
97 with mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
98 'os_utils.get_keystone_client',
99 return_value=mock.Mock()), \
100 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
101 'os_utils.create_tenant',
103 conf_utils.create_tenant_user()
104 msg = ("Failed to create %s tenant"
105 % CONST.__getattribute__('tempest_identity_tenant_name'))
106 mock_logger_error.assert_any_call(msg)
108 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
110 def create_tenant_user_and_user_failed(self, mock_logger_error):
111 with mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
112 'os_utils.get_keystone_client',
113 return_value=mock.Mock()), \
114 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
115 'os_utils.create_user',
117 conf_utils.create_tenant_user()
118 msg = ("Failed to create %s user"
119 % CONST.__getattribute__('tempest_identity_user_name'))
120 mock_logger_error.assert_any_call(msg)
122 def test_get_verifier_id_missing_verifier(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_id(),
133 def test_get_verifier_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_id(),
145 def test_get_verifier_deployment_id_missing_rally(self):
146 CONST.__setattr__('tempest_deployment_name', 'test_deploy_name')
147 with mock.patch('functest.opnfv_tests.openstack.tempest.'
148 'conf_utils.subprocess.Popen') as mock_popen, \
149 self.assertRaises(Exception):
150 mock_stdout = mock.Mock()
151 attrs = {'stdout.readline.return_value': ''}
152 mock_stdout.configure_mock(**attrs)
153 mock_popen.return_value = mock_stdout
154 conf_utils.get_verifier_deployment_id(),
156 def test_get_verifier_deployment_id_default(self):
157 CONST.__setattr__('tempest_deployment_name', 'test_deploy_name')
158 with mock.patch('functest.opnfv_tests.openstack.tempest.'
159 'conf_utils.subprocess.Popen') as mock_popen:
160 mock_stdout = mock.Mock()
161 attrs = {'stdout.readline.return_value': 'test_deploy_id'}
162 mock_stdout.configure_mock(**attrs)
163 mock_popen.return_value = mock_stdout
165 self.assertEqual(conf_utils.get_verifier_deployment_id(),
168 def test_get_verifier_repo_dir_default(self):
169 with mock.patch('functest.opnfv_tests.openstack.tempest.'
170 'conf_utils.os.path.join',
171 return_value='test_verifier_repo_dir'), \
172 mock.patch('functest.opnfv_tests.openstack.tempest.'
173 'conf_utils.get_verifier_id') as m:
174 self.assertEqual(conf_utils.get_verifier_repo_dir(''),
175 'test_verifier_repo_dir')
176 self.assertTrue(m.called)
178 def test_get_verifier_deployment_dir_default(self):
179 with mock.patch('functest.opnfv_tests.openstack.tempest.'
180 'conf_utils.os.path.join',
181 return_value='test_verifier_repo_dir'), \
182 mock.patch('functest.opnfv_tests.openstack.tempest.'
183 'conf_utils.get_verifier_id') as m1, \
184 mock.patch('functest.opnfv_tests.openstack.tempest.'
185 'conf_utils.get_verifier_deployment_id') as m2:
186 self.assertEqual(conf_utils.get_verifier_deployment_dir('', ''),
187 'test_verifier_repo_dir')
188 self.assertTrue(m1.called)
189 self.assertTrue(m2.called)
191 def test_get_repo_tag_default(self):
192 mock_popen = mock.Mock()
193 attrs = {'stdout.readline.return_value': 'test_tag'}
194 mock_popen.configure_mock(**attrs)
196 with mock.patch('functest.opnfv_tests.openstack.tempest.'
197 'conf_utils.subprocess.Popen',
198 return_value=mock_popen):
199 self.assertEqual(conf_utils.get_repo_tag('test_repo'),
202 def test_backup_tempest_config_default(self):
203 with mock.patch('functest.opnfv_tests.openstack.tempest.'
204 'conf_utils.os.path.exists',
205 return_value=False), \
206 mock.patch('functest.opnfv_tests.openstack.tempest.'
207 'conf_utils.os.makedirs') as m1, \
208 mock.patch('functest.opnfv_tests.openstack.tempest.'
209 'conf_utils.shutil.copyfile') as m2:
210 conf_utils.backup_tempest_config('test_conf_file')
211 self.assertTrue(m1.called)
212 self.assertTrue(m2.called)
214 with mock.patch('functest.opnfv_tests.openstack.tempest.'
215 'conf_utils.os.path.exists',
216 return_value=True), \
217 mock.patch('functest.opnfv_tests.openstack.tempest.'
218 'conf_utils.shutil.copyfile') as m2:
219 conf_utils.backup_tempest_config('test_conf_file')
220 self.assertTrue(m2.called)
222 def test_configure_tempest_default(self):
223 with mock.patch('functest.opnfv_tests.openstack.tempest.'
224 'conf_utils.configure_verifier',
225 return_value='test_conf_file'), \
226 mock.patch('functest.opnfv_tests.openstack.tempest.'
227 'conf_utils.configure_tempest_update_params') as m1:
228 conf_utils.configure_tempest('test_dep_dir')
229 self.assertTrue(m1.called)
231 def test_configure_tempest_defcore_default(self):
232 img_flavor_dict = {'image_id': 'test_image_id',
233 'flavor_id': 'test_flavor_id',
234 'image_id_alt': 'test_image_alt_id',
235 'flavor_id_alt': 'test_flavor_alt_id'}
236 with mock.patch('functest.opnfv_tests.openstack.tempest.'
237 'conf_utils.configure_verifier',
238 return_value='test_conf_file'), \
239 mock.patch('functest.opnfv_tests.openstack.tempest.'
240 'conf_utils.configure_tempest_update_params'), \
241 mock.patch('functest.opnfv_tests.openstack.tempest.'
242 'conf_utils.ConfigParser.RawConfigParser.'
244 mock.patch('functest.opnfv_tests.openstack.tempest.'
245 'conf_utils.ConfigParser.RawConfigParser.'
247 mock.patch('functest.opnfv_tests.openstack.tempest.'
248 'conf_utils.ConfigParser.RawConfigParser.'
249 'write') as mwrite, \
250 mock.patch('__builtin__.open', mock.mock_open()), \
251 mock.patch('functest.opnfv_tests.openstack.tempest.'
252 'conf_utils.generate_test_accounts_file'), \
253 mock.patch('functest.opnfv_tests.openstack.tempest.'
254 'conf_utils.shutil.copyfile'):
255 conf_utils.configure_tempest_defcore('test_dep_dir',
257 mset.assert_any_call('compute', 'image_ref', 'test_image_id')
258 mset.assert_any_call('compute', 'image_ref_alt',
260 mset.assert_any_call('compute', 'flavor_ref', 'test_flavor_id')
261 mset.assert_any_call('compute', 'flavor_ref_alt',
262 'test_flavor_alt_id')
263 self.assertTrue(mread.called)
264 self.assertTrue(mwrite.called)
266 def test_generate_test_accounts_file_default(self):
267 with mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
268 'create_tenant_user',
269 return_value='test_tenant_id') as mock_create, \
270 mock.patch("__builtin__.open", mock.mock_open()), \
271 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
272 'yaml.dump') as mock_dump:
273 conf_utils.generate_test_accounts_file()
274 self.assertTrue(mock_create.called)
275 self.assertTrue(mock_dump.called)
277 def _test_missing_param(self, params, image_id, flavor_id):
278 with mock.patch('functest.opnfv_tests.openstack.tempest.'
279 'conf_utils.ConfigParser.RawConfigParser.'
281 mock.patch('functest.opnfv_tests.openstack.tempest.'
282 'conf_utils.ConfigParser.RawConfigParser.'
284 mock.patch('functest.opnfv_tests.openstack.tempest.'
285 'conf_utils.ConfigParser.RawConfigParser.'
286 'write') as mwrite, \
287 mock.patch('__builtin__.open', mock.mock_open()), \
288 mock.patch('functest.opnfv_tests.openstack.tempest.'
289 'conf_utils.backup_tempest_config'), \
290 mock.patch('functest.utils.functest_utils.yaml.safe_load',
291 return_value={'validation': {'ssh_timeout': 300}}):
292 CONST.__setattr__('OS_ENDPOINT_TYPE', None)
294 configure_tempest_update_params('test_conf_file',
297 mset.assert_any_call(params[0], params[1], params[2])
298 self.assertTrue(mread.called)
299 self.assertTrue(mwrite.called)
301 def test_configure_tempest_update_params_missing_image_id(self):
302 CONST.__setattr__('tempest_use_custom_images', True)
303 self._test_missing_param(('compute', 'image_ref',
304 'test_image_id'), 'test_image_id',
307 def test_configure_tempest_update_params_missing_image_id_alt(self):
308 CONST.__setattr__('tempest_use_custom_images', True)
309 conf_utils.IMAGE_ID_ALT = 'test_image_id_alt'
310 self._test_missing_param(('compute', 'image_ref_alt',
311 'test_image_id_alt'), None, None)
313 def test_configure_tempest_update_params_missing_flavor_id(self):
314 CONST.__setattr__('tempest_use_custom_flavors', True)
315 self._test_missing_param(('compute', 'flavor_ref',
316 'test_flavor_id'), None,
319 def test_configure_tempest_update_params_missing_flavor_id_alt(self):
320 CONST.__setattr__('tempest_use_custom_flavors', True)
321 conf_utils.FLAVOR_ID_ALT = 'test_flavor_id_alt'
322 self._test_missing_param(('compute', 'flavor_ref_alt',
323 'test_flavor_id_alt'), None,
326 def test_configure_verifier_missing_temp_conf_file(self):
327 with mock.patch('functest.opnfv_tests.openstack.tempest.'
328 'conf_utils.os.path.isfile',
329 return_value=False), \
330 mock.patch('functest.opnfv_tests.openstack.tempest.'
331 'conf_utils.ft_utils.execute_command') as mexe, \
332 self.assertRaises(Exception) as context:
333 conf_utils.configure_verifier('test_dep_dir')
334 mexe.assert_any_call("rally verify configure-verifier")
335 msg = ("Tempest configuration file 'test_dep_dir/tempest.conf'"
337 self.assertTrue(msg in context)
339 def test_configure_verifier_default(self):
340 with mock.patch('functest.opnfv_tests.openstack.tempest.'
341 'conf_utils.os.path.isfile',
342 return_value=True), \
343 mock.patch('functest.opnfv_tests.openstack.tempest.'
344 'conf_utils.ft_utils.execute_command') as mexe:
345 self.assertEqual(conf_utils.configure_verifier('test_dep_dir'),
346 'test_dep_dir/tempest.conf')
347 mexe.assert_any_call("rally verify configure-verifier "
351 if __name__ == "__main__":
352 logging.disable(logging.CRITICAL)
353 unittest.main(verbosity=2)