1
0

test_fileclient.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Tests for the salt fileclient
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import
  7. import errno
  8. import logging
  9. import os
  10. import shutil
  11. # Import Salt Testing libs
  12. from tests.integration import AdaptedConfigurationTestCaseMixin
  13. from tests.support.mixins import LoaderModuleMockMixin
  14. from tests.support.mock import patch, Mock, MagicMock, NO_MOCK, NO_MOCK_REASON
  15. from tests.support.paths import TMP
  16. from tests.support.unit import TestCase, skipIf
  17. # Import Salt libs
  18. import salt.utils.files
  19. from salt.ext.six.moves import range
  20. from salt import fileclient
  21. from salt.ext import six
  22. log = logging.getLogger(__name__)
  23. class FileclientTestCase(TestCase):
  24. '''
  25. Fileclient test
  26. '''
  27. opts = {
  28. 'extension_modules': '',
  29. 'cachedir': '/__test__',
  30. }
  31. def _fake_makedir(self, num=errno.EEXIST):
  32. def _side_effect(*args, **kwargs):
  33. raise OSError(num, 'Errno {0}'.format(num))
  34. return Mock(side_effect=_side_effect)
  35. def test_cache_skips_makedirs_on_race_condition(self):
  36. '''
  37. If cache contains already a directory, do not raise an exception.
  38. '''
  39. with patch('os.path.isfile', lambda prm: False):
  40. for exists in range(2):
  41. with patch('os.makedirs', self._fake_makedir()):
  42. with fileclient.Client(self.opts)._cache_loc('testfile') as c_ref_itr:
  43. assert c_ref_itr == os.sep + os.sep.join(['__test__', 'files', 'base', 'testfile'])
  44. def test_cache_raises_exception_on_non_eexist_ioerror(self):
  45. '''
  46. If makedirs raises other than EEXIST errno, an exception should be raised.
  47. '''
  48. with patch('os.path.isfile', lambda prm: False):
  49. with patch('os.makedirs', self._fake_makedir(num=errno.EROFS)):
  50. with self.assertRaises(OSError):
  51. with fileclient.Client(self.opts)._cache_loc('testfile') as c_ref_itr:
  52. assert c_ref_itr == '/__test__/files/base/testfile'
  53. def test_extrn_path_with_long_filename(self):
  54. safe_file_name = os.path.split(fileclient.Client(self.opts)._extrn_path('https://test.com/' + ('A' * 254), 'base'))[-1]
  55. assert safe_file_name == 'A' * 254
  56. oversized_file_name = os.path.split(fileclient.Client(self.opts)._extrn_path('https://test.com/' + ('A' * 255), 'base'))[-1]
  57. assert len(oversized_file_name) < 256
  58. assert oversized_file_name != 'A' * 255
  59. oversized_file_with_query_params = os.path.split(fileclient.Client(self.opts)._extrn_path('https://test.com/file?' + ('A' * 255), 'base'))[-1]
  60. assert len(oversized_file_with_query_params) < 256
  61. SALTENVS = ('base', 'dev')
  62. FS_ROOT = os.path.join(TMP, 'fileclient_fs_root')
  63. CACHE_ROOT = os.path.join(TMP, 'fileclient_cache_root')
  64. SUBDIR = 'subdir'
  65. SUBDIR_FILES = ('foo.txt', 'bar.txt', 'baz.txt')
  66. def _get_file_roots():
  67. return dict(
  68. [(x, [os.path.join(FS_ROOT, x)]) for x in SALTENVS]
  69. )
  70. MOCKED_OPTS = {
  71. 'file_roots': _get_file_roots(),
  72. 'fileserver_backend': ['roots'],
  73. 'cachedir': CACHE_ROOT,
  74. 'file_client': 'local',
  75. }
  76. @skipIf(NO_MOCK, NO_MOCK_REASON)
  77. class FileClientTest(TestCase, AdaptedConfigurationTestCaseMixin, LoaderModuleMockMixin):
  78. def setup_loader_modules(self):
  79. return {fileclient: {'__opts__': MOCKED_OPTS}}
  80. def setUp(self):
  81. self.file_client = fileclient.Client(self.master_opts)
  82. def tearDown(self):
  83. del self.file_client
  84. def test_file_list_emptydirs(self):
  85. '''
  86. Ensure that the fileclient class won't allow a direct call to file_list_emptydirs()
  87. '''
  88. with self.assertRaises(NotImplementedError):
  89. self.file_client.file_list_emptydirs()
  90. def test_get_file(self):
  91. '''
  92. Ensure that the fileclient class won't allow a direct call to get_file()
  93. '''
  94. with self.assertRaises(NotImplementedError):
  95. self.file_client.get_file(None)
  96. def test_get_file_client(self):
  97. minion_opts = self.get_temp_config('minion')
  98. minion_opts['file_client'] = 'remote'
  99. with patch('salt.fileclient.RemoteClient', MagicMock(return_value='remote_client')):
  100. ret = fileclient.get_file_client(minion_opts)
  101. self.assertEqual('remote_client', ret)
  102. @skipIf(NO_MOCK, NO_MOCK_REASON)
  103. class FileclientCacheTest(TestCase, AdaptedConfigurationTestCaseMixin, LoaderModuleMockMixin):
  104. '''
  105. Tests for the fileclient caching. The LocalClient is the only thing we can
  106. test as it is the only way we can mock the fileclient (the tests run from
  107. the minion process, so the master cannot be mocked from test code).
  108. '''
  109. def setup_loader_modules(self):
  110. return {fileclient: {'__opts__': MOCKED_OPTS}}
  111. def setUp(self):
  112. '''
  113. No need to add a dummy foo.txt to muddy up the github repo, just make
  114. our own fileserver root on-the-fly.
  115. '''
  116. def _new_dir(path):
  117. '''
  118. Add a new dir at ``path`` using os.makedirs. If the directory
  119. already exists, remove it recursively and then try to create it
  120. again.
  121. '''
  122. try:
  123. os.makedirs(path)
  124. except OSError as exc:
  125. if exc.errno == errno.EEXIST:
  126. # Just in case a previous test was interrupted, remove the
  127. # directory and try adding it again.
  128. shutil.rmtree(path)
  129. os.makedirs(path)
  130. else:
  131. raise
  132. # Crete the FS_ROOT
  133. for saltenv in SALTENVS:
  134. saltenv_root = os.path.join(FS_ROOT, saltenv)
  135. # Make sure we have a fresh root dir for this saltenv
  136. _new_dir(saltenv_root)
  137. path = os.path.join(saltenv_root, 'foo.txt')
  138. with salt.utils.files.fopen(path, 'w') as fp_:
  139. fp_.write(
  140. 'This is a test file in the \'{0}\' saltenv.\n'
  141. .format(saltenv)
  142. )
  143. subdir_abspath = os.path.join(saltenv_root, SUBDIR)
  144. os.makedirs(subdir_abspath)
  145. for subdir_file in SUBDIR_FILES:
  146. path = os.path.join(subdir_abspath, subdir_file)
  147. with salt.utils.files.fopen(path, 'w') as fp_:
  148. fp_.write(
  149. 'This is file \'{0}\' in subdir \'{1} from saltenv '
  150. '\'{2}\''.format(subdir_file, SUBDIR, saltenv)
  151. )
  152. # Create the CACHE_ROOT
  153. _new_dir(CACHE_ROOT)
  154. def tearDown(self):
  155. '''
  156. Remove the directories created for these tests
  157. '''
  158. shutil.rmtree(FS_ROOT)
  159. shutil.rmtree(CACHE_ROOT)
  160. def test_cache_dir(self):
  161. '''
  162. Ensure entire directory is cached to correct location
  163. '''
  164. patched_opts = dict((x, y) for x, y in six.iteritems(self.minion_opts))
  165. patched_opts.update(MOCKED_OPTS)
  166. with patch.dict(fileclient.__opts__, patched_opts):
  167. client = fileclient.get_file_client(fileclient.__opts__, pillar=False)
  168. for saltenv in SALTENVS:
  169. self.assertTrue(
  170. client.cache_dir(
  171. 'salt://{0}'.format(SUBDIR),
  172. saltenv,
  173. cachedir=None
  174. )
  175. )
  176. for subdir_file in SUBDIR_FILES:
  177. cache_loc = os.path.join(fileclient.__opts__['cachedir'],
  178. 'files',
  179. saltenv,
  180. SUBDIR,
  181. subdir_file)
  182. # Double check that the content of the cached file
  183. # identifies it as being from the correct saltenv. The
  184. # setUp function creates the file with the name of the
  185. # saltenv mentioned in the file, so a simple 'in' check is
  186. # sufficient here. If opening the file raises an exception,
  187. # this is a problem, so we are not catching the exception
  188. # and letting it be raised so that the test fails.
  189. with salt.utils.files.fopen(cache_loc) as fp_:
  190. content = fp_.read()
  191. log.debug('cache_loc = %s', cache_loc)
  192. log.debug('content = %s', content)
  193. self.assertTrue(subdir_file in content)
  194. self.assertTrue(SUBDIR in content)
  195. self.assertTrue(saltenv in content)
  196. def test_cache_dir_with_alternate_cachedir_and_absolute_path(self):
  197. '''
  198. Ensure entire directory is cached to correct location when an alternate
  199. cachedir is specified and that cachedir is an absolute path
  200. '''
  201. patched_opts = dict((x, y) for x, y in six.iteritems(self.minion_opts))
  202. patched_opts.update(MOCKED_OPTS)
  203. alt_cachedir = os.path.join(TMP, 'abs_cachedir')
  204. with patch.dict(fileclient.__opts__, patched_opts):
  205. client = fileclient.get_file_client(fileclient.__opts__, pillar=False)
  206. for saltenv in SALTENVS:
  207. self.assertTrue(
  208. client.cache_dir(
  209. 'salt://{0}'.format(SUBDIR),
  210. saltenv,
  211. cachedir=alt_cachedir
  212. )
  213. )
  214. for subdir_file in SUBDIR_FILES:
  215. cache_loc = os.path.join(alt_cachedir,
  216. 'files',
  217. saltenv,
  218. SUBDIR,
  219. subdir_file)
  220. # Double check that the content of the cached file
  221. # identifies it as being from the correct saltenv. The
  222. # setUp function creates the file with the name of the
  223. # saltenv mentioned in the file, so a simple 'in' check is
  224. # sufficient here. If opening the file raises an exception,
  225. # this is a problem, so we are not catching the exception
  226. # and letting it be raised so that the test fails.
  227. with salt.utils.files.fopen(cache_loc) as fp_:
  228. content = fp_.read()
  229. log.debug('cache_loc = %s', cache_loc)
  230. log.debug('content = %s', content)
  231. self.assertTrue(subdir_file in content)
  232. self.assertTrue(SUBDIR in content)
  233. self.assertTrue(saltenv in content)
  234. def test_cache_dir_with_alternate_cachedir_and_relative_path(self):
  235. '''
  236. Ensure entire directory is cached to correct location when an alternate
  237. cachedir is specified and that cachedir is a relative path
  238. '''
  239. patched_opts = dict((x, y) for x, y in six.iteritems(self.minion_opts))
  240. patched_opts.update(MOCKED_OPTS)
  241. alt_cachedir = 'foo'
  242. with patch.dict(fileclient.__opts__, patched_opts):
  243. client = fileclient.get_file_client(fileclient.__opts__, pillar=False)
  244. for saltenv in SALTENVS:
  245. self.assertTrue(
  246. client.cache_dir(
  247. 'salt://{0}'.format(SUBDIR),
  248. saltenv,
  249. cachedir=alt_cachedir
  250. )
  251. )
  252. for subdir_file in SUBDIR_FILES:
  253. cache_loc = os.path.join(fileclient.__opts__['cachedir'],
  254. alt_cachedir,
  255. 'files',
  256. saltenv,
  257. SUBDIR,
  258. subdir_file)
  259. # Double check that the content of the cached file
  260. # identifies it as being from the correct saltenv. The
  261. # setUp function creates the file with the name of the
  262. # saltenv mentioned in the file, so a simple 'in' check is
  263. # sufficient here. If opening the file raises an exception,
  264. # this is a problem, so we are not catching the exception
  265. # and letting it be raised so that the test fails.
  266. with salt.utils.files.fopen(cache_loc) as fp_:
  267. content = fp_.read()
  268. log.debug('cache_loc = %s', cache_loc)
  269. log.debug('content = %s', content)
  270. self.assertTrue(subdir_file in content)
  271. self.assertTrue(SUBDIR in content)
  272. self.assertTrue(saltenv in content)
  273. def test_cache_file(self):
  274. '''
  275. Ensure file is cached to correct location
  276. '''
  277. patched_opts = dict((x, y) for x, y in six.iteritems(self.minion_opts))
  278. patched_opts.update(MOCKED_OPTS)
  279. with patch.dict(fileclient.__opts__, patched_opts):
  280. client = fileclient.get_file_client(fileclient.__opts__, pillar=False)
  281. for saltenv in SALTENVS:
  282. self.assertTrue(
  283. client.cache_file('salt://foo.txt', saltenv, cachedir=None)
  284. )
  285. cache_loc = os.path.join(
  286. fileclient.__opts__['cachedir'], 'files', saltenv, 'foo.txt')
  287. # Double check that the content of the cached file identifies
  288. # it as being from the correct saltenv. The setUp function
  289. # creates the file with the name of the saltenv mentioned in
  290. # the file, so a simple 'in' check is sufficient here. If
  291. # opening the file raises an exception, this is a problem, so
  292. # we are not catching the exception and letting it be raised so
  293. # that the test fails.
  294. with salt.utils.files.fopen(cache_loc) as fp_:
  295. content = fp_.read()
  296. log.debug('cache_loc = %s', cache_loc)
  297. log.debug('content = %s', content)
  298. self.assertTrue(saltenv in content)
  299. def test_cache_file_with_alternate_cachedir_and_absolute_path(self):
  300. '''
  301. Ensure file is cached to correct location when an alternate cachedir is
  302. specified and that cachedir is an absolute path
  303. '''
  304. patched_opts = dict((x, y) for x, y in six.iteritems(self.minion_opts))
  305. patched_opts.update(MOCKED_OPTS)
  306. alt_cachedir = os.path.join(TMP, 'abs_cachedir')
  307. with patch.dict(fileclient.__opts__, patched_opts):
  308. client = fileclient.get_file_client(fileclient.__opts__, pillar=False)
  309. for saltenv in SALTENVS:
  310. self.assertTrue(
  311. client.cache_file('salt://foo.txt',
  312. saltenv,
  313. cachedir=alt_cachedir)
  314. )
  315. cache_loc = os.path.join(alt_cachedir,
  316. 'files',
  317. saltenv,
  318. 'foo.txt')
  319. # Double check that the content of the cached file identifies
  320. # it as being from the correct saltenv. The setUp function
  321. # creates the file with the name of the saltenv mentioned in
  322. # the file, so a simple 'in' check is sufficient here. If
  323. # opening the file raises an exception, this is a problem, so
  324. # we are not catching the exception and letting it be raised so
  325. # that the test fails.
  326. with salt.utils.files.fopen(cache_loc) as fp_:
  327. content = fp_.read()
  328. log.debug('cache_loc = %s', cache_loc)
  329. log.debug('content = %s', content)
  330. self.assertTrue(saltenv in content)
  331. def test_cache_file_with_alternate_cachedir_and_relative_path(self):
  332. '''
  333. Ensure file is cached to correct location when an alternate cachedir is
  334. specified and that cachedir is a relative path
  335. '''
  336. patched_opts = dict((x, y) for x, y in six.iteritems(self.minion_opts))
  337. patched_opts.update(MOCKED_OPTS)
  338. alt_cachedir = 'foo'
  339. with patch.dict(fileclient.__opts__, patched_opts):
  340. client = fileclient.get_file_client(fileclient.__opts__, pillar=False)
  341. for saltenv in SALTENVS:
  342. self.assertTrue(
  343. client.cache_file('salt://foo.txt',
  344. saltenv,
  345. cachedir=alt_cachedir)
  346. )
  347. cache_loc = os.path.join(fileclient.__opts__['cachedir'],
  348. alt_cachedir,
  349. 'files',
  350. saltenv,
  351. 'foo.txt')
  352. # Double check that the content of the cached file identifies
  353. # it as being from the correct saltenv. The setUp function
  354. # creates the file with the name of the saltenv mentioned in
  355. # the file, so a simple 'in' check is sufficient here. If
  356. # opening the file raises an exception, this is a problem, so
  357. # we are not catching the exception and letting it be raised so
  358. # that the test fails.
  359. with salt.utils.files.fopen(cache_loc) as fp_:
  360. content = fp_.read()
  361. log.debug('cache_loc = %s', cache_loc)
  362. log.debug('content = %s', content)
  363. self.assertTrue(saltenv in content)