commit 3fd005a27ca35bf802d3917c992d2626534e87c9 Author: Megan mchang01@wesleyan.edu Date: Thu Jun 14 16:17:32 2012 -0400
Created proc.py testing code and imported unit tests to run_tests.py.
Wrote mock_get_lines to mock the proc module's _get_lines function. Completed unit testing code for proc utilities such as get_system_start_time, get_physical_memory, and get_memory_usage. The test_get_memory_usage function was written to test the functionality of mock_get_lines as it is the only function that takes in multiple line prefixes. --- run_tests.py | 2 + test/unit/util/proc.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 0 deletions(-)
diff --git a/run_tests.py b/run_tests.py index bcd7e12..670947a 100755 --- a/run_tests.py +++ b/run_tests.py @@ -44,6 +44,7 @@ import test.integ.util.conf import test.integ.util.system import test.integ.process import test.integ.version +import test.unit.util.proc
import stem.prereq import stem.util.conf @@ -110,6 +111,7 @@ UNIT_TESTS = ( test.unit.response.protocolinfo.TestProtocolInfoResponse, test.unit.response.authchallenge.TestAuthChallengeResponse, test.unit.connection.authentication.TestAuthenticate, + test.unit.util.proc.TestProc, )
INTEG_TESTS = ( diff --git a/test/unit/util/proc.py b/test/unit/util/proc.py new file mode 100644 index 0000000..a474800 --- /dev/null +++ b/test/unit/util/proc.py @@ -0,0 +1,70 @@ +""" +Unit testing code for proc utilities located in /stem/util/proc.py +""" +import os +import time +import unittest +import functools + +import test.mocking as mocking +import stem.util.proc as proc + +def mock_get_lines(file_path, line_prefixes, return_values): + """ + Provides mocking for the proc module's _get_line function. + """ + if isinstance(line_prefixes, tuple): + prefix_list = sorted(list(line_prefixes)) + else: + # Only one line prefix given. + prefix_list = line_prefixes + def _mock_get_lines(path, prefixes, return_values, caller_path, + caller_prefixes, caller_parameter): + if isinstance(caller_prefixes, tuple): + caller_prefix_list = sorted(list(caller_prefixes)) + else: + #Only one line prefix given. + caller_prefix_list = caller_prefixes + if path == caller_path and prefixes == caller_prefix_list: + return return_values + else: + return None + + return functools.partial(_mock_get_lines, file_path, prefix_list, + return_values) + +class TestProc(unittest.TestCase): + def tearDown(self): + mocking.revert_mocking() + + def test_get_system_start_time(self): + """ + Tests the get_system_start_time function. + """ + mocking.mock(proc._get_line, mock_get_lines("/proc/stat", "btime", + "btime 1001001")) + + self.assertEquals(1001001, proc.get_system_start_time()) + + def test_get_physical_memory(self): + """ + Tests the get_physical_memory function. + """ + mocking.mock(proc._get_line, mock_get_lines("/proc/meminfo", + "MemTotal:", "MemTotal: 12345 kB")) + + self.assertEquals((12345*1024), proc.get_physical_memory()) + + def test_get_memory_usage(self): + """ + Tests the get_memory_usage function. + + This is the only function in proc.py that utilizes + _get_lines explicitly. + """ + + mocking.mock(proc._get_lines, mock_get_lines("/proc/1111/status", + ("VmRSS:", "VmSize:"), {"VmRSS:": "VmRSS: 100 kB", + "VmSize:":"VmSize: 1800 kB"})) + + self.assertEqual((100*1024, 1800*1024), proc.get_memory_usage(1111))
tor-commits@lists.torproject.org