def test_time_window(): # Time window set explicitly on the alert - should be preferred alert = BaseAlert.get(Reactor(), time_window='6second', interval='3second', **BASIC_GRAPHITE_ALERT_OPTS) assert alert.time_window.as_tuple() == (6, SECOND) # Time window set explicitly at the root - should be preferred next reactor = Reactor(interval='10second', time_window='4second') alert = BaseAlert.get(reactor, interval='3second', **BASIC_GRAPHITE_ALERT_OPTS) assert alert.time_window.as_tuple() == (4, SECOND) # No time window set, but interval set directly on the alert reactor = Reactor(interval='10second') alert = BaseAlert.get(reactor, interval='1second', **BASIC_GRAPHITE_ALERT_OPTS) assert alert.time_window.as_tuple() == (1, SECOND) # Only time interval set at root reactor = Reactor(interval='10second') alert = BaseAlert.get(reactor, **BASIC_GRAPHITE_ALERT_OPTS) assert alert.time_window.as_tuple() == (10, SECOND)
def test_reactor(): from graphite_beacon.core import Reactor rr = Reactor() assert rr assert rr.reinit() rr = Reactor(include=['example-config.json'], alerts=[{ 'name': 'test', 'query': '*', 'rules': ["normal: == 0"] }]) assert rr.options['interval'] == '20minute' assert len(rr.alerts) == 2
def test_public_graphite_url(): rr = Reactor(graphite_url='http://localhost', public_graphite_url=None) rr.reinit() assert rr.options.get("public_graphite_url") == 'http://localhost' rr.reinit(public_graphite_url="http://public") assert rr.options.get("public_graphite_url") == "http://public"
def test_graphite(self, mock_smpt_notify, mock_fetch): self.reactor = Reactor( alerts=[ { 'name': 'test', 'query': '*', 'rules': ["normal: == 0", "warning: >= 5"] }, ], smtp={ 'from': 'graphite@localhost', 'to': ['alerts@localhost'], }, interval='0.25second', time_window='10minute', until='1minute', ) assert not self.reactor.is_running() alert = list(self.reactor.alerts)[0] assert len(self.reactor.alerts) == 1 assert isinstance(alert, GraphiteAlert) metric_data = [5, 7, 9] build_resp = lambda: HTTPResponse( HTTPRequest('http://localhost:80/graphite'), 200, buffer=StringIO(build_graphite_response(data=metric_data))) 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 expected = 'http://localhost/render/?target=%2A&from=-11min&until=-1min&format=raw' assert fetch_mock_url(mock_fetch) == expected assert alert.state['*'] == 'warning' assert mock_smpt_notify.call_count == 1 mock_smpt_notify.assert_called_once_with('warning', alert, 7.0, ntype='graphite', rule=ANY, target='*') self.reactor.stop(stop_loop=False)
def test_reactor(): rr = Reactor() assert rr assert rr.reinit() rr = Reactor(include=['examples/example-config.json'], alerts=[{ 'name': 'test', 'query': '*', 'rules': ["normal: == 0"] }]) assert rr.options['interval'] == '20minute' assert len(rr.alerts) == 3 rr = Reactor(include=['examples/example-config.yml'], alerts=[{ 'name': 'test', 'query': '*', 'rules': ["normal: == 0"] }]) assert rr.options['interval'] == '20minute' assert len(rr.alerts) == 3
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)
def reactor(): from graphite_beacon.core import Reactor return Reactor(history_size='40m')
def reactor(): return Reactor(history_size='40m')