Extracted all global parameters into functest_constants.py
[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 from functest.utils import functest_constants as ft_constants
21
22
23 class ODLTestCasesTesting(unittest.TestCase):
24
25     logging.disable(logging.CRITICAL)
26
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"
33     _odl_webport = "8080"
34     _odl_restconfport = "8181"
35     _odl_username = "admin"
36     _odl_password = "admin"
37
38     def setUp(self):
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 = OpenDaylightTesting.ODLTestCases()
43
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())
47
48     @mock.patch('fileinput.input', return_value=[])
49     def test_set_robotframework_vars(self, args):
50         self.assertTrue(self.test.set_robotframework_vars())
51
52     @classmethod
53     def _fake_url_for(cls, service_type='identity', **kwargs):
54         if service_type == 'identity':
55             return "http://{}:5000/v2.0".format(
56                 ODLTestCasesTesting._keystone_ip)
57         elif service_type == 'network':
58             return "http://{}:9696".format(ODLTestCasesTesting._neutron_ip)
59         else:
60             return None
61
62     @classmethod
63     def _get_fake_keystone_client(cls):
64         kclient = mock.Mock()
65         kclient.service_catalog = mock.Mock()
66         kclient.service_catalog.url_for = mock.Mock(
67             side_effect=cls._fake_url_for)
68         return kclient
69
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}
81         if key:
82             del kwargs[key]
83         return kwargs
84
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 = OpenDaylightTesting.ODLTestCases.res_dir
89         if len(args) > 0:
90             args[0].assert_called_once_with(odl_res_dir)
91         if len(args) > 1:
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                 OpenDaylightTesting.ODLTestCases.basic_suite_dir,
103                 OpenDaylightTesting.ODLTestCases.neutron_suite_dir,
104                 log='NONE',
105                 output=output_file,
106                 report='NONE',
107                 stdout=mock.ANY,
108                 variable=variable)
109         if len(args) > 2:
110             stdout_file = os.path.join(odl_res_dir, 'stdout.txt')
111             args[2].assert_called_with(stdout_file)
112
113     def _test_main_missing_keyword(self, key):
114         kwargs = self._get_main_kwargs(key)
115         self.assertEqual(self.test.main(**kwargs),
116                          TestCasesBase.TestCasesBase.EX_RUN_ERROR)
117
118     def test_main_missing_odlusername(self):
119         self._test_main_missing_keyword('odlusername')
120
121     def test_main_missing_odlpassword(self):
122         self._test_main_missing_keyword('odlpassword')
123
124     def test_main_missing_keystoneip(self):
125         self._test_main_missing_keyword('keystoneip')
126
127     def test_main_missing_neutronip(self):
128         self._test_main_missing_keyword('neutronip')
129
130     def test_main_missing_osusername(self):
131         self._test_main_missing_keyword('osusername')
132
133     def test_main_missing_ostenantname(self):
134         self._test_main_missing_keyword('ostenantname')
135
136     def test_main_missing_ospassword(self):
137         self._test_main_missing_keyword('ospassword')
138
139     def test_main_missing_odlip(self):
140         self._test_main_missing_keyword('odlip')
141
142     def test_main_missing_odlwebport(self):
143         self._test_main_missing_keyword('odlwebport')
144
145     def test_main_missing_odlrestconfport(self):
146         self._test_main_missing_keyword('odlrestconfport')
147
148     def test_main_set_robotframework_vars_failed(self):
149         with mock.patch.object(self.test, 'set_robotframework_vars',
150                                return_value=False):
151             self._test_main(TestCasesBase.TestCasesBase.EX_RUN_ERROR)
152             self.test.set_robotframework_vars.assert_called_once_with(
153                 self._odl_username, self._odl_password)
154
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(TestCasesBase.TestCasesBase.EX_RUN_ERROR,
161                             mock_method)
162
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',
166                                return_value=True):
167             self._test_main(TestCasesBase.TestCasesBase.EX_RUN_ERROR,
168                             mock_method)
169
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(TestCasesBase.TestCasesBase.EX_RUN_ERROR, *args)
177
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(TestCasesBase.TestCasesBase.EX_RUN_ERROR, *args)
186
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(TestCasesBase.TestCasesBase.EX_OK, *args)
196
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             self._test_main(TestCasesBase.TestCasesBase.EX_OK, *args)
205
206     @mock.patch('os.remove')
207     @mock.patch('robot.run')
208     @mock.patch('os.makedirs', side_effect=OSError(errno.EEXIST, ''))
209     def test_main_makedirs_oserror17(self, *args):
210         with mock.patch.object(self.test, 'set_robotframework_vars',
211                                return_value=True), \
212                 mock.patch.object(self.test, 'parse_results'):
213             self._test_main(TestCasesBase.TestCasesBase.EX_OK, *args)
214
215     @mock.patch('os.remove')
216     @mock.patch('robot.run', return_value=1)
217     @mock.patch('os.makedirs')
218     def test_main_testcases_in_failure(self, *args):
219         with mock.patch.object(self.test, 'set_robotframework_vars',
220                                return_value=True), \
221                 mock.patch.object(self.test, 'parse_results'):
222             self._test_main(TestCasesBase.TestCasesBase.EX_OK, *args)
223
224     @mock.patch('os.remove', side_effect=OSError)
225     @mock.patch('robot.run')
226     @mock.patch('os.makedirs')
227     def test_main_remove_oserror(self, *args):
228         with mock.patch.object(self.test, 'set_robotframework_vars',
229                                return_value=True), \
230                 mock.patch.object(self.test, 'parse_results'):
231             self._test_main(TestCasesBase.TestCasesBase.EX_OK, *args)
232
233     def _test_run_missing_env_var(self, var):
234         if var == 'OS_USERNAME':
235             ft_constants.OS_USERNAME = None
236         elif var == 'OS_PASSWORD':
237             ft_constants.OS_PASSWORD = None
238         elif var == 'OS_TENANT_NAME':
239             ft_constants.OS_TENANT_NAME = None
240
241         self.assertEqual(self.test.run(),
242                          TestCasesBase.TestCasesBase.EX_RUN_ERROR)
243
244     def _test_run(self, status=TestCasesBase.TestCasesBase.EX_OK,
245                   exception=None, odlip="127.0.0.3", odlwebport="8080"):
246         with mock.patch('functest.utils.openstack_utils.get_keystone_client',
247                         return_value=self._get_fake_keystone_client()):
248             if exception:
249                 self.test.main = mock.Mock(side_effect=exception)
250             else:
251                 self.test.main = mock.Mock(return_value=status)
252             self.assertEqual(self.test.run(), status)
253             self.test.main.assert_called_once_with(
254                 keystoneip=self._keystone_ip, neutronip=self._neutron_ip,
255                 odlip=odlip, odlpassword=self._odl_password,
256                 odlrestconfport=self._odl_restconfport,
257                 odlusername=self._odl_username, odlwebport=odlwebport,
258                 ospassword=self._os_password, ostenantname=self._os_tenantname,
259                 osusername=self._os_username)
260
261     def test_run_missing_os_username(self):
262         self._test_run_missing_env_var("OS_USERNAME")
263
264     def test_run_missing_os_password(self):
265         self._test_run_missing_env_var("OS_PASSWORD")
266
267     def test_run_missing_os_tenant_name(self):
268         self._test_run_missing_env_var("OS_TENANT_NAME")
269
270     def test_run_main_false(self):
271         ft_constants.CI_INSTALLER_TYPE = None
272         ft_constants.SDN_CONTROLLER_IP = self._sdn_controller_ip
273         self._test_run(TestCasesBase.TestCasesBase.EX_RUN_ERROR,
274                        odlip=self._sdn_controller_ip,
275                        odlwebport=self._odl_webport)
276
277     def test_run_main_exception(self):
278         ft_constants.CI_INSTALLER_TYPE = None
279         ft_constants.SDN_CONTROLLER_IP = self._sdn_controller_ip
280         with self.assertRaises(Exception):
281             self._test_run(status=TestCasesBase.TestCasesBase.EX_RUN_ERROR,
282                            exception=Exception(),
283                            odlip=self._sdn_controller_ip,
284                            odlwebport=self._odl_webport)
285
286     def test_run_missing_sdn_controller_ip(self):
287         with mock.patch('functest.utils.openstack_utils.get_keystone_client',
288                         return_value=self._get_fake_keystone_client()):
289             ft_constants.CI_INSTALLER_TYPE = None
290             ft_constants.SDN_CONTROLLER_IP = None
291             self.assertEqual(self.test.run(),
292                              TestCasesBase.TestCasesBase.EX_RUN_ERROR)
293
294     def test_run_without_installer_type(self):
295         ft_constants.SDN_CONTROLLER_IP = self._sdn_controller_ip
296         ft_constants.CI_INSTALLER_TYPE = None
297         self._test_run(TestCasesBase.TestCasesBase.EX_OK,
298                        odlip=self._sdn_controller_ip,
299                        odlwebport=self._odl_webport)
300
301     def test_run_fuel(self):
302         ft_constants.CI_INSTALLER_TYPE = "fuel"
303         self._test_run(TestCasesBase.TestCasesBase.EX_OK,
304                        odlip=self._neutron_ip, odlwebport='8282')
305
306     def test_run_apex_missing_sdn_controller_ip(self):
307         with mock.patch('functest.utils.openstack_utils.get_keystone_client',
308                         return_value=self._get_fake_keystone_client()):
309             ft_constants.CI_INSTALLER_TYPE = "apex"
310             ft_constants.SDN_CONTROLLER_IP = None
311             self.assertEqual(self.test.run(),
312                              TestCasesBase.TestCasesBase.EX_RUN_ERROR)
313
314     def test_run_apex(self):
315         ft_constants.SDN_CONTROLLER_IP = self._sdn_controller_ip
316         ft_constants.CI_INSTALLER_TYPE = "apex"
317         self._test_run(TestCasesBase.TestCasesBase.EX_OK,
318                        odlip=self._sdn_controller_ip, odlwebport='8181')
319
320     def test_run_joid_missing_sdn_controller(self):
321         with mock.patch('functest.utils.openstack_utils.get_keystone_client',
322                         return_value=self._get_fake_keystone_client()):
323             ft_constants.CI_INSTALLER_TYPE = "joid"
324             ft_constants.SDN_CONTROLLER = None
325             self.assertEqual(self.test.run(),
326                              TestCasesBase.TestCasesBase.EX_RUN_ERROR)
327
328     def test_run_joid(self):
329         ft_constants.SDN_CONTROLLER = self._sdn_controller_ip
330         ft_constants.CI_INSTALLER_TYPE = "joid"
331         self._test_run(TestCasesBase.TestCasesBase.EX_OK,
332                        odlip=self._sdn_controller_ip,
333                        odlwebport=self._odl_webport)
334
335     def test_run_compass(self, *args):
336         ft_constants.CI_INSTALLER_TYPE = "compass"
337         self._test_run(TestCasesBase.TestCasesBase.EX_OK,
338                        odlip=self._neutron_ip, odlwebport='8181')
339
340
341 if __name__ == "__main__":
342     unittest.main(verbosity=2)