Skip to content

Commit

Permalink
Refactor package structure
Browse files Browse the repository at this point in the history
- Move all apps into single `health_check` package
- None Django backends move in `health_check.contrib`
- Update requirements
  • Loading branch information
codingjoe committed Jan 7, 2017
1 parent b2a4f35 commit fa72b2f
Show file tree
Hide file tree
Showing 33 changed files with 96 additions and 59 deletions.
4 changes: 2 additions & 2 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
recursive-include health_check/templates *
include README.md
include LICENSE
recursive-include health_check *
1 change: 1 addition & 0 deletions health_check/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class HealthCheckStatusType(object):
working = 1
unexpected_result = 2


HEALTH_CHECK_STATUS_TYPE_TRANSLATOR = {
0: _("unavailable"),
1: _("working"),
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ def check_status(self):
except Exception:
raise ServiceUnavailable("Unknown exception")


plugin_dir.register(CacheBackend)
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
BaseHealthCheckBackend, ServiceUnavailable
)
from health_check.plugins import plugin_dir
from health_check_celery.tasks import add
from .tasks import add

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -47,4 +47,5 @@ def check_status(self):
)
raise ServiceUnavailable("Unknown error")


plugin_dir.register(CeleryHealthCheck)
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging

from health_check.plugins import plugin_dir
from health_check_storage.base import StorageHealthCheck
from health_check.storage.base import StorageHealthCheck


class S3BotoStorageHealthCheck(StorageHealthCheck):
Expand All @@ -24,4 +24,5 @@ def check_delete(self, file_name):
storage = self.get_storage()
storage.delete(file_name)


plugin_dir.register(S3BotoStorageHealthCheck)
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class Migration(migrations.Migration):

initial = True

replaces = [
('health_check_db', '0001_initial'),
]

dependencies = [
]

Expand All @@ -19,5 +23,8 @@ class Migration(migrations.Migration):
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=128)),
],
options={
'db_table': 'health_check_db_testmodel',
},
),
]
Empty file.
3 changes: 3 additions & 0 deletions health_check_db/models.py → health_check/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@

class TestModel(models.Model):
title = models.CharField(max_length=128)

class Meta:
db_table = 'health_check_db_testmodel'
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from health_check.backends.base import (
BaseHealthCheckBackend, ServiceReturnedUnexpectedResult, ServiceUnavailable
)
from health_check.db.models import TestModel
from health_check.plugins import plugin_dir
from health_check_db.models import TestModel

logger = logging.getLogger(__name__)

Expand All @@ -28,4 +28,5 @@ def check_status(self):
logger.exception("Database Error")
raise ServiceUnavailable("Database error")


plugin_dir.register(DjangoDatabaseBackend)
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
from django.conf import settings

from health_check.plugins import plugin_dir
from health_check_storage.base import StorageHealthCheck
from .base import StorageHealthCheck


class DefaultFileStorageHealthCheck(StorageHealthCheck):
storage = settings.DEFAULT_FILE_STORAGE


