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

atagar at torproject.org atagar at torproject.org
Sat Feb 21 22:08:00 UTC 2015


commit da657a915f00456be38b0624275d3150d0a0ffd6
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat Feb 21 13:30:43 2015 -0800

    @require_version decorator
---
 test/integ/control/controller.py     |   37 +++++++++++++---------------------
 test/integ/process.py                |   11 +++++-----
 test/integ/socket/control_message.py |    9 +++++----
 test/runner.py                       |   18 ++++++++++-------
 4 files changed, 36 insertions(+), 39 deletions(-)

diff --git a/test/integ/control/controller.py b/test/integ/control/controller.py
index 632c002..2a7bc69 100644
--- a/test/integ/control/controller.py
+++ b/test/integ/control/controller.py
@@ -26,7 +26,10 @@ from stem.control import EventType, Listener, State
 from stem.exit_policy import ExitPolicy
 from stem.version import Requirement
 
-from test.runner import require_controller
+from test.runner import (
+  require_controller,
+  require_version,
+)
 
 # Router status entry for a relay with a nickname other than 'Unnamed'. This is
 # used for a few tests that need to look up a relay.
@@ -62,14 +65,12 @@ class TestController(unittest.TestCase):
       self.assertRaises(stem.SocketError, stem.control.Controller.from_socket_file, test.runner.CONTROL_SOCKET_PATH)
 
   @require_controller
+  @require_version(Requirement.EVENT_SIGNAL)
   def test_reset_notification(self):
     """
     Checks that a notificiation listener is... well, notified of SIGHUPs.
     """
 
-    if test.runner.require_version(self, stem.version.Requirement.EVENT_SIGNAL):
-      return
-
     with test.runner.get_runner().get_tor_controller() as controller:
       received_events = []
 
@@ -599,14 +600,12 @@ class TestController(unittest.TestCase):
         shutil.rmtree(tmpdir)
 
   @require_controller
+  @require_version(Requirement.LOADCONF)
   def test_loadconf(self):
     """
     Exercises Controller.load_conf with valid and invalid requests.
     """
 
-    if test.runner.require_version(self, stem.version.Requirement.LOADCONF):
-      return
-
     runner = test.runner.get_runner()
 
     with runner.get_tor_controller() as controller:
@@ -710,6 +709,7 @@ class TestController(unittest.TestCase):
       self.assertEqual([('127.0.0.1', 1112)], controller.get_socks_listeners())
 
   @require_controller
+  @require_version(stem.version.Version('0.1.2.2-alpha'))
   def test_enable_feature(self):
     """
     Test Controller.enable_feature with valid and invalid inputs.
@@ -718,9 +718,6 @@ class TestController(unittest.TestCase):
     runner = test.runner.get_runner()
 
     with runner.get_tor_controller() as controller:
-      if not test.runner.require_version(self, stem.version.Version('0.1.2.2-alpha')):
-        controller.enable_feature('VERBOSE_NAMES')
-
       self.assertTrue(controller.is_feature_enabled('VERBOSE_NAMES'))
 
       orconn_output = controller.get_info('orconn-status')
@@ -769,11 +766,10 @@ class TestController(unittest.TestCase):
       self.assertTrue(controller.get_newnym_wait() > 9.0)
 
   @require_controller
+  @require_version(Requirement.EXTENDCIRCUIT_PATH_OPTIONAL)
   def test_extendcircuit(self):
     if test.runner.require_online(self):
       return
-    elif test.runner.require_version(self, Requirement.EXTENDCIRCUIT_PATH_OPTIONAL):
-      return
 
     with test.runner.get_runner().get_tor_controller() as controller:
       circuit_id = controller.extend_circuit('0')
@@ -788,6 +784,7 @@ class TestController(unittest.TestCase):
       self.assertRaises(stem.InvalidRequest, controller.extend_circuit, '0', 'thisroutershouldntexistbecausestemexists!@##$%#', 'foo')
 
   @require_controller
+  @require_version(Requirement.EXTENDCIRCUIT_PATH_OPTIONAL)
   def test_repurpose_circuit(self):
     """
     Tests Controller.repurpose_circuit with valid and invalid input.
@@ -795,8 +792,6 @@ class TestController(unittest.TestCase):
 
     if test.runner.require_online(self):
       return
-    elif test.runner.require_version(self, Requirement.EXTENDCIRCUIT_PATH_OPTIONAL):
-      return
 
     runner = test.runner.get_runner()
 
@@ -814,6 +809,7 @@ class TestController(unittest.TestCase):
       self.assertRaises(stem.InvalidRequest, controller.repurpose_circuit, '4', 'fooo')
 
   @require_controller
