Merge "Add check_criteria() in testcase_base"
[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 import functest.core.testcase_base as testcase_base
19 from functest.opnfv_tests.sdn.odl import odl
20 from functest.utils import functest_constants as ft_constants
21
22
23 class ODLTesting(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 = odl.ODLTests()
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                 ODLTesting._keystone_ip)
57         elif service_type == 'network':
58             return "http://{}:9696".format(ODLTesting._neutron_ip)
59         else:
60             return None
61
62     def _get_main_kwargs(self, key=None):
63         kwargs = {'odlusername': self._odl_username,
64                   'odlpassword': self._odl_password,
65                   'keystoneip': self._keystone_ip,
66                   'neutronip': self._neutron_ip,
67                   'osusername': self._os_username,
68                   'ostenantname': self._os_tenantname,
69                   'ospassword': self._os_password,
70                   'odlip': self._sdn_controller_ip,
71                   'odlwebport': self._odl_webport,
72                   'odlrestconfport': self._odl_restconfport}
73         if key:
74             del kwargs[key]
75         return kwargs
76
77     def _test_main(self, status, *args):
78         kwargs = self._get_main_kwargs()
79         self.assertEqual(self.test.main(**kwargs), status)
80         odl_res_dir = odl.ODLTests.res_dir
81         if len(args) > 0:
82             args[0].assert_called_once_with(odl_res_dir)
83         if len(args) > 1:
84             variable = ['KEYSTONE:{}'.format(self._keystone_ip),
85                         'NEUTRON:{}'.format(self._neutron_ip),
86                         'OSUSERNAME:"{}"'.format(self._os_username),
87                         'OSTENANTNAME:"{}"'.format(self._os_tenantname),
88                         'OSPASSWORD:"{}"'.format(self._os_password),
89                         'ODL_SYSTEM_IP:{}'.format(self._sdn_controller_ip),
90                         'PORT:{}'.format(self._odl_webport),
91                         'RESTCONFPORT:{}'.format(self._odl_restconfport)]
92             output_file = os.path.join(odl_res_dir, 'output.xml')
93             args[1].assert_called_once_with(
94                 odl.ODLTests.basic_suite_dir,
95                 odl.ODLTests.neutron_suite_dir,
96                 log='NONE',
97                 output=output_file,
98                 report='NONE',
99                 stdout=mock.ANY,
100                 variable=variable)
101         if len(args) > 2:
102             stdout_file = os.path.join(odl_res_dir, 'stdout.txt')
103             args[2].assert_called_with(stdout_file)
104
105     def _test_main_missing_keyword(self, key):
106         kwargs = self._get_main_kwargs(key)
107         self.assertEqual(self.test.main(**kwargs),
108                          testcase_base.TestcaseBase.EX_RUN_ERROR)
109
110     def test_main_missing_odlusername(self):
111         self._test_main_missing_keyword('odlusername')
112
113     def test_main_missing_odlpassword(self):
114         self._test_main_missing_keyword('odlpassword')
115
116     def test_main_missing_keystoneip(self):
117         self._test_main_missing_keyword('keystoneip')
118
119     def test_main_missing_neutronip(self):
120         self._test_main_missing_keyword('neutronip')
121
122     def test_main_missing_osusername(self):
123         self._test_main_missing_keyword('osusername')
124
125     def test_main_missing_ostenantname(self):
126         self._test_main_missing_keyword('ostenantname')
127
128     def test_main_missing_ospassword(self):
129         self._test_main_missing_keyword('ospassword')
130
131     def test_main_missing_odlip(self):
132         self._test_main_missing_keyword('odlip')
133
134     def test_main_missing_odlwebport(self):
135         self._test_main_missing_keyword('odlwebport')
136
137     def test_main_missing_odlrestconfport(self):
138         self._test_main_missing_keyword('odlrestconfport')
139
140     def test_main_set_robotframework_vars_failed(self):
141         with mock.patch.object(self.test, 'set_robotframework_vars',
142                                return_value=False):
143             self._test_main(testcase_base.TestcaseBase.EX_RUN_ERROR)
144             self.test.set_robotframework_vars.assert_called_once_with(
145                 self._odl_username, self._odl_password)
146
147     @mock.patch('os.makedirs', side_effect=Exception)
148     def test_main_makedirs_exception(self, mock_method):
149         with mock.patch.object(self.test, 'set_robotframework_vars',
150                                return_value=True), \
151                 self.assertRaises(Exception):
152             self._test_main(testcase_base.TestcaseBase.EX_RUN_ERROR,
153                             mock_method)
154
155     @mock.patch('os.makedirs', side_effect=OSError)
156     def test_main_makedirs_oserror(self, mock_method):
157         with mock.patch.object(self.test, 'set_robotframework_vars',
158                                return_value=True):
159             self._test_main(testcase_base.TestcaseBase.EX_RUN_ERROR,
160                             mock_method)
161
162     @mock.patch('robot.run', side_effect=RobotError)
163     @mock.patch('os.makedirs')
164     def test_main_robot_run_failed(self, *args):
165         with mock.patch.object(self.test, 'set_robotframework_vars',
166                                return_value=True), \
167                 self.assertRaises(RobotError):
168             self._test_main(testcase_base.TestcaseBase.EX_RUN_ERROR, *args)
169
170     @mock.patch('robot.run')
171     @mock.patch('os.makedirs')
172     def test_main_parse_results_failed(self, *args):
173         with mock.patch.object(self.test, 'set_robotframework_vars',
174                                return_value=True), \
175                 mock.patch.object(self.test, 'parse_results',
176                                   side_effect=RobotError):
177             self._test_main(testcase_base.TestcaseBase.EX_RUN_ERROR, *args)
178
179     @mock.patch('os.remove', side_effect=Exception)
180     @mock.patch('robot.run')
181     @mock.patch('os.makedirs')
182     def test_main_remove_exception(self, *args):
183         with mock.patch.object(self.test, 'set_robotframework_vars',
184                                return_value=True), \
185                 mock.patch.object(self.test, 'parse_results'), \
186                 self.assertRaises(Exception):
187             self._test_main(testcase_base.TestcaseBase.EX_OK, *args)
188
189     @mock.patch('os.remove')
190     @mock.patch('robot.run')
191     @mock.patch('os.makedirs')
192     def test_main(self, *args):
193         with mock.patch.object(self.test, 'set_robotframework_vars',
194                                return_value=True), \
195                 mock.patch.object(self.test, 'parse_results'):
196             self._test_main(testcase_base.TestcaseBase.EX_OK, *args)
197
198     @mock.patch('os.remove')
199     @mock.patch('robot.run')
200     @mock.patch('os.makedirs', side_effect=OSError(errno.EEXIST, ''))
201     def test_main_makedirs_oserror17(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(testcase_base.TestcaseBase.EX_OK, *args)
206
207     @mock.patch('os.remove')
208     @mock.patch('robot.run', return_value=1)
209     @mock.patch('os.makedirs')
210     def test_main_testcases_in_failure(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(testcase_base.TestcaseBase.EX_OK, *args)
215
216     @mock.patch('os.remove', side_effect=OSError)
217     @mock.patch('robot.run')
218     @mock.patch('os.makedirs')
219     def test_main_remove_oserror(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(testcase_base.TestcaseBase.EX_OK, *args)
224
225     def _test_run_missing_env_var(self, var):
226         if var == 'OS_USERNAME':
227             ft_constants.OS_USERNAME = None
228         elif var == 'OS_PASSWORD':
229             ft_constants.OS_PASSWORD = None
230         elif var == 'OS_TENANT_NAME':
231             ft_constants.OS_TENANT_NAME = None
232
233         self.assertEqual(self.test.run(),
234                          testcase_base.TestcaseBase.EX_RUN_ERROR)
235
236     def _test_run(self, status=testcase_base.TestcaseBase.EX_OK,
237                   exception=None, odlip="127.0.0.3", odlwebport="8080"):
238         with mock.patch('functest.utils.openstack_utils.get_endpoint',
239                         side_effect=[self._fake_url_for('identity'),
240                                      self._fake_url_for('network')]):
241             if exception:
242                 self.test.main = mock.Mock(side_effect=exception)
243             else:
244                 self.test.main = mock.Mock(return_value=status)
245             self.assertEqual(self.test.run(), status)
246             self.test.main.assert_called_once_with(
247                 keystoneip=self._keystone_ip, neutronip=self._neutron_ip,
248                 odlip=odlip, odlpassword=self._odl_password,
249                 odlrestconfport=self._odl_restconfport,
250                 odlusername=self._odl_username, odlwebport=odlwebport,
251                 ospassword=self._os_password, ostenantname=self._os_tenantname,
252                 osusername=self._os_username)
253
254     def test_run_missing_os_username(self):
255         self._test_run_missing_env_var("OS_USERNAME")
256
257     def test_run_missing_os_password(self):
258         self._test_run_missing_env_var("OS_PASSWORD")
259
260     def test_run_missing_os_tenant_name(self):
261         self._test_run_missing_env_var("OS_TENANT_NAME")
262
263     def test_run_main_false(self):
264         ft_constants.CI_INSTALLER_TYPE = None
265         ft_constants.SDN_CONTROLLER_IP = self._sdn_controller_ip
266         self._test_run(testcase_base.TestcaseBase.EX_RUN_ERROR,
267                        odlip=self._sdn_controller_ip,
268                        odlwebport=self._odl_webport)
269
270     def test_run_main_exception(self):
271         ft_constants.CI_INSTALLER_TYPE = None
272         ft_constants.SDN_CONTROLLER_IP = self._sdn_controller_ip
273         with self.assertRaises(Exception):
274             self._test_run(status=testcase_base.TestcaseBase.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         ft_constants.CI_INSTALLER_TYPE = None
281         ft_constants.SDN_CONTROLLER_IP = None
282         self.assertEqual(self.test.run(),
283                          testcase_base.TestcaseBase.EX_RUN_ERROR)
284
285     def test_run_without_installer_type(self):
286         ft_constants.SDN_CONTROLLER_IP = self._sdn_controller_ip
287         ft_constants.CI_INSTALLER_TYPE = None
288         self._test_run(testcase_base.TestcaseBase.EX_OK,
289                        odlip=self._sdn_controller_ip,
290                        odlwebport=self._odl_webport)
291
292     def test_run_fuel(self):
293         ft_constants.CI_INSTALLER_TYPE = "fuel"
294         self._test_run(testcase_base.TestcaseBase.EX_OK,
295                        odlip=self._neutron_ip, odlwebport='8282')
296
297     def test_run_apex_missing_sdn_controller_ip(self):
298         ft_constants.CI_INSTALLER_TYPE = "apex"
299         ft_constants.SDN_CONTROLLER_IP = None
300         self.assertEqual(self.test.run(),
301                          testcase_base.TestcaseBase.EX_RUN_ERROR)
302
303     def test_run_apex(self):
304         ft_constants.SDN_CONTROLLER_IP = self._sdn_controller_ip
305         ft_constants.CI_INSTALLER_TYPE = "apex"
306         self._test_run(testcase_base.TestcaseBase.EX_OK,
307                        odlip=self._sdn_controller_ip, odlwebport='8181')
308
309     def test_run_joid_missing_sdn_controller(self):
310         ft_constants.CI_INSTALLER_TYPE = "joid"
311         ft_constants.SDN_CONTROLLER = None
312         self.assertEqual(self.test.run(),
313                          testcase_base.TestcaseBase.EX_RUN_ERROR)
314
315     def test_run_joid(self):
316         ft_constants.SDN_CONTROLLER = self._sdn_controller_ip
317         ft_constants.CI_INSTALLER_TYPE = "joid"
318         self._test_run(testcase_base.TestcaseBase.EX_OK,
319                        odlip=self._sdn_controller_ip,
320                        odlwebport=self._odl_webport)
321
322     def test_run_compass(self, *args):
323         ft_constants.CI_INSTALLER_TYPE = "compass"
324         self._test_run(testcase_base.TestcaseBase.EX_OK,
325                        odlip=self._neutron_ip, odlwebport='8181')
326
327
328 if __name__ == "__main__":
329     unittest.main(verbosity=2)