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=invalid-name,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_tempest_resources_missing_network_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_tempest_resources_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_tempest_resources_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_verifier_id_missing_verifier(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_verifier_deployment_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_verifier_deployment_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_verifier_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 m:
193 self.assertEqual(conf_utils.get_verifier_repo_dir(''),
194 'test_verifier_repo_dir')
195 self.assertTrue(m.called)
197 def test_get_verifier_deployment_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 m1, \
203 mock.patch('functest.opnfv_tests.openstack.tempest.'
204 'conf_utils.get_verifier_deployment_id') as m2:
205 self.assertEqual(conf_utils.get_verifier_deployment_dir('', ''),
206 'test_verifier_repo_dir')
207 self.assertTrue(m1.called)
208 self.assertTrue(m2.called)
210 def test_backup_tempest_config_default(self):
211 with mock.patch('functest.opnfv_tests.openstack.tempest.'
212 'conf_utils.os.path.exists',
213 return_value=False), \
214 mock.patch('functest.opnfv_tests.openstack.tempest.'
215 'conf_utils.os.makedirs') as m1, \
216 mock.patch('functest.opnfv_tests.openstack.tempest.'
217 'conf_utils.shutil.copyfile') as m2:
218 conf_utils.backup_tempest_config('test_conf_file')
219 self.assertTrue(m1.called)
220 self.assertTrue(m2.called)
222 with mock.patch('functest.opnfv_tests.openstack.tempest.'
223 'conf_utils.os.path.exists',
224 return_value=True), \
225 mock.patch('functest.opnfv_tests.openstack.tempest.'
226 'conf_utils.shutil.copyfile') as m2:
227 conf_utils.backup_tempest_config('test_conf_file')
228 self.assertTrue(m2.called)
230 def test_configure_tempest_default(self):
231 with mock.patch('functest.opnfv_tests.openstack.tempest.'
232 'conf_utils.configure_verifier',
233 return_value='test_conf_file'), \
234 mock.patch('functest.opnfv_tests.openstack.tempest.'
235 'conf_utils.configure_tempest_update_params') as m1:
236 conf_utils.configure_tempest('test_dep_dir')
237 self.assertTrue(m1.called)
239 def test_configure_tempest_defcore_default(self):
240 with mock.patch('functest.opnfv_tests.openstack.tempest.'
241 'conf_utils.configure_verifier',
242 return_value='test_conf_file'), \
243 mock.patch('functest.opnfv_tests.openstack.tempest.'
244 'conf_utils.configure_tempest_update_params'), \
245 mock.patch('functest.opnfv_tests.openstack.tempest.'
246 'conf_utils.ConfigParser.RawConfigParser.'
248 mock.patch('functest.opnfv_tests.openstack.tempest.'
249 'conf_utils.ConfigParser.RawConfigParser.'
251 mock.patch('functest.opnfv_tests.openstack.tempest.'
252 'conf_utils.ConfigParser.RawConfigParser.'
253 'write') as mwrite, \
254 mock.patch('__builtin__.open', mock.mock_open()), \
255 mock.patch('functest.opnfv_tests.openstack.tempest.'
256 'conf_utils.generate_test_accounts_file'), \
257 mock.patch('functest.opnfv_tests.openstack.tempest.'
258 'conf_utils.shutil.copyfile'):
259 conf_utils.configure_tempest_defcore(
260 'test_dep_dir', 'test_image_id', 'test_flavor_id',
261 'test_image_alt_id', 'test_flavor_alt_id', 'test_tenant_id')
262 mset.assert_any_call('compute', 'image_ref', 'test_image_id')
263 mset.assert_any_call('compute', 'image_ref_alt',
265 mset.assert_any_call('compute', 'flavor_ref', 'test_flavor_id')
266 mset.assert_any_call('compute', 'flavor_ref_alt',
267 'test_flavor_alt_id')
268 self.assertTrue(mread.called)
269 self.assertTrue(mwrite.called)
271 def test_generate_test_accounts_file_default(self):
272 with mock.patch("__builtin__.open", mock.mock_open()), \
273 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
274 'yaml.dump') as mock_dump:
275 conf_utils.generate_test_accounts_file('test_tenant_id')
276 self.assertTrue(mock_dump.called)
278 def _test_missing_param(self, params, image_id, flavor_id):
279 with mock.patch('functest.opnfv_tests.openstack.tempest.'
280 'conf_utils.ConfigParser.RawConfigParser.'
282 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.'
287 'write') as mwrite, \
288 mock.patch('__builtin__.open', mock.mock_open()), \
289 mock.patch('functest.opnfv_tests.openstack.tempest.'
290 'conf_utils.backup_tempest_config'), \
291 mock.patch('functest.utils.functest_utils.yaml.safe_load',
292 return_value={'validation': {'ssh_timeout': 300}}):
293 CONST.__setattr__('OS_ENDPOINT_TYPE', None)
294 conf_utils.configure_tempest_update_params(
295 'test_conf_file', image_id=image_id, flavor_id=flavor_id)
296 mset.assert_any_call(params[0], params[1], params[2])
297 self.assertTrue(mread.called)
298 self.assertTrue(mwrite.called)
300 def test_configure_tempest_update_params_missing_image_id(self):
301 self._test_missing_param(('compute', 'image_ref', 'test_image_id'),
302 'test_image_id', None)
304 def test_configure_tempest_update_params_missing_image_id_alt(self):
305 conf_utils.IMAGE_ID_ALT = 'test_image_id_alt'
306 self._test_missing_param(('compute', 'image_ref_alt',
307 'test_image_id_alt'), None, None)
309 def test_configure_tempest_update_params_missing_flavor_id(self):
310 CONST.__setattr__('tempest_use_custom_flavors', 'True')
311 self._test_missing_param(('compute', 'flavor_ref', 'test_flavor_id'),
312 None, 'test_flavor_id')
314 def test_configure_tempest_update_params_missing_flavor_id_alt(self):
315 CONST.__setattr__('tempest_use_custom_flavors', 'True')
316 conf_utils.FLAVOR_ID_ALT = 'test_flavor_id_alt'
317 self._test_missing_param(('compute', 'flavor_ref_alt',
318 'test_flavor_id_alt'), None, None)
320 def test_configure_verifier_missing_temp_conf_file(self):
321 with mock.patch('functest.opnfv_tests.openstack.tempest.'
322 'conf_utils.os.path.isfile',
323 return_value=False), \
324 mock.patch('functest.opnfv_tests.openstack.tempest.'
325 'conf_utils.ft_utils.execute_command') as mexe, \
326 self.assertRaises(Exception) as context:
327 conf_utils.configure_verifier('test_dep_dir')
328 mexe.assert_any_call("rally verify configure-verifier")
329 msg = ("Tempest configuration file 'test_dep_dir/tempest.conf'"
331 self.assertTrue(msg in context)
333 def test_configure_verifier_default(self):
334 with mock.patch('functest.opnfv_tests.openstack.tempest.'
335 'conf_utils.os.path.isfile',
336 return_value=True), \
337 mock.patch('functest.opnfv_tests.openstack.tempest.'
338 'conf_utils.ft_utils.execute_command') as mexe:
339 self.assertEqual(conf_utils.configure_verifier('test_dep_dir'),
340 'test_dep_dir/tempest.conf')
341 mexe.assert_any_call("rally verify configure-verifier "
345 if __name__ == "__main__":
346 logging.disable(logging.CRITICAL)
347 unittest.main(verbosity=2)