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.opnfv_tests.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('fileinput.input', side_effect=Exception())
47 def test_set_robotframework_vars_failed(self, *args):
48 self.assertFalse(self.test.set_robotframework_vars())
50 @mock.patch('fileinput.input', return_value=[])
51 def test_set_robotframework_vars(self, args):
52 self.assertTrue(self.test.set_robotframework_vars())
55 def _fake_url_for(cls, service_type='identity', **kwargs):
56 if service_type == 'identity':
57 return "http://{}:5000/v2.0".format(
58 ODLTestCasesTesting._keystone_ip)
59 elif service_type == 'network':
60 return "http://{}:9696".format(ODLTestCasesTesting._neutron_ip)
65 def _get_fake_keystone_client(cls):
67 kclient.service_catalog = mock.Mock()
68 kclient.service_catalog.url_for = mock.Mock(
69 side_effect=cls._fake_url_for)
72 def _get_main_kwargs(self, key=None):
73 kwargs = {'odlusername': self._odl_username,
74 'odlpassword': self._odl_password,
75 'keystoneip': self._keystone_ip,
76 'neutronip': self._neutron_ip,
77 'osusername': self._os_username,
78 'ostenantname': self._os_tenantname,
79 'ospassword': self._os_password,
80 'odlip': self._sdn_controller_ip,
81 'odlwebport': self._odl_webport,
82 'odlrestconfport': self._odl_restconfport}
87 def _test_main(self, status, *args):
88 kwargs = self._get_main_kwargs()
89 self.assertEqual(self.test.main(**kwargs), status)
91 args[0].assert_called_once_with(
92 OpenDaylightTesting.ODLTestCases.res_dir)
94 variable = ['KEYSTONE:{}'.format(self._keystone_ip),
95 'NEUTRON:{}'.format(self._neutron_ip),
96 'OSUSERNAME:"{}"'.format(self._os_username),
97 'OSTENANTNAME:"{}"'.format(self._os_tenantname),
98 'OSPASSWORD:"{}"'.format(self._os_password),
99 'ODL_SYSTEM_IP:{}'.format(self._sdn_controller_ip),
100 'PORT:{}'.format(self._odl_webport),
101 'RESTCONFPORT:{}'.format(self._odl_restconfport)]
102 args[1].assert_called_once_with(
103 OpenDaylightTesting.ODLTestCases.basic_suite_dir,
104 OpenDaylightTesting.ODLTestCases.neutron_suite_dir,
106 output=OpenDaylightTesting.ODLTestCases.res_dir + 'output.xml',
111 args[2].assert_called_with(
112 OpenDaylightTesting.ODLTestCases.res_dir + 'stdout.txt')
114 def _test_main_missing_keyword(self, key):
115 kwargs = self._get_main_kwargs(key)
116 self.assertEqual(self.test.main(**kwargs),
117 TestCasesBase.TestCasesBase.EX_RUN_ERROR)
119 def test_main_missing_odlusername(self):
120 self._test_main_missing_keyword('odlusername')
122 def test_main_missing_odlpassword(self):
123 self._test_main_missing_keyword('odlpassword')
125 def test_main_missing_keystoneip(self):
126 self._test_main_missing_keyword('keystoneip')
128 def test_main_missing_neutronip(self):
129 self._test_main_missing_keyword('neutronip')
131 def test_main_missing_osusername(self):
132 self._test_main_missing_keyword('osusername')
134 def test_main_missing_ostenantname(self):
135 self._test_main_missing_keyword('ostenantname')
137 def test_main_missing_ospassword(self):
138 self._test_main_missing_keyword('ospassword')
140 def test_main_missing_odlip(self):
141 self._test_main_missing_keyword('odlip')
143 def test_main_missing_odlwebport(self):
144 self._test_main_missing_keyword('odlwebport')
146 def test_main_missing_odlrestconfport(self):
147 self._test_main_missing_keyword('odlrestconfport')
149 def test_main_set_robotframework_vars_failed(self):
150 with mock.patch.object(self.test, 'set_robotframework_vars',
152 self._test_main(TestCasesBase.TestCasesBase.EX_RUN_ERROR)
153 self.test.set_robotframework_vars.assert_called_once_with(
154 self._odl_username, self._odl_password)
156 @mock.patch('os.makedirs', side_effect=Exception)
157 def test_main_makedirs_exception(self, mock_method):
158 with mock.patch.object(self.test, 'set_robotframework_vars',
159 return_value=True), \
160 self.assertRaises(Exception):
161 self._test_main(TestCasesBase.TestCasesBase.EX_RUN_ERROR,
164 @mock.patch('os.makedirs', side_effect=OSError)
165 def test_main_makedirs_oserror(self, mock_method):
166 with mock.patch.object(self.test, 'set_robotframework_vars',
168 self._test_main(TestCasesBase.TestCasesBase.EX_RUN_ERROR,
171 @mock.patch('robot.run', side_effect=RobotError)
172 @mock.patch('os.makedirs')
173 def test_main_robot_run_failed(self, *args):
174 with mock.patch.object(self.test, 'set_robotframework_vars',
175 return_value=True), \
176 self.assertRaises(RobotError):
177 self._test_main(TestCasesBase.TestCasesBase.EX_RUN_ERROR, *args)
179 @mock.patch('robot.run')
180 @mock.patch('os.makedirs')
181 def test_main_parse_results_failed(self, *args):
182 with mock.patch.object(self.test, 'set_robotframework_vars',
183 return_value=True), \
184 mock.patch.object(self.test, 'parse_results',
185 side_effect=RobotError):
186 self._test_main(TestCasesBase.TestCasesBase.EX_RUN_ERROR, *args)
188 @mock.patch('os.remove', side_effect=Exception)
189 @mock.patch('robot.run')
190 @mock.patch('os.makedirs')
191 def test_main_remove_exception(self, *args):
192 with mock.patch.object(self.test, 'set_robotframework_vars',
193 return_value=True), \
194 mock.patch.object(self.test, 'parse_results'), \
195 self.assertRaises(Exception):
196 self._test_main(TestCasesBase.TestCasesBase.EX_OK, *args)
198 @mock.patch('os.remove')
199 @mock.patch('robot.run')
200 @mock.patch('os.makedirs')
201 def test_main(self, *args):
202 with mock.patch.object(self.test, 'set_robotframework_vars',
203 return_value=True), \
204 mock.patch.object(self.test, 'parse_results'):
205 self._test_main(TestCasesBase.TestCasesBase.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 self._test_main(TestCasesBase.TestCasesBase.EX_OK, *args)
216 @mock.patch('os.remove')
217 @mock.patch('robot.run', return_value=1)
218 @mock.patch('os.makedirs')
219 def test_main_testcases_in_failure(self, *args):
220 with mock.patch.object(self.test, 'set_robotframework_vars',
221 return_value=True), \
222 mock.patch.object(self.test, 'parse_results'):
223 self._test_main(TestCasesBase.TestCasesBase.EX_OK, *args)
225 @mock.patch('os.remove', side_effect=OSError)
226 @mock.patch('robot.run')
227 @mock.patch('os.makedirs')
228 def test_main_remove_oserror(self, *args):
229 with mock.patch.object(self.test, 'set_robotframework_vars',
230 return_value=True), \
231 mock.patch.object(self.test, 'parse_results'):
232 self._test_main(TestCasesBase.TestCasesBase.EX_OK, *args)
234 def _test_run_missing_env_var(self, var):
236 self.assertEqual(self.test.run(),
237 TestCasesBase.TestCasesBase.EX_RUN_ERROR)
239 def _test_run(self, status=TestCasesBase.TestCasesBase.EX_OK,
240 exception=None, odlip="127.0.0.3", odlwebport="8080"):
241 with mock.patch('functest.utils.openstack_utils.get_keystone_client',
242 return_value=self._get_fake_keystone_client()):
244 self.test.main = mock.Mock(side_effect=exception)
246 self.test.main = mock.Mock(return_value=status)
247 self.assertEqual(self.test.run(), status)
248 self.test.main.assert_called_once_with(
249 keystoneip=self._keystone_ip, neutronip=self._neutron_ip,
250 odlip=odlip, odlpassword=self._odl_password,
251 odlrestconfport=self._odl_restconfport,
252 odlusername=self._odl_username, odlwebport=odlwebport,
253 ospassword=self._os_password, ostenantname=self._os_tenantname,
254 osusername=self._os_username)
256 def test_run_missing_os_username(self):
257 self._test_run_missing_env_var("OS_USERNAME")
259 def test_run_missing_os_password(self):
260 self._test_run_missing_env_var("OS_PASSWORD")
262 def test_run_missing_os_tenant_name(self):
263 self._test_run_missing_env_var("OS_TENANT_NAME")
265 def test_run_main_false(self):
266 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
267 self._test_run(TestCasesBase.TestCasesBase.EX_RUN_ERROR,
268 odlip=self._sdn_controller_ip,
269 odlwebport=self._odl_webport)
271 def test_run_main_exception(self):
272 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
273 with self.assertRaises(Exception):
274 self._test_run(status=TestCasesBase.TestCasesBase.EX_RUN_ERROR,
275 exception=Exception(),
276 odlip=self._sdn_controller_ip,
277 odlwebport=self._odl_webport)
279 def test_run_missing_sdn_controller_ip(self):
280 with mock.patch('functest.utils.openstack_utils.get_keystone_client',
281 return_value=self._get_fake_keystone_client()):
282 self.assertEqual(self.test.run(),
283 TestCasesBase.TestCasesBase.EX_RUN_ERROR)
285 def test_run_without_installer_type(self):
286 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
287 self._test_run(TestCasesBase.TestCasesBase.EX_OK,
288 odlip=self._sdn_controller_ip,
289 odlwebport=self._odl_webport)
291 def test_run_fuel(self):
292 os.environ["INSTALLER_TYPE"] = "fuel"
293 self._test_run(TestCasesBase.TestCasesBase.EX_OK,
294 odlip=self._neutron_ip, odlwebport='8282')
296 def test_run_apex_missing_sdn_controller_ip(self):
297 with mock.patch('functest.utils.openstack_utils.get_keystone_client',
298 return_value=self._get_fake_keystone_client()):
299 os.environ["INSTALLER_TYPE"] = "apex"
300 self.assertEqual(self.test.run(),
301 TestCasesBase.TestCasesBase.EX_RUN_ERROR)
303 def test_run_apex(self):
304 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
305 os.environ["INSTALLER_TYPE"] = "apex"
306 self._test_run(TestCasesBase.TestCasesBase.EX_OK,
307 odlip=self._sdn_controller_ip, odlwebport='8181')
309 def test_run_joid_missing_sdn_controller(self):
310 with mock.patch('functest.utils.openstack_utils.get_keystone_client',
311 return_value=self._get_fake_keystone_client()):
312 os.environ["INSTALLER_TYPE"] = "joid"
313 self.assertEqual(self.test.run(),
314 TestCasesBase.TestCasesBase.EX_RUN_ERROR)
316 def test_run_joid(self):
317 os.environ["SDN_CONTROLLER"] = self._sdn_controller_ip
318 os.environ["INSTALLER_TYPE"] = "joid"
319 self._test_run(TestCasesBase.TestCasesBase.EX_OK,
320 odlip=self._sdn_controller_ip,
321 odlwebport=self._odl_webport)
323 def test_run_compass(self, *args):
324 os.environ["INSTALLER_TYPE"] = "compass"
325 self._test_run(TestCasesBase.TestCasesBase.EX_OK,
326 odlip=self._neutron_ip, odlwebport='8181')
329 if __name__ == "__main__":
330 unittest.main(verbosity=2)