Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/functional/configservice/test_subscribe.py
1567 views
1
# Copyright 2015 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 awscli.testutils import mock, BaseAWSCommandParamsTest
14
from awscli.customizations.configservice.subscribe import S3BucketHelper
15
16
17
class TestSubscribe(BaseAWSCommandParamsTest):
18
prefix = 'configservice subscribe'
19
20
def setUp(self):
21
super(TestSubscribe, self).setUp()
22
self.parsed_responses = [
23
{}, # S3 HeadBucket
24
{'TopicArn': 'my-topic-arn'}, # SNS CreateTopic
25
{}, # PutConfigurationRecorder
26
{}, # PutDeliveryChannel
27
{}, # StartConfigurationRecorder
28
{'ConfigurationRecorders': {}}, # DescribeConfigurationRecorders
29
{'DeliveryChannels': {}} # DescribeDeliveryChannels
30
]
31
32
def test_subscribe_when_bucket_exists_and_new_sns_topic(self):
33
self.prefix += ' --s3-bucket mybucket --sns-topic mytopic'
34
self.prefix += ' --iam-role myrole'
35
self.run_cmd(self.prefix)
36
37
self.assertEqual(len(self.operations_called), 7)
38
list_of_operation_names_called = []
39
list_of_parameters_called = []
40
for operation_called in self.operations_called:
41
list_of_operation_names_called.append(operation_called[0].name)
42
list_of_parameters_called.append(operation_called[1])
43
44
self.assertEqual(
45
list_of_operation_names_called, [
46
'HeadBucket',
47
'CreateTopic',
48
'PutConfigurationRecorder',
49
'PutDeliveryChannel',
50
'StartConfigurationRecorder',
51
'DescribeConfigurationRecorders',
52
'DescribeDeliveryChannels'
53
]
54
)
55
self.assertEqual(
56
list_of_parameters_called, [
57
{'Bucket': 'mybucket'}, # S3 HeadBucket
58
{'Name': 'mytopic'}, # SNS CreateTopic
59
{'ConfigurationRecorder': { # PutConfigurationRecorder
60
'name': 'default', 'roleARN': 'myrole'}},
61
{'DeliveryChannel': { # PutDeliveryChannel
62
'name': 'default',
63
's3BucketName': 'mybucket',
64
'snsTopicARN': 'my-topic-arn'}},
65
# StartConfigurationRecorder
66
{'ConfigurationRecorderName': 'default'},
67
{}, # DescribeConfigurationRecorders
68
{} # DescribeDeliveryChannels
69
]
70
)
71
72
def test_subscribe_when_bucket_exists_and_sns_topic_arn_provided(self):
73
self.parsed_responses.pop(1)
74
self.prefix += ' --s3-bucket mybucket --sns-topic arn:mytopic'
75
self.prefix += ' --iam-role myrole'
76
self.run_cmd(self.prefix)
77
78
self.assertEqual(len(self.operations_called), 6)
79
list_of_operation_names_called = []
80
list_of_parameters_called = []
81
for operation_called in self.operations_called:
82
list_of_operation_names_called.append(operation_called[0].name)
83
list_of_parameters_called.append(operation_called[1])
84
85
self.assertEqual(
86
list_of_operation_names_called, [
87
'HeadBucket',
88
'PutConfigurationRecorder',
89
'PutDeliveryChannel',
90
'StartConfigurationRecorder',
91
'DescribeConfigurationRecorders',
92
'DescribeDeliveryChannels'
93
]
94
)
95
self.assertEqual(
96
list_of_parameters_called, [
97
{'Bucket': 'mybucket'}, # S3 HeadBucket
98
{'ConfigurationRecorder': { # PutConfigurationRecorder
99
'name': 'default', 'roleARN': 'myrole'}},
100
{'DeliveryChannel': { # PutDeliveryChannel
101
'name': 'default',
102
's3BucketName': 'mybucket',
103
'snsTopicARN': 'arn:mytopic'}},
104
# StartConfigurationRecorder
105
{'ConfigurationRecorderName': 'default'},
106
{}, # DescribeConfigurationRecorders
107
{} # DescribeDeliveryChannels
108
]
109
)
110
111
def test_subscribe_when_bucket_needs_to_be_created(self):
112
# TODO: fix this patch when we have a better way to stub out responses
113
with mock.patch('botocore.endpoint.Endpoint._send') as \
114
http_session_send_patch:
115
# Mock for HeadBucket request
116
head_bucket_response = mock.Mock()
117
head_bucket_response.status_code = 404
118
head_bucket_response.content = b''
119
head_bucket_response.headers = {}
120
121
# Mock for CreateBucket request
122
create_bucket_response = mock.Mock()
123
create_bucket_response.status_code = 200
124
create_bucket_response.content = b''
125
create_bucket_response.headers = {}
126
127
http_session_send_patch.side_effect = [
128
head_bucket_response, create_bucket_response
129
]
130
131
s3_client = self.driver.session.create_client('s3')
132
bucket_helper = S3BucketHelper(s3_client)
133
bucket_helper.prepare_bucket('mybucket')
134
send_call_list = http_session_send_patch.call_args_list
135
self.assertEqual(send_call_list[0][0][0].method, 'HEAD')
136
# Since the HeadObject fails with 404, the CreateBucket which is
137
# is a PUT request should be made.
138
self.assertEqual(send_call_list[1][0][0].method, 'PUT')
139
140