Merge "Export env vars via os.environ in test_openstack_utils.py"
[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     # pylint: disable=too-many-public-methods
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_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)
43
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',
51                 return_value=None)
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)
56
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))
61
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',
71                 return_value=None)
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)
76
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))
82
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))
88
89     @staticmethod
90     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
91                 '.LOGGER.info')
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,
95                                      mock_logger_info):
96
97         conf_utils.create_rally_deployment()
98
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)
104
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)
109
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)
114
115     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
116                 '.LOGGER.debug')
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)
122
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"
129                                               " does not exist")
130
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()
146
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
160
161             self.assertEqual(conf_utils.get_verifier_id(),
162                              'test_deploy_id')
163
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()
174
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
183
184             self.assertEqual(conf_utils.get_verifier_deployment_id(),
185                              'test_deploy_id')
186
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)
196
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') \
205                 as mock_get_did:
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)
210
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)
222
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)
230
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')\
237                 as mock_upd:
238             conf_utils.configure_tempest('test_dep_dir')
239             self.assertTrue(mock_upd.called)
240
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.'
249                        'set') as mset, \
250             mock.patch('functest.opnfv_tests.openstack.tempest.'
251                        'conf_utils.ConfigParser.RawConfigParser.'
252                        'read') as mread, \
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_image_id', 'test_flavor_id',
263                 'test_image_alt_id', 'test_flavor_alt_id', 'test_tenant_id')
264             mset.assert_any_call('compute', 'image_ref', 'test_image_id')
265             mset.assert_any_call('compute', 'image_ref_alt',
266                                  'test_image_alt_id')
267             mset.assert_any_call('compute', 'flavor_ref', 'test_flavor_id')
268             mset.assert_any_call('compute', 'flavor_ref_alt',
269                                  'test_flavor_alt_id')
270             self.assertTrue(mread.called)
271             self.assertTrue(mwrite.called)
272
273     def test_gen_test_accounts_file_def(self):
274         with mock.patch("__builtin__.open", mock.mock_open()), \
275             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
276                        'yaml.dump') as mock_dump:
277             conf_utils.generate_test_accounts_file('test_tenant_id')
278             self.assertTrue(mock_dump.called)
279
280     def _test_missing_param(self, params, image_id, flavor_id):
281         with mock.patch('functest.opnfv_tests.openstack.tempest.'
282                         'conf_utils.ConfigParser.RawConfigParser.'
283                         'set') as mset, \
284             mock.patch('functest.opnfv_tests.openstack.tempest.'
285                        'conf_utils.ConfigParser.RawConfigParser.'
286                        'read') as mread, \
287             mock.patch('functest.opnfv_tests.openstack.tempest.'
288                        'conf_utils.ConfigParser.RawConfigParser.'
289                        'write') as mwrite, \
290             mock.patch('__builtin__.open', mock.mock_open()), \
291             mock.patch('functest.opnfv_tests.openstack.tempest.'
292                        'conf_utils.backup_tempest_config'), \
293             mock.patch('functest.utils.functest_utils.yaml.safe_load',
294                        return_value={'validation': {'ssh_timeout': 300}}):
295             CONST.__setattr__('OS_ENDPOINT_TYPE', None)
296             conf_utils.configure_tempest_update_params(
297                 'test_conf_file', image_id=image_id, flavor_id=flavor_id)
298             mset.assert_any_call(params[0], params[1], params[2])
299             self.assertTrue(mread.called)
300             self.assertTrue(mwrite.called)
301
302     def test_upd_missing_image_id(self):
303         self._test_missing_param(('compute', 'image_ref', 'test_image_id'),
304                                  'test_image_id', None)
305
306     def test_upd_missing_image_id_alt(self):
307         conf_utils.IMAGE_ID_ALT = 'test_image_id_alt'
308         self._test_missing_param(('compute', 'image_ref_alt',
309                                   'test_image_id_alt'), None, None)
310
311     def test_upd_missing_flavor_id(self):
312         CONST.__setattr__('tempest_use_custom_flavors', 'True')
313         self._test_missing_param(('compute', 'flavor_ref', 'test_flavor_id'),
314                                  None, 'test_flavor_id')
315
316     def test_upd_missing_flavor_id_alt(self):
317         CONST.__setattr__('tempest_use_custom_flavors', 'True')
318         conf_utils.FLAVOR_ID_ALT = 'test_flavor_id_alt'
319         self._test_missing_param(('compute', 'flavor_ref_alt',
320                                   'test_flavor_id_alt'), None, None)
321
322     def test_verif_missing_conf_file(self):
323         with mock.patch('functest.opnfv_tests.openstack.tempest.'
324                         'conf_utils.os.path.isfile',
325                         return_value=False), \
326             mock.patch('functest.opnfv_tests.openstack.tempest.'
327                        'conf_utils.ft_utils.execute_command') as mexe, \
328                 self.assertRaises(Exception) as context:
329             conf_utils.configure_verifier('test_dep_dir')
330             mexe.assert_any_call("rally verify configure-verifier")
331             msg = ("Tempest configuration file 'test_dep_dir/tempest.conf'"
332                    " NOT found.")
333             self.assertTrue(msg in context)
334
335     def test_configure_verifier_default(self):
336         with mock.patch('functest.opnfv_tests.openstack.tempest.'
337                         'conf_utils.os.path.isfile',
338                         return_value=True), \
339             mock.patch('functest.opnfv_tests.openstack.tempest.'
340                        'conf_utils.ft_utils.execute_command') as mexe:
341             self.assertEqual(conf_utils.configure_verifier('test_dep_dir'),
342                              'test_dep_dir/tempest.conf')
343             mexe.assert_any_call("rally verify configure-verifier "
344                                  "--reconfigure")
345
346
347 if __name__ == "__main__":
348     logging.disable(logging.CRITICAL)
349     unittest.main(verbosity=2)