Skip to content

Commit

Permalink
Let awslogs raise boto errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorge Bastida committed Oct 10, 2015
1 parent 3603896 commit d6df728
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 71 deletions.
24 changes: 0 additions & 24 deletions awslogs/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ class BaseAWSLogsException(Exception):
def hint(self):
return "Unknown Error."


class ConnectionError(BaseAWSLogsException):

code = 2

def hint(self):
return self.args[0]


class UnknownDateError(BaseAWSLogsException):

code = 3
Expand All @@ -22,21 +13,6 @@ def hint(self):
return "awslogs doesn't understand '{0}' as a date.".format(self.args[0])


class NoAuthHandlerFoundError(BaseAWSLogsException):

code = 5

def hint(self):
message = [
self.args[0],
"Check that you have provided valid credentials in one of the following ways:",
"* AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables.",
"* ~/.aws/credentials",
"* Instance profile credentials"
]
return '\n'.join(message)


class TooManyStreamsFilteredError(BaseAWSLogsException):

code = 6
Expand Down
49 changes: 2 additions & 47 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from botocore.compat import total_seconds
from botocore.client import ClientError
from botocore.auth import NoCredentialsError
from botocore.retryhandler import EndpointConnectionError

from termcolor import colored

Expand All @@ -19,7 +18,7 @@
from unittest.mock import patch, Mock, call

from awslogs import AWSLogs
from awslogs.exceptions import UnknownDateError, ConnectionError
from awslogs.exceptions import UnknownDateError
from awslogs.bin import main


Expand Down Expand Up @@ -226,14 +225,13 @@ def test_main_get(self, mock_stdout, botoclient):
def paginator(value):
mock = Mock()
mock.paginate.return_value = {
'filter_log_events': logs,
'describe_log_groups': groups,
'describe_log_streams': streams
}.get(value)
return mock

client.get_paginator.side_effect = paginator

client.filter_log_events.side_effect = logs
main("awslogs get AAA DDD --no-color".split())

self.assertEqual(
Expand Down Expand Up @@ -320,46 +318,3 @@ def test_unknown_error(self, mock_stderr, mock_awslogs):
self.assertEqual(code, 1)
self.assertTrue("You've found a bug!" in output)
self.assertTrue("Exception: Error!" in output)

@patch('boto3.client')
@patch('sys.stderr', new_callable=StringIO)
def test_connection_error(self, mock_stderr, botoclient):
client = Mock()
botoclient.return_value = client

exc = EndpointConnectionError(endpoint_url="url")
client.get_paginator.side_effect = exc

code = main("awslogs groups --aws-region=eu-west-1".split())
self.assertEqual(code, 2)
self.assertEqual(mock_stderr.getvalue(), colored("Could not connect to the endpoint URL: \"url\"\n", "red"))

@patch('boto3.client')
@patch('sys.stderr', new_callable=StringIO)
def test_access_denied_error(self, mock_stderr, botoclient):
client = Mock()
botoclient.return_value = client

exc = ClientError(error_response={'Error': {'Code': 'AccessDeniedException', 'Message': 'User XXX...'}}, operation_name="operation")
client.get_paginator.side_effect = exc

code = main("awslogs groups --aws-region=eu-west-1".split())
self.assertEqual(code, 4)
self.assertEqual(mock_stderr.getvalue(), colored("User XXX...\n", "red"))

@patch('boto3.client')
@patch('sys.stderr', new_callable=StringIO)
def test_no_credentials_error(self, mock_stderr, botoclient):
client = Mock()
botoclient.return_value = client

exc = NoCredentialsError()
client.get_paginator.side_effect = exc

code = main("awslogs groups --aws-region=eu-west-1".split())
self.assertEqual(code, 5)
self.assertEqual(mock_stderr.getvalue(), colored("""Unable to locate credentials
Check that you have provided valid credentials in one of the following ways:
* AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables.
* ~/.aws/credentials
* Instance profile credentials\n""", "red"))

0 comments on commit d6df728

Please sign in to comment.