commit b615c85392ad4ee433a3681758eccd6c95a3ee7e Author: Arturo Filastò art@fuffa.org Date: Tue Apr 29 18:17:27 2014 +0200
When running unittests don't write to outside the build dir.
This fixes: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=743108 --- ooni/oonicli.py | 3 ++- ooni/settings.py | 18 ++++++++++++------ ooni/tests/__init__.py | 2 ++ ooni/tests/test_geoip.py | 11 ++++++++--- ooni/tests/test_nettest.py | 4 ++-- 5 files changed, 26 insertions(+), 12 deletions(-)
diff --git a/ooni/oonicli.py b/ooni/oonicli.py index 7693f34..846eadc 100644 --- a/ooni/oonicli.py +++ b/ooni/oonicli.py @@ -101,6 +101,7 @@ def runWithDirector(logging=True, start_tor=True): global_options = parseOptions() config.global_options = global_options config.set_paths() + config.initialize_ooni_home() config.read_config_file() if global_options['verbose']: config.advanced.debug = True @@ -175,7 +176,7 @@ def runWithDirector(logging=True, start_tor=True): sys.exit(5)
d = director.start(start_tor=start_tor) - + def setup_nettest(_): try: return deck.setup() diff --git a/ooni/settings.py b/ooni/settings.py index 841b782..cd4e04b 100644 --- a/ooni/settings.py +++ b/ooni/settings.py @@ -3,15 +3,14 @@ import sys import yaml import getpass
-from shutil import copyfile from os.path import abspath, expanduser
-from twisted.internet import reactor, threads, defer - from ooni import otime, geoip from ooni.utils import Storage
class OConfig(object): + _custom_home = None + def __init__(self): self.current_user = getpass.getuser() self.global_options = {} @@ -28,9 +27,11 @@ class OConfig(object): self.tor = Storage() self.privacy = Storage() self.set_paths() - self.initialize_ooni_home()
- def set_paths(self): + def set_paths(self, ooni_home=None): + if ooni_home: + self._custom_home = ooni_home + if self.global_options.get('datadir'): self.data_directory = abspath(expanduser(self.global_options['datadir'])) elif self.advanced.get('data_dir'): @@ -43,6 +44,8 @@ class OConfig(object): self.nettest_directory = abspath(os.path.join(__file__, '..', 'nettests'))
self.ooni_home = os.path.join(expanduser('~'+self.current_user), '.ooni') + if self._custom_home: + self.ooni_home = self._custom_home self.inputs_directory = os.path.join(self.ooni_home, 'inputs') self.decks_directory = os.path.join(self.ooni_home, 'decks') self.reports_directory = os.path.join(self.ooni_home, 'reports') @@ -56,7 +59,10 @@ class OConfig(object): if 'logfile' in self.basic: self.basic.logfile = expanduser(self.basic.logfile.replace('~','~'+self.current_user))
- def initialize_ooni_home(self): + def initialize_ooni_home(self, ooni_home=None): + if ooni_home: + self.set_paths(ooni_home) + if not os.path.isdir(self.ooni_home): print "Ooni home directory does not exist." print "Creating it in '%s'." % self.ooni_home diff --git a/ooni/tests/__init__.py b/ooni/tests/__init__.py index 2df8646..b0bbfbd 100644 --- a/ooni/tests/__init__.py +++ b/ooni/tests/__init__.py @@ -1,9 +1,11 @@ import socket from ooni.settings import config
+config.initialize_ooni_home('ooni_home') config.logging = False config.advanced.debug = False
+ def is_internet_connected(): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: diff --git a/ooni/tests/test_geoip.py b/ooni/tests/test_geoip.py index 49d2ea3..affcea6 100644 --- a/ooni/tests/test_geoip.py +++ b/ooni/tests/test_geoip.py @@ -1,3 +1,5 @@ +import os + from twisted.internet import defer from twisted.trial import unittest
@@ -5,11 +7,12 @@ from ooni.tests import is_internet_connected from ooni.settings import config from ooni import geoip
+ class TestGeoIP(unittest.TestCase): def setUp(self): - config.set_paths() + config.initialize_ooni_home('ooni_home') config.read_config_file() - + def test_ip_to_location(self): location = geoip.IPToLocation('8.8.8.8') assert 'countrycode' in location @@ -19,7 +22,9 @@ class TestGeoIP(unittest.TestCase): @defer.inlineCallbacks def test_probe_ip(self): if not is_internet_connected(): - self.skipTest("You must be connected to the internet to run this test") + self.skipTest( + "You must be connected to the internet to run this test" + ) probe_ip = geoip.ProbeIP() res = yield probe_ip.lookup() assert len(res.split('.')) == 4 diff --git a/ooni/tests/test_nettest.py b/ooni/tests/test_nettest.py index ed0e4c5..92278ce 100644 --- a/ooni/tests/test_nettest.py +++ b/ooni/tests/test_nettest.py @@ -121,7 +121,7 @@ class TestNetTest(unittest.TestCase): for i in range(10): f.write("%s\n" % i)
- from ooni.settings import config + config.initialize_ooni_home('ooni_home') config.read_config_file()
def assertCallable(self, thing): @@ -298,7 +298,7 @@ class TestNettestTimeout(unittest.TestCase): def tearDown(self): self.factory.stopFactory() self.port.stopListening() - + def test_nettest_timeout(self): ntl = NetTestLoader(('-u', 'http://localhost:8007/')) ntl.loadNetTestString(http_net_test)