Merge "Leverage on Xtesting"
[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         conf_utils.TEMPEST_RAW_LIST = 'raw_list'
84         verifier_repo_dir = 'test_verifier_repo_dir'
85         cmd = ("cd {0};"
86                "testr list-tests {1} > {2};"
87                "cd -;".format(verifier_repo_dir, testr_mode,
88                               conf_utils.TEMPEST_RAW_LIST))
89         self.tempestcommon.generate_test_list('test_verifier_repo_dir')
90         mock_exec.assert_any_call(cmd)
91
92     def test_gen_tl_smoke_mode(self):
93         self._test_gen_tl_mode_default('smoke')
94
95     def test_gen_tl_full_mode(self):
96         self._test_gen_tl_mode_default('full')
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_apply_missing_blacklist(self):
104         with mock.patch('__builtin__.open', mock.mock_open()) as mock_open, \
105             mock.patch.object(self.tempestcommon, 'read_file',
106                               return_value=['test1', 'test2']):
107             conf_utils.TEMPEST_BLACKLIST = Exception
108             os.environ['INSTALLER_TYPE'] = 'installer_type'
109             os.environ['DEPLOY_SCENARIO'] = 'deploy_scenario'
110             self.tempestcommon.apply_tempest_blacklist()
111             obj = mock_open()
112             obj.write.assert_any_call('test1\n')
113             obj.write.assert_any_call('test2\n')
114
115     def test_apply_blacklist_default(self):
116         item_dict = {'scenarios': ['deploy_scenario'],
117                      'installers': ['installer_type'],
118                      'tests': ['test2']}
119         with mock.patch('__builtin__.open', mock.mock_open()) as mock_open, \
120             mock.patch.object(self.tempestcommon, 'read_file',
121                               return_value=['test1', 'test2']), \
122             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
123                        'yaml.safe_load', return_value=item_dict):
124             os.environ['INSTALLER_TYPE'] = 'installer_type'
125             os.environ['DEPLOY_SCENARIO'] = 'deploy_scenario'
126             self.tempestcommon.apply_tempest_blacklist()
127             obj = mock_open()
128             obj.write.assert_any_call('test1\n')
129             self.assertFalse(obj.write.assert_any_call('test2\n'))
130
131     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.info')
132     def test_run_verifier_tests_default(self, mock_logger_info):
133         with mock.patch('__builtin__.open', mock.mock_open()), \
134             mock.patch('__builtin__.iter', return_value=[r'\} tempest\.']), \
135             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
136                        'subprocess.Popen'):
137             conf_utils.TEMPEST_LIST = 'test_tempest_list'
138             cmd = ["rally", "verify", "start", "--load-list",
139                    conf_utils.TEMPEST_LIST]
140             with self.assertRaises(Exception):
141                 self.tempestcommon.run_verifier_tests()
142                 mock_logger_info. \
143                     assert_any_call("Starting Tempest test suite: '%s'.", cmd)
144
145     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
146                 'subprocess.Popen')
147     def test_generate_report(self, mock_popen):
148         self.tempestcommon.verification_id = "1234"
149         html_file = os.path.join(conf_utils.TEMPEST_RESULTS_DIR,
150                                  "tempest-report.html")
151         cmd = ["rally", "verify", "report", "--type", "html", "--uuid",
152                "1234", "--to", html_file]
153         self.tempestcommon.generate_report()
154         mock_popen.assert_called_once_with(cmd, stdout=mock.ANY,
155                                            stderr=mock.ANY)
156
157     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
158                 'os.path.exists', return_value=False)
159     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs',
160                 side_effect=Exception)
161     def test_run_makedirs_ko(self, *args):
162         # pylint: disable=unused-argument
163         self.assertEqual(self.tempestcommon.run(),
164                          testcase.TestCase.EX_RUN_ERROR)
165
166     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
167                 'os.path.exists', return_value=False)
168     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
169     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
170                 'TempestResourcesManager.create', side_effect=Exception)
171     def test_run_create_resources_ko(self, *args):
172         # pylint: disable=unused-argument
173         self.assertEqual(self.tempestcommon.run(),
174                          testcase.TestCase.EX_RUN_ERROR)
175
176     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
177                 'os.path.exists', return_value=False)
178     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
179     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
180                 'TempestResourcesManager.create', return_value={})
181     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
182                 'get_active_compute_cnt', side_effect=Exception)
183     def test_run_get_active_comp_cnt_ko(self, *args):
184         # pylint: disable=unused-argument
185         self.assertEqual(self.tempestcommon.run(),
186                          testcase.TestCase.EX_RUN_ERROR)
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     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
192                 'TempestResourcesManager.create', return_value={})
193     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
194                 'get_active_compute_cnt', return_value=2)
195     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
196                 'conf_utils.configure_tempest', side_effect=Exception)
197     def test_run_configure_tempest_ko(self, *args):
198         # pylint: disable=unused-argument
199         self.assertEqual(self.tempestcommon.run(),
200                          testcase.TestCase.EX_RUN_ERROR)
201
202     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
203                 'os.path.exists', return_value=False)
204     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
205     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
206                 'TempestResourcesManager.create', return_value={})
207     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
208                 'get_active_compute_cnt', return_value=2)
209     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
210                 'conf_utils.configure_tempest')
211     def _test_run(self, status, *args):
212         # pylint: disable=unused-argument
213         self.assertEqual(self.tempestcommon.run(), status)
214
215     def test_run_missing_gen_test_list(self):
216         with mock.patch.object(self.tempestcommon, 'generate_test_list',
217                                side_effect=Exception):
218             self._test_run(testcase.TestCase.EX_RUN_ERROR)
219
220     def test_run_apply_blacklist_ko(self):
221         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
222                 mock.patch.object(
223                     self.tempestcommon, 'apply_tempest_blacklist',
224                     side_effect=Exception()):
225             self._test_run(testcase.TestCase.EX_RUN_ERROR)
226
227     def test_run_verifier_tests_ko(self):
228         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
229                 mock.patch.object(self.tempestcommon,
230                                   'apply_tempest_blacklist'), \
231                 mock.patch.object(self.tempestcommon, 'run_verifier_tests',
232                                   side_effect=Exception()), \
233                 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
234                                   side_effect=Exception):
235             self._test_run(testcase.TestCase.EX_RUN_ERROR)
236
237     def test_run_verif_result_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                 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(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,
252                                   'parse_verifier_result'), \
253                 mock.patch.object(self.tempestcommon, 'generate_report'):
254             self._test_run(testcase.TestCase.EX_OK)
255
256
257 if __name__ == "__main__":
258     logging.disable(logging.CRITICAL)
259     unittest.main(verbosity=2)