1
0

test_blackout.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Tests for minion blackout
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import
  7. import os
  8. import time
  9. import logging
  10. import textwrap
  11. # Import Salt Testing libs
  12. from tests.support.case import ModuleCase
  13. from tests.support.helpers import flaky
  14. from tests.support.runtests import RUNTIME_VARS
  15. # Import Salt libs
  16. import salt.utils.files
  17. log = logging.getLogger(__name__)
  18. class MinionBlackoutTestCase(ModuleCase):
  19. '''
  20. Test minion blackout functionality
  21. '''
  22. @classmethod
  23. def setUpClass(cls):
  24. cls.top_pillar = os.path.join(RUNTIME_VARS.TMP_PILLAR_TREE, 'top.sls')
  25. cls.blackout_pillar = os.path.join(RUNTIME_VARS.TMP_PILLAR_TREE, 'blackout.sls')
  26. @classmethod
  27. def tearDownClass(cls):
  28. if os.path.exists(cls.top_pillar):
  29. os.unlink(cls.top_pillar)
  30. del cls.top_pillar
  31. if os.path.exists(cls.blackout_pillar):
  32. os.unlink(cls.blackout_pillar)
  33. del cls.blackout_pillar
  34. def setUp(self):
  35. with salt.utils.files.fopen(self.top_pillar, 'w') as wfh:
  36. wfh.write(textwrap.dedent('''\
  37. base:
  38. '*':
  39. - blackout
  40. '''))
  41. with salt.utils.files.fopen(self.blackout_pillar, 'w') as wfh:
  42. wfh.write('minion_blackout: False')
  43. self.addCleanup(self.cleanup_blackout_pillar)
  44. def tearDown(self):
  45. self.end_blackout(sleep=False)
  46. # Be sure to also refresh the sub_minion pillar
  47. self.run_function('saltutil.refresh_pillar', minion_tgt='sub_minion')
  48. time.sleep(10) # wait for minion to exit blackout mode
  49. self.wait_for_all_jobs()
  50. def cleanup_blackout_pillar(self):
  51. if os.path.exists(self.top_pillar):
  52. os.unlink(self.top_pillar)
  53. if os.path.exists(self.blackout_pillar):
  54. os.unlink(self.blackout_pillar)
  55. def begin_blackout(self, blackout_data='minion_blackout: True'):
  56. '''
  57. setup minion blackout mode
  58. '''
  59. log.info('Entering minion blackout...')
  60. self.wait_for_all_jobs()
  61. with salt.utils.files.fopen(self.blackout_pillar, 'w') as wfh:
  62. wfh.write(blackout_data)
  63. self.run_function('saltutil.refresh_pillar')
  64. time.sleep(10) # wait for minion to enter blackout mode
  65. log.info('Entered minion blackout.')
  66. def end_blackout(self, sleep=True):
  67. '''
  68. takedown minion blackout mode
  69. '''
  70. log.info('Exiting minion blackout...')
  71. with salt.utils.files.fopen(self.blackout_pillar, 'w') as wfh:
  72. wfh.write('minion_blackout: False\n')
  73. self.run_function('saltutil.refresh_pillar')
  74. if sleep:
  75. time.sleep(10) # wait for minion to exit blackout mode
  76. self.wait_for_all_jobs()
  77. log.info('Exited minion blackout.')
  78. @flaky
  79. def test_blackout(self):
  80. '''
  81. Test that basic minion blackout functionality works
  82. '''
  83. try:
  84. self.begin_blackout()
  85. blackout_ret = self.run_function('test.ping')
  86. self.assertIn('Minion in blackout mode.', blackout_ret)
  87. finally:
  88. self.end_blackout()
  89. ret = self.run_function('test.ping')
  90. self.assertEqual(ret, True)
  91. @flaky
  92. def test_blackout_whitelist(self):
  93. '''
  94. Test that minion blackout whitelist works
  95. '''
  96. self.begin_blackout(textwrap.dedent('''\
  97. minion_blackout: True
  98. minion_blackout_whitelist:
  99. - test.ping
  100. - test.fib
  101. '''))
  102. ping_ret = self.run_function('test.ping')
  103. self.assertEqual(ping_ret, True)
  104. fib_ret = self.run_function('test.fib', [7])
  105. self.assertTrue(isinstance(fib_ret, list))
  106. self.assertEqual(fib_ret[0], 13)
  107. @flaky
  108. def test_blackout_nonwhitelist(self):
  109. '''
  110. Test that minion refuses to run non-whitelisted functions during
  111. blackout whitelist
  112. '''
  113. self.begin_blackout(textwrap.dedent('''\
  114. minion_blackout: True
  115. minion_blackout_whitelist:
  116. - test.ping
  117. - test.fib
  118. '''))
  119. state_ret = self.run_function('state.apply')
  120. self.assertIn('Minion in blackout mode.', state_ret)
  121. cloud_ret = self.run_function('cloud.query', ['list_nodes_full'])
  122. self.assertIn('Minion in blackout mode.', cloud_ret)