<s> from twisted . python import failure <EOL> from twisted . internet import defer <EOL> from ooni . tasks import BaseTask , TaskWithTimeout <EOL> from ooni . managers import TaskManager <EOL> class MockMeasurementFailOnce ( BaseTask ) : <EOL> def run ( self ) : <EOL> f = open ( '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . close ( ) <EOL> if self . failure >= <NUM_LIT:1> : <EOL> return defer . succeed ( self ) <EOL> else : <EOL> return defer . fail ( failure . Failure ) <EOL> class MockMeasurementManager ( TaskManager ) : <EOL> def __init__ ( self ) : <EOL> self . successes = [ ] <EOL> TaskManager . __init__ ( self ) <EOL> def failed ( self , failure , task ) : <EOL> pass <EOL> def succeeded ( self , result , task ) : <EOL> self . successes . append ( ( result , task ) ) <EOL> class MockReporter ( object ) : <EOL> def __init__ ( self ) : <EOL> self . created = defer . Deferred ( ) <EOL> def writeReportEntry ( self , entry ) : <EOL> pass <EOL> def createReport ( self ) : <EOL> self . created . callback ( self ) <EOL> def finish ( self ) : <EOL> pass <EOL> class MockFailure ( Exception ) : <EOL> pass <EOL> mockFailure = failure . Failure ( MockFailure ( '<STR_LIT>' ) ) <EOL> class MockSuccessTask ( BaseTask ) : <EOL> def run ( self ) : <EOL> return defer . succeed ( <NUM_LIT> ) <EOL> class MockFailTask ( BaseTask ) : <EOL> def run ( self ) : <EOL> return defer . fail ( mockFailure ) <EOL> class MockFailOnceTask ( BaseTask ) : <EOL> def run ( self ) : <EOL> if self . failures >= <NUM_LIT:1> : <EOL> return defer . succeed ( <NUM_LIT> ) <EOL> else : <EOL> return defer . fail ( mockFailure ) <EOL> class MockSuccessTaskWithTimeout ( TaskWithTimeout ) : <EOL> def run ( self ) : <EOL> return defer . succeed ( <NUM_LIT> ) <EOL> class MockFailTaskThatTimesOut ( TaskWithTimeout ) : <EOL> def run ( self ) : <EOL> return defer . Deferred ( ) <EOL> class MockTimeoutOnceTask ( TaskWithTimeout ) : <EOL> def run ( self ) : <EOL> if self . failures >= <NUM_LIT:1> : <EOL> return defer . succeed ( <NUM_LIT> ) <EOL> else : <EOL> return defer . Deferred ( ) <EOL> class MockFailTaskWithTimeout ( TaskWithTimeout ) : <EOL> def run ( self ) : <EOL> return defer . fail ( mockFailure ) <EOL> class MockNetTest ( object ) : <EOL> def __init__ ( self ) : <EOL> self . successes = [ ] <EOL> def succeeded ( self , measurement ) : <EOL> self . successes . append ( measurement ) <EOL> class MockMeasurement ( TaskWithTimeout ) : <EOL> def __init__ ( self , net_test ) : <EOL> TaskWithTimeout . __init__ ( self ) <EOL> self . netTest = net_test <EOL> def succeeded ( self , result ) : <EOL> return self . netTest . succeeded ( <NUM_LIT> ) <EOL> class MockSuccessMeasurement ( MockMeasurement ) : <EOL> def run ( self ) : <EOL> return defer . succeed ( <NUM_LIT> ) <EOL> class MockFailMeasurement ( MockMeasurement ) : <EOL> def run ( self ) : <EOL> return defer . fail ( mockFailure ) <EOL> class MockFailOnceMeasurement ( MockMeasurement ) : <EOL> def run ( self ) : <EOL> if self . failures >= <NUM_LIT:1> : <EOL> return defer . succeed ( <NUM_LIT> ) <EOL> else : <EOL> return defer . fail ( mockFailure ) <EOL> class MockDirector ( object ) : <EOL> def __init__ ( self ) : <EOL> self . successes = [ ] <EOL> def measurementFailed ( self , failure , measurement ) : <EOL> pass <EOL> def measurementSucceeded ( self , measurement ) : <EOL> self . successes . append ( measurement ) <EOL> class MockOReporter ( object ) : <EOL> def __init__ ( self ) : <EOL> self . created = defer . Deferred ( ) <EOL> def writeReportEntry ( self , entry ) : <EOL> return defer . succeed ( <NUM_LIT> ) <EOL> def finish ( self ) : <EOL> pass <EOL> def createReport ( self ) : <EOL> from ooni . utils import log <EOL> log . debug ( "<STR_LIT>" % self ) <EOL> self . created . callback ( self ) <EOL> class MockOReporterThatFailsWrite ( MockOReporter ) : <EOL> def writeReportEntry ( self , entry ) : <EOL> raise MockFailure <EOL> class MockOReporterThatFailsOpen ( MockOReporter ) : <EOL> def createReport ( self ) : <EOL> raise MockFailure <EOL> class MockOReporterThatFailsWriteOnce ( MockOReporter ) : <EOL> def __init__ ( self ) : <EOL> self . failure = <NUM_LIT:0> <EOL> MockOReporter . __init__ ( self ) <EOL> def writeReportEntry ( self , entry ) : <EOL> if self . failure >= <NUM_LIT:1> : <EOL> return defer . succeed ( <NUM_LIT> ) <EOL> else : <EOL> self . failure += <NUM_LIT:1> <EOL> raise MockFailure <EOL> class MockTaskManager ( TaskManager ) : <EOL> def __init__ ( self ) : <EOL> self . successes = [ ] <EOL> TaskManager . __init__ ( self ) <EOL> def failed ( self , failure , task ) : <EOL> pass <EOL> def succeeded ( self , result , task ) : <EOL> self . successes . append ( ( result , task ) ) <EOL> class MockOONIBClient ( object ) : <EOL> def lookupTestHelpers ( self , required_test_helpers ) : <EOL> ret = { <EOL> '<STR_LIT:default>' : { <EOL> '<STR_LIT:address>' : '<STR_LIT:127.0.0.1>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> } <EOL> for required_test_helper in required_test_helpers : <EOL> ret [ required_test_helper ] = { <EOL> '<STR_LIT:address>' : '<STR_LIT:127.0.0.1>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> return defer . succeed ( ret ) </s>
