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
8 # pylint: disable=missing-docstring
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
21 class OSTempestConfUtilsTesting(unittest.TestCase):
22 # pylint: disable=too-many-public-methods
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)
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()
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))
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))
52 @mock.patch('subprocess.check_output')
53 def test_create_rally_deployment(self, mock_exec):
54 self.assertEqual(conf_utils.create_rally_deployment(), None)
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)
63 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
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)
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"
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()
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
108 self.assertEqual(conf_utils.get_verifier_id(),
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()
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
131 self.assertEqual(conf_utils.get_verifier_deployment_id(),
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)
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') \
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)
158 def _test_missing_param(self, params, image_id, flavor_id, alt=False):
159 with mock.patch('six.moves.configparser.RawConfigParser.'
161 mock.patch('six.moves.configparser.RawConfigParser.'
163 mock.patch('six.moves.configparser.RawConfigParser.'
164 'write') as mwrite, \
165 mock.patch('six.moves.builtins.open', mock.mock_open()), \
166 mock.patch('functest.utils.functest_utils.yaml.safe_load',
167 return_value={'validation': {'ssh_timeout': 300}}):
168 os.environ['OS_INTERFACE'] = ''
170 conf_utils.configure_tempest_update_params(
171 'test_conf_file', image_id=image_id,
173 mset.assert_any_call(params[0], params[1], params[2])
175 conf_utils.configure_tempest_update_params(
176 'test_conf_file', image_alt_id=image_id,
177 flavor_alt_id=flavor_id)
178 mset.assert_any_call(params[0], params[1], params[2])
179 self.assertTrue(mread.called)
180 self.assertTrue(mwrite.called)
182 def test_upd_missing_image_id(self):
183 self._test_missing_param(('compute', 'image_ref', 'test_image_id'),
184 'test_image_id', None)
186 def test_upd_missing_image_id_alt(self):
187 self._test_missing_param(
188 ('compute', 'image_ref_alt', 'test_image_id_alt'),
189 'test_image_id_alt', None, alt=True)
191 def test_upd_missing_flavor_id(self):
192 self._test_missing_param(('compute', 'flavor_ref', 'test_flavor_id'),
193 None, 'test_flavor_id')
195 def test_upd_missing_flavor_id_alt(self):
196 self._test_missing_param(
197 ('compute', 'flavor_ref_alt', 'test_flavor_id_alt'),
198 None, 'test_flavor_id_alt', alt=True)
200 def test_verif_missing_conf_file(self):
201 with mock.patch('functest.opnfv_tests.openstack.tempest.'
202 'conf_utils.os.path.isfile',
203 return_value=False), \
204 mock.patch('subprocess.check_output') as mexe, \
205 self.assertRaises(Exception) as context:
206 conf_utils.configure_verifier('test_dep_dir')
207 mexe.assert_called_once_with("rally verify configure-verifier")
208 msg = ("Tempest configuration file 'test_dep_dir/tempest.conf'"
210 self.assertTrue(msg in context.exception)
212 def test_configure_verifier_default(self):
213 with mock.patch('functest.opnfv_tests.openstack.tempest.'
214 'conf_utils.os.path.isfile',
215 return_value=True), \
216 mock.patch('subprocess.check_output') as mexe:
217 self.assertEqual(conf_utils.configure_verifier('test_dep_dir'),
218 'test_dep_dir/tempest.conf')
219 mexe.assert_called_once_with(
220 ['rally', 'verify', 'configure-verifier', '--reconfigure',
221 '--id', str(getattr(config.CONF, 'tempest_verifier_name'))])
224 if __name__ == "__main__":
225 logging.disable(logging.CRITICAL)
226 unittest.main(verbosity=2)