1
0

test_archive.py 10 KB

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