commit 25d94151864bea2406469103491ec77b2279fcea Author: aagbsn aagbsn@extc.org Date: Sun Aug 25 19:14:17 2013 +0200
Simplify the bouncer API
Simplified bouncer API requires one helper lookup per NetTest. --- ooni/deck.py | 52 ++++++++++++++++++++++----------------------------- ooni/oonibclient.py | 19 +++++++++++++++---- 2 files changed, 37 insertions(+), 34 deletions(-)
diff --git a/ooni/deck.py b/ooni/deck.py index 1ad8377..81b2afa 100644 --- a/ooni/deck.py +++ b/ooni/deck.py @@ -19,8 +19,6 @@ class Deck(object): self.netTestLoaders = [] self.inputs = [] self.testHelpers = {} - self.collector = None - if deckFile: self.loadDeck(deckFile)
def loadDeck(self, deckFile): @@ -28,6 +26,9 @@ class Deck(object): for test in test_deck: net_test_loader = NetTestLoader(test['options']['subargs'], test_file=test['options']['test_file']) + #XXX: If the deck specifies the collector, we use the specified collector + # And it should also specify the test helper address to use + # net_test_loader.collector = test['options']['collector'] net_test_loader.checkOptions() self.netTestLoaders.append(net_test_loader)
@@ -36,18 +37,30 @@ class Deck(object): net_test_loader.checkOptions() self.netTestLoaders.append(net_test_loader)
- def getRequiredTestHelpers(self): - for net_test_loader in self.netTestLoaders: - for test_helper in net_test_loader.requiredTestHelpers: - self.testHelpers[test_helper['name']] = None - @defer.inlineCallbacks def setup(self): """ fetch and verify inputs for all NetTests in the deck """ for net_test_loader in self.netTestLoaders: yield self.fetchAndVerifyNetTestInput(net_test_loader) - self.getRequiredTestHelpers() - yield self.lookupTestHelpers() + yield self.lookupTestHelper(net_test_loader) + yield self.lookupTestCollector(net_test_loader) + + @defer.inlineCallbacks + def lookupTestHelper(self, net_test_loader): + oonibclient = OONIBClient(self.bouncer) + for th in net_test_loader.requiredTestHelpers: + # {'name':'', 'option':'', 'test_class':''} + helper = yield oonibclient.lookupTestHelper(th['name']) + th['test_class'].localOptions[th['option']] = helper['test-helper'] + #XXX: collector is only set once! + net_test_loader.collector = helper['collector'] + + + @defer.inlineCallbacks + def lookupTestCollector(self, net_test_loader): + oonibclient = OONIBClient(self.bouncer) + if net_test_loader.collector is None: + net_test_loader.collector = oonibclient.lookupTestCollector(th['test_class'].testName)
@defer.inlineCallbacks def fetchAndVerifyNetTestInput(self, net_test_loader): @@ -69,24 +82,3 @@ class Deck(object): raise e.UnableToLoadDeckInput, cached_path
i['test_class'].localOptions[i['key']] = input_file.cached_file - - def setNettestOptions(self): - for net_test_loader in self.netTestLoaders: - for th in net_test_loader.requiredTestHelpers: - test_helper_address = self.testHelpers[th['name']] - th['test_class'].localOptions[th['option']] = test_helper_address - net_test_loader.collector = self.collector - log.debug("Using %s: %s" % (test_helper_address, self.collector)) - - @defer.inlineCallbacks - def lookupTestHelpers(self): - log.msg("Looking up test helpers: %s" % self.testHelpers.keys()) - - required_test_helpers = self.testHelpers.keys() - if required_test_helpers: - oonibclient = OONIBClient(self.bouncer) - test_helpers = yield oonibclient.lookupTestHelpers(required_test_helpers) - self.collector = test_helpers['collector'] - for name in self.testHelpers.keys(): - self.testHelpers[name] = test_helpers[name] - self.setNettestOptions() diff --git a/ooni/oonibclient.py b/ooni/oonibclient.py index 9bdce95..227c775 100644 --- a/ooni/oonibclient.py +++ b/ooni/oonibclient.py @@ -187,15 +187,26 @@ class OONIBClient(object): return self.queryBackend('GET', '/policy/nettest')
@defer.inlineCallbacks - def lookupTestHelpers(self, test_helper_names): + def lookupTestCollector(self, test_name): try: - test_helpers = yield self.queryBackend('POST', '/bouncer', - query={'test-helpers': test_helper_names}) + test_collector = yield self.queryBackend('POST', '/bouncer', + query={'test-collector': test_name}) + except Exception: + raise e.CouldNotFindTestCollector + + defer.returnValue(test_collector) + + @defer.inlineCallbacks + def lookupTestHelper(self, test_helper_name): + try: + + test_helper = yield self.queryBackend('POST', '/bouncer', + query={'test-helper': test_helper_name}) except Exception: raise e.CouldNotFindTestHelper
if not test_helpers: raise e.CouldNotFindTestHelper
- defer.returnValue(test_helpers) + defer.returnValue(test_helper)