test_standard.py 5.1 KB

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