Merge "Modify how to disable logging in unit test."
[functest-xtesting.git] / functest / tests / unit / cli / commands / test_cli_os.py
1 #!/usr/bin/env python
2 #
3 # jose.lausuch@ericsson.com
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9
10 import logging
11 import unittest
12 import os
13
14 import mock
15
16 from functest.cli.commands import cli_os
17 from functest.utils.constants import CONST
18
19
20 class CliOpenStackTesting(unittest.TestCase):
21
22     def setUp(self):
23         self.endpoint_ip = 'test_ip'
24         self.os_auth_url = 'http://test_ip:test_port/v2.0'
25         self.installer_type = 'test_installer_type'
26         self.installer_ip = 'test_installer_ip'
27         self.openstack_creds = 'test_openstack_creds'
28         self.dir_repo_functest = 'test_dir_repo_functest'
29         self.snapshot_file = 'test_snapshot_file'
30         self.cli_os = cli_os.CliOpenStack()
31
32     def test_ping_endpoint_default(self):
33         self.cli_os.os_auth_url = self.os_auth_url
34         self.cli_os.endpoint_ip = self.endpoint_ip
35         with mock.patch('functest.cli.commands.cli_os.os.system',
36                         return_value=0):
37             self.assertEqual(self.cli_os.ping_endpoint(), 0)
38
39     @mock.patch('functest.cli.commands.cli_os.exit', side_effect=Exception)
40     @mock.patch('functest.cli.commands.cli_os.click.echo')
41     def test_ping_endpoint_missing_auth_url(self, mock_click_echo,
42                                             mock_exit):
43         with self.assertRaises(Exception):
44             self.cli_os.os_auth_url = None
45             self.cli_os.ping_endpoint()
46             mock_click_echo.assert_called_once_with("Source the OpenStack "
47                                                     "credentials first '. "
48                                                     "$creds'")
49
50     @mock.patch('functest.cli.commands.cli_os.exit')
51     @mock.patch('functest.cli.commands.cli_os.click.echo')
52     def test_ping_endpoint_os_system_fails(self, mock_click_echo,
53                                            mock_exit):
54         self.cli_os.os_auth_url = self.os_auth_url
55         self.cli_os.endpoint_ip = self.endpoint_ip
56         with mock.patch('functest.cli.commands.cli_os.os.system',
57                         return_value=1):
58             self.cli_os.ping_endpoint()
59             mock_click_echo.assert_called_once_with("Cannot talk to the "
60                                                     "endpoint %s\n" %
61                                                     self.endpoint_ip)
62             mock_exit.assert_called_once_with(0)
63
64     @mock.patch('functest.cli.commands.cli_os.ft_utils.execute_command')
65     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
66                 return_value=False)
67     @mock.patch('functest.cli.commands.cli_os.click.echo')
68     def test_fetch_credentials_default(self, mock_click_echo,
69                                        mock_os_path,
70                                        mock_ftutils_execute):
71         CONST.__setattr__('INSTALLER_TYPE', self.installer_type)
72         CONST.__setattr__('INSTALLER_IP', self.installer_ip)
73         cmd = ("%s/releng/utils/fetch_os_creds.sh -d %s -i %s -a %s"
74                % (CONST.__getattribute__('dir_repos'),
75                   self.openstack_creds,
76                   self.installer_type,
77                   self.installer_ip))
78         self.cli_os.openstack_creds = self.openstack_creds
79         self.cli_os.fetch_credentials()
80         mock_click_echo.assert_called_once_with("Fetching credentials from "
81                                                 "installer node '%s' with "
82                                                 "IP=%s.." %
83                                                 (self.installer_type,
84                                                  self.installer_ip))
85         mock_ftutils_execute.assert_called_once_with(cmd, verbose=False)
86
87     @mock.patch('functest.cli.commands.cli_os.ft_utils.execute_command')
88     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
89                 return_value=False)
90     @mock.patch('functest.cli.commands.cli_os.click.echo')
91     def test_fetch_credentials_missing_installer_type(self, mock_click_echo,
92                                                       mock_os_path,
93                                                       mock_ftutils_execute):
94         CONST.__setattr__('INSTALLER_TYPE', None)
95         CONST.__setattr__('INSTALLER_IP', self.installer_ip)
96         cmd = ("%s/releng/utils/fetch_os_creds.sh -d %s -i %s -a %s"
97                % (CONST.__getattribute__('dir_repos'),
98                   self.openstack_creds,
99                   None,
100                   self.installer_ip))
101         self.cli_os.openstack_creds = self.openstack_creds
102         self.cli_os.fetch_credentials()
103         mock_click_echo.assert_any_call("The environment variable "
104                                         "'INSTALLER_TYPE' is not"
105                                         "defined. Please export it")
106         mock_click_echo.assert_any_call("Fetching credentials from "
107                                         "installer node '%s' with "
108                                         "IP=%s.." %
109                                         (None,
110                                          self.installer_ip))
111         mock_ftutils_execute.assert_called_once_with(cmd, verbose=False)
112
113     @mock.patch('functest.cli.commands.cli_os.ft_utils.execute_command')
114     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
115                 return_value=False)
116     @mock.patch('functest.cli.commands.cli_os.click.echo')
117     def test_fetch_credentials_missing_installer_ip(self, mock_click_echo,
118                                                     mock_os_path,
119                                                     mock_ftutils_execute):
120         installer_type = self.installer_type
121         installer_ip = None
122         CONST.__setattr__('INSTALLER_TYPE', installer_type)
123         CONST.__setattr__('INSTALLER_IP', installer_ip)
124         cmd = ("%s/releng/utils/fetch_os_creds.sh -d %s -i %s -a %s"
125                % (CONST.__getattribute__('dir_repos'),
126                   self.openstack_creds,
127                   installer_type,
128                   installer_ip))
129         self.cli_os.openstack_creds = self.openstack_creds
130         self.cli_os.fetch_credentials()
131         mock_click_echo.assert_any_call("The environment variable "
132                                         "'INSTALLER_IP' is not"
133                                         "defined. Please export it")
134         mock_click_echo.assert_any_call("Fetching credentials from "
135                                         "installer node '%s' with "
136                                         "IP=%s.." %
137                                         (installer_type,
138                                          installer_ip))
139         mock_ftutils_execute.assert_called_once_with(cmd, verbose=False)
140
141     @mock.patch('functest.cli.commands.cli_os.ft_utils.execute_command')
142     def test_check(self, mock_ftutils_execute):
143         with mock.patch.object(self.cli_os, 'ping_endpoint'):
144             CONST.__setattr__('dir_repo_functest', self.dir_repo_functest)
145             cmd = os.path.join(CONST.__getattribute__('dir_repo_functest'),
146                                "functest/ci/check_os.sh")
147             self.cli_os.check()
148             mock_ftutils_execute.assert_called_once_with(cmd, verbose=False)
149
150     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
151                 return_value=False)
152     @mock.patch('functest.cli.commands.cli_os.click.echo')
153     def test_snapshot_create(self, mock_click_echo, mock_os_path):
154         with mock.patch.object(self.cli_os, 'ping_endpoint'), \
155                 mock.patch('functest.cli.commands.cli_os.os_snapshot.main') \
156                 as mock_os_snapshot:
157             self.cli_os.snapshot_create()
158             mock_click_echo.assert_called_once_with("Generating Openstack "
159                                                     "snapshot...")
160             self.assertTrue(mock_os_snapshot.called)
161
162     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
163                 return_value=True)
164     @mock.patch('functest.cli.commands.cli_os.click.echo')
165     def test_snapshot_create_overwrite(self, mock_click_echo, mock_os_path):
166         with mock.patch('__builtin__.raw_input', return_value="y") \
167                 as mock_raw_input, \
168                 mock.patch.object(self.cli_os, 'ping_endpoint'), \
169                 mock.patch('functest.cli.commands.cli_os.os_snapshot.main') \
170                 as mock_os_snapshot:
171             self.cli_os.snapshot_create()
172             mock_click_echo.assert_called_once_with("Generating Openstack "
173                                                     "snapshot...")
174             mock_raw_input.assert_any_call("It seems there is already an "
175                                            "OpenStack snapshot. Do you want "
176                                            "to overwrite it with the current "
177                                            "OpenStack status? [y|n]\n")
178             self.assertTrue(mock_os_snapshot.called)
179
180     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
181                 return_value=False)
182     @mock.patch('functest.cli.commands.cli_os.click.echo')
183     def test_snapshot_show_missing_snap(self, mock_click_echo, mock_os_path):
184         self.cli_os.snapshot_show()
185         mock_click_echo.assert_called_once_with("There is no OpenStack "
186                                                 "snapshot created. To create "
187                                                 "one run the command "
188                                                 "'functest openstack "
189                                                 "snapshot-create'")
190
191     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
192                 return_value=True)
193     @mock.patch('functest.cli.commands.cli_os.click.echo')
194     def test_snapshot_show_default(self, mock_click_echo, mock_os_path):
195         with mock.patch('__builtin__.open', mock.mock_open(read_data='0')) \
196                 as m:
197             self.cli_os.snapshot_file = self.snapshot_file
198             self.cli_os.snapshot_show()
199             m.assert_called_once_with(self.snapshot_file, 'r')
200             mock_click_echo.assert_called_once_with("\n0")
201
202     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
203                 return_value=True)
204     @mock.patch('functest.cli.commands.cli_os.click.echo')
205     def test_clean(self, mock_click_echo, mock_os_path):
206         with mock.patch.object(self.cli_os, 'ping_endpoint'), \
207                 mock.patch('functest.cli.commands.cli_os.os_clean.main') \
208                 as mock_os_clean:
209             self.cli_os.clean()
210             self.assertTrue(mock_os_clean.called)
211
212     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
213                 return_value=False)
214     @mock.patch('functest.cli.commands.cli_os.click.echo')
215     def test_clean_missing_file(self, mock_click_echo, mock_os_path):
216         with mock.patch.object(self.cli_os, 'ping_endpoint'):
217             self.cli_os.clean()
218             mock_click_echo.assert_called_once_with("Not possible to clean "
219                                                     "OpenStack without a "
220                                                     "snapshot. This could "
221                                                     "cause problems. "
222                                                     "Run first the command "
223                                                     "'functest openstack "
224                                                     "snapshot-create'")
225
226     @mock.patch('functest.cli.commands.cli_os.click.echo')
227     def test_show_credentials(self, mock_click_echo):
228         key = 'OS_KEY'
229         value = 'OS_VALUE'
230         with mock.patch.dict(os.environ, {key: value}):
231             self.cli_os.show_credentials()
232             mock_click_echo.assert_any_call("{}={}".format(key, value))
233
234
235 if __name__ == "__main__":
236     logging.disable(logging.CRITICAL)
237     unittest.main(verbosity=2)