-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtest_schemas_aws_config.py
More file actions
124 lines (111 loc) · 5.73 KB
/
test_schemas_aws_config.py
File metadata and controls
124 lines (111 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""Test AWS configuration"""
from unittest import TestCase
from unittest.mock import patch, ANY, Mock
from samcli.commands.local.cli_common.user_exceptions import ResourceNotFound, NotAvailableInRegion
from samcli.lib.schemas.schemas_aws_config import get_aws_configuration_choice
class TestInitAWSConfiguration(TestCase):
@patch("samcli.lib.schemas.schemas_aws_config.Session")
@patch("click.confirm")
def test_get_aws_configuration_profile_is_set_to_none_for_default_selection(self, confirm_mock, session_mock):
confirm_mock.side_effect = [True]
session_mock.return_value.profile_name = "default"
session_mock.return_value.region_name = "us-west-2"
aws_configuration_choice = get_aws_configuration_choice()
self.assertEqual(aws_configuration_choice["profile"], None)
self.assertEqual(aws_configuration_choice["region"], "us-west-2")
confirm_mock.assert_any_call(
"\nDo you want to use the default AWS profile [default] and region [us-west-2]?", default=True
)
@patch("samcli.lib.schemas.schemas_aws_config.Session")
@patch("click.confirm")
@patch("click.prompt")
def test_get_aws_configuration_choice_selected(self, prompt_mock, confirm_mock, session_mock):
confirm_mock.side_effect = [False]
prompt_mock.side_effect = ["2", "us-east-2"]
session_mock.return_value.profile_name = "default"
session_mock.return_value.region_name = "us-west-2"
session_mock.return_value.available_profiles = ["default", "test-profile"]
session_mock.return_value.get_available_regions.return_value = [
"us-east-1",
"us-east-2",
"us-west-2",
"eu-west-1",
"ap-northeast-1",
]
aws_configuration_choice = get_aws_configuration_choice()
self.assertEqual(aws_configuration_choice["profile"], "test-profile")
self.assertEqual(aws_configuration_choice["region"], "us-east-2")
confirm_mock.assert_any_call(
"\nDo you want to use the default AWS profile [default] and region [us-west-2]?", default=True
)
prompt_mock.assert_any_call("Profile", type=ANY, show_choices=False)
prompt_mock.assert_any_call("Region [us-west-2]", type=ANY, show_choices=False)
@patch("samcli.lib.schemas.schemas_aws_config.Session")
@patch("click.confirm")
def test_get_aws_configuration_raises_exception_when_no_profile_found(self, confirm_mock, session_mock):
confirm_mock.side_effect = [False]
session_mock.return_value.profile_name = "default"
session_mock.return_value.region_name = "us-west-2"
session_mock.return_value.available_profiles = []
with self.assertRaises(ResourceNotFound) as ctx:
get_aws_configuration_choice()
msg = "No configured AWS profile found."
self.assertEqual(str(ctx.exception), msg)
@patch("samcli.lib.schemas.schemas_aws_config.Session")
@patch("click.confirm")
@patch("click.prompt")
def test_get_aws_configuration_allow_free_text_region_value(self, prompt_mock, confirm_mock, session_mock):
confirm_mock.side_effect = [False]
prompt_mock.side_effect = ["2", "random-region"]
session_mock.return_value.profile_name = "default"
session_mock.return_value.region_name = "us-west-2"
session_mock.return_value.available_profiles = ["default", "test-profile"]
session_mock.return_value.get_available_regions.return_value = [
"us-east-1",
"us-east-2",
"us-west-2",
"eu-west-1",
"ap-northeast-1",
]
aws_configuration_choice = get_aws_configuration_choice()
self.assertEqual(aws_configuration_choice["profile"], "test-profile")
self.assertEqual(aws_configuration_choice["region"], "random-region")
confirm_mock.assert_any_call(
"\nDo you want to use the default AWS profile [default] and region [us-west-2]?", default=True
)
prompt_mock.assert_any_call("Profile", type=ANY, show_choices=False)
prompt_mock.assert_any_call("Region [us-west-2]", type=ANY, show_choices=False)
@patch("samcli.lib.schemas.schemas_aws_config.Session")
@patch("click.confirm")
@patch("click.prompt")
def test_get_aws_configuration_succeeds_with_default(self, prompt_mock, confirm_mock, session_mock):
region = "us-east-2"
confirm_mock.side_effect = [True]
prompt_mock.side_effect = ["1", region]
def profile_mock(**kwargs):
session = Mock()
session.profile_name = "default"
session.available_profiles = ["test-profile-1", "test-profile-2"]
session.get_available_regions.return_value = [
"us-east-1",
"us-east-2",
"us-west-2",
"eu-west-1",
"ap-northeast-1",
]
if "profile_name" in kwargs:
session.profile_name = kwargs["profile_name"]
session.region_name = region
else:
session.profile_name = "default"
session.region_name = None
return session
session_mock.side_effect = profile_mock
aws_configuration_choice = get_aws_configuration_choice()
self.assertEqual(aws_configuration_choice["profile"], "test-profile-1")
self.assertEqual(aws_configuration_choice["region"], region)
# Since the region will be None, the user should not get prompted to confirm
# whether to choose a different profile.
self.assertFalse(confirm_mock.called)
prompt_mock.assert_any_call("Profile", type=ANY, show_choices=False)
prompt_mock.assert_any_call(f"Region [{region}]", type=ANY, show_choices=False)