1
0

test_state.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import glob
  2. import logging
  3. import os
  4. import shutil
  5. import threading
  6. import time
  7. from tests.support.case import SSHCase
  8. from tests.support.helpers import flaky, slowTest
  9. from tests.support.runtests import RUNTIME_VARS
  10. SSH_SLS = "ssh_state_tests"
  11. SSH_SLS_FILE = "/tmp/salt_test_file"
  12. log = logging.getLogger(__name__)
  13. class SSHStateTest(SSHCase):
  14. """
  15. testing the state system with salt-ssh
  16. """
  17. def _check_dict_ret(self, ret, val, exp_ret, equal=True):
  18. self.assertIsInstance(ret, dict)
  19. for key, value in ret.items():
  20. self.assertIsInstance(value, dict)
  21. if equal:
  22. self.assertEqual(value[val], exp_ret)
  23. else:
  24. self.assertNotEqual(value[val], exp_ret)
  25. def _check_request(self, empty=False):
  26. check = self.run_function("state.check_request", wipe=False)
  27. if empty:
  28. self.assertFalse(bool(check), "bool({}) is not False".format(check))
  29. else:
  30. self._check_dict_ret(
  31. ret=check["default"]["test_run"]["local"]["return"],
  32. val="__sls__",
  33. exp_ret=SSH_SLS,
  34. )
  35. @slowTest
  36. def test_state_apply(self):
  37. """
  38. test state.apply with salt-ssh
  39. """
  40. ret = self.run_function("state.apply", [SSH_SLS])
  41. self._check_dict_ret(ret=ret, val="__sls__", exp_ret=SSH_SLS)
  42. check_file = self.run_function("file.file_exists", [SSH_SLS_FILE])
  43. self.assertTrue(check_file)
  44. @slowTest
  45. def test_state_sls_id(self):
  46. """
  47. test state.sls_id with salt-ssh
  48. """
  49. # check state.sls_id with test=True
  50. ret = self.run_function("state.sls_id", ["ssh-file-test", SSH_SLS, "test=True"])
  51. self._check_dict_ret(
  52. ret=ret,
  53. val="comment",
  54. exp_ret=(
  55. "The file {} is set to be changed\n"
  56. "Note: No changes made, actual changes may\n"
  57. "be different due to other states."
  58. ).format(SSH_SLS_FILE),
  59. )
  60. # check state.sls_id without test=True
  61. ret = self.run_function("state.sls_id", ["ssh-file-test", SSH_SLS])
  62. self._check_dict_ret(ret=ret, val="__sls__", exp_ret=SSH_SLS)
  63. # make sure the other id in the state was not run
  64. self._check_dict_ret(ret=ret, val="__id__", exp_ret="second_id", equal=False)
  65. check_file = self.run_function("file.file_exists", [SSH_SLS_FILE])
  66. self.assertTrue(check_file)
  67. @slowTest
  68. def test_state_sls_wrong_id(self):
  69. """
  70. test state.sls_id when id does not exist
  71. """
  72. # check state.sls_id with test=True
  73. ret = self.run_function("state.sls_id", ["doesnotexist", SSH_SLS])
  74. assert "No matches for ID" in ret
  75. @slowTest
  76. def test_state_sls_id_with_pillar(self):
  77. """
  78. test state.sls_id with pillar data
  79. """
  80. self.run_function(
  81. "state.sls_id",
  82. ["ssh-file-test", SSH_SLS, 'pillar=\'{"test_file_suffix": "_pillar"}\''],
  83. )
  84. check_file = self.run_function(
  85. "file.file_exists", ["/tmp/salt_test_file_pillar"]
  86. )
  87. self.assertTrue(check_file)
  88. @slowTest
  89. def test_state_show_sls(self):
  90. """
  91. test state.show_sls with salt-ssh
  92. """
  93. ret = self.run_function("state.show_sls", [SSH_SLS])
  94. self._check_dict_ret(ret=ret, val="__sls__", exp_ret=SSH_SLS)
  95. check_file = self.run_function("file.file_exists", [SSH_SLS_FILE], wipe=False)
  96. self.assertFalse(check_file)
  97. @slowTest
  98. def test_state_show_top(self):
  99. """
  100. test state.show_top with salt-ssh
  101. """
  102. ret = self.run_function("state.show_top")
  103. self.assertEqual(ret, {"base": ["core", "master_tops_test"]})
  104. @slowTest
  105. def test_state_single(self):
  106. """
  107. state.single with salt-ssh
  108. """
  109. ret_out = {"name": "itworked", "result": True, "comment": "Success!"}
  110. single = self.run_function(
  111. "state.single", ["test.succeed_with_changes name=itworked"]
  112. )
  113. self.assertIsInstance(single, dict)
  114. for key, value in single.items():
  115. self.assertIsInstance(value, dict)
  116. self.assertEqual(value["name"], ret_out["name"])
  117. self.assertEqual(value["result"], ret_out["result"])
  118. self.assertEqual(value["comment"], ret_out["comment"])
  119. @slowTest
  120. def test_show_highstate(self):
  121. """
  122. state.show_highstate with salt-ssh
  123. """
  124. high = self.run_function("state.show_highstate")
  125. destpath = os.path.join(RUNTIME_VARS.TMP, "testfile")
  126. self.assertIsInstance(high, dict)
  127. self.assertIn(destpath, high)
  128. self.assertEqual(high[destpath]["__env__"], "base")
  129. @slowTest
  130. def test_state_high(self):
  131. """
  132. state.high with salt-ssh
  133. """
  134. ret_out = {"name": "itworked", "result": True, "comment": "Success!"}
  135. high = self.run_function(
  136. "state.high", ['"{"itworked": {"test": ["succeed_with_changes"]}}"']
  137. )
  138. self.assertIsInstance(high, dict)
  139. for key, value in high.items():
  140. self.assertIsInstance(value, dict)
  141. self.assertEqual(value["name"], ret_out["name"])
  142. self.assertEqual(value["result"], ret_out["result"])
  143. self.assertEqual(value["comment"], ret_out["comment"])
  144. @slowTest
  145. def test_show_lowstate(self):
  146. """
  147. state.show_lowstate with salt-ssh
  148. """
  149. low = self.run_function("state.show_lowstate")
  150. self.assertIsInstance(low, list)
  151. self.assertIsInstance(low[0], dict)
  152. @slowTest
  153. def test_state_low(self):
  154. """
  155. state.low with salt-ssh
  156. """
  157. ret_out = {"name": "itworked", "result": True, "comment": "Success!"}
  158. low = self.run_function(
  159. "state.low",
  160. ['"{"state": "test", "fun": "succeed_with_changes", "name": "itworked"}"'],
  161. )
  162. self.assertIsInstance(low, dict)
  163. for key, value in low.items():
  164. self.assertIsInstance(value, dict)
  165. self.assertEqual(value["name"], ret_out["name"])
  166. self.assertEqual(value["result"], ret_out["result"])
  167. self.assertEqual(value["comment"], ret_out["comment"])
  168. @slowTest
  169. def test_state_request_check_clear(self):
  170. """
  171. test state.request system with salt-ssh
  172. while also checking and clearing request
  173. """
  174. request = self.run_function("state.request", [SSH_SLS], wipe=False)
  175. self._check_dict_ret(ret=request, val="__sls__", exp_ret=SSH_SLS)
  176. self._check_request()
  177. clear = self.run_function("state.clear_request", wipe=False)
  178. self._check_request(empty=True)
  179. @slowTest
  180. def test_state_run_request(self):
  181. """
  182. test state.request system with salt-ssh
  183. while also running the request later
  184. """
  185. request = self.run_function("state.request", [SSH_SLS], wipe=False)
  186. self._check_dict_ret(ret=request, val="__sls__", exp_ret=SSH_SLS)
  187. run = self.run_function("state.run_request", wipe=False)
  188. check_file = self.run_function("file.file_exists", [SSH_SLS_FILE], wipe=False)
  189. self.assertTrue(check_file)
  190. @flaky
  191. @slowTest
  192. def test_state_running(self):
  193. """
  194. test state.running with salt-ssh
  195. """
  196. def _run_in_background():
  197. self.run_function("state.sls", ["running"], wipe=False)
  198. bg_thread = threading.Thread(target=_run_in_background)
  199. bg_thread.start()
  200. expected = 'The function "state.pkg" is running as'
  201. state_ret = []
  202. for _ in range(30):
  203. time.sleep(5)
  204. get_sls = self.run_function("state.running", wipe=False)
  205. state_ret.append(get_sls)
  206. if expected in " ".join(get_sls):
  207. # We found the expected return
  208. break
  209. else:
  210. self.fail(
  211. "Did not find '{}' in state.running return: {}".format(
  212. expected, state_ret
  213. )
  214. )
  215. # make sure we wait until the earlier state is complete
  216. future = time.time() + 120
  217. while True:
  218. if expected not in " ".join(self.run_function("state.running", wipe=False)):
  219. break
  220. if time.time() > future:
  221. self.fail(
  222. "state.pkg is still running overtime. Test did not clean up correctly."
  223. )
  224. def tearDown(self):
  225. """
  226. make sure to clean up any old ssh directories
  227. """
  228. salt_dir = self.run_function("config.get", ["thin_dir"], wipe=False)
  229. self.assertIsInstance(salt_dir, (str,))
  230. if os.path.exists(salt_dir):
  231. shutil.rmtree(salt_dir)
  232. for test_file_path in glob.glob(SSH_SLS_FILE + "*"):
  233. os.remove(test_file_path)