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