Improve the way of getting env values
[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 # pylint: disable=missing-docstring
10
11 import logging
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_env_file'
28         self.snapshot_file = 'test_snapshot_file'
29         os.environ["OS_AUTH_URL"] = ''
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_auth_url_ko(self, mock_click_echo, 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(
46             "Source the OpenStack credentials first")
47         mock_exit.assert_called_once_with(0)
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_system_fails(self, mock_click_echo, 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(
58                 "Cannot talk to the endpoint %s\n" % self.endpoint_ip)
59             mock_exit.assert_called_once_with(0)
60
61     def test_check(self):
62         with mock.patch.object(self.cli_os, 'ping_endpoint'), \
63             mock.patch('functest.cli.commands.cli_os.check_deployment.'
64                        'CheckDeployment') as mock_check_deployment:
65             self.cli_os.check()
66             self.assertTrue(mock_check_deployment.called)
67
68     @mock.patch('functest.cli.commands.cli_os.click.echo')
69     def test_show_credentials(self, mock_click_echo):
70         key = 'OS_KEY'
71         value = 'OS_VALUE'
72         with mock.patch.dict(os.environ, {key: value}):
73             self.cli_os.show_credentials()
74             mock_click_echo.assert_any_call("{}={}".format(key, value))
75
76
77 if __name__ == "__main__":
78     logging.disable(logging.CRITICAL)
79     unittest.main(verbosity=2)