Merge "Fix a typo in docstring"
[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 # pylint: disable=missing-docstring
9
10 import logging
11 import unittest
12
13 import mock
14
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
18
19
20 class OSTempestConfUtilsTesting(unittest.TestCase):
21
22     def setUp(self):
23         self.os_creds = OSCreds(
24             username='user', password='pass',
25             auth_url='http://foo.com:5000/v3', project_name='bar')
26
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',
32                 return_value=None)
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         tempest_resources = tempest.TempestResourcesManager(
37             os_creds=self.os_creds)
38         with self.assertRaises(Exception) as context:
39             tempest_resources.create()
40         msg = 'Failed to create private network'
41         self.assertTrue(msg in context.exception)
42
43     @mock.patch('snaps.openstack.utils.deploy_utils.create_project',
44                 return_value=mock.Mock())
45     @mock.patch('snaps.openstack.utils.deploy_utils.create_user',
46                 return_value=mock.Mock())
47     @mock.patch('snaps.openstack.utils.deploy_utils.create_network',
48                 return_value=mock.Mock())
49     @mock.patch('snaps.openstack.utils.deploy_utils.create_image',
50                 return_value=None)
51     def test_create_tempest_resources_missing_image(self, *mock_args):
52         tempest_resources = tempest.TempestResourcesManager(
53             os_creds=self.os_creds)
54
55         with self.assertRaises(Exception) as context:
56             tempest_resources.create()
57         msg = 'Failed to create image'
58         self.assertTrue(msg in context.exception, msg=str(context.exception))
59
60     @mock.patch('snaps.openstack.utils.deploy_utils.create_project',
61                 return_value=mock.Mock())
62     @mock.patch('snaps.openstack.utils.deploy_utils.create_user',
63                 return_value=mock.Mock())
64     @mock.patch('snaps.openstack.utils.deploy_utils.create_network',
65                 return_value=mock.Mock())
66     @mock.patch('snaps.openstack.utils.deploy_utils.create_image',
67                 return_value=mock.Mock())
68     @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create',
69                 return_value=None)
70     def test_create_tempest_resources_missing_flavor(self, *mock_args):
71         tempest_resources = tempest.TempestResourcesManager(
72             os_creds=self.os_creds)
73
74         CONST.__setattr__('tempest_use_custom_flavors', 'True')
75         with self.assertRaises(Exception) as context:
76             tempest_resources.create()
77         msg = 'Failed to create flavor'
78         self.assertTrue(msg in context.exception, msg=str(context.exception))
79
80         CONST.__setattr__('tempest_use_custom_flavors', 'False')
81         with self.assertRaises(Exception) as context:
82             tempest_resources.create(use_custom_flavors=True)
83         msg = 'Failed to create flavor'
84         self.assertTrue(msg in context.exception, msg=str(context.exception))
85
86     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
87                 '.logger.info')
88     @mock.patch('functest.utils.functest_utils.execute_command_raise')
89     @mock.patch('functest.utils.functest_utils.execute_command')
90     def test_create_rally_deployment(self, mock_exec, mock_exec_raise,
91                                      mock_logger_info):
92
93         conf_utils.create_rally_deployment()
94
95         cmd = "rally deployment destroy opnfv-rally"
96         error_msg = "Deployment %s does not exist." % \
97                     CONST.__getattribute__('rally_deployment_name')
98         mock_logger_info.assert_any_call("Creating Rally environment...")
99         mock_exec.assert_any_call(cmd, error_msg=error_msg, verbose=False)
100
101         cmd = "rally deployment create --fromenv --name="
102         cmd += CONST.__getattribute__('rally_deployment_name')
103         error_msg = "Problem while creating Rally deployment"
104         mock_exec_raise.assert_any_call(cmd, error_msg=error_msg)
105
106         cmd = "rally deployment check"
107         error_msg = ("OpenStack not responding or "
108                      "faulty Rally deployment.")
109         mock_exec_raise.assert_any_call(cmd, error_msg=error_msg)
110
111     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
112                 '.logger.debug')
113     def test_create_verifier(self, mock_logger_debug):
114         mock_popen = mock.Mock()
115         attrs = {'poll.return_value': None,
116                  'stdout.readline.return_value': '0'}
117         mock_popen.configure_mock(**attrs)
118
119         CONST.__setattr__('tempest_verifier_name', 'test_veifier_name')
120         with mock.patch('functest.utils.functest_utils.execute_command_raise',
121                         side_effect=Exception), \
122                 self.assertRaises(Exception):
123             conf_utils.create_verifier()
124             mock_logger_debug.assert_any_call("Tempest test_veifier_name"
125                                               " does not exist")
126
127     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
128                 'create_verifier', return_value=mock.Mock())
129     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
130                 'create_rally_deployment', return_value=mock.Mock())
131     def test_get_verifier_id_missing_verifier(self, mock_rally, mock_tempest):
132         CONST.__setattr__('tempest_verifier_name', 'test_verifier_name')
133         with mock.patch('functest.opnfv_tests.openstack.tempest.'
134                         'conf_utils.subprocess.Popen') as mock_popen, \
135                 self.assertRaises(Exception):
136             mock_stdout = mock.Mock()
137             attrs = {'stdout.readline.return_value': ''}
138             mock_stdout.configure_mock(**attrs)
139             mock_popen.return_value = mock_stdout
140             conf_utils.get_verifier_id()
141
142     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
143                 'create_verifier', return_value=mock.Mock())
144     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
145                 'create_rally_deployment', return_value=mock.Mock())
146     def test_get_verifier_id_default(self, mock_rally, mock_tempest):
147         CONST.__setattr__('tempest_verifier_name', 'test_verifier_name')
148         with mock.patch('functest.opnfv_tests.openstack.tempest.'
149                         'conf_utils.subprocess.Popen') as mock_popen:
150             mock_stdout = mock.Mock()
151             attrs = {'stdout.readline.return_value': 'test_deploy_id'}
152             mock_stdout.configure_mock(**attrs)
153             mock_popen.return_value = mock_stdout
154
155             self.assertEqual(conf_utils.get_verifier_id(),
156                              'test_deploy_id')
157
158     def test_get_verifier_deployment_id_missing_rally(self):
159         CONST.__setattr__('tempest_verifier_name', 'test_deploy_name')
160         with mock.patch('functest.opnfv_tests.openstack.tempest.'
161                         'conf_utils.subprocess.Popen') as mock_popen, \
162                 self.assertRaises(Exception):
163             mock_stdout = mock.Mock()
164             attrs = {'stdout.readline.return_value': ''}
165             mock_stdout.configure_mock(**attrs)
166             mock_popen.return_value = mock_stdout
167             conf_utils.get_verifier_deployment_id(),
168
169     def test_get_verifier_deployment_id_default(self):
170         CONST.__setattr__('tempest_verifier_name', 'test_deploy_name')
171         with mock.patch('functest.opnfv_tests.openstack.tempest.'
172                         'conf_utils.subprocess.Popen') as mock_popen:
173             mock_stdout = mock.Mock()
174             attrs = {'stdout.readline.return_value': 'test_deploy_id'}
175             mock_stdout.configure_mock(**attrs)
176             mock_popen.return_value = mock_stdout
177
178             self.assertEqual(conf_utils.get_verifier_deployment_id(),
179                              'test_deploy_id')
180
181     def test_get_verifier_repo_dir_default(self):
182         with mock.patch('functest.opnfv_tests.openstack.tempest.'
183                         'conf_utils.os.path.join',
184                         return_value='test_verifier_repo_dir'), \
185             mock.patch('functest.opnfv_tests.openstack.tempest.'
186                        'conf_utils.get_verifier_id') as m:
187             self.assertEqual(conf_utils.get_verifier_repo_dir(''),
188                              'test_verifier_repo_dir')
189             self.assertTrue(m.called)
190
191     def test_get_verifier_deployment_dir_default(self):
192         with mock.patch('functest.opnfv_tests.openstack.tempest.'
193                         'conf_utils.os.path.join',
194                         return_value='test_verifier_repo_dir'), \
195             mock.patch('functest.opnfv_tests.openstack.tempest.'
196                        'conf_utils.get_verifier_id') as m1, \
197             mock.patch('functest.opnfv_tests.openstack.tempest.'
198                        'conf_utils.get_verifier_deployment_id') as m2:
199             self.assertEqual(conf_utils.get_verifier_deployment_dir('', ''),
200                              'test_verifier_repo_dir')
201             self.assertTrue(m1.called)
202             self.assertTrue(m2.called)
203
204     def test_backup_tempest_config_default(self):
205         with mock.patch('functest.opnfv_tests.openstack.tempest.'
206                         'conf_utils.os.path.exists',
207                         return_value=False), \
208             mock.patch('functest.opnfv_tests.openstack.tempest.'
209                        'conf_utils.os.makedirs') as m1, \
210             mock.patch('functest.opnfv_tests.openstack.tempest.'
211                        'conf_utils.shutil.copyfile') as m2:
212             conf_utils.backup_tempest_config('test_conf_file')
213             self.assertTrue(m1.called)
214             self.assertTrue(m2.called)
215
216         with mock.patch('functest.opnfv_tests.openstack.tempest.'
217                         'conf_utils.os.path.exists',
218                         return_value=True), \
219             mock.patch('functest.opnfv_tests.openstack.tempest.'
220                        'conf_utils.shutil.copyfile') as m2:
221             conf_utils.backup_tempest_config('test_conf_file')
222             self.assertTrue(m2.called)
223
224     def test_configure_tempest_default(self):
225         with mock.patch('functest.opnfv_tests.openstack.tempest.'
226                         'conf_utils.configure_verifier',
227                         return_value='test_conf_file'), \
228             mock.patch('functest.opnfv_tests.openstack.tempest.'
229                        'conf_utils.configure_tempest_update_params') as m1:
230             conf_utils.configure_tempest('test_dep_dir')
231             self.assertTrue(m1.called)
232
233     def test_configure_tempest_defcore_default(self):
234         with mock.patch('functest.opnfv_tests.openstack.tempest.'
235                         'conf_utils.configure_verifier',
236                         return_value='test_conf_file'), \
237             mock.patch('functest.opnfv_tests.openstack.tempest.'
238                        'conf_utils.configure_tempest_update_params'), \
239             mock.patch('functest.opnfv_tests.openstack.tempest.'
240                        'conf_utils.ConfigParser.RawConfigParser.'
241                        'set') as mset, \
242             mock.patch('functest.opnfv_tests.openstack.tempest.'
243                        'conf_utils.ConfigParser.RawConfigParser.'
244                        'read') as mread, \
245             mock.patch('functest.opnfv_tests.openstack.tempest.'
246                        'conf_utils.ConfigParser.RawConfigParser.'
247                        'write') as mwrite, \
248             mock.patch('__builtin__.open', mock.mock_open()), \
249             mock.patch('functest.opnfv_tests.openstack.tempest.'
250                        'conf_utils.generate_test_accounts_file'), \
251             mock.patch('functest.opnfv_tests.openstack.tempest.'
252                        'conf_utils.shutil.copyfile'):
253             conf_utils.configure_tempest_defcore(
254                 'test_dep_dir', 'test_image_id', 'test_flavor_id',
255                 'test_image_alt_id', 'test_flavor_alt_id', 'test_tenant_id')
256             mset.assert_any_call('compute', 'image_ref', 'test_image_id')
257             mset.assert_any_call('compute', 'image_ref_alt',
258                                  'test_image_alt_id')
259             mset.assert_any_call('compute', 'flavor_ref', 'test_flavor_id')
260             mset.assert_any_call('compute', 'flavor_ref_alt',
261                                  'test_flavor_alt_id')
262             self.assertTrue(mread.called)
263             self.assertTrue(mwrite.called)
264
265     def test_generate_test_accounts_file_default(self):
266         with mock.patch("__builtin__.open", mock.mock_open()), \
267             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
268                        'yaml.dump') as mock_dump:
269             conf_utils.generate_test_accounts_file('test_tenant_id')
270             self.assertTrue(mock_dump.called)
271
272     def _test_missing_param(self, params, image_id, flavor_id):
273         with mock.patch('functest.opnfv_tests.openstack.tempest.'
274                         'conf_utils.ConfigParser.RawConfigParser.'
275                         'set') as mset, \
276             mock.patch('functest.opnfv_tests.openstack.tempest.'
277                        'conf_utils.ConfigParser.RawConfigParser.'
278                        'read') as mread, \
279             mock.patch('functest.opnfv_tests.openstack.tempest.'
280                        'conf_utils.ConfigParser.RawConfigParser.'
281                        'write') as mwrite, \
282             mock.patch('__builtin__.open', mock.mock_open()), \
283             mock.patch('functest.opnfv_tests.openstack.tempest.'
284                        'conf_utils.backup_tempest_config'), \
285             mock.patch('functest.utils.functest_utils.yaml.safe_load',
286                        return_value={'validation': {'ssh_timeout': 300}}):
287             CONST.__setattr__('OS_ENDPOINT_TYPE', None)
288             conf_utils.configure_tempest_update_params(
289                 'test_conf_file', image_id=image_id, flavor_id=flavor_id)
290             mset.assert_any_call(params[0], params[1], params[2])
291             self.assertTrue(mread.called)
292             self.assertTrue(mwrite.called)
293
294     def test_configure_tempest_update_params_missing_image_id(self):
295             self._test_missing_param(('compute', 'image_ref',
296                                       'test_image_id'), 'test_image_id',
297                                      None)
298
299     def test_configure_tempest_update_params_missing_image_id_alt(self):
300             conf_utils.IMAGE_ID_ALT = 'test_image_id_alt'
301             self._test_missing_param(('compute', 'image_ref_alt',
302                                       'test_image_id_alt'), None, None)
303
304     def test_configure_tempest_update_params_missing_flavor_id(self):
305             CONST.__setattr__('tempest_use_custom_flavors', 'True')
306             self._test_missing_param(('compute', 'flavor_ref',
307                                       'test_flavor_id'), None,
308                                      'test_flavor_id')
309
310     def test_configure_tempest_update_params_missing_flavor_id_alt(self):
311             CONST.__setattr__('tempest_use_custom_flavors', 'True')
312             conf_utils.FLAVOR_ID_ALT = 'test_flavor_id_alt'
313             self._test_missing_param(('compute', 'flavor_ref_alt',
314                                       'test_flavor_id_alt'), None,
315                                      None)
316
317     def test_configure_verifier_missing_temp_conf_file(self):
318         with mock.patch('functest.opnfv_tests.openstack.tempest.'
319                         'conf_utils.os.path.isfile',
320                         return_value=False), \
321             mock.patch('functest.opnfv_tests.openstack.tempest.'
322                        'conf_utils.ft_utils.execute_command') as mexe, \
323                 self.assertRaises(Exception) as context:
324             conf_utils.configure_verifier('test_dep_dir')
325             mexe.assert_any_call("rally verify configure-verifier")
326             msg = ("Tempest configuration file 'test_dep_dir/tempest.conf'"
327                    " NOT found.")
328             self.assertTrue(msg in context)
329
330     def test_configure_verifier_default(self):
331         with mock.patch('functest.opnfv_tests.openstack.tempest.'
332                         'conf_utils.os.path.isfile',
333                         return_value=True), \
334             mock.patch('functest.opnfv_tests.openstack.tempest.'
335                        'conf_utils.ft_utils.execute_command') as mexe:
336             self.assertEqual(conf_utils.configure_verifier('test_dep_dir'),
337                              'test_dep_dir/tempest.conf')
338             mexe.assert_any_call("rally verify configure-verifier "
339                                  "--reconfigure")
340
341
342 if __name__ == "__main__":
343     logging.disable(logging.CRITICAL)
344     unittest.main(verbosity=2)