3 # Copyright (c) 2016 Orange and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
16 from robot.errors import RobotError
18 from functest.core import TestCasesBase
19 from functest.testcases.Controllers.ODL import OpenDaylightTesting
22 class ODLTestCasesTesting(unittest.TestCase):
24 logging.disable(logging.CRITICAL)
26 _keystone_ip = "127.0.0.1"
27 _neutron_ip = "127.0.0.2"
28 _sdn_controller_ip = "127.0.0.3"
29 _os_tenantname = "admin"
30 _os_username = "admin"
31 _os_password = "admin"
33 _odl_restconfport = "8181"
34 _odl_username = "admin"
35 _odl_password = "admin"
38 for var in ("INSTALLER_TYPE", "SDN_CONTROLLER", "SDN_CONTROLLER_IP"):
41 os.environ["OS_USERNAME"] = self._os_username
42 os.environ["OS_PASSWORD"] = self._os_password
43 os.environ["OS_TENANT_NAME"] = self._os_tenantname
44 self.test = OpenDaylightTesting.ODLTestCases()
46 @mock.patch('shutil.copy', side_effect=Exception())
47 def test_copy_opnf_testcases_exception(self, *args):
48 with self.assertRaises(Exception):
49 self.test.copy_opnf_testcases()
51 @mock.patch('shutil.copy', side_effect=IOError())
52 def test_copy_opnf_testcases_ioerror(self, *args):
53 self.assertFalse(self.test.copy_opnf_testcases())
55 @mock.patch('shutil.copy')
56 def test_copy_opnf_testcases(self, *args):
57 self.assertTrue(self.test.copy_opnf_testcases())
59 @mock.patch('fileinput.input', side_effect=Exception())
60 def test_set_robotframework_vars_failed(self, *args):
61 self.assertFalse(self.test.set_robotframework_vars())
63 @mock.patch('fileinput.input', return_value=[])
64 def test_set_robotframework_vars(self, args):
65 self.assertTrue(self.test.set_robotframework_vars())
68 def _fake_url_for(cls, service_type='identity', **kwargs):
69 if service_type == 'identity':
70 return "http://{}:5000/v2.0".format(
71 ODLTestCasesTesting._keystone_ip)
72 elif service_type == 'network':
73 return "http://{}:9696".format(ODLTestCasesTesting._neutron_ip)
78 def _get_fake_keystone_client(cls):
80 kclient.service_catalog = mock.Mock()
81 kclient.service_catalog.url_for = mock.Mock(
82 side_effect=cls._fake_url_for)
85 def _get_main_kwargs(self, key=None):
86 kwargs = {'odlusername': self._odl_username,
87 'odlpassword': self._odl_password,
88 'keystoneip': self._keystone_ip,
89 'neutronip': self._neutron_ip,
90 'osusername': self._os_username,
91 'ostenantname': self._os_tenantname,
92 'ospassword': self._os_password,
93 'odlip': self._sdn_controller_ip,
94 'odlwebport': self._odl_webport,
95 'odlrestconfport': self._odl_restconfport}
100 def _test_main(self, status, *args):
101 kwargs = self._get_main_kwargs()
102 self.assertEqual(self.test.main(**kwargs), status)
104 args[0].assert_called_once_with(
105 OpenDaylightTesting.ODLTestCases.res_dir)
107 variable = ['KEYSTONE:{}'.format(self._keystone_ip),
108 'NEUTRON:{}'.format(self._neutron_ip),
109 'OSUSERNAME:"{}"'.format(self._os_username),
110 'OSTENANTNAME:"{}"'.format(self._os_tenantname),
111 'OSPASSWORD:"{}"'.format(self._os_password),
112 'ODL_SYSTEM_IP:{}'.format(self._sdn_controller_ip),
113 'PORT:{}'.format(self._odl_webport),
114 'RESTCONFPORT:{}'.format(self._odl_restconfport)]
115 args[1].assert_called_once_with(
116 OpenDaylightTesting.ODLTestCases.basic_suite_dir,
117 OpenDaylightTesting.ODLTestCases.neutron_suite_dir,
119 output=OpenDaylightTesting.ODLTestCases.res_dir + 'output.xml',
124 args[2].assert_called_with(
125 OpenDaylightTesting.ODLTestCases.res_dir + 'stdout.txt')
127 def _test_main_missing_keyword(self, key):
128 kwargs = self._get_main_kwargs(key)
129 self.assertEqual(self.test.main(**kwargs),
130 TestCasesBase.TestCasesBase.EX_RUN_ERROR)
132 def test_main_missing_odlusername(self):
133 self._test_main_missing_keyword('odlusername')
135 def test_main_missing_odlpassword(self):
136 self._test_main_missing_keyword('odlpassword')
138 def test_main_missing_keystoneip(self):
139 self._test_main_missing_keyword('keystoneip')
141 def test_main_missing_neutronip(self):
142 self._test_main_missing_keyword('neutronip')
144 def test_main_missing_osusername(self):
145 self._test_main_missing_keyword('osusername')
147 def test_main_missing_ostenantname(self):
148 self._test_main_missing_keyword('ostenantname')
150 def test_main_missing_ospassword(self):
151 self._test_main_missing_keyword('ospassword')
153 def test_main_missing_odlip(self):
154 self._test_main_missing_keyword('odlip')
156 def test_main_missing_odlwebport(self):
157 self._test_main_missing_keyword('odlwebport')
159 def test_main_missing_odlrestconfport(self):
160 self._test_main_missing_keyword('odlrestconfport')
162 def test_main_copy_opnf_testcases_failed(self):
163 with mock.patch.object(self.test, 'copy_opnf_testcases',
165 self._test_main(TestCasesBase.TestCasesBase.EX_RUN_ERROR)
166 self.test.copy_opnf_testcases.assert_called_once_with()
168 def test_main_set_robotframework_vars_failed(self):
169 with mock.patch.object(self.test, 'copy_opnf_testcases',
170 return_value=True), \
171 mock.patch.object(self.test, 'set_robotframework_vars',
173 self._test_main(TestCasesBase.TestCasesBase.EX_RUN_ERROR)
174 self.test.set_robotframework_vars.assert_called_once_with(
175 self._odl_username, self._odl_password)
177 @mock.patch('os.makedirs', side_effect=Exception)
178 def test_main_makedirs_exception(self, mock_method):
179 with mock.patch.object(self.test,
180 'copy_opnf_testcases', return_value=True), \
181 mock.patch.object(self.test, 'set_robotframework_vars',
182 return_value=True), \
183 self.assertRaises(Exception):
184 self._test_main(TestCasesBase.TestCasesBase.EX_RUN_ERROR,
187 @mock.patch('os.makedirs', side_effect=OSError)
188 def test_main_makedirs_oserror(self, mock_method):
189 with mock.patch.object(self.test,
190 'copy_opnf_testcases', return_value=True), \
191 mock.patch.object(self.test, 'set_robotframework_vars',
193 self._test_main(TestCasesBase.TestCasesBase.EX_RUN_ERROR,
196 @mock.patch('robot.run', side_effect=RobotError)
197 @mock.patch('os.makedirs')
198 def test_main_robot_run_failed(self, *args):
199 with mock.patch.object(self.test, 'copy_opnf_testcases',
200 return_value=True), \
201 mock.patch.object(self.test, 'set_robotframework_vars',
202 return_value=True), \
203 self.assertRaises(RobotError):
204 self._test_main(TestCasesBase.TestCasesBase.EX_RUN_ERROR, *args)
206 @mock.patch('robot.run')
207 @mock.patch('os.makedirs')
208 def test_main_parse_results_failed(self, *args):
209 with mock.patch.object(self.test, 'copy_opnf_testcases',
210 return_value=True), \
211 mock.patch.object(self.test, 'set_robotframework_vars',
212 return_value=True), \
213 mock.patch.object(self.test, 'parse_results',
214 side_effect=RobotError):
215 self._test_main(TestCasesBase.TestCasesBase.EX_RUN_ERROR, *args)
217 @mock.patch('os.remove', side_effect=Exception)
218 @mock.patch('robot.run')
219 @mock.patch('os.makedirs')
220 def test_main_remove_exception(self, *args):
221 with mock.patch.object(self.test, 'copy_opnf_testcases',
222 return_value=True), \
223 mock.patch.object(self.test, 'set_robotframework_vars',
224 return_value=True), \
225 mock.patch.object(self.test, 'parse_results'), \
226 self.assertRaises(Exception):
227 self._test_main(TestCasesBase.TestCasesBase.EX_OK, *args)
229 @mock.patch('os.remove')
230 @mock.patch('robot.run')
231 @mock.patch('os.makedirs')
232 def test_main(self, *args):
233 with mock.patch.object(self.test, 'copy_opnf_testcases',
234 return_value=True), \
235 mock.patch.object(self.test, 'set_robotframework_vars',
236 return_value=True), \
237 mock.patch.object(self.test, 'parse_results'):
238 self._test_main(TestCasesBase.TestCasesBase.EX_OK, *args)
240 @mock.patch('os.remove')
241 @mock.patch('robot.run')
242 @mock.patch('os.makedirs', side_effect=OSError(errno.EEXIST, ''))
243 def test_main_makedirs_oserror17(self, *args):
244 with mock.patch.object(self.test, 'copy_opnf_testcases',
245 return_value=True), \
246 mock.patch.object(self.test, 'set_robotframework_vars',
247 return_value=True), \
248 mock.patch.object(self.test, 'parse_results'):
249 self._test_main(TestCasesBase.TestCasesBase.EX_OK, *args)
251 @mock.patch('os.remove')
252 @mock.patch('robot.run', return_value=1)
253 @mock.patch('os.makedirs')
254 def test_main_testcases_in_failure(self, *args):
255 with mock.patch.object(self.test, 'copy_opnf_testcases',
256 return_value=True), \
257 mock.patch.object(self.test, 'set_robotframework_vars',
258 return_value=True), \
259 mock.patch.object(self.test, 'parse_results'):
260 self._test_main(TestCasesBase.TestCasesBase.EX_OK, *args)
262 @mock.patch('os.remove', side_effect=OSError)
263 @mock.patch('robot.run')
264 @mock.patch('os.makedirs')
265 def test_main_remove_oserror(self, *args):
266 with mock.patch.object(self.test, 'copy_opnf_testcases',
267 return_value=True), \
268 mock.patch.object(self.test, 'set_robotframework_vars',
269 return_value=True), \
270 mock.patch.object(self.test, 'parse_results'):
271 self._test_main(TestCasesBase.TestCasesBase.EX_OK, *args)
273 def _test_run_missing_env_var(self, var):
275 self.assertEqual(self.test.run(),
276 TestCasesBase.TestCasesBase.EX_RUN_ERROR)
278 def _test_run(self, status=TestCasesBase.TestCasesBase.EX_OK,
279 exception=None, odlip="127.0.0.3", odlwebport="8080"):
280 with mock.patch('functest.utils.openstack_utils.get_keystone_client',
281 return_value=self._get_fake_keystone_client()):
283 self.test.main = mock.Mock(side_effect=exception)
285 self.test.main = mock.Mock(return_value=status)
286 self.assertEqual(self.test.run(), status)
287 self.test.main.assert_called_once_with(
288 keystoneip=self._keystone_ip, neutronip=self._neutron_ip,
289 odlip=odlip, odlpassword=self._odl_password,
290 odlrestconfport=self._odl_restconfport,
291 odlusername=self._odl_username, odlwebport=odlwebport,
292 ospassword=self._os_password, ostenantname=self._os_tenantname,
293 osusername=self._os_username)
295 def test_run_missing_os_username(self):
296 self._test_run_missing_env_var("OS_USERNAME")
298 def test_run_missing_os_password(self):
299 self._test_run_missing_env_var("OS_PASSWORD")
301 def test_run_missing_os_tenant_name(self):
302 self._test_run_missing_env_var("OS_TENANT_NAME")
304 def test_run_main_false(self):
305 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
306 self._test_run(TestCasesBase.TestCasesBase.EX_RUN_ERROR,
307 odlip=self._sdn_controller_ip,
308 odlwebport=self._odl_webport)
310 def test_run_main_exception(self):
311 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
312 with self.assertRaises(Exception):
313 self._test_run(status=TestCasesBase.TestCasesBase.EX_RUN_ERROR,
314 exception=Exception(),
315 odlip=self._sdn_controller_ip,
316 odlwebport=self._odl_webport)
318 def test_run_missing_sdn_controller_ip(self):
319 with mock.patch('functest.utils.openstack_utils.get_keystone_client',
320 return_value=self._get_fake_keystone_client()):
321 self.assertEqual(self.test.run(),
322 TestCasesBase.TestCasesBase.EX_RUN_ERROR)
324 def test_run_without_installer_type(self):
325 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
326 self._test_run(TestCasesBase.TestCasesBase.EX_OK,
327 odlip=self._sdn_controller_ip,
328 odlwebport=self._odl_webport)
330 def test_run_fuel(self):
331 os.environ["INSTALLER_TYPE"] = "fuel"
332 self._test_run(TestCasesBase.TestCasesBase.EX_OK,
333 odlip=self._neutron_ip, odlwebport='8282')
335 def test_run_apex_missing_sdn_controller_ip(self):
336 with mock.patch('functest.utils.openstack_utils.get_keystone_client',
337 return_value=self._get_fake_keystone_client()):
338 os.environ["INSTALLER_TYPE"] = "apex"
339 self.assertEqual(self.test.run(),
340 TestCasesBase.TestCasesBase.EX_RUN_ERROR)
342 def test_run_apex(self):
343 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
344 os.environ["INSTALLER_TYPE"] = "apex"
345 self._test_run(TestCasesBase.TestCasesBase.EX_OK,
346 odlip=self._sdn_controller_ip, odlwebport='8181')
348 def test_run_joid_missing_sdn_controller(self):
349 with mock.patch('functest.utils.openstack_utils.get_keystone_client',
350 return_value=self._get_fake_keystone_client()):
351 os.environ["INSTALLER_TYPE"] = "joid"
352 self.assertEqual(self.test.run(),
353 TestCasesBase.TestCasesBase.EX_RUN_ERROR)
355 def test_run_joid(self):
356 os.environ["SDN_CONTROLLER"] = self._sdn_controller_ip
357 os.environ["INSTALLER_TYPE"] = "joid"
358 self._test_run(TestCasesBase.TestCasesBase.EX_OK,
359 odlip=self._sdn_controller_ip,
360 odlwebport=self._odl_webport)
362 def test_run_compass(self, *args):
363 os.environ["INSTALLER_TYPE"] = "compass"
364 self._test_run(TestCasesBase.TestCasesBase.EX_OK,
365 odlip=self._neutron_ip, odlwebport='8181')
368 if __name__ == "__main__":
369 unittest.main(verbosity=2)