43682114368d6eddc183a13b193c2e7d121e7637
[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             self.tempestneutrontrunk = tempest.TempestNeutronTrunk()
51
52     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.error')
53     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.debug')
54     def test_gen_tl_cm_missing_file(self, mock_logger_debug,
55                                     mock_logger_error):
56         # pylint: disable=unused-argument
57         self.tempestcommon.mode = 'custom'
58         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
59                         'os.path.isfile', return_value=False), \
60                 self.assertRaises(Exception) as context:
61             msg = "Tempest test list file %s NOT found."
62             self.tempestcommon.generate_test_list()
63             self.assertTrue(
64                 (msg % conf_utils.TEMPEST_CUSTOM) in context.exception)
65
66     def test_gen_tl_cm_default(self):
67         self.tempestcommon.mode = 'custom'
68         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
69                         'shutil.copyfile') as mock_copyfile, \
70             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
71                        'os.path.isfile', return_value=True):
72             self.tempestcommon.generate_test_list()
73             self.assertTrue(mock_copyfile.called)
74
75     @mock.patch('functest.utils.functest_utils.execute_command')
76     def _test_gen_tl_mode_default(self, mode, mock_exec=None):
77         self.tempestcommon.mode = mode
78         if self.tempestcommon.mode == 'smoke':
79             testr_mode = r"'tempest\.(api|scenario).*\[.*\bsmoke\b.*\]'"
80         elif self.tempestcommon.mode == 'full':
81             testr_mode = r"'^tempest\.'"
82         else:
83             testr_mode = self.tempestcommon.mode
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                               self.tempestcommon.list))
89         self.tempestcommon.generate_test_list()
90         mock_exec.assert_called_once_with(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_gen_tl_neutron_trunk_mode(self):
99         self._test_gen_tl_mode_default('neutron_trunk')
100
101     def test_verif_res_missing_verif_id(self):
102         self.tempestcommon.verification_id = None
103         with self.assertRaises(Exception):
104             self.tempestcommon.parse_verifier_result()
105
106     @mock.patch("os.rename")
107     @mock.patch("os.remove")
108     @mock.patch("os.path.exists", return_value=True)
109     def test_apply_missing_blacklist(self, *args):
110         with mock.patch('__builtin__.open', mock.mock_open()) as mock_open, \
111             mock.patch.object(self.tempestcommon, 'read_file',
112                               return_value=['test1', 'test2']):
113             conf_utils.TEMPEST_BLACKLIST = Exception
114             os.environ['INSTALLER_TYPE'] = 'installer_type'
115             os.environ['DEPLOY_SCENARIO'] = 'deploy_scenario'
116             self.tempestcommon.apply_tempest_blacklist()
117             obj = mock_open()
118             obj.write.assert_any_call('test1\n')
119             obj.write.assert_any_call('test2\n')
120             args[0].assert_called_once_with(self.tempestcommon.raw_list)
121             args[1].assert_called_once_with(self.tempestcommon.raw_list)
122             args[2].assert_called_once_with(
123                 self.tempestcommon.list, self.tempestcommon.raw_list)
124
125     @mock.patch("os.rename")
126     @mock.patch("os.remove")
127     @mock.patch("os.path.exists", return_value=True)
128     def test_apply_blacklist_default(self, *args):
129         item_dict = {'scenarios': ['deploy_scenario'],
130                      'installers': ['installer_type'],
131                      'tests': ['test2']}
132         with mock.patch('__builtin__.open', mock.mock_open()) as mock_open, \
133             mock.patch.object(self.tempestcommon, 'read_file',
134                               return_value=['test1', 'test2']), \
135             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
136                        'yaml.safe_load', return_value=item_dict):
137             os.environ['INSTALLER_TYPE'] = 'installer_type'
138             os.environ['DEPLOY_SCENARIO'] = 'deploy_scenario'
139             self.tempestcommon.apply_tempest_blacklist()
140             obj = mock_open()
141             obj.write.assert_any_call('test1\n')
142             self.assertFalse(obj.write.assert_any_call('test2\n'))
143             args[0].assert_called_once_with(self.tempestcommon.raw_list)
144             args[1].assert_called_once_with(self.tempestcommon.raw_list)
145             args[2].assert_called_once_with(
146                 self.tempestcommon.list, self.tempestcommon.raw_list)
147
148     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.LOGGER.info')
149     def test_run_verifier_tests_default(self, mock_logger_info):
150         with mock.patch('__builtin__.open', mock.mock_open()), \
151             mock.patch('__builtin__.iter', return_value=[r'\} tempest\.']), \
152             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
153                        'subprocess.Popen'):
154             conf_utils.TEMPEST_LIST = 'test_tempest_list'
155             cmd = ["rally", "verify", "start", "--load-list",
156                    conf_utils.TEMPEST_LIST]
157             with self.assertRaises(Exception):
158                 self.tempestcommon.run_verifier_tests()
159                 mock_logger_info. \
160                     assert_any_call("Starting Tempest test suite: '%s'.", cmd)
161
162     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
163                 'subprocess.Popen')
164     def test_generate_report(self, mock_popen):
165         self.tempestcommon.verification_id = "1234"
166         html_file = os.path.join(tempest.TempestCommon.TEMPEST_RESULTS_DIR,
167                                  "tempest-report.html")
168         cmd = ["rally", "verify", "report", "--type", "html", "--uuid",
169                "1234", "--to", html_file]
170         self.tempestcommon.generate_report()
171         mock_popen.assert_called_once_with(cmd, stdout=mock.ANY,
172                                            stderr=mock.ANY)
173
174     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
175                 'os.path.exists', return_value=False)
176     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs',
177                 side_effect=Exception)
178     def test_run_makedirs_ko(self, *args):
179         # pylint: disable=unused-argument
180         self.assertEqual(self.tempestcommon.run(),
181                          testcase.TestCase.EX_RUN_ERROR)
182
183     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
184                 'os.path.exists', return_value=False)
185     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
186     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
187                 'TempestResourcesManager.create', side_effect=Exception)
188     def test_run_create_resources_ko(self, *args):
189         # pylint: disable=unused-argument
190         self.assertEqual(self.tempestcommon.run(),
191                          testcase.TestCase.EX_RUN_ERROR)
192
193     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
194                 'os.path.exists', return_value=False)
195     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
196     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
197                 'TempestResourcesManager.create', return_value={})
198     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
199                 'get_active_compute_cnt', side_effect=Exception)
200     def test_run_get_active_comp_cnt_ko(self, *args):
201         # pylint: disable=unused-argument
202         self.assertEqual(self.tempestcommon.run(),
203                          testcase.TestCase.EX_RUN_ERROR)
204
205     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
206                 'os.path.exists', return_value=False)
207     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
208     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
209                 'TempestResourcesManager.create', return_value={})
210     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
211                 'get_active_compute_cnt', return_value=2)
212     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
213                 'TempestCommon.configure', side_effect=Exception)
214     def test_run_configure_tempest_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')
228     def _test_run(self, status, *args):
229         # pylint: disable=unused-argument
230         self.assertEqual(self.tempestcommon.run(), status)
231
232     def test_run_missing_gen_test_list(self):
233         with mock.patch.object(self.tempestcommon, 'generate_test_list',
234                                side_effect=Exception):
235             self._test_run(testcase.TestCase.EX_RUN_ERROR)
236
237     def test_run_apply_blacklist_ko(self):
238         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
239                 mock.patch.object(
240                     self.tempestcommon, 'apply_tempest_blacklist',
241                     side_effect=Exception()):
242             self._test_run(testcase.TestCase.EX_RUN_ERROR)
243
244     def test_run_verifier_tests_ko(self):
245         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
246                 mock.patch.object(self.tempestcommon,
247                                   'apply_tempest_blacklist'), \
248                 mock.patch.object(self.tempestcommon, 'run_verifier_tests',
249                                   side_effect=Exception()), \
250                 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
251                                   side_effect=Exception):
252             self._test_run(testcase.TestCase.EX_RUN_ERROR)
253
254     def test_run_verif_result_ko(self):
255         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
256                 mock.patch.object(self.tempestcommon,
257                                   'apply_tempest_blacklist'), \
258                 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
259                 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
260                                   side_effect=Exception):
261             self._test_run(testcase.TestCase.EX_RUN_ERROR)
262
263     def test_run(self):
264         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
265                 mock.patch.object(self.tempestcommon,
266                                   'apply_tempest_blacklist'), \
267                 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
268                 mock.patch.object(self.tempestcommon,
269                                   'parse_verifier_result'), \
270                 mock.patch.object(self.tempestcommon, 'generate_report'):
271             self._test_run(testcase.TestCase.EX_OK)
272
273
274 if __name__ == "__main__":
275     logging.disable(logging.CRITICAL)
276     unittest.main(verbosity=2)