[tor-commits] [ooni-probe/master] Make the deck code easier to unittest.

art at torproject.org art at torproject.org
Fri Feb 28 14:32:20 UTC 2014


commit ea26c9544457a79076292c6810de44c48e83983e
Author: Arturo Filastò <art at fuffa.org>
Date:   Thu Jan 30 17:14:27 2014 +0100

    Make the deck code easier to unittest.
    
    Move the importing of input and deck inside to avoid circular dependencies
---
 ooni/deck.py        |   21 +++++++++++----------
 ooni/oonibclient.py |    5 ++++-
 2 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/ooni/deck.py b/ooni/deck.py
index ebf2a18..8212c3d 100644
--- a/ooni/deck.py
+++ b/ooni/deck.py
@@ -1,5 +1,6 @@
 #-*- coding: utf-8 -*-
 
+from ooni.oonibclient import OONIBClient
 from ooni.nettest import NetTestLoader
 from ooni.settings import config
 from ooni.utils import log
@@ -85,15 +86,16 @@ class Deck(InputFile):
                  deckFile=None,
                  decks_directory=config.decks_directory):
         self.id = deck_hash
-        self.bouncer = None
+        self.bouncer = ''
         self.netTestLoaders = []
         self.inputs = []
         self.testHelpers = {}
 
-        self.decksDirectory = decks_directory
+        self.oonibclient = OONIBClient(self.bouncer)
 
+        self.decksDirectory = decks_directory
         self.deckHash = deck_hash
- 
+
         if deckFile: self.loadDeck(deckFile)
 
     @property
@@ -106,7 +108,7 @@ class Deck(InputFile):
 
     def loadDeck(self, deckFile):
         with open(deckFile) as f:
-            self.deckHash = sha256(f.read())
+            self.deckHash = sha256(f.read()).hexdigest()
             f.seek(0)
             test_deck = yaml.safe_load(f)
 
@@ -151,8 +153,8 @@ class Deck(InputFile):
     
     @defer.inlineCallbacks
     def lookupTestHelpers(self):
-        from ooni.oonibclient import OONIBClient
-        oonibclient = OONIBClient(self.bouncer)
+        self.oonibclient.address = self.bouncer
+
         required_test_helpers = []
         requires_collector = []
         for net_test_loader in self.netTestLoaders:
@@ -168,7 +170,7 @@ class Deck(InputFile):
         if not required_test_helpers and not requires_collector:
             defer.returnValue(None)
 
-        response = yield oonibclient.lookupTestHelpers(required_test_helpers)
+        response = yield self.oonibclient.lookupTestHelpers(required_test_helpers)
 
         for net_test_loader in self.netTestLoaders:
             log.msg("Setting collector and test helpers for %s" % net_test_loader.testDetails['test_name'])
@@ -193,15 +195,14 @@ class Deck(InputFile):
     @defer.inlineCallbacks
     def fetchAndVerifyNetTestInput(self, net_test_loader):
         """ fetch and verify a single NetTest's inputs """
-        from ooni.oonibclient import OONIBClient
         log.debug("Fetching and verifying inputs")
         for i in net_test_loader.inputFiles:
             if 'url' in i:
                 log.debug("Downloading %s" % i['url'])
-                oonibclient = OONIBClient(i['address'])
+                self.oonibclient.address = i['address']
                 
                 try:
-                    input_file = yield oonibclient.downloadInput(i['hash'])
+                    input_file = yield self.oonibclient.downloadInput(i['hash'])
                 except:
                     raise e.UnableToLoadDeckInput
 
diff --git a/ooni/oonibclient.py b/ooni/oonibclient.py
index 1fa2e5b..af4328b 100644
--- a/ooni/oonibclient.py
+++ b/ooni/oonibclient.py
@@ -6,7 +6,6 @@ from hashlib import sha256
 from twisted.internet import defer, reactor
 from twisted.internet.endpoints import TCP4ClientEndpoint
 
-from ooni.deck import Deck, InputFile
 from ooni import errors as e
 from ooni.settings import config
 from ooni.utils import log
@@ -117,6 +116,7 @@ class OONIBClient(object):
         pass
 
     def getInput(self, input_hash):
+        from ooni.deck import InputFile
         input_file = InputFile(input_hash)
         if input_file.descriptorCached:
             return defer.succeed(input_file)
@@ -140,6 +140,7 @@ class OONIBClient(object):
         return self.queryBackend('GET', '/input')
 
     def downloadInput(self, input_hash):
+        from ooni.deck import InputFile
         input_file = InputFile(input_hash)
 
         if input_file.fileCached:
@@ -169,6 +170,7 @@ class OONIBClient(object):
         return self.queryBackend('GET', '/deck')
 
     def getDeck(self, deck_hash):
+        from ooni.deck import Deck
         deck = Deck(deck_hash)
         if deck.descriptorCached:
             return defer.succeed(deck)
@@ -190,6 +192,7 @@ class OONIBClient(object):
             return d
 
     def downloadDeck(self, deck_hash):
+        from ooni.deck import Deck
         deck = Deck(deck_hash)
         if deck.fileCached:
             return defer.succeed(deck)





More information about the tor-commits mailing list