Modify how to disable logging in unit test.
[functest.git] / functest / tests / unit / ci / test_prepare_env.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.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
17
18
19 class PrepareEnvTesting(unittest.TestCase):
20
21     def setUp(self):
22         self.prepare_envparser = prepare_env.PrepareEnvParser()
23
24     @mock.patch('functest.ci.prepare_env.logger.info')
25     def test_print_separator(self, mock_logger_info):
26         str = "=============================================="
27         prepare_env.print_separator()
28         mock_logger_info.assert_called_once_with(str)
29
30     @mock.patch('functest.ci.prepare_env.logger.info')
31     @mock.patch('functest.ci.prepare_env.logger.warning')
32     def test_check_env_variables_missing_inst_type(self, mock_logger_warn,
33                                                    mock_logger_info):
34         CONST.__setattr__('INSTALLER_TYPE', None)
35         prepare_env.check_env_variables()
36         mock_logger_info.assert_any_call("Checking environment variables"
37                                          "...")
38         mock_logger_warn.assert_any_call("The env variable 'INSTALLER_TYPE'"
39                                          " is not defined.")
40
41     @mock.patch('functest.ci.prepare_env.logger.info')
42     @mock.patch('functest.ci.prepare_env.logger.warning')
43     def test_check_env_variables_missing_inst_ip(self, mock_logger_warn,
44                                                  mock_logger_info):
45         CONST.__setattr__('INSTALLER_IP', None)
46         prepare_env.check_env_variables()
47         mock_logger_info.assert_any_call("Checking environment variables"
48                                          "...")
49         mock_logger_warn.assert_any_call("The env variable 'INSTALLER_IP'"
50                                          " is not defined. It is needed to"
51                                          " fetch the OpenStack credentials."
52                                          " If the credentials are not"
53                                          " provided to the container as a"
54                                          " volume, please add this env"
55                                          " variable to the 'docker run'"
56                                          " command.")
57
58     @mock.patch('functest.ci.prepare_env.logger.info')
59     @mock.patch('functest.ci.prepare_env.logger.warning')
60     def test_check_env_variables_with_inst_ip(self, mock_logger_warn,
61                                               mock_logger_info):
62         CONST.__setattr__('INSTALLER_IP', mock.Mock())
63         prepare_env.check_env_variables()
64         mock_logger_info.assert_any_call("Checking environment variables"
65                                          "...")
66         mock_logger_info.assert_any_call(test_utils.
67                                          SubstrMatch("    INSTALLER_IP="))
68
69     @mock.patch('functest.ci.prepare_env.logger.info')
70     @mock.patch('functest.ci.prepare_env.logger.warning')
71     def test_check_env_variables_missing_scenario(self, mock_logger_warn,
72                                                   mock_logger_info):
73         CONST.__setattr__('DEPLOY_SCENARIO', None)
74         prepare_env.check_env_variables()
75         mock_logger_info.assert_any_call("Checking environment variables"
76                                          "...")
77         mock_logger_warn.assert_any_call("The env variable"
78                                          " 'DEPLOY_SCENARIO' is not defined"
79                                          ". Setting CI_SCENARIO=undefined.")
80
81     @mock.patch('functest.ci.prepare_env.logger.info')
82     @mock.patch('functest.ci.prepare_env.logger.warning')
83     def test_check_env_variables_with_scenario(self, mock_logger_warn,
84                                                mock_logger_info):
85         CONST.__setattr__('DEPLOY_SCENARIO', 'test_scenario')
86         prepare_env.check_env_variables()
87         mock_logger_info.assert_any_call("Checking environment variables"
88                                          "...")
89         mock_logger_info.assert_any_call(test_utils.
90                                          SubstrMatch("DEPLOY_SCENARIO="))
91
92     @mock.patch('functest.ci.prepare_env.logger.info')
93     @mock.patch('functest.ci.prepare_env.logger.warning')
94     def test_check_env_variables_with_ci_debug(self, mock_logger_warn,
95                                                mock_logger_info):
96         CONST.__setattr__('CI_DEBUG', mock.Mock())
97         prepare_env.check_env_variables()
98         mock_logger_info.assert_any_call("Checking environment variables"
99                                          "...")
100         mock_logger_info.assert_any_call(test_utils.
101                                          SubstrMatch("    CI_DEBUG="))
102
103     @mock.patch('functest.ci.prepare_env.logger.info')
104     @mock.patch('functest.ci.prepare_env.logger.warning')
105     def test_check_env_variables_with_node(self, mock_logger_warn,
106                                            mock_logger_info):
107         CONST.__setattr__('NODE_NAME', mock.Mock())
108         prepare_env.check_env_variables()
109         mock_logger_info.assert_any_call("Checking environment variables"
110                                          "...")
111         mock_logger_info.assert_any_call(test_utils.
112                                          SubstrMatch("    NODE_NAME="))
113
114     @mock.patch('functest.ci.prepare_env.logger.info')
115     @mock.patch('functest.ci.prepare_env.logger.warning')
116     def test_check_env_variables_with_build_tag(self, mock_logger_warn,
117                                                 mock_logger_info):
118         CONST.__setattr__('BUILD_TAG', mock.Mock())
119         prepare_env.check_env_variables()
120         mock_logger_info.assert_any_call("Checking environment variables"
121                                          "...")
122
123         mock_logger_info.assert_any_call(test_utils.
124                                          SubstrMatch("    BUILD_TAG="))
125
126     @mock.patch('functest.ci.prepare_env.logger.info')
127     @mock.patch('functest.ci.prepare_env.logger.warning')
128     def test_check_env_variables_with_is_ci_run(self, mock_logger_warn,
129                                                 mock_logger_info):
130         CONST.__setattr__('IS_CI_RUN', mock.Mock())
131         prepare_env.check_env_variables()
132         mock_logger_info.assert_any_call("Checking environment variables"
133                                          "...")
134
135         mock_logger_info.assert_any_call(test_utils.
136                                          SubstrMatch("    IS_CI_RUN="))
137
138     def test_get_deployment_handler_missing_const_vars(self):
139         with mock.patch('functest.ci.prepare_env.'
140                         'factory.Factory.get_handler') as m:
141             CONST.__setattr__('INSTALLER_IP', None)
142             prepare_env.get_deployment_handler()
143             self.assertFalse(m.called)
144
145             CONST.__setattr__('INSTALLER_TYPE', None)
146             prepare_env.get_deployment_handler()
147             self.assertFalse(m.called)
148
149     @mock.patch('functest.ci.prepare_env.logger.debug')
150     def test_get_deployment_handler_missing_print_deploy_info(self,
151                                                               mock_debug):
152         with mock.patch('functest.ci.prepare_env.'
153                         'factory.Factory.get_handler') as m, \
154             mock.patch('functest.ci.prepare_env.'
155                        'ft_utils.get_parameter_from_yaml',
156                        side_effect=ValueError):
157             CONST.__setattr__('INSTALLER_IP', 'test_ip')
158             CONST.__setattr__('INSTALLER_TYPE', 'test_inst_type')
159             opnfv_constants.INSTALLERS = ['test_inst_type']
160             prepare_env.get_deployment_handler()
161             msg = ('Printing deployment info is not supported for '
162                    'test_inst_type')
163             mock_debug.assert_any_call(msg)
164             self.assertFalse(m.called)
165
166     @mock.patch('functest.ci.prepare_env.logger.debug')
167     def test_get_deployment_handler_exception(self, mock_debug):
168         with mock.patch('functest.ci.prepare_env.'
169                         'factory.Factory.get_handler',
170                         side_effect=Exception), \
171             mock.patch('functest.ci.prepare_env.'
172                        'ft_utils.get_parameter_from_yaml'):
173             CONST.__setattr__('INSTALLER_IP', 'test_ip')
174             CONST.__setattr__('INSTALLER_TYPE', 'test_inst_type')
175             opnfv_constants.INSTALLERS = ['test_inst_type']
176             prepare_env.get_deployment_handler()
177             self.assertTrue(mock_debug.called)
178
179     @mock.patch('functest.ci.prepare_env.logger.info')
180     @mock.patch('functest.ci.prepare_env.logger.debug')
181     def test_create_directories_missing_dir(self, mock_logger_debug,
182                                             mock_logger_info):
183         with mock.patch('functest.ci.prepare_env.os.path.exists',
184                         return_value=False), \
185                 mock.patch('functest.ci.prepare_env.os.makedirs') \
186                 as mock_method:
187             prepare_env.create_directories()
188             mock_logger_info.assert_any_call("Creating needed directories...")
189             mock_method.assert_any_call(
190                 CONST.__getattribute__('dir_functest_conf'))
191             mock_method.assert_any_call(
192                 CONST.__getattribute__('dir_functest_data'))
193             mock_logger_info.assert_any_call("    %s created." %
194                                              CONST.__getattribute__(
195                                                  'dir_functest_conf'))
196             mock_logger_info.assert_any_call("    %s created." %
197                                              CONST.__getattribute__(
198                                                  'dir_functest_data'))
199
200     @mock.patch('functest.ci.prepare_env.logger.info')
201     @mock.patch('functest.ci.prepare_env.logger.debug')
202     def test_create_directories_with_dir(self, mock_logger_debug,
203                                          mock_logger_info):
204         with mock.patch('functest.ci.prepare_env.os.path.exists',
205                         return_value=True):
206             prepare_env.create_directories()
207             mock_logger_info.assert_any_call("Creating needed directories...")
208             mock_logger_debug.assert_any_call("   %s already exists." %
209                                               CONST.__getattribute__(
210                                                   'dir_functest_conf'))
211             mock_logger_debug.assert_any_call("   %s already exists." %
212                                               CONST.__getattribute__(
213                                                   'dir_functest_data'))
214
215     def _get_env_cred_dict(self, os_prefix=''):
216         return {'OS_USERNAME': os_prefix + 'username',
217                 'OS_PASSWORD': os_prefix + 'password',
218                 'OS_AUTH_URL': 'http://test_ip:test_port/v2.0',
219                 'OS_TENANT_NAME': os_prefix + 'tenant_name',
220                 'OS_USER_DOMAIN_NAME': os_prefix + 'user_domain_name',
221                 'OS_PROJECT_DOMAIN_NAME': os_prefix + 'project_domain_name',
222                 'OS_PROJECT_NAME': os_prefix + 'project_name',
223                 'OS_ENDPOINT_TYPE': os_prefix + 'endpoint_type',
224                 'OS_REGION_NAME': os_prefix + 'region_name'}
225
226     @mock.patch('functest.ci.prepare_env.logger.error')
227     @mock.patch('functest.ci.prepare_env.logger.info')
228     @mock.patch('functest.ci.prepare_env.logger.warning')
229     def test_source_rc_missing_rc_file(self, mock_logger_warn,
230                                        mock_logger_info,
231                                        mock_logger_error):
232         with mock.patch('functest.ci.prepare_env.os.path.isfile',
233                         return_value=True), \
234                 mock.patch('functest.ci.prepare_env.os.path.getsize',
235                            return_value=0), \
236                 self.assertRaises(Exception):
237             CONST.__setattr__('openstack_creds', 'test_creds')
238             prepare_env.source_rc_file()
239
240     def test_source_rc_missing_installer_ip(self):
241         with mock.patch('functest.ci.prepare_env.os.path.isfile',
242                         return_value=False), \
243                 self.assertRaises(Exception):
244             CONST.__setattr__('INSTALLER_IP', None)
245             CONST.__setattr__('openstack_creds', 'test_creds')
246             prepare_env.source_rc_file()
247
248     def test_source_rc_missing_installer_type(self):
249         with mock.patch('functest.ci.prepare_env.os.path.isfile',
250                         return_value=False), \
251                 self.assertRaises(Exception):
252             CONST.__setattr__('INSTALLER_IP', 'test_ip')
253             CONST.__setattr__('openstack_creds', 'test_creds')
254             CONST.__setattr__('INSTALLER_TYPE', 'test_type')
255             opnfv_constants.INSTALLERS = []
256             prepare_env.source_rc_file()
257
258     def test_source_rc_missing_os_credfile_ci_inst(self):
259         with mock.patch('functest.ci.prepare_env.os.path.isfile',
260                         return_value=False), \
261                 mock.patch('functest.ci.prepare_env.os.path.getsize'), \
262                 mock.patch('functest.ci.prepare_env.os.path.join'), \
263                 mock.patch('functest.ci.prepare_env.subprocess.Popen') \
264                 as mock_subproc_popen, \
265                 self.assertRaises(Exception):
266             CONST.__setattr__('openstack_creds', 'test_creds')
267             CONST.__setattr__('INSTALLER_IP', None)
268             CONST.__setattr__('INSTALLER_TYPE', 'test_type')
269             opnfv_constants.INSTALLERS = ['test_type']
270
271             process_mock = mock.Mock()
272             attrs = {'communicate.return_value': ('output', 'error'),
273                      'return_code': 1}
274             process_mock.configure_mock(**attrs)
275             mock_subproc_popen.return_value = process_mock
276
277             prepare_env.source_rc_file()
278
279     @mock.patch('functest.ci.prepare_env.logger.debug')
280     def test_patch_file(self, mock_logger_debug):
281         with mock.patch("__builtin__.open", mock.mock_open()), \
282             mock.patch('functest.ci.prepare_env.yaml.safe_load',
283                        return_value={'test_scenario': {'tkey': 'tvalue'}}), \
284             mock.patch('functest.ci.prepare_env.ft_utils.get_functest_yaml',
285                        return_value={'tkey1': 'tvalue1'}), \
286             mock.patch('functest.ci.prepare_env.os.remove') as m, \
287                 mock.patch('functest.ci.prepare_env.yaml.dump'):
288             CONST.__setattr__('DEPLOY_SCENARIO', 'test_scenario')
289             prepare_env.patch_file('test_file')
290             self.assertTrue(m.called)
291
292     @mock.patch('functest.ci.prepare_env.logger.info')
293     def test_verify_deployment_error(self, mock_logger_error):
294         mock_popen = mock.Mock()
295         attrs = {'poll.return_value': None,
296                  'stdout.readline.return_value': 'ERROR'}
297         mock_popen.configure_mock(**attrs)
298
299         with mock.patch('functest.ci.prepare_env.print_separator') as m, \
300             mock.patch('functest.ci.prepare_env.subprocess.Popen',
301                        return_value=mock_popen), \
302                 self.assertRaises(Exception) as context:
303             prepare_env.verify_deployment()
304             self.assertTrue(m.called)
305             msg = "Problem while running 'check_os.sh'."
306             mock_logger_error.assert_called_once_with('ERROR')
307             self.assertTrue(msg in context)
308
309     def _get_rally_creds(self):
310         return {"type": "ExistingCloud",
311                 "admin": {"username": 'test_user_name',
312                           "password": 'test_password',
313                           "tenant": 'test_tenant'}}
314
315     @mock.patch('functest.ci.prepare_env.os_utils.get_credentials_for_rally')
316     @mock.patch('functest.ci.prepare_env.logger.info')
317     @mock.patch('functest.ci.prepare_env.ft_utils.execute_command_raise')
318     @mock.patch('functest.ci.prepare_env.ft_utils.execute_command')
319     def test_install_rally(self, mock_exec, mock_exec_raise, mock_logger_info,
320                            mock_os_utils):
321
322         mock_os_utils.return_value = self._get_rally_creds()
323
324         prepare_env.install_rally()
325
326         cmd = "rally deployment destroy opnfv-rally"
327         error_msg = "Deployment %s does not exist." % \
328                     CONST.__getattribute__('rally_deployment_name')
329         mock_logger_info.assert_any_call("Creating Rally environment...")
330         mock_exec.assert_any_call(cmd, error_msg=error_msg, verbose=False)
331
332         cmd = "rally deployment create --file=rally_conf.json --name="
333         cmd += CONST.__getattribute__('rally_deployment_name')
334         error_msg = "Problem while creating Rally deployment"
335         mock_exec_raise.assert_any_call(cmd, error_msg=error_msg)
336
337         cmd = "rally deployment check"
338         error_msg = ("OpenStack not responding or "
339                      "faulty Rally deployment.")
340         mock_exec_raise.assert_any_call(cmd, error_msg=error_msg)
341
342         cmd = "rally deployment list"
343         error_msg = ("Problem while listing "
344                      "Rally deployment.")
345         mock_exec.assert_any_call(cmd, error_msg=error_msg)
346
347         cmd = "rally plugin list | head -5"
348         error_msg = ("Problem while showing "
349                      "Rally plugins.")
350         mock_exec.assert_any_call(cmd, error_msg=error_msg)
351
352     @mock.patch('functest.ci.prepare_env.logger.debug')
353     def test_install_tempest(self, mock_logger_debug):
354         mock_popen = mock.Mock()
355         attrs = {'poll.return_value': None,
356                  'stdout.readline.return_value': '0'}
357         mock_popen.configure_mock(**attrs)
358
359         CONST.__setattr__('tempest_deployment_name', 'test_dep_name')
360         with mock.patch('functest.ci.prepare_env.'
361                         'ft_utils.execute_command_raise',
362                         side_effect=Exception), \
363             mock.patch('functest.ci.prepare_env.subprocess.Popen',
364                        return_value=mock_popen), \
365                 self.assertRaises(Exception):
366             prepare_env.install_tempest()
367             mock_logger_debug.assert_any_call("Tempest test_dep_name"
368                                               " does not exist")
369
370     def test_create_flavor(self):
371         with mock.patch('functest.ci.prepare_env.'
372                         'os_utils.get_or_create_flavor',
373                         return_value=('test_', None)), \
374                 self.assertRaises(Exception) as context:
375             prepare_env.create_flavor()
376             msg = 'Failed to create flavor'
377             self.assertTrue(msg in context)
378
379     @mock.patch('functest.ci.prepare_env.sys.exit')
380     @mock.patch('functest.ci.prepare_env.logger.error')
381     def test_check_environment_missing_file(self, mock_logger_error,
382                                             mock_sys_exit):
383         with mock.patch('functest.ci.prepare_env.os.path.isfile',
384                         return_value=False), \
385                 self.assertRaises(Exception):
386             prepare_env.check_environment()
387
388     @mock.patch('functest.ci.prepare_env.sys.exit')
389     @mock.patch('functest.ci.prepare_env.logger.error')
390     def test_check_environment_with_error(self, mock_logger_error,
391                                           mock_sys_exit):
392         with mock.patch('functest.ci.prepare_env.os.path.isfile',
393                         return_value=True), \
394             mock.patch("__builtin__.open", mock.mock_open(read_data='0')), \
395                 self.assertRaises(Exception):
396             prepare_env.check_environment()
397
398     @mock.patch('functest.ci.prepare_env.logger.info')
399     def test_check_environment_default(self, mock_logger_info):
400         with mock.patch('functest.ci.prepare_env.os.path.isfile',
401                         return_value=True):
402             with mock.patch("__builtin__.open", mock.mock_open(read_data='1')):
403                 prepare_env.check_environment()
404                 mock_logger_info.assert_any_call("Functest environment"
405                                                  " is installed.")
406
407     @mock.patch('functest.ci.prepare_env.print_deployment_info')
408     @mock.patch('functest.ci.prepare_env.check_environment')
409     @mock.patch('functest.ci.prepare_env.create_flavor')
410     @mock.patch('functest.ci.prepare_env.install_tempest')
411     @mock.patch('functest.ci.prepare_env.install_rally')
412     @mock.patch('functest.ci.prepare_env.verify_deployment')
413     @mock.patch('functest.ci.prepare_env.patch_config_file')
414     @mock.patch('functest.ci.prepare_env.source_rc_file')
415     @mock.patch('functest.ci.prepare_env.create_directories')
416     @mock.patch('functest.ci.prepare_env.get_deployment_handler')
417     @mock.patch('functest.ci.prepare_env.check_env_variables')
418     @mock.patch('functest.ci.prepare_env.logger.info')
419     def test_main_start(self, mock_logger_info, mock_env_var, mock_dep_handler,
420                         mock_create_dir, mock_source_rc, mock_patch_config,
421                         mock_verify_depl, mock_install_rally,
422                         mock_install_temp, mock_create_flavor,
423                         mock_check_env, mock_print_info):
424         with mock.patch("__builtin__.open", mock.mock_open()) as m:
425             args = {'action': 'start'}
426             self.assertEqual(prepare_env.main(**args), 0)
427             mock_logger_info.assert_any_call("######### Preparing Functest "
428                                              "environment #########\n")
429             self.assertTrue(mock_env_var.called)
430             self.assertTrue(mock_dep_handler.called)
431             self.assertTrue(mock_create_dir.called)
432             self.assertTrue(mock_source_rc.called)
433             self.assertTrue(mock_patch_config.called)
434             self.assertTrue(mock_verify_depl.called)
435             self.assertTrue(mock_install_rally.called)
436             self.assertTrue(mock_install_temp.called)
437             self.assertTrue(mock_create_flavor.called)
438             m.assert_called_once_with(
439                 CONST.__getattribute__('env_active'), "w")
440             self.assertTrue(mock_check_env.called)
441             self.assertTrue(mock_print_info.called)
442
443     @mock.patch('functest.ci.prepare_env.check_environment')
444     def test_main_check(self, mock_check_env):
445         args = {'action': 'check'}
446         self.assertEqual(prepare_env.main(**args), 0)
447         self.assertTrue(mock_check_env.called)
448
449     @mock.patch('functest.ci.prepare_env.logger.error')
450     def test_main_no_arg(self, mock_logger_error):
451         args = {'action': 'not_valid'}
452         self.assertEqual(prepare_env.main(**args), -1)
453         mock_logger_error.assert_called_once_with('Argument not valid.')
454
455
456 if __name__ == "__main__":
457     logging.disable(logging.CRITICAL)
458     unittest.main(verbosity=2)