Skip to content

Commit 1496fb6

Browse files
benwilbermboersma
authored andcommitted
feat(controller): add simple health check view
1 parent dded21e commit 1496fb6

4 files changed

Lines changed: 39 additions & 0 deletions

File tree

rootfs/api/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def mock_none(*args, **kwargs):
6060
from .test_config import * # noqa
6161
from .test_container import * # noqa
6262
from .test_domain import * # noqa
63+
from .test_healthcheck import * # noqa
6364
from .test_hooks import * # noqa
6465
from .test_key import * # noqa
6566
from .test_limits import * # noqa
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from __future__ import unicode_literals
2+
3+
from django.test import TestCase
4+
5+
6+
class HealthCheckTest(TestCase):
7+
8+
def setUp(self):
9+
self.url = '/health-check'
10+
11+
def test_healthcheck(self):
12+
# GET and HEAD (no auth required)
13+
resp = self.client.get(self.url)
14+
self.assertEqual(resp.status_code, 200)
15+
self.assertEqual(resp.content, "OK")
16+
17+
resp = self.client.head(self.url)
18+
self.assertEqual(resp.status_code, 200)
19+
20+
def test_healthcheck_invalid(self):
21+
for method in ('put', 'post', 'patch', 'delete'):
22+
resp = getattr(self.client, method)(self.url)
23+
# method not allowed
24+
self.assertEqual(resp.status_code, 405)

rootfs/api/views.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
from django.conf import settings
55
from django.core.exceptions import ValidationError
66
from django.contrib.auth.models import User
7+
from django.http import HttpResponse
78
from django.shortcuts import get_object_or_404
89
from guardian.shortcuts import assign_perm, get_objects_for_user, \
910
get_users_with_perms, remove_perm
11+
from django.views.generic import View
1012
from rest_framework import mixins, renderers, status
1113
from rest_framework.exceptions import PermissionDenied
1214
from rest_framework.permissions import IsAuthenticated
@@ -19,6 +21,16 @@
1921
import requests
2022

2123

24+
class HealthCheckView(View):
25+
"""Simple health check view to determine if the server
26+
is responding to HTTP requests.
27+
"""
28+
29+
def get(self, request):
30+
return HttpResponse("OK")
31+
head = get
32+
33+
2234
class UserRegistrationViewSet(GenericViewSet,
2335
mixins.CreateModelMixin):
2436
"""ViewSet to handle registering new users. The logic is in the serializer."""

rootfs/deis/urls.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
from django.conf import settings
1111
from django.conf.urls import patterns, include, url
1212
from django.contrib import admin
13+
from api.views import HealthCheckView
1314

1415

1516
admin.autodiscover()
1617

1718

1819
urlpatterns = patterns(
1920
'',
21+
url(r'^health-check$', HealthCheckView.as_view()),
2022
url(r'^v2/', include('api.urls')),
2123
)
2224

0 commit comments

Comments
 (0)