Create 2 images and flavors for tempest tests
[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 import config
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         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     @staticmethod
85     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
86                 '.LOGGER.info')
87     @mock.patch('functest.utils.functest_utils.execute_command_raise')
88     @mock.patch('functest.utils.functest_utils.execute_command')
89     def test_create_rally_deployment(mock_exec, mock_exec_raise,
90                                      mock_logger_info):
91
92         conf_utils.create_rally_deployment()
93
94         cmd = "rally deployment destroy opnfv-rally"
95         error_msg = "Deployment %s does not exist." % \
96                     getattr(config.CONF, 'rally_deployment_name')
97         mock_logger_info.assert_any_call("Creating Rally environment...")
98         mock_exec.assert_any_call(cmd, error_msg=error_msg, verbose=False)
99
100         cmd = "rally deployment create --fromenv --name="
101         cmd += getattr(config.CONF, 'rally_deployment_name')
102         error_msg = "Problem while creating Rally deployment"
103         mock_exec_raise.assert_any_call(cmd, error_msg=error_msg)
104
105         cmd = "rally deployment check"
106         error_msg = ("OpenStack not responding or "
107                      "faulty Rally deployment.")
108         mock_exec_raise.assert_any_call(cmd, error_msg=error_msg)
109
110     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
111                 '.LOGGER.debug')
112     def test_create_verifier(self, mock_logger_debug):
113         mock_popen = mock.Mock()
114         attrs = {'poll.return_value': None,
115                  'stdout.readline.return_value': '0'}
116         mock_popen.configure_mock(**attrs)
117
118         setattr(config.CONF, 'tempest_verifier_name', 'test_verifier_name')
119         with mock.patch('functest.utils.functest_utils.execute_command_raise',
120                         side_effect=Exception), \
121                 self.assertRaises(Exception):
122             conf_utils.create_verifier()
123             mock_logger_debug.assert_any_call("Tempest test_verifier_name"
124                                               " does not exist")
125
126     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
127                 'create_verifier', return_value=mock.Mock())
128     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
129                 'create_rally_deployment', return_value=mock.Mock())
130     def test_get_verif_id_missing_verif(self, mock_rally, mock_tempest):
131         # pylint: disable=unused-argument
132         setattr(config.CONF, 'tempest_verifier_name', 'test_verifier_name')
133         with mock.patch('functest.opnfv_tests.openstack.tempest.'
134                         'conf_utils.subprocess.Popen') as mock_popen, \
135                 self.assertRaises(Exception):
136             mock_stdout = mock.Mock()
137             attrs = {'stdout.readline.return_value': ''}
138             mock_stdout.configure_mock(**attrs)
139             mock_popen.return_value = mock_stdout
140             conf_utils.get_verifier_id()
141
142     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
143                 'create_verifier', return_value=mock.Mock())
144     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
145                 'create_rally_deployment', return_value=mock.Mock())
146     def test_get_verifier_id_default(self, mock_rally, mock_tempest):
147         # pylint: disable=unused-argument
148         setattr(config.CONF, 'tempest_verifier_name', 'test_verifier_name')
149         with mock.patch('functest.opnfv_tests.openstack.tempest.'
150                         'conf_utils.subprocess.Popen') as mock_popen:
151             mock_stdout = mock.Mock()
152             attrs = {'stdout.readline.return_value': 'test_deploy_id'}
153             mock_stdout.configure_mock(**attrs)
154             mock_popen.return_value = mock_stdout
155
156             self.assertEqual(conf_utils.get_verifier_id(),
157                              'test_deploy_id')
158
159     def test_get_depl_id_missing_rally(self):
160         setattr(config.CONF, 'tempest_verifier_name', 'test_deploy_name')
161         with mock.patch('functest.opnfv_tests.openstack.tempest.'
162                         'conf_utils.subprocess.Popen') as mock_popen, \
163                 self.assertRaises(Exception):
164             mock_stdout = mock.Mock()
165             attrs = {'stdout.readline.return_value': ''}
166             mock_stdout.configure_mock(**attrs)
167             mock_popen.return_value = mock_stdout
168             conf_utils.get_verifier_deployment_id()
169
170     def test_get_depl_id_default(self):
171         setattr(config.CONF, 'tempest_verifier_name', 'test_deploy_name')
172         with mock.patch('functest.opnfv_tests.openstack.tempest.'
173                         'conf_utils.subprocess.Popen') as mock_popen:
174             mock_stdout = mock.Mock()
175             attrs = {'stdout.readline.return_value': 'test_deploy_id'}
176             mock_stdout.configure_mock(**attrs)
177             mock_popen.return_value = mock_stdout
178
179             self.assertEqual(conf_utils.get_verifier_deployment_id(),
180                              'test_deploy_id')
181
182     def test_get_verif_repo_dir_default(self):
183         with mock.patch('functest.opnfv_tests.openstack.tempest.'
184                         'conf_utils.os.path.join',
185                         return_value='test_verifier_repo_dir'), \
186             mock.patch('functest.opnfv_tests.openstack.tempest.'
187                        'conf_utils.get_verifier_id') as mock_get_id:
188             self.assertEqual(conf_utils.get_verifier_repo_dir(''),
189                              'test_verifier_repo_dir')
190             self.assertTrue(mock_get_id.called)
191
192     def test_get_depl_dir_default(self):
193         with mock.patch('functest.opnfv_tests.openstack.tempest.'
194                         'conf_utils.os.path.join',
195                         return_value='test_verifier_repo_dir'), \
196             mock.patch('functest.opnfv_tests.openstack.tempest.'
197                        'conf_utils.get_verifier_id') as mock_get_vid, \
198             mock.patch('functest.opnfv_tests.openstack.tempest.'
199                        'conf_utils.get_verifier_deployment_id') \
200                 as mock_get_did:
201             self.assertEqual(conf_utils.get_verifier_deployment_dir('', ''),
202                              'test_verifier_repo_dir')
203             self.assertTrue(mock_get_vid.called)
204             self.assertTrue(mock_get_did.called)
205
206     def test_backup_config_default(self):
207         with mock.patch('functest.opnfv_tests.openstack.tempest.'
208                         'conf_utils.os.path.exists',
209                         return_value=False), \
210             mock.patch('functest.opnfv_tests.openstack.tempest.'
211                        'conf_utils.os.makedirs') as mock_makedirs, \
212             mock.patch('functest.opnfv_tests.openstack.tempest.'
213                        'conf_utils.shutil.copyfile') as mock_copyfile:
214             conf_utils.backup_tempest_config(
215                 'test_conf_file', res_dir='test_dir')
216             self.assertTrue(mock_makedirs.called)
217             self.assertTrue(mock_copyfile.called)
218
219         with mock.patch('functest.opnfv_tests.openstack.tempest.'
220                         'conf_utils.os.path.exists',
221                         return_value=True), \
222             mock.patch('functest.opnfv_tests.openstack.tempest.'
223                        'conf_utils.shutil.copyfile') as mock_copyfile:
224             conf_utils.backup_tempest_config(
225                 'test_conf_file', res_dir='test_dir')
226             self.assertTrue(mock_copyfile.called)
227
228     def _test_missing_param(self, params, image_id, flavor_id):
229         with 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.backup_tempest_config'), \
241             mock.patch('functest.utils.functest_utils.yaml.safe_load',
242                        return_value={'validation': {'ssh_timeout': 300}}):
243             os.environ['OS_ENDPOINT_TYPE'] = ''
244             conf_utils.configure_tempest_update_params(
245                 'test_conf_file', res_dir='test_dir', image_id=image_id,
246                 flavor_id=flavor_id)
247             mset.assert_any_call(params[0], params[1], params[2])
248             self.assertTrue(mread.called)
249             self.assertTrue(mwrite.called)
250
251     def test_upd_missing_image_id(self):
252         self._test_missing_param(('compute', 'image_ref', 'test_image_id'),
253                                  'test_image_id', None)
254
255     def test_upd_missing_image_id_alt(self):
256         conf_utils.IMAGE_ID_ALT = 'test_image_id_alt'
257         self._test_missing_param(('compute', 'image_ref_alt',
258                                   'test_image_id_alt'), None, None)
259
260     def test_upd_missing_flavor_id(self):
261         setattr(config.CONF, 'tempest_use_custom_flavors', 'True')
262         self._test_missing_param(('compute', 'flavor_ref', 'test_flavor_id'),
263                                  None, 'test_flavor_id')
264
265     def test_upd_missing_flavor_id_alt(self):
266         setattr(config.CONF, 'tempest_use_custom_flavors', 'True')
267         conf_utils.FLAVOR_ID_ALT = 'test_flavor_id_alt'
268         self._test_missing_param(('compute', 'flavor_ref_alt',
269                                   'test_flavor_id_alt'), None, None)
270
271     def test_verif_missing_conf_file(self):
272         with mock.patch('functest.opnfv_tests.openstack.tempest.'
273                         'conf_utils.os.path.isfile',
274                         return_value=False), \
275                 mock.patch('functest.opnfv_tests.openstack.tempest.'
276                            'conf_utils.ft_utils.execute_command') as mexe, \
277                 self.assertRaises(Exception) as context:
278             conf_utils.configure_verifier('test_dep_dir')
279             mexe.assert_any_call("rally verify configure-verifier")
280             msg = ("Tempest configuration file 'test_dep_dir/tempest.conf'"
281                    " NOT found.")
282             self.assertTrue(msg in context.exception)
283
284     def test_configure_verifier_default(self):
285         with mock.patch('functest.opnfv_tests.openstack.tempest.'
286                         'conf_utils.os.path.isfile',
287                         return_value=True), \
288             mock.patch('functest.opnfv_tests.openstack.tempest.'
289                        'conf_utils.ft_utils.execute_command') as mexe:
290             self.assertEqual(conf_utils.configure_verifier('test_dep_dir'),
291                              'test_dep_dir/tempest.conf')
292             mexe.assert_any_call("rally verify configure-verifier "
293                                  "--reconfigure")
294
295
296 if __name__ == "__main__":
297     logging.disable(logging.CRITICAL)
298     unittest.main(verbosity=2)