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 CONST.__setattr__('tempest_use_custom_imagess', True)
52 with self.assertRaises(Exception) as context:
53 tempest_resources.create()
54 msg = 'Failed to create image'
55 self.assertTrue(msg in context.exception, msg=str(context.exception))
57 CONST.__setattr__('tempest_use_custom_imagess', False)
58 with self.assertRaises(Exception) as context:
59 tempest_resources.create(use_custom_images=True)
60 msg = 'Failed to create image'
61 self.assertTrue(msg in context.exception, msg=str(context.exception))
63 @mock.patch('snaps.openstack.utils.deploy_utils.create_project',
64 return_value=mock.Mock())
65 @mock.patch('snaps.openstack.utils.deploy_utils.create_user',
66 return_value=mock.Mock())
67 @mock.patch('snaps.openstack.utils.deploy_utils.create_network',
68 return_value=mock.Mock())
69 @mock.patch('snaps.openstack.utils.deploy_utils.create_image',
70 return_value=mock.Mock())
71 @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create',
73 def test_create_tempest_resources_missing_flavor(self, *mock_args):
74 tempest_resources = tempest.TempestResourcesManager(
75 os_creds=self.os_creds)
77 CONST.__setattr__('tempest_use_custom_images', True)
78 CONST.__setattr__('tempest_use_custom_flavors', True)
79 with self.assertRaises(Exception) as context:
80 tempest_resources.create()
81 msg = 'Failed to create flavor'
82 self.assertTrue(msg in context.exception, msg=str(context.exception))
84 CONST.__setattr__('tempest_use_custom_images', True)
85 CONST.__setattr__('tempest_use_custom_flavors', False)
86 with self.assertRaises(Exception) as context:
87 tempest_resources.create(use_custom_flavors=True)
88 msg = 'Failed to create flavor'
89 self.assertTrue(msg in context.exception, msg=str(context.exception))
91 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
93 @mock.patch('functest.utils.functest_utils.execute_command_raise')
94 @mock.patch('functest.utils.functest_utils.execute_command')
95 def test_create_rally_deployment(self, mock_exec, mock_exec_raise,
98 conf_utils.create_rally_deployment()
100 cmd = "rally deployment destroy opnfv-rally"
101 error_msg = "Deployment %s does not exist." % \
102 CONST.__getattribute__('rally_deployment_name')
103 mock_logger_info.assert_any_call("Creating Rally environment...")
104 mock_exec.assert_any_call(cmd, error_msg=error_msg, verbose=False)
106 cmd = "rally deployment create --fromenv --name="
107 cmd += CONST.__getattribute__('rally_deployment_name')
108 error_msg = "Problem while creating Rally deployment"
109 mock_exec_raise.assert_any_call(cmd, error_msg=error_msg)
111 cmd = "rally deployment check"
112 error_msg = ("OpenStack not responding or "
113 "faulty Rally deployment.")
114 mock_exec_raise.assert_any_call(cmd, error_msg=error_msg)
116 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
118 def test_create_verifier(self, mock_logger_debug):
119 mock_popen = mock.Mock()
120 attrs = {'poll.return_value': None,
121 'stdout.readline.return_value': '0'}
122 mock_popen.configure_mock(**attrs)
124 CONST.__setattr__('tempest_verifier_name', 'test_veifier_name')
125 with mock.patch('functest.utils.functest_utils.execute_command_raise',
126 side_effect=Exception), \
127 self.assertRaises(Exception):
128 conf_utils.create_verifier()
129 mock_logger_debug.assert_any_call("Tempest test_veifier_name"
132 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
133 'create_verifier', return_value=mock.Mock())
134 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
135 'create_rally_deployment', return_value=mock.Mock())
136 def test_get_verifier_id_missing_verifier(self, mock_rally, mock_tempest):
137 CONST.__setattr__('tempest_verifier_name', 'test_verifier_name')
138 with mock.patch('functest.opnfv_tests.openstack.tempest.'
139 'conf_utils.subprocess.Popen') as mock_popen, \
140 self.assertRaises(Exception):
141 mock_stdout = mock.Mock()
142 attrs = {'stdout.readline.return_value': ''}
143 mock_stdout.configure_mock(**attrs)
144 mock_popen.return_value = mock_stdout
145 conf_utils.get_verifier_id()
147 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
148 'create_verifier', return_value=mock.Mock())
149 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
150 'create_rally_deployment', return_value=mock.Mock())
151 def test_get_verifier_id_default(self, mock_rally, mock_tempest):
152 CONST.__setattr__('tempest_verifier_name', 'test_verifier_name')
153 with mock.patch('functest.opnfv_tests.openstack.tempest.'
154 'conf_utils.subprocess.Popen') as mock_popen:
155 mock_stdout = mock.Mock()
156 attrs = {'stdout.readline.return_value': 'test_deploy_id'}
157 mock_stdout.configure_mock(**attrs)
158 mock_popen.return_value = mock_stdout
160 self.assertEqual(conf_utils.get_verifier_id(),
163 def test_get_verifier_deployment_id_missing_rally(self):
164 CONST.__setattr__('tempest_verifier_name', 'test_deploy_name')
165 with mock.patch('functest.opnfv_tests.openstack.tempest.'
166 'conf_utils.subprocess.Popen') as mock_popen, \
167 self.assertRaises(Exception):
168 mock_stdout = mock.Mock()
169 attrs = {'stdout.readline.return_value': ''}
170 mock_stdout.configure_mock(**attrs)
171 mock_popen.return_value = mock_stdout
172 conf_utils.get_verifier_deployment_id(),
174 def test_get_verifier_deployment_id_default(self):
175 CONST.__setattr__('tempest_verifier_name', 'test_deploy_name')
176 with mock.patch('functest.opnfv_tests.openstack.tempest.'
177 'conf_utils.subprocess.Popen') as mock_popen:
178 mock_stdout = mock.Mock()
179 attrs = {'stdout.readline.return_value': 'test_deploy_id'}
180 mock_stdout.configure_mock(**attrs)
181 mock_popen.return_value = mock_stdout
183 self.assertEqual(conf_utils.get_verifier_deployment_id(),
186 def test_get_verifier_repo_dir_default(self):
187 with mock.patch('functest.opnfv_tests.openstack.tempest.'
188 'conf_utils.os.path.join',
189 return_value='test_verifier_repo_dir'), \
190 mock.patch('functest.opnfv_tests.openstack.tempest.'
191 'conf_utils.get_verifier_id') as m:
192 self.assertEqual(conf_utils.get_verifier_repo_dir(''),
193 'test_verifier_repo_dir')
194 self.assertTrue(m.called)
196 def test_get_verifier_deployment_dir_default(self):
197 with mock.patch('functest.opnfv_tests.openstack.tempest.'
198 'conf_utils.os.path.join',
199 return_value='test_verifier_repo_dir'), \
200 mock.patch('functest.opnfv_tests.openstack.tempest.'
201 'conf_utils.get_verifier_id') as m1, \
202 mock.patch('functest.opnfv_tests.openstack.tempest.'
203 'conf_utils.get_verifier_deployment_id') as m2:
204 self.assertEqual(conf_utils.get_verifier_deployment_dir('', ''),
205 'test_verifier_repo_dir')
206 self.assertTrue(m1.called)
207 self.assertTrue(m2.called)
209 def test_backup_tempest_config_default(self):
210 with mock.patch('functest.opnfv_tests.openstack.tempest.'
211 'conf_utils.os.path.exists',
212 return_value=False), \
213 mock.patch('functest.opnfv_tests.openstack.tempest.'
214 'conf_utils.os.makedirs') as m1, \
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(m1.called)
219 self.assertTrue(m2.called)
221 with mock.patch('functest.opnfv_tests.openstack.tempest.'
222 'conf_utils.os.path.exists',
223 return_value=True), \
224 mock.patch('functest.opnfv_tests.openstack.tempest.'
225 'conf_utils.shutil.copyfile') as m2:
226 conf_utils.backup_tempest_config('test_conf_file')
227 self.assertTrue(m2.called)
229 def test_configure_tempest_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') as m1:
235 conf_utils.configure_tempest('test_dep_dir')
236 self.assertTrue(m1.called)
238 def test_configure_tempest_defcore_default(self):
239 with mock.patch('functest.opnfv_tests.openstack.tempest.'
240 'conf_utils.configure_verifier',
241 return_value='test_conf_file'), \
242 mock.patch('functest.opnfv_tests.openstack.tempest.'
243 'conf_utils.configure_tempest_update_params'), \
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.'
250 mock.patch('functest.opnfv_tests.openstack.tempest.'
251 'conf_utils.ConfigParser.RawConfigParser.'
252 'write') as mwrite, \
253 mock.patch('__builtin__.open', mock.mock_open()), \
254 mock.patch('functest.opnfv_tests.openstack.tempest.'
255 'conf_utils.generate_test_accounts_file'), \
256 mock.patch('functest.opnfv_tests.openstack.tempest.'
257 'conf_utils.shutil.copyfile'):
258 conf_utils.configure_tempest_defcore(
259 'test_dep_dir', 'test_image_id', 'test_flavor_id',
260 'test_image_alt_id', 'test_flavor_alt_id', 'test_tenant_id')
261 mset.assert_any_call('compute', 'image_ref', 'test_image_id')
262 mset.assert_any_call('compute', 'image_ref_alt',
264 mset.assert_any_call('compute', 'flavor_ref', 'test_flavor_id')
265 mset.assert_any_call('compute', 'flavor_ref_alt',
266 'test_flavor_alt_id')
267 self.assertTrue(mread.called)
268 self.assertTrue(mwrite.called)
270 def test_generate_test_accounts_file_default(self):
271 with mock.patch("__builtin__.open", mock.mock_open()), \
272 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
273 'yaml.dump') as mock_dump:
274 conf_utils.generate_test_accounts_file('test_tenant_id')
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)