Browse Source

More tests for notifications.

pull/40/head
Pēteris Caune 9 years ago
parent
commit
156f4dbeea
3 changed files with 38 additions and 14 deletions
  1. +6
    -5
      hc/api/models.py
  2. +20
    -5
      hc/api/tests/test_notify.py
  3. +12
    -4
      hc/api/transports.py

+ 6
- 5
hc/api/models.py View File

@ -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)
n.check_status = check.status
n.error = error
n.save()
if error != "no-op":
n = Notification(owner=check, channel=self)
n.check_status = check.status
n.error = error
n.save()
def test(self): def test(self):
return self.transport().test() return self.transport().test()


+ 20
- 5
hc/api/tests/test_notify.py View File

@ -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)
n = Notification.objects.get()
self.assertEqual(n.error, "")
self.assertEqual(Notification.objects.count(), 0)
@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")


+ 12
- 4
hc/api/transports.py View File

@ -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)
if r.status_code not in (200, 201):
return "Received status code %d" % r.status_code
try:
r = requests.post(url, json=payload, timeout=5, headers=headers)
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…
Cancel
Save