test_cp.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, print_function, unicode_literals
  3. import hashlib
  4. import logging
  5. import os
  6. import shutil
  7. import signal
  8. import tempfile
  9. import textwrap
  10. import time
  11. import uuid
  12. import psutil
  13. import pytest
  14. import salt.ext.six as six
  15. import salt.utils.files
  16. import salt.utils.path
  17. import salt.utils.platform
  18. import salt.utils.stringutils
  19. from saltfactories.utils.ports import get_unused_localhost_port
  20. from tests.support.case import ModuleCase
  21. from tests.support.helpers import with_tempfile
  22. from tests.support.runtests import RUNTIME_VARS
  23. from tests.support.unit import skipIf
  24. log = logging.getLogger(__name__)
  25. @pytest.mark.windows_whitelisted
  26. class CPModuleTest(ModuleCase):
  27. """
  28. Validate the cp module
  29. """
  30. def run_function(self, *args, **kwargs): # pylint: disable=arguments-differ
  31. """
  32. Ensure that results are decoded
  33. TODO: maybe move this behavior to ModuleCase itself?
  34. """
  35. return salt.utils.data.decode(
  36. super(CPModuleTest, self).run_function(*args, **kwargs)
  37. )
  38. @with_tempfile()
  39. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  40. def test_get_file(self, tgt):
  41. """
  42. cp.get_file
  43. """
  44. self.run_function("cp.get_file", ["salt://grail/scene33", tgt])
  45. with salt.utils.files.fopen(tgt, "r") as scene:
  46. data = salt.utils.stringutils.to_unicode(scene.read())
  47. self.assertIn("KNIGHT: They're nervous, sire.", data)
  48. self.assertNotIn("bacon", data)
  49. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  50. def test_get_file_to_dir(self):
  51. """
  52. cp.get_file
  53. """
  54. tgt = os.path.join(RUNTIME_VARS.TMP, "")
  55. self.run_function("cp.get_file", ["salt://grail/scene33", tgt])
  56. with salt.utils.files.fopen(tgt + "scene33", "r") as scene:
  57. data = salt.utils.stringutils.to_unicode(scene.read())
  58. self.assertIn("KNIGHT: They're nervous, sire.", data)
  59. self.assertNotIn("bacon", data)
  60. @with_tempfile()
  61. @skipIf(
  62. salt.utils.platform.is_windows() and six.PY3,
  63. "This test hangs on Windows on Py3",
  64. )
  65. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  66. def test_get_file_templated_paths(self, tgt):
  67. """
  68. cp.get_file
  69. """
  70. self.run_function(
  71. "cp.get_file",
  72. [
  73. "salt://{{grains.test_grain}}",
  74. tgt.replace("cheese", "{{grains.test_grain}}"),
  75. ],
  76. template="jinja",
  77. )
  78. with salt.utils.files.fopen(tgt, "r") as cheese:
  79. data = salt.utils.stringutils.to_unicode(cheese.read())
  80. self.assertIn("Gromit", data)
  81. self.assertNotIn("bacon", data)
  82. @with_tempfile()
  83. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  84. def test_get_file_gzipped(self, tgt):
  85. """
  86. cp.get_file
  87. """
  88. src = os.path.join(RUNTIME_VARS.FILES, "file", "base", "file.big")
  89. with salt.utils.files.fopen(src, "rb") as fp_:
  90. hash_str = hashlib.md5(fp_.read()).hexdigest()
  91. self.run_function("cp.get_file", ["salt://file.big", tgt], gzip=5)
  92. with salt.utils.files.fopen(tgt, "rb") as scene:
  93. data = scene.read()
  94. self.assertEqual(hash_str, hashlib.md5(data).hexdigest())
  95. data = salt.utils.stringutils.to_unicode(data)
  96. self.assertIn("KNIGHT: They're nervous, sire.", data)
  97. self.assertNotIn("bacon", data)
  98. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  99. def test_get_file_makedirs(self):
  100. """
  101. cp.get_file
  102. """
  103. tgt = os.path.join(RUNTIME_VARS.TMP, "make", "dirs", "scene33")
  104. self.run_function("cp.get_file", ["salt://grail/scene33", tgt], makedirs=True)
  105. self.addCleanup(
  106. shutil.rmtree, os.path.join(RUNTIME_VARS.TMP, "make"), ignore_errors=True
  107. )
  108. with salt.utils.files.fopen(tgt, "r") as scene:
  109. data = salt.utils.stringutils.to_unicode(scene.read())
  110. self.assertIn("KNIGHT: They're nervous, sire.", data)
  111. self.assertNotIn("bacon", data)
  112. @with_tempfile()
  113. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  114. def test_get_template(self, tgt):
  115. """
  116. cp.get_template
  117. """
  118. self.run_function(
  119. "cp.get_template", ["salt://grail/scene33", tgt], spam="bacon"
  120. )
  121. with salt.utils.files.fopen(tgt, "r") as scene:
  122. data = salt.utils.stringutils.to_unicode(scene.read())
  123. self.assertIn("bacon", data)
  124. self.assertNotIn("spam", data)
  125. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  126. def test_get_dir(self):
  127. """
  128. cp.get_dir
  129. """
  130. tgt = os.path.join(RUNTIME_VARS.TMP, "many")
  131. self.run_function("cp.get_dir", ["salt://grail", tgt])
  132. self.assertIn("grail", os.listdir(tgt))
  133. self.assertIn("36", os.listdir(os.path.join(tgt, "grail")))
  134. self.assertIn("empty", os.listdir(os.path.join(tgt, "grail")))
  135. self.assertIn("scene", os.listdir(os.path.join(tgt, "grail", "36")))
  136. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  137. def test_get_dir_templated_paths(self):
  138. """
  139. cp.get_dir
  140. """
  141. tgt = os.path.join(RUNTIME_VARS.TMP, "many")
  142. self.run_function(
  143. "cp.get_dir",
  144. ["salt://{{grains.script}}", tgt.replace("many", "{{grains.alot}}")],
  145. )
  146. self.assertIn("grail", os.listdir(tgt))
  147. self.assertIn("36", os.listdir(os.path.join(tgt, "grail")))
  148. self.assertIn("empty", os.listdir(os.path.join(tgt, "grail")))
  149. self.assertIn("scene", os.listdir(os.path.join(tgt, "grail", "36")))
  150. # cp.get_url tests
  151. @with_tempfile()
  152. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  153. def test_get_url(self, tgt):
  154. """
  155. cp.get_url with salt:// source given
  156. """
  157. self.run_function("cp.get_url", ["salt://grail/scene33", tgt])
  158. with salt.utils.files.fopen(tgt, "r") as scene:
  159. data = salt.utils.stringutils.to_unicode(scene.read())
  160. self.assertIn("KNIGHT: They're nervous, sire.", data)
  161. self.assertNotIn("bacon", data)
  162. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  163. def test_get_url_makedirs(self):
  164. """
  165. cp.get_url
  166. """
  167. tgt = os.path.join(RUNTIME_VARS.TMP, "make", "dirs", "scene33")
  168. self.run_function("cp.get_url", ["salt://grail/scene33", tgt], makedirs=True)
  169. self.addCleanup(
  170. shutil.rmtree, os.path.join(RUNTIME_VARS.TMP, "make"), ignore_errors=True
  171. )
  172. with salt.utils.files.fopen(tgt, "r") as scene:
  173. data = salt.utils.stringutils.to_unicode(scene.read())
  174. self.assertIn("KNIGHT: They're nervous, sire.", data)
  175. self.assertNotIn("bacon", data)
  176. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  177. def test_get_url_dest_empty(self):
  178. """
  179. cp.get_url with salt:// source given and destination omitted.
  180. """
  181. ret = self.run_function("cp.get_url", ["salt://grail/scene33"])
  182. with salt.utils.files.fopen(ret, "r") as scene:
  183. data = salt.utils.stringutils.to_unicode(scene.read())
  184. self.assertIn("KNIGHT: They're nervous, sire.", data)
  185. self.assertNotIn("bacon", data)
  186. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  187. def test_get_url_no_dest(self):
  188. """
  189. cp.get_url with salt:// source given and destination set as None
  190. """
  191. tgt = None
  192. ret = self.run_function("cp.get_url", ["salt://grail/scene33", tgt])
  193. self.assertIn("KNIGHT: They're nervous, sire.", ret)
  194. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  195. def test_get_url_nonexistent_source(self):
  196. """
  197. cp.get_url with nonexistent salt:// source given
  198. """
  199. tgt = None
  200. ret = self.run_function("cp.get_url", ["salt://grail/nonexistent_scene", tgt])
  201. self.assertEqual(ret, False)
  202. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  203. def test_get_url_to_dir(self):
  204. """
  205. cp.get_url with salt:// source
  206. """
  207. tgt = os.path.join(RUNTIME_VARS.TMP, "")
  208. self.run_function("cp.get_url", ["salt://grail/scene33", tgt])
  209. with salt.utils.files.fopen(tgt + "scene33", "r") as scene:
  210. data = salt.utils.stringutils.to_unicode(scene.read())
  211. self.assertIn("KNIGHT: They're nervous, sire.", data)
  212. self.assertNotIn("bacon", data)
  213. @skipIf(
  214. salt.utils.platform.is_darwin() and six.PY2, "This test hangs on OS X on Py2"
  215. )
  216. @with_tempfile()
  217. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  218. def test_get_url_https(self, tgt):
  219. """
  220. cp.get_url with https:// source given
  221. """
  222. self.run_function("cp.get_url", ["https://repo.saltstack.com/index.html", tgt])
  223. with salt.utils.files.fopen(tgt, "r") as instructions:
  224. data = salt.utils.stringutils.to_unicode(instructions.read())
  225. self.assertIn("Bootstrap", data)
  226. self.assertIn("Debian", data)
  227. self.assertIn("Windows", data)
  228. self.assertNotIn("AYBABTU", data)
  229. @skipIf(
  230. salt.utils.platform.is_darwin() and six.PY2, "This test hangs on OS X on Py2"
  231. )
  232. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  233. def test_get_url_https_dest_empty(self):
  234. """
  235. cp.get_url with https:// source given and destination omitted.
  236. """
  237. ret = self.run_function("cp.get_url", ["https://repo.saltstack.com/index.html"])
  238. with salt.utils.files.fopen(ret, "r") as instructions:
  239. data = salt.utils.stringutils.to_unicode(instructions.read())
  240. self.assertIn("Bootstrap", data)
  241. self.assertIn("Debian", data)
  242. self.assertIn("Windows", data)
  243. self.assertNotIn("AYBABTU", data)
  244. @skipIf(
  245. salt.utils.platform.is_darwin() and six.PY2, "This test hangs on OS X on Py2"
  246. )
  247. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  248. def test_get_url_https_no_dest(self):
  249. """
  250. cp.get_url with https:// source given and destination set as None
  251. """
  252. timeout = 500
  253. start = time.time()
  254. sleep = 5
  255. tgt = None
  256. while time.time() - start <= timeout:
  257. ret = self.run_function(
  258. "cp.get_url", ["https://repo.saltstack.com/index.html", tgt]
  259. )
  260. if ret.find("HTTP 599") == -1:
  261. break
  262. time.sleep(sleep)
  263. if ret.find("HTTP 599") != -1:
  264. raise Exception("https://repo.saltstack.com/index.html returned 599 error")
  265. self.assertIn("Bootstrap", ret)
  266. self.assertIn("Debian", ret)
  267. self.assertIn("Windows", ret)
  268. self.assertNotIn("AYBABTU", ret)
  269. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  270. def test_get_url_file(self):
  271. """
  272. cp.get_url with file:// source given
  273. """
  274. tgt = ""
  275. src = os.path.join("file://", RUNTIME_VARS.FILES, "file", "base", "file.big")
  276. ret = self.run_function("cp.get_url", [src, tgt])
  277. with salt.utils.files.fopen(ret, "r") as scene:
  278. data = salt.utils.stringutils.to_unicode(scene.read())
  279. self.assertIn("KNIGHT: They're nervous, sire.", data)
  280. self.assertNotIn("bacon", data)
  281. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  282. def test_get_url_file_no_dest(self):
  283. """
  284. cp.get_url with file:// source given and destination set as None
  285. """
  286. tgt = None
  287. src = os.path.join("file://", RUNTIME_VARS.FILES, "file", "base", "file.big")
  288. ret = self.run_function("cp.get_url", [src, tgt])
  289. self.assertIn("KNIGHT: They're nervous, sire.", ret)
  290. self.assertNotIn("bacon", ret)
  291. @with_tempfile()
  292. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  293. def test_get_url_ftp(self, tgt):
  294. """
  295. cp.get_url with https:// source given
  296. """
  297. self.run_function(
  298. "cp.get_url",
  299. [
  300. "ftp://ftp.freebsd.org/pub/FreeBSD/releases/amd64/amd64/12.0-RELEASE/MANIFEST",
  301. tgt,
  302. ],
  303. )
  304. with salt.utils.files.fopen(tgt, "r") as instructions:
  305. data = salt.utils.stringutils.to_unicode(instructions.read())
  306. self.assertIn("Base system", data)
  307. # cp.get_file_str tests
  308. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  309. def test_get_file_str_salt(self):
  310. """
  311. cp.get_file_str with salt:// source given
  312. """
  313. src = "salt://grail/scene33"
  314. ret = self.run_function("cp.get_file_str", [src])
  315. self.assertIn("KNIGHT: They're nervous, sire.", ret)
  316. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  317. def test_get_file_str_nonexistent_source(self):
  318. """
  319. cp.get_file_str with nonexistent salt:// source given
  320. """
  321. src = "salt://grail/nonexistent_scene"
  322. ret = self.run_function("cp.get_file_str", [src])
  323. self.assertEqual(ret, False)
  324. @skipIf(
  325. salt.utils.platform.is_darwin() and six.PY2, "This test hangs on OS X on Py2"
  326. )
  327. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  328. def test_get_file_str_https(self):
  329. """
  330. cp.get_file_str with https:// source given
  331. """
  332. src = "https://repo.saltstack.com/index.html"
  333. ret = self.run_function("cp.get_file_str", [src])
  334. self.assertIn("Bootstrap", ret)
  335. self.assertIn("Debian", ret)
  336. self.assertIn("Windows", ret)
  337. self.assertNotIn("AYBABTU", ret)
  338. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  339. def test_get_file_str_local(self):
  340. """
  341. cp.get_file_str with file:// source given
  342. """
  343. src = os.path.join("file://", RUNTIME_VARS.FILES, "file", "base", "file.big")
  344. ret = self.run_function("cp.get_file_str", [src])
  345. self.assertIn("KNIGHT: They're nervous, sire.", ret)
  346. self.assertNotIn("bacon", ret)
  347. # caching tests
  348. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  349. def test_cache_file(self):
  350. """
  351. cp.cache_file
  352. """
  353. ret = self.run_function("cp.cache_file", ["salt://grail/scene33"])
  354. with salt.utils.files.fopen(ret, "r") as scene:
  355. data = salt.utils.stringutils.to_unicode(scene.read())
  356. self.assertIn("KNIGHT: They're nervous, sire.", data)
  357. self.assertNotIn("bacon", data)
  358. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  359. def test_cache_files(self):
  360. """
  361. cp.cache_files
  362. """
  363. ret = self.run_function(
  364. "cp.cache_files", [["salt://grail/scene33", "salt://grail/36/scene"]]
  365. )
  366. for path in ret:
  367. with salt.utils.files.fopen(path, "r") as scene:
  368. data = salt.utils.stringutils.to_unicode(scene.read())
  369. self.assertIn("ARTHUR:", data)
  370. self.assertNotIn("bacon", data)
  371. @with_tempfile()
  372. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  373. def test_cache_master(self, tgt):
  374. """
  375. cp.cache_master
  376. """
  377. ret = self.run_function("cp.cache_master", [tgt],)
  378. for path in ret:
  379. self.assertTrue(os.path.exists(path))
  380. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  381. def test_cache_local_file(self):
  382. """
  383. cp.cache_local_file
  384. """
  385. src = os.path.join(RUNTIME_VARS.TMP, "random")
  386. with salt.utils.files.fopen(src, "w+") as fn_:
  387. fn_.write(salt.utils.stringutils.to_str("foo"))
  388. ret = self.run_function("cp.cache_local_file", [src])
  389. with salt.utils.files.fopen(ret, "r") as cp_:
  390. self.assertEqual(salt.utils.stringutils.to_unicode(cp_.read()), "foo")
  391. @skipIf(not salt.utils.path.which("nginx"), "nginx not installed")
  392. @pytest.mark.skip_if_not_root
  393. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  394. def test_cache_remote_file(self):
  395. """
  396. cp.cache_file
  397. """
  398. nginx_port = get_unused_localhost_port()
  399. url_prefix = "http://localhost:{0}/".format(nginx_port)
  400. temp_dir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
  401. self.addCleanup(shutil.rmtree, temp_dir, ignore_errors=True)
  402. nginx_root_dir = os.path.join(temp_dir, "root")
  403. nginx_conf_dir = os.path.join(temp_dir, "conf")
  404. nginx_conf = os.path.join(nginx_conf_dir, "nginx.conf")
  405. nginx_pidfile = os.path.join(nginx_conf_dir, "nginx.pid")
  406. file_contents = "Hello world!"
  407. for dirname in (nginx_root_dir, nginx_conf_dir):
  408. os.makedirs(dirname)
  409. # Write the temp file
  410. with salt.utils.files.fopen(
  411. os.path.join(nginx_root_dir, "actual_file"), "w"
  412. ) as fp_:
  413. fp_.write(salt.utils.stringutils.to_str(file_contents))
  414. # Write the nginx config
  415. with salt.utils.files.fopen(nginx_conf, "w") as fp_:
  416. fp_.write(
  417. textwrap.dedent(
  418. salt.utils.stringutils.to_str(
  419. """\
  420. user root;
  421. worker_processes 1;
  422. error_log {nginx_conf_dir}/server_error.log;
  423. pid {nginx_pidfile};
  424. events {{
  425. worker_connections 1024;
  426. }}
  427. http {{
  428. include /etc/nginx/mime.types;
  429. default_type application/octet-stream;
  430. access_log {nginx_conf_dir}/access.log;
  431. error_log {nginx_conf_dir}/error.log;
  432. server {{
  433. listen {nginx_port} default_server;
  434. server_name cachefile.local;
  435. root {nginx_root_dir};
  436. location ~ ^/301$ {{
  437. return 301 /actual_file;
  438. }}
  439. location ~ ^/302$ {{
  440. return 302 /actual_file;
  441. }}
  442. }}
  443. }}""".format(
  444. **locals()
  445. )
  446. )
  447. )
  448. )
  449. self.run_function("cmd.run", [["nginx", "-c", nginx_conf]], python_shell=False)
  450. with salt.utils.files.fopen(nginx_pidfile) as fp_:
  451. nginx_pid = int(fp_.read().strip())
  452. nginx_proc = psutil.Process(pid=nginx_pid)
  453. self.addCleanup(nginx_proc.send_signal, signal.SIGQUIT)
  454. for code in ("", "301", "302"):
  455. url = url_prefix + (code or "actual_file")
  456. log.debug("attempting to cache %s", url)
  457. ret = self.run_function("cp.cache_file", [url])
  458. self.assertTrue(ret)
  459. with salt.utils.files.fopen(ret) as fp_:
  460. cached_contents = salt.utils.stringutils.to_unicode(fp_.read())
  461. self.assertEqual(cached_contents, file_contents)
  462. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  463. def test_list_states(self):
  464. """
  465. cp.list_states
  466. """
  467. ret = self.run_function("cp.list_states",)
  468. self.assertIn("core", ret)
  469. self.assertIn("top", ret)
  470. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  471. def test_list_minion(self):
  472. """
  473. cp.list_minion
  474. """
  475. self.run_function("cp.cache_file", ["salt://grail/scene33"])
  476. ret = self.run_function("cp.list_minion")
  477. found = False
  478. search = "grail/scene33"
  479. if salt.utils.platform.is_windows():
  480. search = r"grail\scene33"
  481. for path in ret:
  482. if search in path:
  483. found = True
  484. break
  485. self.assertTrue(found)
  486. @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
  487. def test_is_cached(self):
  488. """
  489. cp.is_cached
  490. """
  491. self.run_function("cp.cache_file", ["salt://grail/scene33"])
  492. ret1 = self.run_function("cp.is_cached", ["salt://grail/scene33"])
  493. self.assertTrue(ret1)
  494. ret2 = self.run_function("cp.is_cached", ["salt://fasldkgj/poicxzbn"])
  495. self.assertFalse(ret2)
  496. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  497. def test_hash_file(self):
  498. """
  499. cp.hash_file
  500. """
  501. sha256_hash = self.run_function("cp.hash_file", ["salt://grail/scene33"])
  502. path = self.run_function("cp.cache_file", ["salt://grail/scene33"])
  503. with salt.utils.files.fopen(path, "rb") as fn_:
  504. data = fn_.read()
  505. self.assertEqual(sha256_hash["hsum"], hashlib.sha256(data).hexdigest())
  506. @with_tempfile()
  507. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  508. def test_get_file_from_env_predefined(self, tgt):
  509. """
  510. cp.get_file
  511. """
  512. tgt = os.path.join(RUNTIME_VARS.TMP, "cheese")
  513. try:
  514. self.run_function("cp.get_file", ["salt://cheese", tgt])
  515. with salt.utils.files.fopen(tgt, "r") as cheese:
  516. data = salt.utils.stringutils.to_unicode(cheese.read())
  517. self.assertIn("Gromit", data)
  518. self.assertNotIn("Comte", data)
  519. finally:
  520. os.unlink(tgt)
  521. @with_tempfile()
  522. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  523. def test_get_file_from_env_in_url(self, tgt):
  524. tgt = os.path.join(RUNTIME_VARS.TMP, "cheese")
  525. try:
  526. self.run_function("cp.get_file", ["salt://cheese?saltenv=prod", tgt])
  527. with salt.utils.files.fopen(tgt, "r") as cheese:
  528. data = salt.utils.stringutils.to_unicode(cheese.read())
  529. self.assertIn("Gromit", data)
  530. self.assertIn("Comte", data)
  531. finally:
  532. os.unlink(tgt)
  533. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  534. def test_push(self):
  535. log_to_xfer = os.path.join(RUNTIME_VARS.TMP, uuid.uuid4().hex)
  536. open(log_to_xfer, "w").close() # pylint: disable=resource-leakage
  537. try:
  538. self.run_function("cp.push", [log_to_xfer])
  539. tgt_cache_file = os.path.join(
  540. RUNTIME_VARS.TMP,
  541. "master-minion-root",
  542. "cache",
  543. "minions",
  544. "minion",
  545. "files",
  546. RUNTIME_VARS.TMP,
  547. log_to_xfer,
  548. )
  549. self.assertTrue(
  550. os.path.isfile(tgt_cache_file), "File was not cached on the master"
  551. )
  552. finally:
  553. os.unlink(tgt_cache_file)
  554. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  555. def test_envs(self):
  556. self.assertEqual(sorted(self.run_function("cp.envs")), sorted(["base", "prod"]))