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