test_cmd.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Tests for the file state
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import errno
  8. import os
  9. import textwrap
  10. import tempfile
  11. import time
  12. import sys
  13. # Import Salt Testing libs
  14. from tests.support.runtests import RUNTIME_VARS
  15. from tests.support.case import ModuleCase
  16. from tests.support.mixins import SaltReturnAssertsMixin
  17. # Import Salt libs
  18. import salt.utils.files
  19. import salt.utils.platform
  20. # Import 3rd-party libs
  21. from salt.ext import six
  22. class CMDTest(ModuleCase, SaltReturnAssertsMixin):
  23. '''
  24. Validate the cmd state
  25. '''
  26. @classmethod
  27. def setUpClass(cls):
  28. cls.__cmd = 'dir' if salt.utils.platform.is_windows() else 'ls'
  29. def test_run_simple(self):
  30. '''
  31. cmd.run
  32. '''
  33. ret = self.run_state('cmd.run', name=self.__cmd, cwd=tempfile.gettempdir())
  34. self.assertSaltTrueReturn(ret)
  35. def test_run_output_loglevel(self):
  36. '''
  37. cmd.run with output_loglevel=quiet
  38. '''
  39. ret = self.run_state('cmd.run', name=self.__cmd,
  40. cwd=tempfile.gettempdir(),
  41. output_loglevel='quiet')
  42. self.assertSaltTrueReturn(ret)
  43. def test_test_run_simple(self):
  44. '''
  45. cmd.run test interface
  46. '''
  47. ret = self.run_state('cmd.run', name=self.__cmd,
  48. cwd=tempfile.gettempdir(), test=True)
  49. self.assertSaltNoneReturn(ret)
  50. def test_run_hide_output(self):
  51. '''
  52. cmd.run with output hidden
  53. '''
  54. ret = self.run_state(
  55. u'cmd.run',
  56. name=self.__cmd,
  57. hide_output=True)
  58. self.assertSaltTrueReturn(ret)
  59. ret = ret[next(iter(ret))]
  60. self.assertEqual(ret['changes']['stdout'], '')
  61. self.assertEqual(ret['changes']['stderr'], '')
  62. class CMDRunRedirectTest(ModuleCase, SaltReturnAssertsMixin):
  63. '''
  64. Validate the cmd state of run_redirect
  65. '''
  66. def setUp(self):
  67. self.state_name = 'run_redirect'
  68. state_filename = self.state_name + '.sls'
  69. self.state_file = os.path.join(RUNTIME_VARS.TMP_STATE_TREE, state_filename)
  70. # Create the testfile and release the handle
  71. fd, self.test_file = tempfile.mkstemp()
  72. try:
  73. os.close(fd)
  74. except OSError as exc:
  75. if exc.errno != errno.EBADF:
  76. six.reraise(*sys.exc_info())
  77. # Create the testfile and release the handle
  78. fd, self.test_tmp_path = tempfile.mkstemp()
  79. try:
  80. os.close(fd)
  81. except OSError as exc:
  82. if exc.errno != errno.EBADF:
  83. six.reraise(*sys.exc_info())
  84. super(CMDRunRedirectTest, self).setUp()
  85. def tearDown(self):
  86. time.sleep(1)
  87. for path in (self.state_file, self.test_tmp_path, self.test_file):
  88. try:
  89. os.remove(path)
  90. except OSError:
  91. # Not all of the tests leave files around that we want to remove
  92. # As some of the tests create the sls files in the test itself,
  93. # And some are using files in the integration test file state tree.
  94. pass
  95. super(CMDRunRedirectTest, self).tearDown()
  96. def test_run_unless(self):
  97. '''
  98. test cmd.run unless
  99. '''
  100. state_key = 'cmd_|-{0}_|-{0}_|-run'.format(self.test_tmp_path)
  101. with salt.utils.files.fopen(self.state_file, 'w') as fb_:
  102. fb_.write(salt.utils.stringutils.to_str(textwrap.dedent('''
  103. {0}:
  104. cmd.run:
  105. - unless: echo cheese > {1}
  106. '''.format(self.test_tmp_path, self.test_file))))
  107. ret = self.run_function('state.sls', [self.state_name])
  108. self.assertTrue(ret[state_key]['result'])
  109. def test_run_unless_multiple_cmds(self):
  110. '''
  111. test cmd.run using multiple unless options where the first cmd in the
  112. list will pass, but the second will fail. This tests the fix for issue
  113. #35384. (The fix is in PR #35545.)
  114. '''
  115. sls = self.run_function('state.sls', mods='issue-35384')
  116. self.assertSaltTrueReturn(sls)
  117. # We must assert against the comment here to make sure the comment reads that the
  118. # command "echo "hello"" was run. This ensures that we made it to the last unless
  119. # command in the state. If the comment reads "unless condition is true", or similar,
  120. # then the unless state run bailed out after the first unless command succeeded,
  121. # which is the bug we're regression testing for.
  122. self.assertEqual(sls['cmd_|-cmd_run_unless_multiple_|-echo "hello"_|-run']['comment'],
  123. 'Command "echo "hello"" run')
  124. def test_run_creates_exists(self):
  125. '''
  126. test cmd.run creates already there
  127. '''
  128. state_key = 'cmd_|-echo >> {0}_|-echo >> {0}_|-run'.format(self.test_file)
  129. with salt.utils.files.fopen(self.state_file, 'w') as fb_:
  130. fb_.write(salt.utils.stringutils.to_str(textwrap.dedent('''
  131. echo >> {0}:
  132. cmd.run:
  133. - creates: {0}
  134. '''.format(self.test_file))))
  135. ret = self.run_function('state.sls', [self.state_name])
  136. self.assertTrue(ret[state_key]['result'])
  137. self.assertEqual(len(ret[state_key]['changes']), 0)
  138. def test_run_creates_new(self):
  139. '''
  140. test cmd.run creates not there
  141. '''
  142. os.remove(self.test_file)
  143. state_key = 'cmd_|-echo >> {0}_|-echo >> {0}_|-run'.format(self.test_file)
  144. with salt.utils.files.fopen(self.state_file, 'w') as fb_:
  145. fb_.write(salt.utils.stringutils.to_str(textwrap.dedent('''
  146. echo >> {0}:
  147. cmd.run:
  148. - creates: {0}
  149. '''.format(self.test_file))))
  150. ret = self.run_function('state.sls', [self.state_name])
  151. self.assertTrue(ret[state_key]['result'])
  152. self.assertEqual(len(ret[state_key]['changes']), 4)
  153. def test_run_redirect(self):
  154. '''
  155. test cmd.run with shell redirect
  156. '''
  157. state_key = 'cmd_|-echo test > {0}_|-echo test > {0}_|-run'.format(self.test_file)
  158. with salt.utils.files.fopen(self.state_file, 'w') as fb_:
  159. fb_.write(salt.utils.stringutils.to_str(textwrap.dedent('''
  160. echo test > {0}:
  161. cmd.run
  162. '''.format(self.test_file))))
  163. ret = self.run_function('state.sls', [self.state_name])
  164. self.assertTrue(ret[state_key]['result'])
  165. class CMDRunWatchTest(ModuleCase, SaltReturnAssertsMixin):
  166. '''
  167. Validate the cmd state of run_watch
  168. '''
  169. def setUp(self):
  170. self.state_name = 'run_watch'
  171. state_filename = self.state_name + '.sls'
  172. self.state_file = os.path.join(RUNTIME_VARS.TMP_STATE_TREE, state_filename)
  173. super(CMDRunWatchTest, self).setUp()
  174. def tearDown(self):
  175. os.remove(self.state_file)
  176. super(CMDRunWatchTest, self).tearDown()
  177. def test_run_watch(self):
  178. '''
  179. test cmd.run watch
  180. '''
  181. saltines_key = 'cmd_|-saltines_|-echo changed=true_|-run'
  182. biscuits_key = 'cmd_|-biscuits_|-echo biscuits_|-wait'
  183. with salt.utils.files.fopen(self.state_file, 'w') as fb_:
  184. fb_.write(salt.utils.stringutils.to_str(textwrap.dedent('''
  185. saltines:
  186. cmd.run:
  187. - name: echo changed=true
  188. - cwd: /
  189. - stateful: True
  190. biscuits:
  191. cmd.wait:
  192. - name: echo biscuits
  193. - cwd: /
  194. - watch:
  195. - cmd: saltines
  196. ''')))
  197. ret = self.run_function('state.sls', [self.state_name])
  198. self.assertTrue(ret[saltines_key]['result'])
  199. self.assertTrue(ret[biscuits_key]['result'])