Browse Source

[213] Test for fail-to-start

If the starting function throws an exception
Michal 'vorner' Vaner 13 years ago
parent
commit
f500fc46e6
1 changed files with 51 additions and 0 deletions
  1. 51 0
      src/lib/python/isc/bind10/tests/component_test.py

+ 51 - 0
src/lib/python/isc/bind10/tests/component_test.py

@@ -22,6 +22,12 @@ import isc.log
 import time
 from isc.bind10.component import Component
 
+class TestError(Exception):
+    """
+    Just a private exception not known to anybody we use for our tests.
+    """
+    pass
+
 class ComponentTests(unittest.TestCase):
     """
     Tests for the bind10.component.Component class
@@ -77,6 +83,18 @@ class ComponentTests(unittest.TestCase):
         """
         self.__failed_called = True
 
+    def fail_to_start(self):
+        """
+        Mock function. It can be installed into the component's start_internal
+        to simulate a component that fails to start by raising an exception.
+        """
+        orig_started = self.__start_called
+        self.__start_called = True
+        if not orig_started:
+            # This one is from restart. Avoid infinite recursion for now.
+            # FIXME: We should use the restart scheduler to avoid it, not this.
+            raise TestError("Test error")
+
     def create_component(self, kind):
         """
         Convenience function that creates a component of given kind
@@ -289,6 +307,39 @@ class ComponentTests(unittest.TestCase):
         component.failed()
         self.check_restarted(component)
 
+    def test_fail_core(self):
+        """
+        Failure to start a core component. Should bring the system down
+        and the exception should get through.
+        """
+        component = self.create_component('core')
+        self.check_startup(component)
+        component.start_internal = self.fail_to_start
+        self.assertRaises(TestError, component.start)
+        self.check_dead(component)
+
+    def test_fail_needed(self):
+        """
+        Failure to start a needed component. Should bring the system down
+        and the exception should get through.
+        """
+        component = self.create_component('needed')
+        self.check_startup(component)
+        component.start_internal = self.fail_to_start
+        self.assertRaises(TestError, component.start)
+        self.check_dead(component)
+
+    def test_fail_dispensable(self):
+        """
+        Failure to start a dispensable component. The exception should get
+        through, but it should be restarted.
+        """
+        component = self.create_component('dispensable')
+        self.check_startup(component)
+        component.start_internal = self.fail_to_start
+        self.assertRaises(TestError, component.start)
+        self.check_restarted(component)
+
 if __name__ == '__main__':
     isc.log.init("bind10") # FIXME Should this be needed?
     isc.log.resetUnitTestRootLogger()