b10-cfgmgr_test.py.in 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. # $Id: cfgmgr_test.py 2126 2010-06-16 14:40:22Z jelte $
  16. #
  17. # Tests for the configuration manager run script
  18. #
  19. import unittest
  20. import os
  21. import sys
  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. if __name__ == '__main__':
  75. unittest.main()