test_beacons.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Justin Anderson <janderson@saltstack.com>
  4. '''
  5. # Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import os
  8. # Salt Libs
  9. from salt.exceptions import CommandExecutionError
  10. # Salttesting libs
  11. from tests.support.case import ModuleCase
  12. from tests.support.unit import skipIf
  13. class BeaconsAddDeleteTest(ModuleCase):
  14. '''
  15. Tests the add and delete functions
  16. '''
  17. def setUp(self):
  18. self.minion_conf_d_dir = os.path.join(
  19. self.minion_opts['config_dir'],
  20. os.path.dirname(self.minion_opts['default_include']))
  21. if not os.path.isdir(self.minion_conf_d_dir):
  22. os.makedirs(self.minion_conf_d_dir)
  23. self.beacons_config_file_path = os.path.join(self.minion_conf_d_dir, 'beacons.conf')
  24. def tearDown(self):
  25. if os.path.isfile(self.beacons_config_file_path):
  26. os.unlink(self.beacons_config_file_path)
  27. # Reset beacons
  28. self.run_function('beacons.reset', f_timeout=300)
  29. def test_add_and_delete(self):
  30. '''
  31. Test adding and deleting a beacon
  32. '''
  33. _add = self.run_function(
  34. 'beacons.add',
  35. ['ps', [{'processes': {'apache2': 'stopped'}}]],
  36. f_timeout=300
  37. )
  38. self.assertTrue(_add['result'])
  39. # save added beacon
  40. _save = self.run_function('beacons.save', f_timeout=300)
  41. self.assertTrue(_save['result'])
  42. # delete the beacon
  43. _delete = self.run_function('beacons.delete', ['ps'], f_timeout=300)
  44. self.assertTrue(_delete['result'])
  45. # save the results
  46. self.run_function('beacons.save', f_timeout=300)
  47. class BeaconsTest(ModuleCase):
  48. '''
  49. Tests the beacons execution module
  50. '''
  51. beacons_config_file_path = minion_conf_d_dir = None
  52. @classmethod
  53. def tearDownClass(cls):
  54. if cls.beacons_config_file_path and os.path.isfile(cls.beacons_config_file_path):
  55. os.unlink(cls.beacons_config_file_path)
  56. def setUp(self):
  57. if self.minion_conf_d_dir is None:
  58. self.minion_conf_d_dir = os.path.join(
  59. self.minion_opts['config_dir'],
  60. os.path.dirname(self.minion_opts['default_include']))
  61. if not os.path.isdir(self.minion_conf_d_dir):
  62. os.makedirs(self.minion_conf_d_dir)
  63. self.__class__.beacons_config_file_path = os.path.join(self.minion_conf_d_dir, 'beacons.conf')
  64. try:
  65. # Add beacon to disable
  66. self.run_function('beacons.add',
  67. ['ps', [{'processes': {'apache2': 'stopped'}}]],
  68. f_timeout=300)
  69. self.run_function('beacons.save', f_timeout=300)
  70. except CommandExecutionError:
  71. self.skipTest('Unable to add beacon')
  72. def tearDown(self):
  73. # delete added beacon
  74. self.run_function('beacons.delete', ['ps'], f_timeout=300)
  75. self.run_function('beacons.save', f_timeout=300)
  76. # Reset beacons
  77. self.run_function('beacons.reset', f_timeout=300)
  78. def test_disable(self):
  79. '''
  80. Test disabling beacons
  81. '''
  82. # assert beacon exists
  83. _list = self.run_function('beacons.list',
  84. return_yaml=False,
  85. f_timeout=300)
  86. self.assertIn('ps', _list)
  87. ret = self.run_function('beacons.disable', f_timeout=300)
  88. self.assertTrue(ret['result'])
  89. # assert beacons are disabled
  90. _list = self.run_function('beacons.list',
  91. return_yaml=False,
  92. f_timeout=300)
  93. self.assertFalse(_list['enabled'])
  94. # disable added beacon
  95. ret = self.run_function('beacons.disable_beacon', ['ps'], f_timeout=300)
  96. self.assertTrue(ret['result'])
  97. # assert beacon ps is disabled
  98. _list = self.run_function('beacons.list',
  99. return_yaml=False,
  100. f_timeout=300)
  101. for bdict in _list['ps']:
  102. if 'enabled' in bdict:
  103. self.assertFalse(bdict['enabled'])
  104. break
  105. def test_enable(self):
  106. '''
  107. Test enabling beacons
  108. '''
  109. # assert beacon exists
  110. _list = self.run_function('beacons.list',
  111. return_yaml=False,
  112. f_timeout=300)
  113. self.assertIn('ps', _list)
  114. # enable beacons on minion
  115. ret = self.run_function('beacons.enable', f_timeout=300)
  116. self.assertTrue(ret['result'])
  117. # assert beacons are enabled
  118. _list = self.run_function('beacons.list',
  119. return_yaml=False,
  120. f_timeout=300)
  121. self.assertTrue(_list['enabled'])
  122. @skipIf(True, 'Skip until https://github.com/saltstack/salt/issues/31516 '
  123. 'problems are resolved.')
  124. def test_enabled_beacons(self):
  125. '''
  126. Test enabled specific beacon
  127. '''
  128. # enable added beacon
  129. ret = self.run_function('beacons.enable_beacon', ['ps'], f_timeout=300)
  130. self.assertTrue(ret['result'])
  131. # assert beacon ps is enabled
  132. _list = self.run_function('beacons.list',
  133. return_yaml=False,
  134. f_timeout=300)
  135. self.assertTrue(_list['ps']['enabled'])
  136. def test_list(self):
  137. '''
  138. Test listing the beacons
  139. '''
  140. # list beacons
  141. ret = self.run_function('beacons.list',
  142. return_yaml=False,
  143. f_timeout=300)
  144. if 'enabled' in ret:
  145. self.assertEqual(ret, {'ps': [{'processes': {'apache2': 'stopped'}}], 'enabled': True})
  146. else:
  147. self.assertEqual(ret, {'ps': [{'processes': {'apache2': 'stopped'}}]})