+  @require_version(Requirement.EXTENDCIRCUIT_PATH_OPTIONAL)
   def test_close_circuit(self):
     """
     Tests Controller.close_circuit with valid and invalid input.
@@ -821,8 +817,6 @@ class TestController(unittest.TestCase):
 
     if test.runner.require_online(self):
       return
-    elif test.runner.require_version(self, Requirement.EXTENDCIRCUIT_PATH_OPTIONAL):
-      return
 
     runner = test.runner.get_runner()
 
@@ -952,14 +946,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_version(Requirement.MICRODESCRIPTOR_IS_DEFAULT)
   def test_get_microdescriptor(self):
     """
     Basic checks for get_microdescriptor().
     """
 
-    if test.runner.require_version(self, Requirement.MICRODESCRIPTOR_IS_DEFAULT):
-      return
-    elif test.runner.require_online(self):
+    if test.runner.require_online(self):
       return
 
     with test.runner.get_runner().get_tor_controller() as controller:
@@ -1112,11 +1105,10 @@ class TestController(unittest.TestCase):
           break
 
   @require_controller
+  @require_version(Requirement.EXTENDCIRCUIT_PATH_OPTIONAL)
   def test_attachstream(self):
     if test.runner.require_online(self):
       return
-    elif test.runner.require_version(self, Requirement.EXTENDCIRCUIT_PATH_OPTIONAL):
-      return
 
     host = socket.gethostbyname('www.torproject.org')
     port = 80
@@ -1156,6 +1148,7 @@ class TestController(unittest.TestCase):
     self.assertEqual(our_stream.circ_id, circuit_id)
 
   @require_controller
+  @require_version(Requirement.EXTENDCIRCUIT_PATH_OPTIONAL)
   def test_get_circuits(self):
     """
     Fetches circuits via the get_circuits() method.
@@ -1163,8 +1156,6 @@ class TestController(unittest.TestCase):
 
     if test.runner.require_online(self):
       return
-    elif test.runner.require_version(self, Requirement.EXTENDCIRCUIT_PATH_OPTIONAL):
-      return
 
     with test.runner.get_runner().get_tor_controller() as controller:
       new_circ = controller.new_circuit()
diff --git a/test/integ/process.py b/test/integ/process.py
index 7b10828..f611191 100644
--- a/test/integ/process.py
+++ b/test/integ/process.py
@@ -21,7 +21,10 @@ import stem.util.tor_tools
 import stem.version
 import test.runner
 
-from test.runner import require_controller
+from test.runner import (
+  require_controller,
+  require_version,
+)
 
 try:
   # added in python 3.3
@@ -328,6 +331,7 @@ class TestProcess(unittest.TestCase):
     if not (runtime > 2 and runtime < 3):
       self.fail('Test should have taken 2-3 seconds, took %i instead' % runtime)
 
+  @require_version(stem.version.Requirement.TAKEOWNERSHIP)
   @patch('os.getpid')
   def test_take_ownership_via_pid(self, getpid_mock):
     """
@@ -340,8 +344,6 @@ class TestProcess(unittest.TestCase):
       return
     elif test.runner.only_run_once(self):
       return
-    elif test.runner.require_version(self, stem.version.Requirement.TAKEOWNERSHIP):
-      return
 
     sleep_process = subprocess.Popen(['sleep', '60'])
     getpid_mock.return_value = str(sleep_process.pid)
@@ -373,6 +375,7 @@ class TestProcess(unittest.TestCase):
 
     self.fail("tor didn't quit after the process that owned it terminated")
 
+  @require_version(stem.version.Requirement.TAKEOWNERSHIP)
   def test_take_ownership_via_controller(self):
     """
     Checks that the tor process quits after the controller that owns it
@@ -381,8 +384,6 @@ class TestProcess(unittest.TestCase):
 
     if test.runner.only_run_once(self):
       return
-    elif test.runner.require_version(self, stem.version.Requirement.TAKEOWNERSHIP):
-      return
 
     tor_process = stem.process.launch_tor_with_config(
       tor_cmd = test.runner.get_runner().get_tor_command(),
diff --git a/test/integ/socket/control_message.py b/test/integ/socket/control_message.py
index e9faf84..567a78d 100644
--- a/test/integ/socket/control_message.py
+++ b/test/integ/socket/control_message.py
@@ -9,7 +9,10 @@ import stem.socket
 import stem.version
 import test.runner
 
-from test.runner import require_controller
+from test.runner import (
+  require_controller,
+  require_version,
+)
 
 
 class TestControlMessage(unittest.TestCase):
@@ -100,14 +103,12 @@ class TestControlMessage(unittest.TestCase):
       self.assertEqual([('250', '-', 'config-file=%s' % torrc_dst), ('250', ' ', 'OK')], config_file_response.content())
 
   @require_controller
+  @require_version(stem.version.Requirement.GETINFO_CONFIG_TEXT)
   def test_getinfo_config_text(self):
     """
     Parses the 'GETINFO config-text' response.
     """
 
-    if test.runner.require_version(self, stem.version.Requirement.GETINFO_CONFIG_TEXT):
-      return
-
     runner = test.runner.get_runner()
 
     # We can't be certain of the order, and there may be extra config-text
diff --git a/test/runner.py b/test/runner.py
index 2f9fbf1..6d637de 100644
--- a/test/runner.py
+++ b/test/runner.py
@@ -131,19 +131,23 @@ def require_controller(func):
   return wrapped
 
 
-def require_version(test_case, req_version):
+def require_version(req_version):
   """
   Skips the test unless we meet the required version.
 
-  :param unittest.TestCase test_case: test being ran
   :param stem.version.Version req_version: required tor version for the test
-
-  :returns: True if test should be skipped, False otherwise
   """
 
-  if get_runner().get_tor_version() < req_version:
-    skip(test_case, '(requires %s)' % req_version)
-    return True
+  def decorator(func):
+    def wrapped(self, *args, **kwargs):
+      if get_runner().get_tor_version() >= req_version:
+        return func(self, *args, **kwargs)
+      else:
+        skip(self, '(requires %s)' % req_version)
+
+    return wrapped
+
+  return decorator
 
 
 def require_online(test_case):





More information about the tor-commits mailing list