test_mine.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Test the salt mine system
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import time
  8. import pprint
  9. # Import Salt Testing libs
  10. from tests.support.case import ModuleCase
  11. import pytest
  12. @pytest.mark.windows_whitelisted
  13. class MineTest(ModuleCase):
  14. '''
  15. Test the mine system
  16. '''
  17. def setUp(self):
  18. self.wait_for_all_jobs()
  19. def test_get(self):
  20. '''
  21. test mine.get and mine.update
  22. '''
  23. self.assertTrue(self.run_function('mine.update', minion_tgt='minion'))
  24. # The sub_minion does not have mine_functions defined in its configuration
  25. # In this case, mine.update returns None
  26. self.assertIsNone(
  27. self.run_function(
  28. 'mine.update',
  29. minion_tgt='sub_minion'
  30. )
  31. )
  32. # Since the minion has mine_functions defined in its configuration,
  33. # mine.update will return True
  34. self.assertTrue(
  35. self.run_function(
  36. 'mine.get',
  37. ['minion', 'test.ping']
  38. )
  39. )
  40. def test_send(self):
  41. '''
  42. test mine.send
  43. '''
  44. self.assertFalse(
  45. self.run_function(
  46. 'mine.send',
  47. ['foo.__spam_and_cheese']
  48. )
  49. )
  50. self.assertTrue(
  51. self.run_function(
  52. 'mine.send',
  53. ['grains.items'],
  54. minion_tgt='minion',
  55. )
  56. )
  57. self.assertTrue(
  58. self.run_function(
  59. 'mine.send',
  60. ['grains.items'],
  61. minion_tgt='sub_minion',
  62. )
  63. )
  64. ret = self.run_function(
  65. 'mine.get',
  66. ['sub_minion', 'grains.items']
  67. )
  68. self.assertEqual(ret['sub_minion']['id'], 'sub_minion')
  69. ret = self.run_function(
  70. 'mine.get',
  71. ['minion', 'grains.items'],
  72. minion_tgt='sub_minion'
  73. )
  74. self.assertEqual(ret['minion']['id'], 'minion')
  75. def test_mine_flush(self):
  76. '''
  77. Test mine.flush
  78. '''
  79. # TODO The calls to sleep were added in an attempt to make this tests
  80. # less flaky. If we still see it fail we need to look for a more robust
  81. # solution.
  82. for minion_id in ('minion', 'sub_minion'):
  83. self.assertTrue(
  84. self.run_function(
  85. 'mine.send',
  86. ['grains.items'],
  87. minion_tgt=minion_id
  88. )
  89. )
  90. time.sleep(1)
  91. for minion_id in ('minion', 'sub_minion'):
  92. ret = self.run_function(
  93. 'mine.get',
  94. [minion_id, 'grains.items'],
  95. minion_tgt=minion_id
  96. )
  97. self.assertEqual(ret[minion_id]['id'], minion_id)
  98. time.sleep(1)
  99. self.assertTrue(
  100. self.run_function(
  101. 'mine.flush',
  102. minion_tgt='minion'
  103. )
  104. )
  105. time.sleep(1)
  106. ret_flushed = self.run_function(
  107. 'mine.get',
  108. ['*', 'grains.items']
  109. )
  110. self.assertEqual(ret_flushed.get('minion', None), None)
  111. self.assertEqual(ret_flushed['sub_minion']['id'], 'sub_minion')
  112. def test_mine_delete(self):
  113. '''
  114. Test mine.delete
  115. '''
  116. self.assertTrue(
  117. self.run_function(
  118. 'mine.send',
  119. ['grains.items'],
  120. minion_tgt='minion'
  121. )
  122. )
  123. self.wait_for_all_jobs(minions=('minion',))
  124. attempts = 10
  125. ret_grains = None
  126. while True:
  127. if ret_grains:
  128. break
  129. # Smoke testing that grains should now exist in the mine
  130. ret_grains = self.run_function(
  131. 'mine.get',
  132. ['minion', 'grains.items'],
  133. minion_tgt='minion'
  134. )
  135. if ret_grains and 'minion' in ret_grains:
  136. break
  137. if attempts:
  138. attempts -= 1
  139. if attempts:
  140. time.sleep(1.5)
  141. continue
  142. self.fail(
  143. '\'minion\' was not found as a key of the \'mine.get\' \'grains.items\' call. Full return: {}'.format(
  144. pprint.pformat(ret_grains)
  145. )
  146. )
  147. self.assertEqual(
  148. ret_grains['minion']['id'], 'minion',
  149. msg='{} != minion, full return payload: {}'.format(
  150. ret_grains['minion']['id'],
  151. pprint.pformat(ret_grains)
  152. )
  153. )
  154. self.assertTrue(
  155. self.run_function(
  156. 'mine.send',
  157. ['test.arg', 'foo=bar', 'fnord=roscivs'],
  158. minion_tgt='minion'
  159. )
  160. )
  161. self.wait_for_all_jobs(minions=('minion',))
  162. ret_args = self.run_function(
  163. 'mine.get',
  164. ['minion', 'test.arg']
  165. )
  166. expected = {
  167. 'minion': {
  168. 'args': [],
  169. 'kwargs': {
  170. 'fnord': 'roscivs',
  171. 'foo': 'bar',
  172. },
  173. },
  174. }
  175. # Smoke testing that test.arg exists in the mine
  176. self.assertDictEqual(ret_args, expected)
  177. self.assertTrue(
  178. self.run_function(
  179. 'mine.send',
  180. ['test.echo', 'foo'],
  181. minion_tgt='minion'
  182. )
  183. )
  184. self.wait_for_all_jobs(minions=('minion',))
  185. ret_echo = self.run_function(
  186. 'mine.get',
  187. ['minion', 'test.echo'],
  188. minion_tgt='minion'
  189. )
  190. # Smoke testing that we were also able to set test.echo in the mine
  191. self.assertEqual(ret_echo['minion'], 'foo')
  192. self.assertTrue(
  193. self.run_function(
  194. 'mine.delete',
  195. ['test.arg'],
  196. minion_tgt='minion'
  197. )
  198. )
  199. self.wait_for_all_jobs(minions=('minion',))
  200. ret_arg_deleted = self.run_function(
  201. 'mine.get',
  202. ['minion', 'test.arg'],
  203. minion_tgt='minion'
  204. )
  205. # Now comes the real test - did we obliterate test.arg from the mine?
  206. # We could assert this a different way, but there shouldn't be any
  207. # other tests that are setting this mine value, so this should
  208. # definitely avoid any race conditions.
  209. self.assertFalse(
  210. ret_arg_deleted.get('minion', {})
  211. .get('kwargs', {})
  212. .get('fnord', None) == 'roscivs',
  213. '{} contained "fnord":"roscivs", which should be gone'.format(
  214. ret_arg_deleted,
  215. )
  216. )
  217. ret_echo_stays = self.run_function(
  218. 'mine.get',
  219. ['minion', 'test.echo'],
  220. minion_tgt='minion'
  221. )
  222. # Of course, one more health check - we want targeted removal.
  223. # This isn't horseshoes or hand grenades - test.arg should go away
  224. # but test.echo should still be available.
  225. self.assertEqual(ret_echo_stays['minion'], 'foo')