1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Add unittests

This commit is contained in:
Sander Steffann
2020-02-27 21:54:43 +01:00
parent 28473fa3e0
commit fcf3b14712
2 changed files with 175 additions and 4 deletions

View File

@ -1,7 +1,7 @@
import logging
import requests
from cacheops import cache
from cacheops.simple import cache, CacheMiss
from django.conf import settings
from django_rq import job
from packaging import version
@ -17,24 +17,35 @@ def get_releases(pre_releases=False):
'Accept': 'application/vnd.github.v3+json',
}
# Check whether this URL has failed and shouldn't be retried yet
try:
failed_url = cache.get('netbox_releases_no_retry')
if url == failed_url:
return []
except CacheMiss:
pass
releases = []
# noinspection PyBroadException
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
for release in response.json():
if 'tag_name' not in release:
continue
if not pre_releases and (release.get('is_devrelease') or release.get('is_prerelease')):
if not pre_releases and (release.get('devrelease') or release.get('prerelease')):
continue
releases.append((version.parse(release['tag_name']), release.get('html_url')))
except Exception:
# Don't retry this URL for 15 minutes
cache.set('netbox_releases_no_retry', url, 900)
logger.exception("Error while fetching {}".format(url))
return []
logger.debug("Found NetBox releases {}".format([str(release) for release, url in releases]))
cache.set('netbox_releases', releases, settings.UPDATE_CACHE_TIMEOUT)
return releases