Skip to content

Commit

Permalink
Merge branch 'develop' into feature/redshift
Browse files Browse the repository at this point in the history
  • Loading branch information
jantman authored Feb 6, 2017
2 parents 58141e7 + b8aea0f commit c2f15fe
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 2 deletions.
18 changes: 16 additions & 2 deletions awslimitchecker/services/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
from collections import defaultdict
from copy import deepcopy

import botocore

from .base import _AwsService
from ..limit import AwsLimit

Expand Down Expand Up @@ -136,7 +138,12 @@ def _find_usage_spot_instances(self):
"may not me accurate in all cases. Please see the notes "
"at: <http://awslimitchecker.readthedocs.io/en/latest"
"/limits.html#ec2>")
res = self.conn.describe_spot_instance_requests()
try:
res = self.conn.describe_spot_instance_requests()
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'UnsupportedOperation':
return
raise
count = 0
for req in res['SpotInstanceRequests']:
if req['State'] in ['open', 'active']:
Expand All @@ -158,7 +165,12 @@ def _find_usage_spot_instances(self):
def _find_usage_spot_fleets(self):
"""calculate spot fleet request usage and update Limits"""
logger.debug('Getting spot fleet request usage')
res = self.conn.describe_spot_fleet_requests()
try:
res = self.conn.describe_spot_fleet_requests()
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'UnsupportedOperation':
return
raise
if 'NextToken' in res:
logger.error('Error: describe_spot_fleet_requests() response '
'includes pagination token, but pagination not '
Expand Down Expand Up @@ -304,6 +316,8 @@ def _update_limits_from_api(self):
elif aname == 'vpc-max-security-groups-per-interface':
lname = 'VPC security groups per elastic network interface'
if lname is not None:
if int(val) == 0:
continue
self.limits[lname]._set_api_limit(int(val))
logger.debug("Done setting limits from API")

Expand Down
28 changes: 28 additions & 0 deletions awslimitchecker/tests/services/result_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1709,6 +1709,34 @@ def test_find_usage_networking_eni_sg(self):
]
}

test_update_limits_from_api_unsupported = {
'ResponseMetadata': {
'HTTPStatusCode': 200,
'RequestId': '16b85906-ab0d-4134-b8bb-df3e6120c6c7'
},
'AccountAttributes': [
{
'AttributeName': 'supported-platforms',
'AttributeValues': [
{
'AttributeValue': 'EC2'
},
{
'AttributeValue': 'VPC'
}
]
},
{
'AttributeName': 'max-elastic-ips',
'AttributeValues': [
{
'AttributeValue': '0'
}
]
},
]
}

test_find_usage_spot_instances = {
'SpotInstanceRequests': [
{
Expand Down
83 changes: 83 additions & 0 deletions awslimitchecker/tests/services/test_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

import sys
from copy import deepcopy
import pytest
import botocore
from awslimitchecker.tests.services import result_fixtures
from awslimitchecker.services.ec2 import _Ec2Service
from awslimitchecker.limit import AwsLimit
Expand Down Expand Up @@ -554,6 +556,41 @@ def test_find_usage_spot_instances(self):
'limit (%s) current usage to: %d', lim, 2)
]

def test_find_usage_spot_instances_unsupported(self):
mock_client_conn = Mock()
err = botocore.exceptions.ClientError(
{'Error': {'Code': 'UnsupportedOperation'}},
'operation',
)
mock_client_conn.describe_spot_instance_requests.side_effect = err
cls = _Ec2Service(21, 43)
cls.conn = mock_client_conn
cls._find_usage_spot_instances()
lim = cls.limits['Max spot instance requests per region']
usage = lim.get_current_usage()
assert len(usage) == 0

def test_find_usage_spot_instances_unknown_code(self):
mock_client_conn = Mock()
err = botocore.exceptions.ClientError(
{'Error': {'Code': 'SomeCode'}},
'operation',
)
mock_client_conn.describe_spot_instance_requests.side_effect = err
cls = _Ec2Service(21, 43)
cls.conn = mock_client_conn
with pytest.raises(botocore.exceptions.ClientError):
cls._find_usage_spot_instances()

def test_find_usage_spot_instances_unknown_error(self):
mock_client_conn = Mock()
err = RuntimeError
mock_client_conn.describe_spot_instance_requests.side_effect = err
cls = _Ec2Service(21, 43)
cls.conn = mock_client_conn
with pytest.raises(RuntimeError):
cls._find_usage_spot_instances()

def test_find_usage_spot_fleets(self):
data = fixtures.test_find_usage_spot_fleets
mock_conn = Mock()
Expand Down Expand Up @@ -668,6 +705,40 @@ def test_find_usage_spot_fleets_paginated(self):
'for all spot fleets: %d', 2, 44)
]

def test_find_usage_spot_fleets_unsupported(self):
mock_client_conn = Mock()
err = botocore.exceptions.ClientError(
{'Error': {'Code': 'UnsupportedOperation'}},
'operation',
)
mock_client_conn.describe_spot_fleet_requests.side_effect = err
cls = _Ec2Service(21, 43)
cls.conn = mock_client_conn
cls._find_usage_spot_fleets()
total = cls.limits['Max active spot fleets per '
'region'].get_current_usage()
assert len(total) == 0

def test_find_usage_spot_fleets_unknown_code(self):
mock_client_conn = Mock()
err = botocore.exceptions.ClientError(
{'Error': {'Code': 'SomeCode'}},
'operation',
)
mock_client_conn.describe_spot_fleet_requests.side_effect = err
cls = _Ec2Service(21, 43)
cls.conn = mock_client_conn
with pytest.raises(botocore.exceptions.ClientError):
cls._find_usage_spot_fleets()

def test_find_usage_spot_fleets_unknown_error(self):
mock_client_conn = Mock()
mock_client_conn.describe_spot_fleet_requests.side_effect = RuntimeError
cls = _Ec2Service(21, 43)
cls.conn = mock_client_conn
with pytest.raises(RuntimeError):
cls._find_usage_spot_fleets()

def test_update_limits_from_api(self):
data = fixtures.test_update_limits_from_api
mock_conn = Mock()
Expand All @@ -692,3 +763,15 @@ def test_update_limits_from_api(self):
assert cls.limits['VPC Elastic IP addresses (EIPs)'].api_limit == 200
assert cls.limits['VPC security groups per elastic '
'network interface'].api_limit == 5

def test_update_limits_from_api_unsupported(self):
data = fixtures.test_update_limits_from_api_unsupported
mock_client_conn = Mock()
mock_client_conn.describe_account_attributes.return_value = data

cls = _Ec2Service(21, 43)
cls.conn = mock_client_conn
cls._update_limits_from_api()
lim = cls.limits['Elastic IP addresses (EIPs)']
usage = lim.get_current_usage()
assert len(usage) == 0

0 comments on commit c2f15fe

Please sign in to comment.