test_archive.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Tests for the archive state
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import errno
  8. import logging
  9. import os
  10. import pytest
  11. # Import Salt Testing libs
  12. from tests.support.case import ModuleCase
  13. from tests.support.helpers import Webserver
  14. from tests.support.mixins import SaltReturnAssertsMixin
  15. from tests.support.runtests import RUNTIME_VARS
  16. # Import Salt libs
  17. import salt.utils.files
  18. import salt.utils.platform
  19. # Setup logging
  20. log = logging.getLogger(__name__)
  21. ARCHIVE_DIR = os.path.join('c:/', 'tmp') \
  22. if salt.utils.platform.is_windows() \
  23. else '/tmp/archive'
  24. ARCHIVE_NAME = 'custom.tar.gz'
  25. ARCHIVE_TAR_SOURCE = 'http://localhost:{0}/{1}'.format(9999, ARCHIVE_NAME)
  26. ARCHIVE_TAR_HASH = 'md5=7643861ac07c30fe7d2310e9f25ca514'
  27. ARCHIVE_TAR_SHA_HASH = 'sha256=9591159d86f0a180e4e0645b2320d0235e23e66c66797df61508bf185e0ac1d2'
  28. ARCHIVE_TAR_BAD_HASH = 'md5=d41d8cd98f00b204e9800998ecf8427e'
  29. ARCHIVE_TAR_HASH_UPPER = 'md5=7643861AC07C30FE7D2310E9F25CA514'
  30. class ArchiveTest(ModuleCase, SaltReturnAssertsMixin):
  31. '''
  32. Validate the archive state
  33. '''
  34. @classmethod
  35. def setUpClass(cls):
  36. cls.webserver = Webserver()
  37. cls.webserver.start()
  38. cls.archive_tar_source = cls.webserver.url('custom.tar.gz')
  39. cls.archive_local_tar_source = 'file://{0}'.format(os.path.join(RUNTIME_VARS.BASE_FILES, ARCHIVE_NAME))
  40. cls.untar_file = os.path.join(ARCHIVE_DIR, 'custom/README')
  41. @classmethod
  42. def tearDownClass(cls):
  43. cls.webserver.stop()
  44. def setUp(self):
  45. self._clear_archive_dir()
  46. def tearDown(self):
  47. self._clear_archive_dir()
  48. @staticmethod
  49. def _clear_archive_dir():
  50. try:
  51. salt.utils.files.rm_rf(ARCHIVE_DIR)
  52. except OSError as exc:
  53. if exc.errno != errno.ENOENT:
  54. raise
  55. def _check_extracted(self, path):
  56. '''
  57. function to check if file was extracted
  58. '''
  59. log.debug('Checking for extracted file: %s', path)
  60. self.assertTrue(os.path.isfile(path))
  61. def run_function(self, *args, **kwargs):
  62. ret = super(ArchiveTest, self).run_function(*args, **kwargs)
  63. log.debug('ret = %s', ret)
  64. return ret
  65. def run_state(self, *args, **kwargs):
  66. ret = super(ArchiveTest, self).run_state(*args, **kwargs)
  67. log.debug('ret = %s', ret)
  68. return ret
  69. def test_archive_extracted_skip_verify(self):
  70. '''
  71. test archive.extracted with skip_verify
  72. '''
  73. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  74. source=self.archive_tar_source, archive_format='tar',
  75. skip_verify=True)
  76. if 'Timeout' in ret:
  77. self.skipTest('Timeout talking to local tornado server.')
  78. self.assertSaltTrueReturn(ret)
  79. self._check_extracted(self.untar_file)
  80. def test_archive_extracted_with_source_hash(self):
  81. '''
  82. test archive.extracted without skip_verify
  83. only external resources work to check to
  84. ensure source_hash is verified correctly
  85. '''
  86. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  87. source=self.archive_tar_source, archive_format='tar',
  88. source_hash=ARCHIVE_TAR_HASH)
  89. if 'Timeout' in ret:
  90. self.skipTest('Timeout talking to local tornado server.')
  91. self.assertSaltTrueReturn(ret)
  92. self._check_extracted(self.untar_file)
  93. @pytest.mark.skip_if_not_root
  94. def test_archive_extracted_with_root_user_and_group(self):
  95. '''
  96. test archive.extracted with user and group set to "root"
  97. '''
  98. r_group = 'root'
  99. if salt.utils.platform.is_darwin():
  100. r_group = 'wheel'
  101. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  102. source=self.archive_tar_source, archive_format='tar',
  103. source_hash=ARCHIVE_TAR_HASH,
  104. user='root', group=r_group)
  105. if 'Timeout' in ret:
  106. self.skipTest('Timeout talking to local tornado server.')
  107. self.assertSaltTrueReturn(ret)
  108. self._check_extracted(self.untar_file)
  109. def test_archive_extracted_with_strip_in_options(self):
  110. '''
  111. test archive.extracted with --strip in options
  112. '''
  113. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  114. source=self.archive_tar_source,
  115. source_hash=ARCHIVE_TAR_HASH,
  116. options='--strip=1',
  117. enforce_toplevel=False)
  118. if 'Timeout' in ret:
  119. self.skipTest('Timeout talking to local tornado server.')
  120. self.assertSaltTrueReturn(ret)
  121. self._check_extracted(os.path.join(ARCHIVE_DIR, 'README'))
  122. def test_archive_extracted_with_strip_components_in_options(self):
  123. '''
  124. test archive.extracted with --strip-components in options
  125. '''
  126. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  127. source=self.archive_tar_source,
  128. source_hash=ARCHIVE_TAR_HASH,
  129. options='--strip-components=1',
  130. enforce_toplevel=False)
  131. if 'Timeout' in ret:
  132. self.skipTest('Timeout talking to local tornado server.')
  133. self.assertSaltTrueReturn(ret)
  134. self._check_extracted(os.path.join(ARCHIVE_DIR, 'README'))
  135. def test_archive_extracted_without_archive_format(self):
  136. '''
  137. test archive.extracted with no archive_format option
  138. '''
  139. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  140. source=self.archive_tar_source,
  141. source_hash=ARCHIVE_TAR_HASH)
  142. if 'Timeout' in ret:
  143. self.skipTest('Timeout talking to local tornado server.')
  144. self.assertSaltTrueReturn(ret)
  145. self._check_extracted(self.untar_file)
  146. def test_archive_extracted_with_cmd_unzip_false(self):
  147. '''
  148. test archive.extracted using use_cmd_unzip argument as false
  149. '''
  150. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  151. source=self.archive_tar_source,
  152. source_hash=ARCHIVE_TAR_HASH,
  153. use_cmd_unzip=False,
  154. archive_format='tar')
  155. if 'Timeout' in ret:
  156. self.skipTest('Timeout talking to local tornado server.')
  157. self.assertSaltTrueReturn(ret)
  158. self._check_extracted(self.untar_file)
  159. def test_local_archive_extracted(self):
  160. '''
  161. test archive.extracted with local file
  162. '''
  163. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  164. source=self.archive_local_tar_source, archive_format='tar')
  165. self.assertSaltTrueReturn(ret)
  166. self._check_extracted(self.untar_file)
  167. def test_local_archive_extracted_skip_verify(self):
  168. '''
  169. test archive.extracted with local file, bad hash and skip_verify
  170. '''
  171. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  172. source=self.archive_local_tar_source, archive_format='tar',
  173. source_hash=ARCHIVE_TAR_BAD_HASH, skip_verify=True)
  174. self.assertSaltTrueReturn(ret)
  175. self._check_extracted(self.untar_file)
  176. def test_local_archive_extracted_with_source_hash(self):
  177. '''
  178. test archive.extracted with local file and valid hash
  179. '''
  180. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  181. source=self.archive_local_tar_source, archive_format='tar',
  182. source_hash=ARCHIVE_TAR_HASH)
  183. self.assertSaltTrueReturn(ret)
  184. self._check_extracted(self.untar_file)
  185. def test_local_archive_extracted_with_bad_source_hash(self):
  186. '''
  187. test archive.extracted with local file and bad hash
  188. '''
  189. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  190. source=self.archive_local_tar_source, archive_format='tar',
  191. source_hash=ARCHIVE_TAR_BAD_HASH)
  192. self.assertSaltFalseReturn(ret)
  193. def test_local_archive_extracted_with_uppercase_source_hash(self):
  194. '''
  195. test archive.extracted with local file and bad hash
  196. '''
  197. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  198. source=self.archive_local_tar_source, archive_format='tar',
  199. source_hash=ARCHIVE_TAR_HASH_UPPER)
  200. self.assertSaltTrueReturn(ret)
  201. self._check_extracted(self.untar_file)
  202. def test_archive_extracted_with_non_base_saltenv(self):
  203. '''
  204. test archive.extracted with a saltenv other than `base`
  205. '''
  206. ret = self.run_function(
  207. 'state.sls',
  208. ['issue45893'],
  209. pillar={'issue45893.name': ARCHIVE_DIR},
  210. saltenv='prod')
  211. self.assertSaltTrueReturn(ret)
  212. self._check_extracted(os.path.join(ARCHIVE_DIR, self.untar_file))
  213. def test_local_archive_extracted_with_skip_files_list_verify(self):
  214. '''
  215. test archive.extracted with local file and skip_files_list_verify set to True
  216. '''
  217. expected_comment = ('existing source sum is the same as the expected one and '
  218. 'skip_files_list_verify argument was set to True. '
  219. 'Extraction is not needed')
  220. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  221. source=self.archive_local_tar_source, archive_format='tar',
  222. skip_files_list_verify=True,
  223. source_hash_update=True,
  224. source_hash=ARCHIVE_TAR_SHA_HASH)
  225. self.assertSaltTrueReturn(ret)
  226. self._check_extracted(self.untar_file)
  227. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  228. source=self.archive_local_tar_source, archive_format='tar',
  229. skip_files_list_verify=True,
  230. source_hash_update=True,
  231. source_hash=ARCHIVE_TAR_SHA_HASH)
  232. self.assertSaltTrueReturn(ret)
  233. self.assertInSaltComment(expected_comment, ret)