Merge "Add docstrings in unit"
[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 = ("fetch_os_creds.sh -d %s -i %s -a %s"
74                % (self.openstack_creds,
75                   self.installer_type,
76                   self.installer_ip))
77         self.cli_os.openstack_creds = self.openstack_creds
78         self.cli_os.fetch_credentials()
79         mock_click_echo.assert_called_once_with("Fetching credentials from "
80                                                 "installer node '%s' with "
81                                                 "IP=%s.." %
82                                                 (self.installer_type,
83                                                  self.installer_ip))
84         mock_ftutils_execute.assert_called_once_with(cmd, verbose=False)
85
86     @mock.patch('functest.cli.commands.cli_os.ft_utils.execute_command')
87     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
88                 return_value=False)
89     @mock.patch('functest.cli.commands.cli_os.click.echo')
90     def test_fetch_credentials_missing_installer_type(self, mock_click_echo,
91                                                       mock_os_path,
92                                                       mock_ftutils_execute):
93         CONST.__setattr__('INSTALLER_TYPE', None)
94         CONST.__setattr__('INSTALLER_IP', self.installer_ip)
95         cmd = ("fetch_os_creds.sh -d %s -i %s -a %s"
96                % (self.openstack_creds,
97                   None,
98                   self.installer_ip))
99         self.cli_os.openstack_creds = self.openstack_creds
100         self.cli_os.fetch_credentials()
101         mock_click_echo.assert_any_call("The environment variable "
102                                         "'INSTALLER_TYPE' is not"
103                                         "defined. Please export it")
104         mock_click_echo.assert_any_call("Fetching credentials from "
105                                         "installer node '%s' with "
106                                         "IP=%s.." %
107                                         (None,
108                                          self.installer_ip))
109         mock_ftutils_execute.assert_called_once_with(cmd, verbose=False)
110
111     @mock.patch('functest.cli.commands.cli_os.ft_utils.execute_command')
112     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
113                 return_value=False)
114     @mock.patch('functest.cli.commands.cli_os.click.echo')
115     def test_fetch_credentials_missing_installer_ip(self, mock_click_echo,
116                                                     mock_os_path,
117                                                     mock_ftutils_execute):
118         installer_type = self.installer_type
119         installer_ip = None
120         CONST.__setattr__('INSTALLER_TYPE', installer_type)
121         CONST.__setattr__('INSTALLER_IP', installer_ip)
122         cmd = ("fetch_os_creds.sh -d %s -i %s -a %s"
123                % (self.openstack_creds,
124                   installer_type,
125                   installer_ip))
126         self.cli_os.openstack_creds = self.openstack_creds
127         self.cli_os.fetch_credentials()
128         mock_click_echo.assert_any_call("The environment variable "
129                                         "'INSTALLER_IP' is not"
130                                         "defined. Please export it")
131         mock_click_echo.assert_any_call("Fetching credentials from "
132                                         "installer node '%s' with "
133                                         "IP=%s.." %
134                                         (installer_type,
135                                          installer_ip))
136         mock_ftutils_execute.assert_called_once_with(cmd, verbose=False)
137
138     @mock.patch('functest.cli.commands.cli_os.ft_utils.execute_command')
139     def test_check(self, mock_ftutils_execute):
140         with mock.patch.object(self.cli_os, 'ping_endpoint'):
141             CONST.__setattr__('dir_repo_functest', self.dir_repo_functest)
142             cmd = os.path.join(CONST.__getattribute__('dir_repo_functest'),
143                                "functest/ci/check_os.sh")
144             self.cli_os.check()
145             mock_ftutils_execute.assert_called_once_with(cmd, verbose=False)
146
147     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
148                 return_value=False)
149     @mock.patch('functest.cli.commands.cli_os.click.echo')
150     def test_snapshot_create(self, mock_click_echo, mock_os_path):
151         with mock.patch.object(self.cli_os, 'ping_endpoint'), \
152                 mock.patch('functest.cli.commands.cli_os.os_snapshot.main') \
153                 as mock_os_snapshot:
154             self.cli_os.snapshot_create()
155             mock_click_echo.assert_called_once_with("Generating Openstack "
156                                                     "snapshot...")
157             self.assertTrue(mock_os_snapshot.called)
158
159     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
160                 return_value=True)
161     @mock.patch('functest.cli.commands.cli_os.click.echo')
162     def test_snapshot_create_overwrite(self, mock_click_echo, mock_os_path):
163         with mock.patch('__builtin__.raw_input', return_value="y") \
164                 as mock_raw_input, \
165                 mock.patch.object(self.cli_os, 'ping_endpoint'), \
166                 mock.patch('functest.cli.commands.cli_os.os_snapshot.main') \
167                 as mock_os_snapshot:
168             self.cli_os.snapshot_create()
169             mock_click_echo.assert_called_once_with("Generating Openstack "
170                                                     "snapshot...")
171             mock_raw_input.assert_any_call("It seems there is already an "
172                                            "OpenStack snapshot. Do you want "
173                                            "to overwrite it with the current "
174                                            "OpenStack status? [y|n]\n")
175             self.assertTrue(mock_os_snapshot.called)
176
177     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
178                 return_value=False)
179     @mock.patch('functest.cli.commands.cli_os.click.echo')
180     def test_snapshot_show_missing_snap(self, mock_click_echo, mock_os_path):
181         self.cli_os.snapshot_show()
182         mock_click_echo.assert_called_once_with("There is no OpenStack "
183                                                 "snapshot created. To create "
184                                                 "one run the command "
185                                                 "'functest openstack "
186                                                 "snapshot-create'")
187
188     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
189                 return_value=True)
190     @mock.patch('functest.cli.commands.cli_os.click.echo')
191     def test_snapshot_show_default(self, mock_click_echo, mock_os_path):
192         with mock.patch('__builtin__.open', mock.mock_open(read_data='0')) \
193                 as m:
194             self.cli_os.snapshot_file = self.snapshot_file
195             self.cli_os.snapshot_show()
196             m.assert_called_once_with(self.snapshot_file, 'r')
197             mock_click_echo.assert_called_once_with("\n0")
198
199     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
200                 return_value=True)
201     @mock.patch('functest.cli.commands.cli_os.click.echo')
202     def test_clean(self, mock_click_echo, mock_os_path):
203         with mock.patch.object(self.cli_os, 'ping_endpoint'), \
204                 mock.patch('functest.cli.commands.cli_os.os_clean.main') \
205                 as mock_os_clean:
206             self.cli_os.clean()
207             self.assertTrue(mock_os_clean.called)
208
209     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
210                 return_value=False)
211     @mock.patch('functest.cli.commands.cli_os.click.echo')
212     def test_clean_missing_file(self, mock_click_echo, mock_os_path):
213         with mock.patch.object(self.cli_os, 'ping_endpoint'):
214             self.cli_os.clean()
215             mock_click_echo.assert_called_once_with("Not possible to clean "
216                                                     "OpenStack without a "
217                                                     "snapshot. This could "
218                                                     "cause problems. "
219                                                     "Run first the command "
220                                                     "'functest openstack "
221                                                     "snapshot-create'")
222
223     @mock.patch('functest.cli.commands.cli_os.click.echo')
224     def test_show_credentials(self, mock_click_echo):
225         key = 'OS_KEY'
226         value = 'OS_VALUE'
227         with mock.patch.dict(os.environ, {key: value}):
228             self.cli_os.show_credentials()
229             mock_click_echo.assert_any_call("{}={}".format(key, value))
230
231
232 if __name__ == "__main__":
233     logging.disable(logging.CRITICAL)
234     unittest.main(verbosity=2)