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