commit 5da092dd0097cd4ffa40de2e6c1a2e99646a516e Author: aagbsn aagbsn@extc.org Date: Tue Feb 26 15:51:39 2013 +0100
Add tests for YAML reporter --- tests/test_reporter.py | 115 ++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 91 insertions(+), 24 deletions(-)
diff --git a/tests/test_reporter.py b/tests/test_reporter.py index d2fcc4e..e21b7a1 100644 --- a/tests/test_reporter.py +++ b/tests/test_reporter.py @@ -1,9 +1,10 @@ from twisted.internet import defer from twisted.trial import unittest
-from ooni.reporter import Report, YAMLReporter, OONIBReporter +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 @@ -13,6 +14,7 @@ from tests.mocks import MockOReporterThatFailsWriteOnce from tests.mocks import MockOReporterThatFailsOpen
from twisted.python import failure +import yaml
class TestReport(unittest.TestCase): def setUp(self): @@ -120,29 +122,94 @@ class TestReport(unittest.TestCase): d.addErrback(f) return d
-#class TestYAMLReporter(unittest.TestCase): -# def setUp(self): -# pass -# def tearDown(self): -# pass -# def test_create_yaml_reporter(self): -# raise NotImplementedError -# def test_open_yaml_report_and_succeed(self): -# raise NotImplementedError -# def test_open_yaml_report_and_fail(self): -# raise NotImplementedError -# def test_write_yaml_report_entry(self): -# raise NotImplementedError -# def test_write_multiple_yaml_report_entry(self): -# raise NotImplementedError -# def test_close_yaml_report(self): -# raise NotImplementedError -# def test_write_yaml_report_after_close(self): -# raise NotImplementedError -# def test_write_yaml_report_before_open(self): -# raise NotImplementedError -# def test_close_yaml_report_after_task_complete(self): -# raise NotImplementedError +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):
tor-commits@lists.torproject.org