[tor-commits] [stem/master] Added integration tests for proc.py.

atagar at torproject.org atagar at torproject.org
Sun Jul 1 02:49:59 UTC 2012


commit 830bc5fa28f9683109a9ed7bee2980312f550910
Author: Megan <mchang01 at wesleyan.edu>
Date:   Thu Jun 28 16:31:59 2012 -0400

    Added integration tests for proc.py.
    
    In the cases of test_get_memory_usage(), test_get_stats(),
    test_get_connections(), there were issues of doing direct tests. First,
    the only way to have values to check against is with code written
    in proc.py, which would mean we are checking the code against itself.
    Second, the results of these functions change frequently, so by the time
    the functions are called, their results will differ from the expected
    values. To get around this, we simply checked that these values were
    nonzero.
---
 run_tests.py            |    2 +
 test/integ/util/proc.py |  106 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 108 insertions(+), 0 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index a93b20e..69cf441 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -43,6 +43,7 @@ import test.integ.descriptor.extrainfo_descriptor
 import test.integ.response.protocolinfo
 import test.integ.util.conf
 import test.integ.util.system
+import test.integ.util.proc
 import test.integ.process
 import test.integ.version
 
@@ -116,6 +117,7 @@ UNIT_TESTS = (
 
 INTEG_TESTS = (
   test.integ.util.conf.TestConf,
+  test.integ.util.proc.TestProc,
   test.integ.util.system.TestSystem,
   test.integ.descriptor.reader.TestDescriptorReader,
   test.integ.descriptor.server_descriptor.TestServerDescriptor,
diff --git a/test/integ/util/proc.py b/test/integ/util/proc.py
new file mode 100644
index 0000000..21cd5d8
--- /dev/null
+++ b/test/integ/util/proc.py
@@ -0,0 +1,106 @@
+"""
+Integration tests for stem.util.proc functions against the tor process that we're running.
+"""
+import os
+import socket
+import unittest
+
+import test.runner
+import stem.socket
+import stem.control
+import stem.util.proc as proc
+
+class TestProc(unittest.TestCase):
+  def test_get_cwd(self):
+    """
+    Tests the stem.util.proc.get_cwd function.
+    """
+    
+    # Skips test if proc utilities are unavailable on this platform.
+    # This is repeated at the beginning of every proc integration test.
+    if not proc.is_available:
+      test.runner.skip(self, "(Unavailable on this platform)")
+      return
+    
+    runner = test.runner.get_runner()
+    
+    runner_pid, tor_cwd = runner.get_pid(), runner.get_tor_cwd()
+    self.assertEquals(tor_cwd, proc.get_cwd(runner_pid))
+    
+  def test_get_uid(self):
+    """
+    Tests the stem.util.proc.get_uid function.
+    """
+    
+    if not proc.is_available:
+      test.runner.skip(self, "(Unavailable on this platform)")
+      return
+    
+    tor_pid = test.runner.get_runner().get_pid()
+    
+    self.assertEquals(os.geteuid(), proc.get_uid(tor_pid))
+    
+  def test_get_memory_usage(self):
+    """
+    Tests the stem.util.proc.get_memory_usage function.
+    """
+    
+    if not proc.is_available:
+      test.runner.skip(self, "(Unavailable on this platform)")
+      return
+    
+    tor_pid = test.runner.get_runner().get_pid()
+    res_size, vir_size = (proc.get_memory_usage(tor_pid))
+    # Checks if get_memory_usage() is greater than a kilobyte.
+    res_bool, vir_bool = res_size > 1024, vir_size > 1024
+    
+    self.assertTrue(res_bool)
+    self.assertTrue(vir_bool)
+    
+  def test_get_stats(self):
+    """
+    Tests the stem.util.proc.get_memory_usage function.
+    """
+    
+    if not proc.is_available:
+      test.runner.skip(self, "(Unavailable on this platform)")
+      return
+    
+    tor_pid = test.runner.get_runner().get_pid()
+    command, utime, stime, start_time = proc.get_stats(tor_pid, 'command', 'utime', 'stime', 'start time')
+    
+    # Checks if utime and stime are greater than 0.
+    utime_bool = utime > 0
+    stime_bool = stime > 0
+    # Checks if start time is greater than get_system_start_time().
+    start_time_bool = start_time > proc.get_system_start_time()
+    
+    self.assertEquals('tor', command)
+    self.assertTrue(utime_bool)
+    self.assertTrue(stime_bool)
+    self.assertTrue(start_time_bool)
+    
+  def test_get_connections(self):
+    """
+    Tests the stem.util.proc.get_connections function.
+    
+    Checks that get_connections() provides the control connection.
+    """
+    
+    if not proc.is_available:
+      test.runner.skip(self, "(Unavailable on this platform)")
+      return
+    
+    tor_pid = test.runner.get_runner().get_pid()
+    test.runner.get_runner().get_tor_controller(test.runner.CONTROL_PASSWORD)
+    ip_bool, socket_bool = False, False
+    for tup in proc.get_connections(tor_pid):
+      if '127.0.0.1' in tup:
+        ip_bool = True
+      if test.runner.CONTROL_PORT in tup:
+        socket_bool = True
+      if ip_bool and socket_bool:
+        continue
+    
+    self.assertTrue(ip_bool)
+    self.assertTrue(socket_bool)





More information about the tor-commits mailing list