[tor-commits] [stem/master] Moving integ runner into testing base

atagar at torproject.org atagar at torproject.org
Mon Oct 17 02:33:38 UTC 2011


commit 24d4881025c891c829a63de9b9b1b8849c8c4faf
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Oct 16 19:30:57 2011 -0700

    Moving integ runner into testing base
    
    Moving the integ runner module from 'test/integ/runner.py' to 'test/runner.py'.
    The 'test/unit/*' and 'test/integ/*' are for test cases, and will later be
    kinda crowded. The runner is special since it doesn't test, but rather provides
    the runtime context for the integ tests so probably best to keep this separate
    from the rest.
---
 run_tests.py           |    4 +-
 test/__init__.py       |    2 +-
 test/integ/__init__.py |    2 +-
 test/integ/runner.py   |  167 ------------------------------------------------
 test/integ/system.py   |    4 +-
 test/runner.py         |  167 ++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 173 insertions(+), 173 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index 284f48c..6d62ab3 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -8,9 +8,9 @@ import sys
 import time
 import getopt
 import unittest
+import test.runner
 import test.unit.message
 import test.unit.version
-import test.integ.runner
 import test.integ.system
 
 from stem.util import enum, term
@@ -113,7 +113,7 @@ if __name__ == '__main__':
   if run_integ_tests:
     print "%s\n%s\n%s\n" % (DIVIDER, "INTEGRATION TESTS".center(70), DIVIDER)
     
-    integ_runner = test.integ.runner.get_runner()
+    integ_runner = test.runner.get_runner()
     
     try:
       integ_runner.run_setup()
diff --git a/test/__init__.py b/test/__init__.py
index 031299c..9afce0a 100644
--- a/test/__init__.py
+++ b/test/__init__.py
@@ -2,5 +2,5 @@
 Unit and integration tests for the stem library.
 """
 
-__all__ = []
+__all__ = ["runner"]
 
diff --git a/test/integ/__init__.py b/test/integ/__init__.py
index ecfc9cd..d6d7b31 100644
--- a/test/integ/__init__.py
+++ b/test/integ/__init__.py
@@ -2,5 +2,5 @@
 Integration tests for the stem library.
 """
 
-__all__ = ["runner"]
+__all__ = ["system"]
 
