test_cp.py 22 KB

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