0deef6bde25dcfe4625db125bb0cf690543cd496
[functest.git] / functest / tests / unit / odl / test_odl.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016 Orange and others.
4 #
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
9
10 import errno
11 import logging
12 import mock
13 import os
14 import unittest
15
16 from robot.errors import RobotError
17
18 from functest.core import testcase_base
19 from functest.opnfv_tests.sdn.odl import odl
20
21
22 class ODLTesting(unittest.TestCase):
23
24     logging.disable(logging.CRITICAL)
25
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"
32     _odl_webport = "8080"
33     _odl_restconfport = "8181"
34     _odl_username = "admin"
35     _odl_password = "admin"
36
37     def setUp(self):
38         for var in ("INSTALLER_TYPE", "SDN_CONTROLLER", "SDN_CONTROLLER_IP"):
39             if var in os.environ:
40                 del os.environ[var]
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()
45
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())
49
50     @mock.patch('fileinput.input', return_value=[])
51     def test_set_robotframework_vars(self, args):
52         self.assertTrue(self.test.set_robotframework_vars())
53
54     @classmethod
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)
61         else:
62             return None
63
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}
75         if key:
76             del kwargs[key]
77         return kwargs
78
79     def _test_main(self, status, *args):
80         kwargs = self._get_main_kwargs()
81         self.assertEqual(self.test.main(**kwargs), status)
82         if len(args) > 0:
83             args[0].assert_called_once_with(
84                 odl.ODLTests.res_dir)
85         if len(args) > 1:
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,
97                 log='NONE',
98                 output=os.path.join(odl.ODLTests.res_dir, 'output.xml'),
99                 report='NONE',
100                 stdout=mock.ANY,
101                 variable=variable)
102         if len(args) > 2:
103             args[2].assert_called_with(
104                 os.path.join(odl.ODLTests.res_dir, 'stdout.txt'))
105
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)
110
111     def test_main_missing_odlusername(self):
112         self._test_main_missing_keyword('odlusername')
113
114     def test_main_missing_odlpassword(self):
115         self._test_main_missing_keyword('odlpassword')
116
117     def test_main_missing_keystoneip(self):
118         self._test_main_missing_keyword('keystoneip')
119
120     def test_main_missing_neutronip(self):
121         self._test_main_missing_keyword('neutronip')
122
123     def test_main_missing_osusername(self):
124         self._test_main_missing_keyword('osusername')
125
126     def test_main_missing_ostenantname(self):
127         self._test_main_missing_keyword('ostenantname')
128
129     def test_main_missing_ospassword(self):
130         self._test_main_missing_keyword('ospassword')
131
132     def test_main_missing_odlip(self):
133         self._test_main_missing_keyword('odlip')
134
135     def test_main_missing_odlwebport(self):
136         self._test_main_missing_keyword('odlwebport')
137
138     def test_main_missing_odlrestconfport(self):
139         self._test_main_missing_keyword('odlrestconfport')
140
141     def test_main_set_robotframework_vars_failed(self):
142         with mock.patch.object(self.test, 'set_robotframework_vars',
143                                return_value=False):
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)
147
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,
154                             mock_method)
155
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',
159                                return_value=True):
160             self._test_main(testcase_base.TestcaseBase.EX_RUN_ERROR,
161                             mock_method)
162
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)
170
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)
179
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)
189
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)
198
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)
207
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)
216
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)
225
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):
229             del os.environ[var]
230             self.assertEqual(self.test.run(),
231                              testcase_base.TestcaseBase.EX_RUN_ERROR)
232
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):
237             if exception:
238                 self.test.main = mock.Mock(side_effect=exception)
239             else:
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)
249
250     def test_run_missing_os_username(self):
251         self._test_run_missing_env_var("OS_USERNAME")
252
253     def test_run_missing_os_password(self):
254         self._test_run_missing_env_var("OS_PASSWORD")
255
256     def test_run_missing_os_tenant_name(self):
257         self._test_run_missing_env_var("OS_TENANT_NAME")
258
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)
264
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)
272
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)
278
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)
284
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')
289
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)
296
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')
302
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)
309
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')
315
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')
320
321
322 if __name__ == "__main__":
323     unittest.main(verbosity=2)