commit d476d8c9c25c432170a0542bb4c3b3e43ad081e7 Author: Isis Lovecruft isis@torproject.org Date: Wed Dec 12 18:29:03 2012 +0000
Moved method NetTestCase._checkRequiredOptions() logic into a function in ooni/runner.py, because it was unnecessary and also more lines of code to put in nettest.py.
* Also removed an extraneous call to _checkRequiredOptions. * Moved another exception to the top of ooni/runner.py. --- ooni/nettest.py | 14 +++----------- ooni/runner.py | 29 +++++++++++++++++++---------- 2 files changed, 22 insertions(+), 21 deletions(-)
diff --git a/ooni/nettest.py b/ooni/nettest.py index 8fc29c3..cff5c62 100644 --- a/ooni/nettest.py +++ b/ooni/nettest.py @@ -180,12 +180,6 @@ class NetTestCase(object): else: pass
- def _checkRequiredOptions(self): - for required_option in self.requiredOptions: - log.debug("Checking if %s is present" % required_option) - if not self.localOptions[required_option]: - raise usage.UsageError("%s not specified!" % required_option) - def _processOptions(self): if self.inputFilename: inputProcessor = self.inputProcessor @@ -199,11 +193,9 @@ class NetTestCase(object): return inputProcessor(inputFilename) self.inputs = inputProcessorIterator()
- self._checkRequiredOptions() - - return {'inputs': self.inputs, - 'name': self.name, 'version': self.version - } + return {'inputs': self.inputs, + 'name': self.name, + 'version': self.version}
def __repr__(self): return "<%s inputs=%s>" % (self.__class__, self.inputs) diff --git a/ooni/runner.py b/ooni/runner.py index 306c1d9..4724d3d 100644 --- a/ooni/runner.py +++ b/ooni/runner.py @@ -47,6 +47,9 @@ class noResumeSession(Exception): class InvalidConfigFile(Exception): message = "Invalid setting in ooniprobe.conf: "
+class UnableToStartTor(Exception): + pass +
def isTestCase(obj): """Return True if obj is a subclass of NetTestCase, False otherwise.""" @@ -55,6 +58,18 @@ def isTestCase(obj): except TypeError: return False
+def checkRequiredOptions(test_instance): + """ + If test_instance has an attribute 'requiredOptions', then check that + those options were utilised on the commandline. + """ + required = getattr(test_instance, 'requiredOptions', None) + if required: + for required_option in required: + log.debug("Checking if %s is present" % required_option) + if not test_instance.localOptions[required_option]: + raise usage.UsageError("%s not specified!" % required_option) + def processTest(obj, cmd_line_options): """ Process the parameters and :class:`twisted.python.usage.Options` of a @@ -91,7 +106,6 @@ def processTest(obj, cmd_line_options):
options = obj.usageOptions() options.parseOptions(config.cmd_line_options['subargs']) - obj.localOptions = options
if obj.inputFile: # inputFilename is the actual filename @@ -99,12 +113,11 @@ def processTest(obj, cmd_line_options):
try: log.debug("Parsing commandline options") - tmp_test_case_object = obj() - tmp_test_case_object._checkRequiredOptions() + tmp_test_instance = obj() + checkRequiredOptions(tmp_test_instance) except usage.UsageError, ue: log.err("%s" % ue) options.opt_help() - raise usage.UsageError("Error parsing command line args for %s" % tmp_test_case_object.name) else: @@ -162,7 +175,6 @@ def loadTestsAndOptions(classes, cmd_line_options):
def getTestTimeout(test_instance, test_method): """ - Returns the timeout value set on this test. Check on the instance first, the the class, then the module, then package. As soon as it finds something with a timeout attribute, returns that. Returns the value set in @@ -199,7 +211,7 @@ def getTestTimeout(test_instance, test_method): return float(default_timeout)
def runTestCasesWithInput(test_cases, test_input, yaml_reporter, - oonib_reporter=None): + 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. @@ -552,9 +564,6 @@ def runTestCases(test_cases, options, cmd_line_options): except Exception: log.exception("Problem in running test")
-class UnableToStartTor(Exception): - pass - def startTor(): @defer.inlineCallbacks def state_complete(state): @@ -617,7 +626,7 @@ def startSniffing(): from ooni.utils.txscapy import ScapyFactory, ScapySniffer try: checkForRoot() - except NotRootError: + except PermissionsError: print "[!] Includepcap options requires root priviledges to run" print " you should run ooniprobe as root or disable the options in ooniprobe.conf" sys.exit(1)