Skip to content

Commit

Permalink
Merge pull request #3161 from peterbe/bug-1240107-move-healthcheck-url
Browse files Browse the repository at this point in the history
fixes bug 1240107 - move /healthcheck URL
  • Loading branch information
adngdb committed Jan 20, 2016
2 parents 523cf38 + e504299 commit d4cb61b
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 69 deletions.
42 changes: 0 additions & 42 deletions webapp-django/crashstats/crashstats/tests/test_views.py
Expand Up @@ -571,48 +571,6 @@ def test_contribute_json(self):
ok_(json.loads(''.join(response.streaming_content)))
eq_(response['Content-Type'], 'application/json')

def test_healthcheck_elb(self):
response = self.client.get('/healthcheck', {'elb': 'true'})
eq_(response.status_code, 200)
eq_(json.loads(response.content)['ok'], True)

# This time, ignoring the results, make sure that running
# this does not cause an DB queries.
self.assertNumQueries(
0,
self.client.get,
'/healthcheck',
{'elb': 'true'}
)

@mock.patch('crashstats.crashstats.views.elasticsearch')
def test_healthcheck(self, mocked_elasticsearch):

es_instance = mock.MagicMock()

def fake_es_instance(**config):
eq_(
config['hosts'],
settings.SOCORRO_IMPLEMENTATIONS_CONFIG
['elasticsearch']['elasticsearch_urls']
)
return es_instance

mocked_elasticsearch.Elasticsearch.side_effect = fake_es_instance
response = self.client.get('/healthcheck')
eq_(response.status_code, 200)
eq_(json.loads(response.content)['ok'], True)

es_instance.info.assert_called_with()

self.assertNumQueries(
1,
self.client.get,
'/healthcheck',
)

eq_(es_instance.info.call_count, 2)

@mock.patch('requests.get')
def test_handler500(self, rget):
root_urlconf = __import__(
Expand Down
3 changes: 0 additions & 3 deletions webapp-django/crashstats/crashstats/urls.py
Expand Up @@ -26,9 +26,6 @@
url('^robots\.txt$',
views.robots_txt,
name='robots_txt'),
url('^healthcheck$',
views.healthcheck,
name='healthcheck'),
url('^home' + products + '$',
views.home,
name='home'),
Expand Down
24 changes: 0 additions & 24 deletions webapp-django/crashstats/crashstats/views.py
@@ -1,7 +1,6 @@
import copy
import json
import datetime
import elasticsearch
import logging
import math
import urllib
Expand All @@ -18,7 +17,6 @@
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import permission_required
from django.core.cache import cache
from django.contrib.auth.models import Permission
from django.utils.http import urlquote
from django.contrib import messages

Expand Down Expand Up @@ -102,28 +100,6 @@ def robots_txt(request):
)


def healthcheck(request):
if not request.GET.get('elb') in ('1', 'true'):
# Perform some really basic DB queries.
# There will always be permissions created
assert Permission.objects.all().count() > 0

# We should also be able to set and get a cache value
cache_key = '__healthcheck__'
cache.set(cache_key, 1, 10)
assert cache.get(cache_key)
cache.delete(cache_key)

# Do a really basic Elasticsearch query
es_settings = settings.SOCORRO_IMPLEMENTATIONS_CONFIG['elasticsearch']
es = elasticsearch.Elasticsearch(
hosts=es_settings['elasticsearch_urls']
)
es.info() # will raise an error if there's a problem with the cluster

return http.JsonResponse({'ok': True})


def has_builds(product, versions):
contains_builds = False
prod_versions = []
Expand Down
Expand Up @@ -10,6 +10,8 @@
<ul>
<li><a href="{{ url('monitoring:crash_analysis_health') }}">Crash Analysis health</a></li>
<li><a href="{{ url('monitoring:crontabber_status') }}">Crontabber Status</a></li>
<li><a href="{{ url('monitoring:healthcheck') }}">Healthcheck</a></li>
<li><a href="{{ url('monitoring:healthcheck') }}?elb=true">Healthcheck (for ELB)</a></li>
</ul>
</body>

Expand Down
47 changes: 47 additions & 0 deletions webapp-django/crashstats/monitoring/tests/test_views.py
Expand Up @@ -238,3 +238,50 @@ def mocked_get(url, **options):
eq_(response.status_code, 200)
data = json.loads(response.content)
eq_(data['status'], 'Stale')


class TestHealthcheckViews(BaseTestViews):

def test_healthcheck_elb(self):
url = reverse('monitoring:healthcheck')
response = self.client.get(url, {'elb': 'true'})
eq_(response.status_code, 200)
eq_(json.loads(response.content)['ok'], True)

# This time, ignoring the results, make sure that running
# this does not cause an DB queries.
self.assertNumQueries(
0,
self.client.get,
url,
{'elb': 'true'}
)

@mock.patch('crashstats.monitoring.views.elasticsearch')
def test_healthcheck(self, mocked_elasticsearch):

es_instance = mock.MagicMock()

def fake_es_instance(**config):
eq_(
config['hosts'],
settings.SOCORRO_IMPLEMENTATIONS_CONFIG
['elasticsearch']['elasticsearch_urls']
)
return es_instance

mocked_elasticsearch.Elasticsearch.side_effect = fake_es_instance
url = reverse('monitoring:healthcheck')
response = self.client.get(url)
eq_(response.status_code, 200)
eq_(json.loads(response.content)['ok'], True)

es_instance.info.assert_called_with()

self.assertNumQueries(
1,
self.client.get,
url,
)

eq_(es_instance.info.call_count, 2)
3 changes: 3 additions & 0 deletions webapp-django/crashstats/monitoring/urls.py
Expand Up @@ -13,4 +13,7 @@
url(r'^crontabber/$',
views.crontabber_status,
name='crontabber_status'),
url(r'^healthcheck/$',
views.healthcheck,
name='healthcheck'),
)
26 changes: 26 additions & 0 deletions webapp-django/crashstats/monitoring/views.py
@@ -1,12 +1,15 @@
import datetime
import urlparse

import elasticsearch
import isodate
import requests

from django.shortcuts import render
from django.conf import settings
from django.utils import timezone
from django.contrib.auth.models import Permission
from django.core.cache import cache

from crashstats.crashstats import utils
from crashstats.crashstats.models import CrontabberState
Expand Down Expand Up @@ -140,3 +143,26 @@ def crontabber_status(request):
context['broken'] = broken
context['blocked'] = blocked
return context


@utils.json_view
def healthcheck(request):
if not request.GET.get('elb') in ('1', 'true'):
# Perform some really basic DB queries.
# There will always be permissions created
assert Permission.objects.all().count() > 0

# We should also be able to set and get a cache value
cache_key = '__healthcheck__'
cache.set(cache_key, 1, 10)
assert cache.get(cache_key)
cache.delete(cache_key)

# Do a really basic Elasticsearch query
es_settings = settings.SOCORRO_IMPLEMENTATIONS_CONFIG['elasticsearch']
es = elasticsearch.Elasticsearch(
hosts=es_settings['elasticsearch_urls']
)
es.info() # will raise an error if there's a problem with the cluster

return {'ok': True}

0 comments on commit d4cb61b

Please sign in to comment.