[tor-commits] [arm/master] Initial Daemon unit tests

atagar at torproject.org atagar at torproject.org
Sun Jan 5 22:38:12 UTC 2014


commit 511c7ac98f13bf7b0817ab759d331a65424472f6
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Jan 5 14:38:28 2014 -0800

    Initial Daemon unit tests
    
    Few simple tests for its initialization. Further testing for the Daemon and
    subclasses in the tracker module coming soon.
    
    As for the load_test() test, it's both for a helper that no longer exists and
    the test wasn't being run due to not having an __init__.py.
---
 arm/util/tracker.py           |    3 ++-
 run_tests.py                  |    8 +++++--
 test/starter/load_settings.py |   31 ---------------------------
 test/util/tracker/daemon.py   |   47 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 55 insertions(+), 34 deletions(-)

diff --git a/arm/util/tracker.py b/arm/util/tracker.py
index 476b11e..8febb12 100644
--- a/arm/util/tracker.py
+++ b/arm/util/tracker.py
@@ -1,5 +1,5 @@
 """
-Background tasks for gathering informatino about the tor process.
+Background tasks for gathering information about the tor process.
 
 ::
 
@@ -108,6 +108,7 @@ def stop_trackers():
       tracker.join()
 
   halt_thread = threading.Thread(target = halt_trackers)
+  halt_thread.setDaemon(True)
   halt_thread.start()
   return halt_thread
 
diff --git a/run_tests.py b/run_tests.py
index 324ed02..1553da8 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -48,10 +48,14 @@ def main():
   static_check_issues = {}
 
   if is_pyflakes_available():
-    static_check_issues.update(get_pyflakes_issues(SRC_PATHS))
+    for path, issues in get_pyflakes_issues(SRC_PATHS).items():
+      for issue in issues:
+        static_check_issues.setdefault(path, []).append(issue)
 
   if is_pep8_available():
-    static_check_issues.update(get_stylistic_issues(SRC_PATHS))
+    for path, issues in get_stylistic_issues(SRC_PATHS).items():
+      for issue in issues:
+        static_check_issues.setdefault(path, []).append(issue)
 
   if static_check_issues:
     print "STATIC CHECKS"
diff --git a/test/starter/load_settings.py b/test/starter/load_settings.py
deleted file mode 100644
index 5c9cfd8..0000000
--- a/test/starter/load_settings.py
+++ /dev/null
@@ -1,31 +0,0 @@
-import io
-import unittest
-
-from mock import patch
-
-from arm.starter import _load_settings
-
-
-class TestLoadSettings(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)
diff --git a/test/util/tracker/__init__.py b/test/util/tracker/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/test/util/tracker/daemon.py b/test/util/tracker/daemon.py
new file mode 100644
index 0000000..9bde71f
--- /dev/null
+++ b/test/util/tracker/daemon.py
@@ -0,0 +1,47 @@
+import unittest
+
+from arm.util.tracker import Daemon
+
+from mock import patch
+
+
+class TestDaemon(unittest.TestCase):
+  @patch('arm.util.tracker.tor_controller')
+  @patch('arm.util.tracker.system')
+  def test_init(self, system_mock, tor_controller_mock):
+    # Check that we register ourselves to listen for status changes, and
+    # properly retrieve the process' pid and name.
+
+    tor_controller_mock().get_pid.return_value = 12345
+    system_mock.get_name_by_pid.return_value = 'local_tor'
+
+    daemon = Daemon(0.05)
+
+    self.assertEqual(0.05, daemon._rate)
+    self.assertEqual(12345, daemon._process_pid)
+    self.assertEqual('local_tor', daemon._process_name)
+
+    tor_controller_mock().add_status_listener.assert_called_with(daemon._tor_status_listener)
+    system_mock.get_name_by_pid.assert_called_with(12345)
+
+  @patch('arm.util.tracker.tor_controller')
+  @patch('arm.util.tracker.system')
+  def test_init_without_name(self, system_mock, tor_controller_mock):
+    # Check when we default to 'tor' if unable to determine the process' name.
+
+    tor_controller_mock().get_pid.return_value = 12345
+    system_mock.get_name_by_pid.return_value = None
+
+    daemon = Daemon(0.05)
+    self.assertEqual('tor', daemon._process_name)
+
+  @patch('arm.util.tracker.tor_controller')
+  @patch('arm.util.tracker.system')
+  def test_init_without_pid(self, system_mock, tor_controller_mock):
+    # Check when we can't determine tor's pid.
+
+    tor_controller_mock().get_pid.return_value = None
+
+    daemon = Daemon(0.05)
+    self.assertEqual(None, daemon._process_pid)
+    self.assertEqual('tor', daemon._process_name)



More information about the tor-commits mailing list