-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
32de047
commit ae198d6
Showing
6 changed files
with
145 additions
and
26 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,74 @@ | ||
import mock | ||
import tornado.gen | ||
from mock import ANY | ||
from tornado import ioloop | ||
from tornado.httpclient import HTTPRequest, HTTPResponse | ||
from tornado.testing import AsyncTestCase, gen_test | ||
|
||
from graphite_beacon.alerts import URLAlert | ||
from graphite_beacon.core import Reactor | ||
from graphite_beacon._compat import StringIO | ||
|
||
from ..util import build_graphite_response | ||
|
||
fetch_mock_url = lambda m: m.call_args_list[0][0][0] | ||
|
||
|
||
class TestGraphite(AsyncTestCase): | ||
|
||
target_url = 'http://localhost/check' | ||
|
||
def get_new_ioloop(self): | ||
return ioloop.IOLoop.instance() | ||
|
||
@mock.patch('graphite_beacon.alerts.hc.AsyncHTTPClient.fetch') | ||
@mock.patch('graphite_beacon.handlers.smtp.SMTPHandler.notify') | ||
@gen_test | ||
def test_graphite(self, mock_smpt_notify, mock_fetch): | ||
self.reactor = Reactor( | ||
alerts=[ | ||
{ | ||
'name': 'test', | ||
'source': 'url', | ||
'query': self.target_url, | ||
'rules': ['warning: != 200'] | ||
} | ||
], | ||
smtp={ | ||
'from': 'graphite@localhost', | ||
'to': ['alerts@localhost'], | ||
}, | ||
interval='0.25second', | ||
) | ||
|
||
assert not self.reactor.is_running() | ||
|
||
alert = list(self.reactor.alerts)[0] | ||
assert len(self.reactor.alerts) == 1 | ||
assert isinstance(alert, URLAlert) | ||
|
||
metric_data = [5, 7, 9] | ||
build_resp = lambda: HTTPResponse(HTTPRequest(self.target_url), 500, | ||
buffer=StringIO('')) | ||
|
||
mock_fetch.side_effect = iter(tornado.gen.maybe_future(build_resp()) | ||
for _ in range(10)) | ||
|
||
self.reactor.start(start_loop=False) | ||
yield tornado.gen.sleep(0.5) | ||
|
||
# There should be at least 1 immediate fetch + 1 instance of the PeriodicCallback | ||
assert mock_fetch.call_count >= 2 | ||
assert fetch_mock_url(mock_fetch) == self.target_url | ||
|
||
assert alert.state[self.target_url] == 'warning' | ||
assert mock_smpt_notify.call_count == 1 | ||
mock_smpt_notify.assert_called_once_with( | ||
'warning', | ||
alert, | ||
500.0, | ||
ntype='url', | ||
rule=ANY, | ||
target=self.target_url) | ||
|
||
self.reactor.stop(stop_loop=False) |
Empty file.
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,45 @@ | ||
import pytest | ||
|
||
from graphite_beacon.graphite import GraphiteRecord | ||
|
||
from ..util import build_graphite_response | ||
|
||
|
||
build_record = lambda data: GraphiteRecord(build_graphite_response(data=data)) | ||
|
||
|
||
class TestGraphiteRecord(object): | ||
def test_invalid_record(self): | ||
with pytest.raises(ValueError): | ||
GraphiteRecord('not,legit,data') | ||
|
||
def test_invalid_record_long(self): | ||
with pytest.raises(ValueError) as e: | ||
GraphiteRecord('<http>' + ('<tag>' * 50)) | ||
assert '<http>' in str(e.value) | ||
assert str(e.value).endswith('..') | ||
|
||
def test_record(self): | ||
assert build_record([1, 2, 3]).values == [1.0, 2.0, 3.0] | ||
|
||
def test_average(self): | ||
assert build_record([1]).average == 1.0 | ||
assert build_record([1, 2, 3]).average == 2.0 | ||
assert build_record([5, 5, 5, 10]).average == 6.25 | ||
assert build_record([1.5, 2.5, 3.5]).average == 2.5 | ||
|
||
def test_last_value(self): | ||
assert build_record([1]).last_value == 1.0 | ||
assert build_record([1, 2, 3]).last_value == 3.0 | ||
|
||
def test_sum(self): | ||
assert build_record([1]).sum == 1.0 | ||
assert build_record([1.5, 2.5, 3]).sum == 7.0 | ||
|
||
def test_minimum(self): | ||
assert build_record([1]).minimum == 1.0 | ||
assert build_record([9.0, 2.3, 4]).minimum == 2.3 | ||
|
||
def test_maximum(self): | ||
assert build_record([1]).maximum == 1.0 | ||
assert build_record([9.0, 2.3, 4]).maximum == 9.0 |
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