Merge "Stop setting ext net in config"
[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 unittest
12
13 import mock
14
15 from functest.core import testcase
16 from functest.opnfv_tests.openstack.tempest import tempest
17 from functest.opnfv_tests.openstack.tempest import conf_utils
18 from functest.utils.constants import CONST
19
20 from snaps.openstack.os_credentials import OSCreds
21
22
23 class OSTempestTesting(unittest.TestCase):
24
25     def setUp(self):
26         os_creds = OSCreds(
27             username='user', password='pass',
28             auth_url='http://foo.com:5000/v3', project_name='bar')
29
30         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
31                         'conf_utils.get_verifier_id',
32                         return_value='test_deploy_id'), \
33             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
34                        'conf_utils.get_verifier_deployment_id',
35                        return_value='test_deploy_id'), \
36             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
37                        'conf_utils.get_verifier_repo_dir',
38                        return_value='test_verifier_repo_dir'), \
39             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
40                        'conf_utils.get_verifier_deployment_dir',
41                        return_value='test_verifier_deploy_dir'), \
42             mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
43                        'get_credentials',
44                        return_value=os_creds):
45             self.tempestcommon = tempest.TempestCommon()
46             self.tempestsmoke_serial = tempest.TempestSmokeSerial()
47             self.tempestsmoke_parallel = tempest.TempestSmokeParallel()
48             self.tempestfull_parallel = tempest.TempestFullParallel()
49             self.tempestcustom = tempest.TempestCustom()
50             self.tempestdefcore = tempest.TempestDefcore()
51
52     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.debug')
53     def test_generate_test_list_defcore_mode(self, mock_logger_debug):
54         self.tempestcommon.MODE = 'defcore'
55         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
56                         'shutil.copyfile') as m:
57             self.tempestcommon.generate_test_list('test_verifier_repo_dir')
58             self.assertTrue(m.called)
59
60     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.error')
61     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.debug')
62     def test_generate_test_list_custom_mode_missing_file(self,
63                                                          mock_logger_debug,
64                                                          mock_logger_error):
65         self.tempestcommon.MODE = 'custom'
66         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
67                         'os.path.isfile', return_value=False), \
68                 self.assertRaises(Exception) as context:
69             msg = "Tempest test list file %s NOT found."
70             self.tempestcommon.generate_test_list('test_verifier_repo_dir')
71             self.assertTrue((msg % conf_utils.TEMPEST_CUSTOM) in context)
72
73     def test_generate_test_list_custom_mode_default(self):
74         self.tempestcommon.MODE = 'custom'
75         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
76                         'shutil.copyfile') as m, \
77             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
78                        'os.path.isfile', return_value=True):
79             self.tempestcommon.generate_test_list('test_verifier_repo_dir')
80             self.assertTrue(m.called)
81
82     def _test_generate_test_list_mode_default(self, mode):
83         self.tempestcommon.MODE = mode
84         if self.tempestcommon.MODE == 'smoke':
85             testr_mode = "smoke"
86         elif self.tempestcommon.MODE == 'full':
87             testr_mode = ""
88         else:
89             testr_mode = 'tempest.api.' + self.tempestcommon.MODE
90         conf_utils.TEMPEST_RAW_LIST = 'raw_list'
91         verifier_repo_dir = 'test_verifier_repo_dir'
92         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
93                         'ft_utils.execute_command') as m:
94             cmd = ("cd {0};"
95                    "testr list-tests {1} > {2};"
96                    "cd -;".format(verifier_repo_dir,
97                                   testr_mode,
98                                   conf_utils.TEMPEST_RAW_LIST))
99             self.tempestcommon.generate_test_list('test_verifier_repo_dir')
100             m.assert_any_call(cmd)
101
102     def test_generate_test_list_smoke_mode(self):
103         self._test_generate_test_list_mode_default('smoke')
104
105     def test_generate_test_list_full_mode(self):
106         self._test_generate_test_list_mode_default('full')
107
108     def test_parse_verifier_result_missing_verification_uuid(self):
109         self.tempestcommon.VERIFICATION_ID = None
110         with self.assertRaises(Exception):
111             self.tempestcommon.parse_verifier_result()
112
113     def test_apply_tempest_blacklist_no_blacklist(self):
114         with mock.patch('__builtin__.open', mock.mock_open()) as m, \
115             mock.patch.object(self.tempestcommon, 'read_file',
116                               return_value=['test1', 'test2']):
117             conf_utils.TEMPEST_BLACKLIST = Exception
118             CONST.__setattr__('INSTALLER_TYPE', 'installer_type')
119             CONST.__setattr__('DEPLOY_SCENARIO', 'deploy_scenario')
120             self.tempestcommon.apply_tempest_blacklist()
121             obj = m()
122             obj.write.assert_any_call('test1\n')
123             obj.write.assert_any_call('test2\n')
124
125     def test_apply_tempest_blacklist_default(self):
126         item_dict = {'scenarios': ['deploy_scenario'],
127                      'installers': ['installer_type'],
128                      'tests': ['test2']}
129         with mock.patch('__builtin__.open', mock.mock_open()) as m, \
130             mock.patch.object(self.tempestcommon, 'read_file',
131                               return_value=['test1', 'test2']), \
132             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
133                        'yaml.safe_load', return_value=item_dict):
134             CONST.__setattr__('INSTALLER_TYPE', 'installer_type')
135             CONST.__setattr__('DEPLOY_SCENARIO', 'deploy_scenario')
136             self.tempestcommon.apply_tempest_blacklist()
137             obj = m()
138             obj.write.assert_any_call('test1\n')
139             self.assertFalse(obj.write.assert_any_call('test2\n'))
140
141     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.info')
142     def test_run_verifier_tests_default(self, mock_logger_info):
143         with mock.patch('__builtin__.open', mock.mock_open()), \
144             mock.patch('__builtin__.iter', return_value=['\} tempest\.']), \
145             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
146                        'subprocess.Popen'):
147             conf_utils.TEMPEST_LIST = 'test_tempest_list'
148             cmd_line = ("rally verify start  --load-list "
149                         "test_tempest_list --detailed")
150             self.tempestcommon.run_verifier_tests()
151             mock_logger_info. \
152                 assert_any_call("Starting Tempest test suite: '%s'."
153                                 % cmd_line)
154
155     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
156                 'os.path.exists', return_value=False)
157     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs',
158                 side_effect=Exception)
159     def test_run_makedirs_ko(self, *args):
160         self.assertEqual(self.tempestcommon.run(),
161                          testcase.TestCase.EX_RUN_ERROR)
162
163     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
164                 'os.path.exists', return_value=False)
165     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
166     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
167                 'TempestResourcesManager.create', side_effect=Exception)
168     def test_run_tempest_create_resources_ko(self, *args):
169         self.assertEqual(self.tempestcommon.run(),
170                          testcase.TestCase.EX_RUN_ERROR)
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     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
176                 'TempestResourcesManager.create', return_value={})
177     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
178                 'get_active_compute_cnt', side_effect=Exception)
179     def test_run_get_active_compute_cnt_ko(self, *args):
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', return_value={})
188     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
189                 'get_active_compute_cnt', return_value=2)
190     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
191                 'conf_utils.configure_tempest', side_effect=Exception)
192     def test_run_configure_tempest_ko(self, *args):
193         self.assertEqual(self.tempestcommon.run(),
194                          testcase.TestCase.EX_RUN_ERROR)
195
196     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
197                 'os.path.exists', return_value=False)
198     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
199     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
200                 'TempestResourcesManager.create', return_value={})
201     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
202                 'get_active_compute_cnt', return_value=2)
203     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
204                 'conf_utils.configure_tempest')
205     def _test_run(self, status, *args):
206         self.assertEqual(self.tempestcommon.run(), status)
207
208     def test_run_missing_generate_test_list(self):
209         with mock.patch.object(self.tempestcommon, 'generate_test_list',
210                                side_effect=Exception):
211             self._test_run(testcase.TestCase.EX_RUN_ERROR)
212
213     def test_run_apply_tempest_blacklist_ko(self):
214         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
215                     mock.patch.object(self.tempestcommon,
216                                       'apply_tempest_blacklist',
217                                       side_effect=Exception()):
218             self._test_run(testcase.TestCase.EX_RUN_ERROR)
219
220     def test_run_verifier_tests_ko(self, *args):
221         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
222                 mock.patch.object(self.tempestcommon,
223                                   'apply_tempest_blacklist'), \
224                 mock.patch.object(self.tempestcommon, 'run_verifier_tests',
225                                   side_effect=Exception()), \
226                 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
227                                   side_effect=Exception):
228             self._test_run(testcase.TestCase.EX_RUN_ERROR)
229
230     def test_run_parse_verifier_result_ko(self, *args):
231         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
232                 mock.patch.object(self.tempestcommon,
233                                   'apply_tempest_blacklist'), \
234                 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
235                 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
236                                   side_effect=Exception):
237             self._test_run(testcase.TestCase.EX_RUN_ERROR)
238
239     def test_run(self, *args):
240         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
241                 mock.patch.object(self.tempestcommon,
242                                   'apply_tempest_blacklist'), \
243                 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
244                 mock.patch.object(self.tempestcommon, 'parse_verifier_result'):
245             self._test_run(testcase.TestCase.EX_OK)
246
247
248 if __name__ == "__main__":
249     logging.disable(logging.CRITICAL)
250     unittest.main(verbosity=2)