<s> from test import CollectorTestCase <EOL> from test import get_collector_config <EOL> from test import unittest <EOL> from mock import Mock <EOL> from mock import patch <EOL> from diamond . collector import Collector <EOL> from traceroute import TracerouteCollector <EOL> class TestTracerouteCollector ( CollectorTestCase ) : <EOL> def setUp ( self ) : <EOL> config = get_collector_config ( '<STR_LIT>' , { <EOL> } ) <EOL> self . collector = TracerouteCollector ( config , None ) <EOL> self . collector . config [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . collector . config [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> def test_import ( self ) : <EOL> self . assertTrue ( TracerouteCollector ) <EOL> @ patch . object ( Collector , '<STR_LIT>' ) <EOL> def test_should_work_with_real_data ( self , publish_mock ) : <EOL> with patch ( '<STR_LIT>' ) as process_mock : <EOL> with patch . object ( process_mock . return_value , '<STR_LIT>' ) as out_mock : <EOL> with patch . object ( process_mock . return_value , '<STR_LIT>' ) as err_mock : <EOL> out_mock . readline = self . getFixture ( '<STR_LIT>' ) . readline <EOL> err_mock . readline . return_value = None <EOL> self . collector . collect ( ) <EOL> rtts = self . getFixture ( '<STR_LIT>' ) . getvalue ( ) . split ( '<STR_LIT:\n>' ) <EOL> for idx , call in enumerate ( publish_mock . mock_calls ) : <EOL> name , args , kwargs = call <EOL> metric_name , metric_value = args <EOL> self . assertEquals ( metric_name , '<STR_LIT>' ) <EOL> self . assertEquals ( metric_value , float ( rtts [ idx ] ) ) <EOL> @ patch . object ( Collector , '<STR_LIT>' ) <EOL> def test_sent_dimensions ( self , publish_metric_mock ) : <EOL> with patch ( '<STR_LIT>' ) as process_mock : <EOL> with patch . object ( process_mock . return_value , '<STR_LIT>' ) as out_mock : <EOL> with patch . object ( process_mock . return_value , '<STR_LIT>' ) as err_mock : <EOL> out_mock . readline = self . getFixture ( '<STR_LIT>' ) . readline <EOL> err_mock . readline . return_value = None <EOL> self . collector . collect ( ) <EOL> hops = self . getFixture ( '<STR_LIT>' ) . getvalue ( ) . split ( '<STR_LIT:\n>' ) <EOL> for idx , call in enumerate ( publish_metric_mock . mock_calls ) : <EOL> name , args , kwargs = call <EOL> metric = args [ <NUM_LIT:0> ] <EOL> hop , ip = hops [ idx ] . strip ( ) . split ( '<STR_LIT:|>' ) <EOL> if '<STR_LIT:*>' not in ip : <EOL> self . assertEquals ( metric . dimensions , { <EOL> '<STR_LIT>' : hop , <EOL> '<STR_LIT>' : ip , <EOL> } ) <EOL> else : <EOL> self . assertEquals ( metric . dimensions , { <EOL> '<STR_LIT>' : hop , <EOL> } ) <EOL> @ patch . object ( Collector , '<STR_LIT>' ) <EOL> def test_should_fail_gracefully ( self , publish_mock ) : <EOL> with patch ( '<STR_LIT>' ) as process_mock : <EOL> with patch . object ( process_mock . return_value , '<STR_LIT>' ) as out_mock : <EOL> with patch . object ( process_mock . return_value , '<STR_LIT>' ) as err_mock : <EOL> with patch . object ( self . collector . log , '<STR_LIT:error>' ) as error_logger : <EOL> err_mock . readline . return_value = '<STR_LIT>' <EOL> self . collector . collect ( ) <EOL> error_logger . assert_called_once_with ( '<STR_LIT>' ) <EOL> self . assertPublishedMany ( publish_mock , { } ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
