[tor-commits] [stem/master] Replacing runner OPT_* with enumeration

atagar at torproject.org atagar at torproject.org
Sun Jan 22 08:06:28 UTC 2012


commit d7fa1891aaac5f5907cbff05a8fd2043155b475f
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat Jan 21 22:14:25 2012 -0800

    Replacing runner OPT_* with enumeration
    
    Replacing the test.runner.OPT_* constants with an enumeration. This is a little
    more elegant and lets us get rid of the cluncky RUNNER_OPT_MAPPING dictionary.
---
 run_tests.py                            |   18 ++++++------------
 test/integ/connection/authentication.py |   10 +++++-----
 test/integ/connection/connect.py        |    4 ++--
 test/integ/connection/protocolinfo.py   |   10 +++++-----
 test/integ/util/system.py               |    2 +-
 test/runner.py                          |   20 +++++++++++---------
 6 files changed, 30 insertions(+), 34 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index fdd6901..22b0488 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -71,16 +71,6 @@ CONFIG = {
   "target.torrc": {},
 }
 
-# mapping between 'target.torrc' options and runner attributes
-# TODO: switch OPT_* to enums so this is unnecessary
-RUNNER_OPT_MAPPING = {
-  "PORT": test.runner.OPT_PORT,
-  "PASSWORD": test.runner.OPT_PASSWORD,
-  "COOKIE": test.runner.OPT_COOKIE,
-  "SOCKET": test.runner.OPT_SOCKET,
-  "PTRACE": test.runner.OPT_PTRACE,
-}
-
 DEFAULT_RUN_TARGET = TARGETS.CONN_OPEN
 
 HELP_MSG = """Usage runTests.py [OPTION]
@@ -133,12 +123,16 @@ if __name__ == '__main__':
   test_config.load(settings_path)
   test_config.update(CONFIG)
   
-  # parses target.torrc as csv values and convert to runner OPT_* values
+  # parses target.torrc as csv values and convert to runner Torrc enums
   for target in CONFIG["target.torrc"]:
     CONFIG["target.torrc"][target] = []
     
     for opt in test_config.get_str_csv("target.torrc", [], sub_key = target):
-      CONFIG["target.torrc"][target].append(RUNNER_OPT_MAPPING[opt])
+      if opt in test.runner.Torrc.keys():
+        CONFIG["target.torrc"][target].append(test.runner.Torrc[opt])
+      else:
+        print "'%s' isn't a test.runner.Torrc enumeration" % opt
+        sys.exit(1)
   
   start_time = time.time()
   run_unit_tests = False
diff --git a/test/integ/connection/authentication.py b/test/integ/connection/authentication.py
index 8c84490..0388f79 100644
--- a/test/integ/connection/authentication.py
+++ b/test/integ/connection/authentication.py
@@ -55,7 +55,7 @@ class TestAuthenticate(unittest.TestCase):
       control_socket = stem.socket.ControlPort(control_port = test.runner.CONTROL_PORT)
     except stem.socket.SocketError:
       # assert that we didn't have a socket to connect to
-      self.assertFalse(test.runner.OPT_PORT in connection_options)
+      self.assertFalse(test.runner.Torrc.PORT in connection_options)
       return
     
     try:
@@ -65,7 +65,7 @@ class TestAuthenticate(unittest.TestCase):
     except stem.connection.IncorrectSocketType:
       self.fail()
     except stem.connection.MissingPassword:
-      self.assertTrue(test.runner.OPT_PASSWORD in connection_options)
+      self.assertTrue(test.runner.Torrc.PASSWORD in connection_options)
       controller_password = test.runner.CONTROL_PASSWORD
       
       try:
@@ -89,7 +89,7 @@ class TestAuthenticate(unittest.TestCase):
     
     runner = test.runner.get_runner()
     connection_options = runner.get_connection_options()
-    is_password_only = test.runner.OPT_PASSWORD in connection_options and not test.runner.OPT_COOKIE in connection_options
+    is_password_only = test.runner.Torrc.PASSWORD in connection_options and not test.runner.Torrc.COOKIE in connection_options
     
     # tests without a password
     control_socket = runner.get_tor_socket(False)
@@ -247,8 +247,8 @@ class TestAuthenticate(unittest.TestCase):
     """
     
     connection_options = test.runner.get_runner().get_connection_options()
-    password_auth = test.runner.OPT_PASSWORD in connection_options
-    cookie_auth = test.runner.OPT_COOKIE in connection_options
+    password_auth = test.runner.Torrc.PASSWORD in connection_options
+    cookie_auth = test.runner.Torrc.COOKIE in connection_options
     
     return password_auth, cookie_auth
   
diff --git a/test/integ/connection/connect.py b/test/integ/connection/connect.py
index eea07a8..1264e9e 100644
--- a/test/integ/connection/connect.py
+++ b/test/integ/connection/connect.py
@@ -48,11 +48,11 @@ class TestConnect(unittest.TestCase):
       controller = stem.connection.Controller.NONE
       
       if is_port:
-        opt_type = test.runner.OPT_PORT
+        opt_type = test.runner.Torrc.PORT
         ctl_port = test.runner.CONTROL_PORT
         control_socket = stem.connection.connect_port(control_port = ctl_port, password = ctl_pw, controller = controller)
       else:
-        opt_type = test.runner.OPT_SOCKET
+        opt_type = test.runner.Torrc.SOCKET
         ctl_socket = test.runner.CONTROL_SOCKET_PATH
         control_socket = stem.connection.connect_socket_file(socket_path = ctl_socket, password = ctl_pw, controller = controller)
       
