b10-cfgmgr_test.py.in 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. from optparse import OptionParser
  22. class MyConfigManager:
  23. def __init__(self, path):
  24. self._path = path
  25. self.read_config_called = False
  26. self.notify_boss_called = False
  27. self.run_called = False
  28. self.write_config_called = False
  29. self.running = True
  30. def read_config(self):
  31. self.read_config_called = True
  32. def notify_boss(self):
  33. self.notify_boss_called = True
  34. def run(self):
  35. self.run_called = True
  36. def write_config(self):
  37. self.write_config_called = True
  38. class TestConfigManagerStartup(unittest.TestCase):
  39. def test_cfgmgr(self):
  40. # some creative module use;
  41. # b10-cfgmgr has a hypen, so we use __import__
  42. # this also gives us the chance to override the imported
  43. # module ConfigManager in it.
  44. b = __import__("b10-cfgmgr")
  45. b.ConfigManager = MyConfigManager
  46. b.main()
  47. self.assertTrue(b.cm.read_config_called)
  48. self.assertTrue(b.cm.notify_boss_called)
  49. self.assertTrue(b.cm.run_called)
  50. # if there are no changes, config is not written
  51. self.assertFalse(b.cm.write_config_called)
  52. self.assertTrue(b.cm.running)
  53. b.signal_handler(None, None)
  54. self.assertFalse(b.cm.running)
  55. # TODO: take value from the 'global config module'
  56. # (and rename the .in away from this file again)
  57. data_path = "@localstatedir@/@PACKAGE@".replace("${prefix}", "@prefix@")
  58. self.assertEqual(data_path, b.DATA_PATH)
  59. # remove the 'module' again, or later tests may fail
  60. # (if it is already present it won't be loaded again)
  61. sys.modules.pop("b10-cfgmgr")
  62. def test_cfgmgr_from_source(self):
  63. tmp_env_var = "/just/some/dir"
  64. env_var = None
  65. if "B10_FROM_SOURCE" in os.environ:
  66. env_var = os.environ["B10_FROM_SOURCE"]
  67. os.environ["B10_FROM_SOURCE"] = tmp_env_var
  68. b = __import__("b10-cfgmgr", globals(), locals())
  69. b.ConfigManager = MyConfigManager
  70. self.assertEqual(tmp_env_var, b.DATA_PATH)
  71. if env_var != None:
  72. os.environ["B10_FROM_SOURCE"] = env_var
  73. sys.modules.pop("b10-cfgmgr")
  74. class OptsError(Exception):
  75. """To know when OptionParser would exit"""
  76. pass
  77. class TestOptParser(OptionParser):
  78. """
  79. We define our own option parser to push into the parsing routine.
  80. This one does not exit the whole application on error, it just raises
  81. exception. It doesn't change anything else. The application uses the
  82. stock one.
  83. """
  84. def error(self, message):
  85. raise OptsError(message)
  86. class TestParseArgs(unittest.TestCase):
  87. """
  88. Test for the parsing of command line arguments. We provide a different
  89. array to parse instead.
  90. """
  91. def test_defaults(self):
  92. """
  93. Test the default values when no options are provided.
  94. """
  95. # Pass it empty array, not our arguments
  96. b = __import__("b10-cfgmgr")
  97. parsed = b.parse_options([], TestOptParser)
  98. self.assertEqual(b.DATA_PATH, parsed.data_path)
  99. self.assertEqual("b10-config.db", parsed.file_name)
  100. def test_wrong_args(self):
  101. """
  102. Test it fails when we pass invalid option.
  103. """
  104. b = __import__("b10-cfgmgr")
  105. self.assertRaises(OptsError, b.parse_options, (['--wrong-option']),
  106. TestOptParser)
  107. def test_not_arg(self):
  108. """
  109. Test it fails when there's an argument that's not option
  110. (eg. without -- at the beginning).
  111. """
  112. b = __import__("b10-cfgmgr")
  113. self.assertRaises(OptsError, b.parse_options, (['not-option']),
  114. TestOptParser)
  115. def test_datapath(self):
  116. """
  117. Test overwriting the data path.
  118. """
  119. b = __import__("b10-cfgmgr")
  120. parsed = b.parse_options(['--data-path=/path'], TestOptParser)
  121. self.assertEqual('/path', parsed.data_path)
  122. self.assertEqual("b10-config.db", parsed.file_name)
  123. def test_db_filename(self):
  124. """
  125. Test setting the configuration database file.
  126. """
  127. b = __import__("b10-cfgmgr")
  128. parsed = b.parse_options(['--database-filename=filename'],
  129. TestOptParser)
  130. self.assertEqual(b.DATA_PATH, parsed.data_path)
  131. self.assertEqual("filename", parsed.file_name)
  132. if __name__ == '__main__':
  133. unittest.main()