test_monit.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Rupesh Tare <rupesht@saltstack.com>
  4. """
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.modules.monit as monit
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.mock import MagicMock, patch
  12. from tests.support.unit import TestCase
  13. class MonitTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.modules.aptpkg
  16. """
  17. def setup_loader_modules(self):
  18. return {monit: {}}
  19. def test_start(self):
  20. """
  21. Test for start
  22. """
  23. with patch.dict(monit.__salt__, {"cmd.retcode": MagicMock(return_value=False)}):
  24. self.assertTrue(monit.start("name"))
  25. def test_stop(self):
  26. """
  27. Test for Stops service via monit
  28. """
  29. with patch.dict(monit.__salt__, {"cmd.retcode": MagicMock(return_value=False)}):
  30. self.assertTrue(monit.stop("name"))
  31. def test_restart(self):
  32. """
  33. Test for Restart service via monit
  34. """
  35. with patch.dict(monit.__salt__, {"cmd.retcode": MagicMock(return_value=False)}):
  36. self.assertTrue(monit.restart("name"))
  37. def test_unmonitor(self):
  38. """
  39. Test for Unmonitor service via monit
  40. """
  41. with patch.dict(monit.__salt__, {"cmd.retcode": MagicMock(return_value=False)}):
  42. self.assertTrue(monit.unmonitor("name"))
  43. def test_monitor(self):
  44. """
  45. Test for monitor service via monit
  46. """
  47. with patch.dict(monit.__salt__, {"cmd.retcode": MagicMock(return_value=False)}):
  48. self.assertTrue(monit.monitor("name"))
  49. def test_summary(self):
  50. """
  51. Test for Display a summary from monit
  52. """
  53. mock = MagicMock(side_effect=["daemon is not running", "A\nB\nC\nD\nE"])
  54. with patch.dict(monit.__salt__, {"cmd.run": mock}):
  55. self.assertEqual(
  56. monit.summary(), {"monit": "daemon is not running", "result": False}
  57. )
  58. self.assertEqual(monit.summary(), {})
  59. def test_status(self):
  60. """
  61. Test for Display a process status from monit
  62. """
  63. with patch.dict(monit.__salt__, {"cmd.run": MagicMock(return_value="Process")}):
  64. self.assertEqual(monit.status("service"), "No such service")
  65. def test_reload(self):
  66. """
  67. Test for Reload configuration
  68. """
  69. mock = MagicMock(return_value=0)
  70. with patch.dict(monit.__salt__, {"cmd.retcode": mock}):
  71. self.assertTrue(monit.reload_())
  72. def test_version(self):
  73. """
  74. Test for Display version from monit -V
  75. """
  76. mock = MagicMock(return_value="This is Monit version 5.14\nA\nB")
  77. with patch.dict(monit.__salt__, {"cmd.run": mock}):
  78. self.assertEqual(monit.version(), "5.14")
  79. def test_id(self):
  80. """
  81. Test for Display unique id
  82. """
  83. mock = MagicMock(return_value="Monit ID: d3b1aba48527dd599db0e86f5ad97120")
  84. with patch.dict(monit.__salt__, {"cmd.run": mock}):
  85. self.assertEqual(monit.id_(), "d3b1aba48527dd599db0e86f5ad97120")
  86. def test_reset_id(self):
  87. """
  88. Test for Regenerate a unique id
  89. """
  90. expected = {"stdout": "Monit id d3b1aba48527dd599db0e86f5ad97120 and ..."}
  91. mock = MagicMock(return_value=expected)
  92. with patch.dict(monit.__salt__, {"cmd.run_all": mock}):
  93. self.assertEqual(monit.id_(reset=True), "d3b1aba48527dd599db0e86f5ad97120")
  94. def test_configtest(self):
  95. """
  96. Test for Check configuration syntax
  97. """
  98. excepted = {"stdout": "Control file syntax OK", "retcode": 0, "stderr": ""}
  99. mock = MagicMock(return_value=excepted)
  100. with patch.dict(monit.__salt__, {"cmd.run_all": mock}):
  101. self.assertTrue(monit.configtest()["result"])
  102. self.assertEqual(monit.configtest()["comment"], "Syntax OK")
  103. def test_validate(self):
  104. """
  105. Test for Check all services are monitored
  106. """
  107. mock = MagicMock(return_value=0)
  108. with patch.dict(monit.__salt__, {"cmd.retcode": mock}):
  109. self.assertTrue(monit.validate())