|
@@ -0,0 +1,137 @@
|
|
|
+# Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
|
|
|
+#
|
|
|
+# Permission to use, copy, modify, and distribute this software for any
|
|
|
+# purpose with or without fee is hereby granted, provided that the above
|
|
|
+# copyright notice and this permission notice appear in all copies.
|
|
|
+#
|
|
|
+# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
|
|
|
+# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
|
|
|
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
|
|
|
+# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
+# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
|
|
|
+# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
+# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
|
|
|
+# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
+
|
|
|
+"""
|
|
|
+Tests for the bind10.component module
|
|
|
+"""
|
|
|
+
|
|
|
+import unittest
|
|
|
+import isc.log
|
|
|
+from isc.bind10.component import Component
|
|
|
+
|
|
|
+class ComponentTests(unittest.TestCase):
|
|
|
+ """
|
|
|
+ Tests for the bind10.component.Component class
|
|
|
+ """
|
|
|
+ def setUp(self):
|
|
|
+ """
|
|
|
+ Pretend a newly started system
|
|
|
+ """
|
|
|
+ self.__shutdown = False
|
|
|
+ self.__exitcode = None
|
|
|
+ self.__start_called = False
|
|
|
+ self.__stop_called = False
|
|
|
+ self.__failed_called = False
|
|
|
+
|
|
|
+ def shutdown(self, exitcode=0):
|
|
|
+ """
|
|
|
+ Mock function to shut down. We just note we were asked to do so.
|
|
|
+ """
|
|
|
+ self.__shutdown = True
|
|
|
+ self.__exitcode = None
|
|
|
+
|
|
|
+ def start(self):
|
|
|
+ """
|
|
|
+ Mock function, installed into the component into start_internal.
|
|
|
+ This only notes the component was "started".
|
|
|
+ """
|
|
|
+ self.__start_called = True
|
|
|
+
|
|
|
+ def stop(self):
|
|
|
+ """
|
|
|
+ Mock function, installed into the component into stop_internal.
|
|
|
+ This only notes the component was "stopped".
|
|
|
+ """
|
|
|
+ self.__stop_called = True
|
|
|
+
|
|
|
+ def fail(self):
|
|
|
+ """
|
|
|
+ Mock function, installed into the component into failed_internal.
|
|
|
+ This only notes the component called the method.
|
|
|
+ """
|
|
|
+ self.__failed_called = True
|
|
|
+
|
|
|
+ def create_component(self, kind):
|
|
|
+ """
|
|
|
+ Convenience function that creates a component of given kind
|
|
|
+ and installs the mock functions into it so we can hook up into
|
|
|
+ its behaviour.
|
|
|
+
|
|
|
+ The process used is some nonsense, as this isn't used in this
|
|
|
+ kind of tests and we pretend to be the boss.
|
|
|
+ """
|
|
|
+ component = Component('No process', self, kind)
|
|
|
+ component.start_internal = self.start
|
|
|
+ component.stop_internal = self.stop
|
|
|
+ component.failed_internal = self.fail
|
|
|
+ return component
|
|
|
+
|
|
|
+ def do_start_stop(self, kind):
|
|
|
+ """
|
|
|
+ This is a body of a test. It creates a componend of given kind,
|
|
|
+ then starts it and stops it. It checks correct functions are called
|
|
|
+ and the component's status is correct.
|
|
|
+
|
|
|
+ It also checks the component can't be started/stopped twice.
|
|
|
+ """
|
|
|
+ # Create it and check it did not do any funny stuff yet
|
|
|
+ component = self.create_component(kind)
|
|
|
+ self.assertFalse(self.__shutdown)
|
|
|
+ self.assertFalse(self.__start_called)
|
|
|
+ self.assertFalse(self.__stop_called)
|
|
|
+ self.assertFalse(self.__failed_called)
|
|
|
+ self.assertFalse(component.running())
|
|
|
+ # Start it and check it called the correct starting functions
|
|
|
+ component.start()
|
|
|
+ self.assertFalse(self.__shutdown)
|
|
|
+ self.assertTrue(self.__start_called)
|
|
|
+ self.assertFalse(self.__stop_called)
|
|
|
+ self.assertFalse(self.__failed_called)
|
|
|
+ self.assertTrue(component.running())
|
|
|
+ # Check it can't be started twice
|
|
|
+ self.assertRaises(ValueError, component.start)
|
|
|
+ # Stop it again and check
|
|
|
+ component.stop()
|
|
|
+ self.assertFalse(self.__shutdown)
|
|
|
+ self.assertTrue(self.__start_called)
|
|
|
+ self.assertTrue(self.__stop_called)
|
|
|
+ self.assertFalse(self.__failed_called)
|
|
|
+ self.assertFalse(component.running())
|
|
|
+ # Check it can't be stopped twice
|
|
|
+ self.assertRaises(ValueError, component.stop)
|
|
|
+ # But it can be started again if it is stopped
|
|
|
+ # (no more checking here, just it doesn't crash)
|
|
|
+ component.start()
|
|
|
+
|
|
|
+ def test_start_stop_core(self):
|
|
|
+ """
|
|
|
+ A start-stop test for core component. See do_start_stop.
|
|
|
+ """
|
|
|
+ self.do_start_stop('core')
|
|
|
+ def test_start_stop_needed(self):
|
|
|
+ """
|
|
|
+ A start-stop test for needed component. See do_start_stop.
|
|
|
+ """
|
|
|
+ self.do_start_stop('needed')
|
|
|
+ def test_start_stop_dispensable(self):
|
|
|
+ """
|
|
|
+ A start-stop test for dispensable component. See do_start_stop.
|
|
|
+ """
|
|
|
+ self.do_start_stop('dispensable')
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ isc.log.init("bind10") # FIXME Should this be needed?
|
|
|
+ isc.log.resetUnitTestRootLogger()
|
|
|
+ unittest.main()
|