Skip to content

Commit

Permalink
Simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pikhovkin committed Dec 1, 2021
1 parent 9762527 commit 1decdd3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 51 deletions.
7 changes: 6 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name='django-simple-health-check',
version='0.3.0',
version='0.3.1',
description='Simple Django health check',
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
Expand Down Expand Up @@ -41,4 +41,9 @@
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
],
keywords=[
'django', 'monitoring', 'healthcheck', 'health-check', 'ping', 'health-checks', 'healthchecks',
'liveness', 'readiness', 'liveness-detection', 'readiness-checker', 'django-health-check',
'readiness-detection', 'liveness-checker',
],
)
6 changes: 5 additions & 1 deletion simple_health_check/apps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.apps import AppConfig
from django.conf import settings
from django.core.signals import setting_changed
from django.utils.module_loading import import_string


Expand All @@ -9,7 +10,7 @@ class SimpleHealthCheckConfig(AppConfig):
checks = {}

@classmethod
def register_checks(cls):
def register_checks(cls, **kwargs):
cls.checks = {}
SIMPLE_HEALTH_CHECKS = getattr(settings, 'SIMPLE_HEALTH_CHECKS', None)
if SIMPLE_HEALTH_CHECKS is None:
Expand Down Expand Up @@ -43,3 +44,6 @@ def check_all(cls):

def ready(self):
self.register_checks()


setting_changed.connect(SimpleHealthCheckConfig.register_checks)
61 changes: 12 additions & 49 deletions simple_health_check/tests.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
from django.test import TestCase, override_settings
from django.apps import apps


class SimpleTest(TestCase):
def test_liveness(self):
apps.get_app_config('simple_health_check').register_checks()

response = self.client.get('/liveness/')
self.assertTrue(response.status_code == 200)
self.assertTrue(response.content == b'ok')
self.assertContains(response, b'ok')

def test_readiness(self):
apps.get_app_config('simple_health_check').register_checks()

response = self.client.get('/readiness/')
self.assertTrue(response.status_code == 200)
self.assertTrue(response.content == b'ok')
self.assertContains(response, b'ok')

@override_settings(SIMPLE_HEALTH_CHECKS={'simple_health_check.checks.dummy.DummyFalse': None})
def test_no_readiness(self):
apps.get_app_config('simple_health_check').register_checks()

response = self.client.get('/readiness/')
self.assertTrue(response.status_code == 500)
self.assertTrue(response.content == b'down')
self.assertContains(response, b'down', status_code=500)

@override_settings(
CACHES={
Expand All @@ -32,11 +22,8 @@ def test_no_readiness(self):
SIMPLE_HEALTH_CHECKS={'simple_health_check.checks.caches.CacheBackends': None},
)
def test_caches(self):
apps.get_app_config('simple_health_check').register_checks()

response = self.client.get('/readiness/')
self.assertTrue(response.status_code == 200)
self.assertTrue(response.content == b'ok')
self.assertContains(response, b'ok')

@override_settings(
CACHES={
Expand All @@ -51,11 +38,8 @@ def test_caches(self):
},
)
def test_cache_aliases(self):
apps.get_app_config('simple_health_check').register_checks()

response = self.client.get('/readiness/')
self.assertTrue(response.status_code == 200)
self.assertTrue(response.content == b'ok')
self.assertContains(response, b'ok')

@override_settings(
CACHES={
Expand All @@ -69,80 +53,59 @@ def test_cache_aliases(self):
},
)
def test_cache_no_rediness(self):
apps.get_app_config('simple_health_check').register_checks()

response = self.client.get('/readiness/')
self.assertTrue(response.status_code == 500)
self.assertTrue(response.content == b'down')
self.assertContains(response, b'down', status_code=500)

@override_settings(
SIMPLE_HEALTH_CHECKS={
'simple_health_check.checks.ps.DiskUsage': None,
},
)
def test_ps_disk_usage_no_value(self):
apps.get_app_config('simple_health_check').register_checks()

response = self.client.get('/readiness/')
self.assertTrue(response.status_code == 200)
self.assertTrue(response.content == b'ok')
self.assertContains(response, b'ok')

@override_settings(
SIMPLE_HEALTH_CHECKS={
'simple_health_check.checks.ps.DiskUsage': dict(max_usage_percent=99),
},
)
def test_ps_disk_usage(self):
apps.get_app_config('simple_health_check').register_checks()

response = self.client.get('/readiness/')
self.assertTrue(response.status_code == 200)
self.assertTrue(response.content == b'ok')
self.assertContains(response, b'ok')

@override_settings(
SIMPLE_HEALTH_CHECKS={
'simple_health_check.checks.ps.DiskUsage': dict(max_usage_percent=0.001),
},
)
def test_ps_disk_usage_no_rediness(self):
apps.get_app_config('simple_health_check').register_checks()

response = self.client.get('/readiness/')
self.assertTrue(response.status_code == 500)
self.assertTrue(response.content == b'down')
self.assertContains(response, b'down', status_code=500)

@override_settings(
SIMPLE_HEALTH_CHECKS={
'simple_health_check.checks.ps.MemoryUsage': None,
},
)
def test_ps_memory_usage_no_value(self):
apps.get_app_config('simple_health_check').register_checks()

response = self.client.get('/readiness/')
self.assertTrue(response.status_code == 200)
self.assertTrue(response.content == b'ok')
self.assertContains(response, b'ok')

@override_settings(
SIMPLE_HEALTH_CHECKS={
'simple_health_check.checks.ps.MemoryUsage': dict(min_memory_mb=10),
},
)
def test_ps_memory_usage(self):
apps.get_app_config('simple_health_check').register_checks()

response = self.client.get('/readiness/')
self.assertTrue(response.status_code == 200)
self.assertTrue(response.content == b'ok')
self.assertContains(response, b'ok')

@override_settings(
SIMPLE_HEALTH_CHECKS={
'simple_health_check.checks.ps.MemoryUsage': dict(min_memory_mb=100_000),
},
)
def test_ps_memory_usage_no_rediness(self):
apps.get_app_config('simple_health_check').register_checks()

response = self.client.get('/readiness/')
self.assertTrue(response.status_code == 500)
self.assertTrue(response.content == b'down')
self.assertContains(response, b'down', status_code=500)

0 comments on commit 1decdd3

Please sign in to comment.