Browse Source

[2353] Use a MockProcessInfo to test BoB.start_process()

Mukund Sivaraman 12 years ago
parent
commit
5450b258db
2 changed files with 30 additions and 2 deletions
  1. 4 1
      src/bin/bind10/bind10_src.py.in
  2. 26 1
      src/bin/bind10/tests/bind10_test.py.in

+ 4 - 1
src/bin/bind10/bind10_src.py.in

@@ -493,6 +493,9 @@ class BoB:
 
     # A couple of utility methods for starting processes...
 
+    def _make_process_info(self, name, args, c_channel_env):
+        return ProcessInfo(name, args, c_channel_env)
+
     def start_process(self, name, args, c_channel_env, port=None, address=None):
         """
             Given a set of command arguments, start the process and output
@@ -502,7 +505,7 @@ class BoB:
             The port and address arguments are for log messages only.
         """
         self.log_starting(name, port, address)
-        newproc = ProcessInfo(name, args, c_channel_env)
+        newproc = self._make_process_info(name, args, c_channel_env)
         newproc.spawn()
         self.log_started(newproc.pid)
         return newproc

+ 26 - 1
src/bin/bind10/tests/bind10_test.py.in

@@ -510,6 +510,22 @@ class TestBoB(unittest.TestCase):
         self.assertEqual({'command': ['shutdown', {'pid': 42}]},
                          bob.cc_session.msg)
 
+# Mock class for testing BoB's usage of ProcessInfo
+class MockProcessInfo:
+    def __init__(self, name, args, env={}, dev_null_stdout=False,
+                 dev_null_stderr=False):
+        self.name = name
+        self.args = args
+        self.env = env
+        self.dev_null_stdout = dev_null_stdout
+        self.dev_null_stderr = dev_null_stderr
+        self.process = None
+        self.pid = None
+
+    def spawn(self):
+        # set some pid (only used for testing that it is not None anymore)
+        self.pid = 42147
+
 # Class for testing the BoB without actually starting processes.
 # This is used for testing the start/stop components routines and
 # the BoB commands.
@@ -732,6 +748,9 @@ class MockBob(BoB):
     def _get_process_exit_status_raises_other(self):
         raise Exception('Mock error')
 
+    def _make_mock_process_info(self, name, args, c_channel_env):
+        return MockProcessInfo(name, args, c_channel_env)
+
 class MockBobSimple(BoB):
     def __init__(self):
         BoB.__init__(self)
@@ -1534,11 +1553,17 @@ class TestBossComponents(unittest.TestCase):
     def test_start_process(self):
         '''Test that processes can be started.'''
         bob = MockBob()
+
+        # use the MockProcessInfo creator
+        bob._make_process_info = bob._make_mock_process_info
+
         pi = bob.start_process('Test Process', ['/bin/true'], {})
         self.assertEqual('Test Process', pi.name)
         self.assertEqual(['/bin/true'], pi.args)
         self.assertEqual({}, pi.env)
-        self.assertNotEqual(0, pi.pid)
+
+        # this is set by ProcessInfo.spawn()
+        self.assertEqual(42147, pi.pid)
 
     def test_register_process(self):
         '''Test that processes can be registered with BoB.'''