Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/customizations/test_globalargs.py
1567 views
1
# Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
from botocore.session import get_session
14
from botocore import UNSIGNED
15
import os
16
17
from awscli.testutils import mock, unittest
18
from awscli.customizations import globalargs
19
20
21
class FakeParsedArgs(object):
22
def __init__(self, **kwargs):
23
self.__dict__.update(kwargs)
24
25
def __getattr__(self, arg):
26
return None
27
28
29
class FakeSession(object):
30
31
def __init__(self, session_vars=None, config_file_vars=None):
32
if session_vars is None:
33
session_vars = {}
34
self.session_var_map = session_vars
35
if config_file_vars is None:
36
config_file_vars = {}
37
self.config_file_vars = config_file_vars
38
39
def get_config_variable(self, name, methods=('env', 'config'),
40
default=None):
41
value = None
42
config_name, envvar_name = self.session_var_map[name]
43
if methods is not None:
44
if 'env' in methods and value is None:
45
value = os.environ.get(envvar_name)
46
if 'config' in methods and value is None:
47
value = self.config_file_vars.get(config_name)
48
else:
49
value = default
50
return value
51
52
53
class TestGlobalArgsCustomization(unittest.TestCase):
54
55
def test_parse_query(self):
56
parsed_args = FakeParsedArgs(query='foo.bar')
57
globalargs.resolve_types(parsed_args)
58
# Assert that it looks like a jmespath parsed expression.
59
self.assertFalse(isinstance(parsed_args.query, str))
60
self.assertTrue(hasattr(parsed_args.query, 'search'))
61
62
def test_parse_query_error_message(self):
63
# Invalid jmespath expression.
64
parsed_args = FakeParsedArgs(query='foo.bar.')
65
with self.assertRaises(ValueError):
66
globalargs.resolve_types(parsed_args)
67
globalargs.resolve_types('query')
68
69
def test_parse_verify_ssl_default_value(self):
70
with mock.patch('os.environ', {}):
71
parsed_args = FakeParsedArgs(verify_ssl=True, ca_bundle=None)
72
session_var_map = {'ca_bundle': ('ca_bundle', 'AWS_CA_BUNDLE')}
73
session = FakeSession(session_vars=session_var_map)
74
globalargs.resolve_verify_ssl(parsed_args, session)
75
# None, so that botocore can apply it's default logic.
76
self.assertIsNone(parsed_args.verify_ssl)
77
78
def test_parse_verify_ssl_verify_turned_off(self):
79
with mock.patch('os.environ', {}):
80
parsed_args = FakeParsedArgs(verify_ssl=False, ca_bundle=None)
81
session_var_map = {'ca_bundle': ('ca_bundle', 'AWS_CA_BUNDLE')}
82
session = FakeSession(session_vars=session_var_map)
83
globalargs.resolve_verify_ssl(parsed_args, session)
84
self.assertFalse(parsed_args.verify_ssl)
85
86
def test_cli_overrides_cert_bundle(self):
87
environ = {}
88
with mock.patch('os.environ', environ):
89
parsed_args = FakeParsedArgs(
90
verify_ssl=True,
91
ca_bundle='/path/to/cli_bundle.pem')
92
config_file_vars = {}
93
session_var_map = {'ca_bundle': ('ca_bundle', 'AWS_CA_BUNDLE')}
94
session = FakeSession(
95
session_vars=session_var_map,
96
config_file_vars=config_file_vars)
97
globalargs.resolve_verify_ssl(parsed_args, session)
98
self.assertEqual(parsed_args.verify_ssl, '/path/to/cli_bundle.pem')
99
100
def test_cli_overrides_env_cert_bundle(self):
101
environ = {
102
'AWS_CA_BUNDLE': '/path/to/env_bundle.pem',
103
}
104
with mock.patch('os.environ', environ):
105
parsed_args = FakeParsedArgs(
106
verify_ssl=True,
107
ca_bundle='/path/to/cli_bundle.pem')
108
config_file_vars = {}
109
session_var_map = {'ca_bundle': ('ca_bundle', 'AWS_CA_BUNDLE')}
110
session = FakeSession(
111
session_vars=session_var_map,
112
config_file_vars=config_file_vars)
113
globalargs.resolve_verify_ssl(parsed_args, session)
114
self.assertEqual(parsed_args.verify_ssl, '/path/to/cli_bundle.pem')
115
116
def test_no_verify_ssl_overrides_cli_cert_bundle(self):
117
environ = {
118
'AWS_CA_BUNDLE': '/path/to/env_bundle.pem',
119
}
120
with mock.patch('os.environ', environ):
121
parsed_args = FakeParsedArgs(
122
verify_ssl=False,
123
ca_bundle='/path/to/cli_bundle.pem')
124
config_file_vars = {}
125
session_var_map = {'ca_bundle': ('ca_bundle', 'AWS_CA_BUNDLE')}
126
session = FakeSession(
127
session_vars=session_var_map,
128
config_file_vars=config_file_vars)
129
globalargs.resolve_verify_ssl(parsed_args, session)
130
self.assertFalse(parsed_args.verify_ssl)
131
132
def test_no_sign_request_if_option_specified(self):
133
args = FakeParsedArgs(sign_request=False)
134
session = mock.Mock()
135
with mock.patch('awscli.customizations.globalargs._update_default_client_config') as mock_update:
136
globalargs.no_sign_request(args, session)
137
mock_update.assert_called_once_with(session, 'signature_version', UNSIGNED)
138
139
def test_request_signed_by_default(self):
140
args = FakeParsedArgs(sign_request=True)
141
session = mock.Mock()
142
globalargs.no_sign_request(args, session)
143
self.assertFalse(session.register.called)
144
145
def test_invalid_endpoint_url(self):
146
# Invalid jmespath expression.
147
parsed_args = FakeParsedArgs(endpoint_url='missing-scheme.com')
148
with self.assertRaises(ValueError):
149
globalargs.resolve_types(parsed_args)
150
151
def test_valid_endpoint_url(self):
152
parsed_args = FakeParsedArgs(endpoint_url='http://custom-endpoint.com')
153
globalargs.resolve_types(parsed_args)
154
self.assertEqual(parsed_args.endpoint_url,
155
'http://custom-endpoint.com')
156
157
def test_cli_read_timeout(self):
158
parsed_args = FakeParsedArgs(read_timeout='60')
159
session = get_session()
160
globalargs.resolve_cli_read_timeout(parsed_args, session)
161
self.assertEqual(parsed_args.read_timeout, 60)
162
self.assertEqual(
163
session.get_default_client_config().read_timeout, 60)
164
165
def test_cli_connect_timeout(self):
166
parsed_args = FakeParsedArgs(connect_timeout='60')
167
session = get_session()
168
globalargs.resolve_cli_connect_timeout(parsed_args, session)
169
self.assertEqual(parsed_args.connect_timeout, 60)
170
self.assertEqual(
171
session.get_default_client_config().connect_timeout, 60)
172
173
def test_cli_read_timeout_for_blocking(self):
174
parsed_args = FakeParsedArgs(read_timeout='0')
175
session = get_session()
176
globalargs.resolve_cli_read_timeout(parsed_args, session)
177
self.assertEqual(parsed_args.read_timeout, None)
178
self.assertEqual(
179
session.get_default_client_config().read_timeout, None)
180
181
def test_cli_connect_timeout_for_blocking(self):
182
parsed_args = FakeParsedArgs(connect_timeout='0')
183
session = get_session()
184
globalargs.resolve_cli_connect_timeout(parsed_args, session)
185
self.assertEqual(parsed_args.connect_timeout, None)
186
self.assertEqual(
187
session.get_default_client_config().connect_timeout, None)
188
189