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
17 from functest.cli.commands import cli_os
20 class CliOpenStackTesting(unittest.TestCase):
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.snapshot_file = 'test_snapshot_file'
29 self.cli_os = cli_os.CliOpenStack()
31 def test_ping_endpoint_default(self):
32 self.cli_os.os_auth_url = self.os_auth_url
33 self.cli_os.endpoint_ip = self.endpoint_ip
34 with mock.patch('functest.cli.commands.cli_os.os.system',
36 self.assertEqual(self.cli_os.ping_endpoint(), 0)
38 @mock.patch('functest.cli.commands.cli_os.exit', side_effect=Exception)
39 @mock.patch('functest.cli.commands.cli_os.click.echo')
40 def test_ping_endpoint_missing_auth_url(self, mock_click_echo,
42 with self.assertRaises(Exception):
43 self.cli_os.os_auth_url = None
44 self.cli_os.ping_endpoint()
45 mock_click_echo.assert_called_once_with("Source the OpenStack "
46 "credentials first '. "
49 @mock.patch('functest.cli.commands.cli_os.exit')
50 @mock.patch('functest.cli.commands.cli_os.click.echo')
51 def test_ping_endpoint_os_system_fails(self, mock_click_echo,
53 self.cli_os.os_auth_url = self.os_auth_url
54 self.cli_os.endpoint_ip = self.endpoint_ip
55 with mock.patch('functest.cli.commands.cli_os.os.system',
57 self.cli_os.ping_endpoint()
58 mock_click_echo.assert_called_once_with("Cannot talk to the "
61 mock_exit.assert_called_once_with(0)
63 @mock.patch('functest.cli.commands.cli_os.ft_utils.execute_command')
64 def test_check(self, mock_ftutils_execute):
65 with mock.patch.object(self.cli_os, 'ping_endpoint'):
67 mock_ftutils_execute.assert_called_once_with(
68 "sh %s" % pkg_resources.resource_filename(
69 'functest', 'ci/check_os.sh'), verbose=False)
71 @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
73 @mock.patch('functest.cli.commands.cli_os.click.echo')
74 def test_snapshot_create(self, mock_click_echo, mock_os_path):
75 with mock.patch.object(self.cli_os, 'ping_endpoint'), \
76 mock.patch('functest.cli.commands.cli_os.os_snapshot.main') \
78 self.cli_os.snapshot_create()
79 mock_click_echo.assert_called_once_with("Generating Openstack "
81 self.assertTrue(mock_os_snapshot.called)
83 @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
85 @mock.patch('functest.cli.commands.cli_os.click.echo')
86 def test_snapshot_create_overwrite(self, mock_click_echo, mock_os_path):
87 with mock.patch('__builtin__.raw_input', return_value="y") \
89 mock.patch.object(self.cli_os, 'ping_endpoint'), \
90 mock.patch('functest.cli.commands.cli_os.os_snapshot.main') \
92 self.cli_os.snapshot_create()
93 mock_click_echo.assert_called_once_with("Generating Openstack "
95 mock_raw_input.assert_any_call("It seems there is already an "
96 "OpenStack snapshot. Do you want "
97 "to overwrite it with the current "
98 "OpenStack status? [y|n]\n")
99 self.assertTrue(mock_os_snapshot.called)
101 @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
103 @mock.patch('functest.cli.commands.cli_os.click.echo')
104 def test_snapshot_show_missing_snap(self, mock_click_echo, mock_os_path):
105 self.cli_os.snapshot_show()
106 mock_click_echo.assert_called_once_with("There is no OpenStack "
107 "snapshot created. To create "
108 "one run the command "
109 "'functest openstack "
112 @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
114 @mock.patch('functest.cli.commands.cli_os.click.echo')
115 def test_snapshot_show_default(self, mock_click_echo, mock_os_path):
116 with mock.patch('__builtin__.open', mock.mock_open(read_data='0')) \
118 self.cli_os.snapshot_file = self.snapshot_file
119 self.cli_os.snapshot_show()
120 m.assert_called_once_with(self.snapshot_file, 'r')
121 mock_click_echo.assert_called_once_with("\n0")
123 @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
125 @mock.patch('functest.cli.commands.cli_os.click.echo')
126 def test_clean(self, mock_click_echo, mock_os_path):
127 with mock.patch.object(self.cli_os, 'ping_endpoint'), \
128 mock.patch('functest.cli.commands.cli_os.os_clean.main') \
131 self.assertTrue(mock_os_clean.called)
133 @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
135 @mock.patch('functest.cli.commands.cli_os.click.echo')
136 def test_clean_missing_file(self, mock_click_echo, mock_os_path):
137 with mock.patch.object(self.cli_os, 'ping_endpoint'):
139 mock_click_echo.assert_called_once_with("Not possible to clean "
140 "OpenStack without a "
141 "snapshot. This could "
143 "Run first the command "
144 "'functest openstack "
147 @mock.patch('functest.cli.commands.cli_os.click.echo')
148 def test_show_credentials(self, mock_click_echo):
151 with mock.patch.dict(os.environ, {key: value}):
152 self.cli_os.show_credentials()
153 mock_click_echo.assert_any_call("{}={}".format(key, value))
156 if __name__ == "__main__":
157 logging.disable(logging.CRITICAL)
158 unittest.main(verbosity=2)