test_cmd.py 8.8 KB

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