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

[tor-commits] [stem/master] @require_online decorator



commit 5aec4d5d8abc614c089a95e09b81bf9f1e3c6ce2
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sat Feb 21 13:39:39 2015 -0800

    @require_online decorator
---
 test/integ/control/controller.py |   53 ++++++++++----------------------------
 test/integ/descriptor/remote.py  |   42 +++++++++++++-----------------
 test/runner.py                   |   16 ++++++------
 3 files changed, 39 insertions(+), 72 deletions(-)

diff --git a/test/integ/control/controller.py b/test/integ/control/controller.py
index 2a7bc69..0a0e112 100644
--- a/test/integ/control/controller.py
+++ b/test/integ/control/controller.py
@@ -29,6 +29,7 @@ from stem.version import Requirement
 from test.runner import (
   require_controller,
   require_version,
+  require_online,
 )
 
 # Router status entry for a relay with a nickname other than 'Unnamed'. This is
@@ -709,6 +710,7 @@ class TestController(unittest.TestCase):
       self.assertEqual([('127.0.0.1', 1112)], controller.get_socks_listeners())
 
   @require_controller
+  @require_online
   @require_version(stem.version.Version('0.1.2.2-alpha'))
   def test_enable_feature(self):
     """
@@ -720,13 +722,6 @@ class TestController(unittest.TestCase):
     with runner.get_tor_controller() as controller:
       self.assertTrue(controller.is_feature_enabled('VERBOSE_NAMES'))
 
-      orconn_output = controller.get_info('orconn-status')
-
-      # the orconn-status results will be empty if we don't have a connection
-      if orconn_output == '':
-        if test.runner.require_online(self):
-          return
-
       self.assertTrue('VERBOSE_NAMES' in controller._enabled_features)
       self.assertRaises(stem.InvalidArguments, controller.enable_feature, ['NOT', 'A', 'FEATURE'])
 
@@ -766,11 +761,9 @@ class TestController(unittest.TestCase):
       self.assertTrue(controller.get_newnym_wait() > 9.0)
 
   @require_controller
+  @require_online
   @require_version(Requirement.EXTENDCIRCUIT_PATH_OPTIONAL)
   def test_extendcircuit(self):
-    if test.runner.require_online(self):
-      return
-
     with test.runner.get_runner().get_tor_controller() as controller:
       circuit_id = controller.extend_circuit('0')
 
@@ -784,15 +777,13 @@ class TestController(unittest.TestCase):
       self.assertRaises(stem.InvalidRequest, controller.extend_circuit, '0', 'thisroutershouldntexistbecausestemexists!@##$%#', 'foo')
 
   @require_controller
+  @require_online
   @require_version(Requirement.EXTENDCIRCUIT_PATH_OPTIONAL)
   def test_repurpose_circuit(self):
     """
     Tests Controller.repurpose_circuit with valid and invalid input.
     """
 
-    if test.runner.require_online(self):
-      return
-
     runner = test.runner.get_runner()
 
     with runner.get_tor_controller() as controller:
@@ -809,15 +800,13 @@ class TestController(unittest.TestCase):
       self.assertRaises(stem.InvalidRequest, controller.repurpose_circuit, '4', 'fooo')
 
   @require_controller
+  @require_online
   @require_version(Requirement.EXTENDCIRCUIT_PATH_OPTIONAL)
   def test_close_circuit(self):
     """
     Tests Controller.close_circuit with valid and invalid input.
     """
 
-    if test.runner.require_online(self):
-      return
-
     runner = test.runner.get_runner()
 
     with runner.get_tor_controller() as controller:
@@ -838,14 +827,12 @@ class TestController(unittest.TestCase):
       self.assertRaises(stem.InvalidRequest, controller.close_circuit, '')
 
   @require_controller
+  @require_online
   def test_get_streams(self):
     """
     Tests Controller.get_streams().
     """
 
-    if test.runner.require_online(self):
-      return
-
     host = socket.gethostbyname('www.torproject.org')
     port = 443
 
@@ -865,14 +852,12 @@ class TestController(unittest.TestCase):
     self.assertTrue('%s:%s' % (host, port) in [stream.target for stream in streams])
 
   @require_controller
+  @require_online
   def test_close_stream(self):
     """
     Tests Controller.close_stream with valid and invalid input.
     """
 
-    if test.runner.require_online(self):
-      return
-
     runner = test.runner.get_runner()
 
     with runner.get_tor_controller() as controller:
@@ -906,10 +891,8 @@ class TestController(unittest.TestCase):
       self.assertRaises(stem.InvalidArguments, controller.close_stream, 'blarg')
 
   @require_controller
+  @require_online
   def test_mapaddress(self):
-    if test.runner.require_online(self):
-      return
-
     runner = test.runner.get_runner()
 
     with runner.get_tor_controller() as controller:
@@ -946,15 +929,13 @@ class TestController(unittest.TestCase):
       self.assertTrue(stem.util.connection.is_valid_ipv4_address(stem.util.str_tools._to_unicode(ip_addr)))
 
   @require_controller
+  @require_online
   @require_version(Requirement.MICRODESCRIPTOR_IS_DEFAULT)
   def test_get_microdescriptor(self):
     """
     Basic checks for get_microdescriptor().
     """
 
-    if test.runner.require_online(self):
-      return
-
     with test.runner.get_runner().get_tor_controller() as controller:
       # we should balk at invalid content
       self.assertRaises(ValueError, controller.get_microdescriptor, '')
@@ -1052,14 +1033,12 @@ class TestController(unittest.TestCase):
           break
 
   @require_controller
+  @require_online
   def test_get_network_status(self):
     """
     Basic checks for get_network_status().
     """
 
-    if test.runner.require_online(self):
-      return
-
     with test.runner.get_runner().get_tor_controller() as controller:
       # we should balk at invalid content
       self.assertRaises(ValueError, controller.get_network_status, '')
@@ -1078,6 +1057,7 @@ class TestController(unittest.TestCase):
       self.assertEqual(desc_by_fingerprint, desc_by_nickname)
 
   @require_controller
+  @require_online
   def test_get_network_statuses(self):
     """
     Fetches a few descriptors via the get_network_statuses() method.
