1
0

test_cmd.py 7.4 KB

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