Path: blob/develop/tests/functional/configured_endpoint_urls/test_configured_endpoint_url.py
1569 views
# Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0/7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.12import json13from pathlib import Path1415import pytest1617from tests import CLIRunner18from awscli.compat import urlparse192021ENDPOINT_TESTDATA_FILE = Path(__file__).parent / "profile-tests.json"222324def dict_to_ini_section(ini_dict, section_header):25section_str = f'[{section_header}]\n'26for key, value in ini_dict.items():27if isinstance(value, dict):28section_str += f"{key} =\n"29for new_key, new_value in value.items():30section_str += f" {new_key}={new_value}\n"31else:32section_str += f"{key}={value}\n"33return section_str + "\n"343536def create_cases():37with open(ENDPOINT_TESTDATA_FILE) as f:38test_suite = json.load(f)['testSuites'][0]3940for test_case_data in test_suite['endpointUrlTests']:41yield pytest.param(42{43'service': test_case_data['service'],44'profile': test_case_data['profile'],45'expected_endpoint_url': test_case_data['output'][46'endpointUrl'47],48'client_args': test_suite['client_configs'].get(49test_case_data['client_config'], {}50),51'config_file_contents': get_config_file_contents(52test_case_data['profile'], test_suite53),54'environment': test_suite['environments'].get(55test_case_data['environment'], {}56),57},58marks=pytest.mark.skipif(59'ignore_configured_endpoint_urls' in (60test_suite['client_configs']61.get(test_case_data['client_config'], {})62),63reason="Parameter not supported on the command line"64),65id=test_case_data['name']66)676869def get_config_file_contents(profile_name, test_suite):70profile = test_suite['profiles'][profile_name]7172profile_str = dict_to_ini_section(73profile,74section_header=f"profile {profile_name}",75)7677services_section_name = profile.get('services', None)7879if services_section_name is None:80return profile_str8182services_section = test_suite['services'][services_section_name]8384service_section_str = dict_to_ini_section(85services_section,86section_header=f'services {services_section_name}',87)8889return profile_str + service_section_str909192def _normalize_endpoint(url):93split_endpoint = urlparse.urlsplit(url)94actual_endpoint = f"{split_endpoint.scheme}://{split_endpoint.netloc}"95return actual_endpoint969798SERVICE_TO_OPERATION = {'s3api': 'list-buckets', 'dynamodb': 'list-tables'}99100101class TestConfiguredEndpointUrl:102def assert_endpoint_used(103self, cli_runner_result, test_case104):105106aws_request = cli_runner_result.aws_requests[0]107assert test_case['expected_endpoint_url'] == \108_normalize_endpoint(aws_request.http_requests[0].url)109110def _create_command(self, test_case):111service = test_case['service']112if test_case['service'] == 's3':113service = 's3api'114115cmd = [116service,117SERVICE_TO_OPERATION[service],118'--profile',119f'{test_case["profile"]}'120]121122if test_case['client_args'].get('endpoint_url', None):123cmd.extend([124'--endpoint-url',125f'{test_case["client_args"]["endpoint_url"]}'126]127)128129return cmd130131@pytest.mark.parametrize('test_case', create_cases())132def test_resolve_configured_endpoint_url(self, tmp_path, test_case):133cli_runner = CLIRunner()134135config_filename = tmp_path / 'config'136137with open(config_filename, 'w') as f:138f.write(test_case['config_file_contents'])139f.flush()140141cli_runner.env['AWS_CONFIG_FILE'] = config_filename142cli_runner.env.update(test_case['environment'])143cli_runner.env.pop('AWS_DEFAULT_REGION')144145result = cli_runner.run(self._create_command(test_case))146147self.assert_endpoint_used(result, test_case)148149150