@@ -1085,9 +1065,6 @@ class TestController(unittest.TestCase):
 
     runner = test.runner.get_runner()
 
-    if test.runner.require_online(self):
-      return
-
     with runner.get_tor_controller() as controller:
       count = 0
 
@@ -1105,11 +1082,9 @@ class TestController(unittest.TestCase):
           break
 
   @require_controller
+  @require_online
   @require_version(Requirement.EXTENDCIRCUIT_PATH_OPTIONAL)
   def test_attachstream(self):
-    if test.runner.require_online(self):
-      return
-
     host = socket.gethostbyname('www.torproject.org')
     port = 80
 
@@ -1148,15 +1123,13 @@ class TestController(unittest.TestCase):
     self.assertEqual(our_stream.circ_id, circuit_id)
 
   @require_controller
+  @require_online
   @require_version(Requirement.EXTENDCIRCUIT_PATH_OPTIONAL)
   def test_get_circuits(self):
     """
     Fetches circuits via the get_circuits() method.
     """
 
-    if test.runner.require_online(self):
-      return
-
     with test.runner.get_runner().get_tor_controller() as controller:
       new_circ = controller.new_circuit()
       circuits = controller.get_circuits()
diff --git a/test/integ/descriptor/remote.py b/test/integ/descriptor/remote.py
index 9450c0e..cc7d4cb 100644
--- a/test/integ/descriptor/remote.py
+++ b/test/integ/descriptor/remote.py
@@ -13,17 +13,18 @@ import stem.descriptor.router_status_entry
 import stem.descriptor.server_descriptor
 import test.runner
 
+from test.runner import require_online
+
 
 class TestDescriptorDownloader(unittest.TestCase):
+  @require_online
   def test_authorities_are_up_to_date(self):
     """
     Check that our hardcoded directory authority data matches the present
     consensus.
     """
 
-    if test.runner.require_online(self):
-      return
-    elif test.runner.only_run_once(self):
+    if test.runner.only_run_once(self):
       return
 
     downloader = stem.descriptor.remote.DescriptorDownloader()
@@ -42,6 +43,7 @@ class TestDescriptorDownloader(unittest.TestCase):
         if getattr(auth, attr) != getattr(stem_auth, attr):
           self.fail("%s has %s %s, but we expected %s" % (auth.nickname, attr, getattr(auth, attr), getattr(stem_auth, attr)))
 
+  @require_online
   def test_using_authorities(self):
     """
     Fetches a descriptor from each of the directory authorities. This is
@@ -52,9 +54,7 @@ class TestDescriptorDownloader(unittest.TestCase):
     then this test will need to be updated.
     """
 
