test_thin.py 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. """
  2. :codeauthor: :email:`Bo Maryniuk <bo@suse.de>`
  3. """
  4. import copy
  5. import os
  6. import pathlib
  7. import shutil
  8. import sys
  9. import tarfile
  10. import tempfile
  11. import jinja2
  12. import salt.exceptions
  13. import salt.ext.six
  14. import salt.utils.hashutils
  15. import salt.utils.json
  16. import salt.utils.platform
  17. import salt.utils.stringutils
  18. from salt.ext.six.moves import range
  19. from salt.utils import thin
  20. from salt.utils.stringutils import to_bytes as bts
  21. from tests.support.helpers import TstSuiteLoggingHandler, VirtualEnv
  22. from tests.support.mock import MagicMock, patch
  23. from tests.support.runtests import RUNTIME_VARS
  24. from tests.support.unit import TestCase, skipIf
  25. try:
  26. import pytest
  27. except ImportError:
  28. pytest = None
  29. @skipIf(pytest is None, "PyTest is missing")
  30. class SSHThinTestCase(TestCase):
  31. """
  32. TestCase for SaltSSH-related parts.
  33. """
  34. def setUp(self):
  35. self.jinja_fp = os.path.dirname(jinja2.__file__)
  36. self.ext_conf = {
  37. "test": {
  38. "py-version": [2, 7],
  39. "path": RUNTIME_VARS.SALT_CODE_DIR,
  40. "dependencies": {"jinja2": self.jinja_fp},
  41. }
  42. }
  43. self.tops = copy.deepcopy(self.ext_conf)
  44. self.tops["test"]["dependencies"] = [self.jinja_fp]
  45. self.tar = self._tarfile(None).open()
  46. self.digest = salt.utils.hashutils.DigestCollector()
  47. self.exp_files = [
  48. os.path.join("salt", "payload.py"),
  49. os.path.join("jinja2", "__init__.py"),
  50. ]
  51. lib_root = os.path.join(RUNTIME_VARS.TMP, "fake-libs")
  52. self.fake_libs = {
  53. "distro": os.path.join(lib_root, "distro"),
  54. "jinja2": os.path.join(lib_root, "jinja2"),
  55. "yaml": os.path.join(lib_root, "yaml"),
  56. "tornado": os.path.join(lib_root, "tornado"),
  57. "msgpack": os.path.join(lib_root, "msgpack"),
  58. }
  59. code_dir = pathlib.Path(RUNTIME_VARS.CODE_DIR).resolve()
  60. self.exp_ret = {
  61. "distro": str(code_dir / "distro.py"),
  62. "jinja2": str(code_dir / "jinja2"),
  63. "yaml": str(code_dir / "yaml"),
  64. "tornado": str(code_dir / "tornado"),
  65. "msgpack": str(code_dir / "msgpack"),
  66. "certifi": str(code_dir / "certifi"),
  67. "singledispatch": str(code_dir / "singledispatch.py"),
  68. }
  69. self.exc_libs = ["jinja2", "yaml"]
  70. def tearDown(self):
  71. for lib, fp in self.fake_libs.items():
  72. if os.path.exists(fp):
  73. shutil.rmtree(fp)
  74. self.exc_libs = None
  75. self.jinja_fp = None
  76. self.ext_conf = None
  77. self.tops = None
  78. self.tar = None
  79. self.digest = None
  80. self.exp_files = None
  81. self.fake_libs = None
  82. self.exp_ret = None
  83. def _popen(self, return_value=None, side_effect=None, returncode=0):
  84. """
  85. Fake subprocess.Popen
  86. :return:
  87. """
  88. proc = MagicMock()
  89. proc.communicate = MagicMock(return_value=return_value, side_effect=side_effect)
  90. proc.returncode = returncode
  91. popen = MagicMock(return_value=proc)
  92. return popen
  93. def _version_info(self, major=None, minor=None):
  94. """
  95. Fake version info.
  96. :param major:
  97. :param minor:
  98. :return:
  99. """
  100. class VersionInfo(tuple):
  101. pass
  102. vi = VersionInfo([major, minor])
  103. vi.major = major or sys.version_info.major
  104. vi.minor = minor or sys.version_info.minor
  105. return vi
  106. def _tarfile(self, getinfo=False):
  107. """
  108. Fake tarfile handler.
  109. :return:
  110. """
  111. spec = ["add", "close"]
  112. if getinfo:
  113. spec.append("getinfo")
  114. tf = MagicMock()
  115. tf.open = MagicMock(return_value=MagicMock(spec=spec))
  116. return tf
  117. @patch("salt.utils.thin.log", MagicMock())
  118. @patch("salt.utils.thin.os.path.isfile", MagicMock(return_value=False))
  119. def test_get_ext_tops_cfg_missing_dependencies(self):
  120. """
  121. Test thin.get_ext_tops contains all required dependencies.
  122. :return:
  123. """
  124. cfg = {"namespace": {"py-version": [0, 0], "path": "/foo", "dependencies": []}}
  125. with pytest.raises(salt.exceptions.SaltSystemExit) as err:
  126. thin.get_ext_tops(cfg)
  127. self.assertIn("Missing dependencies", str(err.value))
  128. self.assertTrue(thin.log.error.called)
  129. self.assertIn("Missing dependencies", thin.log.error.call_args[0][0])
  130. self.assertIn("jinja2, yaml, tornado, msgpack", thin.log.error.call_args[0][0])
  131. @patch("salt.exceptions.SaltSystemExit", Exception)
  132. @patch("salt.utils.thin.log", MagicMock())
  133. @patch("salt.utils.thin.os.path.isfile", MagicMock(return_value=False))
  134. def test_get_ext_tops_cfg_missing_interpreter(self):
  135. """
  136. Test thin.get_ext_tops contains interpreter configuration.
  137. :return:
  138. """
  139. cfg = {"namespace": {"path": "/foo", "dependencies": []}}
  140. with pytest.raises(salt.exceptions.SaltSystemExit) as err:
  141. thin.get_ext_tops(cfg)
  142. self.assertIn("missing specific locked Python version", str(err.value))
  143. @patch("salt.exceptions.SaltSystemExit", Exception)
  144. @patch("salt.utils.thin.log", MagicMock())
  145. @patch("salt.utils.thin.os.path.isfile", MagicMock(return_value=False))
  146. def test_get_ext_tops_cfg_wrong_interpreter(self):
  147. """
  148. Test thin.get_ext_tops contains correct interpreter configuration.
  149. :return:
  150. """
  151. cfg = {"namespace": {"path": "/foo", "py-version": 2, "dependencies": []}}
  152. with pytest.raises(salt.exceptions.SaltSystemExit) as err:
  153. thin.get_ext_tops(cfg)
  154. self.assertIn(
  155. "specific locked Python version should be a list of " "major/minor version",
  156. str(err.value),
  157. )
  158. @patch("salt.exceptions.SaltSystemExit", Exception)
  159. @patch("salt.utils.thin.log", MagicMock())
  160. @patch("salt.utils.thin.os.path.isfile", MagicMock(return_value=False))
  161. def test_get_ext_tops_cfg_interpreter(self):
  162. """
  163. Test thin.get_ext_tops interpreter configuration.
  164. :return:
  165. """
  166. cfg = {
  167. "namespace": {
  168. "path": "/foo",
  169. "py-version": [2, 6],
  170. "dependencies": {
  171. "jinja2": "",
  172. "yaml": "",
  173. "tornado": "",
  174. "msgpack": "",
  175. },
  176. }
  177. }
  178. with pytest.raises(salt.exceptions.SaltSystemExit):
  179. thin.get_ext_tops(cfg)
  180. assert len(thin.log.warning.mock_calls) == 4
  181. assert sorted([x[1][1] for x in thin.log.warning.mock_calls]) == [
  182. "jinja2",
  183. "msgpack",
  184. "tornado",
  185. "yaml",
  186. ]
  187. assert (
  188. "Module test has missing configuration"
  189. == thin.log.warning.mock_calls[0][1][0] % "test"
  190. )
  191. @patch("salt.utils.thin.log", MagicMock())
  192. @patch("salt.utils.thin.os.path.isfile", MagicMock(return_value=False))
  193. def test_get_ext_tops_dependency_config_check(self):
  194. """
  195. Test thin.get_ext_tops dependencies are importable
  196. :return:
  197. """
  198. cfg = {
  199. "namespace": {
  200. "path": "/foo",
  201. "py-version": [2, 6],
  202. "dependencies": {
  203. "jinja2": "/jinja/foo.py",
  204. "yaml": "/yaml/",
  205. "tornado": "/tornado/wrong.rb",
  206. "msgpack": "msgpack.sh",
  207. },
  208. }
  209. }
  210. with pytest.raises(salt.exceptions.SaltSystemExit) as err:
  211. thin.get_ext_tops(cfg)
  212. self.assertIn(
  213. "Missing dependencies for the alternative version in the "
  214. "external configuration",
  215. str(err.value),
  216. )
  217. messages = {}
  218. for cl in thin.log.warning.mock_calls:
  219. messages[cl[1][1]] = cl[1][0] % (cl[1][1], cl[1][2])
  220. for mod in ["tornado", "yaml", "msgpack"]:
  221. self.assertIn("not a Python importable module", messages[mod])
  222. self.assertIn(
  223. "configured with not a file or does not exist", messages["jinja2"]
  224. )
  225. @patch("salt.exceptions.SaltSystemExit", Exception)
  226. @patch("salt.utils.thin.log", MagicMock())
  227. @patch("salt.utils.thin.os.path.isfile", MagicMock(return_value=True))
  228. def test_get_ext_tops_config_pass(self):
  229. """
  230. Test thin.get_ext_tops configuration
  231. :return:
  232. """
  233. cfg = {
  234. "namespace": {
  235. "path": "/foo",
  236. "py-version": [2, 6],
  237. "dependencies": {
  238. "jinja2": "/jinja/foo.py",
  239. "yaml": "/yaml/",
  240. "tornado": "/tornado/tornado.py",
  241. "msgpack": "msgpack.py",
  242. "distro": "distro.py",
  243. },
  244. }
  245. }
  246. out = thin.get_ext_tops(cfg)
  247. assert out["namespace"]["py-version"] == cfg["namespace"]["py-version"]
  248. assert out["namespace"]["path"] == cfg["namespace"]["path"]
  249. assert sorted(out["namespace"]["dependencies"]) == sorted(
  250. [
  251. "/tornado/tornado.py",
  252. "/jinja/foo.py",
  253. "/yaml/",
  254. "msgpack.py",
  255. "distro.py",
  256. ]
  257. )
  258. @patch("salt.utils.thin.sys.argv", [None, '{"foo": "bar"}'])
  259. @patch("salt.utils.thin.get_tops", lambda **kw: kw)
  260. def test_gte(self):
  261. """
  262. Test thin.gte external call for processing the info about tops per interpreter.
  263. :return:
  264. """
  265. assert salt.utils.json.loads(thin.gte()).get("foo") == "bar"
  266. def test_add_dep_path(self):
  267. """
  268. Test thin._add_dependency function to setup dependency paths
  269. :return:
  270. """
  271. container = []
  272. for pth in ["/foo/bar.py", "/something/else/__init__.py"]:
  273. thin._add_dependency(container, type("obj", (), {"__file__": pth})())
  274. assert "__init__" not in container[1]
  275. assert container == ["/foo/bar.py", "/something/else"]
  276. def test_thin_path(self):
  277. """
  278. Test thin.thin_path returns the expected path.
  279. :return:
  280. """
  281. path = os.sep + os.path.join("path", "to")
  282. expected = os.path.join(path, "thin", "thin.tgz")
  283. self.assertEqual(thin.thin_path(path), expected)
  284. def test_get_salt_call_script(self):
  285. """
  286. Test get salt-call script rendered.
  287. :return:
  288. """
  289. out = thin._get_salt_call("foo", "bar", py26=[2, 6], py27=[2, 7], py34=[3, 4])
  290. for line in salt.utils.stringutils.to_str(out).split(os.linesep):
  291. if line.startswith("namespaces = {"):
  292. data = salt.utils.json.loads(line.replace("namespaces = ", "").strip())
  293. assert data.get("py26") == [2, 6]
  294. assert data.get("py27") == [2, 7]
  295. assert data.get("py34") == [3, 4]
  296. if line.startswith("syspaths = "):
  297. data = salt.utils.json.loads(line.replace("syspaths = ", ""))
  298. assert data == ["foo", "bar"]
  299. def test_get_ext_namespaces_empty(self):
  300. """
  301. Test thin._get_ext_namespaces function returns an empty dictionary on nothing
  302. :return:
  303. """
  304. for obj in [None, {}, []]:
  305. assert thin._get_ext_namespaces(obj) == {}
  306. def test_get_ext_namespaces(self):
  307. """
  308. Test thin._get_ext_namespaces function returns namespaces properly out of the config.
  309. :return:
  310. """
  311. cfg = {"ns": {"py-version": [2, 7]}}
  312. assert thin._get_ext_namespaces(cfg).get("ns") == (2, 7,)
  313. assert isinstance(thin._get_ext_namespaces(cfg).get("ns"), tuple)
  314. def test_get_ext_namespaces_failure(self):
  315. """
  316. Test thin._get_ext_namespaces function raises an exception
  317. if python major/minor version is not configured.
  318. :return:
  319. """
  320. with pytest.raises(salt.exceptions.SaltSystemExit):
  321. thin._get_ext_namespaces({"ns": {}})
  322. @patch(
  323. "salt.utils.thin.distro",
  324. type("distro", (), {"__file__": "/site-packages/distro"}),
  325. )
  326. @patch(
  327. "salt.utils.thin.salt", type("salt", (), {"__file__": "/site-packages/salt"}),
  328. )
  329. @patch(
  330. "salt.utils.thin.jinja2",
  331. type("jinja2", (), {"__file__": "/site-packages/jinja2"}),
  332. )
  333. @patch(
  334. "salt.utils.thin.yaml", type("yaml", (), {"__file__": "/site-packages/yaml"}),
  335. )
  336. @patch(
  337. "salt.utils.thin.tornado",
  338. type("tornado", (), {"__file__": "/site-packages/tornado"}),
  339. )
  340. @patch(
  341. "salt.utils.thin.msgpack",
  342. type("msgpack", (), {"__file__": "/site-packages/msgpack"}),
  343. )
  344. @patch(
  345. "salt.utils.thin.certifi",
  346. type("certifi", (), {"__file__": "/site-packages/certifi"}),
  347. )
  348. @patch(
  349. "salt.utils.thin.singledispatch",
  350. type("singledispatch", (), {"__file__": "/site-packages/sdp"}),
  351. )
  352. @patch(
  353. "salt.utils.thin.singledispatch_helpers",
  354. type("singledispatch_helpers", (), {"__file__": "/site-packages/sdp_hlp"}),
  355. )
  356. @patch(
  357. "salt.utils.thin.ssl_match_hostname",
  358. type("ssl_match_hostname", (), {"__file__": "/site-packages/ssl_mh"}),
  359. )
  360. @patch(
  361. "salt.utils.thin.markupsafe",
  362. type("markupsafe", (), {"__file__": "/site-packages/markupsafe"}),
  363. )
  364. @patch(
  365. "salt.utils.thin.backports_abc",
  366. type("backports_abc", (), {"__file__": "/site-packages/backports_abc"}),
  367. )
  368. @patch(
  369. "salt.utils.thin.concurrent",
  370. type("concurrent", (), {"__file__": "/site-packages/concurrent"}),
  371. )
  372. @patch("salt.utils.thin.log", MagicMock())
  373. def test_get_tops(self):
  374. """
  375. Test thin.get_tops to get top directories, based on the interpreter.
  376. :return:
  377. """
  378. base_tops = [
  379. "/site-packages/distro",
  380. "/site-packages/salt",
  381. "/site-packages/jinja2",
  382. "/site-packages/yaml",
  383. "/site-packages/tornado",
  384. "/site-packages/msgpack",
  385. "/site-packages/certifi",
  386. "/site-packages/sdp",
  387. "/site-packages/sdp_hlp",
  388. "/site-packages/ssl_mh",
  389. "/site-packages/markupsafe",
  390. "/site-packages/backports_abc",
  391. "/site-packages/concurrent",
  392. ]
  393. tops = thin.get_tops()
  394. assert len(tops) == len(base_tops)
  395. assert sorted(tops) == sorted(base_tops)
  396. @patch(
  397. "salt.utils.thin.distro",
  398. type("distro", (), {"__file__": "/site-packages/distro"}),
  399. )
  400. @patch(
  401. "salt.utils.thin.salt", type("salt", (), {"__file__": "/site-packages/salt"}),
  402. )
  403. @patch(
  404. "salt.utils.thin.jinja2",
  405. type("jinja2", (), {"__file__": "/site-packages/jinja2"}),
  406. )
  407. @patch(
  408. "salt.utils.thin.yaml", type("yaml", (), {"__file__": "/site-packages/yaml"}),
  409. )
  410. @patch(
  411. "salt.utils.thin.tornado",
  412. type("tornado", (), {"__file__": "/site-packages/tornado"}),
  413. )
  414. @patch(
  415. "salt.utils.thin.msgpack",
  416. type("msgpack", (), {"__file__": "/site-packages/msgpack"}),
  417. )
  418. @patch(
  419. "salt.utils.thin.certifi",
  420. type("certifi", (), {"__file__": "/site-packages/certifi"}),
  421. )
  422. @patch(
  423. "salt.utils.thin.singledispatch",
  424. type("singledispatch", (), {"__file__": "/site-packages/sdp"}),
  425. )
  426. @patch(
  427. "salt.utils.thin.singledispatch_helpers",
  428. type("singledispatch_helpers", (), {"__file__": "/site-packages/sdp_hlp"}),
  429. )
  430. @patch(
  431. "salt.utils.thin.ssl_match_hostname",
  432. type("ssl_match_hostname", (), {"__file__": "/site-packages/ssl_mh"}),
  433. )
  434. @patch(
  435. "salt.utils.thin.markupsafe",
  436. type("markupsafe", (), {"__file__": "/site-packages/markupsafe"}),
  437. )
  438. @patch(
  439. "salt.utils.thin.backports_abc",
  440. type("backports_abc", (), {"__file__": "/site-packages/backports_abc"}),
  441. )
  442. @patch(
  443. "salt.utils.thin.concurrent",
  444. type("concurrent", (), {"__file__": "/site-packages/concurrent"}),
  445. )
  446. @patch("salt.utils.thin.log", MagicMock())
  447. def test_get_tops_extra_mods(self):
  448. """
  449. Test thin.get_tops to get extra-modules alongside the top directories, based on the interpreter.
  450. :return:
  451. """
  452. base_tops = [
  453. "/site-packages/distro",
  454. "/site-packages/salt",
  455. "/site-packages/jinja2",
  456. "/site-packages/yaml",
  457. "/site-packages/tornado",
  458. "/site-packages/msgpack",
  459. "/site-packages/certifi",
  460. "/site-packages/sdp",
  461. "/site-packages/sdp_hlp",
  462. "/site-packages/ssl_mh",
  463. "/site-packages/concurrent",
  464. "/site-packages/markupsafe",
  465. "/site-packages/backports_abc",
  466. os.sep + os.path.join("custom", "foo"),
  467. os.sep + os.path.join("custom", "bar.py"),
  468. ]
  469. builtins = sys.version_info.major == 3 and "builtins" or "__builtin__"
  470. foo = {"__file__": os.sep + os.path.join("custom", "foo", "__init__.py")}
  471. bar = {"__file__": os.sep + os.path.join("custom", "bar")}
  472. with patch(
  473. "{}.__import__".format(builtins),
  474. MagicMock(side_effect=[type("foo", (), foo), type("bar", (), bar)]),
  475. ):
  476. tops = thin.get_tops(extra_mods="foo,bar")
  477. self.assertEqual(len(tops), len(base_tops))
  478. self.assertListEqual(sorted(tops), sorted(base_tops))
  479. @patch(
  480. "salt.utils.thin.distro",
  481. type("distro", (), {"__file__": "/site-packages/distro"}),
  482. )
  483. @patch(
  484. "salt.utils.thin.salt", type("salt", (), {"__file__": "/site-packages/salt"}),
  485. )
  486. @patch(
  487. "salt.utils.thin.jinja2",
  488. type("jinja2", (), {"__file__": "/site-packages/jinja2"}),
  489. )
  490. @patch(
  491. "salt.utils.thin.yaml", type("yaml", (), {"__file__": "/site-packages/yaml"}),
  492. )
  493. @patch(
  494. "salt.utils.thin.tornado",
  495. type("tornado", (), {"__file__": "/site-packages/tornado"}),
  496. )
  497. @patch(
  498. "salt.utils.thin.msgpack",
  499. type("msgpack", (), {"__file__": "/site-packages/msgpack"}),
  500. )
  501. @patch(
  502. "salt.utils.thin.certifi",
  503. type("certifi", (), {"__file__": "/site-packages/certifi"}),
  504. )
  505. @patch(
  506. "salt.utils.thin.singledispatch",
  507. type("singledispatch", (), {"__file__": "/site-packages/sdp"}),
  508. )
  509. @patch(
  510. "salt.utils.thin.singledispatch_helpers",
  511. type("singledispatch_helpers", (), {"__file__": "/site-packages/sdp_hlp"}),
  512. )
  513. @patch(
  514. "salt.utils.thin.ssl_match_hostname",
  515. type("ssl_match_hostname", (), {"__file__": "/site-packages/ssl_mh"}),
  516. )
  517. @patch(
  518. "salt.utils.thin.markupsafe",
  519. type("markupsafe", (), {"__file__": "/site-packages/markupsafe"}),
  520. )
  521. @patch(
  522. "salt.utils.thin.backports_abc",
  523. type("backports_abc", (), {"__file__": "/site-packages/backports_abc"}),
  524. )
  525. @patch(
  526. "salt.utils.thin.concurrent",
  527. type("concurrent", (), {"__file__": "/site-packages/concurrent"}),
  528. )
  529. @patch("salt.utils.thin.log", MagicMock())
  530. def test_get_tops_so_mods(self):
  531. """
  532. Test thin.get_tops to get extra-modules alongside the top directories, based on the interpreter.
  533. :return:
  534. """
  535. base_tops = [
  536. "/site-packages/distro",
  537. "/site-packages/salt",
  538. "/site-packages/jinja2",
  539. "/site-packages/yaml",
  540. "/site-packages/tornado",
  541. "/site-packages/msgpack",
  542. "/site-packages/certifi",
  543. "/site-packages/sdp",
  544. "/site-packages/sdp_hlp",
  545. "/site-packages/ssl_mh",
  546. "/site-packages/concurrent",
  547. "/site-packages/markupsafe",
  548. "/site-packages/backports_abc",
  549. "/custom/foo.so",
  550. "/custom/bar.so",
  551. ]
  552. builtins = sys.version_info.major == 3 and "builtins" or "__builtin__"
  553. with patch(
  554. "{}.__import__".format(builtins),
  555. MagicMock(
  556. side_effect=[
  557. type("salt", (), {"__file__": "/custom/foo.so"}),
  558. type("salt", (), {"__file__": "/custom/bar.so"}),
  559. ]
  560. ),
  561. ):
  562. tops = thin.get_tops(so_mods="foo,bar")
  563. assert len(tops) == len(base_tops)
  564. assert sorted(tops) == sorted(base_tops)
  565. @patch("salt.utils.thin.gen_thin", MagicMock(return_value="/path/to/thin/thin.tgz"))
  566. @patch("salt.utils.hashutils.get_hash", MagicMock(return_value=12345))
  567. def test_thin_sum(self):
  568. """
  569. Test thin.thin_sum function.
  570. :return:
  571. """
  572. assert thin.thin_sum("/cachedir", form="sha256")[1] == 12345
  573. thin.salt.utils.hashutils.get_hash.assert_called()
  574. assert thin.salt.utils.hashutils.get_hash.call_count == 1
  575. path, form = thin.salt.utils.hashutils.get_hash.call_args[0]
  576. assert path == "/path/to/thin/thin.tgz"
  577. assert form == "sha256"
  578. @patch("salt.utils.thin.gen_min", MagicMock(return_value="/path/to/thin/min.tgz"))
  579. @patch("salt.utils.hashutils.get_hash", MagicMock(return_value=12345))
  580. def test_min_sum(self):
  581. """
  582. Test thin.thin_sum function.
  583. :return:
  584. """
  585. assert thin.min_sum("/cachedir", form="sha256") == 12345
  586. thin.salt.utils.hashutils.get_hash.assert_called()
  587. assert thin.salt.utils.hashutils.get_hash.call_count == 1
  588. path, form = thin.salt.utils.hashutils.get_hash.call_args[0]
  589. assert path == "/path/to/thin/min.tgz"
  590. assert form == "sha256"
  591. @patch("salt.utils.thin.sys.version_info", (2, 5))
  592. @patch("salt.exceptions.SaltSystemExit", Exception)
  593. def test_gen_thin_fails_ancient_python_version(self):
  594. """
  595. Test thin.gen_thin function raises an exception
  596. if Python major/minor version is lower than 2.6
  597. :return:
  598. """
  599. with pytest.raises(salt.exceptions.SaltSystemExit) as err:
  600. thin.sys.exc_clear = lambda: None
  601. thin.gen_thin("")
  602. self.assertIn(
  603. "The minimum required python version to run salt-ssh is " '"3"',
  604. str(err.value),
  605. )
  606. @skipIf(
  607. salt.utils.platform.is_windows() and thin._six.PY2, "Dies on Python2 on Windows"
  608. )
  609. @patch("salt.exceptions.SaltSystemExit", Exception)
  610. @patch("salt.utils.thin.log", MagicMock())
  611. @patch("salt.utils.thin.os.makedirs", MagicMock())
  612. @patch("salt.utils.files.fopen", MagicMock())
  613. @patch("salt.utils.thin._get_salt_call", MagicMock())
  614. @patch("salt.utils.thin._get_ext_namespaces", MagicMock())
  615. @patch("salt.utils.thin.get_tops", MagicMock(return_value=["/foo3", "/bar3"]))
  616. @patch("salt.utils.thin.get_ext_tops", MagicMock(return_value={}))
  617. @patch("salt.utils.thin.os.path.isfile", MagicMock())
  618. @patch("salt.utils.thin.os.path.isdir", MagicMock(return_value=True))
  619. @patch("salt.utils.thin.log", MagicMock())
  620. @patch("salt.utils.thin.os.remove", MagicMock())
  621. @patch("salt.utils.thin.os.path.exists", MagicMock())
  622. @patch("salt.utils.path.os_walk", MagicMock(return_value=[]))
  623. @patch(
  624. "salt.utils.thin.subprocess.Popen",
  625. _popen(
  626. None,
  627. side_effect=[(bts("2.7"), bts("")), (bts('["/foo27", "/bar27"]'), bts(""))],
  628. ),
  629. )
  630. @patch("salt.utils.thin.tarfile", MagicMock())
  631. @patch("salt.utils.thin.zipfile", MagicMock())
  632. @patch("salt.utils.thin.os.getcwd", MagicMock())
  633. @patch("salt.utils.thin.os.access", MagicMock(return_value=True))
  634. @patch("salt.utils.thin.os.chdir", MagicMock())
  635. @patch("salt.utils.thin.os.close", MagicMock())
  636. @patch("salt.utils.thin.tempfile.mkdtemp", MagicMock())
  637. @patch(
  638. "salt.utils.thin.tempfile.mkstemp", MagicMock(return_value=(3, ".temporary"))
  639. )
  640. @patch("salt.utils.thin.shutil", MagicMock())
  641. @patch("salt.utils.thin.sys.version_info", _version_info(None, 3, 6))
  642. @patch("salt.utils.path.which", MagicMock(return_value="/usr/bin/python"))
  643. def test_gen_thin_compression_fallback_py3(self):
  644. """
  645. Test thin.gen_thin function if fallbacks to the gzip compression, once setup wrong.
  646. NOTE: Py2 version of this test is not required, as code shares the same spot across the versions.
  647. :return:
  648. """
  649. thin.gen_thin("", compress="arj")
  650. thin.log.warning.assert_called()
  651. pt, msg = thin.log.warning.mock_calls[0][1]
  652. assert (
  653. pt % msg
  654. == 'Unknown compression type: "arj". Falling back to "gzip" compression.'
  655. )
  656. thin.zipfile.ZipFile.assert_not_called()
  657. thin.tarfile.open.assert_called()
  658. @patch("salt.exceptions.SaltSystemExit", Exception)
  659. @patch("salt.utils.thin.log", MagicMock())
  660. @patch("salt.utils.thin.os.makedirs", MagicMock())
  661. @patch("salt.utils.files.fopen", MagicMock())
  662. @patch("salt.utils.thin._get_salt_call", MagicMock())
  663. @patch("salt.utils.thin._get_ext_namespaces", MagicMock())
  664. @patch("salt.utils.thin.get_tops", MagicMock(return_value=["/foo3", "/bar3"]))
  665. @patch("salt.utils.thin.get_ext_tops", MagicMock(return_value={}))
  666. @patch("salt.utils.thin.os.path.isfile", MagicMock())
  667. @patch("salt.utils.thin.os.path.isdir", MagicMock(return_value=False))
  668. @patch("salt.utils.thin.log", MagicMock())
  669. @patch("salt.utils.thin.os.remove", MagicMock())
  670. @patch("salt.utils.thin.os.path.exists", MagicMock())
  671. @patch("salt.utils.path.os_walk", MagicMock(return_value=[]))
  672. @patch(
  673. "salt.utils.thin.subprocess.Popen",
  674. _popen(
  675. None,
  676. side_effect=[(bts("2.7"), bts("")), (bts('["/foo27", "/bar27"]'), bts(""))],
  677. ),
  678. )
  679. @patch("salt.utils.thin.tarfile", MagicMock())
  680. @patch("salt.utils.thin.zipfile", MagicMock())
  681. @patch("salt.utils.thin.os.getcwd", MagicMock())
  682. @patch("salt.utils.thin.os.access", MagicMock(return_value=True))
  683. @patch("salt.utils.thin.os.chdir", MagicMock())
  684. @patch("salt.utils.thin.os.close", MagicMock())
  685. @patch("salt.utils.thin.tempfile.mkdtemp", MagicMock(return_value=""))
  686. @patch(
  687. "salt.utils.thin.tempfile.mkstemp", MagicMock(return_value=(3, ".temporary"))
  688. )
  689. @patch("salt.utils.thin.shutil", MagicMock())
  690. @patch("salt.utils.thin.sys.version_info", _version_info(None, 3, 6))
  691. @patch("salt.utils.path.which", MagicMock(return_value="/usr/bin/python"))
  692. def test_gen_thin_control_files_written_py3(self):
  693. """
  694. Test thin.gen_thin function if control files are written (version, salt-call etc).
  695. :return:
  696. """
  697. thin.gen_thin("")
  698. arc_name, arc_mode = thin.tarfile.method_calls[0][1]
  699. self.assertEqual(arc_name, ".temporary")
  700. self.assertEqual(arc_mode, "w:gz")
  701. for idx, fname in enumerate(
  702. ["version", ".thin-gen-py-version", "salt-call", "supported-versions"]
  703. ):
  704. name = thin.tarfile.open().method_calls[idx + 2][1][0]
  705. self.assertEqual(name, fname)
  706. thin.tarfile.open().close.assert_called()
  707. @patch("salt.exceptions.SaltSystemExit", Exception)
  708. @patch("salt.utils.thin.log", MagicMock())
  709. @patch("salt.utils.thin.os.makedirs", MagicMock())
  710. @patch("salt.utils.files.fopen", MagicMock())
  711. @patch("salt.utils.thin._get_salt_call", MagicMock())
  712. @patch("salt.utils.thin._get_ext_namespaces", MagicMock())
  713. @patch("salt.utils.thin.get_tops", MagicMock(return_value=["/salt", "/bar3"]))
  714. @patch("salt.utils.thin.get_ext_tops", MagicMock(return_value={}))
  715. @patch("salt.utils.thin.os.path.isfile", MagicMock())
  716. @patch("salt.utils.thin.os.path.isdir", MagicMock(return_value=True))
  717. @patch("salt.utils.thin.log", MagicMock())
  718. @patch("salt.utils.thin.os.remove", MagicMock())
  719. @patch("salt.utils.thin.os.path.exists", MagicMock())
  720. @patch(
  721. "salt.utils.path.os_walk",
  722. MagicMock(
  723. return_value=(
  724. ("root", [], ["r1", "r2", "r3"]),
  725. ("root2", [], ["r4", "r5", "r6"]),
  726. )
  727. ),
  728. )
  729. @patch("salt.utils.thin.tarfile", _tarfile(None))
  730. @patch("salt.utils.thin.zipfile", MagicMock())
  731. @patch(
  732. "salt.utils.thin.os.getcwd",
  733. MagicMock(return_value=os.path.join(RUNTIME_VARS.TMP, "fake-cwd")),
  734. )
  735. @patch("salt.utils.thin.os.chdir", MagicMock())
  736. @patch("salt.utils.thin.os.close", MagicMock())
  737. @patch("salt.utils.thin.tempfile.mkdtemp", MagicMock(return_value=""))
  738. @patch(
  739. "salt.utils.thin.tempfile.mkstemp", MagicMock(return_value=(3, ".temporary"))
  740. )
  741. @patch("salt.utils.thin.shutil", MagicMock())
  742. @patch("salt.utils.thin.sys.version_info", _version_info(None, 3, 6))
  743. @patch("salt.utils.hashutils.DigestCollector", MagicMock())
  744. @patch("salt.utils.path.which", MagicMock(return_value="/usr/bin/python"))
  745. def test_gen_thin_main_content_files_written_py3(self):
  746. """
  747. Test thin.gen_thin function if main content files are written.
  748. NOTE: Py2 version of this test is not required, as code shares the same spot across the versions.
  749. :return:
  750. """
  751. thin.gen_thin("")
  752. files = []
  753. for py in ("py3", "pyall"):
  754. for i in range(1, 4):
  755. files.append(os.path.join(py, "root", "r{}".format(i)))
  756. for i in range(4, 7):
  757. files.append(os.path.join(py, "root2", "r{}".format(i)))
  758. for cl in thin.tarfile.open().method_calls[:-6]:
  759. arcname = cl[2].get("arcname")
  760. self.assertIn(arcname, files)
  761. files.pop(files.index(arcname))
  762. self.assertFalse(files)
  763. @patch("salt.exceptions.SaltSystemExit", Exception)
  764. @patch("salt.utils.thin.log", MagicMock())
  765. @patch("salt.utils.thin.os.makedirs", MagicMock())
  766. @patch("salt.utils.files.fopen", MagicMock())
  767. @patch("salt.utils.thin._get_salt_call", MagicMock())
  768. @patch("salt.utils.thin._get_ext_namespaces", MagicMock())
  769. @patch("salt.utils.thin.get_tops", MagicMock(return_value=[]))
  770. @patch(
  771. "salt.utils.thin.get_ext_tops",
  772. MagicMock(
  773. return_value={
  774. "namespace": {
  775. "py-version": [3, 0],
  776. "path": "/opt/2015.8/salt",
  777. "dependencies": ["/opt/certifi", "/opt/whatever"],
  778. }
  779. }
  780. ),
  781. )
  782. @patch("salt.utils.thin.os.path.isfile", MagicMock())
  783. @patch("salt.utils.thin.os.path.isdir", MagicMock(return_value=True))
  784. @patch("salt.utils.thin.log", MagicMock())
  785. @patch("salt.utils.thin.os.remove", MagicMock())
  786. @patch("salt.utils.thin.os.path.exists", MagicMock())
  787. @patch(
  788. "salt.utils.path.os_walk",
  789. MagicMock(
  790. return_value=(
  791. ("root", [], ["r1", "r2", "r3"]),
  792. ("root2", [], ["r4", "r5", "r6"]),
  793. )
  794. ),
  795. )
  796. @patch("salt.utils.thin.tarfile", _tarfile(None))
  797. @patch("salt.utils.thin.zipfile", MagicMock())
  798. @patch(
  799. "salt.utils.thin.os.getcwd",
  800. MagicMock(return_value=os.path.join(RUNTIME_VARS.TMP, "fake-cwd")),
  801. )
  802. @patch("salt.utils.thin.os.chdir", MagicMock())
  803. @patch("salt.utils.thin.os.close", MagicMock())
  804. @patch("salt.utils.thin.tempfile.mkdtemp", MagicMock(return_value=""))
  805. @patch(
  806. "salt.utils.thin.tempfile.mkstemp", MagicMock(return_value=(3, ".temporary"))
  807. )
  808. @patch("salt.utils.thin.shutil", MagicMock())
  809. @patch("salt.utils.thin.sys.version_info", _version_info(None, 3, 6))
  810. @patch("salt.utils.hashutils.DigestCollector", MagicMock())
  811. @patch("salt.utils.path.which", MagicMock(return_value="/usr/bin/python"))
  812. def test_gen_thin_ext_alternative_content_files_written_py3(self):
  813. """
  814. Test thin.gen_thin function if external alternative content files are written.
  815. :return:
  816. """
  817. ext_conf = {
  818. "namespace": {
  819. "py-version": [3, 0],
  820. "path": "/opt/2015.8/salt",
  821. "dependencies": {
  822. "certifi": "/opt/certifi",
  823. "whatever": "/opt/whatever",
  824. },
  825. }
  826. }
  827. thin.gen_thin("", extended_cfg=ext_conf)
  828. files = []
  829. for py in ("pyall", "pyall", "py3"):
  830. for i in range(1, 4):
  831. files.append(os.path.join("namespace", py, "root", "r{}".format(i)))
  832. for i in range(4, 7):
  833. files.append(os.path.join("namespace", py, "root2", "r{}".format(i)))
  834. for idx, cl in enumerate(thin.tarfile.open().method_calls[:-6]):
  835. arcname = cl[2].get("arcname")
  836. self.assertIn(arcname, files)
  837. files.pop(files.index(arcname))
  838. self.assertFalse(files)
  839. def test_get_supported_py_config_typecheck(self):
  840. """
  841. Test collecting proper py-versions. Should return bytes type.
  842. :return:
  843. """
  844. tops = {}
  845. ext_cfg = {}
  846. out = thin._get_supported_py_config(tops=tops, extended_cfg=ext_cfg)
  847. assert type(salt.utils.stringutils.to_bytes("")) == type(out)
  848. def test_get_supported_py_config_base_tops(self):
  849. """
  850. Test collecting proper py-versions. Should return proper base tops.
  851. :return:
  852. """
  853. tops = {"3": ["/groundkeepers", "/stole"], "2": ["/the-root", "/password"]}
  854. ext_cfg = {}
  855. out = (
  856. salt.utils.stringutils.to_str(
  857. thin._get_supported_py_config(tops=tops, extended_cfg=ext_cfg)
  858. )
  859. .strip()
  860. .split(os.linesep)
  861. )
  862. self.assertEqual(len(out), 2)
  863. for t_line in ["py3:3:0", "py2:2:7"]:
  864. self.assertIn(t_line, out)
  865. def test_get_supported_py_config_ext_tops(self):
  866. """
  867. Test collecting proper py-versions. Should return proper ext conf tops.
  868. :return:
  869. """
  870. tops = {}
  871. ext_cfg = {
  872. "solar-interference": {"py-version": [2, 6]},
  873. "second-system-effect": {"py-version": [2, 7]},
  874. }
  875. out = (
  876. salt.utils.stringutils.to_str(
  877. thin._get_supported_py_config(tops=tops, extended_cfg=ext_cfg)
  878. )
  879. .strip()
  880. .split(os.linesep)
  881. )
  882. for t_line in ["second-system-effect:2:7", "solar-interference:2:6"]:
  883. self.assertIn(t_line, out)
  884. @patch("salt.exceptions.SaltSystemExit", Exception)
  885. @patch("salt.utils.thin.log", MagicMock())
  886. @patch("salt.utils.thin.os.makedirs", MagicMock())
  887. @patch("salt.utils.files.fopen", MagicMock())
  888. @patch("salt.utils.thin._get_salt_call", MagicMock())
  889. @patch("salt.utils.thin._get_ext_namespaces", MagicMock())
  890. @patch("salt.utils.thin.get_tops", MagicMock(return_value=["/foo3", "/bar3"]))
  891. @patch("salt.utils.thin.get_ext_tops", MagicMock(return_value={}))
  892. @patch("salt.utils.thin.os.path.isfile", MagicMock())
  893. @patch("salt.utils.thin.os.path.isdir", MagicMock(return_value=False))
  894. @patch("salt.utils.thin.log", MagicMock())
  895. @patch("salt.utils.thin.os.remove", MagicMock())
  896. @patch("salt.utils.thin.os.path.exists", MagicMock())
  897. @patch("salt.utils.path.os_walk", MagicMock(return_value=[]))
  898. @patch(
  899. "salt.utils.thin.subprocess.Popen",
  900. _popen(
  901. None,
  902. side_effect=[(bts("2.7"), bts("")), (bts('["/foo27", "/bar27"]'), bts(""))],
  903. ),
  904. )
  905. @patch("salt.utils.thin.tarfile", MagicMock())
  906. @patch("salt.utils.thin.zipfile", MagicMock())
  907. @patch("salt.utils.thin.os.getcwd", MagicMock())
  908. @patch("salt.utils.thin.os.access", MagicMock(return_value=False))
  909. @patch("salt.utils.thin.os.chdir", MagicMock())
  910. @patch("salt.utils.thin.os.close", MagicMock())
  911. @patch("salt.utils.thin.tempfile.mkdtemp", MagicMock(return_value=""))
  912. @patch(
  913. "salt.utils.thin.tempfile.mkstemp", MagicMock(return_value=(3, ".temporary"))
  914. )
  915. @patch("salt.utils.thin.shutil", MagicMock())
  916. @patch("salt.utils.thin.sys.version_info", _version_info(None, 3, 6))
  917. def test_gen_thin_control_files_written_access_denied_cwd(self):
  918. """
  919. Test thin.gen_thin function if control files are written (version, salt-call etc)
  920. when the current working directory is inaccessible, eg. Salt is configured to run as
  921. a non-root user but the command is executed in a directory that the user does not
  922. have permissions to. Issue #54317.
  923. NOTE: Py2 version of this test is not required, as code shares the same spot across the versions.
  924. :return:
  925. """
  926. thin.gen_thin("")
  927. arc_name, arc_mode = thin.tarfile.method_calls[0][1]
  928. self.assertEqual(arc_name, ".temporary")
  929. self.assertEqual(arc_mode, "w:gz")
  930. for idx, fname in enumerate(
  931. ["version", ".thin-gen-py-version", "salt-call", "supported-versions"]
  932. ):
  933. name = thin.tarfile.open().method_calls[idx + 2][1][0]
  934. self.assertEqual(name, fname)
  935. thin.tarfile.open().close.assert_called()
  936. def test_get_tops_python(self):
  937. """
  938. test get_tops_python
  939. """
  940. patch_proc = patch(
  941. "salt.utils.thin.subprocess.Popen",
  942. self._popen(
  943. None,
  944. side_effect=[
  945. (bts("jinja2/__init__.py"), bts("")),
  946. (bts("yaml/__init__.py"), bts("")),
  947. (bts("tornado/__init__.py"), bts("")),
  948. (bts("msgpack/__init__.py"), bts("")),
  949. (bts("certifi/__init__.py"), bts("")),
  950. (bts("singledispatch.py"), bts("")),
  951. (bts(""), bts("")),
  952. (bts(""), bts("")),
  953. (bts(""), bts("")),
  954. (bts(""), bts("")),
  955. (bts(""), bts("")),
  956. (bts("distro.py"), bts("")),
  957. ],
  958. ),
  959. )
  960. patch_os = patch("os.path.exists", return_value=True)
  961. patch_which = patch("salt.utils.path.which", return_value=True)
  962. with patch_proc, patch_os, patch_which:
  963. with TstSuiteLoggingHandler() as log_handler:
  964. exp_ret = copy.deepcopy(self.exp_ret)
  965. ret = thin.get_tops_python("python3.7", ext_py_ver=[3, 7])
  966. if salt.utils.platform.is_windows():
  967. for key, value in ret.items():
  968. ret[key] = str(pathlib.Path(value).resolve(strict=False))
  969. for key, value in exp_ret.items():
  970. exp_ret[key] = str(pathlib.Path(value).resolve(strict=False))
  971. assert ret == exp_ret
  972. assert (
  973. "ERROR:Could not auto detect file location for module concurrent for python version python3.7"
  974. in log_handler.messages
  975. )
  976. def test_get_tops_python_exclude(self):
  977. """
  978. test get_tops_python when excluding modules
  979. """
  980. patch_proc = patch(
  981. "salt.utils.thin.subprocess.Popen",
  982. self._popen(
  983. None,
  984. side_effect=[
  985. (bts("tornado/__init__.py"), bts("")),
  986. (bts("msgpack/__init__.py"), bts("")),
  987. (bts("certifi/__init__.py"), bts("")),
  988. (bts("singledispatch.py"), bts("")),
  989. (bts(""), bts("")),
  990. (bts(""), bts("")),
  991. (bts(""), bts("")),
  992. (bts(""), bts("")),
  993. (bts(""), bts("")),
  994. (bts("distro.py"), bts("")),
  995. ],
  996. ),
  997. )
  998. exp_ret = copy.deepcopy(self.exp_ret)
  999. for lib in self.exc_libs:
  1000. exp_ret.pop(lib)
  1001. patch_os = patch("os.path.exists", return_value=True)
  1002. patch_which = patch("salt.utils.path.which", return_value=True)
  1003. with patch_proc, patch_os, patch_which:
  1004. ret = thin.get_tops_python(
  1005. "python3.7", exclude=self.exc_libs, ext_py_ver=[3, 7]
  1006. )
  1007. if salt.utils.platform.is_windows():
  1008. for key, value in ret.items():
  1009. ret[key] = str(pathlib.Path(value).resolve(strict=False))
  1010. for key, value in exp_ret.items():
  1011. exp_ret[key] = str(pathlib.Path(value).resolve(strict=False))
  1012. assert ret == exp_ret
  1013. def test_pack_alternatives_exclude(self):
  1014. """
  1015. test pack_alternatives when mixing
  1016. manually set dependencies and auto
  1017. detecting other modules.
  1018. """
  1019. patch_proc = patch(
  1020. "salt.utils.thin.subprocess.Popen",
  1021. self._popen(
  1022. None,
  1023. side_effect=[
  1024. (bts(self.fake_libs["distro"]), bts("")),
  1025. (bts(self.fake_libs["yaml"]), bts("")),
  1026. (bts(self.fake_libs["tornado"]), bts("")),
  1027. (bts(self.fake_libs["msgpack"]), bts("")),
  1028. (bts(""), bts("")),
  1029. (bts(""), bts("")),
  1030. (bts(""), bts("")),
  1031. (bts(""), bts("")),
  1032. (bts(""), bts("")),
  1033. (bts(""), bts("")),
  1034. (bts(""), bts("")),
  1035. ],
  1036. ),
  1037. )
  1038. patch_os = patch("os.path.exists", return_value=True)
  1039. ext_conf = copy.deepcopy(self.ext_conf)
  1040. ext_conf["test"]["auto_detect"] = True
  1041. for lib in self.fake_libs.values():
  1042. os.makedirs(lib)
  1043. with salt.utils.files.fopen(os.path.join(lib, "__init__.py"), "w+") as fp_:
  1044. fp_.write("test")
  1045. exp_files = self.exp_files.copy()
  1046. exp_files.extend(
  1047. [
  1048. os.path.join("yaml", "__init__.py"),
  1049. os.path.join("tornado", "__init__.py"),
  1050. os.path.join("msgpack", "__init__.py"),
  1051. ]
  1052. )
  1053. patch_which = patch("salt.utils.path.which", return_value=True)
  1054. with patch_os, patch_proc, patch_which:
  1055. thin._pack_alternative(ext_conf, self.digest, self.tar)
  1056. calls = self.tar.mock_calls
  1057. for _file in exp_files:
  1058. assert [x for x in calls if "{}".format(_file) in x.args]
  1059. def test_pack_alternatives(self):
  1060. """
  1061. test thin._pack_alternatives
  1062. """
  1063. with patch("salt.utils.thin.get_ext_tops", MagicMock(return_value=self.tops)):
  1064. thin._pack_alternative(self.ext_conf, self.digest, self.tar)
  1065. calls = self.tar.mock_calls
  1066. for _file in self.exp_files:
  1067. assert [x for x in calls if "{}".format(_file) in x.args]
  1068. assert [
  1069. x
  1070. for x in calls
  1071. if os.path.join("test", "pyall", _file) in x.kwargs["arcname"]
  1072. ]
  1073. def test_pack_alternatives_not_normalized(self):
  1074. """
  1075. test thin._pack_alternatives when the path
  1076. is not normalized
  1077. """
  1078. tops = copy.deepcopy(self.tops)
  1079. tops["test"]["dependencies"] = [self.jinja_fp + "/"]
  1080. with patch("salt.utils.thin.get_ext_tops", MagicMock(return_value=tops)):
  1081. thin._pack_alternative(self.ext_conf, self.digest, self.tar)
  1082. calls = self.tar.mock_calls
  1083. for _file in self.exp_files:
  1084. assert [x for x in calls if "{}".format(_file) in x.args]
  1085. assert [
  1086. x
  1087. for x in calls
  1088. if os.path.join("test", "pyall", _file) in x.kwargs["arcname"]
  1089. ]
  1090. def test_pack_alternatives_path_doesnot_exist(self):
  1091. """
  1092. test thin._pack_alternatives when the path
  1093. doesnt exist. Check error log message
  1094. and expect that because the directory
  1095. does not exist jinja2 does not get
  1096. added to the tar
  1097. """
  1098. bad_path = os.path.join(tempfile.gettempdir(), "doesnotexisthere")
  1099. tops = copy.deepcopy(self.tops)
  1100. tops["test"]["dependencies"] = [bad_path]
  1101. with patch("salt.utils.thin.get_ext_tops", MagicMock(return_value=tops)):
  1102. with TstSuiteLoggingHandler() as log_handler:
  1103. thin._pack_alternative(self.ext_conf, self.digest, self.tar)
  1104. msg = "ERROR:File path {} does not exist. Unable to add to salt-ssh thin".format(
  1105. bad_path
  1106. )
  1107. assert msg in log_handler.messages
  1108. calls = self.tar.mock_calls
  1109. for _file in self.exp_files:
  1110. arg = [x for x in calls if "{}".format(_file) in x.args]
  1111. kwargs = [
  1112. x
  1113. for x in calls
  1114. if os.path.join("test", "pyall", _file) in x.kwargs["arcname"]
  1115. ]
  1116. if "jinja2" in _file:
  1117. assert not arg
  1118. assert not kwargs
  1119. else:
  1120. assert arg
  1121. assert kwargs
  1122. def test_pack_alternatives_auto_detect(self):
  1123. """
  1124. test thin._pack_alternatives when auto_detect
  1125. is enabled
  1126. """
  1127. ext_conf = copy.deepcopy(self.ext_conf)
  1128. ext_conf["test"]["auto_detect"] = True
  1129. for lib in self.fake_libs.values():
  1130. os.makedirs(lib)
  1131. with salt.utils.files.fopen(os.path.join(lib, "__init__.py"), "w+") as fp_:
  1132. fp_.write("test")
  1133. patch_tops_py = patch(
  1134. "salt.utils.thin.get_tops_python", return_value=self.fake_libs
  1135. )
  1136. exp_files = self.exp_files.copy()
  1137. exp_files.extend(
  1138. [
  1139. os.path.join("yaml", "__init__.py"),
  1140. os.path.join("tornado", "__init__.py"),
  1141. os.path.join("msgpack", "__init__.py"),
  1142. ]
  1143. )
  1144. with patch_tops_py:
  1145. thin._pack_alternative(ext_conf, self.digest, self.tar)
  1146. calls = self.tar.mock_calls
  1147. for _file in exp_files:
  1148. assert [x for x in calls if "{}".format(_file) in x.args]
  1149. def test_pack_alternatives_empty_dependencies(self):
  1150. """
  1151. test _pack_alternatives when dependencies is not
  1152. set in the config.
  1153. """
  1154. ext_conf = copy.deepcopy(self.ext_conf)
  1155. ext_conf["test"]["auto_detect"] = True
  1156. ext_conf["test"].pop("dependencies")
  1157. for lib in self.fake_libs.values():
  1158. os.makedirs(lib)
  1159. with salt.utils.files.fopen(os.path.join(lib, "__init__.py"), "w+") as fp_:
  1160. fp_.write("test")
  1161. patch_tops_py = patch(
  1162. "salt.utils.thin.get_tops_python", return_value=self.fake_libs
  1163. )
  1164. exp_files = self.exp_files.copy()
  1165. exp_files.extend(
  1166. [
  1167. os.path.join("yaml", "__init__.py"),
  1168. os.path.join("tornado", "__init__.py"),
  1169. os.path.join("msgpack", "__init__.py"),
  1170. ]
  1171. )
  1172. with patch_tops_py:
  1173. thin._pack_alternative(ext_conf, self.digest, self.tar)
  1174. calls = self.tar.mock_calls
  1175. for _file in exp_files:
  1176. assert [x for x in calls if "{}".format(_file) in x.args]
  1177. @skipIf(
  1178. salt.utils.platform.is_windows(), "salt-ssh does not deploy to/from windows"
  1179. )
  1180. def test_thin_dir(self):
  1181. """
  1182. Test the thin dir to make sure salt-call can run
  1183. Run salt call via a python in a new virtual environment to ensure
  1184. salt-call has all dependencies needed.
  1185. """
  1186. # This was previously an integration test and is now here, as a unit test.
  1187. # Should actually be a functional test
  1188. with VirtualEnv() as venv:
  1189. salt.utils.thin.gen_thin(venv.venv_dir)
  1190. thin_dir = os.path.join(venv.venv_dir, "thin")
  1191. thin_archive = os.path.join(thin_dir, "thin.tgz")
  1192. tar = tarfile.open(thin_archive)
  1193. tar.extractall(thin_dir)
  1194. tar.close()
  1195. ret = venv.run(
  1196. venv.venv_python,
  1197. os.path.join(thin_dir, "salt-call"),
  1198. "--version",
  1199. check=False,
  1200. )
  1201. assert ret.exitcode == 0, ret