test_standard.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import os
  5. # Import Salt Testing libs
  6. from tests.support.case import ModuleCase
  7. # Import salt libs
  8. import salt.utils.files
  9. import salt.utils.platform
  10. class StdTest(ModuleCase):
  11. '''
  12. Test standard client calls
  13. '''
  14. def setUp(self):
  15. self.TIMEOUT = 600 if salt.utils.platform.is_windows() else 10
  16. def test_cli(self):
  17. '''
  18. Test cli function
  19. '''
  20. cmd_iter = self.client.cmd_cli(
  21. 'minion',
  22. 'test.ping',
  23. timeout=20,
  24. )
  25. for ret in cmd_iter:
  26. self.assertTrue(ret['minion'])
  27. # make sure that the iter waits for long running jobs too
  28. cmd_iter = self.client.cmd_cli(
  29. 'minion',
  30. 'test.sleep',
  31. [6],
  32. timeout=20,
  33. )
  34. num_ret = 0
  35. for ret in cmd_iter:
  36. num_ret += 1
  37. self.assertTrue(ret['minion'])
  38. assert num_ret > 0
  39. # ping a minion that doesn't exist, to make sure that it doesn't hang forever
  40. # create fake minion
  41. key_file = os.path.join(self.master_opts['pki_dir'], 'minions', 'footest')
  42. # touch the file
  43. with salt.utils.files.fopen(key_file, 'a'):
  44. pass
  45. # ping that minion and ensure it times out
  46. try:
  47. cmd_iter = self.client.cmd_cli(
  48. 'footest',
  49. 'test.ping',
  50. timeout=20,
  51. )
  52. num_ret = 0
  53. for ret in cmd_iter:
  54. num_ret += 1
  55. self.assertTrue(ret['minion'])
  56. assert num_ret == 0
  57. finally:
  58. os.unlink(key_file)
  59. def test_iter(self):
  60. '''
  61. test cmd_iter
  62. '''
  63. cmd_iter = self.client.cmd_iter(
  64. 'minion',
  65. 'test.ping',
  66. )
  67. for ret in cmd_iter:
  68. self.assertTrue(ret['minion'])
  69. def test_iter_no_block(self):
  70. '''
  71. test cmd_iter_no_block
  72. '''
  73. cmd_iter = self.client.cmd_iter_no_block(
  74. 'minion',
  75. 'test.ping',
  76. )
  77. for ret in cmd_iter:
  78. if ret is None:
  79. continue
  80. self.assertTrue(ret['minion'])
  81. def test_batch(self):
  82. '''
  83. test cmd_batch
  84. '''
  85. cmd_batch = self.client.cmd_batch(
  86. 'minion',
  87. 'test.ping',
  88. )
  89. for ret in cmd_batch:
  90. self.assertTrue(ret['minion'])
  91. def test_batch_raw(self):
  92. '''
  93. test cmd_batch with raw option
  94. '''
  95. cmd_batch = self.client.cmd_batch(
  96. 'minion',
  97. 'test.ping',
  98. raw=True,
  99. )
  100. for ret in cmd_batch:
  101. self.assertTrue(ret['data']['success'])
  102. def test_full_returns(self):
  103. '''
  104. test cmd_iter
  105. '''
  106. ret = self.client.cmd_full_return(
  107. 'minion',
  108. 'test.ping',
  109. timeout=20,
  110. )
  111. self.assertIn('minion', ret)
  112. self.assertEqual({'ret': True, 'success': True}, ret['minion'])
  113. def test_disconnected_return(self):
  114. '''
  115. Test return/messaging on a disconnected minion
  116. '''
  117. test_ret = {'ret': 'Minion did not return. [No response]', 'out': 'no_return'}
  118. # Create a minion key, but do not start the "fake" minion. This mimics
  119. # a disconnected minion.
  120. key_file = os.path.join(self.master_opts['pki_dir'], 'minions', 'disconnected')
  121. with salt.utils.files.fopen(key_file, 'a'):
  122. pass
  123. # ping disconnected minion and ensure it times out and returns with correct message
  124. try:
  125. cmd_iter = self.client.cmd_cli(
  126. 'disconnected',
  127. 'test.ping',
  128. show_timeout=True
  129. )
  130. num_ret = 0
  131. for ret in cmd_iter:
  132. num_ret += 1
  133. self.assertEqual(ret['disconnected']['ret'], test_ret['ret'])
  134. self.assertEqual(ret['disconnected']['out'], test_ret['out'])
  135. # Ensure that we entered the loop above
  136. self.assertEqual(num_ret, 1)
  137. finally:
  138. os.unlink(key_file)
  139. def test_missing_minion_list(self):
  140. '''
  141. test cmd with missing minion in nodegroup
  142. '''
  143. ret = self.client.cmd(
  144. 'minion,ghostminion',
  145. 'test.ping',
  146. tgt_type='list',
  147. timeout=self.TIMEOUT
  148. )
  149. self.assertIn('minion', ret)
  150. self.assertIn('ghostminion', ret)
  151. self.assertEqual(True, ret['minion'])
  152. self.assertEqual(u'Minion did not return. [No response]',
  153. ret['ghostminion'])
  154. def test_missing_minion_nodegroup(self):
  155. '''
  156. test cmd with missing minion in nodegroup
  157. '''
  158. ret = self.client.cmd(
  159. 'missing_minion',
  160. 'test.ping',
  161. tgt_type='nodegroup'
  162. )
  163. self.assertIn('minion', ret)
  164. self.assertIn('ghostminion', ret)
  165. self.assertEqual(True, ret['minion'])
  166. self.assertEqual(u'Minion did not return. [No response]',
  167. ret['ghostminion'])