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
13 from functest.ci import prepare_env
14 from functest.tests.unit import test_utils
15 from functest.utils.constants import CONST
16 from opnfv.utils import constants as opnfv_constants
19 class PrepareEnvTesting(unittest.TestCase):
21 logging.disable(logging.CRITICAL)
23 @mock.patch('functest.ci.prepare_env.logger.info')
24 def test_print_separator(self, mock_logger_info):
25 str = "=============================================="
26 prepare_env.print_separator()
27 mock_logger_info.assert_called_once_with(str)
29 @mock.patch('functest.ci.prepare_env.logger.info')
30 @mock.patch('functest.ci.prepare_env.logger.warning')
31 def test_check_env_variables_missing_inst_type(self, mock_logger_warn,
33 CONST.INSTALLER_TYPE = None
34 prepare_env.check_env_variables()
35 mock_logger_info.assert_any_call("Checking environment variables"
37 mock_logger_warn.assert_any_call("The env variable 'INSTALLER_TYPE'"
40 @mock.patch('functest.ci.prepare_env.logger.info')
41 @mock.patch('functest.ci.prepare_env.logger.warning')
42 def test_check_env_variables_missing_inst_ip(self, mock_logger_warn,
44 CONST.INSTALLER_IP = None
45 prepare_env.check_env_variables()
46 mock_logger_info.assert_any_call("Checking environment variables"
48 mock_logger_warn.assert_any_call("The env variable 'INSTALLER_IP'"
49 " is not defined. It is needed to"
50 " fetch the OpenStack credentials."
51 " If the credentials are not"
52 " provided to the container as a"
53 " volume, please add this env"
54 " variable to the 'docker run'"
57 @mock.patch('functest.ci.prepare_env.logger.info')
58 @mock.patch('functest.ci.prepare_env.logger.warning')
59 def test_check_env_variables_with_inst_ip(self, mock_logger_warn,
61 CONST.INSTALLER_IP = mock.Mock()
62 prepare_env.check_env_variables()
63 mock_logger_info.assert_any_call("Checking environment variables"
65 mock_logger_info.assert_any_call(test_utils.
66 SubstrMatch(" INSTALLER_IP="))
68 @mock.patch('functest.ci.prepare_env.logger.info')
69 @mock.patch('functest.ci.prepare_env.logger.warning')
70 def test_check_env_variables_missing_scenario(self, mock_logger_warn,
72 CONST.DEPLOY_SCENARIO = None
73 prepare_env.check_env_variables()
74 mock_logger_info.assert_any_call("Checking environment variables"
76 mock_logger_warn.assert_any_call("The env variable"
77 " 'DEPLOY_SCENARIO' is not defined"
78 ". Setting CI_SCENARIO=undefined.")
80 @mock.patch('functest.ci.prepare_env.logger.info')
81 @mock.patch('functest.ci.prepare_env.logger.warning')
82 def test_check_env_variables_with_scenario(self, mock_logger_warn,
84 CONST.DEPLOY_SCENARIO = mock.Mock()
85 prepare_env.check_env_variables()
86 mock_logger_info.assert_any_call("Checking environment variables"
88 mock_logger_info.assert_any_call(test_utils.
89 SubstrMatch("DEPLOY_SCENARIO="))
91 @mock.patch('functest.ci.prepare_env.logger.info')
92 @mock.patch('functest.ci.prepare_env.logger.warning')
93 def test_check_env_variables_with_ci_debug(self, mock_logger_warn,
95 CONST.CI_DEBUG = mock.Mock()
96 prepare_env.check_env_variables()
97 mock_logger_info.assert_any_call("Checking environment variables"
99 mock_logger_info.assert_any_call(test_utils.
100 SubstrMatch(" CI_DEBUG="))
102 @mock.patch('functest.ci.prepare_env.logger.info')
103 @mock.patch('functest.ci.prepare_env.logger.warning')
104 def test_check_env_variables_with_node(self, mock_logger_warn,
106 CONST.NODE_NAME = mock.Mock()
107 prepare_env.check_env_variables()
108 mock_logger_info.assert_any_call("Checking environment variables"
110 mock_logger_info.assert_any_call(test_utils.
111 SubstrMatch(" NODE_NAME="))
113 @mock.patch('functest.ci.prepare_env.logger.info')
114 @mock.patch('functest.ci.prepare_env.logger.warning')
115 def test_check_env_variables_with_build_tag(self, mock_logger_warn,
117 CONST.BUILD_TAG = mock.Mock()
118 prepare_env.check_env_variables()
119 mock_logger_info.assert_any_call("Checking environment variables"
122 mock_logger_info.assert_any_call(test_utils.
123 SubstrMatch(" BUILD_TAG="))
125 @mock.patch('functest.ci.prepare_env.logger.info')
126 @mock.patch('functest.ci.prepare_env.logger.warning')
127 def test_check_env_variables_with_is_ci_run(self, mock_logger_warn,
129 CONST.IS_CI_RUN = mock.Mock()
130 prepare_env.check_env_variables()
131 mock_logger_info.assert_any_call("Checking environment variables"
134 mock_logger_info.assert_any_call(test_utils.
135 SubstrMatch(" IS_CI_RUN="))
137 @mock.patch('functest.ci.prepare_env.logger.info')
138 @mock.patch('functest.ci.prepare_env.logger.debug')
139 def test_create_directories_missing_dir(self, mock_logger_debug,
141 with mock.patch('functest.ci.prepare_env.os.path.exists',
142 return_value=False), \
143 mock.patch('functest.ci.prepare_env.os.makedirs') \
145 prepare_env.create_directories()
146 mock_logger_info.assert_any_call("Creating needed directories...")
147 mock_method.assert_any_call(CONST.dir_functest_conf)
148 mock_method.assert_any_call(CONST.dir_functest_data)
149 mock_logger_info.assert_any_call(" %s created." %
150 CONST.dir_functest_conf)
151 mock_logger_info.assert_any_call(" %s created." %
152 CONST.dir_functest_data)
154 @mock.patch('functest.ci.prepare_env.logger.info')
155 @mock.patch('functest.ci.prepare_env.logger.debug')
156 def test_create_directories_with_dir(self, mock_logger_debug,
158 with mock.patch('functest.ci.prepare_env.os.path.exists',
160 prepare_env.create_directories()
161 mock_logger_info.assert_any_call("Creating needed directories...")
162 mock_logger_debug.assert_any_call(" %s already exists." %
163 CONST.dir_functest_conf)
164 mock_logger_debug.assert_any_call(" %s already exists." %
165 CONST.dir_functest_data)
167 def _get_env_cred_dict(self, os_prefix=''):
168 return {'OS_USERNAME': os_prefix + 'username',
169 'OS_PASSWORD': os_prefix + 'password',
170 'OS_AUTH_URL': 'http://test_ip:test_port/v2.0',
171 'OS_TENANT_NAME': os_prefix + 'tenant_name',
172 'OS_USER_DOMAIN_NAME': os_prefix + 'user_domain_name',
173 'OS_PROJECT_DOMAIN_NAME': os_prefix + 'project_domain_name',
174 'OS_PROJECT_NAME': os_prefix + 'project_name',
175 'OS_ENDPOINT_TYPE': os_prefix + 'endpoint_type',
176 'OS_REGION_NAME': os_prefix + 'region_name'}
178 @mock.patch('functest.ci.prepare_env.logger.error')
179 @mock.patch('functest.ci.prepare_env.logger.info')
180 @mock.patch('functest.ci.prepare_env.logger.warning')
181 def test_source_rc_missing_rc_file(self, mock_logger_warn,
184 with mock.patch('functest.ci.prepare_env.os.path.isfile',
185 return_value=True), \
186 mock.patch('functest.ci.prepare_env.os.path.getsize',
188 self.assertRaises(Exception):
189 CONST.openstack_creds = 'test_creds'
190 prepare_env.source_rc_file()
192 def test_source_rc_missing_installer_ip(self):
193 with mock.patch('functest.ci.prepare_env.os.path.isfile',
194 return_value=False), \
195 self.assertRaises(Exception):
196 CONST.INSTALLER_IP = None
197 CONST.openstack_creds = 'test_creds'
198 prepare_env.source_rc_file()
200 def test_source_rc_missing_installer_type(self):
201 with mock.patch('functest.ci.prepare_env.os.path.isfile',
202 return_value=False), \
203 self.assertRaises(Exception):
204 CONST.INSTALLER_IP = 'test_ip'
205 CONST.openstack_creds = 'test_creds'
206 CONST.INSTALLER_TYPE = 'test_type'
207 opnfv_constants.INSTALLERS = []
208 prepare_env.source_rc_file()
210 def test_source_rc_missing_os_credfile_ci_inst(self):
211 with mock.patch('functest.ci.prepare_env.os.path.isfile',
212 return_value=False), \
213 mock.patch('functest.ci.prepare_env.os.path.getsize'), \
214 mock.patch('functest.ci.prepare_env.os.path.join'), \
215 mock.patch('functest.ci.prepare_env.subprocess.Popen') \
216 as mock_subproc_popen, \
217 self.assertRaises(Exception):
218 CONST.openstack_creds = 'test_creds'
219 CONST.INSTALLER_IP = None
220 CONST.INSTALLER_TYPE = 'test_type'
221 opnfv_constants.INSTALLERS = ['test_type']
223 process_mock = mock.Mock()
224 attrs = {'communicate.return_value': ('output', 'error'),
226 process_mock.configure_mock(**attrs)
227 mock_subproc_popen.return_value = process_mock
229 prepare_env.source_rc_file()
231 def _get_rally_creds(self):
232 return {"type": "ExistingCloud",
233 "admin": {"username": 'test_user_name',
234 "password": 'test_password',
235 "tenant": 'test_tenant'}}
237 @mock.patch('functest.ci.prepare_env.os_utils.get_credentials_for_rally')
238 @mock.patch('functest.ci.prepare_env.logger.info')
239 @mock.patch('functest.ci.prepare_env.ft_utils.execute_command_raise')
240 @mock.patch('functest.ci.prepare_env.ft_utils.execute_command')
241 def test_install_rally(self, mock_exec, mock_exec_raise, mock_logger_info,
244 mock_os_utils.return_value = self._get_rally_creds()
246 prepare_env.install_rally()
248 cmd = "rally deployment destroy opnfv-rally"
249 error_msg = "Deployment %s does not exist." % \
250 CONST.rally_deployment_name
251 mock_logger_info.assert_any_call("Creating Rally environment...")
252 mock_exec.assert_any_call(cmd, error_msg=error_msg, verbose=False)
254 cmd = "rally deployment create --file=rally_conf.json --name="
255 cmd += CONST.rally_deployment_name
256 error_msg = "Problem while creating Rally deployment"
257 mock_exec_raise.assert_any_call(cmd, error_msg=error_msg)
259 cmd = "rally deployment check"
260 error_msg = ("OpenStack not responding or "
261 "faulty Rally deployment.")
262 mock_exec_raise.assert_any_call(cmd, error_msg=error_msg)
264 cmd = "rally deployment list"
265 error_msg = ("Problem while listing "
267 mock_exec.assert_any_call(cmd, error_msg=error_msg)
269 cmd = "rally plugin list | head -5"
270 error_msg = ("Problem while showing "
272 mock_exec.assert_any_call(cmd, error_msg=error_msg)
274 @mock.patch('functest.ci.prepare_env.sys.exit')
275 @mock.patch('functest.ci.prepare_env.logger.error')
276 def test_check_environment_missing_file(self, mock_logger_error,
278 with mock.patch('functest.ci.prepare_env.os.path.isfile',
279 return_value=False), \
280 self.assertRaises(Exception):
281 prepare_env.check_environment()
283 @mock.patch('functest.ci.prepare_env.sys.exit')
284 @mock.patch('functest.ci.prepare_env.logger.error')
285 def test_check_environment_with_error(self, mock_logger_error,
287 with mock.patch('functest.ci.prepare_env.os.path.isfile',
288 return_value=True), \
289 mock.patch("__builtin__.open", mock.mock_open(read_data='0')), \
290 self.assertRaises(Exception):
291 prepare_env.check_environment()
293 @mock.patch('functest.ci.prepare_env.logger.info')
294 def test_check_environment_default(self, mock_logger_info):
295 with mock.patch('functest.ci.prepare_env.os.path.isfile',
297 with mock.patch("__builtin__.open", mock.mock_open(read_data='1')):
298 prepare_env.check_environment()
299 mock_logger_info.assert_any_call("Functest environment"
302 @mock.patch('functest.ci.prepare_env.check_environment')
303 @mock.patch('functest.ci.prepare_env.create_flavor')
304 @mock.patch('functest.ci.prepare_env.install_tempest')
305 @mock.patch('functest.ci.prepare_env.install_rally')
306 @mock.patch('functest.ci.prepare_env.verify_deployment')
307 @mock.patch('functest.ci.prepare_env.patch_config_file')
308 @mock.patch('functest.ci.prepare_env.source_rc_file')
309 @mock.patch('functest.ci.prepare_env.create_directories')
310 @mock.patch('functest.ci.prepare_env.check_env_variables')
311 @mock.patch('functest.ci.prepare_env.logger.info')
312 def test_main_start(self, mock_logger_info, mock_env_var,
313 mock_create_dir, mock_source_rc, mock_patch_config,
314 mock_verify_depl, mock_install_rally,
315 mock_install_temp, mock_create_flavor,
317 with mock.patch("__builtin__.open", mock.mock_open()) as m:
318 args = {'action': 'start'}
319 self.assertEqual(prepare_env.main(**args), 0)
320 mock_logger_info.assert_any_call("######### Preparing Functest "
321 "environment #########\n")
322 self.assertTrue(mock_env_var.called)
323 self.assertTrue(mock_create_dir.called)
324 self.assertTrue(mock_source_rc.called)
325 self.assertTrue(mock_patch_config.called)
326 self.assertTrue(mock_verify_depl.called)
327 self.assertTrue(mock_install_rally.called)
328 self.assertTrue(mock_install_temp.called)
329 self.assertTrue(mock_create_flavor.called)
330 m.assert_called_once_with(CONST.env_active, "w")
331 self.assertTrue(mock_check_env.called)
333 @mock.patch('functest.ci.prepare_env.check_environment')
334 def test_main_check(self, mock_check_env):
335 args = {'action': 'check'}
336 self.assertEqual(prepare_env.main(**args), 0)
337 self.assertTrue(mock_check_env.called)
339 @mock.patch('functest.ci.prepare_env.logger.error')
340 def test_main_no_arg(self, mock_logger_error):
341 args = {'action': 'not_valid'}
342 self.assertEqual(prepare_env.main(**args), -1)
343 mock_logger_error.assert_called_once_with('Argument not valid.')
346 if __name__ == "__main__":
347 unittest.main(verbosity=2)