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