plugin_dir.register(DefaultFileStorageHealthCheck)
11 changes: 11 additions & 0 deletions requirements-dev.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
celery
coverage
flake8
isort
mccabe
mock
pydocstyle
pep8
pep8-naming
pytest
pytest-django
35 changes: 24 additions & 11 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
-e .
celery==4.0.1
coverage==4.0.3
django-coverage-plugin==1.3
flake8==2.5.4
#
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile --output-file requirements-dev.txt requirements-dev.in
#
amqp==2.1.4 # via kombu
billiard==3.5.0.2 # via celery
celery==4.0.2
coverage==4.3.1
flake8==3.2.1
isort==4.2.5
mccabe==0.4.0
kombu==4.0.2 # via celery
mccabe==0.5.3
mock==2.0.0
pydocstyle==1.0.0
pbr==1.10.0 # via mock
pep8-naming==0.4.1
pep8==1.7.0
pep8-naming==0.3.3
pre-commit==0.8.0
py==1.4.31
pytest==2.9.1
py==1.4.32 # via pytest
pycodestyle==2.2.0 # via flake8
pydocstyle==1.1.1
pyflakes==1.3.0 # via flake8
pytest-django==3.1.2
pytest==3.0.5
pytz==2016.10 # via celery
six==1.10.0 # via mock
vine==1.1.3 # via amqp
4 changes: 1 addition & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ max-line-length = 119
max-complexity = 10
statistics = true
show-source = true
exclude = */migrations/*,docs/*,env/*
exclude = */migrations/*,docs/*,env/*,.tox/*

[pydocstyle]
add-ignore = D100,D101,D102,D103,D104,D105
match-dir = (?!tests|env|\.).*

[coverage:run]
source = .
plugins =
django_coverage_plugin
omit =
*/migrations/*
*/tests/*
Expand Down
13 changes: 2 additions & 11 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
import os

from setuptools import setup
from setuptools import find_packages, setup

from health_check import __version__

Expand All @@ -26,16 +26,7 @@ def read(fname):
license="BSD",
keywords="django health check monitoring",
url="https://github.com/KristianOellegaard/django-health-check",
packages=[
'health_check',
'health_check_celery',
'health_check_db',
'health_check_db.migrations',
'health_check_cache',
'health_check.backends',
'health_check_storage',
'health_check_storage_s3',
],
packages=find_packages(exclude=['tests', 'docs']),
long_description=read('README.rst'),
classifiers=[
'Topic :: Utilities',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_autodiscover.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class TestAutoDiscover(object):
def test_autodiscover(self):
health_check_plugins = list(filter(
lambda x: 'health_check_' in x,
lambda x: 'health_check.' in x,
settings.INSTALLED_APPS
))

Expand Down
11 changes: 5 additions & 6 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# -*- coding: utf-8 -*-
from django.core.cache.backends.base import BaseCache, CacheKeyWarning
from django.test import TestCase

from mock import patch

from health_check.backends.base import (
ServiceReturnedUnexpectedResult, ServiceUnavailable
)
from health_check_cache.plugin_health_check import CacheBackend
from health_check.cache.plugin_health_check import CacheBackend


# A Mock version of the cache to use for testing
Expand Down Expand Up @@ -50,27 +49,27 @@ class HealthCheckCacheTests(TestCase):
Ensures check_status returns/raises the expected result when the cache works, fails, or raises exceptions.
"""

@patch("health_check_cache.plugin_health_check.cache", MockCache())
@patch("health_check.cache.plugin_health_check.cache", MockCache())
def test_check_status_working(self):
cache_backend = CacheBackend()
self.assertTrue(cache_backend.check_status())

# check_status should raise ServiceUnavailable when values at cache key do not match
@patch("health_check_cache.plugin_health_check.cache", MockCache(set_works=False))
@patch("health_check.cache.plugin_health_check.cache", MockCache(set_works=False))
def test_set_fails(self):
cache_backend = CacheBackend()
with self.assertRaises(ServiceUnavailable):
cache_backend.check_status()

# check_status should catch generic exceptions raised by set and convert to ServiceUnavailable
@patch("health_check_cache.plugin_health_check.cache", MockCache(set_raises=Exception))
@patch("health_check.cache.plugin_health_check.cache", MockCache(set_raises=Exception))
def test_set_raises_generic(self):
cache_backend = CacheBackend()
with self.assertRaises(ServiceUnavailable):
cache_backend.check_status()

# check_status should catch CacheKeyWarning and convert to ServiceReturnedUnexpectedResult
@patch("health_check_cache.plugin_health_check.cache", MockCache(set_raises=CacheKeyWarning))
@patch("health_check.cache.plugin_health_check.cache", MockCache(set_raises=CacheKeyWarning))
def test_set_raises_cache_key_warning(self):
cache_backend = CacheBackend()
with self.assertRaises(ServiceReturnedUnexpectedResult):
Expand Down
11 changes: 5 additions & 6 deletions tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
from django.db import DatabaseError, IntegrityError
from django.db.models import Model
from django.test import TestCase

from mock import patch

from health_check.backends.base import (
ServiceReturnedUnexpectedResult, ServiceUnavailable
)
from health_check_db.plugin_health_check import DjangoDatabaseBackend
from health_check.db.plugin_health_check import DjangoDatabaseBackend


class MockDBModel(Model):
Expand Down Expand Up @@ -43,27 +42,27 @@ class HealthCheckDatabaseTests(TestCase):
Ensures check_status returns/raises the expected result when the database works or raises exceptions.
"""

@patch('health_check_db.plugin_health_check.TestModel.objects.create',
@patch('health_check.db.plugin_health_check.TestModel.objects.create',
lambda title=None: MockDBModel())
def test_check_status_works(self):
db_backend = DjangoDatabaseBackend()
self.assertTrue(db_backend.check_status())

@patch('health_check_db.plugin_health_check.TestModel.objects.create',
@patch('health_check.db.plugin_health_check.TestModel.objects.create',
lambda title=None: raise_(IntegrityError))
def test_raise_integrity_error(self):
db_backend = DjangoDatabaseBackend()
with self.assertRaises(ServiceReturnedUnexpectedResult):
db_backend.check_status()

@patch('health_check_db.plugin_health_check.TestModel.objects.create',
@patch('health_check.db.plugin_health_check.TestModel.objects.create',
lambda title=None: MockDBModel(error_thrown=DatabaseError))
def test_raise_database_error(self):
db_backend = DjangoDatabaseBackend()
with self.assertRaises(ServiceUnavailable):
db_backend.check_status()

@patch('health_check_db.plugin_health_check.TestModel.objects.create',
@patch('health_check.db.plugin_health_check.TestModel.objects.create',
lambda title=None: MockDBModel(error_thrown=Exception))
def test_raise_exception(self):
db_backend = DjangoDatabaseBackend()
Expand Down
14 changes: 7 additions & 7 deletions tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from django.test import TestCase

from health_check.backends.base import ServiceUnavailable
from health_check_storage.base import StorageHealthCheck
from health_check_storage.plugin_health_check import \
from health_check.storage.base import StorageHealthCheck
from health_check.storage.plugin_health_check import \
DefaultFileStorageHealthCheck


Expand Down Expand Up @@ -45,8 +45,8 @@ def get_file_content(*args, **kwargs):
return b'mockcontent'


@mock.patch("health_check_storage.base.StorageHealthCheck.get_file_name", get_file_name)
@mock.patch("health_check_storage.base.StorageHealthCheck.get_file_content", get_file_content)
@mock.patch("health_check.storage.base.StorageHealthCheck.get_file_name", get_file_name)
@mock.patch("health_check.storage.base.StorageHealthCheck.get_file_content", get_file_content)
class HealthCheckStorageTests(TestCase):
"""
Tests health check behavior with a mocked storage backend.
Expand All @@ -61,7 +61,7 @@ def test_get_storage(self):
self.assertIsInstance(default_storage.get_storage(), Storage)

@mock.patch(
"health_check_storage.plugin_health_check.DefaultFileStorageHealthCheck.storage",
"health_check.storage.plugin_health_check.DefaultFileStorageHealthCheck.storage",
MockStorage()
)
def test_check_status_working(self):
Expand All @@ -77,7 +77,7 @@ def test_check_status_working(self):
self.assertTrue(default_storage_health.check_status())

@mock.patch(
"health_check_storage.plugin_health_check.DefaultFileStorageHealthCheck.storage",
"health_check.storage.plugin_health_check.DefaultFileStorageHealthCheck.storage",
MockStorage(saves=False)
)
def test_file_does_not_exist(self):
Expand All @@ -87,7 +87,7 @@ def test_file_does_not_exist(self):
default_storage_health.check_status()

@mock.patch(
"health_check_storage.plugin_health_check.DefaultFileStorageHealthCheck.storage",
"health_check.storage.plugin_health_check.DefaultFileStorageHealthCheck.storage",
MockStorage(deletes=False)
)
def test_file_not_deleted(self):
Expand Down
2 changes: 0 additions & 2 deletions tests/testapp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
# -*- coding: utf-8 -*-
import django
django.setup()
10 changes: 10 additions & 0 deletions tests/testapp/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)
10 changes: 5 additions & 5 deletions tests/testapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
'django.contrib.staticfiles',

'health_check',
'health_check_cache',
'health_check_db',
'health_check_storage',
'health_check_celery',
'health_check_celery3',
'health_check.cache',
'health_check.db',
'health_check.storage',
'health_check.contrib.celery',
'health_check.contrib.s3boto_storage',
'tests',
)

Expand Down
Loading

0 comments on commit fa72b2f

Please sign in to comment.