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
13 from functest.ci import run_tests
14 from functest.utils.constants import CONST
15 from functest.core.testcase import TestCase
18 class FakeModule(TestCase):
26 def is_successful(self):
30 class RunTestsTesting(unittest.TestCase):
33 self.runner = run_tests.Runner()
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()
42 test1.get_name.return_value = 'test1'
44 test2.get_name.return_value = 'test2'
45 attrs = {'get_name.return_value': 'test_tier',
46 'get_tests.return_value': [test1, test2],
47 'get_ci_loop.return_value': 'test_ci_loop',
48 'get_test_names.return_value': ['test1', 'test2']}
49 self.tier.configure_mock(**attrs)
51 self.tiers = mock.Mock()
52 attrs = {'get_tiers.return_value': [self.tier]}
53 self.tiers.configure_mock(**attrs)
55 self.run_tests_parser = run_tests.RunTestsParser()
57 @mock.patch('functest.ci.run_tests.logger.info')
58 def test_print_separator(self, mock_logger_info):
59 self.runner.print_separator(self.sep)
60 mock_logger_info.assert_called_once_with(self.sep * 44)
62 @mock.patch('functest.ci.run_tests.logger.error')
63 def test_source_rc_file_missing_file(self, mock_logger_error):
64 with mock.patch('functest.ci.run_tests.os.path.isfile',
65 return_value=False), \
66 self.assertRaises(Exception):
67 self.runner.source_rc_file()
69 @mock.patch('functest.ci.run_tests.logger.debug')
70 @mock.patch('functest.ci.run_tests.os.path.isfile',
72 def test_source_rc_file_default(self, *args):
73 with mock.patch('functest.ci.run_tests.os_utils.source_credentials',
74 return_value=self.creds):
75 self.runner.source_rc_file()
77 def test_get_run_dict_if_defined_default(self):
78 mock_obj = mock.Mock()
79 with mock.patch('functest.ci.run_tests.'
80 'ft_utils.get_dict_by_test',
81 return_value={'run': mock_obj}):
82 self.assertEqual(self.runner.get_run_dict('test_name'),
85 @mock.patch('functest.ci.run_tests.logger.error')
86 def test_get_run_dict_if_defined_missing_config_option(self,
88 with mock.patch('functest.ci.run_tests.'
89 'ft_utils.get_dict_by_test',
91 testname = 'test_name'
92 self.assertEqual(self.runner.get_run_dict(testname),
94 mock_logger_error.assert_called_once_with("Cannot get {}'s config "
98 with mock.patch('functest.ci.run_tests.'
99 'ft_utils.get_dict_by_test',
101 testname = 'test_name'
102 self.assertEqual(self.runner.get_run_dict(testname),
105 @mock.patch('functest.ci.run_tests.logger.exception')
106 def test_get_run_dict_if_defined_exception(self,
108 with mock.patch('functest.ci.run_tests.'
109 'ft_utils.get_dict_by_test',
110 side_effect=Exception):
111 testname = 'test_name'
112 self.assertEqual(self.runner.get_run_dict(testname),
114 mock_logger_except.assert_called_once_with("Cannot get {}'s config"
118 def test_run_tests_import_test_class_exception(self):
119 mock_test = mock.Mock()
120 args = {'get_name.return_value': 'test_name',
121 'needs_clean.return_value': False}
122 mock_test.configure_mock(**args)
123 with mock.patch('functest.ci.run_tests.Runner.print_separator'),\
124 mock.patch('functest.ci.run_tests.Runner.source_rc_file'), \
125 mock.patch('functest.ci.run_tests.Runner.get_run_dict',
126 return_value=None), \
127 self.assertRaises(Exception) as context:
128 self.runner(mock_test, 'tier_name')
129 msg = "Cannot import the class for the test case."
130 self.assertTrue(msg in context)
132 @mock.patch('functest.ci.run_tests.Runner.print_separator')
133 @mock.patch('functest.ci.run_tests.Runner.source_rc_file')
134 @mock.patch('importlib.import_module', name="module",
135 return_value=mock.Mock(test_class=mock.Mock(
136 side_effect=FakeModule)))
137 @mock.patch('functest.utils.functest_utils.get_dict_by_test')
138 def test_run_tests_default(self, *args):
139 mock_test = mock.Mock()
140 kwargs = {'get_name.return_value': 'test_name',
141 'needs_clean.return_value': True}
142 mock_test.configure_mock(**kwargs)
143 test_run_dict = {'module': 'test_module',
144 'class': 'test_class'}
145 with mock.patch('functest.ci.run_tests.Runner.get_run_dict',
146 return_value=test_run_dict):
147 self.runner.clean_flag = True
148 self.runner.run_test(mock_test, 'tier_name')
149 self.assertEqual(self.runner.overall_result,
150 run_tests.Result.EX_OK)
152 @mock.patch('functest.ci.run_tests.logger.info')
153 def test_run_tier_default(self, mock_logger_info):
154 with mock.patch('functest.ci.run_tests.Runner.print_separator'), \
156 'functest.ci.run_tests.Runner.run_test',
157 return_value=TestCase.EX_OK) as mock_method:
158 self.runner.run_tier(self.tier)
159 mock_method.assert_any_call(mock.ANY, 'test_tier')
160 self.assertTrue(mock_logger_info.called)
162 @mock.patch('functest.ci.run_tests.logger.info')
163 def test_run_tier_missing_test(self, mock_logger_info):
164 with mock.patch('functest.ci.run_tests.Runner.print_separator'):
165 self.tier.get_tests.return_value = None
166 self.assertEqual(self.runner.run_tier(self.tier), 0)
167 self.assertTrue(mock_logger_info.called)
169 @mock.patch('functest.ci.run_tests.logger.info')
170 def test_run_all_default(self, mock_logger_info):
172 'functest.ci.run_tests.Runner.run_tier') as mock_method:
173 CONST.__setattr__('CI_LOOP', 'test_ci_loop')
174 self.runner.run_all(self.tiers)
175 mock_method.assert_any_call(self.tier)
176 self.assertTrue(mock_logger_info.called)
178 @mock.patch('functest.ci.run_tests.logger.info')
179 def test_run_all_missing_tier(self, mock_logger_info):
180 CONST.__setattr__('CI_LOOP', 'loop_re_not_available')
181 self.runner.run_all(self.tiers)
182 self.assertTrue(mock_logger_info.called)
184 def test_main_failed(self):
185 kwargs = {'test': 'test_name', 'noclean': True, 'report': True}
186 mock_obj = mock.Mock()
187 args = {'get_tier.return_value': False,
188 'get_test.return_value': False}
189 mock_obj.configure_mock(**args)
190 with mock.patch('functest.ci.run_tests.tb.TierBuilder'), \
191 mock.patch('functest.ci.run_tests.Runner.source_rc_file',
192 side_effect=Exception):
193 self.assertEqual(self.runner.main(**kwargs),
194 run_tests.Result.EX_ERROR)
195 with mock.patch('functest.ci.run_tests.tb.TierBuilder',
196 return_value=mock_obj), \
197 mock.patch('functest.ci.run_tests.Runner.source_rc_file',
198 side_effect=Exception):
199 self.assertEqual(self.runner.main(**kwargs),
200 run_tests.Result.EX_ERROR)
202 def test_main_tier(self, *args):
203 mock_tier = mock.Mock()
204 args = {'get_name.return_value': 'tier_name'}
205 mock_tier.configure_mock(**args)
206 kwargs = {'test': 'tier_name', 'noclean': True, 'report': True}
207 mock_obj = mock.Mock()
208 args = {'get_tier.return_value': mock_tier,
209 'get_test.return_value': None}
210 mock_obj.configure_mock(**args)
211 with mock.patch('functest.ci.run_tests.tb.TierBuilder',
212 return_value=mock_obj), \
213 mock.patch('functest.ci.run_tests.Runner.source_rc_file'), \
214 mock.patch('functest.ci.run_tests.Runner.run_tier') as m:
215 self.assertEqual(self.runner.main(**kwargs),
216 run_tests.Result.EX_OK)
217 self.assertTrue(m.called)
219 def test_main_test(self, *args):
220 kwargs = {'test': 'test_name', 'noclean': True, 'report': True}
221 mock_test = mock.Mock()
222 args = {'get_name.return_value': 'test_name',
223 'needs_clean.return_value': True}
224 mock_test.configure_mock(**args)
225 mock_obj = mock.Mock()
226 args = {'get_tier.return_value': None,
227 'get_test.return_value': mock_test}
228 mock_obj.configure_mock(**args)
229 with mock.patch('functest.ci.run_tests.tb.TierBuilder',
230 return_value=mock_obj), \
231 mock.patch('functest.ci.run_tests.Runner.source_rc_file'), \
232 mock.patch('functest.ci.run_tests.Runner.run_test',
233 return_value=TestCase.EX_OK) as m:
234 self.assertEqual(self.runner.main(**kwargs),
235 run_tests.Result.EX_OK)
236 self.assertTrue(m.called)
238 def test_main_all_tier(self, *args):
239 kwargs = {'test': 'all', 'noclean': True, 'report': True}
240 mock_obj = mock.Mock()
241 args = {'get_tier.return_value': None,
242 'get_test.return_value': None}
243 mock_obj.configure_mock(**args)
244 with mock.patch('functest.ci.run_tests.tb.TierBuilder',
245 return_value=mock_obj), \
246 mock.patch('functest.ci.run_tests.Runner.source_rc_file'), \
247 mock.patch('functest.ci.run_tests.Runner.run_all') as m:
248 self.assertEqual(self.runner.main(**kwargs),
249 run_tests.Result.EX_OK)
250 self.assertTrue(m.called)
252 def test_main_any_tier_test_ko(self, *args):
253 kwargs = {'test': 'any', 'noclean': True, 'report': True}
254 mock_obj = mock.Mock()
255 args = {'get_tier.return_value': None,
256 'get_test.return_value': None}
257 mock_obj.configure_mock(**args)
258 with mock.patch('functest.ci.run_tests.tb.TierBuilder',
259 return_value=mock_obj), \
260 mock.patch('functest.ci.run_tests.Runner.source_rc_file'), \
261 mock.patch('functest.ci.run_tests.logger.debug') as m:
262 self.assertEqual(self.runner.main(**kwargs),
263 run_tests.Result.EX_ERROR)
264 self.assertTrue(m.called)
267 if __name__ == "__main__":
268 logging.disable(logging.CRITICAL)
269 unittest.main(verbosity=2)