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