Merge "Create subnet and net within the same project"
[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
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_tempest_resources_missing_network_dic(self, *mock_args):
36         tempest_resources = tempest.TempestResourcesManager(os_creds={})
37         with self.assertRaises(Exception) as context:
38             tempest_resources.create()
39         msg = 'Failed to create private network'
40         self.assertTrue(msg in context.exception)
41
42     @mock.patch('snaps.openstack.utils.deploy_utils.create_project',
43                 return_value=mock.Mock())
44     @mock.patch('snaps.openstack.utils.deploy_utils.create_user',
45                 return_value=mock.Mock())
46     @mock.patch('snaps.openstack.utils.deploy_utils.create_network',
47                 return_value=mock.Mock())
48     @mock.patch('snaps.openstack.utils.deploy_utils.create_image',
49                 return_value=None)
50     def test_create_tempest_resources_missing_image(self, *mock_args):
51         tempest_resources = tempest.TempestResourcesManager(os_creds={})
52
53         with self.assertRaises(Exception) as context:
54             tempest_resources.create()
55         msg = 'Failed to create image'
56         self.assertTrue(msg in context.exception, msg=str(context.exception))
57
58     @mock.patch('snaps.openstack.utils.deploy_utils.create_project',
59                 return_value=mock.Mock())
60     @mock.patch('snaps.openstack.utils.deploy_utils.create_user',
61                 return_value=mock.Mock())
62     @mock.patch('snaps.openstack.utils.deploy_utils.create_network',
63                 return_value=mock.Mock())
64     @mock.patch('snaps.openstack.utils.deploy_utils.create_image',
65                 return_value=mock.Mock())
66     @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create',
67                 return_value=None)
68     def test_create_tempest_resources_missing_flavor(self, *mock_args):
69         tempest_resources = tempest.TempestResourcesManager(
70             os_creds=self.os_creds)
71
72         CONST.__setattr__('tempest_use_custom_flavors', 'True')
73         with self.assertRaises(Exception) as context:
74             tempest_resources.create()
75         msg = 'Failed to create flavor'
76         self.assertTrue(msg in context.exception, msg=str(context.exception))
77
78         CONST.__setattr__('tempest_use_custom_flavors', 'False')
79         with self.assertRaises(Exception) as context:
80             tempest_resources.create(use_custom_flavors=True)
81         msg = 'Failed to create flavor'
82         self.assertTrue(msg in context.exception, msg=str(context.exception))
83
84     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
85                 '.logger.info')
86     @mock.patch('functest.utils.functest_utils.execute_command_raise')
87     @mock.patch('functest.utils.functest_utils.execute_command')
88     def test_create_rally_deployment(self, mock_exec, mock_exec_raise,
89                                      mock_logger_info):
90
91         conf_utils.create_rally_deployment()
92
93         cmd = "rally deployment destroy opnfv-rally"
94         error_msg = "Deployment %s does not exist." % \
95                     CONST.__getattribute__('rally_deployment_name')
96         mock_logger_info.assert_any_call("Creating Rally environment...")
97         mock_exec.assert_any_call(cmd, error_msg=error_msg, verbose=False)
98
99         cmd = "rally deployment create --fromenv --name="
100         cmd += CONST.__getattribute__('rally_deployment_name')
101         error_msg = "Problem while creating Rally deployment"
102         mock_exec_raise.assert_any_call(cmd, error_msg=error_msg)
103
104         cmd = "rally deployment check"
105         error_msg = ("OpenStack not responding or "
106                      "faulty Rally deployment.")
107         mock_exec_raise.assert_any_call(cmd, error_msg=error_msg)
108
109     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
110                 '.logger.debug')
111     def test_create_verifier(self, mock_logger_debug):
112         mock_popen = mock.Mock()
113         attrs = {'poll.return_value': None,
114                  'stdout.readline.return_value': '0'}
115         mock_popen.configure_mock(**attrs)
116
117         CONST.__setattr__('tempest_verifier_name', 'test_veifier_name')
118         with mock.patch('functest.utils.functest_utils.execute_command_raise',
119                         side_effect=Exception), \
120                 self.assertRaises(Exception):
121             conf_utils.create_verifier()
122             mock_logger_debug.assert_any_call("Tempest test_veifier_name"
123                                               " does not exist")
124
125     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
126                 'create_verifier', return_value=mock.Mock())
127     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
128                 'create_rally_deployment', return_value=mock.Mock())
129     def test_get_verifier_id_missing_verifier(self, mock_rally, mock_tempest):
130         CONST.__setattr__('tempest_verifier_name', 'test_verifier_name')
131         with mock.patch('functest.opnfv_tests.openstack.tempest.'
132                         'conf_utils.subprocess.Popen') as mock_popen, \
133                 self.assertRaises(Exception):
134             mock_stdout = mock.Mock()
135             attrs = {'stdout.readline.return_value': ''}
136             mock_stdout.configure_mock(**attrs)
137             mock_popen.return_value = mock_stdout
138             conf_utils.get_verifier_id()
139
140     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
141                 'create_verifier', return_value=mock.Mock())
142     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
143                 'create_rally_deployment', return_value=mock.Mock())
144     def test_get_verifier_id_default(self, mock_rally, mock_tempest):
145         CONST.__setattr__('tempest_verifier_name', 'test_verifier_name')
146         with mock.patch('functest.opnfv_tests.openstack.tempest.'
147                         'conf_utils.subprocess.Popen') as mock_popen:
148             mock_stdout = mock.Mock()
149             attrs = {'stdout.readline.return_value': 'test_deploy_id'}
150             mock_stdout.configure_mock(**attrs)
151             mock_popen.return_value = mock_stdout
152
153             self.assertEqual(conf_utils.get_verifier_id(),
154                              'test_deploy_id')
155
156     def test_get_verifier_deployment_id_missing_rally(self):
157         CONST.__setattr__('tempest_verifier_name', 'test_deploy_name')
158         with mock.patch('functest.opnfv_tests.openstack.tempest.'
159                         'conf_utils.subprocess.Popen') as mock_popen, \
160                 self.assertRaises(Exception):
161             mock_stdout = mock.Mock()
162             attrs = {'stdout.readline.return_value': ''}
163             mock_stdout.configure_mock(**attrs)
164             mock_popen.return_value = mock_stdout
165             conf_utils.get_verifier_deployment_id(),
166
167     def test_get_verifier_deployment_id_default(self):
168         CONST.__setattr__('tempest_verifier_name', 'test_deploy_name')
169         with mock.patch('functest.opnfv_tests.openstack.tempest.'
170                         'conf_utils.subprocess.Popen') as mock_popen:
171             mock_stdout = mock.Mock()
172             attrs = {'stdout.readline.return_value': 'test_deploy_id'}
173             mock_stdout.configure_mock(**attrs)
174             mock_popen.return_value = mock_stdout
175
176             self.assertEqual(conf_utils.get_verifier_deployment_id(),
177                              'test_deploy_id')
178
179     def test_get_verifier_repo_dir_default(self):
180         with mock.patch('functest.opnfv_tests.openstack.tempest.'
181                         'conf_utils.os.path.join',
182                         return_value='test_verifier_repo_dir'), \
183             mock.patch('functest.opnfv_tests.openstack.tempest.'
184                        'conf_utils.get_verifier_id') as m:
185             self.assertEqual(conf_utils.get_verifier_repo_dir(''),
186                              'test_verifier_repo_dir')
187             self.assertTrue(m.called)
188
189     def test_get_verifier_deployment_dir_default(self):
190         with mock.patch('functest.opnfv_tests.openstack.tempest.'
191                         'conf_utils.os.path.join',
192                         return_value='test_verifier_repo_dir'), \
193             mock.patch('functest.opnfv_tests.openstack.tempest.'
194                        'conf_utils.get_verifier_id') as m1, \
195             mock.patch('functest.opnfv_tests.openstack.tempest.'
196                        'conf_utils.get_verifier_deployment_id') as m2:
197             self.assertEqual(conf_utils.get_verifier_deployment_dir('', ''),
198                              'test_verifier_repo_dir')
199             self.assertTrue(m1.called)
200             self.assertTrue(m2.called)
201
202     def test_backup_tempest_config_default(self):
203         with mock.patch('functest.opnfv_tests.openstack.tempest.'
204                         'conf_utils.os.path.exists',
205                         return_value=False), \
206             mock.patch('functest.opnfv_tests.openstack.tempest.'
207                        'conf_utils.os.makedirs') as m1, \
208             mock.patch('functest.opnfv_tests.openstack.tempest.'
209                        'conf_utils.shutil.copyfile') as m2:
210             conf_utils.backup_tempest_config('test_conf_file')
211             self.assertTrue(m1.called)
212             self.assertTrue(m2.called)
213
214         with mock.patch('functest.opnfv_tests.openstack.tempest.'
215                         'conf_utils.os.path.exists',
216                         return_value=True), \
217             mock.patch('functest.opnfv_tests.openstack.tempest.'
218                        'conf_utils.shutil.copyfile') as m2:
219             conf_utils.backup_tempest_config('test_conf_file')
220             self.assertTrue(m2.called)
221
222     def test_configure_tempest_default(self):
223         with mock.patch('functest.opnfv_tests.openstack.tempest.'
224                         'conf_utils.configure_verifier',
225                         return_value='test_conf_file'), \
226             mock.patch('functest.opnfv_tests.openstack.tempest.'
227                        'conf_utils.configure_tempest_update_params') as m1:
228             conf_utils.configure_tempest('test_dep_dir')
229             self.assertTrue(m1.called)
230
231     def test_configure_tempest_defcore_default(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             mock.patch('functest.opnfv_tests.openstack.tempest.'
238                        'conf_utils.ConfigParser.RawConfigParser.'
239                        'set') as mset, \
240             mock.patch('functest.opnfv_tests.openstack.tempest.'
241                        'conf_utils.ConfigParser.RawConfigParser.'
242                        'read') as mread, \
243             mock.patch('functest.opnfv_tests.openstack.tempest.'
244                        'conf_utils.ConfigParser.RawConfigParser.'
245                        'write') as mwrite, \
246             mock.patch('__builtin__.open', mock.mock_open()), \
247             mock.patch('functest.opnfv_tests.openstack.tempest.'
248                        'conf_utils.generate_test_accounts_file'), \
249             mock.patch('functest.opnfv_tests.openstack.tempest.'
250                        'conf_utils.shutil.copyfile'):
251             conf_utils.configure_tempest_defcore(
252                 'test_dep_dir', 'test_image_id', 'test_flavor_id',
253                 'test_image_alt_id', 'test_flavor_alt_id', 'test_tenant_id')
254             mset.assert_any_call('compute', 'image_ref', 'test_image_id')
255             mset.assert_any_call('compute', 'image_ref_alt',
256                                  'test_image_alt_id')
257             mset.assert_any_call('compute', 'flavor_ref', 'test_flavor_id')
258             mset.assert_any_call('compute', 'flavor_ref_alt',
259                                  'test_flavor_alt_id')
260             self.assertTrue(mread.called)
261             self.assertTrue(mwrite.called)
262
263     def test_generate_test_accounts_file_default(self):
264         with mock.patch("__builtin__.open", mock.mock_open()), \
265             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
266                        'yaml.dump') as mock_dump:
267             conf_utils.generate_test_accounts_file('test_tenant_id')
268             self.assertTrue(mock_dump.called)
269
270     def _test_missing_param(self, params, image_id, flavor_id):
271         with mock.patch('functest.opnfv_tests.openstack.tempest.'
272                         'conf_utils.ConfigParser.RawConfigParser.'
273                         'set') as mset, \
274             mock.patch('functest.opnfv_tests.openstack.tempest.'
275                        'conf_utils.ConfigParser.RawConfigParser.'
276                        'read') as mread, \
277             mock.patch('functest.opnfv_tests.openstack.tempest.'
278                        'conf_utils.ConfigParser.RawConfigParser.'
279                        'write') as mwrite, \
280             mock.patch('__builtin__.open', mock.mock_open()), \
281             mock.patch('functest.opnfv_tests.openstack.tempest.'
282                        'conf_utils.backup_tempest_config'), \
283             mock.patch('functest.utils.functest_utils.yaml.safe_load',
284                        return_value={'validation': {'ssh_timeout': 300}}):
285             CONST.__setattr__('OS_ENDPOINT_TYPE', None)
286             conf_utils.configure_tempest_update_params(
287                 'test_conf_file', image_id=image_id, flavor_id=flavor_id)
288             mset.assert_any_call(params[0], params[1], params[2])
289             self.assertTrue(mread.called)
290             self.assertTrue(mwrite.called)
291
292     def test_configure_tempest_update_params_missing_image_id(self):
293             self._test_missing_param(('compute', 'image_ref',
294                                       'test_image_id'), 'test_image_id',
295                                      None)
296
297     def test_configure_tempest_update_params_missing_image_id_alt(self):
298             conf_utils.IMAGE_ID_ALT = 'test_image_id_alt'
299             self._test_missing_param(('compute', 'image_ref_alt',
300                                       'test_image_id_alt'), None, None)
301
302     def test_configure_tempest_update_params_missing_flavor_id(self):
303             CONST.__setattr__('tempest_use_custom_flavors', 'True')
304             self._test_missing_param(('compute', 'flavor_ref',
305                                       'test_flavor_id'), None,
306                                      'test_flavor_id')
307
308     def test_configure_tempest_update_params_missing_flavor_id_alt(self):
309             CONST.__setattr__('tempest_use_custom_flavors', 'True')
310             conf_utils.FLAVOR_ID_ALT = 'test_flavor_id_alt'
311             self._test_missing_param(('compute', 'flavor_ref_alt',
312                                       'test_flavor_id_alt'), None,
313                                      None)
314
315     def test_configure_verifier_missing_temp_conf_file(self):
316         with mock.patch('functest.opnfv_tests.openstack.tempest.'
317                         'conf_utils.os.path.isfile',
318                         return_value=False), \
319             mock.patch('functest.opnfv_tests.openstack.tempest.'
320                        'conf_utils.ft_utils.execute_command') as mexe, \
321                 self.assertRaises(Exception) as context:
322             conf_utils.configure_verifier('test_dep_dir')
323             mexe.assert_any_call("rally verify configure-verifier")
324             msg = ("Tempest configuration file 'test_dep_dir/tempest.conf'"
325                    " NOT found.")
326             self.assertTrue(msg in context)
327
328     def test_configure_verifier_default(self):
329         with mock.patch('functest.opnfv_tests.openstack.tempest.'
330                         'conf_utils.os.path.isfile',
331                         return_value=True), \
332             mock.patch('functest.opnfv_tests.openstack.tempest.'
333                        'conf_utils.ft_utils.execute_command') as mexe:
334             self.assertEqual(conf_utils.configure_verifier('test_dep_dir'),
335                              'test_dep_dir/tempest.conf')
336             mexe.assert_any_call("rally verify configure-verifier "
337                                  "--reconfigure")
338
339
340 if __name__ == "__main__":
341     logging.disable(logging.CRITICAL)
342     unittest.main(verbosity=2)