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
8 # pylint: disable=missing-docstring
15 from functest.opnfv_tests.openstack.tempest import tempest, conf_utils
16 from functest.utils.constants import CONST
17 from snaps.openstack.os_credentials import OSCreds
20 class OSTempestConfUtilsTesting(unittest.TestCase):
21 # pylint: disable=too-many-public-methods
23 self.os_creds = OSCreds(
24 username='user', password='pass',
25 auth_url='http://foo.com:5000/v3', project_name='bar')
27 @mock.patch('snaps.openstack.utils.deploy_utils.create_project',
28 return_value=mock.Mock())
29 @mock.patch('snaps.openstack.utils.deploy_utils.create_user',
30 return_value=mock.Mock())
31 @mock.patch('snaps.openstack.utils.deploy_utils.create_network',
33 @mock.patch('snaps.openstack.utils.deploy_utils.create_image',
34 return_value=mock.Mock())
35 def test_create_res_missing_net_dic(self, *mock_args):
36 # pylint: disable=unused-argument
37 tempest_resources = tempest.TempestResourcesManager(
38 os_creds=self.os_creds)
39 with self.assertRaises(Exception) as context:
40 tempest_resources.create()
41 msg = 'Failed to create private network'
42 self.assertTrue(msg in context.exception)
44 @mock.patch('snaps.openstack.utils.deploy_utils.create_project',
45 return_value=mock.Mock())
46 @mock.patch('snaps.openstack.utils.deploy_utils.create_user',
47 return_value=mock.Mock())
48 @mock.patch('snaps.openstack.utils.deploy_utils.create_network',
49 return_value=mock.Mock())
50 @mock.patch('snaps.openstack.utils.deploy_utils.create_image',
52 def test_create_res_missing_image(self, *mock_args):
53 # pylint: disable=unused-argument
54 tempest_resources = tempest.TempestResourcesManager(
55 os_creds=self.os_creds)
57 with self.assertRaises(Exception) as context:
58 tempest_resources.create()
59 msg = 'Failed to create image'
60 self.assertTrue(msg in context.exception, msg=str(context.exception))
62 @mock.patch('snaps.openstack.utils.deploy_utils.create_project',
63 return_value=mock.Mock())
64 @mock.patch('snaps.openstack.utils.deploy_utils.create_user',
65 return_value=mock.Mock())
66 @mock.patch('snaps.openstack.utils.deploy_utils.create_network',
67 return_value=mock.Mock())
68 @mock.patch('snaps.openstack.utils.deploy_utils.create_image',
69 return_value=mock.Mock())
70 @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create',
72 def test_create_res_missing_flavor(self, *mock_args):
73 # pylint: disable=unused-argument
74 tempest_resources = tempest.TempestResourcesManager(
75 os_creds=self.os_creds)
77 CONST.__setattr__('tempest_use_custom_flavors', 'True')
78 with self.assertRaises(Exception) as context:
79 tempest_resources.create()
80 msg = 'Failed to create flavor'
81 self.assertTrue(msg in context.exception, msg=str(context.exception))
83 CONST.__setattr__('tempest_use_custom_flavors', 'False')
84 with self.assertRaises(Exception) as context:
85 tempest_resources.create(use_custom_flavors=True)
86 msg = 'Failed to create flavor'
87 self.assertTrue(msg in context.exception, msg=str(context.exception))
90 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
92 @mock.patch('functest.utils.functest_utils.execute_command_raise')
93 @mock.patch('functest.utils.functest_utils.execute_command')
94 def test_create_rally_deployment(mock_exec, mock_exec_raise,
97 conf_utils.create_rally_deployment()
99 cmd = "rally deployment destroy opnfv-rally"
100 error_msg = "Deployment %s does not exist." % \
101 CONST.__getattribute__('rally_deployment_name')
102 mock_logger_info.assert_any_call("Creating Rally environment...")
103 mock_exec.assert_any_call(cmd, error_msg=error_msg, verbose=False)
105 cmd = "rally deployment create --fromenv --name="
106 cmd += CONST.__getattribute__('rally_deployment_name')
107 error_msg = "Problem while creating Rally deployment"
108 mock_exec_raise.assert_any_call(cmd, error_msg=error_msg)
110 cmd = "rally deployment check"
111 error_msg = ("OpenStack not responding or "
112 "faulty Rally deployment.")
113 mock_exec_raise.assert_any_call(cmd, error_msg=error_msg)
115 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
117 def test_create_verifier(self, mock_logger_debug):
118 mock_popen = mock.Mock()
119 attrs = {'poll.return_value': None,
120 'stdout.readline.return_value': '0'}
121 mock_popen.configure_mock(**attrs)
123 CONST.__setattr__('tempest_verifier_name', 'test_veifier_name')
124 with mock.patch('functest.utils.functest_utils.execute_command_raise',
125 side_effect=Exception), \
126 self.assertRaises(Exception):
127 conf_utils.create_verifier()
128 mock_logger_debug.assert_any_call("Tempest test_veifier_name"
131 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
132 'create_verifier', return_value=mock.Mock())
133 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
134 'create_rally_deployment', return_value=mock.Mock())
135 def test_get_verif_id_missing_verif(self, mock_rally, mock_tempest):
136 # pylint: disable=unused-argument
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 # pylint: disable=unused-argument
153 CONST.__setattr__('tempest_verifier_name', 'test_verifier_name')
154 with mock.patch('functest.opnfv_tests.openstack.tempest.'
155 'conf_utils.subprocess.Popen') as mock_popen:
156 mock_stdout = mock.Mock()
157 attrs = {'stdout.readline.return_value': 'test_deploy_id'}
158 mock_stdout.configure_mock(**attrs)
159 mock_popen.return_value = mock_stdout
161 self.assertEqual(conf_utils.get_verifier_id(),
164 def test_get_depl_id_missing_rally(self):
165 CONST.__setattr__('tempest_verifier_name', 'test_deploy_name')
166 with mock.patch('functest.opnfv_tests.openstack.tempest.'
167 'conf_utils.subprocess.Popen') as mock_popen, \
168 self.assertRaises(Exception):
169 mock_stdout = mock.Mock()
170 attrs = {'stdout.readline.return_value': ''}
171 mock_stdout.configure_mock(**attrs)
172 mock_popen.return_value = mock_stdout
173 conf_utils.get_verifier_deployment_id()
175 def test_get_depl_id_default(self):
176 CONST.__setattr__('tempest_verifier_name', 'test_deploy_name')
177 with mock.patch('functest.opnfv_tests.openstack.tempest.'
178 'conf_utils.subprocess.Popen') as mock_popen:
179 mock_stdout = mock.Mock()
180 attrs = {'stdout.readline.return_value': 'test_deploy_id'}
181 mock_stdout.configure_mock(**attrs)
182 mock_popen.return_value = mock_stdout
184 self.assertEqual(conf_utils.get_verifier_deployment_id(),
187 def test_get_verif_repo_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 mock_get_id:
193 self.assertEqual(conf_utils.get_verifier_repo_dir(''),
194 'test_verifier_repo_dir')
195 self.assertTrue(mock_get_id.called)
197 def test_get_depl_dir_default(self):
198 with mock.patch('functest.opnfv_tests.openstack.tempest.'
199 'conf_utils.os.path.join',
200 return_value='test_verifier_repo_dir'), \
201 mock.patch('functest.opnfv_tests.openstack.tempest.'
202 'conf_utils.get_verifier_id') as mock_get_vid, \
203 mock.patch('functest.opnfv_tests.openstack.tempest.'
204 'conf_utils.get_verifier_deployment_id') \
206 self.assertEqual(conf_utils.get_verifier_deployment_dir('', ''),
207 'test_verifier_repo_dir')
208 self.assertTrue(mock_get_vid.called)
209 self.assertTrue(mock_get_did.called)
211 def test_backup_config_default(self):
212 with mock.patch('functest.opnfv_tests.openstack.tempest.'
213 'conf_utils.os.path.exists',
214 return_value=False), \
215 mock.patch('functest.opnfv_tests.openstack.tempest.'
216 'conf_utils.os.makedirs') as mock_makedirs, \
217 mock.patch('functest.opnfv_tests.openstack.tempest.'
218 'conf_utils.shutil.copyfile') as mock_copyfile:
219 conf_utils.backup_tempest_config('test_conf_file')
220 self.assertTrue(mock_makedirs.called)
221 self.assertTrue(mock_copyfile.called)
223 with mock.patch('functest.opnfv_tests.openstack.tempest.'
224 'conf_utils.os.path.exists',
225 return_value=True), \
226 mock.patch('functest.opnfv_tests.openstack.tempest.'
227 'conf_utils.shutil.copyfile') as mock_copyfile:
228 conf_utils.backup_tempest_config('test_conf_file')
229 self.assertTrue(mock_copyfile.called)
231 def test_conf_tempest_def(self):
232 with mock.patch('functest.opnfv_tests.openstack.tempest.'
233 'conf_utils.configure_verifier',
234 return_value='test_conf_file'), \
235 mock.patch('functest.opnfv_tests.openstack.tempest.'
236 'conf_utils.configure_tempest_update_params')\
238 conf_utils.configure_tempest('test_dep_dir')
239 self.assertTrue(mock_upd.called)
241 def test_conf_tempest_defcore_def(self):
242 with mock.patch('functest.opnfv_tests.openstack.tempest.'
243 'conf_utils.configure_verifier',
244 return_value='test_conf_file'), \
245 mock.patch('functest.opnfv_tests.openstack.tempest.'
246 'conf_utils.configure_tempest_update_params'), \
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.'
253 mock.patch('functest.opnfv_tests.openstack.tempest.'
254 'conf_utils.ConfigParser.RawConfigParser.'
255 'write') as mwrite, \
256 mock.patch('__builtin__.open', mock.mock_open()), \
257 mock.patch('functest.opnfv_tests.openstack.tempest.'
258 'conf_utils.generate_test_accounts_file'), \
259 mock.patch('functest.opnfv_tests.openstack.tempest.'
260 'conf_utils.shutil.copyfile'):
261 conf_utils.configure_tempest_defcore(
262 'test_dep_dir', 'test_network_name', 'test_image_id',
263 'test_flavor_id', 'test_image_alt_id', 'test_flavor_alt_id',
265 mset.assert_any_call('compute', 'image_ref', 'test_image_id')
266 mset.assert_any_call('compute', 'image_ref_alt',
268 mset.assert_any_call('compute', 'flavor_ref', 'test_flavor_id')
269 mset.assert_any_call('compute', 'flavor_ref_alt',
270 'test_flavor_alt_id')
271 self.assertTrue(mread.called)
272 self.assertTrue(mwrite.called)
274 def test_gen_test_accounts_file_def(self):
275 with mock.patch("__builtin__.open", mock.mock_open()), \
276 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
277 'yaml.dump') as mock_dump:
278 conf_utils.generate_test_accounts_file('test_tenant_id')
279 self.assertTrue(mock_dump.called)
281 def _test_missing_param(self, params, image_id, flavor_id):
282 with mock.patch('functest.opnfv_tests.openstack.tempest.'
283 'conf_utils.ConfigParser.RawConfigParser.'
285 mock.patch('functest.opnfv_tests.openstack.tempest.'
286 'conf_utils.ConfigParser.RawConfigParser.'
288 mock.patch('functest.opnfv_tests.openstack.tempest.'
289 'conf_utils.ConfigParser.RawConfigParser.'
290 'write') as mwrite, \
291 mock.patch('__builtin__.open', mock.mock_open()), \
292 mock.patch('functest.opnfv_tests.openstack.tempest.'
293 'conf_utils.backup_tempest_config'), \
294 mock.patch('functest.utils.functest_utils.yaml.safe_load',
295 return_value={'validation': {'ssh_timeout': 300}}):
296 CONST.__setattr__('OS_ENDPOINT_TYPE', None)
297 conf_utils.configure_tempest_update_params(
298 'test_conf_file', image_id=image_id, flavor_id=flavor_id)
299 mset.assert_any_call(params[0], params[1], params[2])
300 self.assertTrue(mread.called)
301 self.assertTrue(mwrite.called)
303 def test_upd_missing_image_id(self):
304 self._test_missing_param(('compute', 'image_ref', 'test_image_id'),
305 'test_image_id', None)
307 def test_upd_missing_image_id_alt(self):
308 conf_utils.IMAGE_ID_ALT = 'test_image_id_alt'
309 self._test_missing_param(('compute', 'image_ref_alt',
310 'test_image_id_alt'), None, None)
312 def test_upd_missing_flavor_id(self):
313 CONST.__setattr__('tempest_use_custom_flavors', 'True')
314 self._test_missing_param(('compute', 'flavor_ref', 'test_flavor_id'),
315 None, 'test_flavor_id')
317 def test_upd_missing_flavor_id_alt(self):
318 CONST.__setattr__('tempest_use_custom_flavors', 'True')
319 conf_utils.FLAVOR_ID_ALT = 'test_flavor_id_alt'
320 self._test_missing_param(('compute', 'flavor_ref_alt',
321 'test_flavor_id_alt'), None, None)
323 def test_verif_missing_conf_file(self):
324 with mock.patch('functest.opnfv_tests.openstack.tempest.'
325 'conf_utils.os.path.isfile',
326 return_value=False), \
327 mock.patch('functest.opnfv_tests.openstack.tempest.'
328 'conf_utils.ft_utils.execute_command') as mexe, \
329 self.assertRaises(Exception) as context:
330 conf_utils.configure_verifier('test_dep_dir')
331 mexe.assert_any_call("rally verify configure-verifier")
332 msg = ("Tempest configuration file 'test_dep_dir/tempest.conf'"
334 self.assertTrue(msg in context)
336 def test_configure_verifier_default(self):
337 with mock.patch('functest.opnfv_tests.openstack.tempest.'
338 'conf_utils.os.path.isfile',
339 return_value=True), \
340 mock.patch('functest.opnfv_tests.openstack.tempest.'
341 'conf_utils.ft_utils.execute_command') as mexe:
342 self.assertEqual(conf_utils.configure_verifier('test_dep_dir'),
343 'test_dep_dir/tempest.conf')
344 mexe.assert_any_call("rally verify configure-verifier "
348 if __name__ == "__main__":
349 logging.disable(logging.CRITICAL)
350 unittest.main(verbosity=2)