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
13 from functest.opnfv_tests.openstack.tempest import tempest, conf_utils
14 from functest.utils.constants import CONST
15 from snaps.openstack.os_credentials import OSCreds
18 class OSTempestConfUtilsTesting(unittest.TestCase):
21 self.os_creds = OSCreds(
22 username='user', password='pass',
23 auth_url='http://foo.com:5000/v3', project_name='bar')
25 @mock.patch('snaps.openstack.utils.deploy_utils.create_project',
26 return_value=mock.Mock())
27 @mock.patch('snaps.openstack.utils.deploy_utils.create_user',
28 return_value=mock.Mock())
29 @mock.patch('snaps.openstack.utils.deploy_utils.create_network',
31 @mock.patch('snaps.openstack.utils.deploy_utils.create_image',
32 return_value=mock.Mock())
33 def test_create_tempest_resources_missing_network_dic(self, *mock_args):
34 tempest_resources = tempest.TempestResourcesManager(os_creds={})
35 with self.assertRaises(Exception) as context:
36 tempest_resources.create()
37 msg = 'Failed to create private network'
38 self.assertTrue(msg in context.exception)
40 @mock.patch('snaps.openstack.utils.deploy_utils.create_project',
41 return_value=mock.Mock())
42 @mock.patch('snaps.openstack.utils.deploy_utils.create_user',
43 return_value=mock.Mock())
44 @mock.patch('snaps.openstack.utils.deploy_utils.create_network',
45 return_value=mock.Mock())
46 @mock.patch('snaps.openstack.utils.deploy_utils.create_image',
48 def test_create_tempest_resources_missing_image(self, *mock_args):
49 tempest_resources = tempest.TempestResourcesManager(os_creds={})
51 CONST.__setattr__('tempest_use_custom_imagess', True)
52 with self.assertRaises(Exception) as context:
53 tempest_resources.create()
54 msg = 'Failed to create image'
55 self.assertTrue(msg in context.exception, msg=str(context.exception))
57 CONST.__setattr__('tempest_use_custom_imagess', False)
58 with self.assertRaises(Exception) as context:
59 tempest_resources.create(use_custom_images=True)
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.create_flavor.OpenStackFlavor.create',
73 def test_create_tempest_resources_missing_flavor(self, *mock_args):
74 tempest_resources = tempest.TempestResourcesManager(
75 os_creds=self.os_creds)
77 CONST.__setattr__('tempest_use_custom_images', True)
78 CONST.__setattr__('tempest_use_custom_flavors', True)
79 with self.assertRaises(Exception) as context:
80 tempest_resources.create()
81 msg = 'Failed to create flavor'
82 self.assertTrue(msg in context.exception, msg=str(context.exception))
84 CONST.__setattr__('tempest_use_custom_images', True)
85 CONST.__setattr__('tempest_use_custom_flavors', False)
86 with self.assertRaises(Exception) as context:
87 tempest_resources.create(use_custom_flavors=True)
88 msg = 'Failed to create flavor'
89 self.assertTrue(msg in context.exception, msg=str(context.exception))
91 def test_get_verifier_id_missing_verifier(self):
92 CONST.__setattr__('tempest_deployment_name', 'test_deploy_name')
93 with mock.patch('functest.opnfv_tests.openstack.tempest.'
94 'conf_utils.subprocess.Popen') as mock_popen, \
95 self.assertRaises(Exception):
96 mock_stdout = mock.Mock()
97 attrs = {'stdout.readline.return_value': ''}
98 mock_stdout.configure_mock(**attrs)
99 mock_popen.return_value = mock_stdout
100 conf_utils.get_verifier_id(),
102 def test_get_verifier_id_default(self):
103 CONST.__setattr__('tempest_deployment_name', 'test_deploy_name')
104 with mock.patch('functest.opnfv_tests.openstack.tempest.'
105 'conf_utils.subprocess.Popen') as mock_popen:
106 mock_stdout = mock.Mock()
107 attrs = {'stdout.readline.return_value': 'test_deploy_id'}
108 mock_stdout.configure_mock(**attrs)
109 mock_popen.return_value = mock_stdout
111 self.assertEqual(conf_utils.get_verifier_id(),
114 def test_get_verifier_deployment_id_missing_rally(self):
115 CONST.__setattr__('tempest_deployment_name', 'test_deploy_name')
116 with mock.patch('functest.opnfv_tests.openstack.tempest.'
117 'conf_utils.subprocess.Popen') as mock_popen, \
118 self.assertRaises(Exception):
119 mock_stdout = mock.Mock()
120 attrs = {'stdout.readline.return_value': ''}
121 mock_stdout.configure_mock(**attrs)
122 mock_popen.return_value = mock_stdout
123 conf_utils.get_verifier_deployment_id(),
125 def test_get_verifier_deployment_id_default(self):
126 CONST.__setattr__('tempest_deployment_name', 'test_deploy_name')
127 with mock.patch('functest.opnfv_tests.openstack.tempest.'
128 'conf_utils.subprocess.Popen') as mock_popen:
129 mock_stdout = mock.Mock()
130 attrs = {'stdout.readline.return_value': 'test_deploy_id'}
131 mock_stdout.configure_mock(**attrs)
132 mock_popen.return_value = mock_stdout
134 self.assertEqual(conf_utils.get_verifier_deployment_id(),
137 def test_get_verifier_repo_dir_default(self):
138 with mock.patch('functest.opnfv_tests.openstack.tempest.'
139 'conf_utils.os.path.join',
140 return_value='test_verifier_repo_dir'), \
141 mock.patch('functest.opnfv_tests.openstack.tempest.'
142 'conf_utils.get_verifier_id') as m:
143 self.assertEqual(conf_utils.get_verifier_repo_dir(''),
144 'test_verifier_repo_dir')
145 self.assertTrue(m.called)
147 def test_get_verifier_deployment_dir_default(self):
148 with mock.patch('functest.opnfv_tests.openstack.tempest.'
149 'conf_utils.os.path.join',
150 return_value='test_verifier_repo_dir'), \
151 mock.patch('functest.opnfv_tests.openstack.tempest.'
152 'conf_utils.get_verifier_id') as m1, \
153 mock.patch('functest.opnfv_tests.openstack.tempest.'
154 'conf_utils.get_verifier_deployment_id') as m2:
155 self.assertEqual(conf_utils.get_verifier_deployment_dir('', ''),
156 'test_verifier_repo_dir')
157 self.assertTrue(m1.called)
158 self.assertTrue(m2.called)
160 def test_backup_tempest_config_default(self):
161 with mock.patch('functest.opnfv_tests.openstack.tempest.'
162 'conf_utils.os.path.exists',
163 return_value=False), \
164 mock.patch('functest.opnfv_tests.openstack.tempest.'
165 'conf_utils.os.makedirs') as m1, \
166 mock.patch('functest.opnfv_tests.openstack.tempest.'
167 'conf_utils.shutil.copyfile') as m2:
168 conf_utils.backup_tempest_config('test_conf_file')
169 self.assertTrue(m1.called)
170 self.assertTrue(m2.called)
172 with mock.patch('functest.opnfv_tests.openstack.tempest.'
173 'conf_utils.os.path.exists',
174 return_value=True), \
175 mock.patch('functest.opnfv_tests.openstack.tempest.'
176 'conf_utils.shutil.copyfile') as m2:
177 conf_utils.backup_tempest_config('test_conf_file')
178 self.assertTrue(m2.called)
180 def test_configure_tempest_default(self):
181 with mock.patch('functest.opnfv_tests.openstack.tempest.'
182 'conf_utils.configure_verifier',
183 return_value='test_conf_file'), \
184 mock.patch('functest.opnfv_tests.openstack.tempest.'
185 'conf_utils.configure_tempest_update_params') as m1:
186 conf_utils.configure_tempest('test_dep_dir')
187 self.assertTrue(m1.called)
189 def test_configure_tempest_defcore_default(self):
190 with mock.patch('functest.opnfv_tests.openstack.tempest.'
191 'conf_utils.configure_verifier',
192 return_value='test_conf_file'), \
193 mock.patch('functest.opnfv_tests.openstack.tempest.'
194 'conf_utils.configure_tempest_update_params'), \
195 mock.patch('functest.opnfv_tests.openstack.tempest.'
196 'conf_utils.ConfigParser.RawConfigParser.'
198 mock.patch('functest.opnfv_tests.openstack.tempest.'
199 'conf_utils.ConfigParser.RawConfigParser.'
201 mock.patch('functest.opnfv_tests.openstack.tempest.'
202 'conf_utils.ConfigParser.RawConfigParser.'
203 'write') as mwrite, \
204 mock.patch('__builtin__.open', mock.mock_open()), \
205 mock.patch('functest.opnfv_tests.openstack.tempest.'
206 'conf_utils.generate_test_accounts_file'), \
207 mock.patch('functest.opnfv_tests.openstack.tempest.'
208 'conf_utils.shutil.copyfile'):
209 conf_utils.configure_tempest_defcore(
210 'test_dep_dir', 'test_image_id', 'test_flavor_id',
211 'test_image_alt_id', 'test_flavor_alt_id', 'test_tenant_id')
212 mset.assert_any_call('compute', 'image_ref', 'test_image_id')
213 mset.assert_any_call('compute', 'image_ref_alt',
215 mset.assert_any_call('compute', 'flavor_ref', 'test_flavor_id')
216 mset.assert_any_call('compute', 'flavor_ref_alt',
217 'test_flavor_alt_id')
218 self.assertTrue(mread.called)
219 self.assertTrue(mwrite.called)
221 def test_generate_test_accounts_file_default(self):
222 with mock.patch("__builtin__.open", mock.mock_open()), \
223 mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
224 'yaml.dump') as mock_dump:
225 conf_utils.generate_test_accounts_file('test_tenant_id')
226 self.assertTrue(mock_dump.called)
228 def _test_missing_param(self, params, image_id, flavor_id):
229 with mock.patch('functest.opnfv_tests.openstack.tempest.'
230 'conf_utils.ConfigParser.RawConfigParser.'
232 mock.patch('functest.opnfv_tests.openstack.tempest.'
233 'conf_utils.ConfigParser.RawConfigParser.'
235 mock.patch('functest.opnfv_tests.openstack.tempest.'
236 'conf_utils.ConfigParser.RawConfigParser.'
237 'write') as mwrite, \
238 mock.patch('__builtin__.open', mock.mock_open()), \
239 mock.patch('functest.opnfv_tests.openstack.tempest.'
240 'conf_utils.backup_tempest_config'), \
241 mock.patch('functest.utils.functest_utils.yaml.safe_load',
242 return_value={'validation': {'ssh_timeout': 300}}):
243 CONST.__setattr__('OS_ENDPOINT_TYPE', None)
245 configure_tempest_update_params('test_conf_file',
248 mset.assert_any_call(params[0], params[1], params[2])
249 self.assertTrue(mread.called)
250 self.assertTrue(mwrite.called)
252 def test_configure_tempest_update_params_missing_image_id(self):
253 CONST.__setattr__('tempest_use_custom_images', True)
254 self._test_missing_param(('compute', 'image_ref',
255 'test_image_id'), 'test_image_id',
258 def test_configure_tempest_update_params_missing_image_id_alt(self):
259 CONST.__setattr__('tempest_use_custom_images', True)
260 conf_utils.IMAGE_ID_ALT = 'test_image_id_alt'
261 self._test_missing_param(('compute', 'image_ref_alt',
262 'test_image_id_alt'), None, None)
264 def test_configure_tempest_update_params_missing_flavor_id(self):
265 CONST.__setattr__('tempest_use_custom_flavors', True)
266 self._test_missing_param(('compute', 'flavor_ref',
267 'test_flavor_id'), None,
270 def test_configure_tempest_update_params_missing_flavor_id_alt(self):
271 CONST.__setattr__('tempest_use_custom_flavors', True)
272 conf_utils.FLAVOR_ID_ALT = 'test_flavor_id_alt'
273 self._test_missing_param(('compute', 'flavor_ref_alt',
274 'test_flavor_id_alt'), None,
277 def test_configure_verifier_missing_temp_conf_file(self):
278 with mock.patch('functest.opnfv_tests.openstack.tempest.'
279 'conf_utils.os.path.isfile',
280 return_value=False), \
281 mock.patch('functest.opnfv_tests.openstack.tempest.'
282 'conf_utils.ft_utils.execute_command') as mexe, \
283 self.assertRaises(Exception) as context:
284 conf_utils.configure_verifier('test_dep_dir')
285 mexe.assert_any_call("rally verify configure-verifier")
286 msg = ("Tempest configuration file 'test_dep_dir/tempest.conf'"
288 self.assertTrue(msg in context)
290 def test_configure_verifier_default(self):
291 with mock.patch('functest.opnfv_tests.openstack.tempest.'
292 'conf_utils.os.path.isfile',
293 return_value=True), \
294 mock.patch('functest.opnfv_tests.openstack.tempest.'
295 'conf_utils.ft_utils.execute_command') as mexe:
296 self.assertEqual(conf_utils.configure_verifier('test_dep_dir'),
297 'test_dep_dir/tempest.conf')
298 mexe.assert_any_call("rally verify configure-verifier "
302 if __name__ == "__main__":
303 logging.disable(logging.CRITICAL)
304 unittest.main(verbosity=2)