Improve tempest files management
[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
24     def setUp(self):
25         os_creds = OSCreds(
26             username='user', password='pass',
27             auth_url='http://foo.com:5000/v3', project_name='bar')
28
29         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
30                         'conf_utils.get_verifier_id',
31                         return_value='test_deploy_id'), \
32             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
33                        'conf_utils.get_verifier_deployment_id',
34                        return_value='test_deploy_id'), \
35             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
36                        'conf_utils.get_verifier_repo_dir',
37                        return_value='test_verifier_repo_dir'), \
38             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
39                        'conf_utils.get_verifier_deployment_dir',
40                        return_value='test_verifier_deploy_dir'), \
41             mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
42                        'get_credentials',
43                        return_value=os_creds):
44             self.tempestcommon = tempest.TempestCommon()
45             self.tempestsmoke_serial = tempest.TempestSmokeSerial()
46             self.tempestsmoke_parallel = tempest.TempestSmokeParallel()
47             self.tempestfull_parallel = tempest.TempestFullParallel()
48             self.tempestcustom = tempest.TempestCustom()
49             self.tempestdefcore = tempest.TempestDefcore()
50
51     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.error')
52     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.debug')
53     def test_gen_tl_cm_missing_file(self, mock_logger_debug,
54                                     mock_logger_error):
55         # pylint: disable=unused-argument
56         self.tempestcommon.mode = 'custom'
57         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
58                         'os.path.isfile', return_value=False), \
59                 self.assertRaises(Exception) as context:
60             msg = "Tempest test list file %s NOT found."
61             self.tempestcommon.generate_test_list('test_verifier_repo_dir')
62             self.assertTrue(
63                 (msg % conf_utils.TEMPEST_CUSTOM) in context.exception)
64
65     def test_gen_tl_cm_default(self):
66         self.tempestcommon.mode = 'custom'
67         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
68                         'shutil.copyfile') as mock_copyfile, \
69             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
70                        'os.path.isfile', return_value=True):
71             self.tempestcommon.generate_test_list('test_verifier_repo_dir')
72             self.assertTrue(mock_copyfile.called)
73
74     @mock.patch('functest.utils.functest_utils.execute_command')
75     def _test_gen_tl_mode_default(self, mode, mock_exec=None):
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         verifier_repo_dir = 'test_verifier_repo_dir'
84         cmd = ("cd {0};"
85                "testr list-tests {1} > {2};"
86                "cd -;".format(verifier_repo_dir, testr_mode,
87                               self.tempestcommon.list))
88         self.tempestcommon.generate_test_list('test_verifier_repo_dir')
89         mock_exec.assert_called_once_with(cmd)
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     @mock.patch("os.rename")
103     @mock.patch("os.remove")
104     @mock.patch("os.path.exists", return_value=True)
105     def test_apply_missing_blacklist(self, *args):
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             args[0].assert_called_once_with(self.tempestcommon.raw_list)
117             args[1].assert_called_once_with(self.tempestcommon.raw_list)
118             args[2].assert_called_once_with(
119                 self.tempestcommon.list, self.tempestcommon.raw_list)
120
121     @mock.patch("os.rename")
122     @mock.patch("os.remove")
123     @mock.patch("os.path.exists", return_value=True)
124     def test_apply_blacklist_default(self, *args):
125         item_dict = {'scenarios': ['deploy_scenario'],
126                      'installers': ['installer_type'],
127                      'tests': ['test2']}
128         with mock.patch('__builtin__.open', mock.mock_open()) as mock_open, \
129             mock.patch.object(self.tempestcommon, 'read_file',
130                               return_value=['test1', 'test2']), \
131             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
132                        'yaml.safe_load', return_value=item_dict):
133             os.environ['INSTALLER_TYPE'] = 'installer_type'
134             os.environ['DEPLOY_SCENARIO'] = 'deploy_scenario'
135             self.tempestcommon.apply_tempest_blacklist()
136             obj = mock_open()
137             obj.write.assert_any_call('test1\n')
138             self.assertFalse(obj.write.assert_any_call('test2\n'))
139             args[0].assert_called_once_with(self.tempestcommon.raw_list)
140             args[1].assert_called_once_with(self.tempestcommon.raw_list)
141             args[2].assert_called_once_with(
142                 self.tempestcommon.list, self.tempestcommon.raw_list)
143
144     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.info')
145     def test_run_verifier_tests_default(self, mock_logger_info):
146         with mock.patch('__builtin__.open', mock.mock_open()), \
147             mock.patch('__builtin__.iter', return_value=[r'\} tempest\.']), \
148             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
149                        'subprocess.Popen'):
150             conf_utils.TEMPEST_LIST = 'test_tempest_list'
151             cmd = ["rally", "verify", "start", "--load-list",
152                    conf_utils.TEMPEST_LIST]
153             with self.assertRaises(Exception):
154                 self.tempestcommon.run_verifier_tests()
155                 mock_logger_info. \
156                     assert_any_call("Starting Tempest test suite: '%s'.", cmd)
157
158     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
159                 'subprocess.Popen')
160     def test_generate_report(self, mock_popen):
161         self.tempestcommon.verification_id = "1234"
162         html_file = os.path.join(tempest.TempestCommon.TEMPEST_RESULTS_DIR,
163                                  "tempest-report.html")
164         cmd = ["rally", "verify", "report", "--type", "html", "--uuid",
165                "1234", "--to", html_file]
166         self.tempestcommon.generate_report()
167         mock_popen.assert_called_once_with(cmd, stdout=mock.ANY,
168                                            stderr=mock.ANY)
169
170     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
171                 'os.path.exists', return_value=False)
172     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs',
173                 side_effect=Exception)
174     def test_run_makedirs_ko(self, *args):
175         # pylint: disable=unused-argument
176         self.assertEqual(self.tempestcommon.run(),
177                          testcase.TestCase.EX_RUN_ERROR)
178
179     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
180                 'os.path.exists', return_value=False)
181     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
182     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
183                 'TempestResourcesManager.create', side_effect=Exception)
184     def test_run_create_resources_ko(self, *args):
185         # pylint: disable=unused-argument
186         self.assertEqual(self.tempestcommon.run(),
187                          testcase.TestCase.EX_RUN_ERROR)
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     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
193                 'TempestResourcesManager.create', return_value={})
194     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
195                 'get_active_compute_cnt', side_effect=Exception)
196     def test_run_get_active_comp_cnt_ko(self, *args):
197         # pylint: disable=unused-argument
198         self.assertEqual(self.tempestcommon.run(),
199                          testcase.TestCase.EX_RUN_ERROR)
200
201     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
202                 'os.path.exists', return_value=False)
203     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
204     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
205                 'TempestResourcesManager.create', return_value={})
206     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
207                 'get_active_compute_cnt', return_value=2)
208     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
209                 'conf_utils.configure_tempest', 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                 'TempestResourcesManager.create', return_value={})
220     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
221                 'get_active_compute_cnt', return_value=2)
222     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
223                 'conf_utils.configure_tempest')
224     def _test_run(self, status, *args):
225         # pylint: disable=unused-argument
226         self.assertEqual(self.tempestcommon.run(), status)
227
228     def test_run_missing_gen_test_list(self):
229         with mock.patch.object(self.tempestcommon, 'generate_test_list',
230                                side_effect=Exception):
231             self._test_run(testcase.TestCase.EX_RUN_ERROR)
232
233     def test_run_apply_blacklist_ko(self):
234         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
235                 mock.patch.object(
236                     self.tempestcommon, 'apply_tempest_blacklist',
237                     side_effect=Exception()):
238             self._test_run(testcase.TestCase.EX_RUN_ERROR)
239
240     def test_run_verifier_tests_ko(self):
241         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
242                 mock.patch.object(self.tempestcommon,
243                                   'apply_tempest_blacklist'), \
244                 mock.patch.object(self.tempestcommon, 'run_verifier_tests',
245                                   side_effect=Exception()), \
246                 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
247                                   side_effect=Exception):
248             self._test_run(testcase.TestCase.EX_RUN_ERROR)
249
250     def test_run_verif_result_ko(self):
251         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
252                 mock.patch.object(self.tempestcommon,
253                                   'apply_tempest_blacklist'), \
254                 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
255                 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
256                                   side_effect=Exception):
257             self._test_run(testcase.TestCase.EX_RUN_ERROR)
258
259     def test_run(self):
260         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
261                 mock.patch.object(self.tempestcommon,
262                                   'apply_tempest_blacklist'), \
263                 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
264                 mock.patch.object(self.tempestcommon,
265                                   'parse_verifier_result'), \
266                 mock.patch.object(self.tempestcommon, 'generate_report'):
267             self._test_run(testcase.TestCase.EX_OK)
268
269
270 if __name__ == "__main__":
271     logging.disable(logging.CRITICAL)
272     unittest.main(verbosity=2)