Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/customizations/configservice/test_subscribe.py
1569 views
1
# Copyright 2014 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 botocore.session
14
from botocore.exceptions import ClientError
15
16
from awscli.testutils import mock, unittest
17
from awscli.compat import StringIO
18
from awscli.customizations.configservice.subscribe import SubscribeCommand, \
19
S3BucketHelper, SNSTopicHelper
20
21
22
class TestS3BucketHelper(unittest.TestCase):
23
def setUp(self):
24
self.session = botocore.session.get_session()
25
self.s3_client = mock.Mock(self.session.create_client('s3'))
26
self.helper = S3BucketHelper(self.s3_client)
27
self.error_response = {
28
'Error': {
29
'Code': '404',
30
'Message': 'Not Found'
31
}
32
}
33
self.bucket_no_exists_error = ClientError(
34
self.error_response,
35
'HeadBucket'
36
)
37
38
def test_correct_prefix_returned(self):
39
name = 'MyBucket/MyPrefix'
40
bucket, prefix = self.helper.prepare_bucket(name)
41
# Ensure the returned bucket and key are as expected
42
self.assertEqual(bucket, 'MyBucket')
43
self.assertEqual(prefix, 'MyPrefix')
44
45
def test_bucket_exists(self):
46
name = 'MyBucket'
47
bucket, prefix = self.helper.prepare_bucket(name)
48
# A new bucket should not have been created because no error was thrown
49
self.assertFalse(self.s3_client.create_bucket.called)
50
# Ensure the returned bucket and key are as expected
51
self.assertEqual(bucket, name)
52
self.assertEqual(prefix, '')
53
54
def test_bucket_no_exist(self):
55
name = 'MyBucket/MyPrefix'
56
self.s3_client.head_bucket.side_effect = self.bucket_no_exists_error
57
self.s3_client.meta.region_name = 'us-east-1'
58
bucket, prefix = self.helper.prepare_bucket(name)
59
# Ensure that the create bucket was called with the proper args.
60
self.s3_client.create_bucket.assert_called_with(
61
Bucket='MyBucket'
62
)
63
# Ensure the returned bucket and key are as expected
64
self.assertEqual(bucket, 'MyBucket')
65
self.assertEqual(prefix, 'MyPrefix')
66
67
def test_bucket_no_exist_with_location_constraint(self):
68
name = 'MyBucket/MyPrefix'
69
self.s3_client.head_bucket.side_effect = self.bucket_no_exists_error
70
self.s3_client.meta.region_name = 'us-west-2'
71
bucket, prefix = self.helper.prepare_bucket(name)
72
# Ensure that the create bucket was called with the proper args.
73
self.s3_client.create_bucket.assert_called_with(
74
Bucket='MyBucket',
75
CreateBucketConfiguration={'LocationConstraint': 'us-west-2'}
76
)
77
# Ensure the returned bucket and key are as expected
78
self.assertEqual(bucket, 'MyBucket')
79
self.assertEqual(prefix, 'MyPrefix')
80
81
def test_bucket_client_exception_non_404(self):
82
name = 'MyBucket/MyPrefix'
83
self.error_response['Error']['Code'] = '403'
84
self.error_response['Error']['Message'] = 'Forbidden'
85
forbidden_error = ClientError(self.error_response, 'HeadBucket')
86
self.s3_client.head_bucket.side_effect = forbidden_error
87
self.s3_client._endpoint.region_name = 'us-east-1'
88
bucket, prefix = self.helper.prepare_bucket(name)
89
# A new bucket should not have been created because a 404 error
90
# was not thrown
91
self.assertFalse(self.s3_client.create_bucket.called)
92
# Ensure the returned bucket and key are as expected
93
self.assertEqual(bucket, 'MyBucket')
94
self.assertEqual(prefix, 'MyPrefix')
95
96
def test_output_use_existing_bucket(self):
97
name = 'MyBucket/MyPrefix'
98
with mock.patch('sys.stdout', StringIO()) as mock_stdout:
99
self.helper.prepare_bucket(name)
100
self.assertIn(
101
'Using existing S3 bucket: MyBucket',
102
mock_stdout.getvalue())
103
104
def test_output_create_bucket(self):
105
name = 'MyBucket/MyPrefix'
106
self.s3_client.head_bucket.side_effect = self.bucket_no_exists_error
107
self.s3_client._endpoint.region_name = 'us-east-1'
108
with mock.patch('sys.stdout', StringIO()) as mock_stdout:
109
self.helper.prepare_bucket(name)
110
self.assertIn(
111
'Using new S3 bucket: MyBucket',
112
mock_stdout.getvalue())
113
114
115
class TestSNSTopicHelper(unittest.TestCase):
116
def setUp(self):
117
self.session = botocore.session.get_session()
118
self.sns_client = mock.Mock(self.session.create_client(
119
'sns', 'us-east-1'))
120
self.helper = SNSTopicHelper(self.sns_client)
121
122
def test_sns_topic_by_name(self):
123
name = 'mysnstopic'
124
self.sns_client.create_topic.return_value = {'TopicArn': 'myARN'}
125
sns_arn = self.helper.prepare_topic(name)
126
# Ensure that the topic was create and returned the expected arn
127
self.assertTrue(self.sns_client.create_topic.called)
128
self.assertEqual(sns_arn, 'myARN')
129
130
def test_sns_topic_by_arn(self):
131
name = 'arn:aws:sns:us-east-1:934212987125:config'
132
sns_arn = self.helper.prepare_topic(name)
133
# Ensure that the topic was not created and returned the expected arn
134
self.assertFalse(self.sns_client.create_topic.called)
135
self.assertEqual(sns_arn, name)
136
137
def test_output_existing_topic(self):
138
name = 'mysnstopic'
139
self.sns_client.create_topic.return_value = {'TopicArn': 'myARN'}
140
with mock.patch('sys.stdout', StringIO()) as mock_stdout:
141
self.helper.prepare_topic(name)
142
self.assertIn(
143
'Using new SNS topic: myARN',
144
mock_stdout.getvalue())
145
146
def test_output_new_topic(self):
147
name = 'arn:aws:sns:us-east-1:934212987125:config'
148
with mock.patch('sys.stdout', StringIO()) as mock_stdout:
149
self.helper.prepare_topic(name)
150
self.assertIn(
151
'Using existing SNS topic: %s' % name,
152
mock_stdout.getvalue())
153
154
155
class TestSubscribeCommand(unittest.TestCase):
156
def setUp(self):
157
self.session = botocore.session.get_session()
158
159
# Set up the client mocks.
160
self.s3_client = mock.Mock(self.session.create_client('s3'))
161
self.sns_client = mock.Mock(self.session.create_client(
162
'sns', 'us-east-1'))
163
self.config_client = mock.Mock(self.session.create_client(
164
'config', 'us-east-1'))
165
self.config_client.describe_configuration_recorders.return_value = \
166
{'ConfigurationRecorders': []}
167
self.config_client.describe_delivery_channels.return_value = \
168
{'DeliveryChannels': []}
169
170
self.session = mock.Mock(self.session)
171
self.session.create_client.side_effect = [
172
self.s3_client,
173
self.sns_client,
174
self.config_client
175
]
176
177
self.parsed_args = mock.Mock()
178
self.parsed_args.s3_bucket = 'MyBucket/MyPrefix'
179
self.parsed_args.sns_topic = \
180
'arn:aws:sns:us-east-1:934212987125:config'
181
self.parsed_args.iam_role = 'arn:aws:iam::1234556789:role/config'
182
183
self.parsed_globals = mock.Mock()
184
185
self.cmd = SubscribeCommand(self.session)
186
187
def test_setup_clients(self):
188
self.parsed_globals.verify_ssl = True
189
self.parsed_globals.region = 'us-east-1'
190
self.parsed_globals.endpoint_url = 'http://myendpoint.com'
191
192
self.cmd._run_main(self.parsed_args, self.parsed_globals)
193
194
# Check to see that the clients were created correctly
195
self.session.create_client.assert_any_call(
196
's3',
197
verify=self.parsed_globals.verify_ssl,
198
region_name=self.parsed_globals.region,
199
)
200
self.session.create_client.assert_any_call(
201
'sns',
202
verify=self.parsed_globals.verify_ssl,
203
region_name=self.parsed_globals.region,
204
)
205
self.session.create_client.assert_any_call(
206
'config',
207
verify=self.parsed_globals.verify_ssl,
208
region_name=self.parsed_globals.region,
209
endpoint_url=self.parsed_globals.endpoint_url
210
)
211
212
def test_subscribe(self):
213
self.cmd._run_main(self.parsed_args, self.parsed_globals)
214
215
# Check the call made to put configuration recorder.
216
self.config_client.put_configuration_recorder.assert_called_with(
217
ConfigurationRecorder={
218
'name': 'default',
219
'roleARN': self.parsed_args.iam_role
220
}
221
)
222
223
# Check the call made to put delivery channel.
224
self.config_client.put_delivery_channel.assert_called_with(
225
DeliveryChannel={
226
'name': 'default',
227
's3BucketName': 'MyBucket',
228
'snsTopicARN': self.parsed_args.sns_topic,
229
's3KeyPrefix': 'MyPrefix'
230
}
231
)
232
233
# Check the call made to start configuration recorder.
234
self.config_client.start_configuration_recorder.assert_called_with(
235
ConfigurationRecorderName='default'
236
)
237
238
# Check that the describe delivery channel and configuration recorder
239
# methods were called.
240
self.assertTrue(
241
self.config_client.describe_configuration_recorders.called
242
)
243
self.assertTrue(self.config_client.describe_delivery_channels.called)
244
245