[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [ooni-probe/master] Another cleanup and config coherence consolidated in ooni.tests.bases
commit 09574e93ff6c24b5dc5f6b5ffe0fb5dbca9888e8
Author: kudrom <kudrom@xxxxxxxxxx>
Date: Sun Jun 22 19:44:09 2014 +0200
Another cleanup and config coherence consolidated in ooni.tests.bases
---
ooni/tests/bases.py | 11 +++++++++++
ooni/tests/mocks.py | 15 +++++++++++++++
ooni/tests/test_deck.py | 17 +----------------
ooni/tests/test_director.py | 5 +++--
ooni/tests/test_nettest.py | 12 ++++++------
ooni/tests/test_oonibclient.py | 6 ++----
ooni/tests/test_oonicli.py | 18 ++++++++----------
7 files changed, 46 insertions(+), 38 deletions(-)
diff --git a/ooni/tests/bases.py b/ooni/tests/bases.py
new file mode 100644
index 0000000..a359ef8
--- /dev/null
+++ b/ooni/tests/bases.py
@@ -0,0 +1,11 @@
+from twisted.trial import unittest
+
+from ooni.settings import config
+
+
+class ConfigTestCase(unittest.TestCase):
+ def setUp(self):
+ config.initialize_ooni_home("ooni_home")
+
+ def tearDown(self):
+ config.read_config_file()
\ No newline at end of file
diff --git a/ooni/tests/mocks.py b/ooni/tests/mocks.py
index f323f56..72865b1 100644
--- a/ooni/tests/mocks.py
+++ b/ooni/tests/mocks.py
@@ -189,3 +189,18 @@ class MockTaskManager(TaskManager):
def succeeded(self, result, task):
self.successes.append((result, task))
+
+class MockOONIBClient(object):
+ def lookupTestHelpers(self, required_test_helpers):
+ ret = {
+ 'default': {
+ 'address': '127.0.0.1',
+ 'collector': 'httpo://thirteenchars1234.onion'
+ }
+ }
+ for required_test_helper in required_test_helpers:
+ ret[required_test_helper] = {
+ 'address': '127.0.0.1',
+ 'collector': 'httpo://thirteenchars1234.onion'
+ }
+ return defer.succeed(ret)
diff --git a/ooni/tests/test_deck.py b/ooni/tests/test_deck.py
index 7e9fc7f..d82c0eb 100644
--- a/ooni/tests/test_deck.py
+++ b/ooni/tests/test_deck.py
@@ -5,6 +5,7 @@ from twisted.trial import unittest
from hashlib import sha256
from ooni.deck import InputFile, Deck
+from ooni.tests.mocks import MockOONIBClient
net_test_string = """
from twisted.python import usage
@@ -83,22 +84,6 @@ class TestInputFile(BaseTestCase):
assert input_file.descriptorCached
-class MockOONIBClient(object):
- def lookupTestHelpers(self, required_test_helpers):
- ret = {
- 'default': {
- 'address': '127.0.0.1',
- 'collector': 'httpo://thirteenchars1234.onion'
- }
- }
- for required_test_helper in required_test_helpers:
- ret[required_test_helper] = {
- 'address': '127.0.0.1',
- 'collector': 'httpo://thirteenchars1234.onion'
- }
- return defer.succeed(ret)
-
-
class TestDeck(BaseTestCase):
def setUp(self):
super(TestDeck, self).setUp()
diff --git a/ooni/tests/test_director.py b/ooni/tests/test_director.py
index cd0483b..da81731 100644
--- a/ooni/tests/test_director.py
+++ b/ooni/tests/test_director.py
@@ -2,6 +2,7 @@ from mock import patch, MagicMock
from ooni.settings import config
from ooni.director import Director
+from ooni.tests.bases import ConfigTestCase
from twisted.internet import defer
from twisted.trial import unittest
@@ -26,10 +27,10 @@ mock_launch_tor = MagicMock()
mock_launch_tor.return_value = defer.succeed(proto)
-class TestDirector(unittest.TestCase):
+class TestDirector(ConfigTestCase):
def tearDown(self):
+ super(TestDirector, self).tearDown()
config.tor_state = None
- config.read_config_file()
def test_get_net_tests(self):
director = Director()
diff --git a/ooni/tests/test_nettest.py b/ooni/tests/test_nettest.py
index 92d6cea..4ef6a55 100644
--- a/ooni/tests/test_nettest.py
+++ b/ooni/tests/test_nettest.py
@@ -18,6 +18,7 @@ from ooni.managers import TaskManager
from ooni.tests.mocks import MockMeasurement, MockMeasurementFailOnce
from ooni.tests.mocks import MockNetTest, MockDirector, MockReporter
from ooni.tests.mocks import MockMeasurementManager
+from ooni.tests.bases import ConfigTestCase
net_test_string = """
from twisted.python import usage
@@ -114,20 +115,19 @@ dummyInvalidOptions = {'cram': 'jam'}
dummyArgsWithRequiredOptions = ('--foo', 'moo', '--bar', 'baz')
dummyRequiredOptions = {'foo': 'moo', 'bar': 'baz'}
dummyArgsWithFile = ('--spam', 'notham', '--file', 'dummyInputFile.txt')
+dummyInputFile = 'dummyInputFile.txt'
class TestNetTest(unittest.TestCase):
timeout = 1
- HOME_DIR = 'ooni_home'
- DUMMY_INPUT_FILE = 'dummyInputFile.txt'
def setUp(self):
- with open(self.DUMMY_INPUT_FILE, 'w') as f:
+ with open(dummyInputFile, 'w') as f:
for i in range(10):
f.write("%s\n" % i)
def tearDown(self):
- os.remove(self.DUMMY_INPUT_FILE)
+ os.remove(dummyInputFile)
def assertCallable(self, thing):
self.assertIn('__call__', dir(thing))
@@ -272,7 +272,7 @@ class TestNetTest(unittest.TestCase):
self.assertTrue(test_class.requiresRoot)
-class TestNettestTimeout(unittest.TestCase):
+class TestNettestTimeout(ConfigTestCase):
@defer.inlineCallbacks
def setUp(self):
from twisted.internet.protocol import Protocol, Factory
@@ -302,9 +302,9 @@ class TestNettestTimeout(unittest.TestCase):
config.advanced.measurement_timeout = 2
def tearDown(self):
+ super(TestNettestTimeout, self).tearDown()
self.factory.stopFactory()
self.port.stopListening()
- config.read_config_file()
def test_nettest_timeout(self):
ntl = NetTestLoader(('-u', 'http://localhost:8007/'))
diff --git a/ooni/tests/test_oonibclient.py b/ooni/tests/test_oonibclient.py
index c6615cb..1497105 100644
--- a/ooni/tests/test_oonibclient.py
+++ b/ooni/tests/test_oonibclient.py
@@ -9,12 +9,13 @@ from ooni import errors as e
from ooni.utils import log
from ooni.settings import config
from ooni.oonibclient import OONIBClient
+from ooni.tests.bases import ConfigTestCase
input_id = '37e60e13536f6afe47a830bfb6b371b5cf65da66d7ad65137344679b24fdccd1'
deck_id = 'd4ae40ecfb3c1b943748cce503ab8233efce7823f3e391058fc0f87829c644ed'
-class TestOONIBClient(unittest.TestCase):
+class TestOONIBClient(ConfigTestCase):
def setUp(self):
host = '127.0.0.1'
port = 8889
@@ -37,9 +38,6 @@ class TestOONIBClient(unittest.TestCase):
self.skipTest("OONIB must be listening on port 8888 to run this test (tor_hidden_service: false)")
self.oonibclient = OONIBClient('http://' + host + ':' + str(port))
- def tearDown(self):
- config.read_config_file()
-
@defer.inlineCallbacks
def test_query(self):
res = yield self.oonibclient.queryBackend('GET', '/policy/input')
diff --git a/ooni/tests/test_oonicli.py b/ooni/tests/test_oonicli.py
index 4da63f0..dd41c1d 100644
--- a/ooni/tests/test_oonicli.py
+++ b/ooni/tests/test_oonicli.py
@@ -6,6 +6,7 @@ from twisted.internet import defer
from twisted.trial import unittest
from ooni.tests import is_internet_connected
+from ooni.tests.bases import ConfigTestCase
from ooni.settings import config
from ooni.oonicli import runWithDirector
@@ -26,31 +27,28 @@ def verify_entry(entry):
assert 'input' in entry
-class TestRunDirector(unittest.TestCase):
+class TestRunDirector(ConfigTestCase):
def setUp(self):
if not is_internet_connected():
self.skipTest("You must be connected to the internet to run this test")
config.tor.socks_port = 9050
config.tor.control_port = None
+ self.filenames = ['example-input.txt']
with open('example-input.txt', 'w+') as f:
f.write('http://torproject.org/\n')
f.write('http://bridges.torproject.org/\n')
f.write('http://blog.torproject.org/\n')
def tearDown(self):
- config.read_config_file()
- try:
- os.remove('test_report.yaml')
- except:
- pass
- try:
- os.remove('example-input.txt')
- except:
- pass
+ super(TestRunDirector, self).tearDown()
+ if len(self.filenames) > 0:
+ for filename in self.filenames:
+ os.remove(filename)
@defer.inlineCallbacks
def run_helper(self, test_name, args, verify_function):
output_file = 'test_report.yaml'
+ self.filenames.append(output_file)
sys.argv = ['', '-n', '-o', output_file, test_name]
sys.argv.extend(args)
yield runWithDirector(False, False)
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits