Repo structure modification
[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 TestCasesBase
19 from functest.opnfv_tests.Controllers.ODL import OpenDaylightTesting
20
21
22 class ODLTestCasesTesting(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 = OpenDaylightTesting.ODLTestCases()
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                 ODLTestCasesTesting._keystone_ip)
59         elif service_type == 'network':
60             return "http://{}:9696".format(ODLTestCasesTesting._neutron_ip)
61         else:
62             return None
63
64     @classmethod
65     def _get_fake_keystone_client(cls):
66         kclient = mock.Mock()
67         kclient.service_catalog = mock.Mock()
68         kclient.service_catalog.url_for = mock.Mock(
69             side_effect=cls._fake_url_for)
70         return kclient
71
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}
83         if key:
84             del kwargs[key]
85         return kwargs
86
87     def _test_main(self, status, *args):
88         kwargs = self._get_main_kwargs()
89         self.assertEqual(self.test.main(**kwargs), status)
90         if len(args) > 0:
91             args[0].assert_called_once_with(
92                 OpenDaylightTesting.ODLTestCases.res_dir)
93         if len(args) > 1:
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,
105                 log='NONE',
106                 output=OpenDaylightTesting.ODLTestCases.res_dir + 'output.xml',
107                 report='NONE',
108                 stdout=mock.ANY,
109                 variable=variable)
110         if len(args) > 2:
111             args[2].assert_called_with(
112                 OpenDaylightTesting.ODLTestCases.res_dir + 'stdout.txt')
113
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)
118
119     def test_main_missing_odlusername(self):
120         self._test_main_missing_keyword('odlusername')
121
122     def test_main_missing_odlpassword(self):
123         self._test_main_missing_keyword('odlpassword')
124
125     def test_main_missing_keystoneip(self):
126         self._test_main_missing_keyword('keystoneip')
127
128     def test_main_missing_neutronip(self):
129         self._test_main_missing_keyword('neutronip')
130
131     def test_main_missing_osusername(self):
132         self._test_main_missing_keyword('osusername')
133
134     def test_main_missing_ostenantname(self):
135         self._test_main_missing_keyword('ostenantname')
136
137     def test_main_missing_ospassword(self):
138         self._test_main_missing_keyword('ospassword')
139
140     def test_main_missing_odlip(self):
141         self._test_main_missing_keyword('odlip')
142
143     def test_main_missing_odlwebport(self):
144         self._test_main_missing_keyword('odlwebport')
145
146     def test_main_missing_odlrestconfport(self):
147         self._test_main_missing_keyword('odlrestconfport')
148
149     def test_main_set_robotframework_vars_failed(self):
150         with mock.patch.object(self.test, 'set_robotframework_vars',
151                                return_value=False):
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)
155
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,
162                             mock_method)
163
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',
167                                return_value=True):
168             self._test_main(TestCasesBase.TestCasesBase.EX_RUN_ERROR,
169                             mock_method)
170
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)
178
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)
187
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)
197
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)
206
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)
215
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)
224
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)
233
234     def _test_run_missing_env_var(self, var):
235         del os.environ[var]
236         self.assertEqual(self.test.run(),
237                          TestCasesBase.TestCasesBase.EX_RUN_ERROR)
238
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()):
243             if exception:
244                 self.test.main = mock.Mock(side_effect=exception)
245             else:
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)
255
256     def test_run_missing_os_username(self):
257         self._test_run_missing_env_var("OS_USERNAME")
258
259     def test_run_missing_os_password(self):
260         self._test_run_missing_env_var("OS_PASSWORD")
261
262     def test_run_missing_os_tenant_name(self):
263         self._test_run_missing_env_var("OS_TENANT_NAME")
264
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)
270
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)
278
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)
284
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)
290
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')
295
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)
302
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')
308
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)
315
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)
322
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')
327
328
329 if __name__ == "__main__":
330     unittest.main(verbosity=2)