Merge "Fix typos in docs"
[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
18
19 class CliOpenStackTesting(unittest.TestCase):
20
21     def setUp(self):
22         self.endpoint_ip = 'test_ip'
23         self.os_auth_url = 'http://test_ip:test_port/v2.0'
24         self.installer_type = 'test_installer_type'
25         self.installer_ip = 'test_installer_ip'
26         self.openstack_creds = 'test_openstack_creds'
27         self.snapshot_file = 'test_snapshot_file'
28         self.cli_os = cli_os.CliOpenStack()
29
30     def test_ping_endpoint_default(self):
31         self.cli_os.os_auth_url = self.os_auth_url
32         self.cli_os.endpoint_ip = self.endpoint_ip
33         with mock.patch('functest.cli.commands.cli_os.os.system',
34                         return_value=0):
35             self.assertEqual(self.cli_os.ping_endpoint(), 0)
36
37     @mock.patch('functest.cli.commands.cli_os.exit', side_effect=Exception)
38     @mock.patch('functest.cli.commands.cli_os.click.echo')
39     def test_ping_endpoint_missing_auth_url(self, mock_click_echo,
40                                             mock_exit):
41         with self.assertRaises(Exception):
42             self.cli_os.os_auth_url = None
43             self.cli_os.ping_endpoint()
44             mock_click_echo.assert_called_once_with("Source the OpenStack "
45                                                     "credentials first '. "
46                                                     "$creds'")
47
48     @mock.patch('functest.cli.commands.cli_os.exit')
49     @mock.patch('functest.cli.commands.cli_os.click.echo')
50     def test_ping_endpoint_os_system_fails(self, mock_click_echo,
51                                            mock_exit):
52         self.cli_os.os_auth_url = self.os_auth_url
53         self.cli_os.endpoint_ip = self.endpoint_ip
54         with mock.patch('functest.cli.commands.cli_os.os.system',
55                         return_value=1):
56             self.cli_os.ping_endpoint()
57             mock_click_echo.assert_called_once_with("Cannot talk to the "
58                                                     "endpoint %s\n" %
59                                                     self.endpoint_ip)
60             mock_exit.assert_called_once_with(0)
61
62     @mock.patch('functest.cli.commands.cli_os.ft_utils.execute_command')
63     def test_check(self, mock_ftutils_execute):
64         with mock.patch.object(self.cli_os, 'ping_endpoint'):
65             self.cli_os.check()
66             mock_ftutils_execute.assert_called_once_with(
67                 "check_os.sh", verbose=False)
68
69     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
70                 return_value=False)
71     @mock.patch('functest.cli.commands.cli_os.click.echo')
72     def test_snapshot_create(self, mock_click_echo, mock_os_path):
73         with mock.patch.object(self.cli_os, 'ping_endpoint'), \
74                 mock.patch('functest.cli.commands.cli_os.os_snapshot.main') \
75                 as mock_os_snapshot:
76             self.cli_os.snapshot_create()
77             mock_click_echo.assert_called_once_with("Generating Openstack "
78                                                     "snapshot...")
79             self.assertTrue(mock_os_snapshot.called)
80
81     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
82                 return_value=True)
83     @mock.patch('functest.cli.commands.cli_os.click.echo')
84     def test_snapshot_create_overwrite(self, mock_click_echo, mock_os_path):
85         with mock.patch('__builtin__.raw_input', return_value="y") \
86                 as mock_raw_input, \
87                 mock.patch.object(self.cli_os, 'ping_endpoint'), \
88                 mock.patch('functest.cli.commands.cli_os.os_snapshot.main') \
89                 as mock_os_snapshot:
90             self.cli_os.snapshot_create()
91             mock_click_echo.assert_called_once_with("Generating Openstack "
92                                                     "snapshot...")
93             mock_raw_input.assert_any_call("It seems there is already an "
94                                            "OpenStack snapshot. Do you want "
95                                            "to overwrite it with the current "
96                                            "OpenStack status? [y|n]\n")
97             self.assertTrue(mock_os_snapshot.called)
98
99     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
100                 return_value=False)
101     @mock.patch('functest.cli.commands.cli_os.click.echo')
102     def test_snapshot_show_missing_snap(self, mock_click_echo, mock_os_path):
103         self.cli_os.snapshot_show()
104         mock_click_echo.assert_called_once_with("There is no OpenStack "
105                                                 "snapshot created. To create "
106                                                 "one run the command "
107                                                 "'functest openstack "
108                                                 "snapshot-create'")
109
110     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
111                 return_value=True)
112     @mock.patch('functest.cli.commands.cli_os.click.echo')
113     def test_snapshot_show_default(self, mock_click_echo, mock_os_path):
114         with mock.patch('__builtin__.open', mock.mock_open(read_data='0')) \
115                 as m:
116             self.cli_os.snapshot_file = self.snapshot_file
117             self.cli_os.snapshot_show()
118             m.assert_called_once_with(self.snapshot_file, 'r')
119             mock_click_echo.assert_called_once_with("\n0")
120
121     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
122                 return_value=True)
123     @mock.patch('functest.cli.commands.cli_os.click.echo')
124     def test_clean(self, mock_click_echo, mock_os_path):
125         with mock.patch.object(self.cli_os, 'ping_endpoint'), \
126                 mock.patch('functest.cli.commands.cli_os.os_clean.main') \
127                 as mock_os_clean:
128             self.cli_os.clean()
129             self.assertTrue(mock_os_clean.called)
130
131     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
132                 return_value=False)
133     @mock.patch('functest.cli.commands.cli_os.click.echo')
134     def test_clean_missing_file(self, mock_click_echo, mock_os_path):
135         with mock.patch.object(self.cli_os, 'ping_endpoint'):
136             self.cli_os.clean()
137             mock_click_echo.assert_called_once_with("Not possible to clean "
138                                                     "OpenStack without a "
139                                                     "snapshot. This could "
140                                                     "cause problems. "
141                                                     "Run first the command "
142                                                     "'functest openstack "
143                                                     "snapshot-create'")
144
145     @mock.patch('functest.cli.commands.cli_os.click.echo')
146     def test_show_credentials(self, mock_click_echo):
147         key = 'OS_KEY'
148         value = 'OS_VALUE'
149         with mock.patch.dict(os.environ, {key: value}):
150             self.cli_os.show_credentials()
151             mock_click_echo.assert_any_call("{}={}".format(key, value))
152
153
154 if __name__ == "__main__":
155     logging.disable(logging.CRITICAL)
156     unittest.main(verbosity=2)