IPResolver: fix unreachable code (#3267)

* Fix unreachable code

_get_aws_ip_ranges handles missing file exceptions and returns None,
so the excepttion handling block in the UpdateIPRange thread is never reached.

* Test for missing range file in resolver thread
This commit is contained in:
Kenny Lee Sin Cheong 2018-10-12 10:24:28 -04:00 committed by GitHub
parent 0f1d415c49
commit fc67bbd0a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 5 deletions

View file

@ -211,10 +211,9 @@ class _UpdateIPRange(Thread):
def run(self):
while True:
try:
logger.debug('Updating aws ip range from "%s"', 'util/ipresolver/aws-ip-ranges.json')
aws_ip_range_json = _get_aws_ip_ranges()
except:
logger.debug('Updating aws ip range from "%s"', 'util/ipresolver/aws-ip-ranges.json')
aws_ip_range_json = _get_aws_ip_ranges()
if aws_ip_range_json is None:
logger.exception('Failed trying to update aws ip range')
time.sleep(_FAILED_UPDATE_RETRY_SECS)
continue

View file

@ -2,7 +2,7 @@ import pytest
from mock import patch
from util.ipresolver import IPResolver, ResolvedLocation, CACHE
from util.ipresolver import _UpdateIPRange, IPResolver, ResolvedLocation, CACHE, _FAILED_UPDATE_RETRY_SECS, _UPDATE_INTERVAL
from test.fixtures import *
@pytest.fixture()
@ -55,3 +55,13 @@ def test_resolved(aws_ip_range_data, test_ip_range_cache, test_aws_ip, app):
assert ipresolver.resolve_ip('10.0.0.2') == ResolvedLocation(provider='aws', region=u'GLOBAL', service=None, sync_token=123456789)
assert ipresolver.resolve_ip('1.2.3.4') == ResolvedLocation(provider='internet', region=u'NA', service=u'US', sync_token=123456789)
assert ipresolver.resolve_ip('127.0.0.1') == ResolvedLocation(provider='internet', region=None, service=None, sync_token=123456789)
def test_thread_missing_file():
class LoopInterruptionForTest(Exception):
pass
with patch('util.ipresolver._get_aws_ip_ranges', return_value=None):
with patch('time.sleep', side_effect=LoopInterruptionForTest) as sleep:
update_ip_thread = _UpdateIPRange(_UPDATE_INTERVAL)
update_ip_thread.start()
sleep.assert_called_with(_FAILED_UPDATE_RETRY_SECS)