Merge "Put domain name in CONST"
[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 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     def setUp(self):
22         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
23                         'conf_utils.get_verifier_id',
24                         return_value='test_deploy_id'), \
25             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
26                        'conf_utils.get_verifier_deployment_id',
27                        return_value='test_deploy_id'), \
28             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
29                        'conf_utils.get_verifier_repo_dir',
30                        return_value='test_verifier_repo_dir'), \
31             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
32                        'conf_utils.get_verifier_deployment_dir',
33                        return_value='test_verifier_deploy_dir'):
34             self.tempestcommon = tempest.TempestCommon()
35             self.tempestsmoke_serial = tempest.TempestSmokeSerial()
36             self.tempestsmoke_parallel = tempest.TempestSmokeParallel()
37             self.tempestfull_parallel = tempest.TempestFullParallel()
38             self.tempestcustom = tempest.TempestCustom()
39             self.tempestdefcore = tempest.TempestDefcore()
40
41     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.debug')
42     def test_generate_test_list_defcore_mode(self, mock_logger_debug):
43         self.tempestcommon.MODE = 'defcore'
44         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
45                         'shutil.copyfile') as m:
46             self.tempestcommon.generate_test_list('test_verifier_repo_dir')
47             self.assertTrue(m.called)
48
49     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.error')
50     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.debug')
51     def test_generate_test_list_custom_mode_missing_file(self,
52                                                          mock_logger_debug,
53                                                          mock_logger_error):
54         self.tempestcommon.MODE = 'custom'
55         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
56                         'os.path.isfile', return_value=False), \
57                 self.assertRaises(Exception) as context:
58             msg = "Tempest test list file %s NOT found."
59             self.tempestcommon.generate_test_list('test_verifier_repo_dir')
60             self.assertTrue((msg % conf_utils.TEMPEST_CUSTOM) in context)
61
62     def test_generate_test_list_custom_mode_default(self):
63         self.tempestcommon.MODE = 'custom'
64         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
65                         'shutil.copyfile') as m, \
66             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
67                        'os.path.isfile', return_value=True):
68             self.tempestcommon.generate_test_list('test_verifier_repo_dir')
69             self.assertTrue(m.called)
70
71     def _test_generate_test_list_mode_default(self, mode):
72         self.tempestcommon.MODE = mode
73         if self.tempestcommon.MODE == 'smoke':
74             testr_mode = "smoke"
75         elif self.tempestcommon.MODE == 'full':
76             testr_mode = ""
77         else:
78             testr_mode = 'tempest.api.' + self.tempestcommon.MODE
79         conf_utils.TEMPEST_RAW_LIST = 'raw_list'
80         verifier_repo_dir = 'test_verifier_repo_dir'
81         with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
82                         'ft_utils.execute_command') as m:
83             cmd = ("cd {0};"
84                    "testr list-tests {1} > {2};"
85                    "cd -;".format(verifier_repo_dir,
86                                   testr_mode,
87                                   conf_utils.TEMPEST_RAW_LIST))
88             self.tempestcommon.generate_test_list('test_verifier_repo_dir')
89             m.assert_any_call(cmd)
90
91     def test_generate_test_list_smoke_mode(self):
92         self._test_generate_test_list_mode_default('smoke')
93
94     def test_generate_test_list_full_mode(self):
95         self._test_generate_test_list_mode_default('full')
96
97     def test_parse_verifier_result_missing_verification_uuid(self):
98         self.tempestcommon.VERIFICATION_ID = None
99         with self.assertRaises(Exception):
100             self.tempestcommon.parse_verifier_result()
101
102     def test_apply_tempest_blacklist_no_blacklist(self):
103         with mock.patch('__builtin__.open', mock.mock_open()) as m, \
104             mock.patch.object(self.tempestcommon, 'read_file',
105                               return_value=['test1', 'test2']):
106             conf_utils.TEMPEST_BLACKLIST = Exception
107             CONST.__setattr__('INSTALLER_TYPE', 'installer_type')
108             CONST.__setattr__('DEPLOY_SCENARIO', 'deploy_scenario')
109             self.tempestcommon.apply_tempest_blacklist()
110             obj = m()
111             obj.write.assert_any_call('test1\n')
112             obj.write.assert_any_call('test2\n')
113
114     def test_apply_tempest_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 m, \
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             CONST.__setattr__('INSTALLER_TYPE', 'installer_type')
124             CONST.__setattr__('DEPLOY_SCENARIO', 'deploy_scenario')
125             self.tempestcommon.apply_tempest_blacklist()
126             obj = m()
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=['\} tempest\.']), \
134             mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
135                        'subprocess.Popen'):
136             conf_utils.TEMPEST_LIST = 'test_tempest_list'
137             cmd_line = ("rally verify start  --load-list "
138                         "test_tempest_list --detailed")
139             self.tempestcommon.run_verifier_tests()
140             mock_logger_info. \
141                 assert_any_call("Starting Tempest test suite: '%s'."
142                                 % cmd_line)
143
144     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
145                 'os.path.exists', return_value=False)
146     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs',
147                 side_effect=Exception)
148     def test_run_makedirs_ko(self, *args):
149         self.assertEqual(self.tempestcommon.run(),
150                          testcase.TestCase.EX_RUN_ERROR)
151
152     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
153                 'os.path.exists', return_value=False)
154     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
155     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
156                 'conf_utils.create_tempest_resources', side_effect=Exception)
157     def test_run_create_tempest_resources_ko(self, *args):
158         self.assertEqual(self.tempestcommon.run(),
159                          testcase.TestCase.EX_RUN_ERROR)
160
161     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
162                 'os.path.exists', return_value=False)
163     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.os.makedirs')
164     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
165                 'conf_utils.create_tempest_resources', return_value={})
166     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
167                 'conf_utils.configure_tempest', side_effect=Exception)
168     def test_run_configure_tempest_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                 'conf_utils.create_tempest_resources', return_value={})
177     @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.'
178                 'conf_utils.configure_tempest')
179     def _test_run(self, status, *args):
180         self.assertEqual(self.tempestcommon.run(), status)
181
182     def test_run_missing_generate_test_list(self):
183         with mock.patch.object(self.tempestcommon, 'generate_test_list',
184                                side_effect=Exception):
185             self._test_run(testcase.TestCase.EX_RUN_ERROR)
186
187     def test_run_apply_tempest_blacklist_ko(self):
188         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
189                     mock.patch.object(self.tempestcommon,
190                                       'apply_tempest_blacklist',
191                                       side_effect=Exception()):
192             self._test_run(testcase.TestCase.EX_RUN_ERROR)
193
194     def test_run_verifier_tests_ko(self, *args):
195         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
196                 mock.patch.object(self.tempestcommon,
197                                   'apply_tempest_blacklist'), \
198                 mock.patch.object(self.tempestcommon, 'run_verifier_tests',
199                                   side_effect=Exception()), \
200                 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
201                                   side_effect=Exception):
202             self._test_run(testcase.TestCase.EX_RUN_ERROR)
203
204     def test_run_parse_verifier_result_ko(self, *args):
205         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
206                 mock.patch.object(self.tempestcommon,
207                                   'apply_tempest_blacklist'), \
208                 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
209                 mock.patch.object(self.tempestcommon, 'parse_verifier_result',
210                                   side_effect=Exception):
211             self._test_run(testcase.TestCase.EX_RUN_ERROR)
212
213     def test_run(self, *args):
214         with mock.patch.object(self.tempestcommon, 'generate_test_list'), \
215                 mock.patch.object(self.tempestcommon,
216                                   'apply_tempest_blacklist'), \
217                 mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \
218                 mock.patch.object(self.tempestcommon, 'parse_verifier_result'):
219             self._test_run(testcase.TestCase.EX_OK)
220
221
222 if __name__ == "__main__":
223     logging.disable(logging.CRITICAL)
224     unittest.main(verbosity=2)