Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/test_paramfile.py
1566 views
1
# Copyright 2013 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 awscli.testutils import mock, unittest, FileCreator
14
from awscli.testutils import skip_if_windows
15
16
from awscli.paramfile import (
17
get_paramfile,
18
ResourceLoadingError,
19
LOCAL_PREFIX_MAP,
20
REMOTE_PREFIX_MAP,
21
register_uri_param_handler,
22
)
23
from botocore.session import Session
24
from botocore.exceptions import ProfileNotFound
25
26
27
class TestParamFile(unittest.TestCase):
28
def setUp(self):
29
self.files = FileCreator()
30
31
def tearDown(self):
32
self.files.remove_all()
33
34
def get_paramfile(self, path):
35
return get_paramfile(path, LOCAL_PREFIX_MAP.copy())
36
37
def test_text_file(self):
38
contents = 'This is a test'
39
filename = self.files.create_file('foo', contents)
40
prefixed_filename = 'file://' + filename
41
data = self.get_paramfile(prefixed_filename)
42
self.assertEqual(data, contents)
43
self.assertIsInstance(data, str)
44
45
def test_binary_file(self):
46
contents = 'This is a test'
47
filename = self.files.create_file('foo', contents)
48
prefixed_filename = 'fileb://' + filename
49
data = self.get_paramfile(prefixed_filename)
50
self.assertEqual(data, b'This is a test')
51
self.assertIsInstance(data, bytes)
52
53
@skip_if_windows('Binary content error only occurs '
54
'on non-Windows platforms.')
55
def test_cannot_load_text_file(self):
56
contents = b'\xbfX\xac\xbe'
57
filename = self.files.create_file('foo', contents, mode='wb')
58
prefixed_filename = 'file://' + filename
59
with self.assertRaises(ResourceLoadingError):
60
self.get_paramfile(prefixed_filename)
61
62
def test_file_does_not_exist_raises_error(self):
63
with self.assertRaises(ResourceLoadingError):
64
self.get_paramfile('file://file/does/not/existsasdf.txt')
65
66
def test_no_match_uris_returns_none(self):
67
self.assertIsNone(self.get_paramfile('foobar://somewhere.bar'))
68
69
def test_non_string_type_returns_none(self):
70
self.assertIsNone(self.get_paramfile(100))
71
72
73
class TestHTTPBasedResourceLoading(unittest.TestCase):
74
def setUp(self):
75
self.session_patch = mock.patch('awscli.paramfile.URLLib3Session.send')
76
self.session_mock = self.session_patch.start()
77
self.response = mock.Mock(status_code=200)
78
self.session_mock.return_value = self.response
79
80
def tearDown(self):
81
self.session_patch.stop()
82
83
def get_paramfile(self, path):
84
return get_paramfile(path, REMOTE_PREFIX_MAP.copy())
85
86
def test_resource_from_http(self):
87
self.response.text = 'http contents'
88
loaded = self.get_paramfile('http://foo.bar.baz')
89
self.assertEqual(loaded, 'http contents')
90
91
def test_resource_from_https(self):
92
self.response.text = 'http contents'
93
loaded = self.get_paramfile('https://foo.bar.baz')
94
self.assertEqual(loaded, 'http contents')
95
96
def test_non_200_raises_error(self):
97
self.response.status_code = 500
98
with self.assertRaisesRegex(ResourceLoadingError, 'foo\.bar\.baz'):
99
self.get_paramfile('https://foo.bar.baz')
100
101
def test_connection_error_raises_error(self):
102
self.session_mock.side_effect = Exception("Connection error.")
103
with self.assertRaisesRegex(ResourceLoadingError, 'foo\.bar\.baz'):
104
self.get_paramfile('https://foo.bar.baz')
105
106
107
class TestConfigureURIArgumentHandler(unittest.TestCase):
108
@mock.patch('awscli.paramfile.URIArgumentHandler')
109
def test_profile_not_found(self, mock_handler_cls):
110
session = mock.Mock(spec=Session)
111
session.get_scoped_config.side_effect = ProfileNotFound(profile='foo')
112
113
register_uri_param_handler(session)
114
cases = mock_handler_cls.call_args[0][0]
115
116
self.assertIn('file://', cases)
117
self.assertIn('fileb://', cases)
118
self.assertIn('http://', cases)
119
self.assertIn('http://', cases)
120
121
@mock.patch('awscli.paramfile.URIArgumentHandler')
122
def test_missing_config_value(self, mock_handler_cls):
123
session = mock.Mock(spec=Session)
124
session.get_scoped_config.return_value = {}
125
126
register_uri_param_handler(session)
127
cases = mock_handler_cls.call_args[0][0]
128
129
self.assertIn('file://', cases)
130
self.assertIn('fileb://', cases)
131
self.assertIn('http://', cases)
132
self.assertIn('http://', cases)
133
134
@mock.patch('awscli.paramfile.URIArgumentHandler')
135
def test_config_value_true(self, mock_handler_cls):
136
session = mock.Mock(spec=Session)
137
session.get_scoped_config.return_value = {
138
'cli_follow_urlparam': 'true'}
139
140
register_uri_param_handler(session)
141
cases = mock_handler_cls.call_args[0][0]
142
143
self.assertIn('file://', cases)
144
self.assertIn('fileb://', cases)
145
self.assertIn('http://', cases)
146
self.assertIn('http://', cases)
147
148
@mock.patch('awscli.paramfile.URIArgumentHandler')
149
def test_config_value_false(self, mock_handler_cls):
150
session = mock.Mock(spec=Session)
151
session.get_scoped_config.return_value = {
152
'cli_follow_urlparam': 'false'}
153
154
register_uri_param_handler(session)
155
cases = mock_handler_cls.call_args[0][0]
156
157
self.assertIn('file://', cases)
158
self.assertIn('fileb://', cases)
159
self.assertNotIn('http://', cases)
160
self.assertNotIn('http://', cases)
161
162