ba2c1c48f330bdef078bd58ff2a5967c84b365e7
[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 # pylint: disable=missing-docstring
9
10 import logging
11 import os
12 import unittest
13
14 import mock
15
16 from functest.core import testcase
17 from functest.opnfv_tests.openstack.tempest import tempest
18 from functest.opnfv_tests.openstack.tempest import conf_utils
19
20 from snaps.openstack.os_credentials import OSCreds
21
22
23 class OSTempestTesting(unittest.TestCase):
24
25     def setUp(self):
26         os_creds = OSCreds(
27             username='user', password='pass',
28             auth_url='http://foo.com:5000/v3', project_name='bar')
29
30         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
31                         'conf_utils.get_verifier_id',
32                         return_value='test_deploy_id'), \
33             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
34                        'conf_utils.get_verifier_deployment_id',
35                        return_value='test_deploy_id'), \
36             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
37                        'conf_utils.get_verifier_repo_dir',
38                        return_value='test_verifier_repo_dir'), \
39             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
40                        'conf_utils.get_verifier_deployment_dir',
41                        return_value='test_verifier_deploy_dir'), \
42             mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
43                        'get_credentials',
44                        return_value=os_creds):
45             self.tempestcommon = tempest.TempestCommon()
46             self.tempestsmoke_serial = tempest.TempestSmokeSerial()
47             self.tempestsmoke_parallel = tempest.TempestSmokeParallel()
48             self.tempestfull_parallel = tempest.TempestFullParallel()
49             self.tempestcustom = tempest.TempestCustom()
50             self.tempestdefcore = tempest.TempestDefcore()
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_gen_tl_cm_missing_file(self, mock_logger_debug,
55                                     mock_logger_error):
56         # pylint: disable=unused-argument
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(
64                 (msg % conf_utils.TEMPEST_CUSTOM) in context.exception)
65
66     def test_gen_tl_cm_default(self):
67         self.tempestcommon.mode = 'custom'
68         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
69                         'shutil.copyfile') as mock_copyfile, \
70             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
71                        'os.path.isfile', return_value=True):
72             self.tempestcommon.generate_test_list('test_verifier_repo_dir')
73             self.assertTrue(mock_copyfile.called)
74
75     def _test_gen_tl_mode_default(self, mode):
76         self.tempestcommon.mode = mode
77         if self.tempestcommon.mode == 'smoke':
78             testr_mode = r"'tempest\.(api|scenario).*\[.*\bsmoke\b.*\]'"
79         elif self.tempestcommon.mode == 'full':
80             testr_mode = r"'^tempest\.'"
81         else:
82             testr_mode = 'tempest.api.' + self.tempestcommon.mode
83         conf_utils.TEMPEST_RAW_LIST = 'raw_list'
84         verifier_repo_dir = 'test_verifier_repo_dir'
85         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
86                         'ft_utils.execute_command') as mock_exec:
87             cmd = ("cd {0};"
88                    "testr list-tests {1} > {2};"
89                    "cd -;".format(verifier_repo_dir, testr_mode,
90                                   conf_utils.TEMPEST_RAW_LIST))
91             self.tempestcommon.generate_test_list('test_verifier_repo_dir')
92             mock_exec.assert_any_call(cmd)
93
94     def test_gen_tl_smoke_mode(self):
95         self._test_gen_tl_mode_default('smoke')
96
97     def test_gen_tl_full_mode(self):
98         self._test_gen_tl_mode_default('full')
99
100     def test_verif_res_missing_verif_id(self):
101         self.tempestcommon.verification_id = None
102         with self.assertRaises(Exception):
103             self.tempestcommon.parse_verifier_result()
104
105     def test_apply_missing_blacklist(self):
106         with mock.patch('__builtin__.open', mock.mock_open()) as mock_open, \
107             mock.patch.object(self.tempestcommon, 'read_file',
108                               return_value=['test1', 'test2']):
109             conf_utils.TEMPEST_BLACKLIST = Exception
110             os.environ['INSTALLER_TYPE'] = 'installer_type'
111             os.environ['DEPLOY_SCENARIO'] = 'deploy_scenario'
112             self.tempestcommon.apply_tempest_blacklist()
113             obj = mock_open()
114             obj.write.assert_any_call('test1\n')
115             obj.write.assert_any_call('test2\n')
116
117     def test_apply_blacklist_default(self):
118         item_dict = {'scenarios': ['deploy_scenario'],
119                      'installers': ['installer_type'],
120                      'tests': ['test2']}
121         with mock.patch('__builtin__.open', mock.mock_open()) as mock_open, \
122             mock.patch.object(self.tempestcommon, 'read_file',
123                               return_value=['test1', 'test2']), \
124             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
125                        'yaml.safe_load', return_value=item_dict):
126             os.environ['INSTALLER_TYPE'] = 'installer_type'
127             os.environ['DEPLOY_SCENARIO'] = 'deploy_scenario'
128             self.tempestcommon.apply_tempest_blacklist()
129             obj = mock_open()
130             obj.write.assert_any_call('test1\n')
131             self.assertFalse(obj.write.assert_any_call('test2\n'))
132
133     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.info')
134     def test_run_verifier_tests_default(self, mock_logger_info):
135         with mock.patch('__builtin__.open', mock.mock_open()), \
136             mock.patch('__builtin__.iter', return_value=[r'\} tempest\.']), \
137             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
138                        'subprocess.Popen'):
139             conf_utils.TEMPEST_LIST = 'test_tempest_list'
140             cmd = ["rally", "verify", "start", "--load-list",
141                    conf_utils.TEMPEST_LIST]
142             with self.assertRaises(Exception):
143                 self.tempestcommon.run_verifier_tests()
144                 mock_logger_info. \
145                     assert_any_call("Starting Tempest test suite: '%s'.", cmd)
146
147     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
148                 'subprocess.Popen')
149     def test_generate_report(self, mock_popen):
150         self.tempestcommon.verification_id = "1234"
151         html_file = os.path.join(conf_utils.TEMPEST_RESULTS_DIR,
152                                  "tempest-report.html")
153         cmd = ["rally", "verify", "report", "--type", "html", "--uuid",
154                "1234", "--to", html_file]
155         self.tempestcommon.generate_report()
156         mock_popen.assert_called_once_with(cmd, stdout=mock.ANY,
157                                            stderr=mock.ANY)
158
159     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
160                 'os.path.exists', return_value=False)
161     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs',
162                 side_effect=Exception)
163     def test_run_makedirs_ko(self, *args):
164         # pylint: disable=unused-argument
165         self.assertEqual(self.tempestcommon.run(),
166                          testcase.TestCase.EX_RUN_ERROR)
167
168     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
169                 'os.path.exists', return_value=False)
170     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
171     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
172                 'TempestResourcesManager.create', side_effect=Exception)
173     def test_run_create_resources_ko(self, *args):
174         # pylint: disable=unused-argument
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                 'TempestResourcesManager.create', return_value={})
183     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
184                 'get_active_compute_cnt', side_effect=Exception)
185     def test_run_get_active_comp_cnt_ko(self, *args):
186         # pylint: disable=unused-argument
187         self.assertEqual(self.tempestcommon.run(),
188                          testcase.TestCase.EX_RUN_ERROR)
189
190     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
191                 'os.path.exists', return_value=False)
192     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
193     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
194                 'TempestResourcesManager.create', return_value={})
195     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
196                 'get_active_compute_cnt', return_value=2)
197     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
198                 'conf_utils.configure_tempest', side_effect=Exception)
199     def test_run_configure_tempest_ko(self, *args):
200         # pylint: disable=unused-argument
201         self.assertEqual(self.tempestcommon.run(),
202                          testcase.TestCase.EX_RUN_ERROR)
203
204     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
205                 'os.path.exists', return_value=False)
206     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
207     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
208                 'TempestResourcesManager.create', return_value={})
209     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
210                 'get_active_compute_cnt', return_value=2)
211     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
212                 'conf_utils.configure_tempest')
213     def _test_run(self, status, *args):
214         # pylint: disable=unused-argument
215         self.assertEqual(self.tempestcommon.run(), status)
216
217     def test_run_missing_gen_test_list(self):
218         with mock.patch.object(self.tempestcommon, 'generate_test_list',
219                                side_effect=Exception):
220             self._test_run(testcase.TestCase.EX_RUN_ERROR)
221
222     def test_run_apply_blacklist_ko(self):
223         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
224                     mock.patch.object(self.tempestcommon,
225                                       'apply_tempest_blacklist',
226                                       side_effect=Exception()):
227             self._test_run(testcase.TestCase.EX_RUN_ERROR)
228
229     def test_run_verifier_tests_ko(self):
230         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
231                 mock.patch.object(self.tempestcommon,
232                                   'apply_tempest_blacklist'), \
233                 mock.patch.object(self.tempestcommon, 'run_verifier_tests',
234                                   side_effect=Exception()), \
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_verif_result_ko(self):
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                                   side_effect=Exception):
246             self._test_run(testcase.TestCase.EX_RUN_ERROR)
247
248     def test_run(self):
249         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
250                 mock.patch.object(self.tempestcommon,
251                                   'apply_tempest_blacklist'), \
252                 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
253                 mock.patch.object(self.tempestcommon,
254                                   'parse_verifier_result'), \
255                 mock.patch.object(self.tempestcommon, 'generate_report'):
256             self._test_run(testcase.TestCase.EX_OK)
257
258
259 if __name__ == "__main__":
260     logging.disable(logging.CRITICAL)
261     unittest.main(verbosity=2)