<s> from graphitesend import graphitesend <EOL> import unittest2 as unittest <EOL> import socket <EOL> import os <EOL> class TestAll ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> graphitesend . reset ( ) <EOL> graphitesend . default_graphite_server = '<STR_LIT:localhost>' <EOL> self . hostname = os . uname ( ) [ <NUM_LIT:1> ] <EOL> self . server = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> self . server . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , <NUM_LIT:1> ) <EOL> self . server . bind ( ( '<STR_LIT:localhost>' , <NUM_LIT> ) ) <EOL> self . server . listen ( <NUM_LIT:5> ) <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> graphitesend . reset ( ) <EOL> try : <EOL> self . server . shutdown ( socket . SHUT_RD ) <EOL> self . server . close ( ) <EOL> except Exception : <EOL> pass <EOL> self . server = None <EOL> def test_connect_exception_on_badhost ( self ) : <EOL> bad_graphite_server = '<STR_LIT>' <EOL> graphitesend . default_graphite_server = bad_graphite_server <EOL> with self . assertRaises ( graphitesend . GraphiteSendException ) : <EOL> graphitesend . init ( ) <EOL> def test_set_lowercase_metric_names ( self ) : <EOL> g = graphitesend . init ( lowercase_metric_names = True ) <EOL> self . assertEqual ( g . lowercase_metric_names , True ) <EOL> def test_lowercase_metric_names ( self ) : <EOL> g = graphitesend . init ( lowercase_metric_names = True ) <EOL> send_data = g . send ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . assertEqual ( '<STR_LIT>' in send_data , True ) <EOL> self . assertEqual ( '<STR_LIT>' in send_data , False ) <EOL> def test_create_graphitesend_instance ( self ) : <EOL> g = graphitesend . init ( ) <EOL> expected_type = type ( graphitesend . GraphiteClient ( ) ) <EOL> g_type = type ( g ) <EOL> self . assertEqual ( g_type , expected_type ) <EOL> def test_monkey_patch_of_graphitehost ( self ) : <EOL> g = graphitesend . init ( ) <EOL> custom_prefix = g . addr [ <NUM_LIT:0> ] <EOL> self . assertEqual ( custom_prefix , '<STR_LIT:localhost>' ) <EOL> def test_fqdn_squash ( self ) : <EOL> g = graphitesend . init ( fqdn_squash = True ) <EOL> custom_prefix = g . prefix <EOL> expected_results = '<STR_LIT>' % self . hostname . replace ( '<STR_LIT:.>' , '<STR_LIT:_>' ) <EOL> self . assertEqual ( custom_prefix , expected_results ) <EOL> def test_noprefix ( self ) : <EOL> g = graphitesend . init ( ) <EOL> custom_prefix = g . prefix <EOL> self . assertEqual ( custom_prefix , '<STR_LIT>' % self . hostname ) <EOL> def test_system_name ( self ) : <EOL> g = graphitesend . init ( system_name = '<STR_LIT>' ) <EOL> custom_prefix = g . prefix <EOL> expected_prefix = '<STR_LIT>' <EOL> self . assertEqual ( custom_prefix , expected_prefix ) <EOL> def test_empty_system_name ( self ) : <EOL> g = graphitesend . init ( system_name = '<STR_LIT>' ) <EOL> custom_prefix = g . prefix <EOL> expected_prefix = '<STR_LIT>' <EOL> self . assertEqual ( custom_prefix , expected_prefix ) <EOL> def test_no_system_name ( self ) : <EOL> g = graphitesend . init ( group = '<STR_LIT:foo>' ) <EOL> custom_prefix = g . prefix <EOL> expected_prefix = '<STR_LIT>' % self . hostname <EOL> self . assertEqual ( custom_prefix , expected_prefix ) <EOL> def test_prefix ( self ) : <EOL> g = graphitesend . init ( prefix = '<STR_LIT>' ) <EOL> custom_prefix = g . prefix <EOL> self . assertEqual ( custom_prefix , '<STR_LIT>' % self . hostname ) <EOL> def test_prefix_double_dot ( self ) : <EOL> g = graphitesend . init ( prefix = '<STR_LIT>' ) <EOL> custom_prefix = g . prefix <EOL> self . assertEqual ( custom_prefix , '<STR_LIT>' % self . hostname ) <EOL> def test_prefix_remove_spaces ( self ) : <EOL> g = graphitesend . init ( prefix = '<STR_LIT>' ) <EOL> custom_prefix = g . prefix <EOL> self . assertEqual ( custom_prefix , '<STR_LIT>' % self . hostname ) <EOL> def test_set_prefix_group ( self ) : <EOL> g = graphitesend . init ( prefix = '<STR_LIT>' , group = '<STR_LIT>' ) <EOL> custom_prefix = g . prefix <EOL> expected_prefix = '<STR_LIT>' % self . hostname <EOL> self . assertEqual ( custom_prefix , expected_prefix ) <EOL> def test_set_prefix_group_system ( self ) : <EOL> g = graphitesend . init ( prefix = '<STR_LIT>' , system_name = '<STR_LIT>' , <EOL> group = '<STR_LIT>' ) <EOL> custom_prefix = g . prefix <EOL> expected_prefix = '<STR_LIT>' <EOL> self . assertEqual ( custom_prefix , expected_prefix ) <EOL> def test_set_suffix ( self ) : <EOL> g = graphitesend . init ( suffix = '<STR_LIT>' ) <EOL> custom_suffix = g . suffix <EOL> self . assertEqual ( custom_suffix , '<STR_LIT>' ) <EOL> def test_set_group_prefix ( self ) : <EOL> g = graphitesend . init ( group = '<STR_LIT>' ) <EOL> expected_prefix = "<STR_LIT>" % self . hostname <EOL> custom_prefix = g . prefix <EOL> self . assertEqual ( custom_prefix , expected_prefix ) <EOL> def test_default_prefix ( self ) : <EOL> g = graphitesend . init ( ) <EOL> expected_prefix = "<STR_LIT>" % self . hostname <EOL> custom_prefix = g . prefix <EOL> self . assertEqual ( custom_prefix , expected_prefix ) <EOL> def test_leave_suffix ( self ) : <EOL> g = graphitesend . init ( ) <EOL> default_suffix = g . suffix <EOL> self . assertEqual ( default_suffix , '<STR_LIT>' ) <EOL> def test_clean_metric ( self ) : <EOL> g = graphitesend . init ( ) <EOL> metric_name = g . clean_metric_name ( '<STR_LIT>' ) <EOL> self . assertEqual ( metric_name , '<STR_LIT>' ) <EOL> metric_name = g . clean_metric_name ( '<STR_LIT>' ) <EOL> self . assertEqual ( metric_name , '<STR_LIT>' ) <EOL> metric_name = g . clean_metric_name ( '<STR_LIT>' ) <EOL> self . assertEqual ( metric_name , '<STR_LIT>' ) <EOL> def test_clean_metric_false ( self ) : <EOL> g = graphitesend . init ( clean_metric_name = False ) <EOL> metric_name = g . clean_metric_name ( '<STR_LIT>' ) <EOL> self . assertEqual ( metric_name , '<STR_LIT>' ) <EOL> metric_name = g . clean_metric_name ( '<STR_LIT>' ) <EOL> self . assertEqual ( metric_name , '<STR_LIT>' ) <EOL> metric_name = g . clean_metric_name ( '<STR_LIT>' ) <EOL> self . assertEqual ( metric_name , '<STR_LIT>' ) <EOL> def test_reset ( self ) : <EOL> graphitesend . init ( ) <EOL> graphitesend . reset ( ) <EOL> graphite_instance = graphitesend . _module_instance <EOL> self . assertEqual ( graphite_instance , None ) <EOL> def test_force_failure_on_send ( self ) : <EOL> graphite_instance = graphitesend . init ( ) <EOL> graphite_instance . disconnect ( ) <EOL> with self . assertRaises ( graphitesend . GraphiteSendException ) : <EOL> graphite_instance . send ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> def test_force_unknown_failure_on_send ( self ) : <EOL> graphite_instance = graphitesend . init ( ) <EOL> graphite_instance . socket = None <EOL> with self . assertRaises ( graphitesend . GraphiteSendException ) : <EOL> graphite_instance . send ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> def test_send_list_metric_value ( self ) : <EOL> graphite_instance = graphitesend . init ( prefix = '<STR_LIT:test>' , system_name = '<STR_LIT>' ) <EOL> response = graphite_instance . send_list ( [ ( '<STR_LIT>' , <NUM_LIT:1> ) ] ) <EOL> self . assertEqual ( '<STR_LIT>' in response , True ) <EOL> self . assertEqual ( '<STR_LIT>' in response , True ) <EOL> def test_send_list_metric_value_single_timestamp ( self ) : <EOL> graphite_instance = graphitesend . init ( prefix = '<STR_LIT:test>' ) <EOL> response = graphite_instance . send_list ( [ ( '<STR_LIT>' , <NUM_LIT:1> ) ] , timestamp = <NUM_LIT:1> ) <EOL> self . assertEqual ( '<STR_LIT>' in response , True ) <EOL> self . assertEqual ( response . endswith ( '<STR_LIT>' ) , True ) <EOL> def test_send_list_metric_value_timestamp ( self ) : <EOL> graphite_instance = graphitesend . init ( prefix = '<STR_LIT:test>' ) <EOL> response = graphite_instance . send_list ( [ ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) ] ) <EOL> self . assertEqual ( '<STR_LIT>' in response , True ) <EOL> self . assertEqual ( response . endswith ( '<STR_LIT>' ) , True ) <EOL> def test_send_list_metric_value_timestamp_2 ( self ) : <EOL> graphite_instance = graphitesend . init ( prefix = '<STR_LIT:test>' , system_name = '<STR_LIT>' ) <EOL> response = graphite_instance . send_list ( <EOL> [ ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) , ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:2> ) ] ) <EOL> self . assertEqual ( '<STR_LIT>' in response , True ) <EOL> self . assertEqual ( '<STR_LIT>' in response , True ) <EOL> def test_send_list_metric_value_timestamp_3 ( self ) : <EOL> graphite_instance = graphitesend . init ( prefix = '<STR_LIT:test>' , system_name = '<STR_LIT>' ) <EOL> response = graphite_instance . send_list ( <EOL> [ <EOL> ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:2> ) , <EOL> ] <EOL> ) <EOL> self . assertEqual ( '<STR_LIT>' in response , True ) <EOL> self . assertEqual ( '<STR_LIT>' not in response , True ) <EOL> def test_send_list_metric_value_timestamp_default ( self ) : <EOL> graphite_instance = graphitesend . init ( prefix = '<STR_LIT:test>' , system_name = '<STR_LIT:bar>' ) <EOL> response = graphite_instance . send_list ( <EOL> [ <EOL> ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:2> ) , <EOL> ] , <EOL> timestamp = '<STR_LIT:4>' <EOL> ) <EOL> self . assertEqual ( '<STR_LIT>' in response , True ) <EOL> self . assertEqual ( '<STR_LIT>' in response , True ) <EOL> def test_send_list_metric_value_timestamp_default_2 ( self ) : <EOL> graphite_instance = graphitesend . init ( prefix = '<STR_LIT:test>' , system_name = '<STR_LIT:foo>' ) <EOL> ( c , addr ) = self . server . accept ( ) <EOL> response = graphite_instance . send_list ( <EOL> [ <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:2> , <NUM_LIT:2> ) , <EOL> ] , <EOL> timestamp = '<STR_LIT:4>' <EOL> ) <EOL> self . assertEqual ( '<STR_LIT>' in response , True ) <EOL> self . assertEqual ( '<STR_LIT>' in response , True ) <EOL> sent_on_socket = c . recv ( <NUM_LIT> ) <EOL> self . assertEqual ( '<STR_LIT>' in sent_on_socket , True ) <EOL> self . assertEqual ( '<STR_LIT>' in sent_on_socket , True ) <EOL> def test_send_value_as_string ( self ) : <EOL> graphite_instance = graphitesend . init ( prefix = '<STR_LIT>' ) <EOL> response = graphite_instance . send ( "<STR_LIT>" , "<STR_LIT:1>" , "<STR_LIT:1>" ) <EOL> self . assertEqual ( '<STR_LIT>' in response , True ) <EOL> print response <EOL> self . assertEqual ( response . endswith ( '<STR_LIT>' ) , True ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
