Remove call to fetch_os_creds.sh
[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     def test_check(self, mock_ftutils_execute):
66         with mock.patch.object(self.cli_os, 'ping_endpoint'):
67             CONST.__setattr__('dir_repo_functest', self.dir_repo_functest)
68             cmd = os.path.join(CONST.__getattribute__('dir_repo_functest'),
69                                "functest/ci/check_os.sh")
70             self.cli_os.check()
71             mock_ftutils_execute.assert_called_once_with(cmd, verbose=False)
72
73     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
74                 return_value=False)
75     @mock.patch('functest.cli.commands.cli_os.click.echo')
76     def test_snapshot_create(self, mock_click_echo, mock_os_path):
77         with mock.patch.object(self.cli_os, 'ping_endpoint'), \
78                 mock.patch('functest.cli.commands.cli_os.os_snapshot.main') \
79                 as mock_os_snapshot:
80             self.cli_os.snapshot_create()
81             mock_click_echo.assert_called_once_with("Generating Openstack "
82                                                     "snapshot...")
83             self.assertTrue(mock_os_snapshot.called)
84
85     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
86                 return_value=True)
87     @mock.patch('functest.cli.commands.cli_os.click.echo')
88     def test_snapshot_create_overwrite(self, mock_click_echo, mock_os_path):
89         with mock.patch('__builtin__.raw_input', return_value="y") \
90                 as mock_raw_input, \
91                 mock.patch.object(self.cli_os, 'ping_endpoint'), \
92                 mock.patch('functest.cli.commands.cli_os.os_snapshot.main') \
93                 as mock_os_snapshot:
94             self.cli_os.snapshot_create()
95             mock_click_echo.assert_called_once_with("Generating Openstack "
96                                                     "snapshot...")
97             mock_raw_input.assert_any_call("It seems there is already an "
98                                            "OpenStack snapshot. Do you want "
99                                            "to overwrite it with the current "
100                                            "OpenStack status? [y|n]\n")
101             self.assertTrue(mock_os_snapshot.called)
102
103     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
104                 return_value=False)
105     @mock.patch('functest.cli.commands.cli_os.click.echo')
106     def test_snapshot_show_missing_snap(self, mock_click_echo, mock_os_path):
107         self.cli_os.snapshot_show()
108         mock_click_echo.assert_called_once_with("There is no OpenStack "
109                                                 "snapshot created. To create "
110                                                 "one run the command "
111                                                 "'functest openstack "
112                                                 "snapshot-create'")
113
114     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
115                 return_value=True)
116     @mock.patch('functest.cli.commands.cli_os.click.echo')
117     def test_snapshot_show_default(self, mock_click_echo, mock_os_path):
118         with mock.patch('__builtin__.open', mock.mock_open(read_data='0')) \
119                 as m:
120             self.cli_os.snapshot_file = self.snapshot_file
121             self.cli_os.snapshot_show()
122             m.assert_called_once_with(self.snapshot_file, 'r')
123             mock_click_echo.assert_called_once_with("\n0")
124
125     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
126                 return_value=True)
127     @mock.patch('functest.cli.commands.cli_os.click.echo')
128     def test_clean(self, mock_click_echo, mock_os_path):
129         with mock.patch.object(self.cli_os, 'ping_endpoint'), \
130                 mock.patch('functest.cli.commands.cli_os.os_clean.main') \
131                 as mock_os_clean:
132             self.cli_os.clean()
133             self.assertTrue(mock_os_clean.called)
134
135     @mock.patch('functest.cli.commands.cli_os.os.path.isfile',
136                 return_value=False)
137     @mock.patch('functest.cli.commands.cli_os.click.echo')
138     def test_clean_missing_file(self, mock_click_echo, mock_os_path):
139         with mock.patch.object(self.cli_os, 'ping_endpoint'):
140             self.cli_os.clean()
141             mock_click_echo.assert_called_once_with("Not possible to clean "
142                                                     "OpenStack without a "
143                                                     "snapshot. This could "
144                                                     "cause problems. "
145                                                     "Run first the command "
146                                                     "'functest openstack "
147                                                     "snapshot-create'")
148
149     @mock.patch('functest.cli.commands.cli_os.click.echo')
150     def test_show_credentials(self, mock_click_echo):
151         key = 'OS_KEY'
152         value = 'OS_VALUE'
153         with mock.patch.dict(os.environ, {key: value}):
154             self.cli_os.show_credentials()
155             mock_click_echo.assert_any_call("{}={}".format(key, value))
156
157
158 if __name__ == "__main__":
159     logging.disable(logging.CRITICAL)
160     unittest.main(verbosity=2)