Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #4579 from jwhitlock/k8s-probes-1422009
Browse files Browse the repository at this point in the history
bug 1422009: Adjust liveness tests, avoid DB query in middleware
  • Loading branch information
jwhitlock committed Dec 4, 2017
2 parents c714a15 + de06c5b commit 365848e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
23 changes: 14 additions & 9 deletions kuma/health/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@
from django.core.urlresolvers import reverse


@pytest.mark.parametrize('http_method',
['get', 'head', 'put', 'post', 'delete', 'options'])
def test_liveness(db, client, http_method):
@pytest.mark.parametrize('http_method', ['put', 'post', 'delete', 'options'])
@pytest.mark.parametrize('endpoint', ['health.liveness', 'health.readiness'])
def test_disallowed_methods(client, http_method, endpoint):
"""Alternate HTTP methods are not allowed."""
url = reverse(endpoint)
response = getattr(client, http_method)(url)
assert response.status_code == 405


@pytest.mark.parametrize('http_method', ['get', 'head'])
def test_liveness(client, http_method):
url = reverse('health.liveness')
response = getattr(client, http_method)(url)
assert (response.status_code ==
204 if http_method in ('get', 'head') else 405)
assert response.status_code == 204


@pytest.mark.parametrize('http_method',
['get', 'head', 'put', 'post', 'delete', 'options'])
@pytest.mark.parametrize('http_method', ['get', 'head'])
def test_readiness(db, client, http_method):
url = reverse('health.readiness')
response = getattr(client, http_method)(url)
assert (response.status_code ==
204 if http_method in ('get', 'head') else 405)
assert response.status_code == 204


def test_readiness_with_db_error(db, client):
Expand Down
7 changes: 7 additions & 0 deletions kuma/wiki/middleware.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect
from django.shortcuts import render

Expand Down Expand Up @@ -30,6 +31,12 @@ def process_request(self, request):
('$subscribe' in request.path or '$files' in request.path)):
return None

# Skip slugs that don't have locales, and won't be in a zone
request_slug = request.path_info.lstrip('/')
if any(request_slug.startswith(slug)
for slug in settings.LANGUAGE_URL_IGNORED_PATHS):
return None

remaps = DocumentZoneURLRemapsJob().get(request.LANGUAGE_CODE)
for original_path, new_path in remaps:

Expand Down
7 changes: 7 additions & 0 deletions kuma/wiki/tests/test_middleware.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from django.conf import settings
from django.test import RequestFactory

from kuma.core.cache import memcache
Expand Down Expand Up @@ -123,6 +124,12 @@ def test_no_redirect(self):
(self.other_doc.slug, endpoint))
self.assertIsNone(middleware.process_request(request))

def test_skip_no_language_urls(self):
middleware = DocumentZoneMiddleware()
for path in settings.LANGUAGE_URL_IGNORED_PATHS:
request = self.rf.get('/' + path)
self.assertIsNone(middleware.process_request(request))

def test_zone_url_ends_with_slash(self):
"""Ensure urls only rewrite with a '/' at the end of url_root
Expand Down

0 comments on commit 365848e

Please sign in to comment.