commit ebad1f4ce0fd9d809a8ae3bb3d6d4c2f1e661813 Author: Arturo Filastò art@fuffa.org Date: Thu Apr 11 17:52:45 2013 +0200
Clean up tests for updated ooni codebase --- ooni/reporter.py | 5 +- ooni/runner.py | 9 +- ooni/tests/mocks.py | 2 +- ooni/tests/test-class-design.py | 101 ----------------- ooni/tests/test_director.py | 59 ---------- ooni/tests/test_inputunit.py | 29 ----- ooni/tests/test_managers.py | 12 +- ooni/tests/test_nettest.py | 10 +- ooni/tests/test_reporter.py | 238 --------------------------------------- tests/test_geoip.py | 137 ---------------------- 10 files changed, 21 insertions(+), 581 deletions(-)
diff --git a/ooni/reporter.py b/ooni/reporter.py index e44db57..21a46ca 100644 --- a/ooni/reporter.py +++ b/ooni/reporter.py @@ -394,6 +394,7 @@ class Report(object): for reporter in self.reporters[:]:
def report_created(result): + print "CREATED REPORT %s" % reporter log.debug("Created report with %s" % reporter) self._reporters_openned += 1 are_all_openned() @@ -405,10 +406,11 @@ class Report(object): all_openned.errback(defer.fail(e)) else: are_all_openned() + return failure
d = defer.maybeDeferred(reporter.createReport) - d.addErrback(report_failed) d.addCallback(report_created) + d.addErrback(report_failed)
return all_openned
@@ -454,7 +456,6 @@ class Report(object): Once a report has failed to be created with a reporter we give up and remove the reporter from the list of reporters to write to. """ - failure.trap(errors.OONIBReportError) log.err("Failed to open %s reporter, giving up..." % reporter) log.err("Reporter %s failed, removing from report..." % reporter) #log.exception(failure) diff --git a/ooni/runner.py b/ooni/runner.py index 1c28019..e6c2314 100644 --- a/ooni/runner.py +++ b/ooni/runner.py @@ -176,12 +176,15 @@ def runTestCases(test_cases, options, cmd_line_options):
if cmd_line_options['collector']: log.msg("Using remote collector, please be patient while we create the report.") - try: - yield oonib_reporter.createReport(options) - except OONIBReportError: + d = oonib_reporter.createReport(options) + @d.addErrback + def errback(failure): + print "ERRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR" + failure.trap(errors.OONIBReportError) log.err("Error in creating new report") log.msg("We will only create reports to a file") oonib_reporter = None + yield d else: oonib_reporter = None
diff --git a/ooni/tests/mocks.py b/ooni/tests/mocks.py index fed683e..be55e14 100644 --- a/ooni/tests/mocks.py +++ b/ooni/tests/mocks.py @@ -141,7 +141,7 @@ class MockOReporterThatFailsWrite(MockOReporter):
class MockOReporterThatFailsOpen(MockOReporter): def createReport(self): - self.created.errback(failure.Failure(MockFailure())) + raise MockFailure
class MockOReporterThatFailsWriteOnce(MockOReporter): def __init__(self): diff --git a/ooni/tests/test-class-design.py b/ooni/tests/test-class-design.py deleted file mode 100644 index bb80cd3..0000000 --- a/ooni/tests/test-class-design.py +++ /dev/null @@ -1,101 +0,0 @@ -#!/usr/bin/env python -# -# testing classes to test multiple inheritance. -# these are not meant to be run by trial, though they could be made to be so. -# i didn't know where to put them. --isis - -import abc -from pprint import pprint -from inspect import classify_class_attrs - -class PluginBase(object): - __metaclass__ = abc.ABCMeta - - @abc.abstractproperty - def name(self): - return 'you should not see this' - - @name.setter - def name(self, value): - return 'you should not set this' - - @name.deleter - def name(self): - return 'you should not del this' - - @abc.abstractmethod - def inputParser(self, line): - """Do something to parse something.""" - return - -class Foo(object): - woo = "this class has some shit in it" - def bar(self): - print "i'm a Foo.bar()!" - print woo - -class KwargTest(Foo): - _name = "isis" - - #def __new__(cls, *a, **kw): - # return super(KwargTest, cls).__new__(cls, *a, **kw) - - @property - def name(self): - return self._name - - @name.setter - def name(self, value): - self._name = value - - def __init__(self, *a, **kw): - super(KwargTest, self).__init__() - - ## this causes the instantion args to override the class attrs - for key, value in kw.items(): - setattr(self.__class__, key, value) - - print "%s.__init__(): self.__dict__ = %s" \ - % (type(self), pprint(type(self).__dict__)) - - for attr in classify_class_attrs(self): - print attr - - @classmethod - def sayname(cls): - print cls.name - -class KwargTestChild(KwargTest): - name = "arturo" - def __init__(self): - super(KwargTestChild, self).__init__() - print self.name - -class KwargTestChildOther(KwargTest): - def __init__(self, name="robot", does="lasers"): - super(KwargTestChildOther, self).__init__() - print self.name - - -if __name__ == "__main__": - print "class KwargTest attr name: %s" % KwargTest.name - kwargtest = KwargTest() - print "KwargTest instantiated wo args" - print "kwargtest.name: %s" % kwargtest.name - print "kwargtest.sayname(): %s" % kwargtest.sayname() - kwargtest2 = KwargTest(name="lovecruft", does="hacking") - print "KwargTest instantiated with name args" - print "kwargtest.name: %s" % kwargtest2.name - print "kwargtest.sayname(): %s" % kwargtest2.sayname() - - print "class KwargTestChild attr name: %s" % KwargTestChild.name - kwargtestchild = KwargTestChild() - print "KwargTestChild instantiated wo args" - print "kwargtestchild.name: %s" % kwargtestchild.name - print "kwargtestchild.sayname(): %s" % kwargtestchild.sayname() - - print "class KwargTestChildOther attr name: %s" % KwargTestChildOther.name - kwargtestchildother = KwargTestChildOther() - print "KwargTestChildOther instantiated wo args" - print "kwargtestchildother.name: %s" % kwargtestchildother.name - print "kwargtestchildother.sayname(): %s" % kwargtestchildother.sayname() diff --git a/ooni/tests/test_director.py b/ooni/tests/test_director.py deleted file mode 100644 index a9dfbe8..0000000 --- a/ooni/tests/test_director.py +++ /dev/null @@ -1,59 +0,0 @@ -from twisted.internet import defer, base -from twisted.trial import unittest - -from ooni.director import Director -from ooni.nettest import NetTestLoader -from tests.mocks import MockReporter -base.DelayedCall.debug = True - -net_test_string = """ -from twisted.python import usage -from ooni.nettest import NetTestCase - -class UsageOptions(usage.Options): - optParameters = [['spam', 's', None, 'ham']] - -class DummyTestCase(NetTestCase): - inputFile = ['file', 'f', None, 'The input File'] - - usageOptions = UsageOptions - - def test_a(self): - self.report['bar'] = 'bar' - - def test_b(self): - self.report['foo'] = 'foo' -""" - - -dummyArgs = ('--spam', 1, '--file', 'dummyInputFile.txt') - -class TestDirector(unittest.TestCase): - timeout = 1 - def setUp(self): - with open('dummyInputFile.txt', 'w') as f: - for i in range(10): - f.write("%s\n" % i) - - self.reporters = [MockReporter()] - self.director = Director() - - def tearDown(self): - pass - - def test_start_net_test(self): - ntl = NetTestLoader(dummyArgs) - ntl.loadNetTestString(net_test_string) - - ntl.checkOptions() - d = self.director.startNetTest('', ntl, self.reporters) - - @d.addCallback - def done(result): - self.assertEqual(self.director.successfulMeasurements, 20) - - return d - - def test_stop_net_test(self): - pass - diff --git a/ooni/tests/test_inputunit.py b/ooni/tests/test_inputunit.py deleted file mode 100644 index 1f9043c..0000000 --- a/ooni/tests/test_inputunit.py +++ /dev/null @@ -1,29 +0,0 @@ -import unittest -from ooni.inputunit import InputUnit, InputUnitFactory - -def dummyGenerator(): - for x in range(100): - yield x - -class TestInputUnit(unittest.TestCase): - def test_input_unit_factory(self): - inputUnit = InputUnitFactory(range(100)) - for i in inputUnit: - self.assertEqual(len(list(i)), inputUnit.inputUnitSize) - - def test_input_unit(self): - inputs = range(100) - inputUnit = InputUnit(inputs) - idx = 0 - for i in inputUnit: - idx += 1 - - self.assertEqual(idx, 100) - - def test_input_unit_factory_length(self): - inputUnitFactory = InputUnitFactory(range(100)) - l1 = len(inputUnitFactory) - l2 = sum(1 for _ in inputUnitFactory) - self.assertEqual(l1, 10) - self.assertEqual(l2, 10) - diff --git a/ooni/tests/test_managers.py b/ooni/tests/test_managers.py index 39f0881..e2af7b3 100644 --- a/ooni/tests/test_managers.py +++ b/ooni/tests/test_managers.py @@ -5,12 +5,12 @@ from twisted.internet import defer, task from ooni.tasks import BaseTask, TaskWithTimeout, TaskTimedOut from ooni.managers import TaskManager, MeasurementManager
-from tests.mocks import MockSuccessTask, MockFailTask, MockFailOnceTask, MockFailure -from tests.mocks import MockSuccessTaskWithTimeout, MockFailTaskThatTimesOut -from tests.mocks import MockTimeoutOnceTask, MockFailTaskWithTimeout -from tests.mocks import MockTaskManager, mockFailure, MockDirector -from tests.mocks import MockNetTest, MockMeasurement, MockSuccessMeasurement -from tests.mocks import MockFailMeasurement, MockFailOnceMeasurement +from ooni.tests.mocks import MockSuccessTask, MockFailTask, MockFailOnceTask, MockFailure +from ooni.tests.mocks import MockSuccessTaskWithTimeout, MockFailTaskThatTimesOut +from ooni.tests.mocks import MockTimeoutOnceTask, MockFailTaskWithTimeout +from ooni.tests.mocks import MockTaskManager, mockFailure, MockDirector +from ooni.tests.mocks import MockNetTest, MockMeasurement, MockSuccessMeasurement +from ooni.tests.mocks import MockFailMeasurement, MockFailOnceMeasurement
class TestTaskManager(unittest.TestCase): timeout = 1 diff --git a/ooni/tests/test_nettest.py b/ooni/tests/test_nettest.py index 1d04791..43f03e0 100644 --- a/ooni/tests/test_nettest.py +++ b/ooni/tests/test_nettest.py @@ -14,9 +14,9 @@ from ooni.director import Director
from ooni.managers import TaskManager
-from tests.mocks import MockMeasurement, MockMeasurementFailOnce -from tests.mocks import MockNetTest, MockDirector, MockReporter -from tests.mocks import MockMeasurementManager +from ooni.tests.mocks import MockMeasurement, MockMeasurementFailOnce +from ooni.tests.mocks import MockNetTest, MockDirector, MockReporter +from ooni.tests.mocks import MockMeasurementManager defer.setDebugging(True)
net_test_string = """ @@ -226,8 +226,8 @@ class TestNetTest(unittest.TestCase):
@d.addCallback def complete(result): - #XXX: why is the return type (True, None) ? - self.assertEqual(result, [(True,None)]) + print "IN here y0" + self.assertEqual(result, None) self.assertEqual(director.successfulMeasurements, 20)
return d diff --git a/ooni/tests/test_reporter.py b/ooni/tests/test_reporter.py deleted file mode 100644 index e21b7a1..0000000 --- a/ooni/tests/test_reporter.py +++ /dev/null @@ -1,238 +0,0 @@ -from twisted.internet import defer -from twisted.trial import unittest - -from ooni.reporter import Report, YAMLReporter, OONIBReporter, safe_dump -from ooni.managers import ReportEntryManager, TaskManager -from ooni.nettest import NetTest, NetTestState -from ooni.errors import ReportNotCreated, ReportAlreadyClosed - -from ooni.tasks import TaskWithTimeout -from tests.mocks import MockOReporter, MockTaskManager -from tests.mocks import MockMeasurement, MockNetTest -from tests.mocks import MockOReporterThatFailsWrite -from tests.mocks import MockOReporterThatFailsWriteOnce -from tests.mocks import MockOReporterThatFailsOpen - -from twisted.python import failure -import yaml - -class TestReport(unittest.TestCase): - def setUp(self): - pass - def tearDown(self): - pass - def test_create_report_with_no_reporter(self): - report = Report([],ReportEntryManager()) - self.assertIsInstance(report, Report) - - def test_create_report_with_single_reporter(self): - report = Report([MockOReporter()], ReportEntryManager()) - self.assertIsInstance(report, Report) - - def test_create_report_with_multiple_reporters(self): - report = Report([MockOReporter() for x in xrange(3)], - ReportEntryManager()) - self.assertIsInstance(report, Report) - - def test_report_open_with_single_reporter(self): - report = Report([MockOReporter()],ReportEntryManager()) - d = report.open() - return d - - def test_report_open_with_multiple_reporter(self): - report = Report([MockOReporter() for x in xrange(3)], - ReportEntryManager()) - d = report.open() - return d - - def test_fail_to_open_report_with_single_reporter(self): - report = Report([MockOReporterThatFailsOpen()], - ReportEntryManager()) - d = report.open() - def f(x): - self.assertEquals(len(report.reporters), 0) - d.addCallback(f) - return d - - def test_fail_to_open_single_report_with_multiple_reporter(self): - report = Report([MockOReporterThatFailsOpen(), MockOReporter(), - MockOReporter()], ReportEntryManager()) - d = report.open() - def f(x): - self.assertEquals(len(report.reporters),2) - d.addCallback(f) - return d - - def test_fail_to_open_all_reports_with_multiple_reporter(self): - report = Report([MockOReporterThatFailsOpen() for x in xrange(3)], - ReportEntryManager()) - d = report.open() - def f(x): - self.assertEquals(len(report.reporters),0) - d.addCallback(f) - return d - - def test_write_report_with_single_reporter_and_succeed(self): - #XXX: verify that the MockOReporter writeReportEntry succeeds - report = Report([MockOReporter()], ReportEntryManager()) - report.open() - d = report.write(MockMeasurement(MockNetTest())) - return d - - def test_write_report_with_single_reporter_and_fail_after_timeout(self): - report = Report([MockOReporterThatFailsWrite()], ReportEntryManager()) - report.open() - d = report.write(MockMeasurement(MockNetTest())) - def f(err): - self.assertEquals(len(report.reporters),0) - d.addBoth(f) - return d - - def test_write_report_with_single_reporter_and_succeed_after_timeout(self): - report = Report([MockOReporterThatFailsWriteOnce()], ReportEntryManager()) - report.open() - d = report.write(MockMeasurement(MockNetTest())) - return d - - def test_write_report_with_multiple_reporter_and_succeed(self): - report = Report([MockOReporter() for x in xrange(3)], ReportEntryManager()) - report.open() - d = report.write(MockMeasurement(MockNetTest())) - return d - - def test_write_report_with_multiple_reporter_and_fail_a_single_reporter(self): - report = Report([MockOReporter(), MockOReporter(), MockOReporterThatFailsWrite()], ReportEntryManager()) - d = report.open() - - self.assertEquals(len(report.reporters),3) - d = report.write(MockMeasurement(MockNetTest())) - - def f(x): - # one of the reporters should have been removed - self.assertEquals(len(report.reporters), 2) - d.addBoth(f) - return d - - def test_write_report_with_multiple_reporter_and_fail_all_reporter(self): - report = Report([MockOReporterThatFailsWrite() for x in xrange(3)], ReportEntryManager()) - report.open() - d = report.write(MockMeasurement(MockNetTest())) - def f(err): - self.assertEquals(len(report.reporters),0) - d.addErrback(f) - return d - -class TestYAMLReporter(unittest.TestCase): - def setUp(self): - self.testDetails = {'software_name': 'ooniprobe', 'options': - {'pcapfile': None, 'help': 0, 'subargs': ['-f', 'alexa_10'], 'resume': - 0, 'parallelism': '10', 'no-default-reporter': 0, 'testdeck': None, - 'test': 'nettests/blocking/http_requests.py', 'logfile': None, - 'collector': None, 'reportfile': None}, 'test_version': '0.2.3', - 'software_version': '0.0.10', 'test_name': 'http_requests_test', - 'start_time': 1362054343.0, 'probe_asn': 'AS0', 'probe_ip': - '127.0.0.1', 'probe_cc': 'US'} - - def tearDown(self): - pass - def test_create_yaml_reporter(self): - self.assertIsInstance(YAMLReporter(self.testDetails), - YAMLReporter) - - def test_open_yaml_report_and_succeed(self): - r = YAMLReporter(self.testDetails) - r.createReport() - # verify that testDetails was written to report properly - def f(r): - r._stream.seek(0) - details, = yaml.safe_load_all(r._stream) - self.assertEqual(details, self.testDetails) - r.created.addCallback(f) - return r.created - - #def test_open_yaml_report_and_fail(self): - # #XXX: YAMLReporter does not handle failures of this type - # pass - - def test_write_yaml_report_entry(self): - r = YAMLReporter(self.testDetails) - r.createReport() - - report_entry = {'foo':'bar', 'bin':'baz'} - r.writeReportEntry(report_entry) - - # verify that details and entry were written to report - def f(r): - r._stream.seek(0) - report = yaml.safe_load_all(r._stream) - details, entry = report - self.assertEqual(details, self.testDetails) - self.assertEqual(entry, report_entry) - r.created.addCallback(f) - return r.created - - def test_write_multiple_yaml_report_entry(self): - r = YAMLReporter(self.testDetails) - r.createReport() - def reportEntry(): - for x in xrange(10): - yield {'foo':'bar', 'bin':'baz', 'item':x} - for entry in reportEntry(): - r.writeReportEntry(entry) - # verify that details and multiple entries were written to report - def f(r): - r._stream.seek(0) - report = yaml.safe_load_all(r._stream) - details = report.next() - self.assertEqual(details, self.testDetails) - self.assertEqual([r for r in report], [r for r in reportEntry()]) - r.created.addCallback(f) - return r.created - - def test_close_yaml_report(self): - r = YAMLReporter(self.testDetails) - r.createReport() - r.finish() - self.assertTrue(r._stream.closed) - - def test_write_yaml_report_after_close(self): - r = YAMLReporter(self.testDetails) - r.createReport() - r.finish() - def f(r): - r.writeReportEntry("foo") - r.created.addCallback(f) - self.assertFailure(r.created, ReportAlreadyClosed) - - def test_write_yaml_report_before_open(self): - r = YAMLReporter(self.testDetails) - def f(r): - r.writeReportEntry("foo") - r.created.addCallback(f) - self.assertFailure(r.created, ReportNotCreated) - -#class TestOONIBReporter(unittest.TestCase): -# def setUp(self): -# pass -# def tearDown(self): -# pass -# def test_create_oonib_reporter(self): -# raise NotImplementedError -# def test_open_oonib_report_and_succeed(self): -# raise NotImplementedError -# def test_open_oonib_report_and_fail(self): -# raise NotImplementedError -# def test_write_oonib_report_entry_and_succeed(self): -# raise NotImplementedError -# def test_write_oonib_report_entry_and_succeed_after_timeout(self): -# raise NotImplementedError -# def test_write_oonib_report_entry_and_fail_after_timeout(self): -# raise NotImplementedError -# def test_write_oonib_report_after_close(self): -# raise NotImplementedError -# def test_write_oonib_report_before_open(self): -# raise NotImplementedError -# def test_close_oonib_report_and_succeed(self): -# raise NotImplementedError -# def test_close_oonib_report_and_fail(self): -# raise NotImplementedError diff --git a/tests/test_geoip.py b/tests/test_geoip.py deleted file mode 100644 index 88f3dcb..0000000 --- a/tests/test_geoip.py +++ /dev/null @@ -1,137 +0,0 @@ -from collections import namedtuple - -from twisted.web import server, static, resource -from twisted.internet import reactor, defer -from twisted.trial import unittest -from twisted.python.filepath import FilePath -from twisted.protocols.policies import WrappingFactory - -from ooni import errors -from ooni.geoip import ProbeIP, MaxMindGeoIP, TorProjectGeoIP -from ooni.geoip import UbuntuGeoIP, HTTPGeoIPLookupper, IPToLocation - -class UbuntuGeoIPResource(resource.Resource): - def render(self, request): - return """<?xml version="1.0" encoding="UTF-8"?><Response><Ip>127.0.0.1</Ip><Status>OK</Status></Response>""" - -class MaxMindGeoIPResource(resource.Resource): - def render(self, request): - return """ - <span id="my-ip-address">127.0.0.1</span> - """ - -class TorProjectGeoIPResource(resource.Resource): - def render(self, request): - return """ - Your IP address appears to be: <b>127.0.0.1</b> - """ - -class GeoIPBaseTest(unittest.TestCase): - services = {'ubuntu': UbuntuGeoIPResource, - 'maxmind': MaxMindGeoIPResource, - 'torproject': TorProjectGeoIPResource - } - def _listen(self, site): - return reactor.listenTCP(0, site, interface="127.0.0.1") - - def setUp(self): - r = resource.Resource() - for name, service in self.services.items(): - r.putChild(name, service()) - self.site = server.Site(r, timeout=None) - - self.wrapper = WrappingFactory(self.site) - self.port = self._listen(self.wrapper) - self.portno = self.port.getHost().port - - def getUrl(self, service_name): - return "http://%s:%s/%s" % ('127.0.0.1', self.portno, service_name) - - def tearDown(self): - return self.port.stopListening() - -class TestGeoIPServices(GeoIPBaseTest): - def test_torproject_geoip(self): - gip = TorProjectGeoIP() - gip.url = self.getUrl('torproject') - d = gip.lookup() - @d.addBoth - def cb(res): - self.assertEqual(res, '127.0.0.1') - return d - - def test_ubuntu_geoip(self): - gip = UbuntuGeoIP() - gip.url = self.getUrl('ubuntu') - d = gip.lookup() - @d.addBoth - def cb(res): - self.assertEqual(res, '127.0.0.1') - return d - - def test_maxmind_geoip(self): - gip = MaxMindGeoIP() - gip.url = self.getUrl('maxmind') - d = gip.lookup() - @d.addBoth - def cb(res): - self.assertEqual(res, '127.0.0.1') - return d - -class TestProbeIP(GeoIPBaseTest): - def setUp(self): - GeoIPBaseTest.setUp(self) - - # Override the service addresses with those of the fake localhost - # resource. - self.probe_ip = ProbeIP() - for name in self.probe_ip.geoIPServices.keys(): - self.probe_ip.geoIPServices[name].url = self.getUrl(name) - - def test_ask_geoip_service(self): - d = self.probe_ip.askGeoIPService() - @d.addBoth - def cb(res): - self.assertEqual(self.probe_ip.address, '127.0.0.1') - return d - - def test_fail_traceroute_service(self): - self.assertRaises(errors.InsufficientPrivileges, self.probe_ip.askTraceroute) - - def test_ask_tor(self): - class MockTorState(object): - """ - This is a Mock Tor state object. It will just pretend to answer to - the get_info("address") method call. - """ - protocol = namedtuple('Protocol', 'get_info') - def __init__(self): - def get_info(key): - return defer.succeed({'XXX': '127.0.0.2'}) - self.protocol = self.protocol(get_info=get_info) - - self.probe_ip.tor_state = MockTorState() - d = self.probe_ip.lookup() - @d.addBoth - def cb(res): - self.assertEqual(self.probe_ip.address, '127.0.0.2') - return d - - def test_probe_ip(self): - d = self.probe_ip.lookup() - @d.addBoth - def cb(res): - self.assertEqual(self.probe_ip.address, '127.0.0.1') - self.assertTrue(self.probe_ip.strategy.startswith('geo_ip_service-')) - return d - - def test_failing_probe_ip(self): - self.probe_ip.geoIPServices = {} - - d = self.probe_ip.lookup() - self.assertFailure(d, errors.ProbeIPUnknown) - return d - -class TestIPToLocation(unittest.TestCase): - pass -