Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/functional/configured_endpoint_urls/test_configured_endpoint_url.py
1569 views
1
# Copyright 2023 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
import json
14
from pathlib import Path
15
16
import pytest
17
18
from tests import CLIRunner
19
from awscli.compat import urlparse
20
21
22
ENDPOINT_TESTDATA_FILE = Path(__file__).parent / "profile-tests.json"
23
24
25
def dict_to_ini_section(ini_dict, section_header):
26
section_str = f'[{section_header}]\n'
27
for key, value in ini_dict.items():
28
if isinstance(value, dict):
29
section_str += f"{key} =\n"
30
for new_key, new_value in value.items():
31
section_str += f" {new_key}={new_value}\n"
32
else:
33
section_str += f"{key}={value}\n"
34
return section_str + "\n"
35
36
37
def create_cases():
38
with open(ENDPOINT_TESTDATA_FILE) as f:
39
test_suite = json.load(f)['testSuites'][0]
40
41
for test_case_data in test_suite['endpointUrlTests']:
42
yield pytest.param(
43
{
44
'service': test_case_data['service'],
45
'profile': test_case_data['profile'],
46
'expected_endpoint_url': test_case_data['output'][
47
'endpointUrl'
48
],
49
'client_args': test_suite['client_configs'].get(
50
test_case_data['client_config'], {}
51
),
52
'config_file_contents': get_config_file_contents(
53
test_case_data['profile'], test_suite
54
),
55
'environment': test_suite['environments'].get(
56
test_case_data['environment'], {}
57
),
58
},
59
marks=pytest.mark.skipif(
60
'ignore_configured_endpoint_urls' in (
61
test_suite['client_configs']
62
.get(test_case_data['client_config'], {})
63
),
64
reason="Parameter not supported on the command line"
65
),
66
id=test_case_data['name']
67
)
68
69
70
def get_config_file_contents(profile_name, test_suite):
71
profile = test_suite['profiles'][profile_name]
72
73
profile_str = dict_to_ini_section(
74
profile,
75
section_header=f"profile {profile_name}",
76
)
77
78
services_section_name = profile.get('services', None)
79
80
if services_section_name is None:
81
return profile_str
82
83
services_section = test_suite['services'][services_section_name]
84
85
service_section_str = dict_to_ini_section(
86
services_section,
87
section_header=f'services {services_section_name}',
88
)
89
90
return profile_str + service_section_str
91
92
93
def _normalize_endpoint(url):
94
split_endpoint = urlparse.urlsplit(url)
95
actual_endpoint = f"{split_endpoint.scheme}://{split_endpoint.netloc}"
96
return actual_endpoint
97
98
99
SERVICE_TO_OPERATION = {'s3api': 'list-buckets', 'dynamodb': 'list-tables'}
100
101
102
class TestConfiguredEndpointUrl:
103
def assert_endpoint_used(
104
self, cli_runner_result, test_case
105
):
106
107
aws_request = cli_runner_result.aws_requests[0]
108
assert test_case['expected_endpoint_url'] == \
109
_normalize_endpoint(aws_request.http_requests[0].url)
110
111
def _create_command(self, test_case):
112
service = test_case['service']
113
if test_case['service'] == 's3':
114
service = 's3api'
115
116
cmd = [
117
service,
118
SERVICE_TO_OPERATION[service],
119
'--profile',
120
f'{test_case["profile"]}'
121
]
122
123
if test_case['client_args'].get('endpoint_url', None):
124
cmd.extend([
125
'--endpoint-url',
126
f'{test_case["client_args"]["endpoint_url"]}'
127
]
128
)
129
130
return cmd
131
132
@pytest.mark.parametrize('test_case', create_cases())
133
def test_resolve_configured_endpoint_url(self, tmp_path, test_case):
134
cli_runner = CLIRunner()
135
136
config_filename = tmp_path / 'config'
137
138
with open(config_filename, 'w') as f:
139
f.write(test_case['config_file_contents'])
140
f.flush()
141
142
cli_runner.env['AWS_CONFIG_FILE'] = config_filename
143
cli_runner.env.update(test_case['environment'])
144
cli_runner.env.pop('AWS_DEFAULT_REGION')
145
146
result = cli_runner.run(self._create_command(test_case))
147
148
self.assert_endpoint_used(result, test_case)
149
150