diff --git a/test/integ/runner.py b/test/integ/runner.py
deleted file mode 100644
index c83fd2d..0000000
--- a/test/integ/runner.py
+++ /dev/null
@@ -1,167 +0,0 @@
-"""
-Starts and stops test instances for integration tests.
-"""
-
-import os
-import sys
-import time
-import signal
-import tempfile
-import subprocess
-
-from stem.util import term
-
-# number of seconds before we time out our attempt to start a tor instance
-TOR_INIT_TIMEOUT = 60
-
-BASIC_TORRC = """# configuration for stem integration tests
-DataDirectory %s
-ControlPort 1111
-"""
-
-# singleton Runner instance
-INTEG_RUNNER = None
-
-def get_runner():
-  """
-  Singleton for the runtime context of integration tests.
-  """
-  
-  global INTEG_RUNNER
-  if not INTEG_RUNNER: INTEG_RUNNER = Runner()
-  return INTEG_RUNNER
-
-class Runner:
-  def __init__(self):
-    self._test_dir = tempfile.mktemp("-stem-integ")
-    self._torrc_contents = BASIC_TORRC % self._test_dir
-    self._tor_process = None
-  
-  def run_setup(self):
-    """
-    Makes a temporary directory for the runtime resources of our integ tests.
-    
-    Raises:
-      OSError if unsuccessful
-    """
-    
-    print term.format("Setting up a test instance...", term.Color.BLUE, term.Attr.BOLD)
-    
-    # makes a temporary directory for the runtime resources of our integ test
-    try:
-      sys.stdout.write(term.format("  making test directory (%s)... " % self._test_dir, term.Color.BLUE, term.Attr.BOLD))
-      os.makedirs(self._test_dir)
-      sys.stdout.write(term.format("done\n", term.Color.BLUE, term.Attr.BOLD))
-    except OSError, exc:
-      sys.stdout.write(term.format("failed (%s)\n" % exc, term.Color.RED, term.Attr.BOLD))
-      raise exc
-    
-    # writes our testing torrc
-    torrc_dst = os.path.join(self._test_dir, "torrc")
-    try:
-      sys.stdout.write(term.format("  writing torrc (%s)... " % torrc_dst, term.Color.BLUE, term.Attr.BOLD))
-      
-      torrc_file = open(torrc_dst, "w")
-      torrc_file.write(self._torrc_contents)
-      torrc_file.close()
-      
-      sys.stdout.write(term.format("done\n", term.Color.BLUE, term.Attr.BOLD))
-      
-      for line in self._torrc_contents.strip().split("\n"):
-        print term.format("    %s" % line.strip(), term.Color.BLUE)
-    except Exception, exc:
-      sys.stdout.write(term.format("failed (%s)\n" % exc, term.Color.RED, term.Attr.BOLD))
-      raise exc
-    finally:
-      print # extra newline
-  
-  def start(self):
-    """
-    Initializes a tor process. This blocks until initialization completes or we
-    error out.
-    
-    Raises:
-      OSError if we either fail to create the tor process or reached a timeout
-      without success
-    """
-    
-    print term.format("Starting tor...", term.Color.BLUE, term.Attr.BOLD)
-    start_time = time.time()
-    
-    try:
-      # terminate our previous instance before continuing if we had one
-      if self._tor_process: self._tor_process.kill()
-      
-      # double check that we have a torrc to work with
-      torrc_dst = os.path.join(self._test_dir, "torrc")
-      if not os.path.exists(torrc_dst):
-        raise OSError("torrc doesn't exist (%s)" % torrc_dst)
-      
-      # starts a tor subprocess, raising an OSError if it fails
-      self._tor_process = subprocess.Popen(["tor", "-f", torrc_dst], stdout = subprocess.PIPE, stderr = subprocess.PIPE)
-      
-      # time ourselves out if we reach TOR_INIT_TIMEOUT
-      def timeout_handler(signum, frame):
-        # terminates the uninitialized tor process and raise on timeout
-        self._tor_process.kill()
-        raise OSError("reached a %i second timeout without success" % TOR_INIT_TIMEOUT)
-      
-      signal.signal(signal.SIGALRM, timeout_handler)
-      signal.alarm(TOR_INIT_TIMEOUT)
-      
-      while True:
-        init_line = self._tor_process.stdout.readline().strip()
-        
-        # this will provide empty results if the process is terminated
-        if not init_line:
-          self._tor_process.kill() # ... but best make sure
-          raise OSError("process terminated")
-        
-        print term.format("  %s" % init_line, term.Color.BLUE)
-        
-        # return the process if we're done with bootstrapping
-        if init_line.endswith("Bootstrapped 100%: Done."):
-          print term.format("  done (%i seconds)" % (time.time() - start_time), term.Color.BLUE, term.Attr.BOLD)
-          return
-    except OSError, exc:
-      print term.format("  failed to start tor: %s" % exc, term.Color.RED, term.Attr.BOLD)
-      raise exc
-    finally:
-      print # extra newline
-  
-  def stop(self):
-    """
-    Terminates our tor instance.
-    """
-    
-    if self._tor_process:
-      sys.stdout.write(term.format("Shutting down tor... ", term.Color.BLUE, term.Attr.BOLD))
-      self._tor_process.kill()
-      self._tor_process.communicate() # blocks until the process is done
-      self._tor_process = None
-      sys.stdout.write(term.format("done\n", term.Color.BLUE, term.Attr.BOLD))
-  
-  def get_pid(self):
-    """
-    Provides the process id of tor.
-    
-    Returns:
-      int pid for the tor process, or None if it isn't running
-    """
-    
-    if self._tor_process:
-      return self._tor_process.pid
-    else: return None
-  
-  def get_control_port(self):
-    """
-    Provides the control port tor is running with.
-    
-    Returns:
-      int for the port tor's controller interface is bound to, None if it
-      doesn't have one
-    """
-    
-    # TODO: this will be fetched from torrc contents when we use custom configs
-    return 1111
-
diff --git a/test/integ/system.py b/test/integ/system.py
index edc0291..8661660 100644
--- a/test/integ/system.py
+++ b/test/integ/system.py
@@ -3,7 +3,7 @@ Unit tests for the util.system functions in the context of a tor process.
 """
 
 import unittest
-import test.integ.runner
+import test.runner
 
 from stem.util import system
 
@@ -36,7 +36,7 @@ class TestSystemFunctions(unittest.TestCase):
     Checks the util.system.get_pid function.
     """
     
-    runner = test.integ.runner.get_runner()
+    runner = test.runner.get_runner()
     self.assertEquals(runner.get_pid(), system.get_pid("tor"))
     self.assertEquals(runner.get_pid(), system.get_pid("tor", runner.get_control_port()))
     self.assertEquals(None, system.get_pid("blarg_and_stuff"))
