2b9dcff44daa1510a270527c8be6cad0ec2eb60d
[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.raw_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     def test_apply_missing_blacklist(self):
103         with mock.patch('__builtin__.open', mock.mock_open()) as mock_open, \
104             mock.patch.object(self.tempestcommon, 'read_file',
105                               return_value=['test1', 'test2']):
106             conf_utils.TEMPEST_BLACKLIST = Exception
107             os.environ['INSTALLER_TYPE'] = 'installer_type'
108             os.environ['DEPLOY_SCENARIO'] = 'deploy_scenario'
109             self.tempestcommon.apply_tempest_blacklist()
110             obj = mock_open()
111             obj.write.assert_any_call('test1\n')
112             obj.write.assert_any_call('test2\n')
113
114     def test_apply_blacklist_default(self):
115         item_dict = {'scenarios': ['deploy_scenario'],
116                      'installers': ['installer_type'],
117                      'tests': ['test2']}
118         with mock.patch('__builtin__.open', mock.mock_open()) as mock_open, \
119             mock.patch.object(self.tempestcommon, 'read_file',
120                               return_value=['test1', 'test2']), \
121             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
122                        'yaml.safe_load', return_value=item_dict):
123             os.environ['INSTALLER_TYPE'] = 'installer_type'
124             os.environ['DEPLOY_SCENARIO'] = 'deploy_scenario'
125             self.tempestcommon.apply_tempest_blacklist()
126             obj = mock_open()
127             obj.write.assert_any_call('test1\n')
128             self.assertFalse(obj.write.assert_any_call('test2\n'))
129
130     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.info')
131     def test_run_verifier_tests_default(self, mock_logger_info):
132         with mock.patch('__builtin__.open', mock.mock_open()), \
133             mock.patch('__builtin__.iter', return_value=[r'\} tempest\.']), \
134             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
135                        'subprocess.Popen'):
136             conf_utils.TEMPEST_LIST = 'test_tempest_list'
137             cmd = ["rally", "verify", "start", "--load-list",
138                    conf_utils.TEMPEST_LIST]
139             with self.assertRaises(Exception):
140                 self.tempestcommon.run_verifier_tests()
141                 mock_logger_info. \
142                     assert_any_call("Starting Tempest test suite: '%s'.", cmd)
143
144     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
145                 'subprocess.Popen')
146     def test_generate_report(self, mock_popen):
147         self.tempestcommon.verification_id = "1234"
148         html_file = os.path.join(tempest.TempestCommon.TEMPEST_RESULTS_DIR,
149                                  "tempest-report.html")
150         cmd = ["rally", "verify", "report", "--type", "html", "--uuid",
151                "1234", "--to", html_file]
152         self.tempestcommon.generate_report()
153         mock_popen.assert_called_once_with(cmd, stdout=mock.ANY,
154                                            stderr=mock.ANY)
155
156     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
157                 'os.path.exists', return_value=False)
158     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs',
159                 side_effect=Exception)
160     def test_run_makedirs_ko(self, *args):
161         # pylint: disable=unused-argument
162         self.assertEqual(self.tempestcommon.run(),
163                          testcase.TestCase.EX_RUN_ERROR)
164
165     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
166                 'os.path.exists', return_value=False)
167     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
168     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
169                 'TempestResourcesManager.create', side_effect=Exception)
170     def test_run_create_resources_ko(self, *args):
171         # pylint: disable=unused-argument
172         self.assertEqual(self.tempestcommon.run(),
173                          testcase.TestCase.EX_RUN_ERROR)
174
175     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
176                 'os.path.exists', return_value=False)
177     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
178     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
179                 'TempestResourcesManager.create', return_value={})
180     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
181                 'get_active_compute_cnt', side_effect=Exception)
182     def test_run_get_active_comp_cnt_ko(self, *args):
183         # pylint: disable=unused-argument
184         self.assertEqual(self.tempestcommon.run(),
185                          testcase.TestCase.EX_RUN_ERROR)
186
187     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
188                 'os.path.exists', return_value=False)
189     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
190     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
191                 'TempestResourcesManager.create', return_value={})
192     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
193                 'get_active_compute_cnt', return_value=2)
194     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
195                 'conf_utils.configure_tempest', side_effect=Exception)
196     def test_run_configure_tempest_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')
210     def _test_run(self, status, *args):
211         # pylint: disable=unused-argument
212         self.assertEqual(self.tempestcommon.run(), status)
213
214     def test_run_missing_gen_test_list(self):
215         with mock.patch.object(self.tempestcommon, 'generate_test_list',
216                                side_effect=Exception):
217             self._test_run(testcase.TestCase.EX_RUN_ERROR)
218
219     def test_run_apply_blacklist_ko(self):
220         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
221                 mock.patch.object(
222                     self.tempestcommon, 'apply_tempest_blacklist',
223                     side_effect=Exception()):
224             self._test_run(testcase.TestCase.EX_RUN_ERROR)
225
226     def test_run_verifier_tests_ko(self):
227         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
228                 mock.patch.object(self.tempestcommon,
229                                   'apply_tempest_blacklist'), \
230                 mock.patch.object(self.tempestcommon, 'run_verifier_tests',
231                                   side_effect=Exception()), \
232                 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
233                                   side_effect=Exception):
234             self._test_run(testcase.TestCase.EX_RUN_ERROR)
235
236     def test_run_verif_result_ko(self):
237         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
238                 mock.patch.object(self.tempestcommon,
239                                   'apply_tempest_blacklist'), \
240                 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
241                 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
242                                   side_effect=Exception):
243             self._test_run(testcase.TestCase.EX_RUN_ERROR)
244
245     def test_run(self):
246         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
247                 mock.patch.object(self.tempestcommon,
248                                   'apply_tempest_blacklist'), \
249                 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
250                 mock.patch.object(self.tempestcommon,
251                                   'parse_verifier_result'), \
252                 mock.patch.object(self.tempestcommon, 'generate_report'):
253             self._test_run(testcase.TestCase.EX_OK)
254
255
256 if __name__ == "__main__":
257     logging.disable(logging.CRITICAL)
258     unittest.main(verbosity=2)