Remove get_repo_tag
[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_backup_tempest_config_default(self):
161         with mock.patch('functest.opnfv_tests.openstack.tempest.'
162                         'conf_utils.shutil.copyfile') as m1:
163             conf_utils.backup_tempest_config('test_conf_file')
164             self.assertTrue(m1.called)
165
166     def test_configure_tempest_default(self):
167         with mock.patch('functest.opnfv_tests.openstack.tempest.'
168                         'conf_utils.configure_verifier',
169                         return_value='test_conf_file'), \
170             mock.patch('functest.opnfv_tests.openstack.tempest.'
171                        'conf_utils.configure_tempest_update_params') as m1:
172             conf_utils.configure_tempest('test_dep_dir')
173             self.assertTrue(m1.called)
174
175     def test_configure_tempest_defcore_default(self):
176         with mock.patch('functest.opnfv_tests.openstack.tempest.'
177                         'conf_utils.configure_verifier',
178                         return_value='test_conf_file'), \
179             mock.patch('functest.opnfv_tests.openstack.tempest.'
180                        'conf_utils.configure_tempest_update_params'), \
181             mock.patch('functest.opnfv_tests.openstack.tempest.'
182                        'conf_utils.ConfigParser.RawConfigParser.'
183                        'set') as mset, \
184             mock.patch('functest.opnfv_tests.openstack.tempest.'
185                        'conf_utils.ConfigParser.RawConfigParser.'
186                        'read') as mread, \
187             mock.patch('functest.opnfv_tests.openstack.tempest.'
188                        'conf_utils.ConfigParser.RawConfigParser.'
189                        'write') as mwrite, \
190             mock.patch('__builtin__.open', mock.mock_open()), \
191             mock.patch('functest.opnfv_tests.openstack.tempest.'
192                        'conf_utils.generate_test_accounts_file'), \
193             mock.patch('functest.opnfv_tests.openstack.tempest.'
194                        'conf_utils.shutil.copyfile'):
195             conf_utils.configure_tempest_defcore(
196                 'test_dep_dir', 'test_image_id', 'test_flavor_id',
197                 'test_image_alt_id', 'test_flavor_alt_id', 'test_tenant_id')
198             mset.assert_any_call('compute', 'image_ref', 'test_image_id')
199             mset.assert_any_call('compute', 'image_ref_alt',
200                                  'test_image_alt_id')
201             mset.assert_any_call('compute', 'flavor_ref', 'test_flavor_id')
202             mset.assert_any_call('compute', 'flavor_ref_alt',
203                                  'test_flavor_alt_id')
204             self.assertTrue(mread.called)
205             self.assertTrue(mwrite.called)
206
207     def test_generate_test_accounts_file_default(self):
208         with mock.patch("__builtin__.open", mock.mock_open()), \
209             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
210                        'yaml.dump') as mock_dump:
211             conf_utils.generate_test_accounts_file('test_tenant_id')
212             self.assertTrue(mock_dump.called)
213
214     def _test_missing_param(self, params, image_id, flavor_id):
215         with mock.patch('functest.opnfv_tests.openstack.tempest.'
216                         'conf_utils.ConfigParser.RawConfigParser.'
217                         'set') as mset, \
218             mock.patch('functest.opnfv_tests.openstack.tempest.'
219                        'conf_utils.ConfigParser.RawConfigParser.'
220                        'read') as mread, \
221             mock.patch('functest.opnfv_tests.openstack.tempest.'
222                        'conf_utils.ConfigParser.RawConfigParser.'
223                        'write') as mwrite, \
224             mock.patch('__builtin__.open', mock.mock_open()), \
225             mock.patch('functest.opnfv_tests.openstack.tempest.'
226                        'conf_utils.backup_tempest_config'), \
227             mock.patch('functest.utils.functest_utils.yaml.safe_load',
228                        return_value={'validation': {'ssh_timeout': 300}}):
229             CONST.__setattr__('OS_ENDPOINT_TYPE', None)
230             conf_utils.\
231                 configure_tempest_update_params('test_conf_file',
232                                                 image_id=image_id,
233                                                 flavor_id=flavor_id)
234             mset.assert_any_call(params[0], params[1], params[2])
235             self.assertTrue(mread.called)
236             self.assertTrue(mwrite.called)
237
238     def test_configure_tempest_update_params_missing_image_id(self):
239             CONST.__setattr__('tempest_use_custom_images', True)
240             self._test_missing_param(('compute', 'image_ref',
241                                       'test_image_id'), 'test_image_id',
242                                      None)
243
244     def test_configure_tempest_update_params_missing_image_id_alt(self):
245             CONST.__setattr__('tempest_use_custom_images', True)
246             conf_utils.IMAGE_ID_ALT = 'test_image_id_alt'
247             self._test_missing_param(('compute', 'image_ref_alt',
248                                       'test_image_id_alt'), None, None)
249
250     def test_configure_tempest_update_params_missing_flavor_id(self):
251             CONST.__setattr__('tempest_use_custom_flavors', True)
252             self._test_missing_param(('compute', 'flavor_ref',
253                                       'test_flavor_id'), None,
254                                      'test_flavor_id')
255
256     def test_configure_tempest_update_params_missing_flavor_id_alt(self):
257             CONST.__setattr__('tempest_use_custom_flavors', True)
258             conf_utils.FLAVOR_ID_ALT = 'test_flavor_id_alt'
259             self._test_missing_param(('compute', 'flavor_ref_alt',
260                                       'test_flavor_id_alt'), None,
261                                      None)
262
263     def test_configure_verifier_missing_temp_conf_file(self):
264         with mock.patch('functest.opnfv_tests.openstack.tempest.'
265                         'conf_utils.os.path.isfile',
266                         return_value=False), \
267             mock.patch('functest.opnfv_tests.openstack.tempest.'
268                        'conf_utils.ft_utils.execute_command') as mexe, \
269                 self.assertRaises(Exception) as context:
270             conf_utils.configure_verifier('test_dep_dir')
271             mexe.assert_any_call("rally verify configure-verifier")
272             msg = ("Tempest configuration file 'test_dep_dir/tempest.conf'"
273                    " NOT found.")
274             self.assertTrue(msg in context)
275
276     def test_configure_verifier_default(self):
277         with mock.patch('functest.opnfv_tests.openstack.tempest.'
278                         'conf_utils.os.path.isfile',
279                         return_value=True), \
280             mock.patch('functest.opnfv_tests.openstack.tempest.'
281                        'conf_utils.ft_utils.execute_command') as mexe:
282             self.assertEqual(conf_utils.configure_verifier('test_dep_dir'),
283                              'test_dep_dir/tempest.conf')
284             mexe.assert_any_call("rally verify configure-verifier "
285                                  "--reconfigure")
286
287
288 if __name__ == "__main__":
289     logging.disable(logging.CRITICAL)
290     unittest.main(verbosity=2)