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
16 from functest.cli.commands import cli_os
17 from functest.utils.constants import CONST
20 class CliOpenStackTesting(unittest.TestCase):
21 logging.disable(logging.CRITICAL)
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()
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',
38 self.assertEqual(self.cli_os.ping_endpoint(), 0)
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,
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 '. "
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,
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',
59 self.cli_os.ping_endpoint()
60 mock_click_echo.assert_called_once_with("Cannot talk to the "
63 mock_exit.assert_called_once_with(0)
65 @mock.patch('functest.cli.commands.cli_os.ft_utils.execute_command')
66 @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
68 @mock.patch('functest.cli.commands.cli_os.click.echo')
69 def test_fetch_credentials_default(self, mock_click_echo,
71 mock_ftutils_execute):
72 CONST.__setattr__('INSTALLER_TYPE', self.installer_type)
73 CONST.__setattr__('INSTALLER_IP', self.installer_ip)
74 cmd = ("%s/releng/utils/fetch_os_creds.sh -d %s -i %s -a %s"
75 % (CONST.__getattribute__('dir_repos'),
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 "
86 mock_ftutils_execute.assert_called_once_with(cmd, verbose=False)
88 @mock.patch('functest.cli.commands.cli_os.ft_utils.execute_command')
89 @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
91 @mock.patch('functest.cli.commands.cli_os.click.echo')
92 def test_fetch_credentials_missing_installer_type(self, mock_click_echo,
94 mock_ftutils_execute):
95 CONST.__setattr__('INSTALLER_TYPE', None)
96 CONST.__setattr__('INSTALLER_IP', self.installer_ip)
97 cmd = ("%s/releng/utils/fetch_os_creds.sh -d %s -i %s -a %s"
98 % (CONST.__getattribute__('dir_repos'),
102 self.cli_os.openstack_creds = self.openstack_creds
103 self.cli_os.fetch_credentials()
104 mock_click_echo.assert_any_call("The environment variable "
105 "'INSTALLER_TYPE' is not"
106 "defined. Please export it")
107 mock_click_echo.assert_any_call("Fetching credentials from "
108 "installer node '%s' with "
112 mock_ftutils_execute.assert_called_once_with(cmd, verbose=False)
114 @mock.patch('functest.cli.commands.cli_os.ft_utils.execute_command')
115 @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
117 @mock.patch('functest.cli.commands.cli_os.click.echo')
118 def test_fetch_credentials_missing_installer_ip(self, mock_click_echo,
120 mock_ftutils_execute):
121 installer_type = self.installer_type
123 CONST.__setattr__('INSTALLER_TYPE', installer_type)
124 CONST.__setattr__('INSTALLER_IP', installer_ip)
125 cmd = ("%s/releng/utils/fetch_os_creds.sh -d %s -i %s -a %s"
126 % (CONST.__getattribute__('dir_repos'),
127 self.openstack_creds,
130 self.cli_os.openstack_creds = self.openstack_creds
131 self.cli_os.fetch_credentials()
132 mock_click_echo.assert_any_call("The environment variable "
133 "'INSTALLER_IP' is not"
134 "defined. Please export it")
135 mock_click_echo.assert_any_call("Fetching credentials from "
136 "installer node '%s' with "
140 mock_ftutils_execute.assert_called_once_with(cmd, verbose=False)
142 @mock.patch('functest.cli.commands.cli_os.ft_utils.execute_command')
143 def test_check(self, mock_ftutils_execute):
144 with mock.patch.object(self.cli_os, 'ping_endpoint'):
145 CONST.__setattr__('dir_repo_functest', self.dir_repo_functest)
146 cmd = os.path.join(CONST.__getattribute__('dir_repo_functest'),
147 "functest/ci/check_os.sh")
149 mock_ftutils_execute.assert_called_once_with(cmd, verbose=False)
151 @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
153 @mock.patch('functest.cli.commands.cli_os.click.echo')
154 def test_snapshot_create(self, mock_click_echo, mock_os_path):
155 with mock.patch.object(self.cli_os, 'ping_endpoint'), \
156 mock.patch('functest.cli.commands.cli_os.os_snapshot.main') \
158 self.cli_os.snapshot_create()
159 mock_click_echo.assert_called_once_with("Generating Openstack "
161 self.assertTrue(mock_os_snapshot.called)
163 @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
165 @mock.patch('functest.cli.commands.cli_os.click.echo')
166 def test_snapshot_create_overwrite(self, mock_click_echo, mock_os_path):
167 with mock.patch('__builtin__.raw_input', return_value="y") \
169 mock.patch.object(self.cli_os, 'ping_endpoint'), \
170 mock.patch('functest.cli.commands.cli_os.os_snapshot.main') \
172 self.cli_os.snapshot_create()
173 mock_click_echo.assert_called_once_with("Generating Openstack "
175 mock_raw_input.assert_any_call("It seems there is already an "
176 "OpenStack snapshot. Do you want "
177 "to overwrite it with the current "
178 "OpenStack status? [y|n]\n")
179 self.assertTrue(mock_os_snapshot.called)
181 @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
183 @mock.patch('functest.cli.commands.cli_os.click.echo')
184 def test_snapshot_show_missing_snap(self, mock_click_echo, mock_os_path):
185 self.cli_os.snapshot_show()
186 mock_click_echo.assert_called_once_with("There is no OpenStack "
187 "snapshot created. To create "
188 "one run the command "
189 "'functest openstack "
192 @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
194 @mock.patch('functest.cli.commands.cli_os.click.echo')
195 def test_snapshot_show_default(self, mock_click_echo, mock_os_path):
196 with mock.patch('__builtin__.open', mock.mock_open(read_data='0')) \
198 self.cli_os.snapshot_file = self.snapshot_file
199 self.cli_os.snapshot_show()
200 m.assert_called_once_with(self.snapshot_file, 'r')
201 mock_click_echo.assert_called_once_with("\n0")
203 @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
205 @mock.patch('functest.cli.commands.cli_os.click.echo')
206 def test_clean(self, mock_click_echo, mock_os_path):
207 with mock.patch.object(self.cli_os, 'ping_endpoint'), \
208 mock.patch('functest.cli.commands.cli_os.os_clean.main') \
211 self.assertTrue(mock_os_clean.called)
213 @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
215 @mock.patch('functest.cli.commands.cli_os.click.echo')
216 def test_clean_missing_file(self, mock_click_echo, mock_os_path):
217 with mock.patch.object(self.cli_os, 'ping_endpoint'):
219 mock_click_echo.assert_called_once_with("Not possible to clean "
220 "OpenStack without a "
221 "snapshot. This could "
223 "Run first the command "
224 "'functest openstack "
227 @mock.patch('functest.cli.commands.cli_os.click.echo')
228 def test_show_credentials(self, mock_click_echo):
231 with mock.patch.dict(os.environ, {key: value}):
232 self.cli_os.show_credentials()
233 mock_click_echo.assert_any_call("{}={}".format(key, value))
236 if __name__ == "__main__":
237 unittest.main(verbosity=2)