More Unit Tests for utils module
[functest.git] / functest / tests / unit / ci / test_run_tests.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
9 import unittest
10 import logging
11
12 import mock
13
14 from functest.ci import run_tests
15 from functest.utils.constants import CONST
16
17
18 class RunTestsTesting(unittest.TestCase):
19
20     logging.disable(logging.CRITICAL)
21
22     def setUp(self):
23         self.sep = 'test_sep'
24         self.creds = {'OS_AUTH_URL': 'http://test_ip:test_port/v2.0',
25                       'OS_USERNAME': 'test_os_username',
26                       'OS_TENANT_NAME': 'test_tenant',
27                       'OS_PASSWORD': 'test_password'}
28         self.test = {'test_name': 'test_name'}
29         self.tier = mock.Mock()
30         attrs = {'get_name.return_value': 'test_tier',
31                  'get_tests.return_value': ['test1', 'test2'],
32                  'get_ci_loop.return_value': 'test_ci_loop',
33                  'get_test_names.return_value': ['test1', 'test2']}
34         self.tier.configure_mock(**attrs)
35
36         self.tiers = mock.Mock()
37         attrs = {'get_tiers.return_value': [self.tier]}
38         self.tiers.configure_mock(**attrs)
39
40     @mock.patch('functest.ci.run_tests.logger.info')
41     def test_print_separator(self, mock_logger_info):
42         run_tests.print_separator(self.sep)
43         mock_logger_info.assert_called_once_with(self.sep * 44)
44
45     @mock.patch('functest.ci.run_tests.logger.error')
46     def test_source_rc_file_missing_file(self, mock_logger_error):
47         with mock.patch('functest.ci.run_tests.os.path.isfile',
48                         return_value=False), \
49                 self.assertRaises(Exception):
50             run_tests.source_rc_file()
51
52     @mock.patch('functest.ci.run_tests.logger.debug')
53     def test_source_rc_file_default(self, mock_logger_debug):
54         with mock.patch('functest.ci.run_tests.os.path.isfile',
55                         return_value=True), \
56             mock.patch('functest.ci.run_tests.os_utils.source_credentials',
57                        return_value=self.creds):
58             run_tests.source_rc_file()
59
60     @mock.patch('functest.ci.run_tests.os_snapshot.main')
61     def test_generate_os_snapshot(self, mock_os_snap):
62             run_tests.generate_os_snapshot()
63             self.assertTrue(mock_os_snap.called)
64
65     @mock.patch('functest.ci.run_tests.os_clean.main')
66     def test_cleanup(self, mock_os_clean):
67             run_tests.cleanup()
68             self.assertTrue(mock_os_clean.called)
69
70     def test_update_test_info(self):
71         run_tests.GlobalVariables.EXECUTED_TEST_CASES = [self.test]
72         run_tests.update_test_info('test_name',
73                                    'test_result',
74                                    'test_duration')
75         exp = self.test
76         exp.update({"result": 'test_result',
77                     "duration": 'test_duration'})
78         self.assertEqual(run_tests.GlobalVariables.EXECUTED_TEST_CASES,
79                          [exp])
80
81     def test_get_run_dict_if_defined_default(self):
82         mock_obj = mock.Mock()
83         with mock.patch('functest.ci.run_tests.'
84                         'ft_utils.get_dict_by_test',
85                         return_value={'run': mock_obj}):
86             self.assertEqual(run_tests.get_run_dict('test_name'),
87                              mock_obj)
88
89     @mock.patch('functest.ci.run_tests.logger.error')
90     def test_get_run_dict_if_defined_missing_config_option(self,
91                                                            mock_logger_error):
92         with mock.patch('functest.ci.run_tests.'
93                         'ft_utils.get_dict_by_test',
94                         return_value=None):
95             testname = 'test_name'
96             self.assertEqual(run_tests.get_run_dict(testname),
97                              None)
98             mock_logger_error.assert_called_once_with("Cannot get {}'s config "
99                                                       "options"
100                                                       .format(testname))
101
102         with mock.patch('functest.ci.run_tests.'
103                         'ft_utils.get_dict_by_test',
104                         return_value={}):
105             testname = 'test_name'
106             self.assertEqual(run_tests.get_run_dict(testname),
107                              None)
108
109     @mock.patch('functest.ci.run_tests.logger.exception')
110     def test_get_run_dict_if_defined_exception(self,
111                                                mock_logger_except):
112         with mock.patch('functest.ci.run_tests.'
113                         'ft_utils.get_dict_by_test',
114                         side_effect=Exception):
115             testname = 'test_name'
116             self.assertEqual(run_tests.get_run_dict(testname),
117                              None)
118             mock_logger_except.assert_called_once_with("Cannot get {}'s config"
119                                                        " options"
120                                                        .format(testname))
121
122     def test_run_tests_import_test_class_exception(self):
123         mock_test = mock.Mock()
124         args = {'get_name': 'test_name',
125                 'needs_clean': False}
126         mock_test.configure_mock(**args)
127         with mock.patch('functest.ci.run_tests.print_separator'),\
128             mock.patch('functest.ci.run_tests.source_rc_file'), \
129             mock.patch('functest.ci.run_tests.get_run_dict',
130                        return_value=None), \
131                 self.assertRaises(Exception) as context:
132             run_tests.run_test(mock_test, 'tier_name')
133             msg = "Cannot import the class for the test case."
134             self.assertTrue(msg in context)
135
136     @mock.patch('functest.ci.run_tests.logger.info')
137     def test_run_tier_default(self, mock_logger_info):
138         with mock.patch('functest.ci.run_tests.print_separator'), \
139                 mock.patch('functest.ci.run_tests.run_test') as mock_method:
140             run_tests.run_tier(self.tier)
141             mock_method.assert_any_call('test1', 'test_tier')
142             mock_method.assert_any_call('test2', 'test_tier')
143             self.assertTrue(mock_logger_info.called)
144
145     @mock.patch('functest.ci.run_tests.logger.info')
146     def test_run_tier_missing_test(self, mock_logger_info):
147         with mock.patch('functest.ci.run_tests.print_separator'):
148             self.tier.get_tests.return_value = None
149             self.assertEqual(run_tests.run_tier(self.tier), 0)
150             self.assertTrue(mock_logger_info.called)
151
152     @mock.patch('functest.ci.run_tests.logger.info')
153     def test_run_all_default(self, mock_logger_info):
154         with mock.patch('functest.ci.run_tests.run_tier') as mock_method, \
155             mock.patch('functest.ci.run_tests.generate_report.init'), \
156                 mock.patch('functest.ci.run_tests.generate_report.main'):
157             CONST.CI_LOOP = 'test_ci_loop'
158             run_tests.run_all(self.tiers)
159             mock_method.assert_any_call(self.tier)
160             self.assertTrue(mock_logger_info.called)
161
162     @mock.patch('functest.ci.run_tests.logger.info')
163     def test_run_all__missing_tier(self, mock_logger_info):
164         with mock.patch('functest.ci.run_tests.generate_report.init'), \
165                 mock.patch('functest.ci.run_tests.generate_report.main'):
166             CONST.CI_LOOP = 'loop_re_not_available'
167             run_tests.run_all(self.tiers)
168             self.assertTrue(mock_logger_info.called)
169
170     def test_main_failed(self):
171         kwargs = {'test': 'test_name', 'noclean': True, 'report': True}
172         mock_obj = mock.Mock()
173         args = {'get_tier.return_value': False,
174                 'get_test.return_value': False}
175         mock_obj.configure_mock(**args)
176
177         with mock.patch('functest.ci.run_tests.tb.TierBuilder'), \
178             mock.patch('functest.ci.run_tests.source_rc_file',
179                        side_effect=Exception):
180             self.assertEqual(run_tests.main(**kwargs),
181                              run_tests.Result.EX_ERROR)
182
183         with mock.patch('functest.ci.run_tests.tb.TierBuilder',
184                         return_value=mock_obj), \
185             mock.patch('functest.ci.run_tests.source_rc_file',
186                        side_effect=Exception):
187             self.assertEqual(run_tests.main(**kwargs),
188                              run_tests.Result.EX_ERROR)
189
190
191 if __name__ == "__main__":
192     unittest.main(verbosity=2)