[tor-commits] [stem/master] Integ target for running with a relative data dir

atagar at torproject.org atagar at torproject.org
Mon Nov 21 18:15:07 UTC 2011


commit d3b8f0aed7d715721724a7a94e96a188f1ee4e79
Author: Damian Johnson <atagar at torproject.org>
Date:   Mon Nov 21 10:13:10 2011 -0800

    Integ target for running with a relative data dir
    
    Having a relative path for our data directory can cause headaches since tor
    then provides relative paths for the data it gives (for instance, for the
    authentication cookie location). Adding an integration testing target to have a
    relative data directory, to better exercise the path expansion code.
---
 run_tests.py              |    3 ++-
 test/integ/util/system.py |   13 +++++++------
 test/runner.py            |   30 +++++++++++++++++++++++++++---
 test/testrc.sample        |    4 ++++
 4 files changed, 40 insertions(+), 10 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index 76e1abb..93a715a 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -47,10 +47,11 @@ INTEG_TESTS = (("stem.types.ControlMessage", test.integ.types.control_message.Te
               )
 
 # Integration tests above the basic suite.
-TARGETS = stem.util.enum.Enum(*[(v, v) for v in ("ONLINE", "CONNECTION")])
+TARGETS = stem.util.enum.Enum(*[(v, v) for v in ("ONLINE", "RELATIVE", "CONNECTION")])
 
 TARGET_ATTR = {
   TARGETS.ONLINE: ("test.integ.target.online", "Includes tests that require network activity."),
+  TARGETS.RELATIVE: ("test.integ.target.relative_data_dir", "Uses a relative path for tor's data directory."),
   TARGETS.CONNECTION: ("test.integ.target.connection", "Runs the suite over multiple connection configurations."),
 }
 
diff --git a/test/integ/util/system.py b/test/integ/util/system.py
index 9785400..dfce42d 100644
--- a/test/integ/util/system.py
+++ b/test/integ/util/system.py
@@ -175,9 +175,8 @@ class TestSystem(unittest.TestCase):
     Checks general usage of the stem.util.system.get_cwd function.
     """
     
-    # tor's pwd will match our process since we started it
     runner = test.runner.get_runner()
-    self.assertEquals(os.getcwd(), stem.util.system.get_cwd(runner.get_pid()))
+    self.assertEquals(runner.get_tor_cwd(), stem.util.system.get_cwd(runner.get_pid()))
     self.assertEquals(None, stem.util.system.get_cwd(99999))
   
   def test_get_cwd_pwdx(self):
@@ -192,8 +191,9 @@ class TestSystem(unittest.TestCase):
     pwdx_prefix = stem.util.system.GET_CWD_PWDX % ""
     stem.util.system.CALL_MOCKING = lambda cmd: cmd.startswith(pwdx_prefix)
     
-    runner_pid = test.runner.get_runner().get_pid()
-    self.assertEquals(os.getcwd(), stem.util.system.get_cwd(runner_pid))
+    runner = test.runner.get_runner()
+    runner_pid, tor_cwd = runner.get_pid(), runner.get_tor_cwd()
+    self.assertEquals(tor_cwd, stem.util.system.get_cwd(runner_pid))
   
   def test_get_cwd_lsof(self):
     """
@@ -207,8 +207,9 @@ class TestSystem(unittest.TestCase):
     lsof_prefix = "lsof -a -p "
     stem.util.system.CALL_MOCKING = lambda cmd: cmd.startswith(lsof_prefix)
     
-    runner_pid = test.runner.get_runner().get_pid()
-    self.assertEquals(os.getcwd(), stem.util.system.get_cwd(runner_pid))
+    runner = test.runner.get_runner()
+    runner_pid, tor_cwd = runner.get_pid(), runner.get_tor_cwd()
+    self.assertEquals(tor_cwd, stem.util.system.get_cwd(runner_pid))
   
   def test_get_bsd_jail_id(self):
     """
diff --git a/test/runner.py b/test/runner.py
index 9d26deb..b9b77f4 100644
--- a/test/runner.py
+++ b/test/runner.py
@@ -36,6 +36,7 @@ DEFAULT_CONFIG = {
   "test.integ.test_directory": "./test/data",
   "test.integ.log": "./test/data/log",
   "test.integ.target.online": False,
+  "test.integ.target.relative_data_dir": False,
 }
 
 # Methods for connecting to tor. General integration tests only run with the
@@ -113,6 +114,7 @@ class Runner:
     
     # runtime attributes, set by the start method
     self._test_dir = ""
+    self._tor_cwd = ""
     self._torrc_contents = ""
     self._connection_type = None
     self._tor_process = None
@@ -125,8 +127,8 @@ class Runner:
     Arguments:
       connection_type (TorConnection) - method for controllers to authenticate
                           to tor
-      quiet (bool)      - if False then this prints status information as we
-                          start up to stdout
+      quiet (bool) - if False then this prints status information as we start
+                     up to stdout
     
     Raises:
       OSError if unable to run test preparations or start tor
@@ -153,12 +155,26 @@ class Runner:
     else:
       self._test_dir = tempfile.mktemp("-stem-integ")
     
+    original_cwd, data_dir_path = os.getcwd(), self._test_dir
+    
+    if self._config["test.integ.target.relative_data_dir"]:
+      tor_cwd = os.path.dirname(self._test_dir)
+      if not os.path.exists(tor_cwd): os.makedirs(tor_cwd)
+      
+      os.chdir(tor_cwd)
+      data_dir_path = "./%s" % os.path.basename(self._test_dir)
+    
     self._connection_type = connection_type
-    self._torrc_contents = get_torrc(connection_type) % self._test_dir
+    self._torrc_contents = get_torrc(connection_type) % data_dir_path
     
     try:
+      self._tor_cwd = os.getcwd()
       self._run_setup(quiet)
       self._start_tor(quiet)
+      
+      # revert our cwd back to normal
+      if self._config["test.integ.target.relative_data_dir"]:
+        os.chdir(original_cwd)
     except OSError, exc:
       self.stop(quiet)
       raise exc
@@ -185,6 +201,7 @@ class Runner:
       shutil.rmtree(self._test_dir, ignore_errors = True)
     
     self._test_dir = ""
+    self._tor_cwd = ""
     self._torrc_contents = ""
     self._connection_type = None
     self._tor_process = None
@@ -244,6 +261,13 @@ class Runner:
     test_dir = self._get("_test_dir")
     return os.path.join(test_dir, "control_auth_cookie")
   
+  def get_tor_cwd(self):
+    """
+    Provides the current working directory of our tor process.
+    """
+    
+    return self._get("_tor_cwd")
+  
   def get_torrc_contents(self):
     """
     Provides the contents of our torrc.
diff --git a/test/testrc.sample b/test/testrc.sample
index 83f0861..c7ae7d1 100644
--- a/test/testrc.sample
+++ b/test/testrc.sample
@@ -16,6 +16,9 @@
 #   Runs tests with network activity. If set then we'll wait for tor to fully
 #   bootstrap when starting, which won't happen without a network connection.
 #
+# test.integ.target.relative_data_dir
+#   Uses a relative path for the tor data directory if set.
+#
 # test.integ.target.connection
 #   Runs integration tests for multiple connection and authentiction methods if
 #   true. Otherwise they'll only be run for a single connection type.
@@ -23,5 +26,6 @@
 test.integ.test_directory ./test/data
 test.integ.log ./test/data/log
 test.integ.target.online false
+test.integ.target.relative_data_dir false
 test.integ.target.connection false
 



More information about the tor-commits mailing list