test_cmd.py 8.6 KB

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