b10-cfgmgr_test.py.in 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # Copyright (C) 2010 Internet Systems Consortium.
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. #
  16. # Tests for the configuration manager run script
  17. #
  18. import unittest
  19. import os
  20. import sys
  21. import bind10_config
  22. from isc.testutils.parse_args import OptsError, TestOptParser
  23. class MyConfigManager:
  24. def __init__(self, path, filename, session=None, rename_config_file=False):
  25. self._path = path
  26. self.read_config_called = False
  27. self.notify_boss_called = False
  28. self.run_called = False
  29. self.write_config_called = False
  30. self.rename_config_called = False
  31. self.running = True
  32. self.virtual_modules = []
  33. def read_config(self):
  34. self.read_config_called = True
  35. def notify_boss(self):
  36. self.notify_boss_called = True
  37. def run(self):
  38. self.run_called = True
  39. def write_config(self):
  40. self.write_config_called = True
  41. def rename_config_file(self, ofile, nfile):
  42. self.rename_config_called = True
  43. def set_virtual_module(self, spec, function):
  44. self.virtual_modules.append((spec, function))
  45. class TestPlugins(unittest.TestCase):
  46. def test_load_plugins(self):
  47. """Test we can successfully find and load the mock plugin."""
  48. # Let it load the plugin
  49. b = __import__("b10-cfgmgr")
  50. # The parameters aren't important for this test
  51. cm = MyConfigManager(None, None)
  52. b.load_plugins(os.environ['TESTDATA_PATH'] + os.sep + 'plugins', cm)
  53. # Check exactly one plugin was loaded and the right data were fed into
  54. # the cm
  55. self.assertEqual(len(cm.virtual_modules), 1)
  56. (spec, check) = cm.virtual_modules[0]
  57. self.assertEqual(spec.get_module_name(), "mock_config_plugin")
  58. self.assertEqual(check(None), "Mock config plugin")
  59. class TestConfigManagerStartup(unittest.TestCase):
  60. def test_cfgmgr(self):
  61. # some creative module use;
  62. # b10-cfgmgr has a hypen, so we use __import__
  63. # this also gives us the chance to override the imported
  64. # module ConfigManager in it.
  65. b = __import__("b10-cfgmgr")
  66. orig_load = b.load_plugins
  67. b.PLUGIN_PATHS = ["/plugin/path"]
  68. self.loaded_plugins = False
  69. def load_plugins(path, cm):
  70. # Check it's called with proper arguments
  71. self.assertEqual(path, "/plugin/path")
  72. self.assertTrue(isinstance(cm, MyConfigManager))
  73. self.loaded_plugins = True
  74. b.load_plugins = load_plugins
  75. b.ConfigManager = MyConfigManager
  76. b.main()
  77. b.load_plugins = orig_load
  78. self.assertTrue(b.cm.read_config_called)
  79. self.assertTrue(b.cm.notify_boss_called)
  80. self.assertTrue(b.cm.run_called)
  81. self.assertTrue(self.loaded_plugins)
  82. # if there are no changes, config is not written
  83. self.assertFalse(b.cm.write_config_called)
  84. self.assertFalse(b.cm.rename_config_called)
  85. self.assertTrue(b.cm.running)
  86. b.signal_handler(None, None)
  87. self.assertFalse(b.cm.running)
  88. # TODO: take value from the 'global config module'
  89. # (and rename the .in away from this file again)
  90. data_path = "@localstatedir@/@PACKAGE@".replace("${prefix}", "@prefix@")
  91. self.assertEqual(data_path, b.DATA_PATH)
  92. # remove the 'module' again, or later tests may fail
  93. # (if it is already present it won't be loaded again)
  94. sys.modules.pop("b10-cfgmgr")
  95. def test_cfgmgr_from_source(self):
  96. tmp_env_var = "/just/some/dir"
  97. env_var = None
  98. if "B10_FROM_SOURCE" in os.environ:
  99. env_var = os.environ["B10_FROM_SOURCE"]
  100. os.environ["B10_FROM_SOURCE"] = tmp_env_var
  101. bind10_config.reload()
  102. b = __import__("b10-cfgmgr", globals(), locals())
  103. b.PLUGIN_PATH = [] # It's enough to test plugins in one test
  104. b.ConfigManager = MyConfigManager
  105. self.assertEqual(tmp_env_var, b.DATA_PATH)
  106. if env_var != None:
  107. os.environ["B10_FROM_SOURCE"] = env_var
  108. bind10_config.reload()
  109. sys.modules.pop("b10-cfgmgr")
  110. class TestParseArgs(unittest.TestCase):
  111. """
  112. Test for the parsing of command line arguments. We provide a different
  113. array to parse instead.
  114. """
  115. def test_defaults(self):
  116. """
  117. Test the default values when no options are provided.
  118. """
  119. # Pass it empty array, not our arguments
  120. b = __import__("b10-cfgmgr")
  121. parsed = b.parse_options([], TestOptParser)
  122. self.assertEqual(b.DATA_PATH, parsed.data_path)
  123. self.assertEqual(b.DEFAULT_CONFIG_FILE, parsed.config_file)
  124. def test_wrong_args(self):
  125. """
  126. Test it fails when we pass invalid option.
  127. """
  128. b = __import__("b10-cfgmgr")
  129. self.assertRaises(OptsError, b.parse_options, ['--wrong-option'],
  130. TestOptParser)
  131. def test_not_arg(self):
  132. """
  133. Test it fails when there's an argument that's not option
  134. (eg. without -- at the beginning).
  135. """
  136. b = __import__("b10-cfgmgr")
  137. self.assertRaises(OptsError, b.parse_options, ['not-option'],
  138. TestOptParser)
  139. def test_datapath(self):
  140. """
  141. Test overwriting the data path.
  142. """
  143. b = __import__("b10-cfgmgr")
  144. parsed = b.parse_options(['--data-path=/path'], TestOptParser)
  145. self.assertEqual('/path', parsed.data_path)
  146. self.assertEqual(b.DEFAULT_CONFIG_FILE, parsed.config_file)
  147. parsed = b.parse_options(['-p', '/path'], TestOptParser)
  148. self.assertEqual('/path', parsed.data_path)
  149. self.assertEqual(b.DEFAULT_CONFIG_FILE, parsed.config_file)
  150. self.assertRaises(OptsError, b.parse_options, ['-p'], TestOptParser)
  151. self.assertRaises(OptsError, b.parse_options, ['--data-path'],
  152. TestOptParser)
  153. def test_db_filename(self):
  154. """
  155. Test setting the configuration database file.
  156. """
  157. b = __import__("b10-cfgmgr")
  158. parsed = b.parse_options(['--config-filename=filename'],
  159. TestOptParser)
  160. self.assertEqual(b.DATA_PATH, parsed.data_path)
  161. self.assertEqual("filename", parsed.config_file)
  162. parsed = b.parse_options(['-c', 'filename'], TestOptParser)
  163. self.assertEqual(b.DATA_PATH, parsed.data_path)
  164. self.assertEqual("filename", parsed.config_file)
  165. self.assertRaises(OptsError, b.parse_options, ['-c'], TestOptParser)
  166. self.assertRaises(OptsError, b.parse_options, ['--config-filename'],
  167. TestOptParser)
  168. def test_clear_config(self):
  169. b = __import__("b10-cfgmgr")
  170. parsed = b.parse_options([], TestOptParser)
  171. self.assertFalse(parsed.clear_config)
  172. parsed = b.parse_options(['--clear-config'], TestOptParser)
  173. self.assertTrue(parsed.clear_config)
  174. if __name__ == '__main__':
  175. unittest.main(verbosity=2)