Path: blob/develop/tests/functional/configservice/test_subscribe.py
1567 views
# Copyright 2015 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 awscli.testutils import mock, BaseAWSCommandParamsTest13from awscli.customizations.configservice.subscribe import S3BucketHelper141516class TestSubscribe(BaseAWSCommandParamsTest):17prefix = 'configservice subscribe'1819def setUp(self):20super(TestSubscribe, self).setUp()21self.parsed_responses = [22{}, # S3 HeadBucket23{'TopicArn': 'my-topic-arn'}, # SNS CreateTopic24{}, # PutConfigurationRecorder25{}, # PutDeliveryChannel26{}, # StartConfigurationRecorder27{'ConfigurationRecorders': {}}, # DescribeConfigurationRecorders28{'DeliveryChannels': {}} # DescribeDeliveryChannels29]3031def test_subscribe_when_bucket_exists_and_new_sns_topic(self):32self.prefix += ' --s3-bucket mybucket --sns-topic mytopic'33self.prefix += ' --iam-role myrole'34self.run_cmd(self.prefix)3536self.assertEqual(len(self.operations_called), 7)37list_of_operation_names_called = []38list_of_parameters_called = []39for operation_called in self.operations_called:40list_of_operation_names_called.append(operation_called[0].name)41list_of_parameters_called.append(operation_called[1])4243self.assertEqual(44list_of_operation_names_called, [45'HeadBucket',46'CreateTopic',47'PutConfigurationRecorder',48'PutDeliveryChannel',49'StartConfigurationRecorder',50'DescribeConfigurationRecorders',51'DescribeDeliveryChannels'52]53)54self.assertEqual(55list_of_parameters_called, [56{'Bucket': 'mybucket'}, # S3 HeadBucket57{'Name': 'mytopic'}, # SNS CreateTopic58{'ConfigurationRecorder': { # PutConfigurationRecorder59'name': 'default', 'roleARN': 'myrole'}},60{'DeliveryChannel': { # PutDeliveryChannel61'name': 'default',62's3BucketName': 'mybucket',63'snsTopicARN': 'my-topic-arn'}},64# StartConfigurationRecorder65{'ConfigurationRecorderName': 'default'},66{}, # DescribeConfigurationRecorders67{} # DescribeDeliveryChannels68]69)7071def test_subscribe_when_bucket_exists_and_sns_topic_arn_provided(self):72self.parsed_responses.pop(1)73self.prefix += ' --s3-bucket mybucket --sns-topic arn:mytopic'74self.prefix += ' --iam-role myrole'75self.run_cmd(self.prefix)7677self.assertEqual(len(self.operations_called), 6)78list_of_operation_names_called = []79list_of_parameters_called = []80for operation_called in self.operations_called:81list_of_operation_names_called.append(operation_called[0].name)82list_of_parameters_called.append(operation_called[1])8384self.assertEqual(85list_of_operation_names_called, [86'HeadBucket',87'PutConfigurationRecorder',88'PutDeliveryChannel',89'StartConfigurationRecorder',90'DescribeConfigurationRecorders',91'DescribeDeliveryChannels'92]93)94self.assertEqual(95list_of_parameters_called, [96{'Bucket': 'mybucket'}, # S3 HeadBucket97{'ConfigurationRecorder': { # PutConfigurationRecorder98'name': 'default', 'roleARN': 'myrole'}},99{'DeliveryChannel': { # PutDeliveryChannel100'name': 'default',101's3BucketName': 'mybucket',102'snsTopicARN': 'arn:mytopic'}},103# StartConfigurationRecorder104{'ConfigurationRecorderName': 'default'},105{}, # DescribeConfigurationRecorders106{} # DescribeDeliveryChannels107]108)109110def test_subscribe_when_bucket_needs_to_be_created(self):111# TODO: fix this patch when we have a better way to stub out responses112with mock.patch('botocore.endpoint.Endpoint._send') as \113http_session_send_patch:114# Mock for HeadBucket request115head_bucket_response = mock.Mock()116head_bucket_response.status_code = 404117head_bucket_response.content = b''118head_bucket_response.headers = {}119120# Mock for CreateBucket request121create_bucket_response = mock.Mock()122create_bucket_response.status_code = 200123create_bucket_response.content = b''124create_bucket_response.headers = {}125126http_session_send_patch.side_effect = [127head_bucket_response, create_bucket_response128]129130s3_client = self.driver.session.create_client('s3')131bucket_helper = S3BucketHelper(s3_client)132bucket_helper.prepare_bucket('mybucket')133send_call_list = http_session_send_patch.call_args_list134self.assertEqual(send_call_list[0][0][0].method, 'HEAD')135# Since the HeadObject fails with 404, the CreateBucket which is136# is a PUT request should be made.137self.assertEqual(send_call_list[1][0][0].method, 'PUT')138139140