Merge "Stop downloading openbaton image."
[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 test_get_verifier_id_missing_verifier(self):
92         CONST.__setattr__('tempest_deployment_name', 'test_deploy_name')
93         with mock.patch('functest.opnfv_tests.openstack.tempest.'
94                         'conf_utils.subprocess.Popen') as mock_popen, \
95                 self.assertRaises(Exception):
96             mock_stdout = mock.Mock()
97             attrs = {'stdout.readline.return_value': ''}
98             mock_stdout.configure_mock(**attrs)
99             mock_popen.return_value = mock_stdout
100             conf_utils.get_verifier_id(),
101
102     def test_get_verifier_id_default(self):
103         CONST.__setattr__('tempest_deployment_name', 'test_deploy_name')
104         with mock.patch('functest.opnfv_tests.openstack.tempest.'
105                         'conf_utils.subprocess.Popen') as mock_popen:
106             mock_stdout = mock.Mock()
107             attrs = {'stdout.readline.return_value': 'test_deploy_id'}
108             mock_stdout.configure_mock(**attrs)
109             mock_popen.return_value = mock_stdout
110
111             self.assertEqual(conf_utils.get_verifier_id(),
112                              'test_deploy_id')
113
114     def test_get_verifier_deployment_id_missing_rally(self):
115         CONST.__setattr__('tempest_deployment_name', 'test_deploy_name')
116         with mock.patch('functest.opnfv_tests.openstack.tempest.'
117                         'conf_utils.subprocess.Popen') as mock_popen, \
118                 self.assertRaises(Exception):
119             mock_stdout = mock.Mock()
120             attrs = {'stdout.readline.return_value': ''}
121             mock_stdout.configure_mock(**attrs)
122             mock_popen.return_value = mock_stdout
123             conf_utils.get_verifier_deployment_id(),
124
125     def test_get_verifier_deployment_id_default(self):
126         CONST.__setattr__('tempest_deployment_name', 'test_deploy_name')
127         with mock.patch('functest.opnfv_tests.openstack.tempest.'
128                         'conf_utils.subprocess.Popen') as mock_popen:
129             mock_stdout = mock.Mock()
130             attrs = {'stdout.readline.return_value': 'test_deploy_id'}
131             mock_stdout.configure_mock(**attrs)
132             mock_popen.return_value = mock_stdout
133
134             self.assertEqual(conf_utils.get_verifier_deployment_id(),
135                              'test_deploy_id')
136
137     def test_get_verifier_repo_dir_default(self):
138         with mock.patch('functest.opnfv_tests.openstack.tempest.'
139                         'conf_utils.os.path.join',
140                         return_value='test_verifier_repo_dir'), \
141             mock.patch('functest.opnfv_tests.openstack.tempest.'
142                        'conf_utils.get_verifier_id') as m:
143             self.assertEqual(conf_utils.get_verifier_repo_dir(''),
144                              'test_verifier_repo_dir')
145             self.assertTrue(m.called)
146
147     def test_get_verifier_deployment_dir_default(self):
148         with mock.patch('functest.opnfv_tests.openstack.tempest.'
149                         'conf_utils.os.path.join',
150                         return_value='test_verifier_repo_dir'), \
151             mock.patch('functest.opnfv_tests.openstack.tempest.'
152                        'conf_utils.get_verifier_id') as m1, \
153             mock.patch('functest.opnfv_tests.openstack.tempest.'
154                        'conf_utils.get_verifier_deployment_id') as m2:
155             self.assertEqual(conf_utils.get_verifier_deployment_dir('', ''),
156                              'test_verifier_repo_dir')
157             self.assertTrue(m1.called)
158             self.assertTrue(m2.called)
159
160     def test_get_repo_tag_default(self):
161         mock_popen = mock.Mock()
162         attrs = {'stdout.readline.return_value': 'test_tag'}
163         mock_popen.configure_mock(**attrs)
164
165         with mock.patch('functest.opnfv_tests.openstack.tempest.'
166                         'conf_utils.subprocess.Popen',
167                         return_value=mock_popen):
168             self.assertEqual(conf_utils.get_repo_tag('test_repo'),
169                              'test_tag')
170
171     def test_backup_tempest_config_default(self):
172         with mock.patch('functest.opnfv_tests.openstack.tempest.'
173                         'conf_utils.os.path.exists',
174                         return_value=False), \
175             mock.patch('functest.opnfv_tests.openstack.tempest.'
176                        'conf_utils.os.makedirs') as m1, \
177             mock.patch('functest.opnfv_tests.openstack.tempest.'
178                        'conf_utils.shutil.copyfile') as m2:
179             conf_utils.backup_tempest_config('test_conf_file')
180             self.assertTrue(m1.called)
181             self.assertTrue(m2.called)
182
183         with mock.patch('functest.opnfv_tests.openstack.tempest.'
184                         'conf_utils.os.path.exists',
185                         return_value=True), \
186             mock.patch('functest.opnfv_tests.openstack.tempest.'
187                        'conf_utils.shutil.copyfile') as m2:
188             conf_utils.backup_tempest_config('test_conf_file')
189             self.assertTrue(m2.called)
190
191     def test_configure_tempest_default(self):
192         with mock.patch('functest.opnfv_tests.openstack.tempest.'
193                         'conf_utils.configure_verifier',
194                         return_value='test_conf_file'), \
195             mock.patch('functest.opnfv_tests.openstack.tempest.'
196                        'conf_utils.configure_tempest_update_params') as m1:
197             conf_utils.configure_tempest('test_dep_dir')
198             self.assertTrue(m1.called)
199
200     def test_configure_tempest_defcore_default(self):
201         with mock.patch('functest.opnfv_tests.openstack.tempest.'
202                         'conf_utils.configure_verifier',
203                         return_value='test_conf_file'), \
204             mock.patch('functest.opnfv_tests.openstack.tempest.'
205                        'conf_utils.configure_tempest_update_params'), \
206             mock.patch('functest.opnfv_tests.openstack.tempest.'
207                        'conf_utils.ConfigParser.RawConfigParser.'
208                        'set') as mset, \
209             mock.patch('functest.opnfv_tests.openstack.tempest.'
210                        'conf_utils.ConfigParser.RawConfigParser.'
211                        'read') as mread, \
212             mock.patch('functest.opnfv_tests.openstack.tempest.'
213                        'conf_utils.ConfigParser.RawConfigParser.'
214                        'write') as mwrite, \
215             mock.patch('__builtin__.open', mock.mock_open()), \
216             mock.patch('functest.opnfv_tests.openstack.tempest.'
217                        'conf_utils.generate_test_accounts_file'), \
218             mock.patch('functest.opnfv_tests.openstack.tempest.'
219                        'conf_utils.shutil.copyfile'):
220             conf_utils.configure_tempest_defcore(
221                 'test_dep_dir', 'test_image_id', 'test_flavor_id',
222                 'test_image_alt_id', 'test_flavor_alt_id', 'test_tenant_id')
223             mset.assert_any_call('compute', 'image_ref', 'test_image_id')
224             mset.assert_any_call('compute', 'image_ref_alt',
225                                  'test_image_alt_id')
226             mset.assert_any_call('compute', 'flavor_ref', 'test_flavor_id')
227             mset.assert_any_call('compute', 'flavor_ref_alt',
228                                  'test_flavor_alt_id')
229             self.assertTrue(mread.called)
230             self.assertTrue(mwrite.called)
231
232     def test_generate_test_accounts_file_default(self):
233         with mock.patch("__builtin__.open", mock.mock_open()), \
234             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
235                        'yaml.dump') as mock_dump:
236             conf_utils.generate_test_accounts_file('test_tenant_id')
237             self.assertTrue(mock_dump.called)
238
239     def _test_missing_param(self, params, image_id, flavor_id):
240         with mock.patch('functest.opnfv_tests.openstack.tempest.'
241                         'conf_utils.ConfigParser.RawConfigParser.'
242                         'set') as mset, \
243             mock.patch('functest.opnfv_tests.openstack.tempest.'
244                        'conf_utils.ConfigParser.RawConfigParser.'
245                        'read') as mread, \
246             mock.patch('functest.opnfv_tests.openstack.tempest.'
247                        'conf_utils.ConfigParser.RawConfigParser.'
248                        'write') as mwrite, \
249             mock.patch('__builtin__.open', mock.mock_open()), \
250             mock.patch('functest.opnfv_tests.openstack.tempest.'
251                        'conf_utils.backup_tempest_config'), \
252             mock.patch('functest.utils.functest_utils.yaml.safe_load',
253                        return_value={'validation': {'ssh_timeout': 300}}):
254             CONST.__setattr__('OS_ENDPOINT_TYPE', None)
255             conf_utils.\
256                 configure_tempest_update_params('test_conf_file',
257                                                 image_id=image_id,
258                                                 flavor_id=flavor_id)
259             mset.assert_any_call(params[0], params[1], params[2])
260             self.assertTrue(mread.called)
261             self.assertTrue(mwrite.called)
262
263     def test_configure_tempest_update_params_missing_image_id(self):
264             CONST.__setattr__('tempest_use_custom_images', True)
265             self._test_missing_param(('compute', 'image_ref',
266                                       'test_image_id'), 'test_image_id',
267                                      None)
268
269     def test_configure_tempest_update_params_missing_image_id_alt(self):
270             CONST.__setattr__('tempest_use_custom_images', True)
271             conf_utils.IMAGE_ID_ALT = 'test_image_id_alt'
272             self._test_missing_param(('compute', 'image_ref_alt',
273                                       'test_image_id_alt'), None, None)
274
275     def test_configure_tempest_update_params_missing_flavor_id(self):
276             CONST.__setattr__('tempest_use_custom_flavors', True)
277             self._test_missing_param(('compute', 'flavor_ref',
278                                       'test_flavor_id'), None,
279                                      'test_flavor_id')
280
281     def test_configure_tempest_update_params_missing_flavor_id_alt(self):
282             CONST.__setattr__('tempest_use_custom_flavors', True)
283             conf_utils.FLAVOR_ID_ALT = 'test_flavor_id_alt'
284             self._test_missing_param(('compute', 'flavor_ref_alt',
285                                       'test_flavor_id_alt'), None,
286                                      None)
287
288     def test_configure_verifier_missing_temp_conf_file(self):
289         with mock.patch('functest.opnfv_tests.openstack.tempest.'
290                         'conf_utils.os.path.isfile',
291                         return_value=False), \
292             mock.patch('functest.opnfv_tests.openstack.tempest.'
293                        'conf_utils.ft_utils.execute_command') as mexe, \
294                 self.assertRaises(Exception) as context:
295             conf_utils.configure_verifier('test_dep_dir')
296             mexe.assert_any_call("rally verify configure-verifier")
297             msg = ("Tempest configuration file 'test_dep_dir/tempest.conf'"
298                    " NOT found.")
299             self.assertTrue(msg in context)
300
301     def test_configure_verifier_default(self):
302         with mock.patch('functest.opnfv_tests.openstack.tempest.'
303                         'conf_utils.os.path.isfile',
304                         return_value=True), \
305             mock.patch('functest.opnfv_tests.openstack.tempest.'
306                        'conf_utils.ft_utils.execute_command') as mexe:
307             self.assertEqual(conf_utils.configure_verifier('test_dep_dir'),
308                              'test_dep_dir/tempest.conf')
309             mexe.assert_any_call("rally verify configure-verifier "
310                                  "--reconfigure")
311
312
313 if __name__ == "__main__":
314     logging.disable(logging.CRITICAL)
315     unittest.main(verbosity=2)