diff --git a/test/runner.py b/test/runner.py
new file mode 100644
index 0000000..b3abdfa
--- /dev/null
+++ b/test/runner.py
@@ -0,0 +1,167 @@
+"""
+Runtime context for the integration tests.
+"""
+
+import os
+import sys
+import time
+import signal
+import tempfile
+import subprocess
+
+from stem.util import term
+
+# number of seconds before we time out our attempt to start a tor instance
+TOR_INIT_TIMEOUT = 60
+
+BASIC_TORRC = """# configuration for stem integration tests
+DataDirectory %s
+ControlPort 1111
+"""
+
+# singleton Runner instance
+INTEG_RUNNER = None
+
+def get_runner():
+  """
+  Singleton for the runtime context of integration tests.
+  """
+  
+  global INTEG_RUNNER
+  if not INTEG_RUNNER: INTEG_RUNNER = Runner()
+  return INTEG_RUNNER
+
+class Runner:
+  def __init__(self):
+    self._test_dir = tempfile.mktemp("-stem-integ")
+    self._torrc_contents = BASIC_TORRC % self._test_dir
+    self._tor_process = None
+  
+  def run_setup(self):
+    """
+    Makes a temporary directory for the runtime resources of our integ tests.
+    
+    Raises:
+      OSError if unsuccessful
+    """
+    
+    print term.format("Setting up a test instance...", term.Color.BLUE, term.Attr.BOLD)
+    
+    # makes a temporary directory for the runtime resources of our integ test
+    try:
+      sys.stdout.write(term.format("  making test directory (%s)... " % self._test_dir, term.Color.BLUE, term.Attr.BOLD))
+      os.makedirs(self._test_dir)
+      sys.stdout.write(term.format("done\n", term.Color.BLUE, term.Attr.BOLD))
+    except OSError, exc:
+      sys.stdout.write(term.format("failed (%s)\n" % exc, term.Color.RED, term.Attr.BOLD))
+      raise exc
+    
+    # writes our testing torrc
+    torrc_dst = os.path.join(self._test_dir, "torrc")
+    try:
+      sys.stdout.write(term.format("  writing torrc (%s)... " % torrc_dst, term.Color.BLUE, term.Attr.BOLD))
+      
+      torrc_file = open(torrc_dst, "w")
+      torrc_file.write(self._torrc_contents)
+      torrc_file.close()
+      
+      sys.stdout.write(term.format("done\n", term.Color.BLUE, term.Attr.BOLD))
+      
+      for line in self._torrc_contents.strip().split("\n"):
+        print term.format("    %s" % line.strip(), term.Color.BLUE)
+    except Exception, exc:
+      sys.stdout.write(term.format("failed (%s)\n" % exc, term.Color.RED, term.Attr.BOLD))
+      raise exc
+    finally:
+      print # extra newline
+  
+  def start(self):
+    """
+    Initializes a tor process. This blocks until initialization completes or we
+    error out.
+    
+    Raises:
+      OSError if we either fail to create the tor process or reached a timeout
+      without success
+    """
+    
+    print term.format("Starting tor...", term.Color.BLUE, term.Attr.BOLD)
+    start_time = time.time()
+    
+    try:
+      # terminate our previous instance before continuing if we had one
+      if self._tor_process: self._tor_process.kill()
+      
+      # double check that we have a torrc to work with
+      torrc_dst = os.path.join(self._test_dir, "torrc")
+      if not os.path.exists(torrc_dst):
+        raise OSError("torrc doesn't exist (%s)" % torrc_dst)
+      
+      # starts a tor subprocess, raising an OSError if it fails
+      self._tor_process = subprocess.Popen(["tor", "-f", torrc_dst], stdout = subprocess.PIPE, stderr = subprocess.PIPE)
+      
+      # time ourselves out if we reach TOR_INIT_TIMEOUT
+      def timeout_handler(signum, frame):
+        # terminates the uninitialized tor process and raise on timeout
+        self._tor_process.kill()
+        raise OSError("reached a %i second timeout without success" % TOR_INIT_TIMEOUT)
+      
+      signal.signal(signal.SIGALRM, timeout_handler)
+      signal.alarm(TOR_INIT_TIMEOUT)
+      
+      while True:
+        init_line = self._tor_process.stdout.readline().strip()
+        
+        # this will provide empty results if the process is terminated
+        if not init_line:
+          self._tor_process.kill() # ... but best make sure
+          raise OSError("process terminated")
+        
+        print term.format("  %s" % init_line, term.Color.BLUE)
+        
+        # return the process if we're done with bootstrapping
+        if init_line.endswith("Bootstrapped 100%: Done."):
+          print term.format("  done (%i seconds)" % (time.time() - start_time), term.Color.BLUE, term.Attr.BOLD)
+          return
+    except OSError, exc:
+      print term.format("  failed to start tor: %s" % exc, term.Color.RED, term.Attr.BOLD)
+      raise exc
+    finally:
+      print # extra newline
+  
+  def stop(self):
+    """
+    Terminates our tor instance.
+    """
+    
+    if self._tor_process:
+      sys.stdout.write(term.format("Shutting down tor... ", term.Color.BLUE, term.Attr.BOLD))
+      self._tor_process.kill()
+      self._tor_process.communicate() # blocks until the process is done
+      self._tor_process = None
+      sys.stdout.write(term.format("done\n", term.Color.BLUE, term.Attr.BOLD))
+  
+  def get_pid(self):
+    """
+    Provides the process id of tor.
+    
+    Returns:
+      int pid for the tor process, or None if it isn't running
+    """
+    
+    if self._tor_process:
+      return self._tor_process.pid
+    else: return None
+  
+  def get_control_port(self):
+    """
+    Provides the control port tor is running with.
+    
+    Returns:
+      int for the port tor's controller interface is bound to, None if it
+      doesn't have one
+    """
+    
+    # TODO: this will be fetched from torrc contents when we use custom configs
+    return 1111
+



More information about the tor-commits mailing list