Fix role processing in Patrole
[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 conf_utils
17 from functest.opnfv_tests.openstack.tempest import tempest
18 from functest.utils import config
19 from snaps.openstack.os_credentials import OSCreds
20
21
22 class OSTempestConfUtilsTesting(unittest.TestCase):
23     # pylint: disable=too-many-public-methods
24     def setUp(self):
25         self.os_creds = OSCreds(
26             username='user', password='pass',
27             auth_url='http://foo.com:5000/v3', project_name='bar')
28
29     @mock.patch('snaps.openstack.utils.deploy_utils.create_project',
30                 return_value=mock.Mock())
31     @mock.patch('snaps.openstack.utils.deploy_utils.create_user',
32                 return_value=mock.Mock())
33     @mock.patch('snaps.openstack.utils.deploy_utils.create_network',
34                 return_value=None)
35     @mock.patch('snaps.openstack.utils.deploy_utils.create_image',
36                 return_value=mock.Mock())
37     def test_create_res_missing_net_dic(self, *mock_args):
38         # pylint: disable=unused-argument
39         tempest_resources = tempest.TempestResourcesManager(
40             os_creds=self.os_creds)
41         with self.assertRaises(Exception) as context:
42             tempest_resources.create()
43         msg = 'Failed to create private network'
44         self.assertTrue(msg in context.exception)
45
46     @mock.patch('snaps.openstack.utils.deploy_utils.create_project',
47                 return_value=mock.Mock())
48     @mock.patch('snaps.openstack.utils.deploy_utils.create_user',
49                 return_value=mock.Mock())
50     @mock.patch('snaps.openstack.utils.deploy_utils.create_network',
51                 return_value=mock.Mock())
52     @mock.patch('snaps.openstack.utils.deploy_utils.create_image',
53                 return_value=None)
54     def test_create_res_missing_image(self, *mock_args):
55         # pylint: disable=unused-argument
56         tempest_resources = tempest.TempestResourcesManager(
57             os_creds=self.os_creds)
58
59         with self.assertRaises(Exception) as context:
60             tempest_resources.create()
61         msg = 'Failed to create image'
62         self.assertTrue(msg in context.exception, msg=str(context.exception))
63
64     @mock.patch('snaps.openstack.utils.deploy_utils.create_project',
65                 return_value=mock.Mock())
66     @mock.patch('snaps.openstack.utils.deploy_utils.create_user',
67                 return_value=mock.Mock())
68     @mock.patch('snaps.openstack.utils.deploy_utils.create_network',
69                 return_value=mock.Mock())
70     @mock.patch('snaps.openstack.utils.deploy_utils.create_image',
71                 return_value=mock.Mock())
72     @mock.patch('snaps.openstack.utils.keystone_utils.keystone_client')
73     @mock.patch('snaps.openstack.utils.keystone_utils.get_project')
74     @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create',
75                 return_value=None)
76     def test_create_res_missing_flavor(self, *mock_args):
77         # pylint: disable=unused-argument
78         tempest_resources = tempest.TempestResourcesManager(
79             os_creds=self.os_creds)
80         with self.assertRaises(Exception) as context:
81             tempest_resources.create()
82         msg = 'Failed to create flavor'
83         self.assertTrue(msg in context.exception, msg=str(context.exception))
84
85     @mock.patch('subprocess.check_output')
86     def test_create_rally_deployment(self, mock_exec):
87         self.assertEqual(conf_utils.create_rally_deployment(), None)
88         calls = [
89             mock.call(['rally', 'deployment', 'destroy', '--deployment',
90                        str(getattr(config.CONF, 'rally_deployment_name'))]),
91             mock.call(['rally', 'deployment', 'create', '--fromenv', '--name',
92                        str(getattr(config.CONF, 'rally_deployment_name'))]),
93             mock.call(['rally', 'deployment', 'check'])]
94         mock_exec.assert_has_calls(calls)
95
96     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
97                 '.LOGGER.debug')
98     def test_create_verifier(self, mock_logger_debug):
99         mock_popen = mock.Mock()
100         attrs = {'poll.return_value': None,
101                  'stdout.readline.return_value': '0'}
102         mock_popen.configure_mock(**attrs)
103
104         setattr(config.CONF, 'tempest_verifier_name', 'test_verifier_name')
105         with mock.patch('subprocess.Popen', side_effect=Exception), \
106                 self.assertRaises(Exception):
107             conf_utils.create_verifier()
108             mock_logger_debug.assert_any_call("Tempest test_verifier_name"
109                                               " does not exist")
110
111     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
112                 'create_verifier', return_value=mock.Mock())
113     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
114                 'create_rally_deployment', return_value=mock.Mock())
115     def test_get_verif_id_missing_verif(self, mock_rally, mock_tempest):
116         # pylint: disable=unused-argument
117         setattr(config.CONF, 'tempest_verifier_name', 'test_verifier_name')
118         with mock.patch('functest.opnfv_tests.openstack.tempest.'
119                         'conf_utils.subprocess.Popen') as mock_popen, \
120                 self.assertRaises(Exception):
121             mock_stdout = mock.Mock()
122             attrs = {'stdout.readline.return_value': ''}
123             mock_stdout.configure_mock(**attrs)
124             mock_popen.return_value = mock_stdout
125             conf_utils.get_verifier_id()
126
127     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
128                 'create_verifier', return_value=mock.Mock())
129     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
130                 'create_rally_deployment', return_value=mock.Mock())
131     def test_get_verifier_id_default(self, mock_rally, mock_tempest):
132         # pylint: disable=unused-argument
133         setattr(config.CONF, 'tempest_verifier_name', 'test_verifier_name')
134         with mock.patch('functest.opnfv_tests.openstack.tempest.'
135                         'conf_utils.subprocess.Popen') as mock_popen:
136             mock_stdout = mock.Mock()
137             attrs = {'stdout.readline.return_value': 'test_deploy_id'}
138             mock_stdout.configure_mock(**attrs)
139             mock_popen.return_value = mock_stdout
140
141             self.assertEqual(conf_utils.get_verifier_id(),
142                              'test_deploy_id')
143
144     def test_get_depl_id_missing_rally(self):
145         setattr(config.CONF, 'tempest_verifier_name', 'test_deploy_name')
146         with mock.patch('functest.opnfv_tests.openstack.tempest.'
147                         'conf_utils.subprocess.Popen') as mock_popen, \
148                 self.assertRaises(Exception):
149             mock_stdout = mock.Mock()
150             attrs = {'stdout.readline.return_value': ''}
151             mock_stdout.configure_mock(**attrs)
152             mock_popen.return_value = mock_stdout
153             conf_utils.get_verifier_deployment_id()
154
155     def test_get_depl_id_default(self):
156         setattr(config.CONF, 'tempest_verifier_name', 'test_deploy_name')
157         with mock.patch('functest.opnfv_tests.openstack.tempest.'
158                         'conf_utils.subprocess.Popen') as mock_popen:
159             mock_stdout = mock.Mock()
160             attrs = {'stdout.readline.return_value': 'test_deploy_id'}
161             mock_stdout.configure_mock(**attrs)
162             mock_popen.return_value = mock_stdout
163
164             self.assertEqual(conf_utils.get_verifier_deployment_id(),
165                              'test_deploy_id')
166
167     def test_get_verif_repo_dir_default(self):
168         with mock.patch('functest.opnfv_tests.openstack.tempest.'
169                         'conf_utils.os.path.join',
170                         return_value='test_verifier_repo_dir'), \
171             mock.patch('functest.opnfv_tests.openstack.tempest.'
172                        'conf_utils.get_verifier_id') as mock_get_id:
173             self.assertEqual(conf_utils.get_verifier_repo_dir(''),
174                              'test_verifier_repo_dir')
175             self.assertTrue(mock_get_id.called)
176
177     def test_get_depl_dir_default(self):
178         with mock.patch('functest.opnfv_tests.openstack.tempest.'
179                         'conf_utils.os.path.join',
180                         return_value='test_verifier_repo_dir'), \
181             mock.patch('functest.opnfv_tests.openstack.tempest.'
182                        'conf_utils.get_verifier_id') as mock_get_vid, \
183             mock.patch('functest.opnfv_tests.openstack.tempest.'
184                        'conf_utils.get_verifier_deployment_id') \
185                 as mock_get_did:
186             self.assertEqual(conf_utils.get_verifier_deployment_dir('', ''),
187                              'test_verifier_repo_dir')
188             self.assertTrue(mock_get_vid.called)
189             self.assertTrue(mock_get_did.called)
190
191     def _test_missing_param(self, params, image_id, flavor_id):
192         with mock.patch('functest.opnfv_tests.openstack.tempest.'
193                         'conf_utils.ConfigParser.RawConfigParser.'
194                         'set') as mset, \
195             mock.patch('functest.opnfv_tests.openstack.tempest.'
196                        'conf_utils.ConfigParser.RawConfigParser.'
197                        'read') as mread, \
198             mock.patch('functest.opnfv_tests.openstack.tempest.'
199                        'conf_utils.ConfigParser.RawConfigParser.'
200                        'write') as mwrite, \
201             mock.patch('__builtin__.open', mock.mock_open()), \
202             mock.patch('functest.utils.functest_utils.yaml.safe_load',
203                        return_value={'validation': {'ssh_timeout': 300}}):
204             os.environ['OS_ENDPOINT_TYPE'] = ''
205             conf_utils.configure_tempest_update_params(
206                 'test_conf_file', image_id=image_id,
207                 flavor_id=flavor_id)
208             mset.assert_any_call(params[0], params[1], params[2])
209             self.assertTrue(mread.called)
210             self.assertTrue(mwrite.called)
211
212     def test_upd_missing_image_id(self):
213         self._test_missing_param(('compute', 'image_ref', 'test_image_id'),
214                                  'test_image_id', None)
215
216     def test_upd_missing_image_id_alt(self):
217         conf_utils.IMAGE_ID_ALT = 'test_image_id_alt'
218         self._test_missing_param(('compute', 'image_ref_alt',
219                                   'test_image_id_alt'), None, None)
220
221     def test_upd_missing_flavor_id(self):
222         setattr(config.CONF, 'tempest_use_custom_flavors', 'True')
223         self._test_missing_param(('compute', 'flavor_ref', 'test_flavor_id'),
224                                  None, 'test_flavor_id')
225
226     def test_upd_missing_flavor_id_alt(self):
227         setattr(config.CONF, 'tempest_use_custom_flavors', 'True')
228         conf_utils.FLAVOR_ID_ALT = 'test_flavor_id_alt'
229         self._test_missing_param(('compute', 'flavor_ref_alt',
230                                   'test_flavor_id_alt'), None, None)
231
232     def test_verif_missing_conf_file(self):
233         with mock.patch('functest.opnfv_tests.openstack.tempest.'
234                         'conf_utils.os.path.isfile',
235                         return_value=False), \
236                 mock.patch('subprocess.check_output') as mexe, \
237                 self.assertRaises(Exception) as context:
238             conf_utils.configure_verifier('test_dep_dir')
239             mexe.assert_called_once_with("rally verify configure-verifier")
240             msg = ("Tempest configuration file 'test_dep_dir/tempest.conf'"
241                    " NOT found.")
242             self.assertTrue(msg in context.exception)
243
244     def test_configure_verifier_default(self):
245         with mock.patch('functest.opnfv_tests.openstack.tempest.'
246                         'conf_utils.os.path.isfile',
247                         return_value=True), \
248                 mock.patch('subprocess.check_output') as mexe:
249             self.assertEqual(conf_utils.configure_verifier('test_dep_dir'),
250                              'test_dep_dir/tempest.conf')
251             mexe.assert_called_once_with(
252                 ['rally', 'verify', 'configure-verifier', '--reconfigure',
253                  '--id', str(getattr(config.CONF, 'tempest_verifier_name'))])
254
255
256 if __name__ == "__main__":
257     logging.disable(logging.CRITICAL)
258     unittest.main(verbosity=2)