diff --git a/test/integ/connection/protocolinfo.py b/test/integ/connection/protocolinfo.py
index 24edd04..c9b4992 100644
--- a/test/integ/connection/protocolinfo.py
+++ b/test/integ/connection/protocolinfo.py
@@ -73,7 +73,7 @@ class TestProtocolInfo(unittest.TestCase):
     
     stem.util.system.CALL_MOCKING = port_lookup_filter
     
-    if test.runner.OPT_PORT in test.runner.get_runner().get_connection_options():
+    if test.runner.Torrc.PORT in test.runner.get_runner().get_connection_options():
       control_socket = stem.socket.ControlPort(control_port = test.runner.CONTROL_PORT)
       protocolinfo_response = stem.connection.get_protocolinfo(control_socket)
       self.assert_protocolinfo_attr(protocolinfo_response)
@@ -104,7 +104,7 @@ class TestProtocolInfo(unittest.TestCase):
     
     stem.util.system.CALL_MOCKING = socket_lookup_filter
     
-    if test.runner.OPT_SOCKET in test.runner.get_runner().get_connection_options():
+    if test.runner.Torrc.SOCKET in test.runner.get_runner().get_connection_options():
       control_socket = stem.socket.ControlSocketFile(test.runner.CONTROL_SOCKET_PATH)
       protocolinfo_response = stem.connection.get_protocolinfo(control_socket)
       self.assert_protocolinfo_attr(protocolinfo_response)
@@ -149,10 +149,10 @@ class TestProtocolInfo(unittest.TestCase):
     
     auth_methods = []
     
-    if test.runner.OPT_COOKIE in connection_options:
+    if test.runner.Torrc.COOKIE in connection_options:
       auth_methods.append(stem.connection.AuthMethod.COOKIE)
     
-    if test.runner.OPT_PASSWORD in connection_options:
+    if test.runner.Torrc.PASSWORD in connection_options:
       auth_methods.append(stem.connection.AuthMethod.PASSWORD)
     
     if not auth_methods:
@@ -162,7 +162,7 @@ class TestProtocolInfo(unittest.TestCase):
     self.assertEqual(tuple(auth_methods), protocolinfo_response.auth_methods)
     
     auth_cookie_path = None
-    if test.runner.OPT_COOKIE in connection_options:
+    if test.runner.Torrc.COOKIE in connection_options:
       auth_cookie_path = test.runner.get_runner().get_auth_cookie_path()
     
     self.assertEqual(auth_cookie_path, protocolinfo_response.cookie_path)
diff --git a/test/integ/util/system.py b/test/integ/util/system.py
index 2acb488..ecf88d5 100644
--- a/test/integ/util/system.py
+++ b/test/integ/util/system.py
@@ -319,5 +319,5 @@ class TestSystem(unittest.TestCase):
     True if our test runner has a control port, False otherwise.
     """
     
-    return test.runner.OPT_PORT in test.runner.get_runner().get_connection_options()
+    return test.runner.Torrc.PORT in test.runner.get_runner().get_connection_options()
 
diff --git a/test/runner.py b/test/runner.py
index 37697e7..31e3815 100644
--- a/test/runner.py
+++ b/test/runner.py
@@ -65,11 +65,13 @@ CONTROL_PASSWORD = "pw"
 CONTROL_PORT = 1111
 CONTROL_SOCKET_PATH = "/tmp/stem_integ/socket"
 
-OPT_PORT = "ControlPort %i" % CONTROL_PORT
-OPT_COOKIE = "CookieAuthentication 1"
-OPT_PASSWORD = "HashedControlPassword 16:8C423A41EF4A542C6078985270AE28A4E04D056FB63F9F201505DB8E06"
-OPT_SOCKET = "ControlSocket %s" % CONTROL_SOCKET_PATH
-OPT_PTRACE = "DisableDebuggerAttachment 0"
+Torrc = stem.util.enum.Enum(
+  ("PORT", "ControlPort %i" % CONTROL_PORT),
+  ("COOKIE", "CookieAuthentication 1"),
+  ("PASSWORD", "HashedControlPassword 16:8C423A41EF4A542C6078985270AE28A4E04D056FB63F9F201505DB8E06"),
+  ("SOCKET", "ControlSocket %s" % CONTROL_SOCKET_PATH),
+  ("PTRACE", "DisableDebuggerAttachment 0"),
+)
 
 def get_runner():
   """
@@ -248,7 +250,7 @@ class Runner:
     """
     
     conn_opts = self.get_connection_options()
-    return OPT_PORT in conn_opts or OPT_SOCKET in conn_opts
+    return Torrc.PORT in conn_opts or Torrc.SOCKET in conn_opts
   
   def is_ptraceable(self):
     """
@@ -373,9 +375,9 @@ class Runner:
     
     conn_opts = self.get_connection_options()
     
-    if OPT_PORT in conn_opts:
+    if Torrc.PORT in conn_opts:
       control_socket = stem.socket.ControlPort(control_port = CONTROL_PORT)
-    elif OPT_SOCKET in conn_opts:
+    elif Torrc.SOCKET in conn_opts:
       control_socket = stem.socket.ControlSocketFile(CONTROL_SOCKET_PATH)
     else: raise TorInaccessable("Unable to connect to tor")
     
@@ -460,7 +462,7 @@ class Runner:
     # resides in is only accessable by the tor user (and refuses to finish
     # starting if it isn't).
     
-    if OPT_SOCKET in self.get_connection_options():
+    if Torrc.SOCKET in self.get_connection_options():
       try:
         socket_dir = os.path.dirname(CONTROL_SOCKET_PATH)
         _print_status("  making control socket directory (%s)... " % socket_dir, STATUS_ATTR, quiet)





More information about the tor-commits mailing list