test_fileclient.py 19 KB

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