-    if test.runner.require_online(self):
-      return
-    elif test.runner.only_run_once(self):
+    if test.runner.only_run_once(self):
       return
 
     queries = []
@@ -77,28 +77,26 @@ class TestDescriptorDownloader(unittest.TestCase):
       self.assertEqual(1, len(descriptors))
       self.assertEqual('moria1', descriptors[0].nickname)
 
+  @require_online
   def test_use_directory_mirrors(self):
     """
     Checks that we can fetch and use a list of directory mirrors.
     """
 
-    if test.runner.require_online(self):
-      return
-    elif test.runner.only_run_once(self):
+    if test.runner.only_run_once(self):
       return
 
     downloader = stem.descriptor.remote.DescriptorDownloader()
     downloader.use_directory_mirrors()
     self.assertTrue(len(downloader._endpoints) > 50)
 
+  @require_online
   def test_get_server_descriptors(self):
     """
     Exercises the downloader's get_server_descriptors() method.
     """
 
-    if test.runner.require_online(self):
-      return
-    elif test.runner.only_run_once(self):
+    if test.runner.only_run_once(self):
       return
 
     downloader = stem.descriptor.remote.DescriptorDownloader(validate = True)
@@ -127,14 +125,13 @@ class TestDescriptorDownloader(unittest.TestCase):
 
     self.assertEqual(2, len(list(multiple_query)))
 
+  @require_online
   def test_get_extrainfo_descriptors(self):
     """
     Exercises the downloader's get_extrainfo_descriptors() method.
     """
 
-    if test.runner.require_online(self):
-      return
-    elif test.runner.only_run_once(self):
+    if test.runner.only_run_once(self):
       return
 
     downloader = stem.descriptor.remote.DescriptorDownloader(validate = True)
@@ -156,6 +153,7 @@ class TestDescriptorDownloader(unittest.TestCase):
 
     self.assertEqual(2, len(list(multiple_query)))
 
+  @require_online
   def test_get_microdescriptors(self):
     """
     Exercises the downloader's get_microdescriptors() method.
@@ -166,9 +164,7 @@ class TestDescriptorDownloader(unittest.TestCase):
     test.runner.skip(self, '(test presently broken)')
     return
 
-    if test.runner.require_online(self):
-      return
-    elif test.runner.only_run_once(self):
+    if test.runner.only_run_once(self):
       return
 
     downloader = stem.descriptor.remote.DescriptorDownloader(validate = True)
@@ -190,14 +186,13 @@ class TestDescriptorDownloader(unittest.TestCase):
 
     self.assertEqual(2, len(list(multiple_query)))
 
+  @require_online
   def test_get_consensus(self):
     """
     Exercises the downloader's get_consensus() method.
     """
 
-    if test.runner.require_online(self):
-      return
-    elif test.runner.only_run_once(self):
+    if test.runner.only_run_once(self):
       return
 
     downloader = stem.descriptor.remote.DescriptorDownloader(validate = True)
@@ -209,14 +204,13 @@ class TestDescriptorDownloader(unittest.TestCase):
     self.assertTrue(len(consensus) > 50)
     self.assertTrue(isinstance(consensus[0], stem.descriptor.router_status_entry.RouterStatusEntryV3))
 
+  @require_online
   def test_get_key_certificates(self):
     """
     Exercises the downloader's get_key_certificates() method.
     """
 
-    if test.runner.require_online(self):
-      return
-    elif test.runner.only_run_once(self):
+    if test.runner.only_run_once(self):
       return
 
     downloader = stem.descriptor.remote.DescriptorDownloader(validate = True)
diff --git a/test/runner.py b/test/runner.py
index 6d637de..ecf5fb6 100644
--- a/test/runner.py
+++ b/test/runner.py
@@ -150,19 +150,19 @@ def require_version(req_version):
   return decorator
 
 
-def require_online(test_case):
+def require_online(func):
   """
   Skips the test if we weren't started with the ONLINE target, which indicates
   that tests requiring network connectivity should run.
-
-  :param unittest.TestCase test_case: test being ran
-
-  :returns: True if test should be skipped, False otherwise
   """
 
-  if Target.ONLINE not in get_runner().attribute_targets:
-    skip(test_case, '(requires online target)')
-    return True
+  def wrapped(self, *args, **kwargs):
+    if Target.ONLINE in get_runner().attribute_targets:
+      return func(self, *args, **kwargs)
+    else:
+      skip(self, '(requires online target)')
+
+  return wrapped
 
 
 def only_run_once(test_case):



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