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 import functest.core.testcase_base as testcase_base
19 from functest.opnfv_tests.sdn.odl import odl
20 from functest.utils import functest_constants as ft_constants
23 class ODLTesting(unittest.TestCase):
25 logging.disable(logging.CRITICAL)
27 _keystone_ip = "127.0.0.1"
28 _neutron_ip = "127.0.0.2"
29 _sdn_controller_ip = "127.0.0.3"
30 _os_tenantname = "admin"
31 _os_username = "admin"
32 _os_password = "admin"
34 _odl_restconfport = "8181"
35 _odl_username = "admin"
36 _odl_password = "admin"
39 ft_constants.OS_USERNAME = self._os_username
40 ft_constants.OS_PASSWORD = self._os_password
41 ft_constants.OS_TENANT_NAME = self._os_tenantname
42 self.test = odl.ODLTests()
44 @mock.patch('fileinput.input', side_effect=Exception())
45 def test_set_robotframework_vars_failed(self, *args):
46 self.assertFalse(self.test.set_robotframework_vars())
48 @mock.patch('fileinput.input', return_value=[])
49 def test_set_robotframework_vars(self, args):
50 self.assertTrue(self.test.set_robotframework_vars())
53 def _fake_url_for(cls, service_type='identity', **kwargs):
54 if service_type == 'identity':
55 return "http://{}:5000/v2.0".format(
56 ODLTesting._keystone_ip)
57 elif service_type == 'network':
58 return "http://{}:9696".format(ODLTesting._neutron_ip)
63 def _get_fake_keystone_client(cls):
65 kclient.service_catalog = mock.Mock()
66 kclient.service_catalog.url_for = mock.Mock(
67 side_effect=cls._fake_url_for)
70 def _get_main_kwargs(self, key=None):
71 kwargs = {'odlusername': self._odl_username,
72 'odlpassword': self._odl_password,
73 'keystoneip': self._keystone_ip,
74 'neutronip': self._neutron_ip,
75 'osusername': self._os_username,
76 'ostenantname': self._os_tenantname,
77 'ospassword': self._os_password,
78 'odlip': self._sdn_controller_ip,
79 'odlwebport': self._odl_webport,
80 'odlrestconfport': self._odl_restconfport}
85 def _test_main(self, status, *args):
86 kwargs = self._get_main_kwargs()
87 self.assertEqual(self.test.main(**kwargs), status)
88 odl_res_dir = odl.ODLTests.res_dir
90 args[0].assert_called_once_with(odl_res_dir)
92 variable = ['KEYSTONE:{}'.format(self._keystone_ip),
93 'NEUTRON:{}'.format(self._neutron_ip),
94 'OSUSERNAME:"{}"'.format(self._os_username),
95 'OSTENANTNAME:"{}"'.format(self._os_tenantname),
96 'OSPASSWORD:"{}"'.format(self._os_password),
97 'ODL_SYSTEM_IP:{}'.format(self._sdn_controller_ip),
98 'PORT:{}'.format(self._odl_webport),
99 'RESTCONFPORT:{}'.format(self._odl_restconfport)]
100 output_file = os.path.join(odl_res_dir, 'output.xml')
101 args[1].assert_called_once_with(
102 odl.ODLTests.basic_suite_dir,
103 odl.ODLTests.neutron_suite_dir,
110 stdout_file = os.path.join(odl_res_dir, 'stdout.txt')
111 args[2].assert_called_with(stdout_file)
113 def _test_main_missing_keyword(self, key):
114 kwargs = self._get_main_kwargs(key)
115 self.assertEqual(self.test.main(**kwargs),
116 testcase_base.TestcaseBase.EX_RUN_ERROR)
118 def test_main_missing_odlusername(self):
119 self._test_main_missing_keyword('odlusername')
121 def test_main_missing_odlpassword(self):
122 self._test_main_missing_keyword('odlpassword')
124 def test_main_missing_keystoneip(self):
125 self._test_main_missing_keyword('keystoneip')
127 def test_main_missing_neutronip(self):
128 self._test_main_missing_keyword('neutronip')
130 def test_main_missing_osusername(self):
131 self._test_main_missing_keyword('osusername')
133 def test_main_missing_ostenantname(self):
134 self._test_main_missing_keyword('ostenantname')
136 def test_main_missing_ospassword(self):
137 self._test_main_missing_keyword('ospassword')
139 def test_main_missing_odlip(self):
140 self._test_main_missing_keyword('odlip')
142 def test_main_missing_odlwebport(self):
143 self._test_main_missing_keyword('odlwebport')
145 def test_main_missing_odlrestconfport(self):
146 self._test_main_missing_keyword('odlrestconfport')
148 def test_main_set_robotframework_vars_failed(self):
149 with mock.patch.object(self.test, 'set_robotframework_vars',
151 self._test_main(testcase_base.TestcaseBase.EX_RUN_ERROR)
152 self.test.set_robotframework_vars.assert_called_once_with(
153 self._odl_username, self._odl_password)
155 @mock.patch('os.makedirs', side_effect=Exception)
156 def test_main_makedirs_exception(self, mock_method):
157 with mock.patch.object(self.test, 'set_robotframework_vars',
158 return_value=True), \
159 self.assertRaises(Exception):
160 self._test_main(testcase_base.TestcaseBase.EX_RUN_ERROR,
163 @mock.patch('os.makedirs', side_effect=OSError)
164 def test_main_makedirs_oserror(self, mock_method):
165 with mock.patch.object(self.test, 'set_robotframework_vars',
167 self._test_main(testcase_base.TestcaseBase.EX_RUN_ERROR,
170 @mock.patch('robot.run', side_effect=RobotError)
171 @mock.patch('os.makedirs')
172 def test_main_robot_run_failed(self, *args):
173 with mock.patch.object(self.test, 'set_robotframework_vars',
174 return_value=True), \
175 self.assertRaises(RobotError):
176 self._test_main(testcase_base.TestcaseBase.EX_RUN_ERROR, *args)
178 @mock.patch('robot.run')
179 @mock.patch('os.makedirs')
180 def test_main_parse_results_failed(self, *args):
181 with mock.patch.object(self.test, 'set_robotframework_vars',
182 return_value=True), \
183 mock.patch.object(self.test, 'parse_results',
184 side_effect=RobotError):
185 self._test_main(testcase_base.TestcaseBase.EX_RUN_ERROR, *args)
187 @mock.patch('os.remove', side_effect=Exception)
188 @mock.patch('robot.run')
189 @mock.patch('os.makedirs')
190 def test_main_remove_exception(self, *args):
191 with mock.patch.object(self.test, 'set_robotframework_vars',
192 return_value=True), \
193 mock.patch.object(self.test, 'parse_results'), \
194 self.assertRaises(Exception):
195 self._test_main(testcase_base.TestcaseBase.EX_OK, *args)
197 @mock.patch('os.remove')
198 @mock.patch('robot.run')
199 @mock.patch('os.makedirs')
200 def test_main(self, *args):
201 with mock.patch.object(self.test, 'set_robotframework_vars',
202 return_value=True), \
203 mock.patch.object(self.test, 'parse_results',
204 return_value="PASS"):
205 self._test_main(testcase_base.TestcaseBase.EX_OK, *args)
207 @mock.patch('os.remove')
208 @mock.patch('robot.run')
209 @mock.patch('os.makedirs', side_effect=OSError(errno.EEXIST, ''))
210 def test_main_makedirs_oserror17(self, *args):
211 with mock.patch.object(self.test, 'set_robotframework_vars',
212 return_value=True), \
213 mock.patch.object(self.test, 'parse_results',
214 return_value="PASS"):
215 self._test_main(testcase_base.TestcaseBase.EX_OK, *args)
217 @mock.patch('os.remove')
218 @mock.patch('robot.run', return_value=1)
219 @mock.patch('os.makedirs')
220 def test_main_testcases_in_failure(self, *args):
221 with mock.patch.object(self.test, 'set_robotframework_vars',
222 return_value=True), \
223 mock.patch.object(self.test, 'parse_results',
224 return_value="PASS"):
225 self._test_main(testcase_base.TestcaseBase.EX_OK, *args)
227 @mock.patch('os.remove', side_effect=OSError)
228 @mock.patch('robot.run')
229 @mock.patch('os.makedirs')
230 def test_main_remove_oserror(self, *args):
231 with mock.patch.object(self.test, 'set_robotframework_vars',
232 return_value=True), \
233 mock.patch.object(self.test, 'parse_results',
234 return_value="PASS"):
235 self._test_main(testcase_base.TestcaseBase.EX_OK, *args)
237 def _test_run_missing_env_var(self, var):
238 if var == 'OS_USERNAME':
239 ft_constants.OS_USERNAME = None
240 elif var == 'OS_PASSWORD':
241 ft_constants.OS_PASSWORD = None
242 elif var == 'OS_TENANT_NAME':
243 ft_constants.OS_TENANT_NAME = None
245 self.assertEqual(self.test.run(),
246 testcase_base.TestcaseBase.EX_RUN_ERROR)
248 def _test_run(self, status=testcase_base.TestcaseBase.EX_OK,
249 exception=None, odlip="127.0.0.3", odlwebport="8080"):
250 with mock.patch('functest.utils.openstack_utils.get_keystone_client',
251 return_value=self._get_fake_keystone_client()):
253 self.test.main = mock.Mock(side_effect=exception)
255 self.test.main = mock.Mock(return_value=status)
256 self.assertEqual(self.test.run(), status)
257 self.test.main.assert_called_once_with(
258 keystoneip=self._keystone_ip, neutronip=self._neutron_ip,
259 odlip=odlip, odlpassword=self._odl_password,
260 odlrestconfport=self._odl_restconfport,
261 odlusername=self._odl_username, odlwebport=odlwebport,
262 ospassword=self._os_password, ostenantname=self._os_tenantname,
263 osusername=self._os_username)
265 def test_run_missing_os_username(self):
266 self._test_run_missing_env_var("OS_USERNAME")
268 def test_run_missing_os_password(self):
269 self._test_run_missing_env_var("OS_PASSWORD")
271 def test_run_missing_os_tenant_name(self):
272 self._test_run_missing_env_var("OS_TENANT_NAME")
274 def test_run_main_false(self):
275 ft_constants.CI_INSTALLER_TYPE = None
276 ft_constants.SDN_CONTROLLER_IP = self._sdn_controller_ip
277 self._test_run(testcase_base.TestcaseBase.EX_RUN_ERROR,
278 odlip=self._sdn_controller_ip,
279 odlwebport=self._odl_webport)
281 def test_run_main_exception(self):
282 ft_constants.CI_INSTALLER_TYPE = None
283 ft_constants.SDN_CONTROLLER_IP = self._sdn_controller_ip
284 with self.assertRaises(Exception):
285 self._test_run(status=testcase_base.TestcaseBase.EX_RUN_ERROR,
286 exception=Exception(),
287 odlip=self._sdn_controller_ip,
288 odlwebport=self._odl_webport)
290 def test_run_missing_sdn_controller_ip(self):
291 with mock.patch('functest.utils.openstack_utils.get_keystone_client',
292 return_value=self._get_fake_keystone_client()):
293 ft_constants.CI_INSTALLER_TYPE = None
294 ft_constants.SDN_CONTROLLER_IP = None
295 self.assertEqual(self.test.run(),
296 testcase_base.TestcaseBase.EX_RUN_ERROR)
298 def test_run_without_installer_type(self):
299 ft_constants.SDN_CONTROLLER_IP = self._sdn_controller_ip
300 ft_constants.CI_INSTALLER_TYPE = None
301 self._test_run(testcase_base.TestcaseBase.EX_OK,
302 odlip=self._sdn_controller_ip,
303 odlwebport=self._odl_webport)
305 def test_run_fuel(self):
306 ft_constants.CI_INSTALLER_TYPE = "fuel"
307 self._test_run(testcase_base.TestcaseBase.EX_OK,
308 odlip=self._neutron_ip, odlwebport='8282')
310 def test_run_apex_missing_sdn_controller_ip(self):
311 with mock.patch('functest.utils.openstack_utils.get_keystone_client',
312 return_value=self._get_fake_keystone_client()):
313 ft_constants.CI_INSTALLER_TYPE = "apex"
314 ft_constants.SDN_CONTROLLER_IP = None
315 self.assertEqual(self.test.run(),
316 testcase_base.TestcaseBase.EX_RUN_ERROR)
318 def test_run_apex(self):
319 ft_constants.SDN_CONTROLLER_IP = self._sdn_controller_ip
320 ft_constants.CI_INSTALLER_TYPE = "apex"
321 self._test_run(testcase_base.TestcaseBase.EX_OK,
322 odlip=self._sdn_controller_ip, odlwebport='8181')
324 def test_run_joid_missing_sdn_controller(self):
325 with mock.patch('functest.utils.openstack_utils.get_keystone_client',
326 return_value=self._get_fake_keystone_client()):
327 ft_constants.CI_INSTALLER_TYPE = "joid"
328 ft_constants.SDN_CONTROLLER = None
329 self.assertEqual(self.test.run(),
330 testcase_base.TestcaseBase.EX_RUN_ERROR)
332 def test_run_joid(self):
333 ft_constants.SDN_CONTROLLER = self._sdn_controller_ip
334 ft_constants.CI_INSTALLER_TYPE = "joid"
335 self._test_run(testcase_base.TestcaseBase.EX_OK,
336 odlip=self._sdn_controller_ip,
337 odlwebport=self._odl_webport)
339 def test_run_compass(self, *args):
340 ft_constants.CI_INSTALLER_TYPE = "compass"
341 self._test_run(testcase_base.TestcaseBase.EX_OK,
342 odlip=self._neutron_ip, odlwebport='8181')
345 if __name__ == "__main__":
346 unittest.main(verbosity=2)