Merge "Fully cover pytest_suite_runner.py"
[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 import logging
9 import unittest
10
11 import mock
12
13 from functest.ci import run_tests
14 from functest.utils.constants import CONST
15 from functest.core.testcase import TestCase
16
17
18 class FakeModule(TestCase):
19
20     def run(self):
21         return TestCase.EX_OK
22
23     def push_to_db(self):
24         return TestCase.EX_OK
25
26     def is_successful(self):
27         return TestCase.EX_OK
28
29
30 class RunTestsTesting(unittest.TestCase):
31
32     def setUp(self):
33         self.runner = run_tests.Runner()
34         self.sep = 'test_sep'
35         self.creds = {'OS_AUTH_URL': 'http://test_ip:test_port/v2.0',
36                       'OS_USERNAME': 'test_os_username',
37                       'OS_TENANT_NAME': 'test_tenant',
38                       'OS_PASSWORD': 'test_password'}
39         self.test = {'test_name': 'test_name'}
40         self.tier = mock.Mock()
41         attrs = {'get_name.return_value': 'test_tier',
42                  'get_tests.return_value': ['test1', 'test2'],
43                  'get_ci_loop.return_value': 'test_ci_loop',
44                  'get_test_names.return_value': ['test1', 'test2']}
45         self.tier.configure_mock(**attrs)
46
47         self.tiers = mock.Mock()
48         attrs = {'get_tiers.return_value': [self.tier]}
49         self.tiers.configure_mock(**attrs)
50
51         self.run_tests_parser = run_tests.RunTestsParser()
52
53     @mock.patch('functest.ci.run_tests.logger.info')
54     def test_print_separator(self, mock_logger_info):
55         self.runner.print_separator(self.sep)
56         mock_logger_info.assert_called_once_with(self.sep * 44)
57
58     @mock.patch('functest.ci.run_tests.logger.error')
59     def test_source_rc_file_missing_file(self, mock_logger_error):
60         with mock.patch('functest.ci.run_tests.os.path.isfile',
61                         return_value=False), \
62                 self.assertRaises(Exception):
63             self.runner.source_rc_file()
64
65     @mock.patch('functest.ci.run_tests.logger.debug')
66     @mock.patch('functest.ci.run_tests.os.path.isfile',
67                 return_value=True)
68     def test_source_rc_file_default(self, *args):
69         with mock.patch('functest.ci.run_tests.os_utils.source_credentials',
70                         return_value=self.creds):
71             self.runner.source_rc_file()
72
73     @mock.patch('functest.ci.run_tests.os_snapshot.main')
74     def test_generate_os_snapshot(self, mock_os_snap):
75         self.runner.generate_os_snapshot()
76         self.assertTrue(mock_os_snap.called)
77
78     @mock.patch('functest.ci.run_tests.os_clean.main')
79     def test_cleanup(self, mock_os_clean):
80         self.runner.cleanup()
81         self.assertTrue(mock_os_clean.called)
82
83     def test_get_run_dict_if_defined_default(self):
84         mock_obj = mock.Mock()
85         with mock.patch('functest.ci.run_tests.'
86                         'ft_utils.get_dict_by_test',
87                         return_value={'run': mock_obj}):
88             self.assertEqual(self.runner.get_run_dict('test_name'),
89                              mock_obj)
90
91     @mock.patch('functest.ci.run_tests.logger.error')
92     def test_get_run_dict_if_defined_missing_config_option(self,
93                                                            mock_logger_error):
94         with mock.patch('functest.ci.run_tests.'
95                         'ft_utils.get_dict_by_test',
96                         return_value=None):
97             testname = 'test_name'
98             self.assertEqual(self.runner.get_run_dict(testname),
99                              None)
100             mock_logger_error.assert_called_once_with("Cannot get {}'s config "
101                                                       "options"
102                                                       .format(testname))
103
104         with mock.patch('functest.ci.run_tests.'
105                         'ft_utils.get_dict_by_test',
106                         return_value={}):
107             testname = 'test_name'
108             self.assertEqual(self.runner.get_run_dict(testname),
109                              None)
110
111     @mock.patch('functest.ci.run_tests.logger.exception')
112     def test_get_run_dict_if_defined_exception(self,
113                                                mock_logger_except):
114         with mock.patch('functest.ci.run_tests.'
115                         'ft_utils.get_dict_by_test',
116                         side_effect=Exception):
117             testname = 'test_name'
118             self.assertEqual(self.runner.get_run_dict(testname),
119                              None)
120             mock_logger_except.assert_called_once_with("Cannot get {}'s config"
121                                                        " options"
122                                                        .format(testname))
123
124     def test_run_tests_import_test_class_exception(self):
125         mock_test = mock.Mock()
126         args = {'get_name.return_value': 'test_name',
127                 'needs_clean.return_value': False}
128         mock_test.configure_mock(**args)
129         with mock.patch('functest.ci.run_tests.Runner.print_separator'),\
130             mock.patch('functest.ci.run_tests.Runner.source_rc_file'), \
131             mock.patch('functest.ci.run_tests.Runner.get_run_dict',
132                        return_value=None), \
133                 self.assertRaises(Exception) as context:
134             self.runner(mock_test, 'tier_name')
135             msg = "Cannot import the class for the test case."
136             self.assertTrue(msg in context)
137
138     @mock.patch('functest.ci.run_tests.Runner.print_separator')
139     @mock.patch('functest.ci.run_tests.Runner.source_rc_file')
140     @mock.patch('functest.ci.run_tests.Runner.generate_os_snapshot')
141     @mock.patch('functest.ci.run_tests.Runner.cleanup')
142     @mock.patch('importlib.import_module', name="module",
143                 return_value=mock.Mock(test_class=mock.Mock(
144                     side_effect=FakeModule)))
145     @mock.patch('functest.utils.functest_utils.get_dict_by_test')
146     def test_run_tests_default(self, *args):
147         mock_test = mock.Mock()
148         kwargs = {'get_name.return_value': 'test_name',
149                   'needs_clean.return_value': True}
150         mock_test.configure_mock(**kwargs)
151         test_run_dict = {'module': 'test_module',
152                          'class': 'test_class'}
153         with mock.patch('functest.ci.run_tests.Runner.get_run_dict',
154                         return_value=test_run_dict):
155             self.runner.clean_flag = True
156             self.runner.run_test(mock_test, 'tier_name')
157         self.assertEqual(self.runner.overall_result,
158                          run_tests.Result.EX_OK)
159
160     @mock.patch('functest.ci.run_tests.logger.info')
161     def test_run_tier_default(self, mock_logger_info):
162         with mock.patch('functest.ci.run_tests.Runner.print_separator'), \
163                 mock.patch(
164                     'functest.ci.run_tests.Runner.run_test') as mock_method:
165             self.runner.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.Runner.print_separator'):
173             self.tier.get_tests.return_value = None
174             self.assertEqual(self.runner.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(
180                 'functest.ci.run_tests.Runner.run_tier') as mock_method:
181             CONST.__setattr__('CI_LOOP', 'test_ci_loop')
182             self.runner.run_all(self.tiers)
183             mock_method.assert_any_call(self.tier)
184             self.assertTrue(mock_logger_info.called)
185
186     @mock.patch('functest.ci.run_tests.logger.info')
187     def test_run_all_missing_tier(self, mock_logger_info):
188         CONST.__setattr__('CI_LOOP', 'loop_re_not_available')
189         self.runner.run_all(self.tiers)
190         self.assertTrue(mock_logger_info.called)
191
192     def test_main_failed(self):
193         kwargs = {'test': 'test_name', 'noclean': True, 'report': True}
194         mock_obj = mock.Mock()
195         args = {'get_tier.return_value': False,
196                 'get_test.return_value': False}
197         mock_obj.configure_mock(**args)
198         with mock.patch('functest.ci.run_tests.tb.TierBuilder'), \
199             mock.patch('functest.ci.run_tests.Runner.source_rc_file',
200                        side_effect=Exception):
201             self.assertEqual(self.runner.main(**kwargs),
202                              run_tests.Result.EX_ERROR)
203         with mock.patch('functest.ci.run_tests.tb.TierBuilder',
204                         return_value=mock_obj), \
205             mock.patch('functest.ci.run_tests.Runner.source_rc_file',
206                        side_effect=Exception):
207             self.assertEqual(self.runner.main(**kwargs),
208                              run_tests.Result.EX_ERROR)
209
210     def test_main_tier(self, *args):
211         mock_tier = mock.Mock()
212         args = {'get_name.return_value': 'tier_name'}
213         mock_tier.configure_mock(**args)
214         kwargs = {'test': 'tier_name', 'noclean': True, 'report': True}
215         mock_obj = mock.Mock()
216         args = {'get_tier.return_value': mock_tier,
217                 'get_test.return_value': None}
218         mock_obj.configure_mock(**args)
219         with mock.patch('functest.ci.run_tests.tb.TierBuilder',
220                         return_value=mock_obj), \
221             mock.patch('functest.ci.run_tests.Runner.source_rc_file'), \
222                 mock.patch('functest.ci.run_tests.Runner.run_tier') as m:
223             self.assertEqual(self.runner.main(**kwargs),
224                              run_tests.Result.EX_OK)
225             self.assertTrue(m.called)
226
227     def test_main_test(self, *args):
228         kwargs = {'test': 'test_name', 'noclean': True, 'report': True}
229         mock_test = mock.Mock()
230         args = {'get_name.return_value': 'test_name',
231                 'needs_clean.return_value': True}
232         mock_test.configure_mock(**args)
233         mock_obj = mock.Mock()
234         args = {'get_tier.return_value': None,
235                 'get_test.return_value': mock_test}
236         mock_obj.configure_mock(**args)
237         with mock.patch('functest.ci.run_tests.tb.TierBuilder',
238                         return_value=mock_obj), \
239             mock.patch('functest.ci.run_tests.Runner.source_rc_file'), \
240                 mock.patch('functest.ci.run_tests.Runner.run_test') as m:
241             self.assertEqual(self.runner.main(**kwargs),
242                              run_tests.Result.EX_OK)
243             self.assertTrue(m.called)
244
245     def test_main_all_tier(self, *args):
246         kwargs = {'test': 'all', 'noclean': True, 'report': True}
247         mock_obj = mock.Mock()
248         args = {'get_tier.return_value': None,
249                 'get_test.return_value': None}
250         mock_obj.configure_mock(**args)
251         with mock.patch('functest.ci.run_tests.tb.TierBuilder',
252                         return_value=mock_obj), \
253             mock.patch('functest.ci.run_tests.Runner.source_rc_file'), \
254                 mock.patch('functest.ci.run_tests.Runner.run_all') as m:
255             self.assertEqual(self.runner.main(**kwargs),
256                              run_tests.Result.EX_OK)
257             self.assertTrue(m.called)
258
259     def test_main_any_tier_test_ko(self, *args):
260         kwargs = {'test': 'any', 'noclean': True, 'report': True}
261         mock_obj = mock.Mock()
262         args = {'get_tier.return_value': None,
263                 'get_test.return_value': None}
264         mock_obj.configure_mock(**args)
265         with mock.patch('functest.ci.run_tests.tb.TierBuilder',
266                         return_value=mock_obj), \
267             mock.patch('functest.ci.run_tests.Runner.source_rc_file'), \
268                 mock.patch('functest.ci.run_tests.logger.debug') as m:
269             self.assertEqual(self.runner.main(**kwargs),
270                              run_tests.Result.EX_ERROR)
271             self.assertTrue(m.called)
272
273
274 if __name__ == "__main__":
275     logging.disable(logging.CRITICAL)
276     unittest.main(verbosity=2)