test_cp.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 hashlib
  7. import logging
  8. # Import Salt Testing libs
  9. from tests.support.runtime import RUNTIME_VARS
  10. # Import 3rd party libs
  11. import pytest
  12. import salt.ext.six as six
  13. # Import salt libs
  14. import salt.utils.files
  15. import salt.utils.path
  16. import salt.utils.platform
  17. import salt.utils.stringutils
  18. log = logging.getLogger(__name__)
  19. SSL3_SUPPORT = sys.version_info >= (2, 7, 9)
  20. def test_get_file(modules):
  21. '''
  22. cp.get_file
  23. '''
  24. with pytest.helpers.temp_file('scene33') as path:
  25. ret = modules.cp.get_file('salt://grail/scene33', path)
  26. assert ret == path
  27. assert os.path.isfile(path)
  28. with salt.utils.files.fopen(path, 'r') as scene:
  29. data = salt.utils.stringutils.to_unicode(scene.read())
  30. assert 'KNIGHT: They\'re nervous, sire.' in data
  31. assert 'bacon' not in data
  32. def test_get_file_to_dir(modules):
  33. '''
  34. cp.get_file
  35. '''
  36. with pytest.helpers.temp_directory() as tgt:
  37. path = os.path.join(tgt, 'scene33')
  38. ret = modules.cp.get_file('salt://grail/scene33', tgt)
  39. assert ret == path
  40. assert os.path.isfile(path)
  41. with salt.utils.files.fopen(path, 'r') as scene:
  42. data = salt.utils.stringutils.to_unicode(scene.read())
  43. assert 'KNIGHT: They\'re nervous, sire.' in data
  44. assert 'bacon' not in data
  45. def test_get_file_templated_paths(modules):
  46. '''
  47. cp.get_file
  48. '''
  49. with pytest.helpers.temp_file('cheese') as path:
  50. ret = modules.cp.get_file('salt://{{grains.test_grain}}',
  51. path.replace('cheese', '{{grains.test_grain}}'),
  52. template='jinja')
  53. assert ret == path
  54. assert os.path.isfile(path)
  55. with salt.utils.files.fopen(path, 'r') as scene:
  56. data = salt.utils.stringutils.to_unicode(scene.read())
  57. assert 'Gromit' in data
  58. assert 'bacon' not in data
  59. def test_get_file_gzipped(modules):
  60. '''
  61. cp.get_file
  62. '''
  63. src = os.path.join(RUNTIME_VARS.FILES, 'file', 'base', 'file.big')
  64. with salt.utils.files.fopen(src, 'rb') as fp_:
  65. hash_str = hashlib.md5(fp_.read()).hexdigest()
  66. with pytest.helpers.temp_file('scene33') as path:
  67. ret = modules.cp.get_file('salt://file.big', path, gzip=5)
  68. assert ret == path
  69. with salt.utils.files.fopen(path, 'rb') as scene:
  70. data = scene.read()
  71. assert hashlib.md5(data).hexdigest() == hash_str
  72. data = salt.utils.stringutils.to_unicode(data)
  73. assert 'KNIGHT: They\'re nervous, sire.' in data
  74. assert 'bacon' not in data
  75. def test_get_file_makedirs(modules):
  76. '''
  77. cp.get_file
  78. '''
  79. with pytest.helpers.temp_directory('make') as path:
  80. tgt = os.path.join(path, 'dirs', 'scene33')
  81. ret = modules.cp.get_file('salt://grail/scene33', tgt, makedirs=True)
  82. assert ret == tgt
  83. assert os.path.isfile(tgt)
  84. with salt.utils.files.fopen(tgt, 'r') as scene:
  85. data = salt.utils.stringutils.to_unicode(scene.read())
  86. assert 'KNIGHT: They\'re nervous, sire.' in data
  87. assert 'bacon' not in data
  88. def test_get_template(modules):
  89. '''
  90. cp.get_template
  91. '''
  92. with pytest.helpers.temp_file('scene33') as path:
  93. ret = modules.cp.get_template('salt://grail/scene33', path, spam='bacon')
  94. assert ret == path
  95. assert os.path.isfile(path)
  96. with salt.utils.files.fopen(path, 'r') as scene:
  97. data = salt.utils.stringutils.to_unicode(scene.read())
  98. assert 'bacon' in data
  99. assert 'spam' not in data
  100. def test_get_dir(modules):
  101. '''
  102. cp.get_dir
  103. '''
  104. with pytest.helpers.temp_directory('many') as path:
  105. ret = modules.cp.get_dir('salt://grail', path)
  106. assert ret != []
  107. assert 'grail' in os.listdir(path)
  108. assert '36' in os.listdir(os.path.join(path, 'grail'))
  109. assert 'empty' in os.listdir(os.path.join(path, 'grail'))
  110. assert 'scene' in os.listdir(os.path.join(path, 'grail', '36'))
  111. def test_get_dir_templated_paths(modules):
  112. '''
  113. cp.get_dir
  114. '''
  115. with pytest.helpers.temp_directory('many') as path:
  116. ret = modules.cp.get_dir('salt://{{grains.script}}',
  117. path.replace('many', '{{grains.alot}}'),
  118. template='jinja')
  119. assert ret != []
  120. assert 'grail' in os.listdir(path)
  121. assert '36' in os.listdir(os.path.join(path, 'grail'))
  122. assert 'empty' in os.listdir(os.path.join(path, 'grail'))
  123. assert 'scene' in os.listdir(os.path.join(path, 'grail', '36'))
  124. # cp.get_url tests
  125. def test_get_url(modules):
  126. '''
  127. cp.get_url with salt:// source given
  128. '''
  129. with pytest.helpers.temp_file('scene33') as path:
  130. ret = modules.cp.get_url('salt://grail/scene33', path)
  131. assert ret == path
  132. assert os.path.isfile(path)
  133. with salt.utils.files.fopen(path, 'r') as scene:
  134. data = salt.utils.stringutils.to_unicode(scene.read())
  135. assert 'KNIGHT: They\'re nervous, sire.' in data
  136. assert 'bacon' not in data
  137. def test_get_url_makedirs(modules):
  138. '''
  139. cp.get_url
  140. '''
  141. with pytest.helpers.temp_directory('make') as path:
  142. tgt = os.path.join(path, 'dirs', 'scene33')
  143. ret = modules.cp.get_url('salt://grail/scene33', tgt, makedirs=True)
  144. assert ret == tgt
  145. assert os.path.isfile(tgt)
  146. with salt.utils.files.fopen(tgt, 'r') as scene:
  147. data = salt.utils.stringutils.to_unicode(scene.read())
  148. assert 'KNIGHT: They\'re nervous, sire.' in data
  149. assert 'bacon' not in data
  150. def test_get_url_dest_empty(modules):
  151. '''
  152. cp.get_url with salt:// source given and destination omitted.
  153. '''
  154. ret = modules.cp.get_url('salt://grail/scene33')
  155. try:
  156. assert os.path.isfile(ret)
  157. with salt.utils.files.fopen(ret, 'r') as scene:
  158. data = salt.utils.stringutils.to_unicode(scene.read())
  159. assert 'KNIGHT: They\'re nervous, sire.' in data
  160. assert 'bacon' not in data
  161. finally:
  162. os.unlink(ret)
  163. def test_get_url_no_dest(modules):
  164. '''
  165. cp.get_url with salt:// source given and destination set as None
  166. '''
  167. ret = modules.cp.get_url('salt://grail/scene33', None)
  168. assert isinstance(ret, six.text_type)
  169. assert 'KNIGHT: They\'re nervous, sire.' in ret
  170. assert 'bacon' not in ret
  171. def test_get_url_nonexistent_source(modules):
  172. '''
  173. cp.get_url with nonexistent salt:// source given
  174. '''
  175. ret = modules.cp.get_url('salt://grail/nonexistent_scene', None)
  176. assert ret is False
  177. ret = modules.cp.get_url('salt://grail/nonexistent_scene')
  178. assert ret is False
  179. def test_get_url_to_dir(modules):
  180. '''
  181. cp.get_url with salt:// source
  182. '''
  183. with pytest.helpers.temp_directory() as tgt:
  184. path = os.path.join(tgt, 'scene33')
  185. ret = modules.cp.get_url('salt://grail/scene33', tgt)
  186. assert ret == path
  187. assert os.path.isfile(path)
  188. with salt.utils.files.fopen(path, 'r') as scene:
  189. data = salt.utils.stringutils.to_unicode(scene.read())
  190. assert 'KNIGHT: They\'re nervous, sire.' in data
  191. assert 'bacon' not in data
  192. @pytest.mark.skipif(SSL3_SUPPORT is False, reason='Requires python with SSL3 support')
  193. @pytest.mark.requires_network
  194. def test_get_url_https(modules):
  195. '''
  196. cp.get_url with https:// source given
  197. '''
  198. with pytest.helpers.temp_file('index.html') as path:
  199. ret = modules.cp.get_url('https://repo.saltstack.com/index.html', path)
  200. assert ret == path
  201. with salt.utils.files.fopen(path, 'r') as instructions:
  202. data = salt.utils.stringutils.to_unicode(instructions.read())
  203. assert 'Bootstrap' in data
  204. assert 'Debian' in data
  205. assert 'Windows' in data
  206. assert 'AYBABTU' not in data
  207. @pytest.mark.skipif(SSL3_SUPPORT is False, reason='Requires python with SSL3 support')
  208. @pytest.mark.requires_network
  209. def test_get_url_https_empty_dest(modules):
  210. '''
  211. cp.get_url with https:// source given and destination omitted.
  212. '''
  213. path = modules.cp.get_url('https://repo.saltstack.com/index.html')
  214. try:
  215. with salt.utils.files.fopen(path, 'r') as instructions:
  216. data = salt.utils.stringutils.to_unicode(instructions.read())
  217. assert 'Bootstrap' in data
  218. assert 'Debian' in data
  219. assert 'Windows' in data
  220. assert 'AYBABTU' not in data
  221. finally:
  222. try:
  223. os.unlink(path)
  224. except OSError:
  225. pass
  226. @pytest.mark.skipif(SSL3_SUPPORT is False, reason='Requires python with SSL3 support')
  227. @pytest.mark.requires_network
  228. def test_get_url_https_no_dest(modules):
  229. '''
  230. cp.get_url with https:// source given and destination set as None
  231. '''
  232. data = modules.cp.get_url('https://repo.saltstack.com/index.html', None)
  233. assert 'Bootstrap' in data
  234. assert 'Debian' in data
  235. assert 'Windows' in data
  236. assert 'AYBABTU' not in data
  237. def test_get_url_file(modules):
  238. '''
  239. cp.get_url with file:// source given
  240. '''
  241. src = os.path.join('file://', RUNTIME_VARS.FILES, 'file', 'base', 'file.big')
  242. path = modules.cp.get_url(src)
  243. with salt.utils.files.fopen(path, 'r') as scene:
  244. data = salt.utils.stringutils.to_unicode(scene.read())
  245. assert 'KNIGHT: They\'re nervous, sire.' in data
  246. assert 'bacon' not in data
  247. def test_get_url_file_no_dest(modules):
  248. '''
  249. cp.get_url with file:// source given and destination set as None
  250. '''
  251. src = os.path.join('file://', RUNTIME_VARS.FILES, 'file', 'base', 'file.big')
  252. ret = modules.cp.get_url(src, None)
  253. assert 'KNIGHT: They\'re nervous, sire.' in ret
  254. assert 'bacon' not in ret
  255. # cp.get_file_str tests
  256. def test_get_file_str_salt(modules):
  257. '''
  258. cp.get_file_str with salt:// source given
  259. '''
  260. src = 'salt://grail/scene33'
  261. ret = modules.cp.get_file_str(src)
  262. assert 'KNIGHT: They\'re nervous, sire.' in ret
  263. def test_get_file_str_nonexistent_source(modules):
  264. '''
  265. cp.get_file_str with nonexistent salt:// source given
  266. '''
  267. src = 'salt://grail/nonexistent_scene'
  268. ret = modules.cp.get_file_str(src)
  269. assert ret is False
  270. @pytest.mark.skipif(SSL3_SUPPORT is False, reason='Requires python with SSL3 support')
  271. @pytest.mark.requires_network
  272. def test_get_file_str_https(modules):
  273. '''
  274. cp.get_file_str with https:// source given
  275. '''
  276. src = 'https://repo.saltstack.com/index.html'
  277. data = modules.cp.get_file_str(src)
  278. assert 'Bootstrap' in data
  279. assert 'Debian' in data
  280. assert 'Windows' in data
  281. assert 'AYBABTU' not in data
  282. def test_get_file_str_local(modules):
  283. '''
  284. cp.get_file_str with file:// source given
  285. '''
  286. src = os.path.join('file://', RUNTIME_VARS.FILES, 'file', 'base', 'file.big')
  287. ret = modules.cp.get_file_str(src)
  288. assert 'KNIGHT: They\'re nervous, sire.' in ret
  289. assert 'bacon' not in ret
  290. def test_list_states(modules):
  291. '''
  292. cp.list_states
  293. '''
  294. top_sls_contents = '''
  295. base:
  296. '*':
  297. - core
  298. '''
  299. core_sls_contents = '''
  300. # Goes nowhere, does nothing.
  301. gndn:
  302. test.succeed_with_changes
  303. '''
  304. with pytest.helpers.temp_state_file('top.sls', top_sls_contents):
  305. with pytest.helpers.temp_state_file('core.sls', core_sls_contents):
  306. ret = modules.cp.list_states()
  307. assert 'top' in ret
  308. assert 'core' in ret
  309. def test_envs(modules):
  310. assert sorted(modules.cp.envs()) == sorted(['base', 'prod'])