forked from GithubBackups/healthchecks
More tests for notifications.
This commit is contained in:
parent
90d7806783
commit
156f4dbeea
@ -144,13 +144,14 @@ class Channel(models.Model):
|
|||||||
# Make 3 attempts--
|
# Make 3 attempts--
|
||||||
for x in range(0, 3):
|
for x in range(0, 3):
|
||||||
error = self.transport.notify(check) or ""
|
error = self.transport.notify(check) or ""
|
||||||
if error == "":
|
if error in ("", "no-op"):
|
||||||
break # Success!
|
break # Success!
|
||||||
|
|
||||||
n = Notification(owner=check, channel=self)
|
if error != "no-op":
|
||||||
n.check_status = check.status
|
n = Notification(owner=check, channel=self)
|
||||||
n.error = error
|
n.check_status = check.status
|
||||||
n.save()
|
n.error = error
|
||||||
|
n.save()
|
||||||
|
|
||||||
def test(self):
|
def test(self):
|
||||||
return self.transport().test()
|
return self.transport().test()
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
from django.core import mail
|
from django.core import mail
|
||||||
from mock import patch
|
from mock import patch
|
||||||
from requests.exceptions import ReadTimeout
|
from requests.exceptions import ConnectionError, Timeout
|
||||||
|
|
||||||
from hc.api.models import Channel, Check, Notification
|
from hc.api.models import Channel, Check, Notification
|
||||||
from hc.test import BaseTestCase
|
from hc.test import BaseTestCase
|
||||||
@ -30,7 +30,7 @@ class NotifyTestCase(BaseTestCase):
|
|||||||
u"http://example", headers={"User-Agent": "healthchecks.io"},
|
u"http://example", headers={"User-Agent": "healthchecks.io"},
|
||||||
timeout=5)
|
timeout=5)
|
||||||
|
|
||||||
@patch("hc.api.transports.requests.get", side_effect=ReadTimeout)
|
@patch("hc.api.transports.requests.get", side_effect=Timeout)
|
||||||
def test_webhooks_handle_timeouts(self, mock_get):
|
def test_webhooks_handle_timeouts(self, mock_get):
|
||||||
self._setup_data("webhook", "http://example")
|
self._setup_data("webhook", "http://example")
|
||||||
self.channel.notify(self.check)
|
self.channel.notify(self.check)
|
||||||
@ -38,15 +38,21 @@ class NotifyTestCase(BaseTestCase):
|
|||||||
n = Notification.objects.get()
|
n = Notification.objects.get()
|
||||||
self.assertEqual(n.error, "Connection timed out")
|
self.assertEqual(n.error, "Connection timed out")
|
||||||
|
|
||||||
|
@patch("hc.api.transports.requests.get", side_effect=ConnectionError)
|
||||||
|
def test_webhooks_handle_connection_errors(self, mock_get):
|
||||||
|
self._setup_data("webhook", "http://example")
|
||||||
|
self.channel.notify(self.check)
|
||||||
|
|
||||||
|
n = Notification.objects.get()
|
||||||
|
self.assertEqual(n.error, "A connection to http://example failed")
|
||||||
|
|
||||||
@patch("hc.api.transports.requests.get")
|
@patch("hc.api.transports.requests.get")
|
||||||
def test_webhooks_ignore_up_events(self, mock_get):
|
def test_webhooks_ignore_up_events(self, mock_get):
|
||||||
self._setup_data("webhook", "http://example", status="up")
|
self._setup_data("webhook", "http://example", status="up")
|
||||||
self.channel.notify(self.check)
|
self.channel.notify(self.check)
|
||||||
|
|
||||||
self.assertFalse(mock_get.called)
|
self.assertFalse(mock_get.called)
|
||||||
|
self.assertEqual(Notification.objects.count(), 0)
|
||||||
n = Notification.objects.get()
|
|
||||||
self.assertEqual(n.error, "")
|
|
||||||
|
|
||||||
@patch("hc.api.transports.requests.get")
|
@patch("hc.api.transports.requests.get")
|
||||||
def test_webhooks_handle_500(self, mock_get):
|
def test_webhooks_handle_500(self, mock_get):
|
||||||
@ -113,6 +119,15 @@ class NotifyTestCase(BaseTestCase):
|
|||||||
n = Notification.objects.get()
|
n = Notification.objects.get()
|
||||||
self.assertEqual(n.error, "Received status code 500")
|
self.assertEqual(n.error, "Received status code 500")
|
||||||
|
|
||||||
|
@patch("hc.api.transports.requests.post", side_effect=Timeout)
|
||||||
|
def test_slack_handles_timeout(self, mock_post):
|
||||||
|
self._setup_data("slack", "123")
|
||||||
|
|
||||||
|
self.channel.notify(self.check)
|
||||||
|
|
||||||
|
n = Notification.objects.get()
|
||||||
|
self.assertEqual(n.error, "Connection timed out")
|
||||||
|
|
||||||
@patch("hc.api.transports.requests.post")
|
@patch("hc.api.transports.requests.post")
|
||||||
def test_hipchat(self, mock_post):
|
def test_hipchat(self, mock_post):
|
||||||
self._setup_data("hipchat", "123")
|
self._setup_data("hipchat", "123")
|
||||||
|
@ -57,7 +57,7 @@ class Webhook(Transport):
|
|||||||
def notify(self, check):
|
def notify(self, check):
|
||||||
# Webhook integration only fires when check goes down.
|
# Webhook integration only fires when check goes down.
|
||||||
if check.status != "down":
|
if check.status != "down":
|
||||||
return
|
return "no-op"
|
||||||
|
|
||||||
# Webhook transport sends no arguments, so the
|
# Webhook transport sends no arguments, so the
|
||||||
# notify and test actions are the same
|
# notify and test actions are the same
|
||||||
@ -72,14 +72,22 @@ class Webhook(Transport):
|
|||||||
except requests.exceptions.Timeout:
|
except requests.exceptions.Timeout:
|
||||||
# Well, we tried
|
# Well, we tried
|
||||||
return "Connection timed out"
|
return "Connection timed out"
|
||||||
|
except requests.exceptions.ConnectionError:
|
||||||
|
return "A connection to %s failed" % self.channel.value
|
||||||
|
|
||||||
|
|
||||||
class JsonTransport(Transport):
|
class JsonTransport(Transport):
|
||||||
def post(self, url, payload):
|
def post(self, url, payload):
|
||||||
headers = {"User-Agent": "healthchecks.io"}
|
headers = {"User-Agent": "healthchecks.io"}
|
||||||
r = requests.post(url, json=payload, timeout=5, headers=headers)
|
try:
|
||||||
if r.status_code not in (200, 201):
|
r = requests.post(url, json=payload, timeout=5, headers=headers)
|
||||||
return "Received status code %d" % r.status_code
|
if r.status_code not in (200, 201):
|
||||||
|
return "Received status code %d" % r.status_code
|
||||||
|
except requests.exceptions.Timeout:
|
||||||
|
# Well, we tried
|
||||||
|
return "Connection timed out"
|
||||||
|
except requests.exceptions.ConnectionError:
|
||||||
|
return "A connection to %s failed" % url
|
||||||
|
|
||||||
|
|
||||||
class Slack(JsonTransport):
|
class Slack(JsonTransport):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user