Remove obsolete Functest utils
[functest.git] / functest / tests / unit / utils / test_functest_utils.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016 Orange and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # pylint: disable=missing-docstring
11
12 import logging
13 import time
14 import unittest
15
16 import mock
17 import pkg_resources
18
19 from functest.utils import functest_utils
20
21
22 class FunctestUtilsTesting(unittest.TestCase):
23
24     readline = 0
25     test_ip = ['10.1.23.4', '10.1.14.15', '10.1.16.15']
26
27     def setUp(self):
28         self.url = 'http://www.opnfv.org/'
29         self.timeout = 5
30         self.dest_path = 'test_path'
31         self.repo_path = 'test_repo_path'
32         self.installer = 'test_installer'
33         self.scenario = 'test_scenario'
34         self.build_tag = 'jenkins-functest-fuel-opnfv-jump-2-daily-master-190'
35         self.build_tag_week = 'jenkins-functest-fuel-baremetal-weekly-master-8'
36         self.version = 'master'
37         self.node_name = 'test_node_name'
38         self.project = 'test_project'
39         self.case_name = 'test_case_name'
40         self.status = 'test_status'
41         self.details = 'test_details'
42         self.db_url = 'test_db_url'
43         self.criteria = 50
44         self.result = 75
45         self.start_date = 1482624000
46         self.stop_date = 1482624000
47         self.start_time = time.time()
48         self.stop_time = time.time()
49         self.readline = -1
50         self.test_ip = ['10.1.23.4', '10.1.14.15', '10.1.16.15']
51         self.test_file = 'test_file'
52         self.error_msg = 'test_error_msg'
53         self.cmd = 'test_cmd'
54         self.output_file = 'test_output_file'
55         self.testname = 'testname'
56         self.parameter = 'general.openstack.image_name'
57         self.config_yaml = pkg_resources.resource_filename(
58             'functest', 'ci/config_functest.yaml')
59         self.db_url_env = 'http://foo/testdb'
60         self.testcases_yaml = "test_testcases_yaml"
61         self.file_yaml = {'general': {'openstack': {'image_name':
62                                                     'test_image_name'}}}
63
64     def _get_env_dict(self, var):
65         dic = {'INSTALLER_TYPE': self.installer,
66                'DEPLOY_SCENARIO': self.scenario,
67                'NODE_NAME': self.node_name,
68                'BUILD_TAG': self.build_tag}
69         dic.pop(var, None)
70         return dic
71
72     @staticmethod
73     def readline_side():
74         if FunctestUtilsTesting.readline == \
75                 len(FunctestUtilsTesting.test_ip) - 1:
76             return False
77         FunctestUtilsTesting.readline += 1
78         return FunctestUtilsTesting.test_ip[FunctestUtilsTesting.readline]
79
80     def _get_environ(self, var, *args):  # pylint: disable=unused-argument
81         if var == 'INSTALLER_TYPE':
82             return self.installer
83         elif var == 'DEPLOY_SCENARIO':
84             return self.scenario
85         return var
86
87     @staticmethod
88     def cmd_readline():
89         return 'test_value\n'
90
91     @mock.patch('functest.utils.functest_utils.LOGGER.error')
92     @mock.patch('functest.utils.functest_utils.LOGGER.info')
93     def test_execute_command_args_present_with_error(self, mock_logger_info,
94                                                      mock_logger_error):
95         with mock.patch('functest.utils.functest_utils.subprocess.Popen') \
96                 as mock_subproc_open, \
97                 mock.patch('six.moves.builtins.open',
98                            mock.mock_open()) as mopen:
99
100             FunctestUtilsTesting.readline = 0
101
102             mock_obj = mock.Mock()
103             attrs = {'readline.side_effect': self.cmd_readline()}
104             mock_obj.configure_mock(**attrs)
105
106             mock_obj2 = mock.Mock()
107             attrs = {'stdout': mock_obj, 'wait.return_value': 1}
108             mock_obj2.configure_mock(**attrs)
109
110             mock_subproc_open.return_value = mock_obj2
111
112             resp = functest_utils.execute_command(self.cmd, info=True,
113                                                   error_msg=self.error_msg,
114                                                   verbose=True,
115                                                   output_file=self.output_file)
116             self.assertEqual(resp, 1)
117             msg_exec = ("Executing command: '%s'" % self.cmd)
118             mock_logger_info.assert_called_once_with(msg_exec)
119             mopen.assert_called_once_with(self.output_file, "w")
120             mock_logger_error.assert_called_once_with(self.error_msg)
121
122     @mock.patch('functest.utils.functest_utils.LOGGER.info')
123     def test_execute_command_args_present_with_success(self, mock_logger_info,
124                                                        ):
125         with mock.patch('functest.utils.functest_utils.subprocess.Popen') \
126                 as mock_subproc_open, \
127                 mock.patch('six.moves.builtins.open',
128                            mock.mock_open()) as mopen:
129
130             FunctestUtilsTesting.readline = 0
131
132             mock_obj = mock.Mock()
133             attrs = {'readline.side_effect': self.cmd_readline()}
134             mock_obj.configure_mock(**attrs)
135
136             mock_obj2 = mock.Mock()
137             attrs = {'stdout': mock_obj, 'wait.return_value': 0}
138             mock_obj2.configure_mock(**attrs)
139
140             mock_subproc_open.return_value = mock_obj2
141
142             resp = functest_utils.execute_command(self.cmd, info=True,
143                                                   error_msg=self.error_msg,
144                                                   verbose=True,
145                                                   output_file=self.output_file)
146             self.assertEqual(resp, 0)
147             msg_exec = ("Executing command: '%s'" % self.cmd)
148             mock_logger_info.assert_called_once_with(msg_exec)
149             mopen.assert_called_once_with(self.output_file, "w")
150
151     @mock.patch('sys.stdout')
152     def test_execute_command_args_missing_with_success(self, stdout=None):
153         with mock.patch('functest.utils.functest_utils.subprocess.Popen') \
154                 as mock_subproc_open:
155
156             FunctestUtilsTesting.readline = 2
157
158             mock_obj = mock.Mock()
159             attrs = {'readline.side_effect': self.cmd_readline()}
160             mock_obj.configure_mock(**attrs)
161
162             mock_obj2 = mock.Mock()
163             attrs = {'stdout': mock_obj, 'wait.return_value': 0}
164             mock_obj2.configure_mock(**attrs)
165
166             mock_subproc_open.return_value = mock_obj2
167
168             resp = functest_utils.execute_command(self.cmd, info=False,
169                                                   error_msg="",
170                                                   verbose=False,
171                                                   output_file=None)
172             self.assertEqual(resp, 0)
173
174     @mock.patch('sys.stdout')
175     def test_execute_command_args_missing_with_error(self, stdout=None):
176         with mock.patch('functest.utils.functest_utils.subprocess.Popen') \
177                 as mock_subproc_open:
178
179             FunctestUtilsTesting.readline = 2
180             mock_obj = mock.Mock()
181             attrs = {'readline.side_effect': self.cmd_readline()}
182             mock_obj.configure_mock(**attrs)
183
184             mock_obj2 = mock.Mock()
185             attrs = {'stdout': mock_obj, 'wait.return_value': 1}
186             mock_obj2.configure_mock(**attrs)
187
188             mock_subproc_open.return_value = mock_obj2
189
190             resp = functest_utils.execute_command(self.cmd, info=False,
191                                                   error_msg="",
192                                                   verbose=False,
193                                                   output_file=None)
194             self.assertEqual(resp, 1)
195
196     def test_get_parameter_from_yaml_failed(self):
197         self.file_yaml['general'] = None
198         with mock.patch('six.moves.builtins.open', mock.mock_open()), \
199                 mock.patch('functest.utils.functest_utils.yaml.safe_load') \
200                 as mock_yaml, \
201                 self.assertRaises(ValueError) as excep:
202             mock_yaml.return_value = self.file_yaml
203             functest_utils.get_parameter_from_yaml(self.parameter,
204                                                    self.test_file)
205             self.assertTrue(("The parameter %s is not"
206                              " defined in config_functest.yaml" %
207                              self.parameter) in excep.exception)
208
209     def test_get_parameter_from_yaml_default(self):
210         with mock.patch('six.moves.builtins.open', mock.mock_open()), \
211                 mock.patch('functest.utils.functest_utils.yaml.safe_load') \
212                 as mock_yaml:
213             mock_yaml.return_value = self.file_yaml
214             self.assertEqual(functest_utils.
215                              get_parameter_from_yaml(self.parameter,
216                                                      self.test_file),
217                              'test_image_name')
218
219
220 if __name__ == "__main__":
221     logging.disable(logging.CRITICAL)
222     unittest.main(verbosity=2)