Merge "More Unit Tests for utils module"
[functest.git] / functest / tests / unit / opnfv_tests / 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 import logging
9 import unittest
10
11 import mock
12
13 from functest.core import testcase
14 from functest.opnfv_tests.openstack.tempest import tempest
15 from functest.opnfv_tests.openstack.tempest import conf_utils
16 from functest.utils.constants import CONST
17
18
19 class OSTempestTesting(unittest.TestCase):
20
21     logging.disable(logging.CRITICAL)
22
23     def setUp(self):
24         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
25                         'conf_utils.get_verifier_id',
26                         return_value='test_deploy_id'), \
27             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
28                        'conf_utils.get_verifier_deployment_id',
29                        return_value='test_deploy_id'), \
30             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
31                        'conf_utils.get_verifier_repo_dir',
32                        return_value='test_verifier_repo_dir'), \
33             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
34                        'conf_utils.get_verifier_deployment_dir',
35                        return_value='test_verifier_deploy_dir'):
36             self.tempestcommon = tempest.TempestCommon()
37             self.tempestsmoke_serial = tempest.TempestSmokeSerial()
38             self.tempestsmoke_parallel = tempest.TempestSmokeParallel()
39             self.tempestfull_parallel = tempest.TempestFullParallel()
40             with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
41                             'conf_utils.install_verifier_ext'):
42                 self.tempestmultisite = tempest.TempestMultisite()
43             self.tempestcustom = tempest.TempestCustom()
44             self.tempestdefcore = tempest.TempestDefcore()
45
46     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.debug')
47     def test_generate_test_list_defcore_mode(self, mock_logger_debug):
48         self.tempestcommon.MODE = 'defcore'
49         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
50                         'shutil.copyfile') as m:
51             self.tempestcommon.generate_test_list('test_verifier_repo_dir')
52             self.assertTrue(m.called)
53
54     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.error')
55     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.debug')
56     def test_generate_test_list_custom_mode_missing_file(self,
57                                                          mock_logger_debug,
58                                                          mock_logger_error):
59         self.tempestcommon.MODE = 'custom'
60         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
61                         'os.path.isfile', return_value=False), \
62                 self.assertRaises(Exception) as context:
63             msg = "Tempest test list file %s NOT found."
64             self.tempestcommon.generate_test_list('test_verifier_repo_dir')
65             self.assertTrue((msg % conf_utils.TEMPEST_CUSTOM) in context)
66
67     def test_generate_test_list_custom_mode_default(self):
68         self.tempestcommon.MODE = 'custom'
69         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
70                         'shutil.copyfile') as m, \
71             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
72                        'os.path.isfile', return_value=True):
73             self.tempestcommon.generate_test_list('test_verifier_repo_dir')
74             self.assertTrue(m.called)
75
76     def _test_generate_test_list_mode_default(self, mode):
77         self.tempestcommon.MODE = mode
78         if self.tempestcommon.MODE == 'smoke':
79             testr_mode = "smoke"
80         elif self.tempestcommon.MODE == 'feature_multisite':
81             testr_mode = "'[Kk]ingbird'"
82         elif self.tempestcommon.MODE == 'full':
83             testr_mode = ""
84         else:
85             testr_mode = 'tempest.api.' + self.tempestcommon.MODE
86         conf_utils.TEMPEST_RAW_LIST = 'raw_list'
87         verifier_repo_dir = 'test_verifier_repo_dir'
88         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
89                         'ft_utils.execute_command') as m:
90             cmd = ("cd {0};"
91                    "testr list-tests {1} > {2};"
92                    "cd -;".format(verifier_repo_dir,
93                                   testr_mode,
94                                   conf_utils.TEMPEST_RAW_LIST))
95             self.tempestcommon.generate_test_list('test_verifier_repo_dir')
96             m.assert_any_call(cmd)
97
98     def test_generate_test_list_smoke_mode(self):
99         self._test_generate_test_list_mode_default('smoke')
100
101     def test_generate_test_list_feature_multisite_mode(self):
102         self._test_generate_test_list_mode_default('feature_multisite')
103
104     def test_generate_test_list_full_mode(self):
105         self._test_generate_test_list_mode_default('full')
106
107     def test_parse_verifier_result_missing_verification_uuid(self):
108         self.tempestcommon.VERIFICATION_ID = ''
109         with self.assertRaises(Exception):
110             self.tempestcommon.parse_verifier_result()
111
112     def test_apply_tempest_blacklist_no_blacklist(self):
113         with mock.patch('__builtin__.open', mock.mock_open()) as m, \
114             mock.patch.object(self.tempestcommon, 'read_file',
115                               return_value=['test1', 'test2']):
116             conf_utils.TEMPEST_BLACKLIST = Exception
117             CONST.INSTALLER_TYPE = 'installer_type'
118             CONST.DEPLOY_SCENARIO = 'deploy_scenario'
119             self.tempestcommon.apply_tempest_blacklist()
120             obj = m()
121             obj.write.assert_any_call('test1\n')
122             obj.write.assert_any_call('test2\n')
123
124     def test_apply_tempest_blacklist_default(self):
125         item_dict = {'scenarios': ['deploy_scenario'],
126                      'installers': ['installer_type'],
127                      'tests': ['test2']}
128         with mock.patch('__builtin__.open', mock.mock_open()) as m, \
129             mock.patch.object(self.tempestcommon, 'read_file',
130                               return_value=['test1', 'test2']), \
131             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
132                        'yaml.safe_load', return_value=item_dict):
133             CONST.INSTALLER_TYPE = 'installer_type'
134             CONST.DEPLOY_SCENARIO = 'deploy_scenario'
135             self.tempestcommon.apply_tempest_blacklist()
136             obj = m()
137             obj.write.assert_any_call('test1\n')
138             self.assertFalse(obj.write.assert_any_call('test2\n'))
139
140     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.info')
141     def test_run_verifier_tests_default(self, mock_logger_info):
142         with mock.patch('__builtin__.open', mock.mock_open()), \
143             mock.patch('__builtin__.iter', return_value=['\} tempest\.']), \
144             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
145                        'subprocess.Popen'):
146             conf_utils.TEMPEST_LIST = 'test_tempest_list'
147             cmd_line = ("rally verify start  --load-list "
148                         "test_tempest_list --detailed")
149             self.tempestcommon.run_verifier_tests()
150             mock_logger_info. \
151                 assert_any_call("Starting Tempest test suite: '%s'."
152                                 % cmd_line)
153
154     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.info')
155     def test_parse_verifier_result_default(self, mock_logger_info):
156         self.tempestcommon.VERIFICATION_ID = 'test_uuid'
157         self.tempestcommon.case_name = 'test_case_name'
158         stdout = ['Testscount||2', 'Success||2', 'Skipped||0', 'Failures||0']
159         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
160                         'subprocess.Popen') as mock_popen, \
161             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
162                        'ft_utils.check_success_rate') as mock_method, \
163                 mock.patch('__builtin__.open', mock.mock_open()):
164             mock_stdout = mock.Mock()
165             attrs = {'stdout': stdout}
166             mock_stdout.configure_mock(**attrs)
167             mock_popen.return_value = mock_stdout
168
169             self.tempestcommon.parse_verifier_result()
170             mock_method.assert_any_call('test_case_name', 100)
171
172     def test_run_missing_create_tempest_dir(self):
173         ret = testcase.TestCase.EX_RUN_ERROR
174         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
175                         'os.path.exists', return_value=False), \
176             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
177                        'os.makedirs') as mock_os_makedirs, \
178             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
179                        'conf_utils.create_tempest_resources',
180                        return_value="image_and_flavor"):
181             self.assertEqual(self.tempestcommon.run(),
182                              ret)
183             self.assertTrue(mock_os_makedirs.called)
184
185     def test_run_missing_configure_tempest(self):
186         ret = testcase.TestCase.EX_RUN_ERROR
187         ret_ok = testcase.TestCase.EX_OK
188         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
189                         'os.path.exists', return_value=False), \
190             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
191                        'os.makedirs') as mock_os_makedirs, \
192             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
193                        'conf_utils.create_tempest_resources',
194                        return_value=ret_ok), \
195             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
196                        'conf_utils.configure_tempest',
197                        return_value=ret):
198             self.assertEqual(self.tempestcommon.run(),
199                              ret)
200             self.assertTrue(mock_os_makedirs.called)
201
202     def test_run_missing_generate_test_list(self):
203         ret = testcase.TestCase.EX_RUN_ERROR
204         ret_ok = testcase.TestCase.EX_OK
205         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
206                         'os.path.exists', return_value=False), \
207             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
208                        'os.makedirs') as mock_os_makedirs, \
209             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
210                        'conf_utils.create_tempest_resources',
211                        return_value=ret_ok), \
212             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
213                        'conf_utils.configure_tempest',
214                        return_value=ret_ok), \
215             mock.patch.object(self.tempestcommon, 'generate_test_list',
216                               return_value=ret):
217             self.assertEqual(self.tempestcommon.run(),
218                              ret)
219             self.assertTrue(mock_os_makedirs.called)
220
221     def test_run_missing_apply_tempest_blacklist(self):
222         ret = testcase.TestCase.EX_RUN_ERROR
223         ret_ok = testcase.TestCase.EX_OK
224         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
225                         'os.path.exists', return_value=False), \
226             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
227                        'os.makedirs') as mock_os_makedirs, \
228             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
229                        'conf_utils.create_tempest_resources',
230                        return_value=ret_ok), \
231             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
232                        'conf_utils.configure_tempest',
233                        return_value=ret_ok), \
234             mock.patch.object(self.tempestcommon, 'generate_test_list',
235                               return_value=ret_ok), \
236             mock.patch.object(self.tempestcommon, 'apply_tempest_blacklist',
237                               return_value=ret):
238             self.assertEqual(self.tempestcommon.run(),
239                              ret)
240             self.assertTrue(mock_os_makedirs.called)
241
242     def test_run_missing_parse_verifier_result(self):
243         ret = testcase.TestCase.EX_RUN_ERROR
244         ret_ok = testcase.TestCase.EX_OK
245         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
246                         'os.path.exists', return_value=False), \
247             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
248                        'os.makedirs') as mock_os_makedirs, \
249             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
250                        'conf_utils.create_tempest_resources',
251                        return_value=ret_ok), \
252             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
253                        'conf_utils.configure_tempest',
254                        return_value=ret_ok), \
255             mock.patch.object(self.tempestcommon, 'generate_test_list',
256                               return_value=ret_ok), \
257             mock.patch.object(self.tempestcommon, 'apply_tempest_blacklist',
258                               return_value=ret_ok), \
259             mock.patch.object(self.tempestcommon, 'run_verifier_tests',
260                               return_value=ret_ok), \
261             mock.patch.object(self.tempestcommon, 'parse_verifier_result',
262                               return_value=ret):
263             self.assertEqual(self.tempestcommon.run(),
264                              ret)
265             self.assertTrue(mock_os_makedirs.called)
266
267
268 if __name__ == "__main__":
269     unittest.main(verbosity=2)