f20a7e934ebceafb6fd0c414ed51722988f5a8de
[functest.git] / functest / tests / unit / openstack / tempest / test_conf_utils.py
1 #!/usr/bin/env python
2
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
7
8 import logging
9 import unittest
10
11 import mock
12
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
16
17
18 class OSTempestConfUtilsTesting(unittest.TestCase):
19
20     def setUp(self):
21         self.os_creds = OSCreds(
22             username='user', password='pass',
23             auth_url='http://foo.com:5000/v3', project_name='bar')
24
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',
30                 return_value=None)
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)
39
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',
47                 return_value=None)
48     def test_create_tempest_resources_missing_image(self, *mock_args):
49         tempest_resources = tempest.TempestResourcesManager(os_creds={})
50
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))
56
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))
62
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',
72                 return_value=None)
73     def test_create_tempest_resources_missing_flavor(self, *mock_args):
74         tempest_resources = tempest.TempestResourcesManager(
75             os_creds=self.os_creds)
76
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))
83
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))
90
91     def _get_rally_creds(self):
92         return {"type": "ExistingCloud",
93                 "admin": {"username": 'test_user_name',
94                           "password": 'test_password',
95                           "tenant": 'test_tenant'}}
96
97     @mock.patch('functest.utils.openstack_utils.get_credentials_for_rally')
98     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
99                 '.logger.info')
100     @mock.patch('functest.utils.functest_utils.execute_command_raise')
101     @mock.patch('functest.utils.functest_utils.execute_command')
102     def test_create_rally_deployment(self, mock_exec, mock_exec_raise,
103                                      mock_logger_info, mock_os_utils):
104
105         mock_os_utils.return_value = self._get_rally_creds()
106
107         conf_utils.create_rally_deployment()
108
109         cmd = "rally deployment destroy opnfv-rally"
110         error_msg = "Deployment %s does not exist." % \
111                     CONST.__getattribute__('rally_deployment_name')
112         mock_logger_info.assert_any_call("Creating Rally environment...")
113         mock_exec.assert_any_call(cmd, error_msg=error_msg, verbose=False)
114
115         cmd = "rally deployment create --file=rally_conf.json --name="
116         cmd += CONST.__getattribute__('rally_deployment_name')
117         error_msg = "Problem while creating Rally deployment"
118         mock_exec_raise.assert_any_call(cmd, error_msg=error_msg)
119
120         cmd = "rally deployment check"
121         error_msg = ("OpenStack not responding or "
122                      "faulty Rally deployment.")
123         mock_exec_raise.assert_any_call(cmd, error_msg=error_msg)
124
125     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
126                 '.logger.debug')
127     def test_create_verifier(self, mock_logger_debug):
128         mock_popen = mock.Mock()
129         attrs = {'poll.return_value': None,
130                  'stdout.readline.return_value': '0'}
131         mock_popen.configure_mock(**attrs)
132
133         CONST.__setattr__('tempest_verifier_name', 'test_veifier_name')
134         with mock.patch('functest.utils.functest_utils.execute_command_raise',
135                         side_effect=Exception), \
136                 self.assertRaises(Exception):
137             conf_utils.create_verifier()
138             mock_logger_debug.assert_any_call("Tempest test_veifier_name"
139                                               " does not exist")
140
141     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
142                 'create_verifier', return_value=mock.Mock())
143     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
144                 'create_rally_deployment', return_value=mock.Mock())
145     def test_get_verifier_id_missing_verifier(self, mock_rally, mock_tempest):
146         CONST.__setattr__('tempest_verifier_name', 'test_verifier_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_id()
155
156     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
157                 'create_verifier', return_value=mock.Mock())
158     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
159                 'create_rally_deployment', return_value=mock.Mock())
160     def test_get_verifier_id_default(self, mock_rally, mock_tempest):
161         CONST.__setattr__('tempest_verifier_name', 'test_verifier_name')
162         with mock.patch('functest.opnfv_tests.openstack.tempest.'
163                         'conf_utils.subprocess.Popen') as mock_popen:
164             mock_stdout = mock.Mock()
165             attrs = {'stdout.readline.return_value': 'test_deploy_id'}
166             mock_stdout.configure_mock(**attrs)
167             mock_popen.return_value = mock_stdout
168
169             self.assertEqual(conf_utils.get_verifier_id(),
170                              'test_deploy_id')
171
172     def test_get_verifier_deployment_id_missing_rally(self):
173         CONST.__setattr__('tempest_verifier_name', 'test_deploy_name')
174         with mock.patch('functest.opnfv_tests.openstack.tempest.'
175                         'conf_utils.subprocess.Popen') as mock_popen, \
176                 self.assertRaises(Exception):
177             mock_stdout = mock.Mock()
178             attrs = {'stdout.readline.return_value': ''}
179             mock_stdout.configure_mock(**attrs)
180             mock_popen.return_value = mock_stdout
181             conf_utils.get_verifier_deployment_id(),
182
183     def test_get_verifier_deployment_id_default(self):
184         CONST.__setattr__('tempest_verifier_name', 'test_deploy_name')
185         with mock.patch('functest.opnfv_tests.openstack.tempest.'
186                         'conf_utils.subprocess.Popen') as mock_popen:
187             mock_stdout = mock.Mock()
188             attrs = {'stdout.readline.return_value': 'test_deploy_id'}
189             mock_stdout.configure_mock(**attrs)
190             mock_popen.return_value = mock_stdout
191
192             self.assertEqual(conf_utils.get_verifier_deployment_id(),
193                              'test_deploy_id')
194
195     def test_get_verifier_repo_dir_default(self):
196         with mock.patch('functest.opnfv_tests.openstack.tempest.'
197                         'conf_utils.os.path.join',
198                         return_value='test_verifier_repo_dir'), \
199             mock.patch('functest.opnfv_tests.openstack.tempest.'
200                        'conf_utils.get_verifier_id') as m:
201             self.assertEqual(conf_utils.get_verifier_repo_dir(''),
202                              'test_verifier_repo_dir')
203             self.assertTrue(m.called)
204
205     def test_get_verifier_deployment_dir_default(self):
206         with mock.patch('functest.opnfv_tests.openstack.tempest.'
207                         'conf_utils.os.path.join',
208                         return_value='test_verifier_repo_dir'), \
209             mock.patch('functest.opnfv_tests.openstack.tempest.'
210                        'conf_utils.get_verifier_id') as m1, \
211             mock.patch('functest.opnfv_tests.openstack.tempest.'
212                        'conf_utils.get_verifier_deployment_id') as m2:
213             self.assertEqual(conf_utils.get_verifier_deployment_dir('', ''),
214                              'test_verifier_repo_dir')
215             self.assertTrue(m1.called)
216             self.assertTrue(m2.called)
217
218     def test_backup_tempest_config_default(self):
219         with mock.patch('functest.opnfv_tests.openstack.tempest.'
220                         'conf_utils.os.path.exists',
221                         return_value=False), \
222             mock.patch('functest.opnfv_tests.openstack.tempest.'
223                        'conf_utils.os.makedirs') as m1, \
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(m1.called)
228             self.assertTrue(m2.called)
229
230         with mock.patch('functest.opnfv_tests.openstack.tempest.'
231                         'conf_utils.os.path.exists',
232                         return_value=True), \
233             mock.patch('functest.opnfv_tests.openstack.tempest.'
234                        'conf_utils.shutil.copyfile') as m2:
235             conf_utils.backup_tempest_config('test_conf_file')
236             self.assertTrue(m2.called)
237
238     def test_configure_tempest_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') as m1:
244             conf_utils.configure_tempest('test_dep_dir')
245             self.assertTrue(m1.called)
246
247     def test_configure_tempest_defcore_default(self):
248         with mock.patch('functest.opnfv_tests.openstack.tempest.'
249                         'conf_utils.configure_verifier',
250                         return_value='test_conf_file'), \
251             mock.patch('functest.opnfv_tests.openstack.tempest.'
252                        'conf_utils.configure_tempest_update_params'), \
253             mock.patch('functest.opnfv_tests.openstack.tempest.'
254                        'conf_utils.ConfigParser.RawConfigParser.'
255                        'set') as mset, \
256             mock.patch('functest.opnfv_tests.openstack.tempest.'
257                        'conf_utils.ConfigParser.RawConfigParser.'
258                        'read') as mread, \
259             mock.patch('functest.opnfv_tests.openstack.tempest.'
260                        'conf_utils.ConfigParser.RawConfigParser.'
261                        'write') as mwrite, \
262             mock.patch('__builtin__.open', mock.mock_open()), \
263             mock.patch('functest.opnfv_tests.openstack.tempest.'
264                        'conf_utils.generate_test_accounts_file'), \
265             mock.patch('functest.opnfv_tests.openstack.tempest.'
266                        'conf_utils.shutil.copyfile'):
267             conf_utils.configure_tempest_defcore(
268                 'test_dep_dir', 'test_image_id', 'test_flavor_id',
269                 'test_image_alt_id', 'test_flavor_alt_id', 'test_tenant_id')
270             mset.assert_any_call('compute', 'image_ref', 'test_image_id')
271             mset.assert_any_call('compute', 'image_ref_alt',
272                                  'test_image_alt_id')
273             mset.assert_any_call('compute', 'flavor_ref', 'test_flavor_id')
274             mset.assert_any_call('compute', 'flavor_ref_alt',
275                                  'test_flavor_alt_id')
276             self.assertTrue(mread.called)
277             self.assertTrue(mwrite.called)
278
279     def test_generate_test_accounts_file_default(self):
280         with mock.patch("__builtin__.open", mock.mock_open()), \
281             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
282                        'yaml.dump') as mock_dump:
283             conf_utils.generate_test_accounts_file('test_tenant_id')
284             self.assertTrue(mock_dump.called)
285
286     def _test_missing_param(self, params, image_id, flavor_id):
287         with mock.patch('functest.opnfv_tests.openstack.tempest.'
288                         'conf_utils.ConfigParser.RawConfigParser.'
289                         'set') as mset, \
290             mock.patch('functest.opnfv_tests.openstack.tempest.'
291                        'conf_utils.ConfigParser.RawConfigParser.'
292                        'read') as mread, \
293             mock.patch('functest.opnfv_tests.openstack.tempest.'
294                        'conf_utils.ConfigParser.RawConfigParser.'
295                        'write') as mwrite, \
296             mock.patch('__builtin__.open', mock.mock_open()), \
297             mock.patch('functest.opnfv_tests.openstack.tempest.'
298                        'conf_utils.backup_tempest_config'), \
299             mock.patch('functest.utils.functest_utils.yaml.safe_load',
300                        return_value={'validation': {'ssh_timeout': 300}}):
301             CONST.__setattr__('OS_ENDPOINT_TYPE', None)
302             conf_utils.\
303                 configure_tempest_update_params('test_conf_file',
304                                                 image_id=image_id,
305                                                 flavor_id=flavor_id)
306             mset.assert_any_call(params[0], params[1], params[2])
307             self.assertTrue(mread.called)
308             self.assertTrue(mwrite.called)
309
310     def test_configure_tempest_update_params_missing_image_id(self):
311             CONST.__setattr__('tempest_use_custom_images', True)
312             self._test_missing_param(('compute', 'image_ref',
313                                       'test_image_id'), 'test_image_id',
314                                      None)
315
316     def test_configure_tempest_update_params_missing_image_id_alt(self):
317             CONST.__setattr__('tempest_use_custom_images', True)
318             conf_utils.IMAGE_ID_ALT = 'test_image_id_alt'
319             self._test_missing_param(('compute', 'image_ref_alt',
320                                       'test_image_id_alt'), None, None)
321
322     def test_configure_tempest_update_params_missing_flavor_id(self):
323             CONST.__setattr__('tempest_use_custom_flavors', True)
324             self._test_missing_param(('compute', 'flavor_ref',
325                                       'test_flavor_id'), None,
326                                      'test_flavor_id')
327
328     def test_configure_tempest_update_params_missing_flavor_id_alt(self):
329             CONST.__setattr__('tempest_use_custom_flavors', True)
330             conf_utils.FLAVOR_ID_ALT = 'test_flavor_id_alt'
331             self._test_missing_param(('compute', 'flavor_ref_alt',
332                                       'test_flavor_id_alt'), None,
333                                      None)
334
335     def test_configure_verifier_missing_temp_conf_file(self):
336         with mock.patch('functest.opnfv_tests.openstack.tempest.'
337                         'conf_utils.os.path.isfile',
338                         return_value=False), \
339             mock.patch('functest.opnfv_tests.openstack.tempest.'
340                        'conf_utils.ft_utils.execute_command') as mexe, \
341                 self.assertRaises(Exception) as context:
342             conf_utils.configure_verifier('test_dep_dir')
343             mexe.assert_any_call("rally verify configure-verifier")
344             msg = ("Tempest configuration file 'test_dep_dir/tempest.conf'"
345                    " NOT found.")
346             self.assertTrue(msg in context)
347
348     def test_configure_verifier_default(self):
349         with mock.patch('functest.opnfv_tests.openstack.tempest.'
350                         'conf_utils.os.path.isfile',
351                         return_value=True), \
352             mock.patch('functest.opnfv_tests.openstack.tempest.'
353                        'conf_utils.ft_utils.execute_command') as mexe:
354             self.assertEqual(conf_utils.configure_verifier('test_dep_dir'),
355                              'test_dep_dir/tempest.conf')
356             mexe.assert_any_call("rally verify configure-verifier "
357                                  "--reconfigure")
358
359
360 if __name__ == "__main__":
361     logging.disable(logging.CRITICAL)
362     unittest.main(verbosity=2)