Path: blob/develop/tests/unit/customizations/configservice/test_subscribe.py
1569 views
# Copyright 2014 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.12import botocore.session13from botocore.exceptions import ClientError1415from awscli.testutils import mock, unittest16from awscli.compat import StringIO17from awscli.customizations.configservice.subscribe import SubscribeCommand, \18S3BucketHelper, SNSTopicHelper192021class TestS3BucketHelper(unittest.TestCase):22def setUp(self):23self.session = botocore.session.get_session()24self.s3_client = mock.Mock(self.session.create_client('s3'))25self.helper = S3BucketHelper(self.s3_client)26self.error_response = {27'Error': {28'Code': '404',29'Message': 'Not Found'30}31}32self.bucket_no_exists_error = ClientError(33self.error_response,34'HeadBucket'35)3637def test_correct_prefix_returned(self):38name = 'MyBucket/MyPrefix'39bucket, prefix = self.helper.prepare_bucket(name)40# Ensure the returned bucket and key are as expected41self.assertEqual(bucket, 'MyBucket')42self.assertEqual(prefix, 'MyPrefix')4344def test_bucket_exists(self):45name = 'MyBucket'46bucket, prefix = self.helper.prepare_bucket(name)47# A new bucket should not have been created because no error was thrown48self.assertFalse(self.s3_client.create_bucket.called)49# Ensure the returned bucket and key are as expected50self.assertEqual(bucket, name)51self.assertEqual(prefix, '')5253def test_bucket_no_exist(self):54name = 'MyBucket/MyPrefix'55self.s3_client.head_bucket.side_effect = self.bucket_no_exists_error56self.s3_client.meta.region_name = 'us-east-1'57bucket, prefix = self.helper.prepare_bucket(name)58# Ensure that the create bucket was called with the proper args.59self.s3_client.create_bucket.assert_called_with(60Bucket='MyBucket'61)62# Ensure the returned bucket and key are as expected63self.assertEqual(bucket, 'MyBucket')64self.assertEqual(prefix, 'MyPrefix')6566def test_bucket_no_exist_with_location_constraint(self):67name = 'MyBucket/MyPrefix'68self.s3_client.head_bucket.side_effect = self.bucket_no_exists_error69self.s3_client.meta.region_name = 'us-west-2'70bucket, prefix = self.helper.prepare_bucket(name)71# Ensure that the create bucket was called with the proper args.72self.s3_client.create_bucket.assert_called_with(73Bucket='MyBucket',74CreateBucketConfiguration={'LocationConstraint': 'us-west-2'}75)76# Ensure the returned bucket and key are as expected77self.assertEqual(bucket, 'MyBucket')78self.assertEqual(prefix, 'MyPrefix')7980def test_bucket_client_exception_non_404(self):81name = 'MyBucket/MyPrefix'82self.error_response['Error']['Code'] = '403'83self.error_response['Error']['Message'] = 'Forbidden'84forbidden_error = ClientError(self.error_response, 'HeadBucket')85self.s3_client.head_bucket.side_effect = forbidden_error86self.s3_client._endpoint.region_name = 'us-east-1'87bucket, prefix = self.helper.prepare_bucket(name)88# A new bucket should not have been created because a 404 error89# was not thrown90self.assertFalse(self.s3_client.create_bucket.called)91# Ensure the returned bucket and key are as expected92self.assertEqual(bucket, 'MyBucket')93self.assertEqual(prefix, 'MyPrefix')9495def test_output_use_existing_bucket(self):96name = 'MyBucket/MyPrefix'97with mock.patch('sys.stdout', StringIO()) as mock_stdout:98self.helper.prepare_bucket(name)99self.assertIn(100'Using existing S3 bucket: MyBucket',101mock_stdout.getvalue())102103def test_output_create_bucket(self):104name = 'MyBucket/MyPrefix'105self.s3_client.head_bucket.side_effect = self.bucket_no_exists_error106self.s3_client._endpoint.region_name = 'us-east-1'107with mock.patch('sys.stdout', StringIO()) as mock_stdout:108self.helper.prepare_bucket(name)109self.assertIn(110'Using new S3 bucket: MyBucket',111mock_stdout.getvalue())112113114class TestSNSTopicHelper(unittest.TestCase):115def setUp(self):116self.session = botocore.session.get_session()117self.sns_client = mock.Mock(self.session.create_client(118'sns', 'us-east-1'))119self.helper = SNSTopicHelper(self.sns_client)120121def test_sns_topic_by_name(self):122name = 'mysnstopic'123self.sns_client.create_topic.return_value = {'TopicArn': 'myARN'}124sns_arn = self.helper.prepare_topic(name)125# Ensure that the topic was create and returned the expected arn126self.assertTrue(self.sns_client.create_topic.called)127self.assertEqual(sns_arn, 'myARN')128129def test_sns_topic_by_arn(self):130name = 'arn:aws:sns:us-east-1:934212987125:config'131sns_arn = self.helper.prepare_topic(name)132# Ensure that the topic was not created and returned the expected arn133self.assertFalse(self.sns_client.create_topic.called)134self.assertEqual(sns_arn, name)135136def test_output_existing_topic(self):137name = 'mysnstopic'138self.sns_client.create_topic.return_value = {'TopicArn': 'myARN'}139with mock.patch('sys.stdout', StringIO()) as mock_stdout:140self.helper.prepare_topic(name)141self.assertIn(142'Using new SNS topic: myARN',143mock_stdout.getvalue())144145def test_output_new_topic(self):146name = 'arn:aws:sns:us-east-1:934212987125:config'147with mock.patch('sys.stdout', StringIO()) as mock_stdout:148self.helper.prepare_topic(name)149self.assertIn(150'Using existing SNS topic: %s' % name,151mock_stdout.getvalue())152153154class TestSubscribeCommand(unittest.TestCase):155def setUp(self):156self.session = botocore.session.get_session()157158# Set up the client mocks.159self.s3_client = mock.Mock(self.session.create_client('s3'))160self.sns_client = mock.Mock(self.session.create_client(161'sns', 'us-east-1'))162self.config_client = mock.Mock(self.session.create_client(163'config', 'us-east-1'))164self.config_client.describe_configuration_recorders.return_value = \165{'ConfigurationRecorders': []}166self.config_client.describe_delivery_channels.return_value = \167{'DeliveryChannels': []}168169self.session = mock.Mock(self.session)170self.session.create_client.side_effect = [171self.s3_client,172self.sns_client,173self.config_client174]175176self.parsed_args = mock.Mock()177self.parsed_args.s3_bucket = 'MyBucket/MyPrefix'178self.parsed_args.sns_topic = \179'arn:aws:sns:us-east-1:934212987125:config'180self.parsed_args.iam_role = 'arn:aws:iam::1234556789:role/config'181182self.parsed_globals = mock.Mock()183184self.cmd = SubscribeCommand(self.session)185186def test_setup_clients(self):187self.parsed_globals.verify_ssl = True188self.parsed_globals.region = 'us-east-1'189self.parsed_globals.endpoint_url = 'http://myendpoint.com'190191self.cmd._run_main(self.parsed_args, self.parsed_globals)192193# Check to see that the clients were created correctly194self.session.create_client.assert_any_call(195's3',196verify=self.parsed_globals.verify_ssl,197region_name=self.parsed_globals.region,198)199self.session.create_client.assert_any_call(200'sns',201verify=self.parsed_globals.verify_ssl,202region_name=self.parsed_globals.region,203)204self.session.create_client.assert_any_call(205'config',206verify=self.parsed_globals.verify_ssl,207region_name=self.parsed_globals.region,208endpoint_url=self.parsed_globals.endpoint_url209)210211def test_subscribe(self):212self.cmd._run_main(self.parsed_args, self.parsed_globals)213214# Check the call made to put configuration recorder.215self.config_client.put_configuration_recorder.assert_called_with(216ConfigurationRecorder={217'name': 'default',218'roleARN': self.parsed_args.iam_role219}220)221222# Check the call made to put delivery channel.223self.config_client.put_delivery_channel.assert_called_with(224DeliveryChannel={225'name': 'default',226's3BucketName': 'MyBucket',227'snsTopicARN': self.parsed_args.sns_topic,228's3KeyPrefix': 'MyPrefix'229}230)231232# Check the call made to start configuration recorder.233self.config_client.start_configuration_recorder.assert_called_with(234ConfigurationRecorderName='default'235)236237# Check that the describe delivery channel and configuration recorder238# methods were called.239self.assertTrue(240self.config_client.describe_configuration_recorders.called241)242self.assertTrue(self.config_client.describe_delivery_channels.called)243244245