[tor-commits] [stem/master] Invoke asynchronous test's run_tests dynamically

atagar at torproject.org atagar at torproject.org
Sat Jun 17 18:18:16 UTC 2017


commit ce50d1de2771939058e120db977a649e231190f6
Author: Damian Johnson <atagar at torproject.org>
Date:   Tue Jun 13 19:59:18 2017 -0700

    Invoke asynchronous test's run_tests dynamically
    
    For any module using @asynchronous there's a run_tests() function to start
    them. Rather than hardcoding all these calls doing so dynamically.
---
 run_tests.py                                  | 31 +++++++++------------------
 test/__init__.py                              |  2 ++
 test/integ/descriptor/extrainfo_descriptor.py |  4 ++--
 test/integ/descriptor/microdescriptor.py      |  4 ++--
 test/integ/descriptor/networkstatus.py        |  6 +++---
 test/integ/descriptor/server_descriptor.py    |  4 ++--
 test/integ/installation.py                    |  2 +-
 test/integ/process.py                         |  4 ++--
 8 files changed, 24 insertions(+), 33 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index 58f9ffa..22a0229 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -6,6 +6,7 @@
 Runs unit and integration tests. For usage information run this with '--help'.
 """
 
+import importlib
 import os
 import sys
 import threading
@@ -27,12 +28,6 @@ import stem.version
 
 import test
 import test.arguments
-import test.integ.descriptor.extrainfo_descriptor
-import test.integ.descriptor.microdescriptor
-import test.integ.descriptor.networkstatus
-import test.integ.descriptor.server_descriptor
-import test.integ.installation
-import test.integ.process
 import test.output
 import test.runner
 import test.task
@@ -234,24 +229,18 @@ def main():
 
   if args.run_integ:
     default_test_dir = stem.util.system.expand_path(CONFIG['integ.test_directory'], test.STEM_BASE)
+    async_args = test.AsyncTestArgs(default_test_dir, args.tor_path)
 
-    if not args.specific_test or 'test.integ.descriptor.extrainfo_descriptor'.startswith(args.specific_test):
-      test.integ.descriptor.extrainfo_descriptor.TestExtraInfoDescriptor.run_tests(default_test_dir)
+    for module_str in stem.util.test_tools.ASYNC_TESTS:
+      if not args.specific_test or module_str.startswith(args.specific_test):
+        module = importlib.import_module(module_str.rsplit('.', 1)[0])
+        test_classes = [v for k, v in module.__dict__.items() if k.startswith('Test')]
 
-    if not args.specific_test or 'test.integ.descriptor.microdescriptor'.startswith(args.specific_test):
-      test.integ.descriptor.microdescriptor.TestMicrodescriptor.run_tests(default_test_dir)
+        if len(test_classes) != 1:
+          print('BUG: Detected multiple tests for %s: %s' % (module_str, ', '.join(test_classes)))
+          sys.exit(1)
 
-    if not args.specific_test or 'test.integ.descriptor.networkstatus'.startswith(args.specific_test):
-      test.integ.descriptor.networkstatus.TestNetworkStatus.run_tests(default_test_dir)
-
-    if not args.specific_test or 'test.integ.descriptor.server_descriptor'.startswith(args.specific_test):
-      test.integ.descriptor.server_descriptor.TestServerDescriptor.run_tests(default_test_dir)
-
-    if not args.specific_test or 'test.integ.installation'.startswith(args.specific_test):
-      test.integ.installation.TestInstallation.run_tests()
-
-    if not args.specific_test or 'test.integ.process'.startswith(args.specific_test):
-      test.integ.process.TestProcess.run_tests(args.tor_path)
+        test_classes[0].run_tests(async_args)
 
   if args.run_unit:
     test.output.print_divider('UNIT TESTS', True)
diff --git a/test/__init__.py b/test/__init__.py
index 8b5d576..5a4c6e9 100644
--- a/test/__init__.py
+++ b/test/__init__.py
@@ -13,6 +13,7 @@ Unit and integration tests for the stem library. Helpers include...
   tor_version - provides the version of tor we're testing against
 """
 
+import collections
 import itertools
 import os
 
@@ -50,6 +51,7 @@ Target = stem.util.enum.UppercaseEnum(
   'RUN_ALL',
 )
 
+AsyncTestArgs = collections.namedtuple('AsyncTestArgs', ['test_dir', 'tor_cmd'])
 TOR_VERSION = None
 
 # We make some paths relative to stem's base directory (the one above us)
diff --git a/test/integ/descriptor/extrainfo_descriptor.py b/test/integ/descriptor/extrainfo_descriptor.py
index 0e13435..c0a4d30 100644
--- a/test/integ/descriptor/extrainfo_descriptor.py
+++ b/test/integ/descriptor/extrainfo_descriptor.py
@@ -14,8 +14,8 @@ from stem.util.test_tools import asynchronous
 
 class TestExtraInfoDescriptor(unittest.TestCase):
   @staticmethod
