1
0

test_cmd.py 8.4 KB

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