<s> """<STR_LIT>""" <EOL> from grr . endtoend_tests import base <EOL> from grr . lib import action_mocks <EOL> from grr . lib import aff4 <EOL> from grr . lib import client_fixture <EOL> from grr . lib import flags <EOL> from grr . lib import flow <EOL> from grr . lib import test_lib <EOL> from grr . lib import utils <EOL> from grr . lib . flows . cron import system <EOL> from grr . lib . flows . general import endtoend as endtoend_flows <EOL> from grr . lib . flows . general import endtoend_test <EOL> from grr . lib . rdfvalues import client as client_rdf <EOL> from grr . lib . rdfvalues import flows <EOL> class SystemCronFlowTest ( test_lib . FlowTestsBaseclass ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( SystemCronFlowTest , self ) . setUp ( ) <EOL> fixture = test_lib . FilterFixture ( regex = "<STR_LIT>" ) <EOL> for i in range ( <NUM_LIT:0> , <NUM_LIT:10> ) : <EOL> test_lib . ClientFixture ( "<STR_LIT>" % i , token = self . token , fixture = fixture ) <EOL> with aff4 . FACTORY . Open ( <EOL> "<STR_LIT>" % i , mode = "<STR_LIT>" , token = self . token ) as client : <EOL> client . AddLabels ( "<STR_LIT>" , "<STR_LIT>" , owner = "<STR_LIT>" ) <EOL> client . AddLabels ( "<STR_LIT>" , owner = "<STR_LIT>" ) <EOL> for i in range ( <NUM_LIT:0> , <NUM_LIT:10> ) : <EOL> test_lib . ClientFixture ( "<STR_LIT>" % i , token = self . token , <EOL> fixture = client_fixture . LINUX_FIXTURE ) <EOL> def _CheckVersionStats ( self , label , attribute , counts ) : <EOL> fd = aff4 . FACTORY . Open ( "<STR_LIT>" % label , <EOL> token = self . token ) <EOL> histogram = fd . Get ( attribute ) <EOL> self . assertEqual ( histogram [ <NUM_LIT:0> ] . title , "<STR_LIT>" % label ) <EOL> self . assertEqual ( len ( histogram [ <NUM_LIT:0> ] ) , counts [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( histogram [ <NUM_LIT:1> ] . title , "<STR_LIT>" % label ) <EOL> self . assertEqual ( len ( histogram [ <NUM_LIT:1> ] ) , counts [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( histogram [ <NUM_LIT:2> ] . title , "<STR_LIT>" % label ) <EOL> self . assertEqual ( histogram [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] . label , "<STR_LIT>" ) <EOL> self . assertEqual ( histogram [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] . y_value , counts [ <NUM_LIT:2> ] ) <EOL> self . assertEqual ( histogram [ <NUM_LIT:3> ] . title , "<STR_LIT>" % label ) <EOL> self . assertEqual ( histogram [ <NUM_LIT:3> ] [ <NUM_LIT:0> ] . label , "<STR_LIT>" ) <EOL> self . assertEqual ( histogram [ <NUM_LIT:3> ] [ <NUM_LIT:0> ] . y_value , counts [ <NUM_LIT:3> ] ) <EOL> def testGRRVersionBreakDown ( self ) : <EOL> """<STR_LIT>""" <EOL> for _ in test_lib . TestFlowHelper ( "<STR_LIT>" , token = self . token ) : <EOL> pass <EOL> histogram = aff4 . ClientFleetStats . SchemaCls . GRRVERSION_HISTOGRAM <EOL> self . _CheckVersionStats ( "<STR_LIT>" , histogram , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:20> , <NUM_LIT:20> ] ) <EOL> self . _CheckVersionStats ( "<STR_LIT>" , histogram , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:10> , <NUM_LIT:10> ] ) <EOL> self . _CheckVersionStats ( "<STR_LIT>" , histogram , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:10> , <NUM_LIT:10> ] ) <EOL> aff4 . FACTORY . Open ( "<STR_LIT>" , "<STR_LIT>" , <EOL> token = self . token ) <EOL> def _CheckOSStats ( self , label , attribute , counts ) : <EOL> fd = aff4 . FACTORY . Open ( "<STR_LIT>" % label , <EOL> token = self . token ) <EOL> histogram = fd . Get ( attribute ) <EOL> self . assertEqual ( histogram [ <NUM_LIT:0> ] . title , "<STR_LIT>" % label ) <EOL> self . assertEqual ( len ( histogram [ <NUM_LIT:0> ] ) , counts [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( histogram [ <NUM_LIT:1> ] . title , "<STR_LIT>" % label ) <EOL> self . assertEqual ( len ( histogram [ <NUM_LIT:1> ] ) , counts [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( histogram [ <NUM_LIT:2> ] . title , "<STR_LIT>" % label ) <EOL> all_labels = [ ] <EOL> for item in histogram [ <NUM_LIT:2> ] : <EOL> all_labels . append ( item . label ) <EOL> self . assertEqual ( item . y_value , counts [ <NUM_LIT:2> ] [ item . label ] ) <EOL> self . assertItemsEqual ( all_labels , counts [ <NUM_LIT:2> ] . keys ( ) ) <EOL> self . assertEqual ( histogram [ <NUM_LIT:3> ] . title , "<STR_LIT>" % label ) <EOL> all_labels = [ ] <EOL> for item in histogram [ <NUM_LIT:3> ] : <EOL> all_labels . append ( item . label ) <EOL> self . assertEqual ( item . y_value , counts [ <NUM_LIT:3> ] [ item . label ] ) <EOL> self . assertItemsEqual ( all_labels , counts [ <NUM_LIT:3> ] . keys ( ) ) <EOL> def testOSBreakdown ( self ) : <EOL> """<STR_LIT>""" <EOL> for _ in test_lib . TestFlowHelper ( "<STR_LIT>" , token = self . token ) : <EOL> pass <EOL> histogram = aff4 . ClientFleetStats . SchemaCls . OS_HISTOGRAM <EOL> self . _CheckOSStats ( "<STR_LIT>" , histogram , [ <NUM_LIT:0> , <NUM_LIT:0> , { "<STR_LIT>" : <NUM_LIT:10> , "<STR_LIT>" : <NUM_LIT:10> } , <EOL> { "<STR_LIT>" : <NUM_LIT:10> , "<STR_LIT>" : <NUM_LIT:10> } ] ) <EOL> self . _CheckOSStats ( "<STR_LIT>" , histogram , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , { "<STR_LIT>" : <NUM_LIT:10> } , { "<STR_LIT>" : <NUM_LIT:10> } ] ) <EOL> self . _CheckOSStats ( "<STR_LIT>" , histogram , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , { "<STR_LIT>" : <NUM_LIT:10> } , { "<STR_LIT>" : <NUM_LIT:10> } ] ) <EOL> def _CheckAccessStats ( self , label , count ) : <EOL> fd = aff4 . FACTORY . Open ( "<STR_LIT>" % label , <EOL> token = self . token ) <EOL> histogram = fd . Get ( fd . Schema . LAST_CONTACTED_HISTOGRAM ) <EOL> data = [ ( x . x_value , x . y_value ) for x in histogram ] <EOL> self . assertEqual ( data , [ <EOL> ( <NUM_LIT> L , <NUM_LIT:0> L ) , <EOL> ( <NUM_LIT> L , <NUM_LIT:0> L ) , <EOL> ( <NUM_LIT> L , <NUM_LIT:0> L ) , <EOL> ( <NUM_LIT> L , <NUM_LIT:0> L ) , <EOL> ( <NUM_LIT> L , count ) , <EOL> ( <NUM_LIT> L , count ) , <EOL> ( <NUM_LIT> L , count ) ] ) <EOL> def testLastAccessStats ( self ) : <EOL> """<STR_LIT>""" <EOL> for _ in test_lib . TestFlowHelper ( "<STR_LIT>" , token = self . token ) : <EOL> pass <EOL> self . _CheckAccessStats ( "<STR_LIT>" , count = <NUM_LIT:20> L ) <EOL> self . _CheckAccessStats ( "<STR_LIT>" , count = <NUM_LIT:10> L ) <EOL> self . _CheckAccessStats ( "<STR_LIT>" , count = <NUM_LIT:10> L ) <EOL> def testPurgeClientStats ( self ) : <EOL> max_age = system . PurgeClientStats . MAX_AGE <EOL> for t in [ <NUM_LIT:1> * max_age , <NUM_LIT> * max_age , <NUM_LIT:2> * max_age ] : <EOL> with test_lib . FakeTime ( t ) : <EOL> urn = self . client_id . Add ( "<STR_LIT>" ) <EOL> stats_fd = aff4 . FACTORY . Create ( urn , "<STR_LIT>" , token = self . token , <EOL> mode = "<STR_LIT>" ) <EOL> st = client_rdf . ClientStats ( RSS_size = int ( t ) ) <EOL> stats_fd . AddAttribute ( stats_fd . Schema . STATS ( st ) ) <EOL> stats_fd . Close ( ) <EOL> stat_obj = aff4 . FACTORY . Open ( <EOL> urn , age = aff4 . ALL_TIMES , token = self . token , ignore_cache = True ) <EOL> stat_entries = list ( stat_obj . GetValuesForAttribute ( stat_obj . Schema . STATS ) ) <EOL> self . assertEqual ( len ( stat_entries ) , <NUM_LIT:3> ) <EOL> self . assertTrue ( max_age in [ e . RSS_size for e in stat_entries ] ) <EOL> with test_lib . FakeTime ( <NUM_LIT> * max_age ) : <EOL> for _ in test_lib . TestFlowHelper ( <EOL> "<STR_LIT>" , None , client_id = self . client_id , token = self . token ) : <EOL> pass <EOL> stat_obj = aff4 . FACTORY . Open ( <EOL> urn , age = aff4 . ALL_TIMES , token = self . token , ignore_cache = True ) <EOL> stat_entries = list ( stat_obj . GetValuesForAttribute ( stat_obj . Schema . STATS ) ) <EOL> self . assertEqual ( len ( stat_entries ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( max_age not in [ e . RSS_size for e in stat_entries ] ) <EOL> def _SetSummaries ( self , client_id ) : <EOL> client = aff4 . FACTORY . Create ( client_id , "<STR_LIT>" , mode = "<STR_LIT>" , <EOL> token = self . token ) <EOL> client . Set ( client . Schema . HOSTNAME ( client_id ) ) <EOL> client . Set ( client . Schema . SYSTEM ( "<STR_LIT>" ) ) <EOL> client . Set ( client . Schema . OS_RELEASE ( "<STR_LIT>" ) ) <EOL> client . Set ( client . Schema . OS_VERSION ( "<STR_LIT>" ) ) <EOL> client . Set ( client . Schema . KERNEL ( "<STR_LIT>" ) ) <EOL> client . Set ( client . Schema . FQDN ( "<STR_LIT>" % client_id ) ) <EOL> client . Set ( client . Schema . ARCH ( "<STR_LIT>" ) ) <EOL> client . Flush ( ) <EOL> def testEndToEndTests ( self ) : <EOL> self . client_ids = [ "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> for clientid in self . client_ids : <EOL> self . _SetSummaries ( clientid ) <EOL> self . client_mock = action_mocks . ActionMock ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> with test_lib . ConfigOverrider ( { <EOL> "<STR_LIT>" : self . client_ids } ) : <EOL> with utils . MultiStubber ( ( base . AutomatedTest , "<STR_LIT>" , <EOL> { "<STR_LIT>" : <EOL> endtoend_test . MockEndToEndTest } ) , <EOL> ( system . EndToEndTests , "<STR_LIT>" , <NUM_LIT:0> ) ) : <EOL> for _ in test_lib . TestFlowHelper ( "<STR_LIT>" , self . client_mock , <EOL> client_id = self . client_id , <EOL> check_flow_errors = False , <EOL> token = self . token ) : <EOL> pass <EOL> test_lib . TestHuntHelperWithMultipleMocks ( { } , check_flow_errors = False , <EOL> token = self . token ) <EOL> hunt_ids = list ( aff4 . FACTORY . Open ( "<STR_LIT>" , <EOL> token = self . token ) . ListChildren ( ) ) <EOL> self . assertEqual ( len ( hunt_ids ) , <NUM_LIT:1> ) <EOL> hunt_obj = aff4 . FACTORY . Open ( hunt_ids [ <NUM_LIT:0> ] , token = self . token , <EOL> age = aff4 . ALL_TIMES ) <EOL> self . assertItemsEqual ( sorted ( hunt_obj . GetClients ( ) ) , <EOL> sorted ( self . client_ids ) ) <EOL> def _CreateResult ( self , success , clientid ) : <EOL> success = endtoend_flows . EndToEndTestResult ( success = success ) <EOL> return flows . GrrMessage ( source = clientid , payload = success ) <EOL> def testEndToEndTestsResultChecking ( self ) : <EOL> self . client_ids = [ "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> for clientid in self . client_ids : <EOL> self . _SetSummaries ( clientid ) <EOL> self . client_mock = action_mocks . ActionMock ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> endtoend = system . EndToEndTests ( None , token = self . token ) <EOL> endtoend . state . Register ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> endtoend . state . Register ( "<STR_LIT>" , set ( self . client_ids ) ) <EOL> endtoend . state . Register ( "<STR_LIT>" , set ( ) ) <EOL> endtoend . state . Register ( "<STR_LIT>" , set ( ) ) <EOL> self . assertRaises ( flow . FlowError , endtoend . _CheckForSuccess , [ ] ) <EOL> endtoend . state . Register ( "<STR_LIT>" , set ( ) ) <EOL> endtoend . state . Register ( "<STR_LIT>" , set ( ) ) <EOL> self . assertRaises ( flow . FlowError , <EOL> endtoend . _CheckForSuccess , <EOL> [ self . _CreateResult ( True , "<STR_LIT>" ) ] ) <EOL> endtoend . state . Register ( "<STR_LIT>" , set ( ) ) <EOL> endtoend . state . Register ( "<STR_LIT>" , set ( ) ) <EOL> endtoend . _CheckForSuccess ( <EOL> [ self . _CreateResult ( True , "<STR_LIT>" ) , <EOL> self . _CreateResult ( True , "<STR_LIT>" ) , <EOL> self . _CreateResult ( True , "<STR_LIT>" ) ] ) <EOL> endtoend . state . Register ( "<STR_LIT>" , set ( ) ) <EOL> endtoend . state . Register ( "<STR_LIT>" , set ( ) ) <EOL> self . assertRaises ( flow . FlowError , <EOL> endtoend . _CheckForSuccess , <EOL> [ self . _CreateResult ( True , "<STR_LIT>" ) , <EOL> self . _CreateResult ( False , "<STR_LIT>" ) , <EOL> self . _CreateResult ( False , "<STR_LIT>" ) ] ) <EOL> def main ( argv ) : <EOL> test_lib . GrrTestProgram ( argv = argv ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> flags . StartMain ( main ) </s>
