3 # Copyright (c) 2016 Orange and others.
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
10 # pylint: disable=missing-docstring
19 from functest.utils import functest_utils
22 class FunctestUtilsTesting(unittest.TestCase):
25 test_ip = ['10.1.23.4', '10.1.14.15', '10.1.16.15']
28 self.url = 'http://www.opnfv.org/'
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'
45 self.start_date = 1482624000
46 self.stop_date = 1482624000
47 self.start_time = time.time()
48 self.stop_time = time.time()
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'
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':
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}
74 if FunctestUtilsTesting.readline == \
75 len(FunctestUtilsTesting.test_ip) - 1:
77 FunctestUtilsTesting.readline += 1
78 return FunctestUtilsTesting.test_ip[FunctestUtilsTesting.readline]
80 def _get_environ(self, var, *args): # pylint: disable=unused-argument
81 if var == 'INSTALLER_TYPE':
83 elif var == 'DEPLOY_SCENARIO':
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,
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:
100 FunctestUtilsTesting.readline = 0
102 mock_obj = mock.Mock()
103 attrs = {'readline.side_effect': self.cmd_readline()}
104 mock_obj.configure_mock(**attrs)
106 mock_obj2 = mock.Mock()
107 attrs = {'stdout': mock_obj, 'wait.return_value': 1}
108 mock_obj2.configure_mock(**attrs)
110 mock_subproc_open.return_value = mock_obj2
112 resp = functest_utils.execute_command(self.cmd, info=True,
113 error_msg=self.error_msg,
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)
122 @mock.patch('functest.utils.functest_utils.LOGGER.info')
123 def test_execute_command_args_present_with_success(self, mock_logger_info,
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:
130 FunctestUtilsTesting.readline = 0
132 mock_obj = mock.Mock()
133 attrs = {'readline.side_effect': self.cmd_readline()}
134 mock_obj.configure_mock(**attrs)
136 mock_obj2 = mock.Mock()
137 attrs = {'stdout': mock_obj, 'wait.return_value': 0}
138 mock_obj2.configure_mock(**attrs)
140 mock_subproc_open.return_value = mock_obj2
142 resp = functest_utils.execute_command(self.cmd, info=True,
143 error_msg=self.error_msg,
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")
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:
156 FunctestUtilsTesting.readline = 2
158 mock_obj = mock.Mock()
159 attrs = {'readline.side_effect': self.cmd_readline()}
160 mock_obj.configure_mock(**attrs)
162 mock_obj2 = mock.Mock()
163 attrs = {'stdout': mock_obj, 'wait.return_value': 0}
164 mock_obj2.configure_mock(**attrs)
166 mock_subproc_open.return_value = mock_obj2
168 resp = functest_utils.execute_command(self.cmd, info=False,
172 self.assertEqual(resp, 0)
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:
179 FunctestUtilsTesting.readline = 2
180 mock_obj = mock.Mock()
181 attrs = {'readline.side_effect': self.cmd_readline()}
182 mock_obj.configure_mock(**attrs)
184 mock_obj2 = mock.Mock()
185 attrs = {'stdout': mock_obj, 'wait.return_value': 1}
186 mock_obj2.configure_mock(**attrs)
188 mock_subproc_open.return_value = mock_obj2
190 resp = functest_utils.execute_command(self.cmd, info=False,
194 self.assertEqual(resp, 1)
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') \
201 self.assertRaises(ValueError) as excep:
202 mock_yaml.return_value = self.file_yaml
203 functest_utils.get_parameter_from_yaml(self.parameter,
205 self.assertTrue(("The parameter %s is not"
206 " defined in config_functest.yaml" %
207 self.parameter) in excep.exception)
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') \
213 mock_yaml.return_value = self.file_yaml
214 self.assertEqual(functest_utils.
215 get_parameter_from_yaml(self.parameter,
220 if __name__ == "__main__":
221 logging.disable(logging.CRITICAL)
222 unittest.main(verbosity=2)