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
14 from functest.ci import run_tests
15 from functest.utils.constants import CONST
18 class RunTestsTesting(unittest.TestCase):
20 logging.disable(logging.CRITICAL)
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)
36 self.tiers = mock.Mock()
37 attrs = {'get_tiers.return_value': [self.tier]}
38 self.tiers.configure_mock(**attrs)
40 self.run_tests_parser = run_tests.RunTestsParser()
41 self.global_variables = run_tests.GlobalVariables()
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)
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()
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',
59 mock.patch('functest.ci.run_tests.os_utils.source_credentials',
60 return_value=self.creds):
61 run_tests.source_rc_file()
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)
68 @mock.patch('functest.ci.run_tests.os_clean.main')
69 def test_cleanup(self, mock_os_clean):
71 self.assertTrue(mock_os_clean.called)
73 def test_get_run_dict_if_defined_default(self):
74 mock_obj = mock.Mock()
75 with mock.patch('functest.ci.run_tests.'
76 'ft_utils.get_dict_by_test',
77 return_value={'run': mock_obj}):
78 self.assertEqual(run_tests.get_run_dict('test_name'),
81 @mock.patch('functest.ci.run_tests.logger.error')
82 def test_get_run_dict_if_defined_missing_config_option(self,
84 with mock.patch('functest.ci.run_tests.'
85 'ft_utils.get_dict_by_test',
87 testname = 'test_name'
88 self.assertEqual(run_tests.get_run_dict(testname),
90 mock_logger_error.assert_called_once_with("Cannot get {}'s config "
94 with mock.patch('functest.ci.run_tests.'
95 'ft_utils.get_dict_by_test',
97 testname = 'test_name'
98 self.assertEqual(run_tests.get_run_dict(testname),
101 @mock.patch('functest.ci.run_tests.logger.exception')
102 def test_get_run_dict_if_defined_exception(self,
104 with mock.patch('functest.ci.run_tests.'
105 'ft_utils.get_dict_by_test',
106 side_effect=Exception):
107 testname = 'test_name'
108 self.assertEqual(run_tests.get_run_dict(testname),
110 mock_logger_except.assert_called_once_with("Cannot get {}'s config"
114 def test_run_tests_import_test_class_exception(self):
115 mock_test = mock.Mock()
116 args = {'get_name.return_value': 'test_name',
117 'needs_clean.return_value': False}
118 mock_test.configure_mock(**args)
119 with mock.patch('functest.ci.run_tests.print_separator'),\
120 mock.patch('functest.ci.run_tests.source_rc_file'), \
121 mock.patch('functest.ci.run_tests.get_run_dict',
122 return_value=None), \
123 self.assertRaises(Exception) as context:
124 run_tests.run_test(mock_test, 'tier_name')
125 msg = "Cannot import the class for the test case."
126 self.assertTrue(msg in context)
128 def test_run_tests_default(self):
129 mock_test = mock.Mock()
130 args = {'get_name.return_value': 'test_name',
131 'needs_clean.return_value': True}
132 mock_test.configure_mock(**args)
133 test_run_dict = {'module': 'test_module',
136 with mock.patch('functest.ci.run_tests.print_separator'),\
137 mock.patch('functest.ci.run_tests.source_rc_file'), \
138 mock.patch('functest.ci.run_tests.generate_os_snapshot'), \
139 mock.patch('functest.ci.run_tests.cleanup'), \
140 mock.patch('functest.ci.run_tests.get_run_dict',
141 return_value=test_run_dict), \
142 self.assertRaises(run_tests.BlockingTestFailed) as context:
143 run_tests.GlobalVariables.CLEAN_FLAG = True
144 run_tests.run_test(mock_test, 'tier_name')
145 msg = 'The test case test_name failed and is blocking'
146 self.assertTrue(msg in context)
148 @mock.patch('functest.ci.run_tests.logger.info')
149 def test_run_tier_default(self, mock_logger_info):
150 with mock.patch('functest.ci.run_tests.print_separator'), \
151 mock.patch('functest.ci.run_tests.run_test') as mock_method:
152 run_tests.run_tier(self.tier)
153 mock_method.assert_any_call('test1', 'test_tier')
154 mock_method.assert_any_call('test2', 'test_tier')
155 self.assertTrue(mock_logger_info.called)
157 @mock.patch('functest.ci.run_tests.logger.info')
158 def test_run_tier_missing_test(self, mock_logger_info):
159 with mock.patch('functest.ci.run_tests.print_separator'):
160 self.tier.get_tests.return_value = None
161 self.assertEqual(run_tests.run_tier(self.tier), 0)
162 self.assertTrue(mock_logger_info.called)
164 @mock.patch('functest.ci.run_tests.logger.info')
165 def test_run_all_default(self, mock_logger_info):
166 with mock.patch('functest.ci.run_tests.run_tier') as mock_method:
167 CONST.__setattr__('CI_LOOP', 'test_ci_loop')
168 run_tests.run_all(self.tiers)
169 mock_method.assert_any_call(self.tier)
170 self.assertTrue(mock_logger_info.called)
172 @mock.patch('functest.ci.run_tests.logger.info')
173 def test_run_all_missing_tier(self, mock_logger_info):
174 CONST.__setattr__('CI_LOOP', 'loop_re_not_available')
175 run_tests.run_all(self.tiers)
176 self.assertTrue(mock_logger_info.called)
178 def test_main_failed(self):
179 kwargs = {'test': 'test_name', 'noclean': True, 'report': True}
180 mock_obj = mock.Mock()
181 args = {'get_tier.return_value': False,
182 'get_test.return_value': False}
183 mock_obj.configure_mock(**args)
185 with mock.patch('functest.ci.run_tests.tb.TierBuilder'), \
186 mock.patch('functest.ci.run_tests.source_rc_file',
187 side_effect=Exception):
188 self.assertEqual(run_tests.main(**kwargs),
189 run_tests.Result.EX_ERROR)
191 with mock.patch('functest.ci.run_tests.tb.TierBuilder',
192 return_value=mock_obj), \
193 mock.patch('functest.ci.run_tests.source_rc_file',
194 side_effect=Exception):
195 self.assertEqual(run_tests.main(**kwargs),
196 run_tests.Result.EX_ERROR)
198 def test_main_default(self):
199 kwargs = {'test': 'test_name', 'noclean': True, 'report': True}
200 mock_obj = mock.Mock()
201 args = {'get_tier.return_value': True,
202 'get_test.return_value': False}
203 mock_obj.configure_mock(**args)
204 with mock.patch('functest.ci.run_tests.tb.TierBuilder',
205 return_value=mock_obj), \
206 mock.patch('functest.ci.run_tests.source_rc_file'), \
207 mock.patch('functest.ci.run_tests.run_tier') as m:
208 self.assertEqual(run_tests.main(**kwargs),
209 run_tests.Result.EX_OK)
210 self.assertTrue(m.called)
212 mock_obj = mock.Mock()
213 args = {'get_tier.return_value': False,
214 'get_test.return_value': True}
215 mock_obj.configure_mock(**args)
216 with mock.patch('functest.ci.run_tests.tb.TierBuilder',
217 return_value=mock_obj), \
218 mock.patch('functest.ci.run_tests.source_rc_file'), \
219 mock.patch('functest.ci.run_tests.run_test') as m:
220 self.assertEqual(run_tests.main(**kwargs),
221 run_tests.Result.EX_OK)
222 self.assertTrue(m.called)
224 kwargs = {'test': 'all', 'noclean': True, 'report': True}
225 mock_obj = mock.Mock()
226 args = {'get_tier.return_value': False,
227 'get_test.return_value': False}
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.source_rc_file'), \
232 mock.patch('functest.ci.run_tests.run_all') as m:
233 self.assertEqual(run_tests.main(**kwargs),
234 run_tests.Result.EX_OK)
235 self.assertTrue(m.called)
237 kwargs = {'test': 'any', 'noclean': True, 'report': True}
238 mock_obj = mock.Mock()
239 args = {'get_tier.return_value': False,
240 'get_test.return_value': False}
241 mock_obj.configure_mock(**args)
242 with mock.patch('functest.ci.run_tests.tb.TierBuilder',
243 return_value=mock_obj), \
244 mock.patch('functest.ci.run_tests.source_rc_file'), \
245 mock.patch('functest.ci.run_tests.logger.debug') as m:
246 self.assertEqual(run_tests.main(**kwargs),
247 run_tests.Result.EX_ERROR)
248 self.assertTrue(m.called)
251 if __name__ == "__main__":
252 unittest.main(verbosity=2)