<s> import json <EOL> import socket <EOL> from twisted . trial import unittest <EOL> from twisted . internet import defer , endpoints , reactor <EOL> from twisted . web import server , static <EOL> from tensor . sources . linux import basic , process <EOL> from tensor . sources import riak , nginx , network <EOL> class TestLinuxSources ( unittest . TestCase ) : <EOL> def skip_if_no_hostname ( self ) : <EOL> try : <EOL> socket . gethostbyaddr ( socket . gethostname ( ) ) <EOL> except socket . herror : <EOL> raise unittest . SkipTest ( '<STR_LIT>' ) <EOL> def _qb ( self , result ) : <EOL> pass <EOL> def test_basic_cpu ( self ) : <EOL> self . skip_if_no_hostname ( ) <EOL> s = basic . CPU ( <EOL> { '<STR_LIT>' : <NUM_LIT:1.0> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT> } , self . _qb , None ) <EOL> try : <EOL> s . get ( ) <EOL> s . get ( ) <EOL> except : <EOL> raise unittest . SkipTest ( '<STR_LIT>' ) <EOL> def test_basic_cpu_calculation ( self ) : <EOL> s = basic . CPU ( { <EOL> '<STR_LIT>' : <NUM_LIT:1.0> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT:localhost>' , <EOL> } , self . _qb , None ) <EOL> stats = "<STR_LIT>" <EOL> s . _read_proc_stat = lambda : stats <EOL> self . assertEqual ( s . get ( ) , None ) <EOL> stats = "<STR_LIT>" <EOL> s . _read_proc_stat = lambda : stats <EOL> events = s . get ( ) <EOL> cpu_event = events [ - <NUM_LIT:1> ] <EOL> iowait_event = events [ <NUM_LIT:4> ] <EOL> self . assertEqual ( cpu_event . service , '<STR_LIT>' ) <EOL> self . assertEqual ( round ( cpu_event . metric , <NUM_LIT:4> ) , <NUM_LIT> ) <EOL> self . assertEqual ( iowait_event . service , '<STR_LIT>' ) <EOL> self . assertEqual ( round ( iowait_event . metric , <NUM_LIT:4> ) , <NUM_LIT> ) <EOL> def test_basic_cpu_calculation_no_guest_stats ( self ) : <EOL> s = basic . CPU ( { <EOL> '<STR_LIT>' : <NUM_LIT:1.0> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT:localhost>' , <EOL> } , self . _qb , None ) <EOL> stats = "<STR_LIT>" <EOL> s . _read_proc_stat = lambda : stats <EOL> self . assertEqual ( s . get ( ) , None ) <EOL> stats = "<STR_LIT>" <EOL> s . _read_proc_stat = lambda : stats <EOL> events = s . get ( ) <EOL> cpu_event = events [ - <NUM_LIT:1> ] <EOL> iowait_event = events [ <NUM_LIT:4> ] <EOL> self . assertEqual ( cpu_event . service , '<STR_LIT>' ) <EOL> self . assertEqual ( round ( cpu_event . metric , <NUM_LIT:4> ) , <NUM_LIT> ) <EOL> self . assertEqual ( iowait_event . service , '<STR_LIT>' ) <EOL> self . assertEqual ( round ( iowait_event . metric , <NUM_LIT:4> ) , <NUM_LIT> ) <EOL> def test_disk_io ( self ) : <EOL> s = basic . DiskIO ( { <EOL> '<STR_LIT>' : <NUM_LIT:1.0> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT:localhost>' } , self . _qb , None ) <EOL> stats = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> s . _getstats = lambda : stats <EOL> events = s . get ( ) <EOL> self . assertEqual ( events [ <NUM_LIT:0> ] . metric , <NUM_LIT:32> ) <EOL> self . assertEqual ( events [ <NUM_LIT:1> ] . metric , <NUM_LIT:2> ) <EOL> def test_basic_memory ( self ) : <EOL> self . skip_if_no_hostname ( ) <EOL> s = basic . Memory ( <EOL> { '<STR_LIT>' : <NUM_LIT:1.0> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT> } , self . _qb , None ) <EOL> s . get ( ) <EOL> def test_basic_load ( self ) : <EOL> self . skip_if_no_hostname ( ) <EOL> s = basic . LoadAverage ( <EOL> { '<STR_LIT>' : <NUM_LIT:1.0> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT> } , self . _qb , None ) <EOL> s . get ( ) <EOL> @ defer . inlineCallbacks <EOL> def test_process_count ( self ) : <EOL> self . skip_if_no_hostname ( ) <EOL> s = process . ProcessCount ( <EOL> { '<STR_LIT>' : <NUM_LIT:1.0> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT> } , self . _qb , None ) <EOL> yield s . get ( ) <EOL> @ defer . inlineCallbacks <EOL> def test_basic_disk_space ( self ) : <EOL> self . skip_if_no_hostname ( ) <EOL> s = basic . DiskFree ( <EOL> { '<STR_LIT>' : <NUM_LIT:1.0> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT> } , self . _qb , None ) <EOL> yield s . get ( ) <EOL> @ defer . inlineCallbacks <EOL> def test_process_stats ( self ) : <EOL> self . skip_if_no_hostname ( ) <EOL> s = process . ProcessStats ( <EOL> { '<STR_LIT>' : <NUM_LIT:1.0> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT> } , self . _qb , None ) <EOL> yield s . get ( ) <EOL> @ defer . inlineCallbacks <EOL> def test_http_request ( self ) : <EOL> self . skip_if_no_hostname ( ) <EOL> s = network . HTTP ( { <EOL> '<STR_LIT>' : <NUM_LIT:1.0> , <EOL> '<STR_LIT>' : '<STR_LIT:http>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT:url>' : '<STR_LIT>' <EOL> } , self . _qb , None ) <EOL> event = yield s . _get ( ) <EOL> self . assertEquals ( event . state , '<STR_LIT>' ) <EOL> @ defer . inlineCallbacks <EOL> def test_http_request_timeout ( self ) : <EOL> self . skip_if_no_hostname ( ) <EOL> s = network . HTTP ( { <EOL> '<STR_LIT>' : <NUM_LIT:1.0> , <EOL> '<STR_LIT>' : '<STR_LIT:http>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT:url>' : '<STR_LIT>' <EOL> } , self . _qb , None ) <EOL> event = yield s . _get ( ) <EOL> self . assertEquals ( event . state , '<STR_LIT>' ) <EOL> self . assertEquals ( '<STR_LIT>' in event . description , True ) <EOL> @ defer . inlineCallbacks <EOL> def test_http_request_fail ( self ) : <EOL> self . skip_if_no_hostname ( ) <EOL> s = network . HTTP ( { <EOL> '<STR_LIT>' : <NUM_LIT:1.0> , <EOL> '<STR_LIT>' : '<STR_LIT:http>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT:url>' : '<STR_LIT>' <EOL> } , self . _qb , None ) <EOL> event = yield s . _get ( ) <EOL> self . assertEquals ( event . state , '<STR_LIT>' ) <EOL> def test_network_stats ( self ) : <EOL> self . skip_if_no_hostname ( ) <EOL> s = basic . Network ( <EOL> { '<STR_LIT>' : <NUM_LIT:1.0> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT> } , self . _qb , None ) <EOL> s . _readStats = lambda : [ <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' <EOL> ] <EOL> ev = s . get ( ) <EOL> self . assertEquals ( ev [ <NUM_LIT:0> ] . metric , <NUM_LIT> ) <EOL> self . assertEquals ( ev [ <NUM_LIT:1> ] . metric , <NUM_LIT> ) <EOL> self . assertEquals ( ev [ <NUM_LIT:2> ] . metric , <NUM_LIT:0> ) <EOL> self . assertEquals ( ev [ <NUM_LIT:3> ] . metric , <NUM_LIT> ) <EOL> self . assertEquals ( ev [ <NUM_LIT:4> ] . metric , <NUM_LIT> ) <EOL> self . assertEquals ( ev [ <NUM_LIT:5> ] . metric , <NUM_LIT:0> ) <EOL> def test_nginx_parse ( self ) : <EOL> src = nginx . Nginx ( { <EOL> '<STR_LIT>' : <NUM_LIT:1.0> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT:localhost>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , self . _qb , None ) <EOL> ngstats = """<STR_LIT>""" <EOL> metrics = src . _parse_nginx_stats ( ngstats ) <EOL> self . assertEquals ( metrics [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> def test_nginx_log_nohistory ( self ) : <EOL> try : <EOL> os . unlink ( '<STR_LIT>' ) <EOL> os . unlink ( '<STR_LIT>' ) <EOL> except : <EOL> pass <EOL> events = [ ] <EOL> def qb ( src , ev ) : <EOL> events . append ( ev ) <EOL> f = open ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . flush ( ) <EOL> src = nginx . NginxLogMetrics ( { <EOL> '<STR_LIT>' : <NUM_LIT:1.0> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT:localhost>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:file>' : '<STR_LIT>' <EOL> } , qb , None ) <EOL> src . log . tmp = '<STR_LIT>' <EOL> src . get ( ) <EOL> self . assertEquals ( len ( events ) , <NUM_LIT:0> ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . flush ( ) <EOL> src . get ( ) <EOL> self . assertEquals ( len ( events ) > <NUM_LIT:0> , True ) <EOL> def test_nginx_log ( self ) : <EOL> try : <EOL> os . unlink ( '<STR_LIT>' ) <EOL> os . unlink ( '<STR_LIT>' ) <EOL> except : <EOL> pass <EOL> events = [ ] <EOL> def qb ( src , ev ) : <EOL> events . append ( ev ) <EOL> f = open ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . flush ( ) <EOL> src = nginx . NginxLogMetrics ( { <EOL> '<STR_LIT>' : <NUM_LIT:1.0> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT:localhost>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:file>' : '<STR_LIT>' <EOL> } , qb , None ) <EOL> src . log . tmp = '<STR_LIT>' <EOL> src . get ( ) <EOL> ev1 = events [ <NUM_LIT:0> ] <EOL> ev2 = events [ <NUM_LIT:1> ] <EOL> for i in ev1 : <EOL> if i . service == '<STR_LIT>' : <EOL> self . assertEquals ( i . metric , <NUM_LIT> ) <EOL> for i in ev2 : <EOL> if i . service == '<STR_LIT>' : <EOL> self . assertEquals ( i . metric , <NUM_LIT> ) <EOL> events = [ ] <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . close ( ) <EOL> src . get ( ) <EOL> for i in events [ <NUM_LIT:0> ] : <EOL> if i . service == '<STR_LIT>' : <EOL> self . assertEquals ( i . metric , <NUM_LIT:2> ) <EOL> if i . service == '<STR_LIT>' : <EOL> self . assertEquals ( i . metric , <NUM_LIT> ) <EOL> if i . service == '<STR_LIT>' : <EOL> self . assertEquals ( i . metric , <NUM_LIT> ) <EOL> class TestRiakSources ( unittest . TestCase ) : <EOL> def _qb ( self , result ) : <EOL> pass <EOL> def start_fake_riak_server ( self , stats ) : <EOL> def cb ( listener ) : <EOL> self . addCleanup ( listener . stopListening ) <EOL> return listener <EOL> data = static . Data ( json . dumps ( stats ) , '<STR_LIT:application/json>' ) <EOL> data . isLeaf = True <EOL> site = server . Site ( data ) <EOL> endpoint = endpoints . TCP4ServerEndpoint ( reactor , <NUM_LIT:0> ) <EOL> return endpoint . listen ( site ) . addCallback ( cb ) <EOL> def make_riak_stats_source ( self , config_overrides = { } ) : <EOL> config = { <EOL> '<STR_LIT>' : <NUM_LIT:1.0> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT:localhost>' , <EOL> } <EOL> config . update ( config_overrides ) <EOL> return riak . RiakStats ( config , self . _qb , None ) <EOL> @ defer . inlineCallbacks <EOL> def test_riak_stats_zeros ( self ) : <EOL> listener = yield self . start_fake_riak_server ( { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> } ) <EOL> addr = listener . getHost ( ) <EOL> url = '<STR_LIT>' % ( addr . port , ) <EOL> s = self . make_riak_stats_source ( { '<STR_LIT:url>' : url } ) <EOL> [ gets , puts ] = yield s . get ( ) <EOL> self . assertEqual ( gets . service , "<STR_LIT>" ) <EOL> self . assertEqual ( gets . metric , <NUM_LIT:0.0> ) <EOL> self . assertEqual ( puts . service , "<STR_LIT>" ) <EOL> self . assertEqual ( puts . metric , <NUM_LIT:0.0> ) <EOL> @ defer . inlineCallbacks <EOL> def test_riak_stats ( self ) : <EOL> listener = yield self . start_fake_riak_server ( { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } ) <EOL> addr = listener . getHost ( ) <EOL> url = '<STR_LIT>' % ( addr . port , ) <EOL> s = self . make_riak_stats_source ( { '<STR_LIT:url>' : url } ) <EOL> [ gets , puts ] = yield s . get ( ) <EOL> self . assertEqual ( gets . service , "<STR_LIT>" ) <EOL> self . assertEqual ( gets . metric , <NUM_LIT> ) <EOL> self . assertEqual ( puts . service , "<STR_LIT>" ) <EOL> self . assertEqual ( puts . metric , <NUM_LIT> ) </s>
