|
1 | 1 | """ |
2 | | - SoftLayer.tests.CLI.modules.config_tests |
3 | | - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 2 | + SoftLayer.tests.config_tests |
| 3 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4 | 4 |
|
5 | 5 | :license: MIT, see LICENSE for more details. |
6 | 6 | """ |
7 | | -import json |
8 | | -import tempfile |
9 | | - |
10 | 7 | import mock |
11 | 8 |
|
12 | | -from SoftLayer.CLI.config import setup as config |
13 | | -from SoftLayer.CLI import exceptions |
14 | | -from SoftLayer import consts |
| 9 | +from SoftLayer import config |
15 | 10 | from SoftLayer import testing |
16 | 11 |
|
17 | 12 |
|
18 | | -class TestHelpShow(testing.TestCase): |
19 | | - |
20 | | - def test_show(self): |
21 | | - result = self.run_command(['config', 'show']) |
22 | | - |
23 | | - self.assertEqual(result.exit_code, 0) |
24 | | - self.assertEqual(json.loads(result.output), |
25 | | - {'Username': 'default-user', |
26 | | - 'API Key': 'default-key', |
27 | | - 'Endpoint URL': 'not set', |
28 | | - 'Timeout': 'not set'}) |
29 | | - |
30 | | - |
31 | | -class TestHelpSetup(testing.TestCase): |
32 | | - |
33 | | - @mock.patch('SoftLayer.CLI.formatting.confirm') |
34 | | - @mock.patch('SoftLayer.CLI.environment.Environment.getpass') |
35 | | - @mock.patch('SoftLayer.CLI.environment.Environment.input') |
36 | | - def test_setup(self, input, getpass, confirm_mock): |
37 | | - with tempfile.NamedTemporaryFile() as config_file: |
38 | | - confirm_mock.return_value = True |
39 | | - getpass.return_value = 'A' * 64 |
40 | | - input.side_effect = ['user', 'public'] |
41 | | - |
42 | | - result = self.run_command(['--config=%s' % config_file.name, |
43 | | - 'config', 'setup']) |
44 | | - |
45 | | - self.assertEqual(result.exit_code, 0) |
46 | | - self.assertTrue('Configuration Updated Successfully' |
47 | | - in result.output) |
48 | | - contents = config_file.read().decode("utf-8") |
49 | | - self.assertTrue('[softlayer]' in contents) |
50 | | - self.assertTrue('username = user' in contents) |
51 | | - self.assertTrue('api_key = AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' |
52 | | - 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAA' in contents) |
53 | | - self.assertTrue('endpoint_url = %s' % consts.API_PUBLIC_ENDPOINT |
54 | | - in contents) |
55 | | - |
56 | | - @mock.patch('SoftLayer.CLI.formatting.confirm') |
57 | | - @mock.patch('SoftLayer.CLI.environment.Environment.getpass') |
58 | | - @mock.patch('SoftLayer.CLI.environment.Environment.input') |
59 | | - def test_setup_cancel(self, input, getpass, confirm_mock): |
60 | | - with tempfile.NamedTemporaryFile() as config_file: |
61 | | - confirm_mock.return_value = False |
62 | | - getpass.return_value = 'A' * 64 |
63 | | - input.side_effect = ['user', 'public'] |
64 | | - |
65 | | - result = self.run_command(['--config=%s' % config_file.name, |
66 | | - 'config', 'setup']) |
67 | | - |
68 | | - self.assertEqual(result.exit_code, 2) |
69 | | - self.assertIsInstance(result.exception, exceptions.CLIAbort) |
70 | | - |
71 | | - @mock.patch('SoftLayer.CLI.environment.Environment.getpass') |
72 | | - @mock.patch('SoftLayer.CLI.environment.Environment.input') |
73 | | - def test_get_user_input_private(self, input, getpass): |
74 | | - getpass.return_value = 'A' * 64 |
75 | | - input.side_effect = ['user', 'private'] |
76 | | - |
77 | | - username, secret, endpoint_url, timeout = ( |
78 | | - config.get_user_input(self.env)) |
79 | | - |
80 | | - self.assertEqual(username, 'user') |
81 | | - self.assertEqual(secret, 'A' * 64) |
82 | | - self.assertEqual(endpoint_url, consts.API_PRIVATE_ENDPOINT) |
83 | | - |
84 | | - @mock.patch('SoftLayer.CLI.environment.Environment.getpass') |
85 | | - @mock.patch('SoftLayer.CLI.environment.Environment.input') |
86 | | - def test_get_user_input_custom(self, input, getpass): |
87 | | - getpass.return_value = 'A' * 64 |
88 | | - input.side_effect = ['user', 'custom', 'custom-endpoint'] |
89 | | - |
90 | | - _, _, endpoint_url, _ = config.get_user_input(self.env) |
91 | | - |
92 | | - self.assertEqual(endpoint_url, 'custom-endpoint') |
93 | | - |
94 | | - @mock.patch('SoftLayer.CLI.environment.Environment.getpass') |
95 | | - @mock.patch('SoftLayer.CLI.environment.Environment.input') |
96 | | - def test_get_user_input_default(self, input, getpass): |
97 | | - self.env.getpass.return_value = 'A' * 64 |
98 | | - self.env.input.side_effect = ['user', ''] |
99 | | - |
100 | | - _, _, endpoint_url, _ = config.get_user_input(self.env) |
101 | | - |
102 | | - self.assertEqual(endpoint_url, consts.API_PUBLIC_ENDPOINT) |
| 13 | +class TestGetClientSettings(testing.TestCase): |
| 14 | + |
| 15 | + @mock.patch('SoftLayer.config.SETTING_RESOLVERS', []) |
| 16 | + def test_no_resolvers(self): |
| 17 | + result = config.get_client_settings() |
| 18 | + self.assertEqual(result, {}) |
| 19 | + |
| 20 | + def test_resolve_one(self): |
| 21 | + resolvers = [mock.Mock() for i in range(1)] |
| 22 | + resolvers[0].return_value = {'auth': 'AUTH HANDLER'} |
| 23 | + with mock.patch('SoftLayer.config.SETTING_RESOLVERS', resolvers): |
| 24 | + result = config.get_client_settings() |
| 25 | + self.assertEqual(result, {'auth': 'AUTH HANDLER'}) |
| 26 | + |
| 27 | + def test_inherit(self): |
| 28 | + # This tests the inheritting properties of the list of resolvers. |
| 29 | + # Values should be preferred on earlier resolvers except where their |
| 30 | + # value is false-ish |
| 31 | + resolvers = [mock.Mock() for i in range(4)] |
| 32 | + resolvers[0].return_value = {'timeout': 20} |
| 33 | + resolvers[1].return_value = {'timeout': 10, 'auth': None} |
| 34 | + resolvers[2].return_value = None |
| 35 | + resolvers[3].return_value = {'auth': 'AUTH HANDLER'} |
| 36 | + with mock.patch('SoftLayer.config.SETTING_RESOLVERS', resolvers): |
| 37 | + result = config.get_client_settings() |
| 38 | + self.assertEqual(result, {'auth': 'AUTH HANDLER', 'timeout': 20}) |
| 39 | + |
| 40 | + |
| 41 | +class TestGetClientSettingsArgs(testing.TestCase): |
| 42 | + |
| 43 | + def test_username_api_key(self): |
| 44 | + result = config.get_client_settings_args( |
| 45 | + username='username', |
| 46 | + api_key='api_key', |
| 47 | + endpoint_url='http://endpoint/', |
| 48 | + timeout=10, |
| 49 | + proxy='https://localhost:3128') |
| 50 | + |
| 51 | + self.assertEqual(result['endpoint_url'], 'http://endpoint/') |
| 52 | + self.assertEqual(result['timeout'], 10) |
| 53 | + self.assertEqual(result['username'], 'username') |
| 54 | + self.assertEqual(result['api_key'], 'api_key') |
| 55 | + self.assertEqual(result['proxy'], 'https://localhost:3128') |
| 56 | + |
| 57 | + |
| 58 | +class TestGetClientSettingsEnv(testing.TestCase): |
| 59 | + |
| 60 | + @mock.patch.dict('os.environ', {'SL_USERNAME': 'username', |
| 61 | + 'SL_API_KEY': 'api_key', |
| 62 | + 'https_proxy': 'https://localhost:3128'}) |
| 63 | + def test_username_api_key(self): |
| 64 | + result = config.get_client_settings_env() |
| 65 | + |
| 66 | + self.assertEqual(result['username'], 'username') |
| 67 | + self.assertEqual(result['api_key'], 'api_key') |
| 68 | + |
| 69 | + |
| 70 | +class TestGetClientSettingsConfigFile(testing.TestCase): |
| 71 | + |
| 72 | + @mock.patch('six.moves.configparser.RawConfigParser') |
| 73 | + def test_username_api_key(self, config_parser): |
| 74 | + result = config.get_client_settings_config_file() |
| 75 | + |
| 76 | + self.assertEqual(result['endpoint_url'], config_parser().get()) |
| 77 | + self.assertEqual(result['timeout'], config_parser().getfloat()) |
| 78 | + self.assertEqual(result['proxy'], config_parser().get()) |
| 79 | + self.assertEqual(result['username'], config_parser().get()) |
| 80 | + self.assertEqual(result['api_key'], config_parser().get()) |
| 81 | + |
| 82 | + @mock.patch('six.moves.configparser.RawConfigParser') |
| 83 | + def test_no_section(self, config_parser): |
| 84 | + config_parser().has_section.return_value = False |
| 85 | + result = config.get_client_settings_config_file() |
| 86 | + |
| 87 | + self.assertIsNone(result) |
| 88 | + |
| 89 | + @mock.patch('six.moves.configparser.RawConfigParser') |
| 90 | + def test_config_file(self, config_parser): |
| 91 | + config.get_client_settings_config_file(config_file='path/to/config') |
| 92 | + config_parser().read.assert_called_with([mock.ANY, |
| 93 | + mock.ANY, |
| 94 | + 'path/to/config']) |
0 commit comments