Cover KeyError in ODLTestCases.run()
[functest-xtesting.git] / unit_tests / 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.testcases.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('shutil.copy', side_effect=Exception())
47     def test_copy_opnf_testcases_exception(self, *args):
48         with self.assertRaises(Exception):
49             self.test.copy_opnf_testcases()
50
51     @mock.patch('shutil.copy', side_effect=IOError())
52     def test_copy_opnf_testcases_ioerror(self, *args):
53         self.assertFalse(self.test.copy_opnf_testcases())
54
55     @mock.patch('shutil.copy')
56     def test_copy_opnf_testcases(self, *args):
57         self.assertTrue(self.test.copy_opnf_testcases())
58
59     @mock.patch('fileinput.input', side_effect=Exception())
60     def test_set_robotframework_vars_failed(self, *args):
61         self.assertFalse(self.test.set_robotframework_vars())
62
63     @mock.patch('fileinput.input', return_value=[])
64     def test_set_robotframework_vars(self, args):
65         self.assertTrue(self.test.set_robotframework_vars())
66
67     @classmethod
68     def _fake_url_for(cls, service_type='identity', **kwargs):
69         if service_type == 'identity':
70             return "http://{}:5000/v2.0".format(
71                 ODLTestCasesTesting._keystone_ip)
72         elif service_type == 'network':
73             return "http://{}:9696".format(ODLTestCasesTesting._neutron_ip)
74         else:
75             return None
76
77     @classmethod
78     def _get_fake_keystone_client(cls):
79         kclient = mock.Mock()
80         kclient.service_catalog = mock.Mock()
81         kclient.service_catalog.url_for = mock.Mock(
82             side_effect=cls._fake_url_for)
83         return kclient
84
85     def _get_main_kwargs(self, key=None):
86         kwargs = {'odlusername': self._odl_username,
87                   'odlpassword': self._odl_password,
88                   'keystoneip': self._keystone_ip,
89                   'neutronip': self._neutron_ip,
90                   'osusername': self._os_username,
91                   'ostenantname': self._os_tenantname,
92                   'ospassword': self._os_password,
93                   'odlip': self._sdn_controller_ip,
94                   'odlwebport': self._odl_webport,
95                   'odlrestconfport': self._odl_restconfport}
96         if key:
97             del kwargs[key]
98         return kwargs
99
100     def _test_main(self, status, *args):
101         kwargs = self._get_main_kwargs()
102         self.assertEqual(self.test.main(**kwargs), status)
103         if len(args) > 0:
104             args[0].assert_called_once_with(
105                 OpenDaylightTesting.ODLTestCases.res_dir)
106         if len(args) > 1:
107             variable = ['KEYSTONE:{}'.format(self._keystone_ip),
108                         'NEUTRON:{}'.format(self._neutron_ip),
109                         'OSUSERNAME:"{}"'.format(self._os_username),
110                         'OSTENANTNAME:"{}"'.format(self._os_tenantname),
111                         'OSPASSWORD:"{}"'.format(self._os_password),
112                         'ODL_SYSTEM_IP:{}'.format(self._sdn_controller_ip),
113                         'PORT:{}'.format(self._odl_webport),
114                         'RESTCONFPORT:{}'.format(self._odl_restconfport)]
115             args[1].assert_called_once_with(
116                 OpenDaylightTesting.ODLTestCases.basic_suite_dir,
117                 OpenDaylightTesting.ODLTestCases.neutron_suite_dir,
118                 log='NONE',
119                 output=OpenDaylightTesting.ODLTestCases.res_dir + 'output.xml',
120                 report='NONE',
121                 stdout=mock.ANY,
122                 variable=variable)
123         if len(args) > 2:
124             args[2].assert_called_with(
125                 OpenDaylightTesting.ODLTestCases.res_dir + 'stdout.txt')
126
127     def _test_main_missing_keyword(self, key):
128         kwargs = self._get_main_kwargs(key)
129         self.assertEqual(self.test.main(**kwargs),
130                          TestCasesBase.TestCasesBase.EX_RUN_ERROR)
131
132     def test_main_missing_odlusername(self):
133         self._test_main_missing_keyword('odlusername')
134
135     def test_main_missing_odlpassword(self):
136         self._test_main_missing_keyword('odlpassword')
137
138     def test_main_missing_keystoneip(self):
139         self._test_main_missing_keyword('keystoneip')
140
141     def test_main_missing_neutronip(self):
142         self._test_main_missing_keyword('neutronip')
143
144     def test_main_missing_osusername(self):
145         self._test_main_missing_keyword('osusername')
146
147     def test_main_missing_ostenantname(self):
148         self._test_main_missing_keyword('ostenantname')
149
150     def test_main_missing_ospassword(self):
151         self._test_main_missing_keyword('ospassword')
152
153     def test_main_missing_odlip(self):
154         self._test_main_missing_keyword('odlip')
155
156     def test_main_missing_odlwebport(self):
157         self._test_main_missing_keyword('odlwebport')
158
159     def test_main_missing_odlrestconfport(self):
160         self._test_main_missing_keyword('odlrestconfport')
161
162     def test_main_copy_opnf_testcases_failed(self):
163         with mock.patch.object(self.test, 'copy_opnf_testcases',
164                                return_value=False):
165             self._test_main(TestCasesBase.TestCasesBase.EX_RUN_ERROR)
166             self.test.copy_opnf_testcases.assert_called_once_with()
167
168     def test_main_set_robotframework_vars_failed(self):
169         with mock.patch.object(self.test, 'copy_opnf_testcases',
170                                return_value=True), \
171                 mock.patch.object(self.test, 'set_robotframework_vars',
172                                   return_value=False):
173             self._test_main(TestCasesBase.TestCasesBase.EX_RUN_ERROR)
174             self.test.set_robotframework_vars.assert_called_once_with(
175                 self._odl_username, self._odl_password)
176
177     @mock.patch('os.makedirs', side_effect=Exception)
178     def test_main_makedirs_exception(self, mock_method):
179         with mock.patch.object(self.test,
180                                'copy_opnf_testcases', return_value=True), \
181                 mock.patch.object(self.test, 'set_robotframework_vars',
182                                   return_value=True), \
183                 self.assertRaises(Exception):
184             self._test_main(TestCasesBase.TestCasesBase.EX_RUN_ERROR,
185                             mock_method)
186
187     @mock.patch('os.makedirs', side_effect=OSError)
188     def test_main_makedirs_oserror(self, mock_method):
189         with mock.patch.object(self.test,
190                                'copy_opnf_testcases', return_value=True), \
191                 mock.patch.object(self.test, 'set_robotframework_vars',
192                                   return_value=True):
193             self._test_main(TestCasesBase.TestCasesBase.EX_RUN_ERROR,
194                             mock_method)
195
196     @mock.patch('robot.run', side_effect=RobotError)
197     @mock.patch('os.makedirs')
198     def test_main_robot_run_failed(self, *args):
199         with mock.patch.object(self.test, 'copy_opnf_testcases',
200                                return_value=True), \
201                 mock.patch.object(self.test, 'set_robotframework_vars',
202                                   return_value=True), \
203                 self.assertRaises(RobotError):
204             self._test_main(TestCasesBase.TestCasesBase.EX_RUN_ERROR, *args)
205
206     @mock.patch('robot.run')
207     @mock.patch('os.makedirs')
208     def test_main_parse_results_failed(self, *args):
209         with mock.patch.object(self.test, 'copy_opnf_testcases',
210                                return_value=True), \
211                 mock.patch.object(self.test, 'set_robotframework_vars',
212                                   return_value=True), \
213                 mock.patch.object(self.test, 'parse_results',
214                                   side_effect=RobotError):
215             self._test_main(TestCasesBase.TestCasesBase.EX_RUN_ERROR, *args)
216
217     @mock.patch('os.remove', side_effect=Exception)
218     @mock.patch('robot.run')
219     @mock.patch('os.makedirs')
220     def test_main_remove_exception(self, *args):
221         with mock.patch.object(self.test, 'copy_opnf_testcases',
222                                return_value=True), \
223                 mock.patch.object(self.test, 'set_robotframework_vars',
224                                   return_value=True), \
225                 mock.patch.object(self.test, 'parse_results'), \
226                 self.assertRaises(Exception):
227             self._test_main(TestCasesBase.TestCasesBase.EX_OK, *args)
228
229     @mock.patch('os.remove')
230     @mock.patch('robot.run')
231     @mock.patch('os.makedirs')
232     def test_main(self, *args):
233         with mock.patch.object(self.test, 'copy_opnf_testcases',
234                                return_value=True), \
235                 mock.patch.object(self.test, 'set_robotframework_vars',
236                                   return_value=True), \
237                 mock.patch.object(self.test, 'parse_results'):
238             self._test_main(TestCasesBase.TestCasesBase.EX_OK, *args)
239
240     @mock.patch('os.remove')
241     @mock.patch('robot.run')
242     @mock.patch('os.makedirs', side_effect=OSError(errno.EEXIST, ''))
243     def test_main_makedirs_oserror17(self, *args):
244         with mock.patch.object(self.test, 'copy_opnf_testcases',
245                                return_value=True), \
246                 mock.patch.object(self.test, 'set_robotframework_vars',
247                                   return_value=True), \
248                 mock.patch.object(self.test, 'parse_results'):
249             self._test_main(TestCasesBase.TestCasesBase.EX_OK, *args)
250
251     @mock.patch('os.remove')
252     @mock.patch('robot.run', return_value=1)
253     @mock.patch('os.makedirs')
254     def test_main_testcases_in_failure(self, *args):
255         with mock.patch.object(self.test, 'copy_opnf_testcases',
256                                return_value=True), \
257                 mock.patch.object(self.test, 'set_robotframework_vars',
258                                   return_value=True), \
259                 mock.patch.object(self.test, 'parse_results'):
260             self._test_main(TestCasesBase.TestCasesBase.EX_OK, *args)
261
262     @mock.patch('os.remove', side_effect=OSError)
263     @mock.patch('robot.run')
264     @mock.patch('os.makedirs')
265     def test_main_remove_oserror(self, *args):
266         with mock.patch.object(self.test, 'copy_opnf_testcases',
267                                return_value=True), \
268                 mock.patch.object(self.test, 'set_robotframework_vars',
269                                   return_value=True), \
270                 mock.patch.object(self.test, 'parse_results'):
271             self._test_main(TestCasesBase.TestCasesBase.EX_OK, *args)
272
273     def _test_run_missing_env_var(self, var):
274         del os.environ[var]
275         self.assertEqual(self.test.run(),
276                          TestCasesBase.TestCasesBase.EX_RUN_ERROR)
277
278     def _test_run(self, status=TestCasesBase.TestCasesBase.EX_OK,
279                   exception=None, odlip="127.0.0.3", odlwebport="8080"):
280         with mock.patch('functest.utils.openstack_utils.get_keystone_client',
281                         return_value=self._get_fake_keystone_client()):
282             if exception:
283                 self.test.main = mock.Mock(side_effect=exception)
284             else:
285                 self.test.main = mock.Mock(return_value=status)
286             self.assertEqual(self.test.run(), status)
287             self.test.main.assert_called_once_with(
288                 keystoneip=self._keystone_ip, neutronip=self._neutron_ip,
289                 odlip=odlip, odlpassword=self._odl_password,
290                 odlrestconfport=self._odl_restconfport,
291                 odlusername=self._odl_username, odlwebport=odlwebport,
292                 ospassword=self._os_password, ostenantname=self._os_tenantname,
293                 osusername=self._os_username)
294
295     def test_run_missing_os_username(self):
296         self._test_run_missing_env_var("OS_USERNAME")
297
298     def test_run_missing_os_password(self):
299         self._test_run_missing_env_var("OS_PASSWORD")
300
301     def test_run_missing_os_tenant_name(self):
302         self._test_run_missing_env_var("OS_TENANT_NAME")
303
304     def test_run_main_false(self):
305         os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
306         self._test_run(TestCasesBase.TestCasesBase.EX_RUN_ERROR,
307                        odlip=self._sdn_controller_ip,
308                        odlwebport=self._odl_webport)
309
310     def test_run_main_exception(self):
311         os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
312         with self.assertRaises(Exception):
313             self._test_run(status=TestCasesBase.TestCasesBase.EX_RUN_ERROR,
314                            exception=Exception(),
315                            odlip=self._sdn_controller_ip,
316                            odlwebport=self._odl_webport)
317
318     def test_run_missing_sdn_controller_ip(self):
319         with mock.patch('functest.utils.openstack_utils.get_keystone_client',
320                         return_value=self._get_fake_keystone_client()):
321             self.assertEqual(self.test.run(),
322                              TestCasesBase.TestCasesBase.EX_RUN_ERROR)
323
324     def test_run_without_installer_type(self):
325         os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
326         self._test_run(TestCasesBase.TestCasesBase.EX_OK,
327                        odlip=self._sdn_controller_ip,
328                        odlwebport=self._odl_webport)
329
330     def test_run_fuel(self):
331         os.environ["INSTALLER_TYPE"] = "fuel"
332         self._test_run(TestCasesBase.TestCasesBase.EX_OK,
333                        odlip=self._neutron_ip, odlwebport='8282')
334
335     def test_run_apex_missing_sdn_controller_ip(self):
336         with mock.patch('functest.utils.openstack_utils.get_keystone_client',
337                         return_value=self._get_fake_keystone_client()):
338             os.environ["INSTALLER_TYPE"] = "apex"
339             self.assertEqual(self.test.run(),
340                              TestCasesBase.TestCasesBase.EX_RUN_ERROR)
341
342     def test_run_apex(self):
343         os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
344         os.environ["INSTALLER_TYPE"] = "apex"
345         self._test_run(TestCasesBase.TestCasesBase.EX_OK,
346                        odlip=self._sdn_controller_ip, odlwebport='8181')
347
348     def test_run_joid_missing_sdn_controller(self):
349         with mock.patch('functest.utils.openstack_utils.get_keystone_client',
350                         return_value=self._get_fake_keystone_client()):
351             os.environ["INSTALLER_TYPE"] = "joid"
352             self.assertEqual(self.test.run(),
353                              TestCasesBase.TestCasesBase.EX_RUN_ERROR)
354
355     def test_run_joid(self):
356         os.environ["SDN_CONTROLLER"] = self._sdn_controller_ip
357         os.environ["INSTALLER_TYPE"] = "joid"
358         self._test_run(TestCasesBase.TestCasesBase.EX_OK,
359                        odlip=self._sdn_controller_ip,
360                        odlwebport=self._odl_webport)
361
362     def test_run_compass(self, *args):
363         os.environ["INSTALLER_TYPE"] = "compass"
364         self._test_run(TestCasesBase.TestCasesBase.EX_OK,
365                        odlip=self._neutron_ip, odlwebport='8181')
366
367
368 if __name__ == "__main__":
369     unittest.main(verbosity=2)