-  def run_tests(test_dir):
-    stem.util.test_tools.ASYNC_TESTS['test.integ.descriptor.extrainfo_descriptor.test_cached_descriptor'].run(test_dir, threaded = True)
+  def run_tests(args):
+    stem.util.test_tools.ASYNC_TESTS['test.integ.descriptor.extrainfo_descriptor.test_cached_descriptor'].run(args.test_dir, threaded = True)
 
   @asynchronous
   def test_cached_descriptor(test_dir):
diff --git a/test/integ/descriptor/microdescriptor.py b/test/integ/descriptor/microdescriptor.py
index 8987906..3f2b20c 100644
--- a/test/integ/descriptor/microdescriptor.py
+++ b/test/integ/descriptor/microdescriptor.py
@@ -14,8 +14,8 @@ from stem.util.test_tools import asynchronous
 
 class TestMicrodescriptor(unittest.TestCase):
   @staticmethod
-  def run_tests(test_dir):
-    stem.util.test_tools.ASYNC_TESTS['test.integ.descriptor.microdescriptor.test_cached_microdescriptors'].run(test_dir, threaded = True)
+  def run_tests(args):
+    stem.util.test_tools.ASYNC_TESTS['test.integ.descriptor.microdescriptor.test_cached_microdescriptors'].run(args.test_dir, threaded = True)
 
   @asynchronous
   def test_cached_microdescriptors(test_dir):
diff --git a/test/integ/descriptor/networkstatus.py b/test/integ/descriptor/networkstatus.py
index dee8e7a..81eea54 100644
--- a/test/integ/descriptor/networkstatus.py
+++ b/test/integ/descriptor/networkstatus.py
@@ -19,9 +19,9 @@ from stem.util.test_tools import asynchronous
 
 class TestNetworkStatus(unittest.TestCase):
   @staticmethod
-  def run_tests(test_dir):
-    stem.util.test_tools.ASYNC_TESTS['test.integ.descriptor.networkstatus.test_cached_consensus'].run(test_dir, threaded = True)
-    stem.util.test_tools.ASYNC_TESTS['test.integ.descriptor.networkstatus.test_cached_microdesc_consensus'].run(test_dir, threaded = True)
+  def run_tests(args):
+    stem.util.test_tools.ASYNC_TESTS['test.integ.descriptor.networkstatus.test_cached_consensus'].run(args.test_dir, threaded = True)
+    stem.util.test_tools.ASYNC_TESTS['test.integ.descriptor.networkstatus.test_cached_microdesc_consensus'].run(args.test_dir, threaded = True)
 
   @test.require.only_run_once
   @test.require.online
diff --git a/test/integ/descriptor/server_descriptor.py b/test/integ/descriptor/server_descriptor.py
index a4a78ac..a62ee8f 100644
--- a/test/integ/descriptor/server_descriptor.py
+++ b/test/integ/descriptor/server_descriptor.py
@@ -14,8 +14,8 @@ from stem.util.test_tools import asynchronous
 
 class TestServerDescriptor(unittest.TestCase):
   @staticmethod
-  def run_tests(test_dir):
-    stem.util.test_tools.ASYNC_TESTS['test.integ.descriptor.server_descriptor.test_cached_descriptor'].run(test_dir, threaded = True)
+  def run_tests(args):
+    stem.util.test_tools.ASYNC_TESTS['test.integ.descriptor.server_descriptor.test_cached_descriptor'].run(args.test_dir, threaded = True)
 
   @asynchronous
   def test_cached_descriptor(test_dir):
diff --git a/test/integ/installation.py b/test/integ/installation.py
index 82bc717..e52cc63 100644
--- a/test/integ/installation.py
+++ b/test/integ/installation.py
@@ -57,7 +57,7 @@ def _assert_has_all_files(path):
 
 class TestInstallation(unittest.TestCase):
   @staticmethod
-  def run_tests():
+  def run_tests(args):
     test_install = stem.util.test_tools.ASYNC_TESTS['test.integ.installation.test_install']
     test_install.run()
     stem.util.test_tools.ASYNC_TESTS['test.integ.installation.test_sdist'].run(test_install.pid())
diff --git a/test/integ/process.py b/test/integ/process.py
index 9ee96ad..6fb101c 100644
--- a/test/integ/process.py
+++ b/test/integ/process.py
@@ -103,10 +103,10 @@ def run_tor(tor_cmd, *args, **kwargs):
 
 class TestProcess(unittest.TestCase):
   @staticmethod
-  def run_tests(tor_cmd):
+  def run_tests(args):
     for func, async_test in stem.util.test_tools.ASYNC_TESTS.items():
       if func.startswith('test.integ.process.'):
-        async_test.run(tor_cmd)
+        async_test.run(args.tor_cmd)
 
   @asynchronous
   def test_version_argument(tor_cmd):





More information about the tor-commits mailing list