50ebe4b5d1f0278f4d5482bb4d5a83b7a2edbac3
[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 pkg_resources
12 import unittest
13 import os
14
15 import mock
16
17 from functest.cli.commands import cli_os
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.snapshot_file = 'test_snapshot_file'
29         self.cli_os = cli_os.CliOpenStack()
30
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',
35                         return_value=0):
36             self.assertEqual(self.cli_os.ping_endpoint(), 0)
37
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,
41                                             mock_exit):
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 '. "
47                                                     "$creds'")
48
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,
52                                            mock_exit):
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',
56                         return_value=1):
57             self.cli_os.ping_endpoint()
58             mock_click_echo.assert_called_once_with("Cannot talk to the "
59                                                     "endpoint %s\n" %
60                                                     self.endpoint_ip)
61             mock_exit.assert_called_once_with(0)
62
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'):
66             self.cli_os.check()
67             mock_ftutils_execute.assert_called_once_with(
68                 "sh %s" % pkg_resources.resource_filename(
69                     'functest', 'ci/check_os.sh'), verbose=False)
70
71     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
72                 return_value=False)
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') \
77                 as mock_os_snapshot:
78             self.cli_os.snapshot_create()
79             mock_click_echo.assert_called_once_with("Generating Openstack "
80                                                     "snapshot...")
81             self.assertTrue(mock_os_snapshot.called)
82
83     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
84                 return_value=True)
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") \
88                 as mock_raw_input, \
89                 mock.patch.object(self.cli_os, 'ping_endpoint'), \
90                 mock.patch('functest.cli.commands.cli_os.os_snapshot.main') \
91                 as mock_os_snapshot:
92             self.cli_os.snapshot_create()
93             mock_click_echo.assert_called_once_with("Generating Openstack "
94                                                     "snapshot...")
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)
100
101     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
102                 return_value=False)
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 "
110                                                 "snapshot-create'")
111
112     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
113                 return_value=True)
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')) \
117                 as m:
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")
122
123     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
124                 return_value=True)
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') \
129                 as mock_os_clean:
130             self.cli_os.clean()
131             self.assertTrue(mock_os_clean.called)
132
133     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
134                 return_value=False)
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'):
138             self.cli_os.clean()
139             mock_click_echo.assert_called_once_with("Not possible to clean "
140                                                     "OpenStack without a "
141                                                     "snapshot. This could "
142                                                     "cause problems. "
143                                                     "Run first the command "
144                                                     "'functest openstack "
145                                                     "snapshot-create'")
146
147     @mock.patch('functest.cli.commands.cli_os.click.echo')
148     def test_show_credentials(self, mock_click_echo):
149         key = 'OS_KEY'
150         value = 'OS_VALUE'
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))
154
155
156 if __name__ == "__main__":
157     logging.disable(logging.CRITICAL)
158     unittest.main(verbosity=2)