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 tempest, conf_utils
17 from functest.utils import config
18 from snaps.openstack.os_credentials import OSCreds
21 class OSTempestConfUtilsTesting(unittest.TestCase):
22 # pylint: disable=too-many-public-methods
24 self.os_creds = OSCreds(
25 username='user', password='pass',
26 auth_url='http://foo.com:5000/v3', project_name='bar')
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',
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)
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',
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)
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))
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',
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)
80 setattr(config.CONF, 'tempest_use_custom_flavors', 'True')
81 with self.assertRaises(Exception) as context:
82 tempest_resources.create()
83 msg = 'Failed to create flavor'
84 self.assertTrue(msg in context.exception, msg=str(context.exception))
86 setattr(config.CONF, 'tempest_use_custom_flavors', 'False')
87 with self.assertRaises(Exception) as context:
88 tempest_resources.create(use_custom_flavors=True)
89 msg = 'Failed to create flavor'
90 self.assertTrue(msg in context.exception, msg=str(context.exception))
93 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
95 @mock.patch('functest.utils.functest_utils.execute_command_raise')
96 @mock.patch('functest.utils.functest_utils.execute_command')
97 def test_create_rally_deployment(mock_exec, mock_exec_raise,
100 conf_utils.create_rally_deployment()
102 cmd = "rally deployment destroy opnfv-rally"
103 error_msg = "Deployment %s does not exist." % \
104 getattr(config.CONF, 'rally_deployment_name')
105 mock_logger_info.assert_any_call("Creating Rally environment...")
106 mock_exec.assert_any_call(cmd, error_msg=error_msg, verbose=False)
108 cmd = "rally deployment create --fromenv --name="
109 cmd += getattr(config.CONF, 'rally_deployment_name')
110 error_msg = "Problem while creating Rally deployment"
111 mock_exec_raise.assert_any_call(cmd, error_msg=error_msg)
113 cmd = "rally deployment check"
114 error_msg = ("OpenStack not responding or "
115 "faulty Rally deployment.")
116 mock_exec_raise.assert_any_call(cmd, error_msg=error_msg)
118 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils'
120 def test_create_verifier(self, mock_logger_debug):
121 mock_popen = mock.Mock()
122 attrs = {'poll.return_value': None,
123 'stdout.readline.return_value': '0'}
124 mock_popen.configure_mock(**attrs)
126 setattr(config.CONF, 'tempest_verifier_name', 'test_verifier_name')
127 with mock.patch('functest.utils.functest_utils.execute_command_raise',
128 side_effect=Exception), \
129 self.assertRaises(Exception):
130 conf_utils.create_verifier()
131 mock_logger_debug.assert_any_call("Tempest test_verifier_name"
134 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
135 'create_verifier', return_value=mock.Mock())
136 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
137 'create_rally_deployment', return_value=mock.Mock())
138 def test_get_verif_id_missing_verif(self, mock_rally, mock_tempest):
139 # pylint: disable=unused-argument
140 setattr(config.CONF, 'tempest_verifier_name', 'test_verifier_name')
141 with mock.patch('functest.opnfv_tests.openstack.tempest.'
142 'conf_utils.subprocess.Popen') as mock_popen, \
143 self.assertRaises(Exception):
144 mock_stdout = mock.Mock()
145 attrs = {'stdout.readline.return_value': ''}
146 mock_stdout.configure_mock(**attrs)
147 mock_popen.return_value = mock_stdout
148 conf_utils.get_verifier_id()
150 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
151 'create_verifier', return_value=mock.Mock())
152 @mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
153 'create_rally_deployment', return_value=mock.Mock())
154 def test_get_verifier_id_default(self, mock_rally, mock_tempest):
155 # pylint: disable=unused-argument
156 setattr(config.CONF, 'tempest_verifier_name', 'test_verifier_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
164 self.assertEqual(conf_utils.get_verifier_id(),
167 def test_get_depl_id_missing_rally(self):
168 setattr(config.CONF, 'tempest_verifier_name', 'test_deploy_name')
169 with mock.patch('functest.opnfv_tests.openstack.tempest.'
170 'conf_utils.subprocess.Popen') as mock_popen, \
171 self.assertRaises(Exception):
172 mock_stdout = mock.Mock()
173 attrs = {'stdout.readline.return_value': ''}
174 mock_stdout.configure_mock(**attrs)
175 mock_popen.return_value = mock_stdout
176 conf_utils.get_verifier_deployment_id()
178 def test_get_depl_id_default(self):
179 setattr(config.CONF, 'tempest_verifier_name', 'test_deploy_name')
180 with mock.patch('functest.opnfv_tests.openstack.tempest.'
181 'conf_utils.subprocess.Popen') as mock_popen:
182 mock_stdout = mock.Mock()
183 attrs = {'stdout.readline.return_value': 'test_deploy_id'}
184 mock_stdout.configure_mock(**attrs)
185 mock_popen.return_value = mock_stdout
187 self.assertEqual(conf_utils.get_verifier_deployment_id(),
190 def test_get_verif_repo_dir_default(self):
191 with mock.patch('functest.opnfv_tests.openstack.tempest.'
192 'conf_utils.os.path.join',
193 return_value='test_verifier_repo_dir'), \
194 mock.patch('functest.opnfv_tests.openstack.tempest.'
195 'conf_utils.get_verifier_id') as mock_get_id:
196 self.assertEqual(conf_utils.get_verifier_repo_dir(''),
197 'test_verifier_repo_dir')
198 self.assertTrue(mock_get_id.called)
200 def test_get_depl_dir_default(self):
201 with mock.patch('functest.opnfv_tests.openstack.tempest.'
202 'conf_utils.os.path.join',
203 return_value='test_verifier_repo_dir'), \
204 mock.patch('functest.opnfv_tests.openstack.tempest.'
205 'conf_utils.get_verifier_id') as mock_get_vid, \
206 mock.patch('functest.opnfv_tests.openstack.tempest.'
207 'conf_utils.get_verifier_deployment_id') \
209 self.assertEqual(conf_utils.get_verifier_deployment_dir('', ''),
210 'test_verifier_repo_dir')
211 self.assertTrue(mock_get_vid.called)
212 self.assertTrue(mock_get_did.called)
214 def test_backup_config_default(self):
215 with mock.patch('functest.opnfv_tests.openstack.tempest.'
216 'conf_utils.os.path.exists',
217 return_value=False), \
218 mock.patch('functest.opnfv_tests.openstack.tempest.'
219 'conf_utils.os.makedirs') as mock_makedirs, \
220 mock.patch('functest.opnfv_tests.openstack.tempest.'
221 'conf_utils.shutil.copyfile') as mock_copyfile:
222 conf_utils.backup_tempest_config(
223 'test_conf_file', res_dir='test_dir')
224 self.assertTrue(mock_makedirs.called)
225 self.assertTrue(mock_copyfile.called)
227 with mock.patch('functest.opnfv_tests.openstack.tempest.'
228 'conf_utils.os.path.exists',
229 return_value=True), \
230 mock.patch('functest.opnfv_tests.openstack.tempest.'
231 'conf_utils.shutil.copyfile') as mock_copyfile:
232 conf_utils.backup_tempest_config(
233 'test_conf_file', res_dir='test_dir')
234 self.assertTrue(mock_copyfile.called)
236 def test_conf_tempest_def(self):
237 with mock.patch('functest.opnfv_tests.openstack.tempest.'
238 'conf_utils.configure_verifier',
239 return_value='test_conf_file'), \
240 mock.patch('functest.opnfv_tests.openstack.tempest.'
241 'conf_utils.configure_tempest_update_params')\
243 conf_utils.configure_tempest('test_dep_dir', 'test_dir')
244 self.assertTrue(mock_upd.called)
246 def test_gen_test_accounts_file_def(self):
247 with mock.patch("__builtin__.open", mock.mock_open()), \
248 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
249 'yaml.dump') as mock_dump:
250 conf_utils.generate_test_accounts_file('test_tenant_id')
251 self.assertTrue(mock_dump.called)
253 def _test_missing_param(self, params, image_id, flavor_id):
254 with mock.patch('functest.opnfv_tests.openstack.tempest.'
255 'conf_utils.ConfigParser.RawConfigParser.'
257 mock.patch('functest.opnfv_tests.openstack.tempest.'
258 'conf_utils.ConfigParser.RawConfigParser.'
260 mock.patch('functest.opnfv_tests.openstack.tempest.'
261 'conf_utils.ConfigParser.RawConfigParser.'
262 'write') as mwrite, \
263 mock.patch('__builtin__.open', mock.mock_open()), \
264 mock.patch('functest.opnfv_tests.openstack.tempest.'
265 'conf_utils.backup_tempest_config'), \
266 mock.patch('functest.utils.functest_utils.yaml.safe_load',
267 return_value={'validation': {'ssh_timeout': 300}}):
268 os.environ['OS_ENDPOINT_TYPE'] = ''
269 conf_utils.configure_tempest_update_params(
270 'test_conf_file', res_dir='test_dir', image_id=image_id,
272 mset.assert_any_call(params[0], params[1], params[2])
273 self.assertTrue(mread.called)
274 self.assertTrue(mwrite.called)
276 def test_upd_missing_image_id(self):
277 self._test_missing_param(('compute', 'image_ref', 'test_image_id'),
278 'test_image_id', None)
280 def test_upd_missing_image_id_alt(self):
281 conf_utils.IMAGE_ID_ALT = 'test_image_id_alt'
282 self._test_missing_param(('compute', 'image_ref_alt',
283 'test_image_id_alt'), None, None)
285 def test_upd_missing_flavor_id(self):
286 setattr(config.CONF, 'tempest_use_custom_flavors', 'True')
287 self._test_missing_param(('compute', 'flavor_ref', 'test_flavor_id'),
288 None, 'test_flavor_id')
290 def test_upd_missing_flavor_id_alt(self):
291 setattr(config.CONF, 'tempest_use_custom_flavors', 'True')
292 conf_utils.FLAVOR_ID_ALT = 'test_flavor_id_alt'
293 self._test_missing_param(('compute', 'flavor_ref_alt',
294 'test_flavor_id_alt'), None, None)
296 def test_verif_missing_conf_file(self):
297 with mock.patch('functest.opnfv_tests.openstack.tempest.'
298 'conf_utils.os.path.isfile',
299 return_value=False), \
300 mock.patch('functest.opnfv_tests.openstack.tempest.'
301 'conf_utils.ft_utils.execute_command') as mexe, \
302 self.assertRaises(Exception) as context:
303 conf_utils.configure_verifier('test_dep_dir')
304 mexe.assert_any_call("rally verify configure-verifier")
305 msg = ("Tempest configuration file 'test_dep_dir/tempest.conf'"
307 self.assertTrue(msg in context.exception)
309 def test_configure_verifier_default(self):
310 with mock.patch('functest.opnfv_tests.openstack.tempest.'
311 'conf_utils.os.path.isfile',
312 return_value=True), \
313 mock.patch('functest.opnfv_tests.openstack.tempest.'
314 'conf_utils.ft_utils.execute_command') as mexe:
315 self.assertEqual(conf_utils.configure_verifier('test_dep_dir'),
316 'test_dep_dir/tempest.conf')
317 mexe.assert_any_call("rally verify configure-verifier "
321 if __name__ == "__main__":
322 logging.disable(logging.CRITICAL)
323 unittest.main(verbosity=2)