-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #261 from uktrade/LTD-4964-dbt-healthcheck
LTD-4964-dbt-healthcheck
- Loading branch information
Showing
5 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import pytest | ||
|
||
from django.urls import reverse | ||
|
||
from health_check.backends import BaseHealthCheckBackend | ||
from health_check.exceptions import HealthCheckException | ||
from health_check.plugins import plugin_dir | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def backends(): | ||
unaltered_value = plugin_dir._registry | ||
yield plugin_dir | ||
plugin_dir._registry = unaltered_value | ||
|
||
|
||
class HealthCheckOk(BaseHealthCheckBackend): | ||
def check_status(self): | ||
return True | ||
|
||
def run_check(self): | ||
super().run_check() | ||
self.time_taken = 0.33 | ||
|
||
|
||
class HealthCheckBroken(BaseHealthCheckBackend): | ||
def check_status(self): | ||
raise HealthCheckException("error") | ||
|
||
def run_check(self): | ||
super().run_check() | ||
self.time_taken = 0.23 | ||
|
||
|
||
def test_healthcheck_ok(client, backends): | ||
backends.reset() | ||
backends.register(HealthCheckOk) | ||
url = reverse("healthcheck-pingdom") | ||
response = client.get(url) | ||
assert response.status_code == 200 | ||
assert response.render().content == ( | ||
b'<?xml version="1.0" encoding="UTF-8"?>' | ||
b"<pingdom_http_custom_check><status>\n \n " | ||
b"OK\n \n " | ||
b"</status>" | ||
b"<response_time>0.33</response_time>" | ||
b"</pingdom_http_custom_check>\n" | ||
) | ||
|
||
|
||
def test_healthcheck_down(client, backends): | ||
backends.reset() | ||
backends.register(HealthCheckBroken) | ||
url = reverse("healthcheck-pingdom") | ||
response = client.get(url) | ||
|
||
assert response.status_code == 500 | ||
assert response.render().content == ( | ||
b'<?xml version="1.0" encoding="UTF-8"?>' | ||
b"<pingdom_http_custom_check>" | ||
b"<status>\n \n " | ||
b"HealthCheckBroken: unknown error: error\n \n " | ||
b"</status><response_time>0.23</response_time>" | ||
b"</pingdom_http_custom_check>\n" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from health_check.views import MainView | ||
|
||
|
||
class HealthCheckPingdomView(MainView): | ||
template_name = "pingdom.xml" | ||
|
||
def render_to_response(self, context, status): | ||
context["errored_plugins"] = [plugin for plugin in context["plugins"] if plugin.errors] | ||
context["total_response_time"] = sum([plugin.time_taken for plugin in context["plugins"]]) | ||
return super().render_to_response(context=context, status=status, content_type="text/xml") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{% spaceless %} | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<pingdom_http_custom_check> | ||
<status> | ||
{% for plugin in errored_plugins %} | ||
{{ plugin.identifier }}: {{ plugin.pretty_status }} | ||
{% empty %} | ||
OK | ||
{% endfor %} | ||
</status> | ||
<response_time>{{ total_response_time }}</response_time> | ||
</pingdom_http_custom_check> | ||
{% endspaceless %} |