test_beacons.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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')
  29. def test_add_and_delete(self):
  30. '''
  31. Test adding and deleting a beacon
  32. '''
  33. _add = self.run_function('beacons.add', ['ps', [{'processes': {'apache2': 'stopped'}}]])
  34. self.assertTrue(_add['result'])
  35. # save added beacon
  36. _save = self.run_function('beacons.save')
  37. self.assertTrue(_save['result'])
  38. # delete the beacon
  39. _delete = self.run_function('beacons.delete', ['ps'])
  40. self.assertTrue(_delete['result'])
  41. # save the results
  42. self.run_function('beacons.save')
  43. class BeaconsTest(ModuleCase):
  44. '''
  45. Tests the beacons execution module
  46. '''
  47. beacons_config_file_path = minion_conf_d_dir = None
  48. @classmethod
  49. def tearDownClass(cls):
  50. if cls.beacons_config_file_path and os.path.isfile(cls.beacons_config_file_path):
  51. os.unlink(cls.beacons_config_file_path)
  52. def setUp(self):
  53. if self.minion_conf_d_dir is None:
  54. self.minion_conf_d_dir = os.path.join(
  55. self.minion_opts['config_dir'],
  56. os.path.dirname(self.minion_opts['default_include']))
  57. if not os.path.isdir(self.minion_conf_d_dir):
  58. os.makedirs(self.minion_conf_d_dir)
  59. self.__class__.beacons_config_file_path = os.path.join(self.minion_conf_d_dir, 'beacons.conf')
  60. try:
  61. # Add beacon to disable
  62. self.run_function('beacons.add', ['ps', [{'processes': {'apache2': 'stopped'}}]])
  63. self.run_function('beacons.save')
  64. except CommandExecutionError:
  65. self.skipTest('Unable to add beacon')
  66. def tearDown(self):
  67. # delete added beacon
  68. self.run_function('beacons.delete', ['ps'])
  69. self.run_function('beacons.save')
  70. # Reset beacons
  71. self.run_function('beacons.reset')
  72. def test_disable(self):
  73. '''
  74. Test disabling beacons
  75. '''
  76. # assert beacon exists
  77. _list = self.run_function('beacons.list', return_yaml=False)
  78. self.assertIn('ps', _list)
  79. ret = self.run_function('beacons.disable')
  80. self.assertTrue(ret['result'])
  81. # assert beacons are disabled
  82. _list = self.run_function('beacons.list', return_yaml=False)
  83. self.assertFalse(_list['enabled'])
  84. # disable added beacon
  85. ret = self.run_function('beacons.disable_beacon', ['ps'])
  86. self.assertTrue(ret['result'])
  87. # assert beacon ps is disabled
  88. _list = self.run_function('beacons.list', return_yaml=False)
  89. for bdict in _list['ps']:
  90. if 'enabled' in bdict:
  91. self.assertFalse(bdict['enabled'])
  92. break
  93. def test_enable(self):
  94. '''
  95. Test enabling beacons
  96. '''
  97. # assert beacon exists
  98. _list = self.run_function('beacons.list', return_yaml=False)
  99. self.assertIn('ps', _list)
  100. # enable beacons on minion
  101. ret = self.run_function('beacons.enable')
  102. self.assertTrue(ret['result'])
  103. # assert beacons are enabled
  104. _list = self.run_function('beacons.list', return_yaml=False)
  105. self.assertTrue(_list['enabled'])
  106. @skipIf(True, 'Skip until https://github.com/saltstack/salt/issues/31516 problems are resolved.')
  107. def test_enabled_beacons(self):
  108. '''
  109. Test enabled specific beacon
  110. '''
  111. # enable added beacon
  112. ret = self.run_function('beacons.enable_beacon', ['ps'])
  113. self.assertTrue(ret['result'])
  114. # assert beacon ps is enabled
  115. _list = self.run_function('beacons.list', return_yaml=False)
  116. self.assertTrue(_list['ps']['enabled'])
  117. def test_list(self):
  118. '''
  119. Test lising the beacons
  120. '''
  121. # list beacons
  122. ret = self.run_function('beacons.list', return_yaml=False)
  123. if 'enabled' in ret:
  124. self.assertEqual(ret, {'ps': [{'processes': {'apache2': 'stopped'}}], 'enabled': True})
  125. else:
  126. self.assertEqual(ret, {'ps': [{'processes': {'apache2': 'stopped'}}]})