[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [ooni-probe/master] Clean up NetTest tests



commit bc62ec09ab0b62a92364c019797a2c44e41991f2
Author: aagbsn <aagbsn@xxxxxxxx>
Date:   Fri Jan 25 14:15:08 2013 +0000

    Clean up NetTest tests
---
 tests/test_nettest.py |  143 +++++++++++++++++++++++++-----------------------
 1 files changed, 74 insertions(+), 69 deletions(-)

diff --git a/tests/test_nettest.py b/tests/test_nettest.py
index a029fae..ee571e7 100644
--- a/tests/test_nettest.py
+++ b/tests/test_nettest.py
@@ -4,9 +4,10 @@ from tempfile import TemporaryFile, mkstemp
 
 from twisted.trial import unittest
 from twisted.internet import defer, reactor
+from twisted.python.usage import UsageError
 
 from ooni.nettest import NetTest, InvalidOption, MissingRequiredOption
-from ooni.nettest import FailureToLoadNetTest
+from ooni.nettest import NetTestLoader, FailureToLoadNetTest
 from ooni.tasks import BaseTask
 from ooni.utils import NotRootError
 
@@ -17,6 +18,7 @@ from ooni.managers import TaskManager
 from tests.mocks import MockMeasurement, MockMeasurementFailOnce
 from tests.mocks import MockNetTest, MockDirector, MockReporter
 from tests.mocks import MockMeasurementManager
+defer.setDebugging(True)
 
 net_test_string = """
 from twisted.python import usage
@@ -84,9 +86,13 @@ class DummyTestCase(NetTestCase):
 """
 
 dummyInputs = range(1)
-dummyOptions = {'spam': 'notham'}
-dummyInvalidOptions = {'cram': 'jam'}
-dummyOptionsWithRequiredOptions = {'foo':'moo', 'bar':'baz'}
+dummyArgs = ('--spam', 'notham')
+dummyOptions = {'spam':'notham'}
+dummyInvalidArgs = ('--cram', 'jam')
+dummyInvalidOptions= {'cram':'jam'}
+dummyArgsWithRequiredOptions = ('--foo', 'moo', '--bar', 'baz')
+dummyRequiredOptions = {'foo':'moo', 'bar':'baz'}
+dummyArgsWithFile = ('--spam', 'notham', '--file', 'dummyInputFile.txt')
 
 class TestNetTest(unittest.TestCase):
     timeout = 1
@@ -108,11 +114,11 @@ class TestNetTest(unittest.TestCase):
             f.write(net_test_string)
         f.close()
 
-        net_test_from_file = NetTest(net_test_file,
-                dummyOptions, MockReporter())
+        ntl = NetTestLoader(net_test_file,
+                dummyArgs)
 
         test_methods = set()
-        for test_class, test_method in net_test_from_file.test_cases:
+        for test_class, test_method in ntl.testCases:
             instance = test_class()
             c = getattr(instance, test_method)
             self.assertCallable(c)
@@ -128,11 +134,11 @@ class TestNetTest(unittest.TestCase):
         Given a file like object verify that the net test cases are properly
         generated.
         """
-        net_test_from_string = NetTest(net_test_string,
-                dummyOptions, MockReporter())
+        ntl = NetTestLoader(net_test_string,
+                dummyArgs)
 
         test_methods = set()
-        for test_class, test_method in net_test_from_string.test_cases:
+        for test_class, test_method in ntl.testCases:
             instance = test_class()
             c = getattr(instance, test_method)
             self.assertCallable(c)
@@ -146,11 +152,11 @@ class TestNetTest(unittest.TestCase):
         Given a file like object verify that the net test cases are properly
         generated.
         """
-        net_test_from_string = NetTest(StringIO(net_test_string),
-                dummyOptions, MockReporter())
+        ntl = NetTestLoader(StringIO(net_test_string),
+                dummyArgs)
 
         test_methods = set()
-        for test_class, test_method in net_test_from_string.test_cases:
+        for test_class, test_method in ntl.testCases:
             instance = test_class()
             c = getattr(instance, test_method)
             self.assertCallable(c)
@@ -160,95 +166,94 @@ class TestNetTest(unittest.TestCase):
         self.assertEqual(set(['test_a', 'test_b']), test_methods)
 
     def test_load_with_option(self):
-        net_test = NetTest(StringIO(net_test_string),
-                dummyOptions, None)
-        self.assertIsInstance(net_test, NetTest)
-        net_test.setUpNetTestCases()
-        for test_klass, test_meth in net_test.test_cases:
+        ntl = NetTestLoader(StringIO(net_test_string),
+                dummyArgs)
+        self.assertIsInstance(ntl, NetTestLoader)
+        for test_klass, test_meth in ntl.testCases:
             for option in dummyOptions.keys():
                 self.assertIn(option, test_klass.usageOptions())
 
     def test_load_with_invalid_option(self):
         try:
-            net_test = NetTest(StringIO(net_test_string), dummyInvalidOptions, None)
-            net_test.setUpNetTestCases()
-        except InvalidOption:
+            ntl = NetTestLoader(StringIO(net_test_string), dummyInvalidArgs)
+            ntl.checkOptions()
+            raise Exception
+        except UsageError:
             pass
 
     def test_load_with_required_option(self):
-        net_test = NetTest(StringIO(net_test_with_required_option),
-                dummyOptionsWithRequiredOptions, None)
-        net_test.setUpNetTestCases()
-        self.assertIsInstance(net_test, NetTest)
+        net_test = NetTestLoader(StringIO(net_test_with_required_option),
+                dummyArgsWithRequiredOptions)
+        self.assertIsInstance(net_test, NetTestLoader)
 
     def test_load_with_missing_required_option(self):
         try:
-            net_test = NetTest(StringIO(net_test_with_required_option),
-                    dummyOptions, None)
-            net_test.setUpNetTestCases()
+            net_test = NetTestLoader(StringIO(net_test_with_required_option),
+                    dummyArgs)
         except MissingRequiredOption:
             pass
 
     def test_net_test_inputs(self):
-        dummyOptionsWithFile = dict(dummyOptions)
-        dummyOptionsWithFile['file'] = 'dummyInputFile.txt'
-
-        net_test = NetTest(StringIO(net_test_string_with_file),
-            dummyOptionsWithFile, None)
-        net_test.setUpNetTestCases()
-
-        for test_class, test_method in net_test.test_cases:
-            self.assertEqual(len(list(test_class.inputs)), 10)
+        ntl = NetTestLoader(StringIO(net_test_string_with_file),
+            dummyArgsWithFile)
+        ntl.checkOptions()
+
+        # XXX: if you use the same test_class twice you will have consumed all
+        # of its inputs!
+        tested = set([])
+        for test_class, test_method in ntl.testCases:
+            if test_class not in tested:
+                tested.update([test_class])
+                self.assertEqual(len(list(test_class.inputs)), 10)
 
     def test_setup_local_options_in_test_cases(self):
-        net_test = NetTest(StringIO(net_test_string),
-            dummyOptions, None)
-        net_test.setUpNetTestCases()
+        ntl = NetTestLoader(StringIO(net_test_string),
+            dummyArgs)
+        ntl.checkOptions()
 
-        for test_class, test_method in net_test.test_cases:
+        for test_class, test_method in ntl.testCases:
             self.assertEqual(test_class.localOptions, dummyOptions)
 
     def test_generate_measurements_size(self):
-        dummyOptionsWithFile = dict(dummyOptions)
-        dummyOptionsWithFile['file'] = 'dummyInputFile.txt'
 
-        net_test = NetTest(StringIO(net_test_string_with_file),
-            dummyOptionsWithFile, None)
-        net_test.setUpNetTestCases()
+        ntl = NetTestLoader(StringIO(net_test_string_with_file),
+            dummyArgsWithFile)
+        ntl.checkOptions()
+        net_test = NetTest(ntl, None)
+
         measurements = list(net_test.generateMeasurements())
         self.assertEqual(len(measurements), 20)
 
     def test_net_test_completed_callback(self):
-        dummyOptionsWithFile = dict(dummyOptions)
-        dummyOptionsWithFile['file'] = 'dummyInputFile.txt'
+        ntl = NetTestLoader(StringIO(net_test_string_with_file),
+            dummyArgsWithFile)
+        ntl.checkOptions()
+        director = Director()
 
-        net_test = NetTest(StringIO(net_test_string_with_file),
-            dummyOptionsWithFile, MockReporter())
-        net_test.measurementManager = MockMeasurementManager()
-        net_test.director = Director()
+        d = director.startNetTest('', ntl, [MockReporter()])
 
-        d = net_test.start()
         @d.addCallback
         def complete(result):
-            self.assertEqual(result, None)
+            #XXX: why is the return type (True, None) ?
+            self.assertEqual(result, [(True,None)])
+            self.assertEqual(director.successfulMeasurements, 20)
 
         return d
 
-    #def test_require_root_succeed(self):
-    #    #XXX: will require root to run
-    #    n = NetTest(StringIO(net_test_root_required),
-    #            dummyOptions, None)
-    #    for test_class, method in n.test_cases:
-    #        self.assertTrue(test_class.requiresRoot)
-
-    def test_require_root_failed(self):
-        #XXX: will fail if you run as root
-        try:
-            net_test = NetTest(StringIO(net_test_root_required),
-                    dummyOptions, None)
-            net_test.setUpNetTestCases()
-        except NotRootError:
-            pass
+    def test_require_root_succeed(self):
+        #XXX: will require root to run
+        ntl = NetTestLoader(StringIO(net_test_root_required),
+                dummyArgs)
+        for test_class, method in ntl.testCases:
+            self.assertTrue(test_class.requiresRoot)
+
+    #def test_require_root_failed(self):
+    #    #XXX: will fail if you run as root
+    #    try:
+    #        net_test = NetTestLoader(StringIO(net_test_root_required),
+    #                dummyArgs)
+    #    except NotRootError:
+    #        pass
 
     #def test_create_report_succeed(self):
     #    pass



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits