Modify how to disable logging in unit test.
[functest.git] / functest / tests / unit / openstack / tempest / test_tempest.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.core import testcase
14 from functest.opnfv_tests.openstack.tempest import tempest
15 from functest.opnfv_tests.openstack.tempest import conf_utils
16 from functest.utils.constants import CONST
17
18
19 class OSTempestTesting(unittest.TestCase):
20
21     def setUp(self):
22         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
23                         'conf_utils.get_verifier_id',
24                         return_value='test_deploy_id'), \
25             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
26                        'conf_utils.get_verifier_deployment_id',
27                        return_value='test_deploy_id'), \
28             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
29                        'conf_utils.get_verifier_repo_dir',
30                        return_value='test_verifier_repo_dir'), \
31             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
32                        'conf_utils.get_verifier_deployment_dir',
33                        return_value='test_verifier_deploy_dir'):
34             self.tempestcommon = tempest.TempestCommon()
35             self.tempestsmoke_serial = tempest.TempestSmokeSerial()
36             self.tempestsmoke_parallel = tempest.TempestSmokeParallel()
37             self.tempestfull_parallel = tempest.TempestFullParallel()
38             with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
39                             'conf_utils.install_verifier_ext'):
40                 self.tempestmultisite = tempest.TempestMultisite()
41             self.tempestcustom = tempest.TempestCustom()
42             self.tempestdefcore = tempest.TempestDefcore()
43
44     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.debug')
45     def test_generate_test_list_defcore_mode(self, mock_logger_debug):
46         self.tempestcommon.MODE = 'defcore'
47         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
48                         'shutil.copyfile') as m:
49             self.tempestcommon.generate_test_list('test_verifier_repo_dir')
50             self.assertTrue(m.called)
51
52     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.error')
53     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.debug')
54     def test_generate_test_list_custom_mode_missing_file(self,
55                                                          mock_logger_debug,
56                                                          mock_logger_error):
57         self.tempestcommon.MODE = 'custom'
58         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
59                         'os.path.isfile', return_value=False), \
60                 self.assertRaises(Exception) as context:
61             msg = "Tempest test list file %s NOT found."
62             self.tempestcommon.generate_test_list('test_verifier_repo_dir')
63             self.assertTrue((msg % conf_utils.TEMPEST_CUSTOM) in context)
64
65     def test_generate_test_list_custom_mode_default(self):
66         self.tempestcommon.MODE = 'custom'
67         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
68                         'shutil.copyfile') as m, \
69             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
70                        'os.path.isfile', return_value=True):
71             self.tempestcommon.generate_test_list('test_verifier_repo_dir')
72             self.assertTrue(m.called)
73
74     def _test_generate_test_list_mode_default(self, mode):
75         self.tempestcommon.MODE = mode
76         if self.tempestcommon.MODE == 'smoke':
77             testr_mode = "smoke"
78         elif self.tempestcommon.MODE == 'feature_multisite':
79             testr_mode = "'[Kk]ingbird'"
80         elif self.tempestcommon.MODE == 'full':
81             testr_mode = ""
82         else:
83             testr_mode = 'tempest.api.' + self.tempestcommon.MODE
84         conf_utils.TEMPEST_RAW_LIST = 'raw_list'
85         verifier_repo_dir = 'test_verifier_repo_dir'
86         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
87                         'ft_utils.execute_command') as m:
88             cmd = ("cd {0};"
89                    "testr list-tests {1} > {2};"
90                    "cd -;".format(verifier_repo_dir,
91                                   testr_mode,
92                                   conf_utils.TEMPEST_RAW_LIST))
93             self.tempestcommon.generate_test_list('test_verifier_repo_dir')
94             m.assert_any_call(cmd)
95
96     def test_generate_test_list_smoke_mode(self):
97         self._test_generate_test_list_mode_default('smoke')
98
99     def test_generate_test_list_feature_multisite_mode(self):
100         self._test_generate_test_list_mode_default('feature_multisite')
101
102     def test_generate_test_list_full_mode(self):
103         self._test_generate_test_list_mode_default('full')
104
105     def test_parse_verifier_result_missing_verification_uuid(self):
106         self.tempestcommon.VERIFICATION_ID = None
107         with self.assertRaises(Exception):
108             self.tempestcommon.parse_verifier_result()
109
110     def test_apply_tempest_blacklist_no_blacklist(self):
111         with mock.patch('__builtin__.open', mock.mock_open()) as m, \
112             mock.patch.object(self.tempestcommon, 'read_file',
113                               return_value=['test1', 'test2']):
114             conf_utils.TEMPEST_BLACKLIST = Exception
115             CONST.INSTALLER_TYPE = 'installer_type'
116             CONST.DEPLOY_SCENARIO = 'deploy_scenario'
117             self.tempestcommon.apply_tempest_blacklist()
118             obj = m()
119             obj.write.assert_any_call('test1\n')
120             obj.write.assert_any_call('test2\n')
121
122     def test_apply_tempest_blacklist_default(self):
123         item_dict = {'scenarios': ['deploy_scenario'],
124                      'installers': ['installer_type'],
125                      'tests': ['test2']}
126         with mock.patch('__builtin__.open', mock.mock_open()) as m, \
127             mock.patch.object(self.tempestcommon, 'read_file',
128                               return_value=['test1', 'test2']), \
129             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
130                        'yaml.safe_load', return_value=item_dict):
131             CONST.INSTALLER_TYPE = 'installer_type'
132             CONST.DEPLOY_SCENARIO = 'deploy_scenario'
133             self.tempestcommon.apply_tempest_blacklist()
134             obj = m()
135             obj.write.assert_any_call('test1\n')
136             self.assertFalse(obj.write.assert_any_call('test2\n'))
137
138     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.info')
139     def test_run_verifier_tests_default(self, mock_logger_info):
140         with mock.patch('__builtin__.open', mock.mock_open()), \
141             mock.patch('__builtin__.iter', return_value=['\} tempest\.']), \
142             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
143                        'subprocess.Popen'):
144             conf_utils.TEMPEST_LIST = 'test_tempest_list'
145             cmd_line = ("rally verify start  --load-list "
146                         "test_tempest_list --detailed")
147             self.tempestcommon.run_verifier_tests()
148             mock_logger_info. \
149                 assert_any_call("Starting Tempest test suite: '%s'."
150                                 % cmd_line)
151
152     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.info')
153     def test_parse_verifier_result_default(self, mock_logger_info):
154         self.tempestcommon.VERIFICATION_ID = 'test_uuid'
155         self.tempestcommon.case_name = 'test_case_name'
156         stdout = ['Testscount||2', 'Success||2', 'Skipped||0', 'Failures||0']
157         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
158                         'subprocess.Popen') as mock_popen, \
159             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
160                        'ft_utils.check_success_rate') as mock_method, \
161                 mock.patch('__builtin__.open', mock.mock_open()):
162             mock_stdout = mock.Mock()
163             attrs = {'stdout': stdout}
164             mock_stdout.configure_mock(**attrs)
165             mock_popen.return_value = mock_stdout
166
167             self.tempestcommon.parse_verifier_result()
168             mock_method.assert_any_call('test_case_name', 100)
169
170     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
171                 'os.path.exists', return_value=False)
172     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs',
173                 side_effect=Exception)
174     def test_run_makedirs_ko(self, *args):
175         self.assertEqual(self.tempestcommon.run(),
176                          testcase.TestCase.EX_RUN_ERROR)
177
178     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
179                 'os.path.exists', return_value=False)
180     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
181     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
182                 'conf_utils.create_tempest_resources', side_effect=Exception)
183     def test_run_create_tempest_resources_ko(self, *args):
184         self.assertEqual(self.tempestcommon.run(),
185                          testcase.TestCase.EX_RUN_ERROR)
186
187     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
188                 'os.path.exists', return_value=False)
189     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
190     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
191                 'conf_utils.create_tempest_resources', return_value={})
192     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
193                 'conf_utils.configure_tempest', side_effect=Exception)
194     def test_run_configure_tempest_ko(self, *args):
195         self.assertEqual(self.tempestcommon.run(),
196                          testcase.TestCase.EX_RUN_ERROR)
197
198     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
199                 'os.path.exists', return_value=False)
200     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
201     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
202                 'conf_utils.create_tempest_resources', return_value={})
203     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
204                 'conf_utils.configure_tempest')
205     def _test_run(self, status, *args):
206         self.assertEqual(self.tempestcommon.run(), status)
207
208     def test_run_missing_generate_test_list(self):
209         with mock.patch.object(self.tempestcommon, 'generate_test_list',
210                                side_effect=Exception):
211             self._test_run(testcase.TestCase.EX_RUN_ERROR)
212
213     def test_run_apply_tempest_blacklist_ko(self):
214         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
215                     mock.patch.object(self.tempestcommon,
216                                       'apply_tempest_blacklist',
217                                       side_effect=Exception()):
218             self._test_run(testcase.TestCase.EX_RUN_ERROR)
219
220     def test_run_verifier_tests_ko(self, *args):
221         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
222                 mock.patch.object(self.tempestcommon,
223                                   'apply_tempest_blacklist'), \
224                 mock.patch.object(self.tempestcommon, 'run_verifier_tests',
225                                   side_effect=Exception()), \
226                 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
227                                   side_effect=Exception):
228             self._test_run(testcase.TestCase.EX_RUN_ERROR)
229
230     def test_run_parse_verifier_result_ko(self, *args):
231         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
232                 mock.patch.object(self.tempestcommon,
233                                   'apply_tempest_blacklist'), \
234                 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
235                 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
236                                   side_effect=Exception):
237             self._test_run(testcase.TestCase.EX_RUN_ERROR)
238
239     def test_run(self, *args):
240         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
241                 mock.patch.object(self.tempestcommon,
242                                   'apply_tempest_blacklist'), \
243                 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
244                 mock.patch.object(self.tempestcommon, 'parse_verifier_result'):
245             self._test_run(testcase.TestCase.EX_OK)
246
247
248 if __name__ == "__main__":
249     logging.disable(logging.CRITICAL)
250     unittest.main(verbosity=2)