Merge "More Unit Tests for utils module"
[functest.git] / functest / tests / unit / ci / test_run_tests.py
1 #!/usr/bin/env python
2
3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Apache License, Version 2.0
5 # which accompanies this distribution, and is available at
6 # http://www.apache.org/licenses/LICENSE-2.0
7
8
9 import unittest
10 import logging
11
12 import mock
13
14 from functest.ci import run_tests
15 from functest.utils.constants import CONST
16
17
18 class RunTestsTesting(unittest.TestCase):
19
20     logging.disable(logging.CRITICAL)
21
22     def setUp(self):
23         self.sep = 'test_sep'
24         self.creds = {'OS_AUTH_URL': 'http://test_ip:test_port/v2.0',
25                       'OS_USERNAME': 'test_os_username',
26                       'OS_TENANT_NAME': 'test_tenant',
27                       'OS_PASSWORD': 'test_password'}
28         self.test = {'test_name': 'test_name'}
29         self.tier = mock.Mock()
30         attrs = {'get_name.return_value': 'test_tier',
31                  'get_tests.return_value': ['test1', 'test2'],
32                  'get_ci_loop.return_value': 'test_ci_loop',
33                  'get_test_names.return_value': ['test1', 'test2']}
34         self.tier.configure_mock(**attrs)
35
36         self.tiers = mock.Mock()
37         attrs = {'get_tiers.return_value': [self.tier]}
38         self.tiers.configure_mock(**attrs)
39
40         self.run_tests_parser = run_tests.RunTestsParser()
41         self.global_variables = run_tests.GlobalVariables()
42
43     @mock.patch('functest.ci.run_tests.logger.info')
44     def test_print_separator(self, mock_logger_info):
45         run_tests.print_separator(self.sep)
46         mock_logger_info.assert_called_once_with(self.sep * 44)
47
48     @mock.patch('functest.ci.run_tests.logger.error')
49     def test_source_rc_file_missing_file(self, mock_logger_error):
50         with mock.patch('functest.ci.run_tests.os.path.isfile',
51                         return_value=False), \
52                 self.assertRaises(Exception):
53             run_tests.source_rc_file()
54
55     @mock.patch('functest.ci.run_tests.logger.debug')
56     def test_source_rc_file_default(self, mock_logger_debug):
57         with mock.patch('functest.ci.run_tests.os.path.isfile',
58                         return_value=True), \
59             mock.patch('functest.ci.run_tests.os_utils.source_credentials',
60                        return_value=self.creds):
61             run_tests.source_rc_file()
62
63     @mock.patch('functest.ci.run_tests.os_snapshot.main')
64     def test_generate_os_snapshot(self, mock_os_snap):
65             run_tests.generate_os_snapshot()
66             self.assertTrue(mock_os_snap.called)
67
68     @mock.patch('functest.ci.run_tests.os_clean.main')
69     def test_cleanup(self, mock_os_clean):
70             run_tests.cleanup()
71             self.assertTrue(mock_os_clean.called)
72
73     def test_update_test_info(self):
74         run_tests.GlobalVariables.EXECUTED_TEST_CASES = [self.test]
75         run_tests.update_test_info('test_name',
76                                    'test_result',
77                                    'test_duration')
78         exp = self.test
79         exp.update({"result": 'test_result',
80                     "duration": 'test_duration'})
81         self.assertEqual(run_tests.GlobalVariables.EXECUTED_TEST_CASES,
82                          [exp])
83
84     def test_get_run_dict_if_defined_default(self):
85         mock_obj = mock.Mock()
86         with mock.patch('functest.ci.run_tests.'
87                         'ft_utils.get_dict_by_test',
88                         return_value={'run': mock_obj}):
89             self.assertEqual(run_tests.get_run_dict('test_name'),
90                              mock_obj)
91
92     @mock.patch('functest.ci.run_tests.logger.error')
93     def test_get_run_dict_if_defined_missing_config_option(self,
94                                                            mock_logger_error):
95         with mock.patch('functest.ci.run_tests.'
96                         'ft_utils.get_dict_by_test',
97                         return_value=None):
98             testname = 'test_name'
99             self.assertEqual(run_tests.get_run_dict(testname),
100                              None)
101             mock_logger_error.assert_called_once_with("Cannot get {}'s config "
102                                                       "options"
103                                                       .format(testname))
104
105         with mock.patch('functest.ci.run_tests.'
106                         'ft_utils.get_dict_by_test',
107                         return_value={}):
108             testname = 'test_name'
109             self.assertEqual(run_tests.get_run_dict(testname),
110                              None)
111
112     @mock.patch('functest.ci.run_tests.logger.exception')
113     def test_get_run_dict_if_defined_exception(self,
114                                                mock_logger_except):
115         with mock.patch('functest.ci.run_tests.'
116                         'ft_utils.get_dict_by_test',
117                         side_effect=Exception):
118             testname = 'test_name'
119             self.assertEqual(run_tests.get_run_dict(testname),
120                              None)
121             mock_logger_except.assert_called_once_with("Cannot get {}'s config"
122                                                        " options"
123                                                        .format(testname))
124
125     def test_run_tests_import_test_class_exception(self):
126         mock_test = mock.Mock()
127         args = {'get_name.return_value': 'test_name',
128                 'needs_clean.return_value': False}
129         mock_test.configure_mock(**args)
130         with mock.patch('functest.ci.run_tests.print_separator'),\
131             mock.patch('functest.ci.run_tests.source_rc_file'), \
132             mock.patch('functest.ci.run_tests.get_run_dict',
133                        return_value=None), \
134                 self.assertRaises(Exception) as context:
135             run_tests.run_test(mock_test, 'tier_name')
136             msg = "Cannot import the class for the test case."
137             self.assertTrue(msg in context)
138
139     def test_run_tests_default(self):
140         mock_test = mock.Mock()
141         args = {'get_name.return_value': 'test_name',
142                 'needs_clean.return_value': True}
143         mock_test.configure_mock(**args)
144         test_run_dict = {'module': 'test_module',
145                          'class': mock.Mock,
146                          'args': 'test_args'}
147         with mock.patch('functest.ci.run_tests.print_separator'),\
148             mock.patch('functest.ci.run_tests.source_rc_file'), \
149             mock.patch('functest.ci.run_tests.generate_os_snapshot'), \
150             mock.patch('functest.ci.run_tests.cleanup'), \
151             mock.patch('functest.ci.run_tests.update_test_info'), \
152             mock.patch('functest.ci.run_tests.get_run_dict',
153                        return_value=test_run_dict), \
154             mock.patch('functest.ci.run_tests.generate_report.main'), \
155                 self.assertRaises(run_tests.BlockingTestFailed) as context:
156             run_tests.GlobalVariables.CLEAN_FLAG = True
157             run_tests.run_test(mock_test, 'tier_name')
158             msg = 'The test case test_name failed and is blocking'
159             self.assertTrue(msg in context)
160
161     @mock.patch('functest.ci.run_tests.logger.info')
162     def test_run_tier_default(self, mock_logger_info):
163         with mock.patch('functest.ci.run_tests.print_separator'), \
164                 mock.patch('functest.ci.run_tests.run_test') as mock_method:
165             run_tests.run_tier(self.tier)
166             mock_method.assert_any_call('test1', 'test_tier')
167             mock_method.assert_any_call('test2', 'test_tier')
168             self.assertTrue(mock_logger_info.called)
169
170     @mock.patch('functest.ci.run_tests.logger.info')
171     def test_run_tier_missing_test(self, mock_logger_info):
172         with mock.patch('functest.ci.run_tests.print_separator'):
173             self.tier.get_tests.return_value = None
174             self.assertEqual(run_tests.run_tier(self.tier), 0)
175             self.assertTrue(mock_logger_info.called)
176
177     @mock.patch('functest.ci.run_tests.logger.info')
178     def test_run_all_default(self, mock_logger_info):
179         with mock.patch('functest.ci.run_tests.run_tier') as mock_method, \
180             mock.patch('functest.ci.run_tests.generate_report.init'), \
181                 mock.patch('functest.ci.run_tests.generate_report.main'):
182             CONST.CI_LOOP = 'test_ci_loop'
183             run_tests.run_all(self.tiers)
184             mock_method.assert_any_call(self.tier)
185             self.assertTrue(mock_logger_info.called)
186
187     @mock.patch('functest.ci.run_tests.logger.info')
188     def test_run_all__missing_tier(self, mock_logger_info):
189         with mock.patch('functest.ci.run_tests.generate_report.init'), \
190                 mock.patch('functest.ci.run_tests.generate_report.main'):
191             CONST.CI_LOOP = 'loop_re_not_available'
192             run_tests.run_all(self.tiers)
193             self.assertTrue(mock_logger_info.called)
194
195     def test_main_failed(self):
196         kwargs = {'test': 'test_name', 'noclean': True, 'report': True}
197         mock_obj = mock.Mock()
198         args = {'get_tier.return_value': False,
199                 'get_test.return_value': False}
200         mock_obj.configure_mock(**args)
201
202         with mock.patch('functest.ci.run_tests.tb.TierBuilder'), \
203             mock.patch('functest.ci.run_tests.source_rc_file',
204                        side_effect=Exception):
205             self.assertEqual(run_tests.main(**kwargs),
206                              run_tests.Result.EX_ERROR)
207
208         with mock.patch('functest.ci.run_tests.tb.TierBuilder',
209                         return_value=mock_obj), \
210             mock.patch('functest.ci.run_tests.source_rc_file',
211                        side_effect=Exception):
212             self.assertEqual(run_tests.main(**kwargs),
213                              run_tests.Result.EX_ERROR)
214
215     def test_main_default(self):
216         kwargs = {'test': 'test_name', 'noclean': True, 'report': True}
217         mock_obj = mock.Mock()
218         args = {'get_tier.return_value': True,
219                 'get_test.return_value': False}
220         mock_obj.configure_mock(**args)
221         with mock.patch('functest.ci.run_tests.tb.TierBuilder',
222                         return_value=mock_obj), \
223             mock.patch('functest.ci.run_tests.source_rc_file'), \
224             mock.patch('functest.ci.run_tests.generate_report.init'), \
225                 mock.patch('functest.ci.run_tests.run_tier') as m:
226             self.assertEqual(run_tests.main(**kwargs),
227                              run_tests.Result.EX_OK)
228             self.assertTrue(m.called)
229
230         mock_obj = mock.Mock()
231         args = {'get_tier.return_value': False,
232                 'get_test.return_value': True}
233         mock_obj.configure_mock(**args)
234         with mock.patch('functest.ci.run_tests.tb.TierBuilder',
235                         return_value=mock_obj), \
236             mock.patch('functest.ci.run_tests.source_rc_file'), \
237             mock.patch('functest.ci.run_tests.generate_report.init'), \
238                 mock.patch('functest.ci.run_tests.run_test') as m:
239             self.assertEqual(run_tests.main(**kwargs),
240                              run_tests.Result.EX_OK)
241             self.assertTrue(m.called)
242
243         kwargs = {'test': 'all', 'noclean': True, 'report': True}
244         mock_obj = mock.Mock()
245         args = {'get_tier.return_value': False,
246                 'get_test.return_value': False}
247         mock_obj.configure_mock(**args)
248         with mock.patch('functest.ci.run_tests.tb.TierBuilder',
249                         return_value=mock_obj), \
250             mock.patch('functest.ci.run_tests.source_rc_file'), \
251             mock.patch('functest.ci.run_tests.generate_report.init'), \
252                 mock.patch('functest.ci.run_tests.run_all') as m:
253             self.assertEqual(run_tests.main(**kwargs),
254                              run_tests.Result.EX_OK)
255             self.assertTrue(m.called)
256
257         kwargs = {'test': 'any', 'noclean': True, 'report': True}
258         mock_obj = mock.Mock()
259         args = {'get_tier.return_value': False,
260                 'get_test.return_value': False}
261         mock_obj.configure_mock(**args)
262         with mock.patch('functest.ci.run_tests.tb.TierBuilder',
263                         return_value=mock_obj), \
264             mock.patch('functest.ci.run_tests.source_rc_file'), \
265             mock.patch('functest.ci.run_tests.generate_report.init'), \
266                 mock.patch('functest.ci.run_tests.logger.debug') as m:
267             self.assertEqual(run_tests.main(**kwargs),
268                              run_tests.Result.EX_ERROR)
269             self.assertTrue(m.called)
270
271 if __name__ == "__main__":
272     unittest.main(verbosity=2)