Skip to content

Commit

Permalink
When running unittests don't write to outside the build dir.
Browse files Browse the repository at this point in the history
  • Loading branch information
hellais committed Apr 30, 2014
1 parent 858f09d commit b615c85
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
3 changes: 2 additions & 1 deletion ooni/oonicli.py
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
18 changes: 12 additions & 6 deletions ooni/settings.py
Expand Up @@ -3,15 +3,14 @@
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 = {}
Expand All @@ -28,9 +27,11 @@ def __init__(self):
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'):
Expand All @@ -43,6 +44,8 @@ def set_paths(self):
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')
Expand All @@ -56,7 +59,10 @@ def set_paths(self):
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
Expand Down
2 changes: 2 additions & 0 deletions 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:
Expand Down
11 changes: 8 additions & 3 deletions ooni/tests/test_geoip.py
@@ -1,15 +1,18 @@
import os

from twisted.internet import defer
from twisted.trial import unittest

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
Expand All @@ -19,7 +22,9 @@ def test_ip_to_location(self):
@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
4 changes: 2 additions & 2 deletions ooni/tests/test_nettest.py
Expand Up @@ -121,7 +121,7 @@ def setUp(self):
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):
Expand Down Expand Up @@ -298,7 +298,7 @@ def stopFactory(self):
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)
Expand Down

0 comments on commit b615c85

Please sign in to comment.