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