Skip to content

Commit

Permalink
Add some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgtaylor committed Feb 23, 2015
1 parent 8baecb2 commit b58c019
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
3 changes: 2 additions & 1 deletion awscli/customizations/cloudtrail.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,13 @@ def _get_policy(self, key_name):
data = self.s3.GetObject(
bucket='awscloudtrail-policy-' + self.region_name,
key=key_name)
policy = data['Body'].read().decode('utf-8')
except Exception:
LOG.error('Unable to get regional policy template for'
' region %s: %s', self.region_name, key_name)
raise

return data['Body'].read().decode('utf-8')
return policy

def setup_new_bucket(self, bucket, prefix, policy_url=None):
"""
Expand Down
34 changes: 31 additions & 3 deletions tests/unit/customizations/test_cloudtrail.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import os
from awscli.compat import six

from awscli.customizations import cloudtrail
from awscli.customizations.cloudtrail import CloudTrailSubscribe
from awscli.customizations.service import Service
from mock import ANY, Mock, patch
Expand Down Expand Up @@ -157,14 +158,41 @@ def test_s3_create_set_policy_fail(self):

s3.PutBucketPolicy = orig

def test_get_policy_fail(self):
orig = self.subscribe.s3.GetObject
def test_s3_get_policy_fail(self):
self.subscribe.s3.GetObject = Mock(side_effect=Exception('Error!'))

with self.assertRaises(Exception):
self.subscribe.setup_new_bucket('test', 'logs')

self.subscribe.s3.GetObject = orig
def test_s3_get_policy_logs_messages(self):
cloudtrail.LOG = Mock()
self.subscribe.s3.GetObject = Mock(side_effect=Exception('Error!'))

try:
self.subscribe.setup_new_bucket('test', 'logs')
except:
pass

self.assertIn(
'Unable to get regional policy template for region',
cloudtrail.LOG.error.call_args[0][0])
self.assertEqual('us-east-1', cloudtrail.LOG.error.call_args[0][1])

def test_get_policy_read_timeout(self):
response = {
'Body': Mock()
}
response['Body'].read.side_effect = Exception('Error!')
self.subscribe.s3.GetObject.return_value = response

with self.assertRaises(Exception):
self.subscribe.setup_new_bucket('test', 'logs')

def test_sns_get_policy_fail(self):
self.subscribe.s3.GetObject = Mock(side_effect=Exception('Error!'))

with self.assertRaises(Exception):
self.subscribe.setup_new_bucket('test', 'logs')

def test_sns_create(self):
s3 = self.subscribe.s3
Expand Down

0 comments on commit b58c019

Please sign in to comment.