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