Merge "[fuel] Skip test_server_basic_ops tempest 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     logging.disable(logging.CRITICAL)
22
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)
28
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,
32                                                    mock_logger_info):
33         CONST.INSTALLER_TYPE = None
34         prepare_env.check_env_variables()
35         mock_logger_info.assert_any_call("Checking environment variables"
36                                          "...")
37         mock_logger_warn.assert_any_call("The env variable 'INSTALLER_TYPE'"
38                                          " is not defined.")
39
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,
43                                                  mock_logger_info):
44         CONST.INSTALLER_IP = None
45         prepare_env.check_env_variables()
46         mock_logger_info.assert_any_call("Checking environment variables"
47                                          "...")
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'"
55                                          " command.")
56
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,
60                                               mock_logger_info):
61         CONST.INSTALLER_IP = mock.Mock()
62         prepare_env.check_env_variables()
63         mock_logger_info.assert_any_call("Checking environment variables"
64                                          "...")
65         mock_logger_info.assert_any_call(test_utils.
66                                          SubstrMatch("    INSTALLER_IP="))
67
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,
71                                                   mock_logger_info):
72         CONST.DEPLOY_SCENARIO = None
73         prepare_env.check_env_variables()
74         mock_logger_info.assert_any_call("Checking environment variables"
75                                          "...")
76         mock_logger_warn.assert_any_call("The env variable"
77                                          " 'DEPLOY_SCENARIO' is not defined"
78                                          ". Setting CI_SCENARIO=undefined.")
79
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,
83                                                mock_logger_info):
84         CONST.DEPLOY_SCENARIO = mock.Mock()
85         prepare_env.check_env_variables()
86         mock_logger_info.assert_any_call("Checking environment variables"
87                                          "...")
88         mock_logger_info.assert_any_call(test_utils.
89                                          SubstrMatch("DEPLOY_SCENARIO="))
90
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,
94                                                mock_logger_info):
95         CONST.CI_DEBUG = mock.Mock()
96         prepare_env.check_env_variables()
97         mock_logger_info.assert_any_call("Checking environment variables"
98                                          "...")
99         mock_logger_info.assert_any_call(test_utils.
100                                          SubstrMatch("    CI_DEBUG="))
101
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,
105                                            mock_logger_info):
106         CONST.NODE_NAME = mock.Mock()
107         prepare_env.check_env_variables()
108         mock_logger_info.assert_any_call("Checking environment variables"
109                                          "...")
110         mock_logger_info.assert_any_call(test_utils.
111                                          SubstrMatch("    NODE_NAME="))
112
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,
116                                                 mock_logger_info):
117         CONST.BUILD_TAG = mock.Mock()
118         prepare_env.check_env_variables()
119         mock_logger_info.assert_any_call("Checking environment variables"
120                                          "...")
121
122         mock_logger_info.assert_any_call(test_utils.
123                                          SubstrMatch("    BUILD_TAG="))
124
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,
128                                                 mock_logger_info):
129         CONST.IS_CI_RUN = mock.Mock()
130         prepare_env.check_env_variables()
131         mock_logger_info.assert_any_call("Checking environment variables"
132                                          "...")
133
134         mock_logger_info.assert_any_call(test_utils.
135                                          SubstrMatch("    IS_CI_RUN="))
136
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,
140                                             mock_logger_info):
141         with mock.patch('functest.ci.prepare_env.os.path.exists',
142                         return_value=False), \
143                 mock.patch('functest.ci.prepare_env.os.makedirs') \
144                 as mock_method:
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)
153
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,
157                                          mock_logger_info):
158         with mock.patch('functest.ci.prepare_env.os.path.exists',
159                         return_value=True):
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)
166
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'}
177
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,
182                                        mock_logger_info,
183                                        mock_logger_error):
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',
187                            return_value=0), \
188                 self.assertRaises(Exception):
189             CONST.openstack_creds = 'test_creds'
190             prepare_env.source_rc_file()
191
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()
199
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()
209
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']
222
223             process_mock = mock.Mock()
224             attrs = {'communicate.return_value': ('output', 'error'),
225                      'return_code': 1}
226             process_mock.configure_mock(**attrs)
227             mock_subproc_popen.return_value = process_mock
228
229             prepare_env.source_rc_file()
230
231     def _get_rally_creds(self):
232         return {"type": "ExistingCloud",
233                 "admin": {"username": 'test_user_name',
234                           "password": 'test_password',
235                           "tenant": 'test_tenant'}}
236
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,
242                            mock_os_utils):
243
244         mock_os_utils.return_value = self._get_rally_creds()
245
246         prepare_env.install_rally()
247
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)
253
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)
258
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)
263
264         cmd = "rally deployment list"
265         error_msg = ("Problem while listing "
266                      "Rally deployment.")
267         mock_exec.assert_any_call(cmd, error_msg=error_msg)
268
269         cmd = "rally plugin list | head -5"
270         error_msg = ("Problem while showing "
271                      "Rally plugins.")
272         mock_exec.assert_any_call(cmd, error_msg=error_msg)
273
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,
277                                             mock_sys_exit):
278         with mock.patch('functest.ci.prepare_env.os.path.isfile',
279                         return_value=False), \
280                 self.assertRaises(Exception):
281                 prepare_env.check_environment()
282
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,
286                                           mock_sys_exit):
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()
292
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',
296                         return_value=True):
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"
300                                                  " is installed.")
301
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,
316                         mock_check_env):
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)
332
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)
338
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.')
344
345
346 if __name__ == "__main__":
347     unittest.main(verbosity=2)