commit 5cbd79a53c6e9d2612b270677ed7d3f5c9401225 Author: Arturo Filastò art@fuffa.org Date: Tue Nov 27 11:54:55 2012 +0100
Parse the test specific command line options when running a test or tests * Read the SOCKS port and Control port via the control port when starting with Tor --- ooni/oonicli.py | 57 ++++++++++++++++++++++++++++++++---------------------- ooni/runner.py | 20 ++++++++++++++---- 2 files changed, 49 insertions(+), 28 deletions(-)
diff --git a/ooni/oonicli.py b/ooni/oonicli.py index 26cfdab..f706ee3 100644 --- a/ooni/oonicli.py +++ b/ooni/oonicli.py @@ -111,26 +111,18 @@ def startSniffing(): sniffer = ScapySniffer(config.reports.pcap) config.scapyFactory.registerProtocol(sniffer)
+def runTestList(none, test_list): + """ + none: is always None.
-def runTestDeckOrTest(result, cmd_line_options): + test_list (list): a list of tuples containing (test_cases, options, + cmd_line_options) + """ deck_dl = [] - resume = cmd_line_options['resume']
- if cmd_line_options['testdeck']: - test_deck = yaml.safe_load(open(cmd_line_options['testdeck'])) - for test in test_deck: - del cmd_line_options - cmd_line_options = test['options'] - if resume: - cmd_line_options['resume'] = True - else: - cmd_line_options['resume'] = False - d1 = runner.runTest(cmd_line_options) - deck_dl.append(d1) - else: - log.msg("No test deck detected") - del cmd_line_options['testdeck'] - d1 = runner.runTest(cmd_line_options) + for test in test_list: + test_cases, options, cmd_line_options = test + d1 = runner.runTestCases(test_cases, options, cmd_line_options) deck_dl.append(d1)
d2 = defer.DeferredList(deck_dl) @@ -146,9 +138,8 @@ def errorRunningTests(failure):
def run(): """ - Call me to begin testing from a file. + Parses command line arguments of test. """ - cmd_line_options = Options() if len(sys.argv) == 1: cmd_line_options.getUsage() @@ -163,15 +154,35 @@ def run(): log.msg("Starting") runner.startSniffing()
+ resume = cmd_line_options['resume'] + + # contains (test_cases, options, cmd_line_options) + test_list = [] + + if cmd_line_options['testdeck']: + test_deck = yaml.safe_load(open(cmd_line_options['testdeck'])) + for test in test_deck: + del cmd_line_options + cmd_line_options = test['options'] + if resume: + cmd_line_options['resume'] = True + else: + cmd_line_options['resume'] = False + test_list.append(runner.loadTest(cmd_line_options)) + else: + log.msg("No test deck detected") + del cmd_line_options['testdeck'] + test_list.append(runner.loadTest(cmd_line_options)) + if config.advanced.start_tor: log.msg("Starting Tor...") d = runner.startTor() - d.addCallback(runTestDeckOrTest, cmd_line_options) + d.addCallback(runTestList, test_list) d.addErrback(errorRunningTests) else: - # We need to pass None as first argument because when the callback - # is fired it will pass it's result to runTestCase. - d = runTestDeckOrTest(None, cmd_line_options) + # We need to pass None as first argument because when the callback is + # fired it will pass it's result to runTestCase. + d = runTestList(None, test_list) d.addErrback(errorRunningTests)
reactor.run() diff --git a/ooni/runner.py b/ooni/runner.py index 30682e9..d5cc5fb 100644 --- a/ooni/runner.py +++ b/ooni/runner.py @@ -44,9 +44,11 @@ def processTest(obj): :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 = [] @@ -440,8 +442,12 @@ def startTor(): log.debug("We now have the following circuits: ") for circuit in state.circuits.values(): log.debug(" * %s" % circuit) - config.tor.socks_port = yield state.protocol.get_conf("SocksPort") - config.tor.control_port = yield state.protocol.get_conf("ControlPort") + + socks_port = yield state.protocol.get_conf("SocksPort") + control_port = yield state.protocol.get_conf("ControlPort") + + config.tor.socks_port = int(socks_port.values()[0]) + config.tor.control_port = int(control_port.values()[0])
def setup_failed(failure): log.exception(failure) @@ -458,7 +464,7 @@ def startTor(): return state.post_bootstrap
def updates(prog, tag, summary): - print "%d%%: %s" % (prog, summary) + log.msg("%d%%: %s" % (prog, summary))
tor_config = TorConfig() if config.tor.control_port: @@ -501,7 +507,11 @@ def startSniffing(): sniffer = ScapySniffer(config.reports.pcap) config.scapyFactory.registerProtocol(sniffer)
-def runTest(cmd_line_options): +def loadTest(cmd_line_options): + """ + Takes care of parsing test command line arguments and loading their + options. + """ config.cmd_line_options = cmd_line_options config.generateReportFilenames()
@@ -517,4 +527,4 @@ def runTest(cmd_line_options): classes = findTestClassesFromFile(cmd_line_options['test']) test_cases, options = loadTestsAndOptions(classes, cmd_line_options)
- return runTestCases(test_cases, options, cmd_line_options) + return test_cases, options, cmd_line_options