[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [arm/master] Unit tests for _get_controller()



commit 5ef90b441a7f77104c835c5dc3968e6a0523b78f
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Mon Sep 9 13:46:13 2013 -0700

    Unit tests for _get_controller()
    
    Handful of unit tests for our helper that gets an unauthenticated Controller.
---
 arm/starter.py  |    2 +-
 test/starter.py |   61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 61 insertions(+), 2 deletions(-)

diff --git a/arm/starter.py b/arm/starter.py
index f046a31..96f4f19 100644
--- a/arm/starter.py
+++ b/arm/starter.py
@@ -233,7 +233,7 @@ def _get_controller(args):
       return Controller.from_socket_file(args.control_socket)
     except stem.SocketError as exc:
       if args.user_provided_socket:
-        raise ValueError("Unable to connect to %s: %s" % (args.control_socket, exc))
+        raise ValueError("Unable to connect to '%s': %s" % (args.control_socket, exc))
   elif args.user_provided_socket:
     raise ValueError("The socket file you specified (%s) doesn't exist" % args.control_socket)
 
diff --git a/test/starter.py b/test/starter.py
index a4e6fdf..f8c4f0c 100644
--- a/test/starter.py
+++ b/test/starter.py
@@ -5,7 +5,11 @@ Unit tests for arm's initialization module.
 import getopt
 import unittest
 
-from arm.starter import _get_args, ARGS
+from mock import Mock, patch
+
+from arm.starter import _get_args, _get_controller, ARGS
+
+import stem
 
 class TestArgumentParsing(unittest.TestCase):
   def test_that_we_get_default_values(self):
@@ -68,3 +72,58 @@ class TestArgumentParsing(unittest.TestCase):
     for invalid_input in invalid_inputs:
       self.assertRaises(ValueError, _get_args, ['--interface', invalid_input])
 
+class TestGetController(unittest.TestCase):
+  @patch('os.path.exists', Mock(return_value = True))
+  @patch('stem.util.system.is_running')
+  @patch('stem.control.Controller.from_socket_file', Mock(side_effect = stem.SocketError('failed')))
+  @patch('stem.control.Controller.from_port', Mock(side_effect = stem.SocketError('failed')))
+  def test_failue_with_the_default_endpoint(self, is_running_mock):
+    is_running_mock.return_value = False
+    self._assert_get_controller_fails_with([], "Unable to connect to tor. Are you sure it's running?")
+
+    is_running_mock.return_value = True
+    self._assert_get_controller_fails_with([], "Unable to connect to tor. Maybe it's running without a ControlPort?")
+
+  @patch('os.path.exists')
+  @patch('stem.util.system.is_running', Mock(return_value = True))
+  @patch('stem.control.Controller.from_socket_file', Mock(side_effect = stem.SocketError('failed')))
+  @patch('stem.control.Controller.from_port', Mock(side_effect = stem.SocketError('failed')))
+  def test_failure_with_a_custom_endpoint(self, path_exists_mock):
+    path_exists_mock.return_value = True
+    self._assert_get_controller_fails_with(['--interface', '80'], "Unable to connect to 127.0.0.1:80: failed")
+    self._assert_get_controller_fails_with(['--socket', '/tmp/my_socket'], "Unable to connect to '/tmp/my_socket': failed")
+
+    path_exists_mock.return_value = False
+    self._assert_get_controller_fails_with(['--interface', '80'], "Unable to connect to 127.0.0.1:80: failed")
+    self._assert_get_controller_fails_with(['--socket', '/tmp/my_socket'], "The socket file you specified (/tmp/my_socket) doesn't exist")
+
+  @patch('os.path.exists', Mock(return_value = False))
+  @patch('stem.control.Controller.from_port')
+  def test_getting_a_control_port(self, from_port_mock):
+    from_port_mock.return_value = 'success'
+
+    self.assertEqual('success', _get_controller(_get_args([])))
+    from_port_mock.assert_called_once_with('127.0.0.1', 9051)
+    from_port_mock.reset_mock()
+
+    self.assertEqual('success', _get_controller(_get_args(['--interface', '255.0.0.10:80'])))
+    from_port_mock.assert_called_once_with('255.0.0.10', 80)
+
+  @patch('os.path.exists', Mock(return_value = True))
+  @patch('stem.control.Controller.from_socket_file')
+  def test_getting_a_control_socket(self, from_socket_file_mock):
+    from_socket_file_mock.return_value = 'success'
+
+    self.assertEqual('success', _get_controller(_get_args([])))
+    from_socket_file_mock.assert_called_once_with('/var/run/tor/control')
+    from_socket_file_mock.reset_mock()
+
+    self.assertEqual('success', _get_controller(_get_args(['--socket', '/tmp/my_socket'])))
+    from_socket_file_mock.assert_called_once_with('/tmp/my_socket')
+
+  def _assert_get_controller_fails_with(self, args, msg):
+    try:
+      _get_controller(_get_args(args))
+      self.fail()
+    except ValueError, exc:
+      self.assertEqual(msg, str(exc))



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits