9116100cd217cad453167470015d73ae561f799e
[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     def test_gen_tl_cm_default(self):
61         self.tempestcommon.mode = 'custom'
62         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
63                         'shutil.copyfile') as mock_copyfile, \
64             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
65                        'os.path.isfile', return_value=True):
66             self.tempestcommon.generate_test_list()
67             self.assertTrue(mock_copyfile.called)
68
69     @mock.patch('subprocess.check_output')
70     def _test_gen_tl_mode_default(self, mode, mock_exec=None):
71         self.tempestcommon.mode = mode
72         if self.tempestcommon.mode == 'smoke':
73             testr_mode = r"'^tempest\.(api|scenario).*\[.*\bsmoke\b.*\]$'"
74         elif self.tempestcommon.mode == 'full':
75             testr_mode = r"'^tempest\.'"
76         else:
77             testr_mode = self.tempestcommon.mode
78         verifier_repo_dir = 'test_verifier_repo_dir'
79         cmd = "(cd {0}; stestr list {1} >{2} 2>/dev/null)".format(
80             verifier_repo_dir, testr_mode, self.tempestcommon.list)
81         self.tempestcommon.generate_test_list()
82         mock_exec.assert_called_once_with(cmd, shell=True)
83
84     def test_gen_tl_smoke_mode(self):
85         self._test_gen_tl_mode_default('smoke')
86
87     def test_gen_tl_full_mode(self):
88         self._test_gen_tl_mode_default('full')
89
90     def test_gen_tl_neutron_trunk_mode(self):
91         self._test_gen_tl_mode_default('neutron_trunk')
92
93     def test_verif_res_missing_verif_id(self):
94         self.tempestcommon.verification_id = None
95         with self.assertRaises(Exception):
96             self.tempestcommon.parse_verifier_result()
97
98     def test_backup_config_default(self):
99         with mock.patch('os.path.exists', return_value=False), \
100                 mock.patch('os.makedirs') as mock_makedirs, \
101                 mock.patch('shutil.copyfile') as mock_copyfile:
102             self.tempestcommon.backup_tempest_config(
103                 'test_conf_file', res_dir='test_dir')
104             self.assertTrue(mock_makedirs.called)
105             self.assertTrue(mock_copyfile.called)
106
107         with mock.patch('os.path.exists', return_value=True), \
108                 mock.patch('shutil.copyfile') as mock_copyfile:
109             self.tempestcommon.backup_tempest_config(
110                 'test_conf_file', res_dir='test_dir')
111             self.assertTrue(mock_copyfile.called)
112
113     @mock.patch("os.rename")
114     @mock.patch("os.remove")
115     @mock.patch("os.path.exists", return_value=True)
116     def test_apply_missing_blacklist(self, *args):
117         with mock.patch('six.moves.builtins.open',
118                         mock.mock_open()) as mock_open, \
119             mock.patch.object(self.tempestcommon, 'read_file',
120                               return_value=['test1', 'test2']):
121             conf_utils.TEMPEST_BLACKLIST = Exception
122             os.environ['INSTALLER_TYPE'] = 'installer_type'
123             os.environ['DEPLOY_SCENARIO'] = 'deploy_scenario'
124             self.tempestcommon.apply_tempest_blacklist()
125             obj = mock_open()
126             obj.write.assert_any_call('test1\n')
127             obj.write.assert_any_call('test2\n')
128             args[0].assert_called_once_with(self.tempestcommon.raw_list)
129             args[1].assert_called_once_with(self.tempestcommon.raw_list)
130             args[2].assert_called_once_with(
131                 self.tempestcommon.list, self.tempestcommon.raw_list)
132
133     @mock.patch("os.rename")
134     @mock.patch("os.remove")
135     @mock.patch("os.path.exists", return_value=True)
136     def test_apply_blacklist_default(self, *args):
137         item_dict = {'scenarios': ['deploy_scenario'],
138                      'installers': ['installer_type'],
139                      'tests': ['test2']}
140         with mock.patch('six.moves.builtins.open',
141                         mock.mock_open()) as mock_open, \
142             mock.patch.object(self.tempestcommon, 'read_file',
143                               return_value=['test1', 'test2']), \
144             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
145                        'yaml.safe_load', return_value=item_dict):
146             os.environ['INSTALLER_TYPE'] = 'installer_type'
147             os.environ['DEPLOY_SCENARIO'] = 'deploy_scenario'
148             self.tempestcommon.apply_tempest_blacklist()
149             obj = mock_open()
150             obj.write.assert_any_call('test1\n')
151             self.assertFalse(obj.write.assert_any_call('test2\n'))
152             args[0].assert_called_once_with(self.tempestcommon.raw_list)
153             args[1].assert_called_once_with(self.tempestcommon.raw_list)
154             args[2].assert_called_once_with(
155                 self.tempestcommon.list, self.tempestcommon.raw_list)
156
157     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.info')
158     def test_run_verifier_tests_default(self, mock_logger_info):
159         with mock.patch('six.moves.builtins.open', mock.mock_open()), \
160             mock.patch('six.moves.builtins.iter',
161                        return_value=[r'\} tempest\.']), \
162             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
163                        'subprocess.Popen'):
164             conf_utils.TEMPEST_LIST = 'test_tempest_list'
165             cmd = ["rally", "verify", "start", "--load-list",
166                    conf_utils.TEMPEST_LIST]
167             with self.assertRaises(Exception):
168                 self.tempestcommon.run_verifier_tests()
169                 mock_logger_info. \
170                     assert_any_call("Starting Tempest test suite: '%s'.", cmd)
171
172     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
173                 'subprocess.Popen')
174     def test_generate_report(self, mock_popen):
175         self.tempestcommon.verification_id = "1234"
176         html_file = os.path.join(tempest.TempestCommon.TEMPEST_RESULTS_DIR,
177                                  "tempest-report.html")
178         cmd = ["rally", "verify", "report", "--type", "html", "--uuid",
179                "1234", "--to", html_file]
180         self.tempestcommon.generate_report()
181         mock_popen.assert_called_once_with(cmd, stdout=mock.ANY,
182                                            stderr=mock.ANY)
183
184     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
185                 'os.path.exists', return_value=False)
186     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs',
187                 side_effect=Exception)
188     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
189                 'TempestResourcesManager.cleanup')
190     def test_run_makedirs_ko(self, *args):
191         # pylint: disable=unused-argument
192         self.assertEqual(self.tempestcommon.run(),
193                          testcase.TestCase.EX_RUN_ERROR)
194
195     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
196                 'os.path.exists', return_value=False)
197     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
198     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
199                 'TempestResourcesManager.create', side_effect=Exception)
200     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
201                 'TempestResourcesManager.cleanup')
202     def test_run_create_resources_ko(self, *args):
203         # pylint: disable=unused-argument
204         self.assertEqual(self.tempestcommon.run(),
205                          testcase.TestCase.EX_RUN_ERROR)
206
207     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
208                 'os.path.exists', return_value=False)
209     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
210     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
211                 'TempestResourcesManager.create', return_value={})
212     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
213                 'TempestResourcesManager.cleanup')
214     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
215                 'TempestCommon.configure', side_effect=Exception)
216     def test_run_configure_tempest_ko(self, *args):
217         # pylint: disable=unused-argument
218         self.assertEqual(self.tempestcommon.run(),
219                          testcase.TestCase.EX_RUN_ERROR)
220
221     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
222                 'os.path.exists', return_value=False)
223     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
224     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
225                 'TempestResourcesManager.create', return_value={})
226     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
227                 'TempestResourcesManager.cleanup')
228     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
229                 'TempestCommon.configure')
230     def _test_run(self, status, *args):
231         # pylint: disable=unused-argument
232         self.assertEqual(self.tempestcommon.run(), status)
233
234     def test_run_missing_gen_test_list(self):
235         with mock.patch.object(self.tempestcommon, 'generate_test_list',
236                                side_effect=Exception):
237             self._test_run(testcase.TestCase.EX_RUN_ERROR)
238
239     def test_run_apply_blacklist_ko(self):
240         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
241                 mock.patch.object(
242                     self.tempestcommon, 'apply_tempest_blacklist',
243                     side_effect=Exception()):
244             self._test_run(testcase.TestCase.EX_RUN_ERROR)
245
246     def test_run_verifier_tests_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                                   side_effect=Exception()), \
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_verif_result_ko(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, 'parse_verifier_result',
262                                   side_effect=Exception):
263             self._test_run(testcase.TestCase.EX_RUN_ERROR)
264
265     def test_run(self):
266         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
267                 mock.patch.object(self.tempestcommon,
268                                   'apply_tempest_blacklist'), \
269                 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
270                 mock.patch.object(self.tempestcommon,
271                                   'parse_verifier_result'), \
272                 mock.patch.object(self.tempestcommon, 'generate_report'):
273             self._test_run(testcase.TestCase.EX_OK)
274
275
276 if __name__ == "__main__":
277     logging.disable(logging.CRITICAL)
278     unittest.main(verbosity=2)