Skip to content

Commit

Permalink
Fixes #421 - switch utils._get_latest_version() from botocore.vendore…
Browse files Browse the repository at this point in the history
…d.requests to urllib3
  • Loading branch information
jantman committed Aug 28, 2019
1 parent 7511449 commit f948232
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Unreleased Changes
* `Issue #421 <https://github.com/jantman/awslimitchecker/issues/421>`__

* Stop referencing deprecated ``botocore.vendored.requests.exceptions.ConnectTimeout`` in favor of new, and higher-level, ``botocore.exceptions.ConnectionError``
* In :py:meth:`awslimitchecker.utils._get_latest_version`, replace use of ``botocore.vendored.requests`` with ``urllib3``.

7.0.0 (2019-08-13)
------------------
Expand Down
45 changes: 25 additions & 20 deletions awslimitchecker/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,51 +409,56 @@ def test_set_dict_value_by_path_empty(self):
class TestGetCurrentVersion(object):

def test_exception(self):
mock_http = Mock()
with patch('%s._VERSION_TUP' % pbm, (0, 2, 3)):
with patch('%s.requests' % pbm, autospec=True) as mock_req:
with patch('%s.urllib3.PoolManager' % pbm, autospec=True) as m_pm:
with patch('%s.logger' % pbm, autospec=True) as mock_logger:
mock_req.get.side_effect = RuntimeError()
m_pm.return_value = mock_http
mock_http.request.side_effect = RuntimeError()
res = _get_latest_version()
assert res is None
assert mock_logger.mock_calls == [
call.debug('Error getting latest version from PyPI', exc_info=True)
]

def test_older(self):
mock_resp = Mock()
mock_resp.json.return_value = {
'info': {'version': '1.0.1'}
}
mock_http = Mock()
mock_resp = Mock(
status=200, data='{"info": {"version": "1.0.1"}}'
)
with patch('%s._VERSION_TUP' % pbm, (0, 2, 3)):
with patch('%s.requests' % pbm, autospec=True) as mock_req:
with patch('%s.urllib3.PoolManager' % pbm, autospec=True) as m_pm:
with patch('%s.logger' % pbm, autospec=True) as mock_logger:
mock_req.get.return_value = mock_resp
m_pm.return_value = mock_http
mock_http.request.return_value = mock_resp
res = _get_latest_version()
assert res == '1.0.1'
assert mock_logger.mock_calls == []

def test_equal(self):
mock_resp = Mock()
mock_resp.json.return_value = {
'info': {'version': '0.2.3'}
}
mock_http = Mock()
mock_resp = Mock(
status=200, data='{"info": {"version": "0.2.3"}}'
)
with patch('%s._VERSION_TUP' % pbm, (0, 2, 3)):
with patch('%s.requests' % pbm, autospec=True) as mock_req:
with patch('%s.urllib3.PoolManager' % pbm, autospec=True) as m_pm:
with patch('%s.logger' % pbm, autospec=True) as mock_logger:
mock_req.get.return_value = mock_resp
m_pm.return_value = mock_http
mock_http.request.return_value = mock_resp
res = _get_latest_version()
assert res is None
assert mock_logger.mock_calls == []

def test_newer(self):
mock_resp = Mock()
mock_resp.json.return_value = {
'info': {'version': '0.1.2'}
}
mock_http = Mock()
mock_resp = Mock(
status=200, data='{"info": {"version": "0.1.2"}}'
)
with patch('%s._VERSION_TUP' % pbm, (0, 2, 3)):
with patch('%s.requests' % pbm, autospec=True) as mock_req:
with patch('%s.urllib3.PoolManager' % pbm, autospec=True) as m_pm:
with patch('%s.logger' % pbm, autospec=True) as mock_logger:
mock_req.get.return_value = mock_resp
m_pm.return_value = mock_http
mock_http.request.return_value = mock_resp
res = _get_latest_version()
assert res is None
assert mock_logger.mock_calls == []
11 changes: 7 additions & 4 deletions awslimitchecker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
import argparse
import logging
from copy import deepcopy
import botocore.vendored.requests as requests
import json
import urllib3
from awslimitchecker.version import _VERSION_TUP, _VERSION

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -227,14 +228,16 @@ def _get_latest_version():
:rtype: `str` or `None`
"""
try:
r = requests.get(
'https://pypi.org/pypi/awslimitchecker/json',
http = urllib3.PoolManager()
r = http.request(
'GET', 'https://pypi.org/pypi/awslimitchecker/json',
timeout=4.0, headers={
'User-Agent': 'github.com/jantman/awslimitchecker '
'%s' % _VERSION
}
)
j = r.json()
assert r.status == 200, "PyPI responded HTTP %s" % r.status
j = json.loads(r.data)
latest = tuple([
int(i) for i in j['info']['version'].split('.')[0:3]
])
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
'termcolor>=1.1.0',
'python-dateutil>=2.4.2',
'versionfinder>=0.1.1',
'pytz'
'pytz',
'urllib3'
]

classifiers = [
Expand Down

0 comments on commit f948232

Please sign in to comment.