1
0

test_monit.py 4.6 KB

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