commit 8e52a8854892ee53585fc75f9d8fad2dc04bbe24 Author: Arturo Filastò art@fuffa.org Date: Wed Jan 16 21:07:49 2013 +0100
Remove now unused and dead code
We no longer need inputunit. nettesttask was never needed --- ooni/inputunit.py | 85 ------------------ ooni/nettesttask.py | 118 ------------------------- ooni/runner.py | 242 --------------------------------------------------- 3 files changed, 0 insertions(+), 445 deletions(-)
diff --git a/ooni/inputunit.py b/ooni/inputunit.py deleted file mode 100644 index fb85d9e..0000000 --- a/ooni/inputunit.py +++ /dev/null @@ -1,85 +0,0 @@ -#-*- coding: utf-8 -*- -# -# inputunit.py -# ------------- -# IN here we have functions related to the creation of input -# units. Input units are how the inputs to be fed to tests are -# split up into. -# -# :authors: Arturo Filastò -# :license: see included LICENSE file -from math import ceil - -class InputUnitFactory(object): - """ - This is a factory that takes the size of input units to be generated a set - of units that is a python iterable item and outputs InputUnit objects - containing inputUnitSize elements. - - This object is a python iterable, this means that it does not need to keep - all the elements in memory to be able to produce InputUnits. - """ - inputUnitSize = 10 - length = None - def __init__(self, inputs=[]): - """ - Args: - inputs (iterable): inputs *must* be an iterable. - """ - self._inputs = iter(inputs) - self.inputs = iter(inputs) - self._ended = False - - def __iter__(self): - return self - - def __len__(self): - """ - Returns the number of input units in the input unit factory. - """ - if not self.length: - self.length = ceil(float(sum(1 for _ in self._inputs))/self.inputUnitSize) - return self.length - - def next(self): - input_unit_elements = [] - - if self._ended: - raise StopIteration - - for i in xrange(self.inputUnitSize): - try: - input_unit_elements.append(self.inputs.next()) - except StopIteration: - self._ended = True - break - - if not input_unit_elements: - raise StopIteration - - return InputUnit(input_unit_elements) - -class InputUnit(object): - """ - This is a python iterable object that contains the input elements to be - passed onto a TestCase. - """ - def __init__(self, inputs=[]): - self._inputs = iter(inputs) - - def __str__(self): - return "<%s inputs=%s>" % (self.__class__, self._inputs) - - def __add__(self, inputs): - for i in inputs: - self._inputs.append(i) - - def __iter__(self): - return self - - def next(self): - return self._inputs.next() - - def append(self, input): - self._inputs.append(input) - diff --git a/ooni/nettesttask.py b/ooni/nettesttask.py deleted file mode 100644 index 48c5108..0000000 --- a/ooni/nettesttask.py +++ /dev/null @@ -1,118 +0,0 @@ -from twisted.internet.task import CooperativeTask -from twisted.internet import defer -from ooni.reporter import OONIBReporter, YAMLReporter, OONIBReportError -from ooni.utils import log -import time -from twisted.internet.task import cooperate - -class NetTestTask(CooperativeTask): - """ - The object produced by a NetTestTaskFactory. - - A NetTestTask wraps a test_ callable with its options and input unit. - - """ - def __init__(self, test_case, test_input, oonib_reporter=None, yaml_reporter=None): - test_class, test_method = test_case - #log.debug("Running %s with %s..." % (test_method, test_input)) - self.oonib_reporter = oonib_reporter - self.oonib_reporter = yaml_reporter - self.test_instance = test_class() - self.test_instance.input = test_input - self.test_instance.report = {} - self.test_instance._start_time = time.time() - self.test_instance._setUp() - self.test_instance.setUp() - self.test = getattr(self.test_instance, test_method) - - # XXX: override CoordinatedTask methods - def start(self): #??? - d = defer.maybeDeferred(self.test) - d.addCallback(self.test_done) - d.addErrback(self.test_error) - return d - - def write_report(self): - if not self.oonib_reporter: - return self.yaml_reporter.testDone(self.test_instance, str(self.test)) - d1 = self.oonib_reporter.testDone(self.test_instance, str(self.test)) - d2 = self.yaml_reporter.testDone(self.test_instance, str(self.test)) - dl = defer.DeferredList([d1, d2]) - @dl.addErrback - def reportingFailed(failure): - log.err("Error in reporting %s" % self.test) - log.exception(failure) - return dl - - def test_done(self, result): - log.msg("Finished running %s" % self.test) - log.debug("Deferred callback result: %s" % result) - return self.write_report() - - def test_error(self, failure): - log.err("Error in running %s" % self.test) - log.exception(failure) - return self.write_report() - - #XXX: does not implement tests_done! - -class NetTestTaskFactory(object): - def __init__(self, test_cases, input_unit_list): - self.input_unit_list = input_unit_list - self.inputs = self.generate_inputs() - self.test_cases = test_cases - - def __iter__(self): - return self - - def next(self): - return self.inputs.next() - # XXX: raise exception or fire callback when inputs are exhausted - - def generate_inputs(self): - for input_unit in self.input_unit_list: - for test_case in self.test_cases: - yield NetTestTask(test_case, input_unit) - -@defer.inlineCallbacks -def runTestCases(test_cases, options, cmd_line_options): - - log.debug("Running %s" % test_cases) - log.debug("Options %s" % options) - log.debug("cmd_line_options %s" % dict(cmd_line_options)) - - test_inputs = options['inputs'] - - oonib_reporter = OONIBReporter(cmd_line_options) - yaml_reporter = YAMLReporter(cmd_line_options) - - if cmd_line_options['collector']: - log.msg("Using remote collector, please be patient while we create the report.") - try: - yield oonib_reporter.createReport(options) - except OONIBReportError: - log.err("Error in creating new report") - log.msg("We will only create reports to a file") - oonib_reporter = None - else: - oonib_reporter = None - - yield yaml_reporter.createReport(options) - log.msg("Reporting to file %s" % yaml_reporter._stream.name) - - nettest_task_factory = NetTestTaskFactory(test_cases, test_inputs) - - #XXX: resume is not supported! - try: - #XXX: override the default cooperator, set up own scheduler - #XXX: add callback when tasks are all exhausted - for nettest_task in nettest_task_factory.generate_inputs(): - nettest_task.yaml_reporter = yaml_reporter - nettest_task.oonib_reporter = oonib_reporter - log.debug("Running %s with input unit %s" % (nettest_task, - nettest_task.test_instance.input)) - # feed the cooperator - nettest_task.start() - - except Exception: - log.exception("Problem in running test") diff --git a/ooni/runner.py b/ooni/runner.py index a602234..21c3e0e 100644 --- a/ooni/runner.py +++ b/ooni/runner.py @@ -27,248 +27,6 @@ from ooni.utils import log, checkForRoot, pushFilenameStack from ooni.utils import NotRootError, Storage from ooni.utils.net import randomFreePort
-def processTest(obj, cmd_line_options): - """ - Process the parameters and :class:`twisted.python.usage.Options` of a - :class:`ooni.nettest.Nettest`. - - :param obj: - An uninstantiated old test, which should be a subclass of - :class:`ooni.plugoo.tests.OONITest`. - - :param cmd_line_options: - A configured and instantiated :class:`twisted.python.usage.Options` - class. - - """ - if not hasattr(obj.usageOptions, 'optParameters'): - obj.usageOptions.optParameters = [] - - if obj.inputFile: - obj.usageOptions.optParameters.append(obj.inputFile) - - if obj.baseParameters: - for parameter in obj.baseParameters: - obj.usageOptions.optParameters.append(parameter) - - if obj.baseFlags: - if not hasattr(obj.usageOptions, 'optFlags'): - obj.usageOptions.optFlags = [] - for flag in obj.baseFlags: - obj.usageOptions.optFlags.append(flag) - - options = obj.usageOptions() - - options.parseOptions(cmd_line_options['subargs']) - obj.localOptions = options - - if obj.inputFile: - obj.inputFilename = options[obj.inputFile[0]] - - try: - log.debug("processing options") - tmp_test_case_object = obj() - tmp_test_case_object._checkRequiredOptions() - - except usage.UsageError, e: - test_name = tmp_test_case_object.name - log.err("There was an error in running %s!" % test_name) - log.err("%s" % e) - options.opt_help() - raise usage.UsageError("Error in parsing command line args for %s" % test_name) - - if obj.requiresRoot: - try: - checkForRoot() - except NotRootError: - log.err("%s requires root to run" % obj.name) - sys.exit(1) - - return obj - -def isTestCase(obj): - try: - return issubclass(obj, NetTestCase) - except TypeError: - return False - -def findTestClassesFromFile(cmd_line_options): - """ - Takes as input the command line config parameters and returns the test - case classes. - - :param filename: - the absolute path to the file containing the ooniprobe test classes - - :return: - A list of class objects found in a file or module given on the - commandline. - """ - filename = cmd_line_options['test'] - classes = [] - module = filenameToModule(filename) - for name, val in inspect.getmembers(module): - if isTestCase(val): - classes.append(processTest(val, cmd_line_options)) - return classes - -def makeTestCases(klass, tests, method_prefix): - """ - Takes a class some tests and returns the test cases. method_prefix is how - the test case functions should be prefixed with. - """ - cases = [] - for test in tests: - cases.append((klass, method_prefix+test)) - return cases - -class NoTestCasesFound(Exception): - pass - -def loadTestsAndOptions(classes, cmd_line_options): - """ - Takes a list of test classes and returns their testcases and options. - """ - method_prefix = 'test' - options = None - test_cases = [] - - for klass in classes: - tests = reflect.prefixedMethodNames(klass, method_prefix) - if tests: - test_cases = makeTestCases(klass, tests, method_prefix) - - test_klass = klass() - options = test_klass._processOptions() - - if not test_cases: - raise NoTestCasesFound - - return test_cases, options - -def runTestCasesWithInput(test_cases, test_input, yaml_reporter, - oonib_reporter=None): - """ - Runs in parallel all the test methods that are inside of the specified test case. - Reporting happens every time a Test Method has concluded running. - Once all the test methods have been called we check to see if the - postProcessing class method returns something. If it does return something - we will write this as another entry inside of the report called post_processing. - - Args: - - test_cases (list): A list of tuples containing the test_class (a - class) and the test_method (a string) - - test_input (instance): Any instance that will be passed as input to - the test. - - yaml_reporter: An instance of :class:ooni.reporter.YAMLReporter - - oonib_reporter: An instance of :class:ooni.reporter.OONIBReporter. If - this is set to none then we will only report to the YAML reporter. - - """ - - # This is used to store a copy of all the test reports - tests_report = {} - - def write_report(test_instance, test_name): - if not oonib_reporter: - return yaml_reporter.testDone(test_instance, test_name) - d1 = oonib_reporter.testDone(test_instance, test_name) - d2 = yaml_reporter.testDone(test_instance, test_name) - dl = defer.DeferredList([d1, d2]) - @dl.addErrback - def reportingFailed(failure): - log.err("Error in reporting %s" % test_name) - log.exception(failure) - return dl - - def test_done(result, test_instance, test_name): - log.msg("Finished running %s" % test_name) - log.debug("Deferred callback result: %s" % result) - tests_report[test_name] = dict(test_instance.report) - return write_report(test_instance, test_name) - - def test_error(failure, test_instance, test_name): - log.err("Error in running %s" % test_name) - log.exception(failure) - return write_report(test_instance, test_name) - - def tests_done(result, test_class): - test_instance = test_class() - test_instance.report = {} - test_instance.input = None - test_instance._start_time = time.time() - post = getattr(test_instance, 'postProcessor') - try: - post_processing = post(tests_report) - if not oonib_reporter: - return yaml_reporter.testDone(test_instance, 'summary') - d1 = oonib_reporter.testDone(test_instance, 'summary') - d2 = yaml_reporter.testDone(test_instance, 'summary') - return defer.DeferredList([d1, d2]) - except NoPostProcessor: - log.debug("No post processor configured") - return - - dl = [] - for test_case in test_cases: - log.debug("Processing %s" % test_case[1]) - test_class = test_case[0] - test_method = test_case[1] - - log.debug("Running %s with %s..." % (test_method, test_input)) - - test_instance = test_class() - test_instance.input = test_input - test_instance.report = {} - # use this to keep track of the test runtime - test_instance._start_time = time.time() - # call setups on the test - test_instance._setUp() - test_instance.setUp() - test = getattr(test_instance, test_method) - - d = defer.maybeDeferred(test) - d.addCallback(test_done, test_instance, test_method) - d.addErrback(test_error, test_instance, test_method) - dl.append(d) - - test_methods_d = defer.DeferredList(dl) - test_methods_d.addCallback(tests_done, test_cases[0][0]) - @test_methods_d.addErrback - def deferredListFailed(failure): - log.err("Error Test Method Deferred List") - log.exception(failure) - - return test_methods_d - -def runTestCasesWithInputUnit(test_cases, input_unit, yaml_reporter, - oonib_reporter): - """ - Runs the Test Cases that are given as input parallely. - A Test Case is a subclass of ooni.nettest.NetTestCase and a list of - methods. - - The deferred list will fire once all the test methods have been - run once per item in the input unit. - - test_cases: A list of tuples containing the test class and the test method as a string. - - input_unit: A generator that yields an input per iteration - - """ - log.debug("Running test cases with input unit") - dl = [] - for test_input in input_unit: - log.debug("Running test with this input %s" % test_input) - d = runTestCasesWithInput(test_cases, - test_input, yaml_reporter, oonib_reporter) - dl.append(d) - return defer.DeferredList(dl) - class InvalidResumeFile(Exception): pass
tor-commits@lists.torproject.org