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 testcase_base
19 from functest.opnfv_tests.sdn.odl import odl
22 class ODLTesting(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 = odl.ODLTests()
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 ODLTesting._keystone_ip)
59 elif service_type == 'network':
60 return "http://{}:9696".format(ODLTesting._neutron_ip)
64 def _get_main_kwargs(self, key=None):
65 kwargs = {'odlusername': self._odl_username,
66 'odlpassword': self._odl_password,
67 'keystoneip': self._keystone_ip,
68 'neutronip': self._neutron_ip,
69 'osusername': self._os_username,
70 'ostenantname': self._os_tenantname,
71 'ospassword': self._os_password,
72 'odlip': self._sdn_controller_ip,
73 'odlwebport': self._odl_webport,
74 'odlrestconfport': self._odl_restconfport}
79 def _test_main(self, status, *args):
80 kwargs = self._get_main_kwargs()
81 self.assertEqual(self.test.main(**kwargs), status)
83 args[0].assert_called_once_with(
86 variable = ['KEYSTONE:{}'.format(self._keystone_ip),
87 'NEUTRON:{}'.format(self._neutron_ip),
88 'OSUSERNAME:"{}"'.format(self._os_username),
89 'OSTENANTNAME:"{}"'.format(self._os_tenantname),
90 'OSPASSWORD:"{}"'.format(self._os_password),
91 'ODL_SYSTEM_IP:{}'.format(self._sdn_controller_ip),
92 'PORT:{}'.format(self._odl_webport),
93 'RESTCONFPORT:{}'.format(self._odl_restconfport)]
94 args[1].assert_called_once_with(
95 odl.ODLTests.basic_suite_dir,
96 odl.ODLTests.neutron_suite_dir,
98 output=os.path.join(odl.ODLTests.res_dir, 'output.xml'),
103 args[2].assert_called_with(
104 os.path.join(odl.ODLTests.res_dir, 'stdout.txt'))
106 def _test_main_missing_keyword(self, key):
107 kwargs = self._get_main_kwargs(key)
108 self.assertEqual(self.test.main(**kwargs),
109 testcase_base.TestcaseBase.EX_RUN_ERROR)
111 def test_main_missing_odlusername(self):
112 self._test_main_missing_keyword('odlusername')
114 def test_main_missing_odlpassword(self):
115 self._test_main_missing_keyword('odlpassword')
117 def test_main_missing_keystoneip(self):
118 self._test_main_missing_keyword('keystoneip')
120 def test_main_missing_neutronip(self):
121 self._test_main_missing_keyword('neutronip')
123 def test_main_missing_osusername(self):
124 self._test_main_missing_keyword('osusername')
126 def test_main_missing_ostenantname(self):
127 self._test_main_missing_keyword('ostenantname')
129 def test_main_missing_ospassword(self):
130 self._test_main_missing_keyword('ospassword')
132 def test_main_missing_odlip(self):
133 self._test_main_missing_keyword('odlip')
135 def test_main_missing_odlwebport(self):
136 self._test_main_missing_keyword('odlwebport')
138 def test_main_missing_odlrestconfport(self):
139 self._test_main_missing_keyword('odlrestconfport')
141 def test_main_set_robotframework_vars_failed(self):
142 with mock.patch.object(self.test, 'set_robotframework_vars',
144 self._test_main(testcase_base.TestcaseBase.EX_RUN_ERROR)
145 self.test.set_robotframework_vars.assert_called_once_with(
146 self._odl_username, self._odl_password)
148 @mock.patch('os.makedirs', side_effect=Exception)
149 def test_main_makedirs_exception(self, mock_method):
150 with mock.patch.object(self.test, 'set_robotframework_vars',
151 return_value=True), \
152 self.assertRaises(Exception):
153 self._test_main(testcase_base.TestcaseBase.EX_RUN_ERROR,
156 @mock.patch('os.makedirs', side_effect=OSError)
157 def test_main_makedirs_oserror(self, mock_method):
158 with mock.patch.object(self.test, 'set_robotframework_vars',
160 self._test_main(testcase_base.TestcaseBase.EX_RUN_ERROR,
163 @mock.patch('robot.run', side_effect=RobotError)
164 @mock.patch('os.makedirs')
165 def test_main_robot_run_failed(self, *args):
166 with mock.patch.object(self.test, 'set_robotframework_vars',
167 return_value=True), \
168 self.assertRaises(RobotError):
169 self._test_main(testcase_base.TestcaseBase.EX_RUN_ERROR, *args)
171 @mock.patch('robot.run')
172 @mock.patch('os.makedirs')
173 def test_main_parse_results_failed(self, *args):
174 with mock.patch.object(self.test, 'set_robotframework_vars',
175 return_value=True), \
176 mock.patch.object(self.test, 'parse_results',
177 side_effect=RobotError):
178 self._test_main(testcase_base.TestcaseBase.EX_RUN_ERROR, *args)
180 @mock.patch('os.remove', side_effect=Exception)
181 @mock.patch('robot.run')
182 @mock.patch('os.makedirs')
183 def test_main_remove_exception(self, *args):
184 with mock.patch.object(self.test, 'set_robotframework_vars',
185 return_value=True), \
186 mock.patch.object(self.test, 'parse_results'), \
187 self.assertRaises(Exception):
188 self._test_main(testcase_base.TestcaseBase.EX_OK, *args)
190 @mock.patch('os.remove')
191 @mock.patch('robot.run')
192 @mock.patch('os.makedirs')
193 def test_main(self, *args):
194 with mock.patch.object(self.test, 'set_robotframework_vars',
195 return_value=True), \
196 mock.patch.object(self.test, 'parse_results'):
197 self._test_main(testcase_base.TestcaseBase.EX_OK, *args)
199 @mock.patch('os.remove')
200 @mock.patch('robot.run')
201 @mock.patch('os.makedirs', side_effect=OSError(errno.EEXIST, ''))
202 def test_main_makedirs_oserror17(self, *args):
203 with mock.patch.object(self.test, 'set_robotframework_vars',
204 return_value=True), \
205 mock.patch.object(self.test, 'parse_results'):
206 self._test_main(testcase_base.TestcaseBase.EX_OK, *args)
208 @mock.patch('os.remove')
209 @mock.patch('robot.run', return_value=1)
210 @mock.patch('os.makedirs')
211 def test_main_testcases_in_failure(self, *args):
212 with mock.patch.object(self.test, 'set_robotframework_vars',
213 return_value=True), \
214 mock.patch.object(self.test, 'parse_results'):
215 self._test_main(testcase_base.TestcaseBase.EX_OK, *args)
217 @mock.patch('os.remove', side_effect=OSError)
218 @mock.patch('robot.run')
219 @mock.patch('os.makedirs')
220 def test_main_remove_oserror(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 self._test_main(testcase_base.TestcaseBase.EX_OK, *args)
226 def _test_run_missing_env_var(self, var):
227 with mock.patch('functest.utils.openstack_utils.get_endpoint',
228 side_effect=self._fake_url_for):
230 self.assertEqual(self.test.run(),
231 testcase_base.TestcaseBase.EX_RUN_ERROR)
233 def _test_run(self, status=testcase_base.TestcaseBase.EX_OK,
234 exception=None, odlip="127.0.0.3", odlwebport="8080"):
235 with mock.patch('functest.utils.openstack_utils.get_endpoint',
236 side_effect=self._fake_url_for):
238 self.test.main = mock.Mock(side_effect=exception)
240 self.test.main = mock.Mock(return_value=status)
241 self.assertEqual(self.test.run(), status)
242 self.test.main.assert_called_once_with(
243 keystoneip=self._keystone_ip, neutronip=self._neutron_ip,
244 odlip=odlip, odlpassword=self._odl_password,
245 odlrestconfport=self._odl_restconfport,
246 odlusername=self._odl_username, odlwebport=odlwebport,
247 ospassword=self._os_password, ostenantname=self._os_tenantname,
248 osusername=self._os_username)
250 def test_run_missing_os_username(self):
251 self._test_run_missing_env_var("OS_USERNAME")
253 def test_run_missing_os_password(self):
254 self._test_run_missing_env_var("OS_PASSWORD")
256 def test_run_missing_os_tenant_name(self):
257 self._test_run_missing_env_var("OS_TENANT_NAME")
259 def test_run_main_false(self):
260 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
261 self._test_run(testcase_base.TestcaseBase.EX_RUN_ERROR,
262 odlip=self._sdn_controller_ip,
263 odlwebport=self._odl_webport)
265 def test_run_main_exception(self):
266 with self.assertRaises(Exception):
267 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
268 self._test_run(status=testcase_base.TestcaseBase.EX_RUN_ERROR,
269 exception=Exception(),
270 odlip=self._sdn_controller_ip,
271 odlwebport=self._odl_webport)
273 def test_run_missing_sdn_controller_ip(self):
274 with mock.patch('functest.utils.openstack_utils.get_endpoint',
275 side_effect=self._fake_url_for):
276 self.assertEqual(self.test.run(),
277 testcase_base.TestcaseBase.EX_RUN_ERROR)
279 def test_run_without_installer_type(self):
280 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
281 self._test_run(testcase_base.TestcaseBase.EX_OK,
282 odlip=self._sdn_controller_ip,
283 odlwebport=self._odl_webport)
285 def test_run_fuel(self):
286 os.environ["INSTALLER_TYPE"] = "fuel"
287 self._test_run(testcase_base.TestcaseBase.EX_OK,
288 odlip=self._neutron_ip, odlwebport='8282')
290 def test_run_apex_missing_sdn_controller_ip(self):
291 with mock.patch('functest.utils.openstack_utils.get_endpoint',
292 side_effect=self._fake_url_for):
293 os.environ["INSTALLER_TYPE"] = "apex"
294 self.assertEqual(self.test.run(),
295 testcase_base.TestcaseBase.EX_RUN_ERROR)
297 def test_run_apex(self):
298 os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
299 os.environ["INSTALLER_TYPE"] = "apex"
300 self._test_run(testcase_base.TestcaseBase.EX_OK,
301 odlip=self._sdn_controller_ip, odlwebport='8181')
303 def test_run_joid_missing_sdn_controller(self):
304 with mock.patch('functest.utils.openstack_utils.get_endpoint',
305 side_effect=self._fake_url_for):
306 os.environ["INSTALLER_TYPE"] = "joid"
307 self.assertEqual(self.test.run(),
308 testcase_base.TestcaseBase.EX_RUN_ERROR)
310 def test_run_joid(self):
311 os.environ["SDN_CONTROLLER"] = self._sdn_controller_ip
312 os.environ["INSTALLER_TYPE"] = "joid"
313 self._test_run(testcase_base.TestcaseBase.EX_OK,
314 odlip=self._sdn_controller_ip, odlwebport='8080')
316 def test_run_compass(self, *args):
317 os.environ["INSTALLER_TYPE"] = "compass"
318 self._test_run(testcase_base.TestcaseBase.EX_OK,
319 odlip=self._neutron_ip, odlwebport='8181')
322 if __name__ == "__main__":
323 unittest.main(verbosity=2)