Move logics out of TempestCommon.__init__()
[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.utils import config
18
19
20 class OSTempestConfUtilsTesting(unittest.TestCase):
21     # pylint: disable=too-many-public-methods
22
23     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
24                 '.get_verifier_deployment_id', return_value='foo')
25     @mock.patch('subprocess.check_output')
26     def test_create_rally_deployment(self, mock_exec, mock_get_id):
27         # pylint: disable=unused-argument
28         self.assertEqual(conf_utils.create_rally_deployment(), 'foo')
29         calls = [
30             mock.call(['rally', 'deployment', 'destroy', '--deployment',
31                        str(getattr(config.CONF, 'rally_deployment_name'))]),
32             mock.call(['rally', 'deployment', 'create', '--fromenv', '--name',
33                        str(getattr(config.CONF, 'rally_deployment_name'))],
34                       env=None),
35             mock.call(['rally', 'deployment', 'check'])]
36         mock_exec.assert_has_calls(calls)
37
38     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
39                 '.LOGGER.debug')
40     def test_create_verifier(self, mock_logger_debug):
41         mock_popen = mock.Mock()
42         attrs = {'poll.return_value': None,
43                  'stdout.readline.return_value': '0'}
44         mock_popen.configure_mock(**attrs)
45
46         setattr(config.CONF, 'tempest_verifier_name', 'test_verifier_name')
47         with mock.patch('subprocess.Popen', side_effect=Exception), \
48                 self.assertRaises(Exception):
49             conf_utils.create_verifier()
50             mock_logger_debug.assert_any_call("Tempest test_verifier_name"
51                                               " does not exist")
52
53     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
54                 'create_verifier', return_value=mock.Mock())
55     @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
56                 'create_rally_deployment', return_value=mock.Mock())
57     def test_get_verifier_id_default(self, mock_rally, mock_tempest):
58         # pylint: disable=unused-argument
59         setattr(config.CONF, 'tempest_verifier_name', 'test_verifier_name')
60         with mock.patch('functest.opnfv_tests.openstack.tempest.'
61                         'conf_utils.subprocess.Popen') as mock_popen:
62             mock_stdout = mock.Mock()
63             attrs = {'stdout.readline.return_value': 'test_deploy_id'}
64             mock_stdout.configure_mock(**attrs)
65             mock_popen.return_value = mock_stdout
66
67             self.assertEqual(conf_utils.get_verifier_id(),
68                              'test_deploy_id')
69
70     def test_get_depl_id_default(self):
71         setattr(config.CONF, 'tempest_verifier_name', 'test_deploy_name')
72         with mock.patch('functest.opnfv_tests.openstack.tempest.'
73                         'conf_utils.subprocess.Popen') as mock_popen:
74             mock_stdout = mock.Mock()
75             attrs = {'stdout.readline.return_value': 'test_deploy_id'}
76             mock_stdout.configure_mock(**attrs)
77             mock_popen.return_value = mock_stdout
78
79             self.assertEqual(conf_utils.get_verifier_deployment_id(),
80                              'test_deploy_id')
81
82     def test_get_verif_repo_dir_default(self):
83         with mock.patch('functest.opnfv_tests.openstack.tempest.'
84                         'conf_utils.os.path.join',
85                         return_value='test_verifier_repo_dir'):
86             self.assertEqual(conf_utils.get_verifier_repo_dir(''),
87                              'test_verifier_repo_dir')
88
89     def test_get_depl_dir_default(self):
90         with mock.patch('functest.opnfv_tests.openstack.tempest.'
91                         'conf_utils.os.path.join',
92                         return_value='test_verifier_repo_dir'):
93             self.assertEqual(conf_utils.get_verifier_deployment_dir('', ''),
94                              'test_verifier_repo_dir')
95
96     def _test_missing_param(self, params, image_id, flavor_id, alt=False):
97         with mock.patch('six.moves.configparser.RawConfigParser.'
98                         'set') as mset, \
99             mock.patch('six.moves.configparser.RawConfigParser.'
100                        'read') as mread, \
101             mock.patch('six.moves.configparser.RawConfigParser.'
102                        'write') as mwrite, \
103             mock.patch('six.moves.builtins.open', mock.mock_open()), \
104             mock.patch('functest.utils.functest_utils.yaml.safe_load',
105                        return_value={'validation': {'ssh_timeout': 300}}):
106             os.environ['OS_INTERFACE'] = ''
107             if not alt:
108                 conf_utils.configure_tempest_update_params(
109                     'test_conf_file', image_id=image_id,
110                     flavor_id=flavor_id)
111                 mset.assert_any_call(params[0], params[1], params[2])
112             else:
113                 conf_utils.configure_tempest_update_params(
114                     'test_conf_file', image_alt_id=image_id,
115                     flavor_alt_id=flavor_id)
116                 mset.assert_any_call(params[0], params[1], params[2])
117             self.assertTrue(mread.called)
118             self.assertTrue(mwrite.called)
119
120     def test_upd_missing_image_id(self):
121         self._test_missing_param(('compute', 'image_ref', 'test_image_id'),
122                                  'test_image_id', None)
123
124     def test_upd_missing_image_id_alt(self):
125         self._test_missing_param(
126             ('compute', 'image_ref_alt', 'test_image_id_alt'),
127             'test_image_id_alt', None, alt=True)
128
129     def test_upd_missing_flavor_id(self):
130         self._test_missing_param(('compute', 'flavor_ref', 'test_flavor_id'),
131                                  None, 'test_flavor_id')
132
133     def test_upd_missing_flavor_id_alt(self):
134         self._test_missing_param(
135             ('compute', 'flavor_ref_alt', 'test_flavor_id_alt'),
136             None, 'test_flavor_id_alt', alt=True)
137
138     def test_verif_missing_conf_file(self):
139         with mock.patch('functest.opnfv_tests.openstack.tempest.'
140                         'conf_utils.os.path.isfile',
141                         return_value=False), \
142                 mock.patch('subprocess.check_output') as mexe, \
143                 self.assertRaises(Exception) as context:
144             conf_utils.configure_verifier('test_dep_dir')
145             mexe.assert_called_once_with("rally verify configure-verifier")
146             msg = ("Tempest configuration file 'test_dep_dir/tempest.conf'"
147                    " NOT found.")
148             self.assertTrue(msg in context.exception)
149
150     def test_configure_verifier_default(self):
151         with mock.patch('functest.opnfv_tests.openstack.tempest.'
152                         'conf_utils.os.path.isfile',
153                         return_value=True), \
154                 mock.patch('subprocess.check_output') as mexe:
155             self.assertEqual(conf_utils.configure_verifier('test_dep_dir'),
156                              'test_dep_dir/tempest.conf')
157             mexe.assert_called_once_with(
158                 ['rally', 'verify', 'configure-verifier', '--reconfigure',
159                  '--id', str(getattr(config.CONF, 'tempest_verifier_name'))])
160
161
162 if __name__ == "__main__":
163     logging.disable(logging.CRITICAL)
164     unittest.main(verbosity=2)