<s> """<STR_LIT>""" <EOL> import logging <EOL> import pytest <EOL> import mock <EOL> @ pytest . fixture <EOL> def reactor ( ) : <EOL> from graphite_beacon . core import Reactor <EOL> return Reactor ( history_size = '<STR_LIT>' ) <EOL> def test_reactor ( ) : <EOL> from graphite_beacon . core import Reactor <EOL> rr = Reactor ( ) <EOL> assert rr <EOL> assert rr . reinit ( ) <EOL> rr = Reactor ( include = [ '<STR_LIT>' ] , alerts = [ <EOL> { '<STR_LIT:name>' : '<STR_LIT:test>' , '<STR_LIT>' : '<STR_LIT:*>' , '<STR_LIT>' : [ "<STR_LIT>" ] } ] ) <EOL> assert rr . options [ '<STR_LIT>' ] == '<STR_LIT>' <EOL> assert len ( rr . alerts ) == <NUM_LIT:3> <EOL> def test_convert_config_log_level ( ) : <EOL> from graphite_beacon . core import _get_numeric_log_level <EOL> assert logging . DEBUG == _get_numeric_log_level ( '<STR_LIT>' ) <EOL> assert logging . DEBUG == _get_numeric_log_level ( '<STR_LIT>' ) <EOL> assert logging . INFO == _get_numeric_log_level ( '<STR_LIT:info>' ) <EOL> assert logging . INFO == _get_numeric_log_level ( '<STR_LIT>' ) <EOL> assert logging . WARN == _get_numeric_log_level ( '<STR_LIT>' ) <EOL> assert logging . WARN == _get_numeric_log_level ( '<STR_LIT>' ) <EOL> assert logging . WARNING == _get_numeric_log_level ( '<STR_LIT>' ) <EOL> assert logging . WARNING == _get_numeric_log_level ( '<STR_LIT>' ) <EOL> assert logging . ERROR == _get_numeric_log_level ( '<STR_LIT:error>' ) <EOL> assert logging . ERROR == _get_numeric_log_level ( '<STR_LIT>' ) <EOL> assert logging . CRITICAL == _get_numeric_log_level ( '<STR_LIT>' ) <EOL> assert logging . CRITICAL == _get_numeric_log_level ( '<STR_LIT>' ) <EOL> def test_public_graphite_url ( ) : <EOL> from graphite_beacon . core import Reactor <EOL> rr = Reactor ( graphite_url = '<STR_LIT>' , public_graphite_url = None ) <EOL> rr . reinit ( ) <EOL> assert rr . options . get ( "<STR_LIT>" ) == '<STR_LIT>' <EOL> rr . reinit ( public_graphite_url = "<STR_LIT>" ) <EOL> assert rr . options . get ( "<STR_LIT>" ) == "<STR_LIT>" <EOL> def test_alert ( reactor ) : <EOL> from graphite_beacon . alerts import BaseAlert , GraphiteAlert , URLAlert <EOL> alert1 = BaseAlert . get ( reactor , name = '<STR_LIT>' , query = '<STR_LIT:*>' , rules = [ "<STR_LIT>" ] ) <EOL> assert alert1 <EOL> assert isinstance ( alert1 , GraphiteAlert ) <EOL> alert2 = BaseAlert . get ( reactor , name = '<STR_LIT>' , query = '<STR_LIT:*>' , source = '<STR_LIT:url>' , rules = [ "<STR_LIT>" ] ) <EOL> assert isinstance ( alert2 , URLAlert ) <EOL> assert alert1 != alert2 <EOL> alert3 = BaseAlert . get ( reactor , name = '<STR_LIT>' , query = '<STR_LIT:*>' , interval = '<STR_LIT>' , rules = [ "<STR_LIT>" ] ) <EOL> assert alert3 . interval == '<STR_LIT>' <EOL> assert alert1 == alert3 <EOL> assert set ( [ alert1 , alert3 ] ) == set ( [ alert1 ] ) <EOL> alert = BaseAlert . get ( reactor , name = '<STR_LIT>' , query = '<STR_LIT:*>' , rules = [ "<STR_LIT>" ] ) <EOL> assert alert . rules [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:value>' ] == <NUM_LIT> <EOL> def test_multimetrics ( reactor ) : <EOL> from graphite_beacon . alerts import BaseAlert <EOL> alert = BaseAlert . get ( <EOL> reactor , name = "<STR_LIT>" , query = "<STR_LIT:*>" , rules = [ <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> reactor . alerts = set ( [ alert ] ) <EOL> with mock . patch . object ( reactor , '<STR_LIT>' ) : <EOL> alert . check ( [ ( <NUM_LIT> , '<STR_LIT>' ) , ( <NUM_LIT> , '<STR_LIT>' ) , ( <NUM_LIT:30> , '<STR_LIT>' ) ] ) <EOL> assert reactor . notify . call_count == <NUM_LIT:2> <EOL> assert reactor . notify . call_args_list [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == '<STR_LIT>' <EOL> assert reactor . notify . call_args_list [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ '<STR_LIT:target>' ] == '<STR_LIT>' <EOL> assert reactor . notify . call_args_list [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == '<STR_LIT>' <EOL> assert reactor . notify . call_args_list [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] [ '<STR_LIT:target>' ] == '<STR_LIT>' <EOL> assert list ( alert . history [ '<STR_LIT>' ] ) == [ <NUM_LIT> ] <EOL> with mock . patch . object ( reactor , '<STR_LIT>' ) : <EOL> alert . check ( [ ( <NUM_LIT> , '<STR_LIT>' ) , ( <NUM_LIT> , '<STR_LIT>' ) , ( <NUM_LIT:30> , '<STR_LIT>' ) ] ) <EOL> assert reactor . notify . call_count == <NUM_LIT:1> <EOL> assert reactor . notify . call_args_list [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == '<STR_LIT>' <EOL> assert reactor . notify . call_args_list [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ '<STR_LIT:target>' ] == '<STR_LIT>' <EOL> assert list ( alert . history [ '<STR_LIT>' ] ) == [ <NUM_LIT> , <NUM_LIT> ] <EOL> with mock . patch . object ( reactor , '<STR_LIT>' ) : <EOL> alert . check ( [ ( <NUM_LIT> , '<STR_LIT>' ) , ( <NUM_LIT:30> , '<STR_LIT>' ) , ( <NUM_LIT> , '<STR_LIT>' ) ] ) <EOL> assert reactor . notify . call_count == <NUM_LIT:2> <EOL> assert reactor . notify . call_args_list [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == '<STR_LIT>' <EOL> assert reactor . notify . call_args_list [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ '<STR_LIT:target>' ] == '<STR_LIT>' <EOL> assert reactor . notify . call_args_list [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == '<STR_LIT>' <EOL> assert reactor . notify . call_args_list [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] [ '<STR_LIT:target>' ] == '<STR_LIT>' <EOL> assert list ( alert . history [ '<STR_LIT>' ] ) == [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> with mock . patch . object ( reactor , '<STR_LIT>' ) : <EOL> alert . check ( [ ( <NUM_LIT> , '<STR_LIT>' ) , ( <NUM_LIT:30> , '<STR_LIT>' ) , ( <NUM_LIT> , '<STR_LIT>' ) ] ) <EOL> assert reactor . notify . call_count == <NUM_LIT:0> <EOL> with mock . patch . object ( reactor , '<STR_LIT>' ) : <EOL> alert . check ( [ ( <NUM_LIT> , '<STR_LIT>' ) , ( <NUM_LIT> , '<STR_LIT>' ) , ( <NUM_LIT> , '<STR_LIT>' ) ] ) <EOL> assert reactor . notify . call_count == <NUM_LIT:1> <EOL> assert reactor . notify . call_args_list [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == '<STR_LIT>' <EOL> assert reactor . notify . call_args_list [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ '<STR_LIT:target>' ] == '<STR_LIT>' <EOL> assert list ( alert . history [ '<STR_LIT>' ] ) == [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> assert alert . state [ '<STR_LIT>' ] == '<STR_LIT>' <EOL> reactor . repeat ( ) <EOL> assert alert . state == { <EOL> None : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> def test_multiexpressions ( reactor ) : <EOL> from graphite_beacon . alerts import BaseAlert <EOL> alert = BaseAlert . get ( <EOL> reactor , name = "<STR_LIT>" , query = "<STR_LIT:*>" , rules = [ "<STR_LIT>" ] ) <EOL> reactor . alerts = set ( [ alert ] ) <EOL> with mock . patch . object ( reactor , '<STR_LIT>' ) : <EOL> alert . check ( [ <EOL> ( <NUM_LIT:50> , '<STR_LIT>' ) , ( <NUM_LIT> , '<STR_LIT>' ) , ( <NUM_LIT> , '<STR_LIT>' ) , ( <NUM_LIT> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT> , '<STR_LIT>' ) , ( <NUM_LIT> , '<STR_LIT>' ) ] ) <EOL> assert reactor . notify . call_count == <NUM_LIT:1> <EOL> assert reactor . notify . call_args_list [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == '<STR_LIT>' <EOL> assert reactor . notify . call_args_list [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ '<STR_LIT:target>' ] == '<STR_LIT>' <EOL> assert list ( alert . history [ '<STR_LIT>' ] ) == [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> def test_invalid_handler ( reactor ) : <EOL> reactor . reinit ( critical_handlers = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> assert len ( reactor . handlers [ '<STR_LIT>' ] ) == <NUM_LIT:1> <EOL> def test_convert ( ) : <EOL> from graphite_beacon . utils import convert_to_format , convert_from_format <EOL> assert convert_to_format ( <NUM_LIT> ) == <NUM_LIT> <EOL> assert convert_from_format ( <NUM_LIT> ) <EOL> assert convert_to_format ( <NUM_LIT> , '<STR_LIT>' ) == "<STR_LIT>" <EOL> assert convert_from_format ( '<STR_LIT>' , '<STR_LIT:%>' ) == <NUM_LIT> <EOL> assert convert_to_format ( <NUM_LIT> , '<STR_LIT>' ) == <NUM_LIT> <EOL> assert convert_to_format ( <NUM_LIT> , '<STR_LIT>' ) == '<STR_LIT>' <EOL> assert convert_from_format ( '<STR_LIT>' , '<STR_LIT>' ) == <NUM_LIT> <EOL> assert convert_to_format ( <NUM_LIT> , '<STR_LIT>' ) == '<STR_LIT>' <EOL> assert convert_from_format ( '<STR_LIT>' , '<STR_LIT>' ) == <NUM_LIT> <EOL> assert convert_to_format ( <NUM_LIT> , '<STR_LIT>' ) == '<STR_LIT>' <EOL> assert convert_from_format ( '<STR_LIT>' , '<STR_LIT>' ) == <NUM_LIT> <EOL> assert convert_from_format ( '<STR_LIT>' , '<STR_LIT>' ) == <NUM_LIT> <EOL> assert convert_from_format ( '<STR_LIT>' , '<STR_LIT>' ) == <NUM_LIT> <EOL> assert convert_to_format ( <NUM_LIT> , '<STR_LIT>' ) == <NUM_LIT> <EOL> assert convert_to_format ( <NUM_LIT> , '<STR_LIT>' ) == '<STR_LIT>' <EOL> assert convert_from_format ( '<STR_LIT>' , '<STR_LIT>' ) == <NUM_LIT> <EOL> assert convert_to_format ( <NUM_LIT> , '<STR_LIT>' ) == '<STR_LIT>' <EOL> assert convert_from_format ( '<STR_LIT>' , '<STR_LIT>' ) == <NUM_LIT> <EOL> assert convert_to_format ( <NUM_LIT> , '<STR_LIT>' ) == '<STR_LIT>' <EOL> assert convert_from_format ( '<STR_LIT>' , '<STR_LIT>' ) == <NUM_LIT> <EOL> assert convert_to_format ( <NUM_LIT> , '<STR_LIT:s>' ) == "<STR_LIT>" <EOL> assert convert_from_format ( '<STR_LIT>' , '<STR_LIT:m>' ) == <NUM_LIT> <EOL> assert convert_to_format ( <NUM_LIT> , '<STR_LIT:s>' ) == "<STR_LIT>" <EOL> assert convert_from_format ( '<STR_LIT>' , '<STR_LIT:w>' ) == <NUM_LIT> <EOL> assert convert_to_format ( <NUM_LIT> , '<STR_LIT:s>' ) == "<STR_LIT>" <EOL> assert convert_to_format ( <NUM_LIT> , '<STR_LIT>' ) == "<STR_LIT>" <EOL> assert convert_to_format ( <NUM_LIT> , '<STR_LIT>' ) == "<STR_LIT>" <EOL> def test_parse_interval ( ) : <EOL> from graphite_beacon . utils import parse_interval <EOL> assert parse_interval ( <NUM_LIT:10> ) == <NUM_LIT> <EOL> assert parse_interval ( '<STR_LIT>' ) == <NUM_LIT> <EOL> assert parse_interval ( '<STR_LIT>' ) == <NUM_LIT> <EOL> assert parse_interval ( '<STR_LIT>' ) == <NUM_LIT> <EOL> assert parse_interval ( '<STR_LIT>' ) == <NUM_LIT> <EOL> assert parse_interval ( '<STR_LIT>' ) == <NUM_LIT> <EOL> assert parse_interval ( '<STR_LIT>' ) == <NUM_LIT> <EOL> assert parse_interval ( '<STR_LIT>' ) == <NUM_LIT> <EOL> def test_interval_to_graphite ( ) : <EOL> from graphite_beacon . utils import interval_to_graphite <EOL> assert interval_to_graphite ( '<STR_LIT>' ) == '<STR_LIT>' <EOL> assert interval_to_graphite ( '<STR_LIT>' ) == '<STR_LIT>' <EOL> assert interval_to_graphite ( '<STR_LIT>' ) == '<STR_LIT>' <EOL> def test_parse_rule ( ) : <EOL> from graphite_beacon . utils import parse_rule as parse_rule , IDENTITY <EOL> from funcparserlib . lexer import LexerError <EOL> import operator as op <EOL> with pytest . raises ( LexerError ) : <EOL> assert parse_rule ( '<STR_LIT>' ) <EOL> assert parse_rule ( '<STR_LIT>' ) == { <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ { '<STR_LIT>' : op . eq , '<STR_LIT:value>' : <NUM_LIT:0> , '<STR_LIT>' : IDENTITY } ] } <EOL> assert parse_rule ( '<STR_LIT>' ) == { <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ { '<STR_LIT>' : op . lt , '<STR_LIT:value>' : <NUM_LIT> , '<STR_LIT>' : IDENTITY } ] } <EOL> assert parse_rule ( '<STR_LIT>' ) == { <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ { '<STR_LIT>' : op . ge , '<STR_LIT:value>' : <NUM_LIT> , '<STR_LIT>' : IDENTITY } ] } <EOL> assert parse_rule ( '<STR_LIT>' ) == { <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ { '<STR_LIT>' : op . ge , '<STR_LIT:value>' : '<STR_LIT>' , '<STR_LIT>' : IDENTITY } ] } <EOL> assert parse_rule ( '<STR_LIT>' ) == { <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ { '<STR_LIT>' : op . ge , '<STR_LIT:value>' : '<STR_LIT>' , '<STR_LIT>' : IDENTITY } , <EOL> op . and_ , <EOL> { '<STR_LIT>' : op . gt , '<STR_LIT:value>' : <NUM_LIT> , '<STR_LIT>' : IDENTITY } ] } <EOL> rule = parse_rule ( '<STR_LIT>' ) <EOL> assert rule [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> assert rule [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ( <NUM_LIT:5> ) == <NUM_LIT:6> <EOL> def test_html_template ( reactor ) : <EOL> from graphite_beacon . handlers . smtp import SMTPHandler <EOL> from graphite_beacon . alerts import BaseAlert <EOL> target = '<STR_LIT>' <EOL> galert = BaseAlert . get ( reactor , name = '<STR_LIT>' , query = '<STR_LIT:*>' , rules = [ "<STR_LIT>" ] ) <EOL> galert . history [ target ] += [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] <EOL> reactor . options [ '<STR_LIT>' ] = { <EOL> '<STR_LIT:to>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> smtp = SMTPHandler ( reactor ) <EOL> message = smtp . get_message ( <EOL> '<STR_LIT>' , galert , <NUM_LIT> , target = target , ntype = '<STR_LIT>' , rule = galert . rules [ <NUM_LIT:0> ] ) <EOL> assert message <EOL> assert len ( message . _payload ) == <NUM_LIT:2> <EOL> text , html = message . _payload <EOL> assert '<STR_LIT>' in html . as_string ( ) <EOL> ualert = BaseAlert . get ( <EOL> reactor , source = '<STR_LIT:url>' , name = '<STR_LIT>' , query = '<STR_LIT>' , rules = [ "<STR_LIT>" ] ) <EOL> message = smtp . get_message ( '<STR_LIT>' , ualert , '<STR_LIT>' , target , '<STR_LIT:url>' ) <EOL> assert message <EOL> assert len ( message . _payload ) == <NUM_LIT:2> <EOL> _ , html = message . _payload <EOL> assert '<STR_LIT>' in html . as_string ( ) <EOL> ealert = BaseAlert . get ( reactor , name = '<STR_LIT>' , query = '<STR_LIT:*>' , rules = [ "<STR_LIT>" ] ) <EOL> message = smtp . get_message ( <EOL> '<STR_LIT>' , ealert , <NUM_LIT:8> , target = target , ntype = '<STR_LIT>' , rule = ealert . rules [ <NUM_LIT:0> ] ) <EOL> assert message <EOL> assert len ( message . _payload ) == <NUM_LIT:2> </s>
