Merge "Add concurrency parameter to refstack_defcore tests"
[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 from xtesting.core import testcase
16
17 from functest.opnfv_tests.openstack.tempest import tempest
18 from functest.opnfv_tests.openstack.tempest import conf_utils
19 from functest.utils import config
20
21
22 class OSTempestTesting(unittest.TestCase):
23     # pylint: disable=too-many-public-methods
24
25     def setUp(self):
26         with mock.patch('os_client_config.get_config'), \
27                 mock.patch('shade.OpenStackCloud'), \
28                 mock.patch('functest.core.tenantnetwork.NewProject'), \
29                 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
30                            'conf_utils.create_rally_deployment'), \
31                 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
32                            'conf_utils.create_verifier'), \
33                 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
34                            'conf_utils.get_verifier_id',
35                            return_value='test_deploy_id'), \
36                 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
37                            'conf_utils.get_verifier_deployment_id',
38                            return_value='test_deploy_id'), \
39                 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
40                            'conf_utils.get_verifier_repo_dir',
41                            return_value='test_verifier_repo_dir'), \
42                 mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
43                            'conf_utils.get_verifier_deployment_dir',
44                            return_value='test_verifier_deploy_dir'), \
45                 mock.patch('os_client_config.make_shade'):
46             self.tempestcommon = tempest.TempestCommon()
47
48     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.error')
49     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.debug')
50     def test_gen_tl_cm_missing_file(self, mock_logger_debug,
51                                     mock_logger_error):
52         # pylint: disable=unused-argument
53         self.tempestcommon.mode = 'custom'
54         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
55                         'os.path.isfile', return_value=False), \
56                 self.assertRaises(Exception) as context:
57             msg = "Tempest test list file %s NOT found."
58             self.tempestcommon.generate_test_list()
59             self.assertTrue(
60                 (msg % conf_utils.TEMPEST_CUSTOM) in context.exception)
61
62     @mock.patch('subprocess.check_output')
63     @mock.patch('os.remove')
64     def test_gen_tl_cm_default(self, *args):
65         self.tempestcommon.mode = 'custom'
66         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
67                         'shutil.copyfile') as mock_copyfile, \
68             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
69                        'os.path.isfile', return_value=True):
70             self.tempestcommon.generate_test_list()
71             self.assertTrue(mock_copyfile.called)
72         args[0].assert_called_once_with('/etc/tempest.conf')
73
74     @mock.patch('os.remove')
75     @mock.patch('shutil.copyfile')
76     @mock.patch('subprocess.check_output')
77     def _test_gen_tl_mode_default(self, mode, *args):
78         if mode == 'smoke':
79             testr_mode = r'^tempest\.(api|scenario).*\[.*\bsmoke\b.*\]$'
80         elif mode == 'full':
81             testr_mode = r'^tempest\.'
82         else:
83             testr_mode = self.tempestcommon.mode
84         verifier_repo_dir = 'test_verifier_repo_dir'
85         cmd = "(cd {0}; stestr list '{1}' >{2} 2>/dev/null)".format(
86             verifier_repo_dir, testr_mode, self.tempestcommon.list)
87         self.tempestcommon.generate_test_list(mode=testr_mode)
88         args[0].assert_called_once_with(cmd, shell=True)
89         args[2].assert_called_once_with('/etc/tempest.conf')
90
91     def test_gen_tl_smoke_mode(self):
92         self._test_gen_tl_mode_default('smoke')
93
94     def test_gen_tl_full_mode(self):
95         self._test_gen_tl_mode_default('full')
96
97     def test_verif_res_missing_verif_id(self):
98         self.tempestcommon.verification_id = None
99         with self.assertRaises(Exception):
100             self.tempestcommon.parse_verifier_result()
101
102     def test_backup_config_default(self):
103         with mock.patch('os.path.exists', return_value=False), \
104                 mock.patch('os.makedirs') as mock_makedirs, \
105                 mock.patch('shutil.copyfile') as mock_copyfile:
106             self.tempestcommon.backup_tempest_config(
107                 'test_conf_file', res_dir='test_dir')
108             self.assertTrue(mock_makedirs.called)
109             self.assertTrue(mock_copyfile.called)
110
111         with mock.patch('os.path.exists', return_value=True), \
112                 mock.patch('shutil.copyfile') as mock_copyfile:
113             self.tempestcommon.backup_tempest_config(
114                 'test_conf_file', res_dir='test_dir')
115             self.assertTrue(mock_copyfile.called)
116
117     @mock.patch("os.rename")
118     @mock.patch("os.remove")
119     @mock.patch("os.path.exists", return_value=True)
120     def test_apply_missing_blacklist(self, *args):
121         with mock.patch('six.moves.builtins.open',
122                         mock.mock_open()) as mock_open, \
123             mock.patch.object(self.tempestcommon, 'read_file',
124                               return_value=['test1', 'test2']):
125             conf_utils.TEMPEST_BLACKLIST = Exception
126             os.environ['DEPLOY_SCENARIO'] = 'deploy_scenario'
127             self.tempestcommon.apply_tempest_blacklist()
128             obj = mock_open()
129             obj.write.assert_any_call('test1\n')
130             obj.write.assert_any_call('test2\n')
131             args[0].assert_called_once_with(self.tempestcommon.raw_list)
132             args[1].assert_called_once_with(self.tempestcommon.raw_list)
133             args[2].assert_called_once_with(
134                 self.tempestcommon.list, self.tempestcommon.raw_list)
135
136     @mock.patch("os.rename")
137     @mock.patch("os.remove")
138     @mock.patch("os.path.exists", return_value=True)
139     def test_apply_blacklist_default(self, *args):
140         item_dict = {'scenarios': ['deploy_scenario'],
141                      'tests': ['test2']}
142         with mock.patch('six.moves.builtins.open',
143                         mock.mock_open()) as mock_open, \
144             mock.patch.object(self.tempestcommon, 'read_file',
145                               return_value=['test1', 'test2']), \
146             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
147                        'yaml.safe_load', return_value=item_dict):
148             os.environ['DEPLOY_SCENARIO'] = 'deploy_scenario'
149             self.tempestcommon.apply_tempest_blacklist()
150             obj = mock_open()
151             obj.write.assert_any_call('test1\n')
152             self.assertFalse(obj.write.assert_any_call('test2\n'))
153             args[0].assert_called_once_with(self.tempestcommon.raw_list)
154             args[1].assert_called_once_with(self.tempestcommon.raw_list)
155             args[2].assert_called_once_with(
156                 self.tempestcommon.list, self.tempestcommon.raw_list)
157
158     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.info')
159     def test_run_verifier_tests_default(self, mock_logger_info):
160         with mock.patch('six.moves.builtins.open', mock.mock_open()), \
161             mock.patch('six.moves.builtins.iter',
162                        return_value=[r'\} tempest\.']), \
163             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
164                        'subprocess.Popen'):
165             conf_utils.TEMPEST_LIST = 'test_tempest_list'
166             cmd = ["rally", "verify", "start", "--load-list",
167                    conf_utils.TEMPEST_LIST]
168             with self.assertRaises(Exception):
169                 self.tempestcommon.run_verifier_tests()
170                 mock_logger_info. \
171                     assert_any_call("Starting Tempest test suite: '%s'.", cmd)
172
173     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
174                 'subprocess.Popen')
175     def test_generate_report(self, mock_popen):
176         self.tempestcommon.verification_id = "1234"
177         html_file = os.path.join(
178             os.path.join(
179                 getattr(config.CONF, 'dir_results'),
180                 self.tempestcommon.case_name),
181             "tempest-report.html")
182         cmd = ["rally", "verify", "report", "--type", "html", "--uuid",
183                "1234", "--to", html_file]
184         self.tempestcommon.generate_report()
185         mock_popen.assert_called_once_with(cmd, stdout=mock.ANY,
186                                            stderr=mock.ANY)
187
188     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
189                 'os.path.exists', return_value=False)
190     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs',
191                 side_effect=Exception)
192     def test_run_makedirs_ko(self, *args):
193         # pylint: disable=unused-argument
194         self.assertEqual(self.tempestcommon.run(),
195                          testcase.TestCase.EX_RUN_ERROR)
196
197     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
198                 'os.path.exists', return_value=False)
199     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
200     def test_run_create_resources_ko(self, *args):
201         # pylint: disable=unused-argument
202         self.assertEqual(self.tempestcommon.run(),
203                          testcase.TestCase.EX_RUN_ERROR)
204
205     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
206                 'os.path.exists', return_value=False)
207     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
208     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
209                 'TempestCommon.configure', side_effect=Exception)
210     def test_run_configure_tempest_ko(self, *args):
211         # pylint: disable=unused-argument
212         self.assertEqual(self.tempestcommon.run(),
213                          testcase.TestCase.EX_RUN_ERROR)
214
215     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
216                 'os.path.exists', return_value=False)
217     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
218     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
219                 'TempestCommon.configure')
220     def _test_run(self, status, *args):
221         # pylint: disable=unused-argument
222         self.assertEqual(self.tempestcommon.run(), status)
223
224     def test_run_missing_gen_test_list(self):
225         with mock.patch.object(self.tempestcommon, 'generate_test_list',
226                                side_effect=Exception):
227             self._test_run(testcase.TestCase.EX_RUN_ERROR)
228
229     def test_run_apply_blacklist_ko(self):
230         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
231                 mock.patch.object(
232                     self.tempestcommon, 'apply_tempest_blacklist',
233                     side_effect=Exception()):
234             self._test_run(testcase.TestCase.EX_RUN_ERROR)
235
236     def test_run_verifier_tests_ko(self):
237         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
238                 mock.patch.object(self.tempestcommon,
239                                   'apply_tempest_blacklist'), \
240                 mock.patch.object(self.tempestcommon, 'run_verifier_tests',
241                                   side_effect=Exception()), \
242                 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
243                                   side_effect=Exception):
244             self._test_run(testcase.TestCase.EX_RUN_ERROR)
245
246     def test_run_verif_result_ko(self):
247         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
248                 mock.patch.object(self.tempestcommon,
249                                   'apply_tempest_blacklist'), \
250                 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
251                 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
252                                   side_effect=Exception):
253             self._test_run(testcase.TestCase.EX_RUN_ERROR)
254
255     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.TempestCommon.'
256                 'run', return_value=testcase.TestCase.EX_OK)
257     def test_run(self, *args):
258         with mock.patch.object(self.tempestcommon, 'update_rally_regex'), \
259                 mock.patch.object(self.tempestcommon, 'generate_test_list'), \
260                 mock.patch.object(self.tempestcommon,
261                                   'apply_tempest_blacklist'), \
262                 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
263                 mock.patch.object(self.tempestcommon,
264                                   'parse_verifier_result'), \
265                 mock.patch.object(self.tempestcommon, 'generate_report'):
266             self._test_run(testcase.TestCase.EX_OK)
267             args[0].assert_called_once_with()
268
269
270 if __name__ == "__main__":
271     logging.disable(logging.CRITICAL)
272     unittest.main(verbosity=2)