Merge "Precise self.result in pytest_suite_runner.py"
[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.__setattr__('INSTALLER_TYPE', 'installer_type')
116             CONST.__setattr__('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.__setattr__('INSTALLER_TYPE', 'installer_type')
132             CONST.__setattr__('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.'
153                 'os.path.exists', return_value=False)
154     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs',
155                 side_effect=Exception)
156     def test_run_makedirs_ko(self, *args):
157         self.assertEqual(self.tempestcommon.run(),
158                          testcase.TestCase.EX_RUN_ERROR)
159
160     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
161                 'os.path.exists', return_value=False)
162     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
163     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
164                 'conf_utils.create_tempest_resources', side_effect=Exception)
165     def test_run_create_tempest_resources_ko(self, *args):
166         self.assertEqual(self.tempestcommon.run(),
167                          testcase.TestCase.EX_RUN_ERROR)
168
169     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
170                 'os.path.exists', return_value=False)
171     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
172     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
173                 'conf_utils.create_tempest_resources', return_value={})
174     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
175                 'conf_utils.configure_tempest', side_effect=Exception)
176     def test_run_configure_tempest_ko(self, *args):
177         self.assertEqual(self.tempestcommon.run(),
178                          testcase.TestCase.EX_RUN_ERROR)
179
180     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
181                 'os.path.exists', return_value=False)
182     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
183     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
184                 'conf_utils.create_tempest_resources', return_value={})
185     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
186                 'conf_utils.configure_tempest')
187     def _test_run(self, status, *args):
188         self.assertEqual(self.tempestcommon.run(), status)
189
190     def test_run_missing_generate_test_list(self):
191         with mock.patch.object(self.tempestcommon, 'generate_test_list',
192                                side_effect=Exception):
193             self._test_run(testcase.TestCase.EX_RUN_ERROR)
194
195     def test_run_apply_tempest_blacklist_ko(self):
196         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
197                     mock.patch.object(self.tempestcommon,
198                                       'apply_tempest_blacklist',
199                                       side_effect=Exception()):
200             self._test_run(testcase.TestCase.EX_RUN_ERROR)
201
202     def test_run_verifier_tests_ko(self, *args):
203         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
204                 mock.patch.object(self.tempestcommon,
205                                   'apply_tempest_blacklist'), \
206                 mock.patch.object(self.tempestcommon, 'run_verifier_tests',
207                                   side_effect=Exception()), \
208                 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
209                                   side_effect=Exception):
210             self._test_run(testcase.TestCase.EX_RUN_ERROR)
211
212     def test_run_parse_verifier_result_ko(self, *args):
213         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
214                 mock.patch.object(self.tempestcommon,
215                                   'apply_tempest_blacklist'), \
216                 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
217                 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
218                                   side_effect=Exception):
219             self._test_run(testcase.TestCase.EX_RUN_ERROR)
220
221     def test_run(self, *args):
222         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
223                 mock.patch.object(self.tempestcommon,
224                                   'apply_tempest_blacklist'), \
225                 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
226                 mock.patch.object(self.tempestcommon, 'parse_verifier_result'):
227             self._test_run(testcase.TestCase.EX_OK)
228
229
230 if __name__ == "__main__":
231     logging.disable(logging.CRITICAL)
232     unittest.main(verbosity=2)