[tor-commits] [arm/master] Unit tests for _load_settings()
atagar at torproject.org
atagar at torproject.org
Mon Dec 16 02:29:50 UTC 2013
commit 6eee9f1009d39c0bc83d9ae88a5a56991041e490
Author: Damian Johnson <atagar at torproject.org>
Date: Sun Dec 15 18:28:58 2013 -0800
Unit tests for _load_settings()
Few unit tests for the function that loads our internal settings.
---
arm/starter.py | 25 +++++++++++++++----------
test/starter/arg_parsing.py | 3 +--
test/starter/authenticate.py | 4 +---
test/starter/get_controller.py | 1 +
test/starter/load_settings.py | 31 +++++++++++++++++++++++++++++++
5 files changed, 49 insertions(+), 15 deletions(-)
diff --git a/arm/starter.py b/arm/starter.py
index 8b224a9..19292a8 100644
--- a/arm/starter.py
+++ b/arm/starter.py
@@ -34,9 +34,9 @@ import stem.util.log
import stem.util.system
LOG_DUMP_PATH = os.path.expanduser("~/.arm/log")
+SETTINGS_PATH = os.path.join(os.path.dirname(__file__), 'settings.cfg')
CONFIG = stem.util.conf.config_dict("arm", {
- 'settings_loaded': False,
'tor.password': None,
'startup.events': 'N3',
'msg.help': '',
@@ -75,23 +75,28 @@ OPT = "i:s:c:dbe:vh"
OPT_EXPANDED = ["interface=", "socket=", "config=", "debug", "blind", "event=", "version", "help"]
-def _load_settings():
+def _load_settings(config = 'arm'):
"""
- Loads arms internal settings from its 'settings.cfg'. This comes bundled with
- arm and should be considered to be an error if it can't be loaded. If the
- settings have already been loaded then this is a no-op.
+ Loads arm's internal settings from its 'settings.cfg'. This comes bundled
+ with arm and should be considered to be an error if it can't be loaded. If
+ the settings have already been loaded then this is a no-op.
+
+ :param str config: configuration config to load the parameters into
+
+ :returns: **stem.util.conf.Config** for the given handle
:raises: **ValueError** if the settings can't be loaded
"""
- if not CONFIG['settings_loaded']:
- config = stem.util.conf.get_config("arm")
- settings_path = os.path.join(os.path.dirname(__file__), 'settings.cfg')
+ config = stem.util.conf.get_config(config)
+ if not config.get('settings_loaded', False):
try:
- config.load(settings_path)
+ config.load(SETTINGS_PATH)
except IOError as exc:
- raise ValueError("Unable to load arm's internal configuration (%s): %s" % (settings_path, exc))
+ raise ValueError("Unable to load arm's internal configuration (%s): %s" % (SETTINGS_PATH, exc))
+
+ return config
def _get_args(argv):
diff --git a/test/starter/arg_parsing.py b/test/starter/arg_parsing.py
index 26ed3bf..f6c60f5 100644
--- a/test/starter/arg_parsing.py
+++ b/test/starter/arg_parsing.py
@@ -3,6 +3,7 @@ import unittest
from arm.starter import _get_args, ARGS
+
class TestArgumentParsing(unittest.TestCase):
def test_that_we_get_default_values(self):
args = _get_args([])
@@ -63,5 +64,3 @@ class TestArgumentParsing(unittest.TestCase):
for invalid_input in invalid_inputs:
self.assertRaises(ValueError, _get_args, ['--interface', invalid_input])
-
-
diff --git a/test/starter/authenticate.py b/test/starter/authenticate.py
index 5f9e0aa..17c846e 100644
--- a/test/starter/authenticate.py
+++ b/test/starter/authenticate.py
@@ -4,10 +4,8 @@ from mock import Mock, patch
from arm.starter import (
_load_settings,
- _get_args,
_get_controller,
_authenticate,
- ARGS,
)
import stem
@@ -16,6 +14,7 @@ import stem.socket
_load_settings()
+
class TestAuthenticate(unittest.TestCase):
@patch('arm.util.torTools.get_chroot')
def test_success(self, get_chroot_mock):
@@ -80,4 +79,3 @@ class TestAuthenticate(unittest.TestCase):
except ValueError, exc:
if not msg in str(exc):
self.fail("Expected...\n\n%s\n\n... which couldn't be found in...\n\n%s" % (msg, exc))
-
diff --git a/test/starter/get_controller.py b/test/starter/get_controller.py
index 5890dba..974b4b5 100644
--- a/test/starter/get_controller.py
+++ b/test/starter/get_controller.py
@@ -8,6 +8,7 @@ import stem
import stem.connection
import stem.socket
+
class TestGetController(unittest.TestCase):
@patch('os.path.exists', Mock(return_value = True))
@patch('stem.util.system.is_running')
diff --git a/test/starter/load_settings.py b/test/starter/load_settings.py
new file mode 100644
index 0000000..c0a2dfa
--- /dev/null
+++ b/test/starter/load_settings.py
@@ -0,0 +1,31 @@
+import io
+import unittest
+
+from mock import patch
+
+from arm.starter import _load_settings
+
+
+class TestArgumentParsing(unittest.TestCase):
+ def test_we_can_load_the_settings(self):
+ config = _load_settings(self.id())
+ self.assertEqual(config.get('settings_loaded'), 'true')
+
+ @patch('stem.util.conf.open', create = True)
+ def test_when_file_doesnt_exist(self, open_mock):
+ open_mock.side_effect = IOError("No such file or directory")
+
+ try:
+ _load_settings(self.id())
+ self.fail("We didn't raise an exception for a missing settings.cfg")
+ except ValueError as exc:
+ self.assertTrue("Unable to load arm's internal configuration" in str(exc))
+
+ @patch('stem.util.conf.open', create = True)
+ def test_that_repeated_calls_are_ignored(self, open_mock):
+ open_mock.return_value = io.BytesIO("settings_loaded true")
+
+ _load_settings(self.id())
+ _load_settings(self.id())
+ _load_settings(self.id())
+ self.assertEqual(1, open_mock.call_count)
More information about the tor-commits
mailing list