1
0

test_cmd.py 7.5 KB

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