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