[tor-commits] [stem/master] Testing option for chroot setups

atagar at torproject.org atagar at torproject.org
Sun Apr 22 22:42:59 UTC 2012


commit 13f7ce34e735b2547f3ab669bed0ceb571624c0f
Author: Damian Johnson <atagar at torproject.org>
Date:   Thu Apr 19 19:14:42 2012 -0700

    Testing option for chroot setups
    
    Adding a new target to simulate a chroot setup by stripping the test directory
    from any control socket responses. As expected this is causing integ tests that
    validate this path to fail.
    
    Sathyanarayanan took the first several stabs at this, and this is just another
    potential option for...
    https://trac.torproject.org/projects/tor/ticket/4896
---
 run_tests.py       |    1 +
 test/runner.py     |   38 ++++++++++++++++++++++++++++++++++++++
 test/settings.cfg  |    2 ++
 test/testrc.sample |   11 ++---------
 4 files changed, 43 insertions(+), 9 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index 1fee4da..dcfcdef 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -63,6 +63,7 @@ CONFIG = stem.util.conf.config_dict("test", {
 Target = stem.util.enum.UppercaseEnum(
   "ONLINE",
   "RELATIVE",
+  "CHROOT",
   "RUN_NONE",
   "RUN_OPEN",
   "RUN_PASSWORD",
diff --git a/test/runner.py b/test/runner.py
index 32c3872..3636b44 100644
--- a/test/runner.py
+++ b/test/runner.py
@@ -49,6 +49,7 @@ CONFIG = stem.util.conf.config_dict("test", {
   "integ.log": "./test/data/log",
   "integ.target.online": False,
   "integ.target.relative_data_dir": False,
+  "integ.target.chroot": False,
 })
 
 STATUS_ATTR = (term.Color.BLUE, term.Attr.BOLD)
@@ -135,6 +136,20 @@ def get_runner():
   if not INTEG_RUNNER: INTEG_RUNNER = Runner()
   return INTEG_RUNNER
 
+class MockChrootFile:
+  """
+  Wrapper around a file object that strips given content from readline()
+  responses. This is used to simulate a chroot setup by removing the restign
+  directory from the paths we report.
+  """
+  
+  def __init__(self, wrapped_file, strip_text):
+    self.wrapped_file = wrapped_file
+    self.strip_text = strip_text
+  
+  def readline(self):
+    return self.wrapped_file.readline().replace(self.strip_text, "")
+
 class Runner:
   def __init__(self):
     self._runner_lock = threading.RLock()
@@ -146,6 +161,10 @@ class Runner:
     self._torrc_contents = ""
     self._custom_opts = None
     self._tor_process = None
+    
+    # set if we monkey patch stem.socket.recv_message()
+    
+    self._original_recv_message = None
   
   def start(self, tor_cmd, extra_torrc_opts):
     """
@@ -198,6 +217,20 @@ class Runner:
         self._run_setup()
         self._start_tor(tor_cmd)
         
+        # strip the testing directory from recv_message responses if we're
+        # simulating a chroot setup
+        
+        if CONFIG["integ.target.chroot"] and not self._original_recv_message:
+          # TODO: when we have a function for telling stem the chroot we'll
+          # need to set that too
+          
+          self._original_recv_message = stem.socket.recv_message
+          
+          def _chroot_recv_message(control_file):
+            return self._original_recv_message(MockChrootFile(control_file, data_dir_path))
+          
+          stem.socket.recv_message = _chroot_recv_message
+        
         # revert our cwd back to normal
         if CONFIG["integ.target.relative_data_dir"]:
           os.chdir(original_cwd)
@@ -226,6 +259,11 @@ class Runner:
       if self._test_dir and CONFIG["integ.test_directory"] == "":
         shutil.rmtree(self._test_dir, ignore_errors = True)
       
+      # reverts any mockin of stem.socket.recv_message
+      if self._original_recv_message:
+        stem.socket.recv_message = self._original_recv_message
+        self._original_recv_message = None
+      
       self._test_dir = ""
       self._tor_cmd = None
       self._tor_cwd = ""
diff --git a/test/settings.cfg b/test/settings.cfg
index 74bb933..77156c5 100644
--- a/test/settings.cfg
+++ b/test/settings.cfg
@@ -30,6 +30,7 @@ msg.help
 
 target.config ONLINE       => integ.target.online
 target.config RELATIVE     => integ.target.relative_data_dir
+target.config CHROOT       => integ.target.chroot
 target.config RUN_NONE     => integ.target.run.none
 target.config RUN_OPEN     => integ.target.run.open
 target.config RUN_PASSWORD => integ.target.run.password
@@ -44,6 +45,7 @@ target.config RUN_ALL      => integ.target.run.all
 
 target.description ONLINE       => Includes tests that require network activity.
 target.description RELATIVE     => Uses a relative path for tor's data directory.
+target.description CHROOT       => Simulates a chroot setup.
 target.description RUN_NONE     => Configuration without a way for controllers to connect.
 target.description RUN_OPEN     => Configuration with an open control port (default).
 target.description RUN_PASSWORD => Configuration with password authentication.
diff --git a/test/testrc.sample b/test/testrc.sample
index a1cd5a6..165a8d8 100644
--- a/test/testrc.sample
+++ b/test/testrc.sample
@@ -26,15 +26,7 @@
 # integ.target.relative_data_dir
 #   Uses a relative path for the tor data directory if set.
 #
-# integ.target.run.none
-# integ.target.run.open
-# integ.target.run.password
-# integ.target.run.cookie
-# integ.target.run.multiple
-# integ.target.run.socket
-# integ.target.run.scookie
-# integ.target.run.ptrace
-# integ.target.run.all
+# integ.target.run.*
 #   Runs the integration test suite for all of the given connection and
 #   authentication configurations. If the 'all' option is set then the other
 #   flags are ignored.
@@ -50,6 +42,7 @@ integ.log ./test/data/log
 
 integ.target.online false
 integ.target.relative_data_dir false
+integ.target.chroot false
 integ.target.run.none false
 integ.target.run.open true
 integ.target.run.password false





More information about the tor-commits mailing list