Path: blob/develop/tests/unit/customizations/configure/test_list.py
1569 views
# Copyright 2016 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.12from argparse import Namespace1314from awscli.testutils import mock, unittest15from awscli.customizations.configure.list import ConfigureListCommand16from awscli.compat import StringIO1718from . import FakeSession192021class TestConfigureListCommand(unittest.TestCase):2223def test_configure_list_command_nothing_set(self):24# Test the case where the user only wants to change a single_value.25session = FakeSession(26all_variables={'config_file': '/config/location'})27session.full_config = {28'profiles': {'default': {'region': 'AWS_DEFAULT_REGION'}}}29stream = StringIO()30self.configure_list = ConfigureListCommand(session, stream)31self.configure_list(args=[], parsed_globals=None)32rendered = stream.getvalue()33self.assertRegex(rendered, r'profile\s+<not set>')34self.assertRegex(rendered, r'access_key\s+<not set>')35self.assertRegex(rendered, r'secret_key\s+<not set>')36self.assertRegex(rendered, r'region\s+<not set>')3738def test_configure_from_env(self):39env_vars = {40'profile': 'myprofilename'41}42session = FakeSession(43all_variables={'config_file': '/config/location'},44environment_vars=env_vars)45session.session_var_map = {'profile': (None, "PROFILE_ENV_VAR")}46session.full_config = {47'profiles': {'default': {'region': 'AWS_DEFAULT_REGION'}}}48stream = StringIO()49self.configure_list = ConfigureListCommand(session, stream)50self.configure_list(args=[], parsed_globals=None)51rendered = stream.getvalue()52self.assertRegex(53rendered, r'profile\s+myprofilename\s+env\s+PROFILE_ENV_VAR')5455def test_configure_from_config_file(self):56config_file_vars = {57'region': 'us-west-2'58}59session = FakeSession(60all_variables={'config_file': '/config/location'},61config_file_vars=config_file_vars)62session.session_var_map = {'region': ('region', "AWS_DEFAULT_REGION")}63session.full_config = {64'profiles': {'default': {'region': 'AWS_DEFAULT_REGION'}}}65stream = StringIO()66self.configure_list = ConfigureListCommand(session, stream)67self.configure_list(args=[], parsed_globals=None)68rendered = stream.getvalue()69self.assertRegex(70rendered, r'region\s+us-west-2\s+config-file\s+/config/location')7172def test_configure_from_multiple_sources(self):73# Here the profile is from an env var, the74# region is from the config file, and the credentials75# are from an iam-role.76env_vars = {77'profile': 'myprofilename'78}79config_file_vars = {80'region': 'us-west-2'81}82credentials = mock.Mock()83credentials.access_key = 'access_key'84credentials.secret_key = 'secret_key'85credentials.method = 'iam-role'86session = FakeSession(87all_variables={'config_file': '/config/location'},88environment_vars=env_vars,89config_file_vars=config_file_vars,90credentials=credentials)91session.session_var_map = {92'region': ('region', 'AWS_DEFAULT_REGION'),93'profile': ('profile', 'AWS_DEFAULT_PROFILE')}94session.full_config = {95'profiles': {'default': {'region': 'AWS_DEFAULT_REGION'}}}96stream = StringIO()97self.configure_list = ConfigureListCommand(session, stream)98self.configure_list(args=[], parsed_globals=None)99rendered = stream.getvalue()100# The profile came from an env var.101self.assertRegex(102rendered, r'profile\s+myprofilename\s+env\s+AWS_DEFAULT_PROFILE')103# The region came from the config file.104self.assertRegex(105rendered, r'region\s+us-west-2\s+config-file\s+/config/location')106# The credentials came from an IAM role. Note how we're107# also checking that the access_key/secret_key are masked108# with '*' chars except for the last 4 chars.109self.assertRegex(110rendered, r'access_key\s+\*+_key\s+iam-role')111self.assertRegex(112rendered, r'secret_key\s+\*+_key\s+iam-role')113114def test_configure_from_args(self):115parsed_globals = Namespace(profile='foo')116env_vars = {117'profile': 'myprofilename'118}119session = FakeSession(120all_variables={'config_file': '/config/location'},121profile='foo', environment_vars=env_vars)122session.session_var_map = {'profile': (None, ['AWS_PROFILE'])}123session.full_config = {124'profiles': {'foo': {'region': 'AWS_REGION'}}}125stream = StringIO()126self.configure_list = ConfigureListCommand(session, stream)127self.configure_list(args=[], parsed_globals=parsed_globals)128rendered = stream.getvalue()129self.assertRegex(130rendered, r'profile\s+foo\s+manual\s+--profile')131132133