0001-add-caching-to-health-API-26836.patch
hobo/environment/models.py | ||
---|---|---|
1 | 1 |
import datetime |
2 | 2 |
import json |
3 |
import random |
|
3 | 4 |
import requests |
4 | 5 |
import socket |
5 | 6 | |
6 | 7 |
from django.conf import settings |
8 |
from django.core.cache import cache |
|
7 | 9 |
from django.db import models |
8 | 10 |
from django.utils.crypto import get_random_string |
9 | 11 |
from django.utils.encoding import force_text |
... | ... | |
189 | 191 |
r = requests.get(self.get_admin_zones()[0].href, verify=False) |
190 | 192 |
return bool(r.status_code is 200) |
191 | 193 | |
194 |
def get_health_dict(self): |
|
195 |
properties = [ |
|
196 |
('is_resolvable', 120), |
|
197 |
('has_valid_certificate', 3600), |
|
198 |
('is_running', 60), |
|
199 |
('is_operational', 60), |
|
200 |
] |
|
201 |
result = {} |
|
202 |
for name, cache_duration in properties: |
|
203 |
cache_key = '%s_%s' % (self.slug, name) |
|
204 |
value = cache.get(cache_key) |
|
205 |
if value is None: |
|
206 |
value = getattr(self, name)() |
|
207 |
cache.set(cache_key, value, cache_duration * (0.5 + random.random())) |
|
208 |
result[name] = value |
|
209 |
return result |
|
210 | ||
192 | 211 | |
193 | 212 |
class Authentic(ServiceBase): |
194 | 213 |
use_as_idp_for_self = models.BooleanField( |
hobo/environment/urls.py | ||
---|---|---|
19 | 19 |
url(r'^new-variable-(?P<service>\w+)/(?P<slug>[\w-]+)$', |
20 | 20 |
VariableCreateView.as_view(), name='new-variable-service',), |
21 | 21 |
url(r'^debug.json$', debug_json, name='debug-json'), |
22 |
url(r'^health.json$', health_json, name='health-json'), |
|
22 | 23 |
] |
hobo/environment/views.py | ||
---|---|---|
4 | 4 |
from django.conf import settings |
5 | 5 |
from django.contrib.contenttypes.models import ContentType |
6 | 6 |
from django.core.urlresolvers import reverse_lazy |
7 |
from django.http import HttpResponse, HttpResponseRedirect, Http404 |
|
7 |
from django.http import HttpResponse, HttpResponseRedirect, Http404, JsonResponse
|
|
8 | 8 |
from django.shortcuts import get_object_or_404 |
9 | 9 |
from django.views.generic.base import TemplateView |
10 | 10 |
from django.views.generic.edit import CreateView, UpdateView, DeleteView |
... | ... | |
200 | 200 |
response = HttpResponse(content_type='application/json') |
201 | 201 |
json.dump((utils.get_installed_services_dict(),), response, indent=2) |
202 | 202 |
return response |
203 | ||
204 | ||
205 |
def health_json(request): |
|
206 |
data = {x.slug: x.get_health_dict() for x in utils.get_installed_services() if not x.secondary} |
|
207 |
return JsonResponse({'data': data}) |
hobo/rest/serializers.py | ||
---|---|---|
1 |
# hobo - portal to configure and deploy applications |
|
2 |
# Copyright (C) 2015-2018 Entr'ouvert |
|
3 |
# |
|
4 |
# This program is free software; you can redistribute it and/or modify |
|
5 |
# it under the terms of the GNU General Public License as published by |
|
6 |
# the Free Software Foundation; either version 2 of the License, or |
|
7 |
# (at your option) any later version. |
|
8 |
# |
|
9 |
# This program is distributed in the hope that it will be useful, |
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
# GNU General Public License for more details. |
|
13 |
# |
|
14 |
# You should have received a copy of the GNU General Public License |
|
15 |
# along with this program; if not, see <http://www.gnu.org/licenses/>. |
|
16 | ||
17 |
from rest_framework import serializers |
|
18 | ||
19 | ||
20 |
class HealthBaseSerializer(serializers.Serializer): |
|
21 |
title = serializers.CharField() |
|
22 |
slug = serializers.SlugField() |
|
23 |
base_url = serializers.CharField() |
|
24 |
is_resolvable = serializers.BooleanField() |
|
25 |
has_valid_certificate = serializers.BooleanField() |
|
26 |
is_running = serializers.BooleanField() |
|
27 |
is_operational = serializers.BooleanField() |
hobo/rest/urls.py | ||
---|---|---|
1 |
# hobo - portal to configure and deploy applications |
|
2 |
# Copyright (C) 2015-2018 Entr'ouvert |
|
3 |
# |
|
4 |
# This program is free software; you can redistribute it and/or modify |
|
5 |
# it under the terms of the GNU General Public License as published by |
|
6 |
# the Free Software Foundation; either version 2 of the License, or |
|
7 |
# (at your option) any later version. |
|
8 |
# |
|
9 |
# This program is distributed in the hope that it will be useful, |
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
# GNU General Public License for more details. |
|
13 |
# |
|
14 |
# You should have received a copy of the GNU General Public License |
|
15 |
# along with this program; if not, see <http://www.gnu.org/licenses/>. |
|
16 | ||
17 |
from django.conf.urls import url |
|
18 |
from .views import HealthList |
|
19 | ||
20 |
urlpatterns = [ |
|
21 |
url(r'^health/$', HealthList.as_view(), name='api-health-list'), |
|
22 |
] |
hobo/rest/views.py | ||
---|---|---|
1 |
# hobo - portal to configure and deploy applications |
|
2 |
# Copyright (C) 2015-2018 Entr'ouvert |
|
3 |
# |
|
4 |
# This program is free software; you can redistribute it and/or modify |
|
5 |
# it under the terms of the GNU General Public License as published by |
|
6 |
# the Free Software Foundation; either version 2 of the License, or |
|
7 |
# (at your option) any later version. |
|
8 |
# |
|
9 |
# This program is distributed in the hope that it will be useful, |
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
# GNU General Public License for more details. |
|
13 |
# |
|
14 |
# You should have received a copy of the GNU General Public License |
|
15 |
# along with this program; if not, see <http://www.gnu.org/licenses/>. |
|
16 | ||
17 |
from rest_framework import generics |
|
18 |
from rest_framework import permissions |
|
19 |
from rest_framework.response import Response |
|
20 | ||
21 |
from hobo.environment.utils import get_installed_services |
|
22 |
from hobo.rest.serializers import HealthBaseSerializer |
|
23 | ||
24 | ||
25 |
class HealthList(generics.ListAPIView): |
|
26 |
serializer_class = HealthBaseSerializer |
|
27 |
permission_classes = (permissions.IsAuthenticatedOrReadOnly,) |
|
28 | ||
29 |
def get_queryset(self): |
|
30 |
return [x for x in get_installed_services() if not x.secondary] |
|
31 | ||
32 |
def list(self, request, *args, **kwargs): |
|
33 |
""" |
|
34 |
Custom dictionary object |
|
35 |
""" |
|
36 |
self.object_list = self.filter_queryset(self.get_queryset()) |
|
37 | ||
38 |
# Switch between paginated or standard style responses |
|
39 |
page = self.paginate_queryset(self.object_list) |
|
40 |
if page is not None: |
|
41 |
serializer = self.get_pagination_serializer(page) |
|
42 |
else: |
|
43 |
serializer = self.get_serializer(self.object_list, many=True) |
|
44 | ||
45 |
response = {d['slug']: d for d in serializer.data} |
|
46 |
return Response({'data': response}) |
hobo/urls.py | ||
---|---|---|
10 | 10 |
from .profile.urls import urlpatterns as profile_urls |
11 | 11 |
from .theme.urls import urlpatterns as theme_urls |
12 | 12 |
from .emails.urls import urlpatterns as emails_urls |
13 |
from .rest.urls import urlpatterns as rest_urls |
|
14 | 13 | |
15 | 14 |
urlpatterns = [ |
16 | 15 |
url(r'^$', home, name='home'), |
... | ... | |
21 | 20 |
url(r'^theme/', decorated_includes(admin_required, |
22 | 21 |
include(theme_urls))), |
23 | 22 |
url(r'^emails/', decorated_includes(admin_required, include(emails_urls))), |
24 |
url(r'^api/', include(rest_urls)), |
|
25 | 23 |
url(r'^menu.json$', menu_json, name='menu_json'), |
26 | 24 |
url(r'^hobos.json$', hobo), |
27 | 25 |
url(r'^admin/', include(admin.site.urls)), |
tests/test_health_api.py | ||
---|---|---|
4 | 4 |
import requests |
5 | 5 |
import socket |
6 | 6 | |
7 |
from django.core.cache import cache |
|
7 | 8 |
from django.utils import timezone |
8 | 9 | |
10 |
from httmock import urlmatch, remember_called, HTTMock |
|
11 | ||
9 | 12 |
from hobo.environment.models import Authentic, Combo |
10 | 13 | |
14 |
from test_manager import admin_user, login |
|
15 | ||
11 | 16 |
pytestmark = pytest.mark.django_db |
12 | 17 | |
13 | 18 | |
... | ... | |
22 | 27 |
c.save() |
23 | 28 | |
24 | 29 | |
25 |
def test_response(app, services, monkeypatch): |
|
30 |
def test_response(app, admin_user, services, monkeypatch): |
|
31 |
cache.clear() |
|
26 | 32 |
monkeypatch.setattr(socket, 'gethostbyname', lambda x: '176.31.123.109') |
27 | 33 |
monkeypatch.setattr(requests, 'get', lambda x, verify: MagicMock(status_code=200)) |
28 |
response = app.get('/api/health/')
|
|
34 |
response = login(app).get('/sites/health.json')
|
|
29 | 35 |
assert response.status_code == 200 |
30 | 36 |
content = json.loads(response.content) |
31 | 37 |
assert 'blues' in content['data'].keys() |
32 | 38 |
assert 'jazz' in content['data'].keys() |
33 | 39 | |
34 | 40 | |
35 |
def test_is_resolvable(app, services, monkeypatch): |
|
41 |
def test_is_resolvable(app, admin_user, services, monkeypatch): |
|
42 |
cache.clear() |
|
36 | 43 |
def gethostname(netloc): |
37 | 44 |
if netloc == "jazz.example.publik": |
38 | 45 |
return '176.31.123.109' |
... | ... | |
40 | 47 |
raise socket.gaierror |
41 | 48 |
monkeypatch.setattr(socket, 'gethostbyname', gethostname) |
42 | 49 |
monkeypatch.setattr(requests, 'get', lambda x, verify: MagicMock(status_code=200)) |
43 |
response = app.get('/api/health/')
|
|
50 |
response = login(app).get('/sites/health.json')
|
|
44 | 51 |
content = json.loads(response.content) |
45 | 52 |
blues = content['data']['blues'] |
46 | 53 |
jazz = content['data']['jazz'] |
... | ... | |
48 | 55 |
assert jazz['is_resolvable'] |
49 | 56 | |
50 | 57 | |
51 |
def test_is_running(app, services, monkeypatch): |
|
52 |
def get(url, verify): |
|
53 |
if url == 'https://jazz.example.publik/manage/': |
|
54 |
return MagicMock(status_code=200) |
|
55 |
else: |
|
56 |
return MagicMock(status_code=404) |
|
58 |
def test_is_running(app, admin_user, services, monkeypatch): |
|
59 |
cache.clear() |
|
57 | 60 |
monkeypatch.setattr(socket, 'gethostbyname', lambda x: '176.31.123.109') |
58 |
monkeypatch.setattr(requests, 'get', get) |
|
59 |
response = app.get('/api/health/') |
|
60 |
content = json.loads(response.content) |
|
61 |
blues = content['data']['blues'] |
|
62 |
jazz = content['data']['jazz'] |
|
63 |
assert not blues['is_running'] |
|
64 |
assert jazz['is_running'] |
|
61 | ||
62 |
@urlmatch(netloc='jazz.example.publik') |
|
63 |
@remember_called |
|
64 |
def jazz_mock(url, request): |
|
65 |
return {'status_code': 200} |
|
66 | ||
67 |
@urlmatch(netloc='blues.example.publik') |
|
68 |
@remember_called |
|
69 |
def blues_mock(url, request): |
|
70 |
return {'status_code': 404} |
|
71 | ||
72 |
with HTTMock(blues_mock, jazz_mock) as mock: |
|
73 |
response = login(app).get('/sites/health.json') |
|
74 |
content = json.loads(response.content) |
|
75 |
blues = content['data']['blues'] |
|
76 |
jazz = content['data']['jazz'] |
|
77 |
assert not blues['is_running'] |
|
78 |
assert jazz['is_running'] |
|
79 |
assert blues_mock.call['count'] == 2 |
|
80 |
assert jazz_mock.call['count'] == 2 |
|
81 | ||
82 |
# check it gets results from cache |
|
83 |
response = login(app).get('/sites/health.json') |
|
84 |
content = json.loads(response.content) |
|
85 |
blues = content['data']['blues'] |
|
86 |
jazz = content['data']['jazz'] |
|
87 |
assert not blues['is_running'] |
|
88 |
assert jazz['is_running'] |
|
89 |
assert blues_mock.call['count'] == 2 |
|
90 |
assert jazz_mock.call['count'] == 2 |
|
65 | 91 | |
66 | 92 | |
67 |
def test_is_operational(app, services, monkeypatch): |
|
93 |
def test_is_operational(app, admin_user, services, monkeypatch): |
|
94 |
cache.clear() |
|
68 | 95 |
monkeypatch.setattr(socket, 'gethostbyname', lambda x: '176.31.123.109') |
69 | 96 |
monkeypatch.setattr(requests, 'get', lambda x, verify: MagicMock(status_code=200)) |
70 |
response = app.get('/api/health/')
|
|
97 |
response = login(app).get('/sites/health.json')
|
|
71 | 98 |
content = json.loads(response.content) |
72 | 99 |
blues = content['data']['blues'] |
73 | 100 |
jazz = content['data']['jazz'] |
... | ... | |
75 | 102 |
assert not jazz['is_operational'] |
76 | 103 | |
77 | 104 | |
78 |
def test_has_valid_certificate(app, services, monkeypatch): |
|
105 |
def test_has_valid_certificate(app, admin_user, services, monkeypatch): |
|
106 |
cache.clear() |
|
79 | 107 |
def get(url, verify): |
80 | 108 |
if 'blues.example.publik' in url or not verify: |
81 | 109 |
return MagicMock(status_code=200) |
... | ... | |
83 | 111 |
raise requests.exceptions.SSLError |
84 | 112 |
monkeypatch.setattr(socket, 'gethostbyname', lambda x: '176.31.123.109') |
85 | 113 |
monkeypatch.setattr(requests, 'get', get) |
86 |
response = app.get('/api/health/')
|
|
114 |
response = login(app).get('/sites/health.json')
|
|
87 | 115 |
content = json.loads(response.content) |
88 | 116 |
blues = content['data']['blues'] |
89 | 117 |
jazz = content['data']['jazz'] |
90 |
- |