fc8c9cc85d5e4506d396359e109717c838982444
[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 snaps.openstack.os_credentials import OSCreds
16 from xtesting.core import testcase
17
18 from functest.opnfv_tests.openstack.tempest import tempest
19 from functest.opnfv_tests.openstack.tempest import conf_utils
20
21
22 class OSTempestTesting(unittest.TestCase):
23     # pylint: disable=too-many-public-methods
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             self.tempestneutrontrunk = tempest.TempestNeutronTrunk()
52
53     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.error')
54     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.debug')
55     def test_gen_tl_cm_missing_file(self, mock_logger_debug,
56                                     mock_logger_error):
57         # pylint: disable=unused-argument
58         self.tempestcommon.mode = 'custom'
59         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
60                         'os.path.isfile', return_value=False), \
61                 self.assertRaises(Exception) as context:
62             msg = "Tempest test list file %s NOT found."
63             self.tempestcommon.generate_test_list()
64             self.assertTrue(
65                 (msg % conf_utils.TEMPEST_CUSTOM) in context.exception)
66
67     def test_gen_tl_cm_default(self):
68         self.tempestcommon.mode = 'custom'
69         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
70                         'shutil.copyfile') as mock_copyfile, \
71             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
72                        'os.path.isfile', return_value=True):
73             self.tempestcommon.generate_test_list()
74             self.assertTrue(mock_copyfile.called)
75
76     @mock.patch('subprocess.check_output')
77     def _test_gen_tl_mode_default(self, mode, mock_exec=None):
78         self.tempestcommon.mode = mode
79         if self.tempestcommon.mode == 'smoke':
80             testr_mode = r"'^tempest\.(api|scenario).*\[.*\bsmoke\b.*\]$'"
81         elif self.tempestcommon.mode == 'full':
82             testr_mode = r"'^tempest\.'"
83         else:
84             testr_mode = self.tempestcommon.mode
85         verifier_repo_dir = 'test_verifier_repo_dir'
86         cmd = "(cd {0}; stestr list {1} >{2} 2>/dev/null)".format(
87             verifier_repo_dir, testr_mode, self.tempestcommon.list)
88         self.tempestcommon.generate_test_list()
89         mock_exec.assert_called_once_with(cmd, shell=True)
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_gen_tl_neutron_trunk_mode(self):
98         self._test_gen_tl_mode_default('neutron_trunk')
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_backup_config_default(self):
106         with mock.patch('os.path.exists', return_value=False), \
107                 mock.patch('os.makedirs') as mock_makedirs, \
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_makedirs.called)
112             self.assertTrue(mock_copyfile.called)
113
114         with mock.patch('os.path.exists', return_value=True), \
115                 mock.patch('shutil.copyfile') as mock_copyfile:
116             self.tempestcommon.backup_tempest_config(
117                 'test_conf_file', res_dir='test_dir')
118             self.assertTrue(mock_copyfile.called)
119
120     @mock.patch("os.rename")
121     @mock.patch("os.remove")
122     @mock.patch("os.path.exists", return_value=True)
123     def test_apply_missing_blacklist(self, *args):
124         with mock.patch('__builtin__.open', mock.mock_open()) as mock_open, \
125             mock.patch.object(self.tempestcommon, 'read_file',
126                               return_value=['test1', 'test2']):
127             conf_utils.TEMPEST_BLACKLIST = Exception
128             os.environ['INSTALLER_TYPE'] = 'installer_type'
129             os.environ['DEPLOY_SCENARIO'] = 'deploy_scenario'
130             self.tempestcommon.apply_tempest_blacklist()
131             obj = mock_open()
132             obj.write.assert_any_call('test1\n')
133             obj.write.assert_any_call('test2\n')
134             args[0].assert_called_once_with(self.tempestcommon.raw_list)
135             args[1].assert_called_once_with(self.tempestcommon.raw_list)
136             args[2].assert_called_once_with(
137                 self.tempestcommon.list, self.tempestcommon.raw_list)
138
139     @mock.patch("os.rename")
140     @mock.patch("os.remove")
141     @mock.patch("os.path.exists", return_value=True)
142     def test_apply_blacklist_default(self, *args):
143         item_dict = {'scenarios': ['deploy_scenario'],
144                      'installers': ['installer_type'],
145                      'tests': ['test2']}
146         with mock.patch('__builtin__.open', 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('__builtin__.open', mock.mock_open()), \
165             mock.patch('__builtin__.iter', return_value=[r'\} tempest\.']), \
166             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
167                        'subprocess.Popen'):
168             conf_utils.TEMPEST_LIST = 'test_tempest_list'
169             cmd = ["rally", "verify", "start", "--load-list",
170                    conf_utils.TEMPEST_LIST]
171             with self.assertRaises(Exception):
172                 self.tempestcommon.run_verifier_tests()
173                 mock_logger_info. \
174                     assert_any_call("Starting Tempest test suite: '%s'.", cmd)
175
176     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
177                 'subprocess.Popen')
178     def test_generate_report(self, mock_popen):
179         self.tempestcommon.verification_id = "1234"
180         html_file = os.path.join(tempest.TempestCommon.TEMPEST_RESULTS_DIR,
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     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
201                 'TempestResourcesManager.create', side_effect=Exception)
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.snaps.snaps_utils.'
213                 'get_active_compute_cnt', side_effect=Exception)
214     def test_run_get_active_comp_cnt_ko(self, *args):
215         # pylint: disable=unused-argument
216         self.assertEqual(self.tempestcommon.run(),
217                          testcase.TestCase.EX_RUN_ERROR)
218
219     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
220                 'os.path.exists', return_value=False)
221     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
222     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
223                 'TempestResourcesManager.create', return_value={})
224     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
225                 'get_active_compute_cnt', return_value=2)
226     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
227                 'TempestCommon.configure', side_effect=Exception)
228     def test_run_configure_tempest_ko(self, *args):
229         # pylint: disable=unused-argument
230         self.assertEqual(self.tempestcommon.run(),
231                          testcase.TestCase.EX_RUN_ERROR)
232
233     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
234                 'os.path.exists', return_value=False)
235     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
236     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
237                 'TempestResourcesManager.create', return_value={})
238     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
239                 'get_active_compute_cnt', return_value=2)
240     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
241                 'TempestCommon.configure')
242     def _test_run(self, status, *args):
243         # pylint: disable=unused-argument
244         self.assertEqual(self.tempestcommon.run(), status)
245
246     def test_run_missing_gen_test_list(self):
247         with mock.patch.object(self.tempestcommon, 'generate_test_list',
248                                side_effect=Exception):
249             self._test_run(testcase.TestCase.EX_RUN_ERROR)
250
251     def test_run_apply_blacklist_ko(self):
252         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
253                 mock.patch.object(
254                     self.tempestcommon, 'apply_tempest_blacklist',
255                     side_effect=Exception()):
256             self._test_run(testcase.TestCase.EX_RUN_ERROR)
257
258     def test_run_verifier_tests_ko(self):
259         with 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                                   side_effect=Exception()), \
264                 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
265                                   side_effect=Exception):
266             self._test_run(testcase.TestCase.EX_RUN_ERROR)
267
268     def test_run_verif_result_ko(self):
269         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
270                 mock.patch.object(self.tempestcommon,
271                                   'apply_tempest_blacklist'), \
272                 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
273                 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
274                                   side_effect=Exception):
275             self._test_run(testcase.TestCase.EX_RUN_ERROR)
276
277     def test_run(self):
278         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
279                 mock.patch.object(self.tempestcommon,
280                                   'apply_tempest_blacklist'), \
281                 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
282                 mock.patch.object(self.tempestcommon,
283                                   'parse_verifier_result'), \
284                 mock.patch.object(self.tempestcommon, 'generate_report'):
285             self._test_run(testcase.TestCase.EX_OK)
286
287
288 if __name__ == "__main__":
289     logging.disable(logging.CRITICAL)
290     unittest.main(verbosity=2)