Merge "Modify how to disable logging in unit test."
[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 conf_utils
14 from functest.utils.constants import CONST
15
16
17 class OSTempestConfUtilsTesting(unittest.TestCase):
18
19     def test_create_tempest_resources_missing_network_dic(self):
20         with mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
21                         'os_utils.get_keystone_client',
22                         return_value=mock.Mock()), \
23             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
24                        'os_utils.create_tenant',
25                        return_value='test_tenant_id'), \
26             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
27                        'os_utils.create_user',
28                        return_value='test_user_id'), \
29             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
30                        'os_utils.create_shared_network_full',
31                        return_value=None), \
32                 self.assertRaises(Exception) as context:
33             conf_utils.create_tempest_resources()
34             msg = 'Failed to create private network'
35             self.assertTrue(msg in context)
36
37     def test_create_tempest_resources_missing_image(self):
38         with mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
39                         'os_utils.get_keystone_client',
40                         return_value=mock.Mock()), \
41             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
42                        'os_utils.create_tenant',
43                        return_value='test_tenant_id'), \
44             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
45                        'os_utils.create_user',
46                        return_value='test_user_id'), \
47             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
48                        'os_utils.create_shared_network_full',
49                        return_value=mock.Mock()), \
50             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
51                        'os_utils.get_or_create_image',
52                        return_value=(mock.Mock(), None)), \
53                 self.assertRaises(Exception) as context:
54
55             CONST.tempest_use_custom_images = True
56             conf_utils.create_tempest_resources()
57             msg = 'Failed to create image'
58             self.assertTrue(msg in context)
59
60             CONST.tempest_use_custom_images = False
61             conf_utils.create_tempest_resources(use_custom_images=True)
62             msg = 'Failed to create image'
63             self.assertTrue(msg in context)
64
65     def test_create_tempest_resources_missing_flavor(self):
66         with mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
67                         'os_utils.get_keystone_client',
68                         return_value=mock.Mock()), \
69             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
70                        'os_utils.create_tenant',
71                        return_value='test_tenant_id'), \
72             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
73                        'os_utils.create_user',
74                        return_value='test_user_id'), \
75             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
76                        'os_utils.create_shared_network_full',
77                        return_value=mock.Mock()), \
78             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
79                        'os_utils.get_or_create_image',
80                        return_value=(mock.Mock(), 'image_id')), \
81             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
82                        'os_utils.get_or_create_flavor',
83                        return_value=(mock.Mock(), None)), \
84                 self.assertRaises(Exception) as context:
85             CONST.tempest_use_custom_images = True
86             CONST.tempest_use_custom_flavors = True
87             conf_utils.create_tempest_resources()
88             msg = 'Failed to create flavor'
89             self.assertTrue(msg in context)
90
91             CONST.tempest_use_custom_images = True
92             CONST.tempest_use_custom_flavors = False
93             conf_utils.create_tempest_resources(use_custom_flavors=False)
94             msg = 'Failed to create flavor'
95             self.assertTrue(msg in context)
96
97     def test_get_verifier_id_missing_verifier(self):
98         CONST.tempest_deployment_name = 'test_deploy_name'
99         with mock.patch('functest.opnfv_tests.openstack.tempest.'
100                         'conf_utils.subprocess.Popen') as mock_popen, \
101                 self.assertRaises(Exception):
102             mock_stdout = mock.Mock()
103             attrs = {'stdout.readline.return_value': ''}
104             mock_stdout.configure_mock(**attrs)
105             mock_popen.return_value = mock_stdout
106             conf_utils.get_verifier_id(),
107
108     def test_get_verifier_id_default(self):
109         CONST.tempest_deployment_name = 'test_deploy_name'
110         with mock.patch('functest.opnfv_tests.openstack.tempest.'
111                         'conf_utils.subprocess.Popen') as mock_popen:
112             mock_stdout = mock.Mock()
113             attrs = {'stdout.readline.return_value': 'test_deploy_id'}
114             mock_stdout.configure_mock(**attrs)
115             mock_popen.return_value = mock_stdout
116
117             self.assertEqual(conf_utils.get_verifier_id(),
118                              'test_deploy_id')
119
120     def test_get_verifier_deployment_id_missing_rally(self):
121         CONST.rally_deployment_name = 'test_rally_deploy_name'
122         with mock.patch('functest.opnfv_tests.openstack.tempest.'
123                         'conf_utils.subprocess.Popen') as mock_popen, \
124                 self.assertRaises(Exception):
125             mock_stdout = mock.Mock()
126             attrs = {'stdout.readline.return_value': ''}
127             mock_stdout.configure_mock(**attrs)
128             mock_popen.return_value = mock_stdout
129             conf_utils.get_verifier_deployment_id(),
130
131     def test_get_verifier_deployment_id_default(self):
132         CONST.rally_deployment_name = 'test_rally_deploy_name'
133         with mock.patch('functest.opnfv_tests.openstack.tempest.'
134                         'conf_utils.subprocess.Popen') as mock_popen:
135             mock_stdout = mock.Mock()
136             attrs = {'stdout.readline.return_value': 'test_deploy_id'}
137             mock_stdout.configure_mock(**attrs)
138             mock_popen.return_value = mock_stdout
139
140             self.assertEqual(conf_utils.get_verifier_deployment_id(),
141                              'test_deploy_id')
142
143     def test_get_verifier_repo_dir_default(self):
144         with mock.patch('functest.opnfv_tests.openstack.tempest.'
145                         'conf_utils.os.path.join',
146                         return_value='test_verifier_repo_dir'), \
147             mock.patch('functest.opnfv_tests.openstack.tempest.'
148                        'conf_utils.get_verifier_id') as m:
149             self.assertEqual(conf_utils.get_verifier_repo_dir(''),
150                              'test_verifier_repo_dir')
151             self.assertTrue(m.called)
152
153     def test_get_verifier_deployment_dir_default(self):
154         with mock.patch('functest.opnfv_tests.openstack.tempest.'
155                         'conf_utils.os.path.join',
156                         return_value='test_verifier_repo_dir'), \
157             mock.patch('functest.opnfv_tests.openstack.tempest.'
158                        'conf_utils.get_verifier_id') as m1, \
159             mock.patch('functest.opnfv_tests.openstack.tempest.'
160                        'conf_utils.get_verifier_deployment_id') as m2:
161             self.assertEqual(conf_utils.get_verifier_deployment_dir('', ''),
162                              'test_verifier_repo_dir')
163             self.assertTrue(m1.called)
164             self.assertTrue(m2.called)
165
166     def test_get_repo_tag_default(self):
167         mock_popen = mock.Mock()
168         attrs = {'stdout.readline.return_value': 'test_tag'}
169         mock_popen.configure_mock(**attrs)
170
171         with mock.patch('functest.opnfv_tests.openstack.tempest.'
172                         'conf_utils.subprocess.Popen',
173                         return_value=mock_popen):
174             self.assertEqual(conf_utils.get_repo_tag('test_repo'),
175                              'test_tag')
176
177     def test_backup_tempest_config_default(self):
178         with mock.patch('functest.opnfv_tests.openstack.tempest.'
179                         'conf_utils.os.path.exists',
180                         return_value=False), \
181             mock.patch('functest.opnfv_tests.openstack.tempest.'
182                        'conf_utils.os.makedirs') as m1, \
183             mock.patch('functest.opnfv_tests.openstack.tempest.'
184                        'conf_utils.shutil.copyfile') as m2:
185             conf_utils.backup_tempest_config('test_conf_file')
186             self.assertTrue(m1.called)
187             self.assertTrue(m2.called)
188
189         with mock.patch('functest.opnfv_tests.openstack.tempest.'
190                         'conf_utils.os.path.exists',
191                         return_value=True), \
192             mock.patch('functest.opnfv_tests.openstack.tempest.'
193                        'conf_utils.shutil.copyfile') as m2:
194             conf_utils.backup_tempest_config('test_conf_file')
195             self.assertTrue(m2.called)
196
197     def test_configure_tempest_default(self):
198         with mock.patch('functest.opnfv_tests.openstack.tempest.'
199                         'conf_utils.configure_verifier',
200                         return_value='test_conf_file'), \
201             mock.patch('functest.opnfv_tests.openstack.tempest.'
202                        'conf_utils.configure_tempest_update_params') as m1, \
203             mock.patch('functest.opnfv_tests.openstack.tempest.'
204                        'conf_utils.configure_tempest_multisite_params') as m2:
205             conf_utils.configure_tempest('test_dep_dir',
206                                          MODE='feature_multisite')
207             self.assertTrue(m1.called)
208             self.assertTrue(m2.called)
209
210         with mock.patch('functest.opnfv_tests.openstack.tempest.'
211                         'conf_utils.configure_verifier',
212                         return_value='test_conf_file'), \
213             mock.patch('functest.opnfv_tests.openstack.tempest.'
214                        'conf_utils.configure_tempest_update_params') as m1:
215             conf_utils.configure_tempest('test_dep_dir')
216             self.assertTrue(m1.called)
217             self.assertTrue(m2.called)
218
219     def test_configure_tempest_defcore_default(self):
220         img_flavor_dict = {'image_id': 'test_image_id',
221                            'flavor_id': 'test_flavor_id',
222                            'image_id_alt': 'test_image_alt_id',
223                            'flavor_id_alt': 'test_flavor_alt_id'}
224         with mock.patch('functest.opnfv_tests.openstack.tempest.'
225                         'conf_utils.configure_verifier',
226                         return_value='test_conf_file'), \
227             mock.patch('functest.opnfv_tests.openstack.tempest.'
228                        'conf_utils.configure_tempest_update_params'), \
229             mock.patch('functest.opnfv_tests.openstack.tempest.'
230                        'conf_utils.ConfigParser.RawConfigParser.'
231                        'set') as mset, \
232             mock.patch('functest.opnfv_tests.openstack.tempest.'
233                        'conf_utils.ConfigParser.RawConfigParser.'
234                        'read') as mread, \
235             mock.patch('functest.opnfv_tests.openstack.tempest.'
236                        'conf_utils.ConfigParser.RawConfigParser.'
237                        'write') as mwrite, \
238             mock.patch('__builtin__.open', mock.mock_open()), \
239             mock.patch('functest.opnfv_tests.openstack.tempest.'
240                        'conf_utils.shutil.copyfile'):
241             CONST.dir_functest_test = 'test_dir'
242             CONST.refstack_tempest_conf_path = 'test_path'
243             conf_utils.configure_tempest_defcore('test_dep_dir',
244                                                  img_flavor_dict)
245             mset.assert_any_call('compute', 'image_ref', 'test_image_id')
246             mset.assert_any_call('compute', 'image_ref_alt',
247                                  'test_image_alt_id')
248             mset.assert_any_call('compute', 'flavor_ref', 'test_flavor_id')
249             mset.assert_any_call('compute', 'flavor_ref_alt',
250                                  'test_flavor_alt_id')
251             self.assertTrue(mread.called)
252             self.assertTrue(mwrite.called)
253
254     def _test_missing_param(self, params, image_id, flavor_id):
255         with mock.patch('functest.opnfv_tests.openstack.tempest.'
256                         'conf_utils.ConfigParser.RawConfigParser.'
257                         'set') as mset, \
258             mock.patch('functest.opnfv_tests.openstack.tempest.'
259                        'conf_utils.ConfigParser.RawConfigParser.'
260                        'read') as mread, \
261             mock.patch('functest.opnfv_tests.openstack.tempest.'
262                        'conf_utils.ConfigParser.RawConfigParser.'
263                        'write') as mwrite, \
264             mock.patch('__builtin__.open', mock.mock_open()), \
265             mock.patch('functest.opnfv_tests.openstack.tempest.'
266                        'conf_utils.backup_tempest_config'):
267             CONST.dir_functest_test = 'test_dir'
268             CONST.OS_ENDPOINT_TYPE = None
269             conf_utils.\
270                 configure_tempest_update_params('test_conf_file',
271                                                 IMAGE_ID=image_id,
272                                                 FLAVOR_ID=flavor_id)
273             mset.assert_any_call(params[0], params[1], params[2])
274             self.assertTrue(mread.called)
275             self.assertTrue(mwrite.called)
276
277     def test_configure_tempest_update_params_missing_image_id(self):
278             CONST.tempest_use_custom_images = True
279             self._test_missing_param(('compute', 'image_ref',
280                                       'test_image_id'), 'test_image_id',
281                                      None)
282
283     def test_configure_tempest_update_params_missing_image_id_alt(self):
284             CONST.tempest_use_custom_images = True
285             conf_utils.IMAGE_ID_ALT = 'test_image_id_alt'
286             self._test_missing_param(('compute', 'image_ref_alt',
287                                       'test_image_id_alt'), None, None)
288
289     def test_configure_tempest_update_params_missing_flavor_id(self):
290             CONST.tempest_use_custom_flavors = True
291             self._test_missing_param(('compute', 'flavor_ref',
292                                       'test_flavor_id'), None,
293                                      'test_flavor_id')
294
295     def test_configure_tempest_update_params_missing_flavor_id_alt(self):
296             CONST.tempest_use_custom_flavors = True
297             conf_utils.FLAVOR_ID_ALT = 'test_flavor_id_alt'
298             self._test_missing_param(('compute', 'flavor_ref_alt',
299                                       'test_flavor_id_alt'), None,
300                                      None)
301
302     def test_configure_verifier_missing_temp_conf_file(self):
303         with mock.patch('functest.opnfv_tests.openstack.tempest.'
304                         'conf_utils.os.path.isfile',
305                         return_value=False), \
306             mock.patch('functest.opnfv_tests.openstack.tempest.'
307                        'conf_utils.ft_utils.execute_command') as mexe, \
308                 self.assertRaises(Exception) as context:
309             conf_utils.configure_verifier('test_dep_dir')
310             mexe.assert_any_call("rally verify configure-verifier")
311             msg = ("Tempest configuration file 'test_dep_dir/tempest.conf'"
312                    " NOT found.")
313             self.assertTrue(msg in context)
314
315     def test_configure_verifier_default(self):
316         with mock.patch('functest.opnfv_tests.openstack.tempest.'
317                         'conf_utils.os.path.isfile',
318                         return_value=True), \
319             mock.patch('functest.opnfv_tests.openstack.tempest.'
320                        'conf_utils.ft_utils.execute_command') as mexe:
321             self.assertEqual(conf_utils.configure_verifier('test_dep_dir'),
322                              'test_dep_dir/tempest.conf')
323             mexe.assert_any_call("rally verify configure-verifier "
324                                  "--reconfigure")
325
326     def test_configure_tempest_multisite_params_without_fuel(self):
327         conf_utils.CI_INSTALLER_TYPE = 'not_fuel'
328         with mock.patch('functest.opnfv_tests.openstack.tempest.'
329                         'conf_utils.os_utils.get_endpoint',
330                         return_value='kingbird_endpoint_url'), \
331             mock.patch('functest.opnfv_tests.openstack.tempest.'
332                        'conf_utils.ConfigParser.RawConfigParser.'
333                        'set') as mset, \
334             mock.patch('functest.opnfv_tests.openstack.tempest.'
335                        'conf_utils.ConfigParser.RawConfigParser.'
336                        'read') as mread, \
337             mock.patch('functest.opnfv_tests.openstack.tempest.'
338                        'conf_utils.ConfigParser.RawConfigParser.'
339                        'add_section') as msection, \
340             mock.patch('functest.opnfv_tests.openstack.tempest.'
341                        'conf_utils.ConfigParser.RawConfigParser.'
342                        'write') as mwrite, \
343             mock.patch('__builtin__.open', mock.mock_open()), \
344             mock.patch('functest.opnfv_tests.openstack.tempest.'
345                        'conf_utils.backup_tempest_config'):
346
347             conf_utils.configure_tempest_multisite_params('test_conf_file')
348             msection.assert_any_call("kingbird")
349             mset.assert_any_call('service_available', 'kingbird', 'true')
350             mset.assert_any_call('kingbird', 'endpoint_type', 'publicURL')
351             mset.assert_any_call('kingbird', 'TIME_TO_SYNC', '120')
352             mset.assert_any_call('kingbird', 'endpoint_url',
353                                  'kingbird_endpoint_url')
354             self.assertTrue(mread.called)
355             self.assertTrue(mwrite.called)
356
357     def test_install_verifier_ext_default(self):
358         with mock.patch('functest.opnfv_tests.openstack.tempest.'
359                         'conf_utils.get_repo_tag',
360                         return_value='test_tag'), \
361             mock.patch('functest.opnfv_tests.openstack.tempest.'
362                        'conf_utils.ft_utils.'
363                        'execute_command_raise') as mexe:
364             conf_utils.install_verifier_ext('test_path')
365             cmd = ("rally verify add-verifier-ext --source test_path "
366                    "--version test_tag")
367             error_msg = ("Problem while adding verifier extension from"
368                          " test_path")
369             mexe.assert_called_once_with(cmd, error_msg=error_msg)
370
371 if __name__ == "__main__":
372     logging.disable(logging.CRITICAL)
373     unittest.main(verbosity=2)