1
0

test_archive.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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.paths import FILES
  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_LOCAL_TAR_SOURCE = 'file://{0}'.format(os.path.join(FILES, 'file', 'base', ARCHIVE_NAME))
  26. UNTAR_FILE = os.path.join(ARCHIVE_DIR, 'custom/README')
  27. ARCHIVE_TAR_HASH = 'md5=7643861ac07c30fe7d2310e9f25ca514'
  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. @classmethod
  40. def tearDownClass(cls):
  41. cls.webserver.stop()
  42. def setUp(self):
  43. self._clear_archive_dir()
  44. def tearDown(self):
  45. self._clear_archive_dir()
  46. @staticmethod
  47. def _clear_archive_dir():
  48. try:
  49. salt.utils.files.rm_rf(ARCHIVE_DIR)
  50. except OSError as exc:
  51. if exc.errno != errno.ENOENT:
  52. raise
  53. def _check_extracted(self, path):
  54. '''
  55. function to check if file was extracted
  56. '''
  57. log.debug('Checking for extracted file: %s', path)
  58. self.assertTrue(os.path.isfile(path))
  59. def run_function(self, *args, **kwargs):
  60. ret = super(ArchiveTest, self).run_function(*args, **kwargs)
  61. log.debug('ret = %s', ret)
  62. return ret
  63. def run_state(self, *args, **kwargs):
  64. ret = super(ArchiveTest, self).run_state(*args, **kwargs)
  65. log.debug('ret = %s', ret)
  66. return ret
  67. def test_archive_extracted_skip_verify(self):
  68. '''
  69. test archive.extracted with skip_verify
  70. '''
  71. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  72. source=self.archive_tar_source, archive_format='tar',
  73. skip_verify=True)
  74. if 'Timeout' in ret:
  75. self.skipTest('Timeout talking to local tornado server.')
  76. self.assertSaltTrueReturn(ret)
  77. self._check_extracted(UNTAR_FILE)
  78. def test_archive_extracted_with_source_hash(self):
  79. '''
  80. test archive.extracted without skip_verify
  81. only external resources work to check to
  82. ensure source_hash is verified correctly
  83. '''
  84. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  85. source=self.archive_tar_source, archive_format='tar',
  86. source_hash=ARCHIVE_TAR_HASH)
  87. if 'Timeout' in ret:
  88. self.skipTest('Timeout talking to local tornado server.')
  89. self.assertSaltTrueReturn(ret)
  90. self._check_extracted(UNTAR_FILE)
  91. @skip_if_not_root
  92. def test_archive_extracted_with_root_user_and_group(self):
  93. '''
  94. test archive.extracted with user and group set to "root"
  95. '''
  96. r_group = 'root'
  97. if salt.utils.platform.is_darwin():
  98. r_group = 'wheel'
  99. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  100. source=self.archive_tar_source, archive_format='tar',
  101. source_hash=ARCHIVE_TAR_HASH,
  102. user='root', group=r_group)
  103. if 'Timeout' in ret:
  104. self.skipTest('Timeout talking to local tornado server.')
  105. self.assertSaltTrueReturn(ret)
  106. self._check_extracted(UNTAR_FILE)
  107. def test_archive_extracted_with_strip_in_options(self):
  108. '''
  109. test archive.extracted with --strip in options
  110. '''
  111. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  112. source=self.archive_tar_source,
  113. source_hash=ARCHIVE_TAR_HASH,
  114. options='--strip=1',
  115. enforce_toplevel=False)
  116. if 'Timeout' in ret:
  117. self.skipTest('Timeout talking to local tornado server.')
  118. self.assertSaltTrueReturn(ret)
  119. self._check_extracted(os.path.join(ARCHIVE_DIR, 'README'))
  120. def test_archive_extracted_with_strip_components_in_options(self):
  121. '''
  122. test archive.extracted with --strip-components in options
  123. '''
  124. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  125. source=self.archive_tar_source,
  126. source_hash=ARCHIVE_TAR_HASH,
  127. options='--strip-components=1',
  128. enforce_toplevel=False)
  129. if 'Timeout' in ret:
  130. self.skipTest('Timeout talking to local tornado server.')
  131. self.assertSaltTrueReturn(ret)
  132. self._check_extracted(os.path.join(ARCHIVE_DIR, 'README'))
  133. def test_archive_extracted_without_archive_format(self):
  134. '''
  135. test archive.extracted with no archive_format option
  136. '''
  137. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  138. source=self.archive_tar_source,
  139. source_hash=ARCHIVE_TAR_HASH)
  140. if 'Timeout' in ret:
  141. self.skipTest('Timeout talking to local tornado server.')
  142. self.assertSaltTrueReturn(ret)
  143. self._check_extracted(UNTAR_FILE)
  144. def test_archive_extracted_with_cmd_unzip_false(self):
  145. '''
  146. test archive.extracted using use_cmd_unzip argument as false
  147. '''
  148. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  149. source=self.archive_tar_source,
  150. source_hash=ARCHIVE_TAR_HASH,
  151. use_cmd_unzip=False,
  152. archive_format='tar')
  153. if 'Timeout' in ret:
  154. self.skipTest('Timeout talking to local tornado server.')
  155. self.assertSaltTrueReturn(ret)
  156. self._check_extracted(UNTAR_FILE)
  157. def test_local_archive_extracted(self):
  158. '''
  159. test archive.extracted with local file
  160. '''
  161. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  162. source=ARCHIVE_LOCAL_TAR_SOURCE, archive_format='tar')
  163. self.assertSaltTrueReturn(ret)
  164. self._check_extracted(UNTAR_FILE)
  165. def test_local_archive_extracted_skip_verify(self):
  166. '''
  167. test archive.extracted with local file, bad hash and skip_verify
  168. '''
  169. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  170. source=ARCHIVE_LOCAL_TAR_SOURCE, archive_format='tar',
  171. source_hash=ARCHIVE_TAR_BAD_HASH, skip_verify=True)
  172. self.assertSaltTrueReturn(ret)
  173. self._check_extracted(UNTAR_FILE)
  174. def test_local_archive_extracted_with_source_hash(self):
  175. '''
  176. test archive.extracted with local file and valid hash
  177. '''
  178. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  179. source=ARCHIVE_LOCAL_TAR_SOURCE, archive_format='tar',
  180. source_hash=ARCHIVE_TAR_HASH)
  181. self.assertSaltTrueReturn(ret)
  182. self._check_extracted(UNTAR_FILE)
  183. def test_local_archive_extracted_with_bad_source_hash(self):
  184. '''
  185. test archive.extracted with local file and bad hash
  186. '''
  187. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  188. source=ARCHIVE_LOCAL_TAR_SOURCE, archive_format='tar',
  189. source_hash=ARCHIVE_TAR_BAD_HASH)
  190. self.assertSaltFalseReturn(ret)
  191. def test_local_archive_extracted_with_uppercase_source_hash(self):
  192. '''
  193. test archive.extracted with local file and bad hash
  194. '''
  195. ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
  196. source=ARCHIVE_LOCAL_TAR_SOURCE, archive_format='tar',
  197. source_hash=ARCHIVE_TAR_HASH_UPPER)
  198. self.assertSaltTrueReturn(ret)
  199. self._check_extracted(UNTAR_FILE)
  200. def test_archive_extracted_with_non_base_saltenv(self):
  201. '''
  202. test archive.extracted with a saltenv other than `base`
  203. '''
  204. ret = self.run_function(
  205. 'state.sls',
  206. ['issue45893'],
  207. pillar={'issue45893.name': ARCHIVE_DIR},
  208. saltenv='prod')
  209. self.assertSaltTrueReturn(ret)
  210. self._check_extracted(os.path.join(ARCHIVE_DIR, UNTAR_FILE))