1
0

test_pip_state.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Pedro Algarvio (pedro@algarvio.me)
  4. tests.integration.states.pip_state
  5. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. """
  7. from __future__ import absolute_import, print_function, unicode_literals
  8. import errno
  9. import glob
  10. import os
  11. import pprint
  12. import shutil
  13. import sys
  14. import pytest
  15. import salt.utils.files
  16. import salt.utils.path
  17. import salt.utils.platform
  18. import salt.utils.versions
  19. import salt.utils.win_dacl
  20. import salt.utils.win_functions
  21. import salt.utils.win_runas
  22. from salt.exceptions import CommandExecutionError
  23. from salt.ext import six
  24. from salt.modules.virtualenv_mod import KNOWN_BINARY_NAMES
  25. from tests.support.case import ModuleCase
  26. from tests.support.helpers import (
  27. destructiveTest,
  28. patched_environ,
  29. requires_system_grains,
  30. skip_if_not_root,
  31. with_system_user,
  32. with_tempdir,
  33. )
  34. from tests.support.mixins import SaltReturnAssertsMixin
  35. from tests.support.runtests import RUNTIME_VARS
  36. from tests.support.unit import skipIf
  37. try:
  38. import pwd
  39. HAS_PWD = True
  40. except ImportError:
  41. HAS_PWD = False
  42. class VirtualEnv(object):
  43. def __init__(self, test, venv_dir):
  44. self.venv_dir = venv_dir
  45. self.test = test
  46. self.test.addCleanup(shutil.rmtree, self.venv_dir, ignore_errors=True)
  47. def __enter__(self):
  48. ret = self.test._create_virtualenv(self.venv_dir)
  49. self.test.assertEqual(
  50. ret["retcode"],
  51. 0,
  52. msg="Expected 'retcode' key did not match. Full return dictionary:\n{}".format(
  53. pprint.pformat(ret)
  54. ),
  55. )
  56. def __exit__(self, *args):
  57. pass
  58. @skipIf(
  59. salt.utils.path.which_bin(KNOWN_BINARY_NAMES) is None, "virtualenv not installed"
  60. )
  61. @pytest.mark.windows_whitelisted
  62. class PipStateTest(ModuleCase, SaltReturnAssertsMixin):
  63. def _create_virtualenv(self, path, **kwargs):
  64. """
  65. The reason why the virtualenv creation is proxied by this function is mostly
  66. because under windows, we can't seem to properly create a virtualenv off of
  67. another virtualenv(we can on linux) and also because, we really don't want to
  68. test virtualenv creation off of another virtualenv, we want a virtualenv created
  69. from the original python.
  70. Also, one windows, we must also point to the virtualenv binary outside the existing
  71. virtualenv because it will fail otherwise
  72. """
  73. self.addCleanup(shutil.rmtree, path, ignore_errors=True)
  74. try:
  75. if salt.utils.platform.is_windows():
  76. python = os.path.join(sys.real_prefix, os.path.basename(sys.executable))
  77. else:
  78. python_binary_names = [
  79. "python{}.{}".format(*sys.version_info),
  80. "python{}".format(*sys.version_info),
  81. "python",
  82. ]
  83. for binary_name in python_binary_names:
  84. python = os.path.join(sys.real_prefix, "bin", binary_name)
  85. if os.path.exists(python):
  86. break
  87. else:
  88. self.fail(
  89. "Couldn't find a python binary name under '{}' matching: {}".format(
  90. os.path.join(sys.real_prefix, "bin"), python_binary_names
  91. )
  92. )
  93. # We're running off a virtualenv, and we don't want to create a virtualenv off of
  94. # a virtualenv, let's point to the actual python that created the virtualenv
  95. kwargs["python"] = python
  96. except AttributeError:
  97. # We're running off of the system python
  98. pass
  99. return self.run_function("virtualenv.create", [path], **kwargs)
  100. @skipIf(True, "SLOWTEST skip")
  101. def test_pip_installed_removed(self):
  102. """
  103. Tests installed and removed states
  104. """
  105. name = "pudb"
  106. if name in self.run_function("pip.list"):
  107. self.skipTest(
  108. "{0} is already installed, uninstall to run this test".format(name)
  109. )
  110. ret = self.run_state("pip.installed", name=name)
  111. self.assertSaltTrueReturn(ret)
  112. ret = self.run_state("pip.removed", name=name)
  113. self.assertSaltTrueReturn(ret)
  114. @skipIf(True, "SLOWTEST skip")
  115. def test_pip_installed_removed_venv(self):
  116. venv_dir = os.path.join(RUNTIME_VARS.TMP, "pip_installed_removed")
  117. with VirtualEnv(self, venv_dir):
  118. name = "pudb"
  119. ret = self.run_state("pip.installed", name=name, bin_env=venv_dir)
  120. self.assertSaltTrueReturn(ret)
  121. ret = self.run_state("pip.removed", name=name, bin_env=venv_dir)
  122. self.assertSaltTrueReturn(ret)
  123. @skipIf(True, "SLOWTEST skip")
  124. def test_pip_installed_errors(self):
  125. venv_dir = os.path.join(RUNTIME_VARS.TMP, "pip-installed-errors")
  126. self.addCleanup(shutil.rmtree, venv_dir, ignore_errors=True)
  127. # Since we don't have the virtualenv created, pip.installed will
  128. # throw an error.
  129. # Example error strings:
  130. # * "Error installing 'pep8': /tmp/pip-installed-errors: not found"
  131. # * "Error installing 'pep8': /bin/sh: 1: /tmp/pip-installed-errors: not found"
  132. # * "Error installing 'pep8': /bin/bash: /tmp/pip-installed-errors: No such file or directory"
  133. with patched_environ(SHELL="/bin/sh"):
  134. ret = self.run_function("state.sls", mods="pip-installed-errors")
  135. self.assertSaltFalseReturn(ret)
  136. self.assertSaltCommentRegexpMatches(ret, "Error installing 'pep8':")
  137. # We now create the missing virtualenv
  138. ret = self.run_function("virtualenv.create", [venv_dir])
  139. self.assertEqual(ret["retcode"], 0)
  140. # The state should not have any issues running now
  141. ret = self.run_function("state.sls", mods="pip-installed-errors")
  142. self.assertSaltTrueReturn(ret)
  143. @skipIf(six.PY3, "Issue is specific to carbon module, which is PY2-only")
  144. @skipIf(salt.utils.platform.is_windows(), "Carbon does not install in Windows")
  145. @requires_system_grains
  146. def test_pip_installed_weird_install(self, grains=None):
  147. # First, check to see if this is running on CentOS 5 or MacOS.
  148. # If so, skip this test.
  149. if grains["os"] in ("CentOS",) and grains["osrelease_info"][0] in (5,):
  150. self.skipTest("This test does not run reliably on CentOS 5")
  151. if grains["os"] in ("MacOS",):
  152. self.skipTest("This test does not run reliably on MacOS")
  153. ographite = "/opt/graphite"
  154. if os.path.isdir(ographite):
  155. self.skipTest(
  156. "You already have '{0}'. This test would overwrite this "
  157. "directory".format(ographite)
  158. )
  159. try:
  160. os.makedirs(ographite)
  161. except OSError as err:
  162. if err.errno == errno.EACCES:
  163. # Permission denied
  164. self.skipTest(
  165. "You don't have the required permissions to run this test"
  166. )
  167. finally:
  168. if os.path.isdir(ographite):
  169. shutil.rmtree(ographite, ignore_errors=True)
  170. venv_dir = os.path.join(RUNTIME_VARS.TMP, "pip-installed-weird-install")
  171. try:
  172. # We may be able to remove this, I had to add it because the custom
  173. # modules from the test suite weren't available in the jinja
  174. # context when running the call to state.sls that comes after.
  175. self.run_function("saltutil.sync_modules")
  176. # Since we don't have the virtualenv created, pip.installed will
  177. # throw an error.
  178. ret = self.run_function("state.sls", mods="pip-installed-weird-install")
  179. self.assertSaltTrueReturn(ret)
  180. # We cannot use assertInSaltComment here because we need to skip
  181. # some of the state return parts
  182. for key in six.iterkeys(ret):
  183. self.assertTrue(ret[key]["result"])
  184. if ret[key]["name"] != "carbon < 1.1":
  185. continue
  186. self.assertEqual(
  187. ret[key]["comment"],
  188. "There was no error installing package 'carbon < 1.1' "
  189. "although it does not show when calling 'pip.freeze'.",
  190. )
  191. break
  192. else:
  193. raise Exception("Expected state did not run")
  194. finally:
  195. if os.path.isdir(ographite):
  196. shutil.rmtree(ographite, ignore_errors=True)
  197. @skipIf(True, "SLOWTEST skip")
  198. def test_issue_2028_pip_installed_state(self):
  199. ret = self.run_function("state.sls", mods="issue-2028-pip-installed")
  200. venv_dir = os.path.join(RUNTIME_VARS.TMP, "issue-2028-pip-installed")
  201. self.addCleanup(shutil.rmtree, venv_dir, ignore_errors=True)
  202. pep8_bin = os.path.join(venv_dir, "bin", "pep8")
  203. if salt.utils.platform.is_windows():
  204. pep8_bin = os.path.join(venv_dir, "Scripts", "pep8.exe")
  205. self.assertSaltTrueReturn(ret)
  206. self.assertTrue(os.path.isfile(pep8_bin))
  207. @skipIf(True, "SLOWTEST skip")
  208. def test_issue_2087_missing_pip(self):
  209. venv_dir = os.path.join(RUNTIME_VARS.TMP, "issue-2087-missing-pip")
  210. # Let's create the testing virtualenv
  211. ret = self._create_virtualenv(venv_dir)
  212. self.assertEqual(
  213. ret["retcode"],
  214. 0,
  215. msg="Expected 'retcode' key did not match. Full return dictionary:\n{}".format(
  216. pprint.pformat(ret)
  217. ),
  218. )
  219. # Let's remove the pip binary
  220. pip_bin = os.path.join(venv_dir, "bin", "pip")
  221. site_dir = self.run_function(
  222. "virtualenv.get_distribution_path", [venv_dir, "pip"]
  223. )
  224. if salt.utils.platform.is_windows():
  225. pip_bin = os.path.join(venv_dir, "Scripts", "pip.exe")
  226. site_dir = os.path.join(venv_dir, "lib", "site-packages")
  227. if not os.path.isfile(pip_bin):
  228. self.skipTest("Failed to find the pip binary to the test virtualenv")
  229. os.remove(pip_bin)
  230. # Also remove the pip dir from site-packages
  231. # This is needed now that we're using python -m pip instead of the
  232. # pip binary directly. python -m pip will still work even if the
  233. # pip binary is missing
  234. shutil.rmtree(os.path.join(site_dir, "pip"))
  235. # Let's run the state which should fail because pip is missing
  236. ret = self.run_function("state.sls", mods="issue-2087-missing-pip")
  237. self.assertSaltFalseReturn(ret)
  238. self.assertInSaltComment(
  239. "Error installing 'pep8': Could not find a `pip` binary", ret
  240. )
  241. def test_issue_5940_multiple_pip_mirrors(self):
  242. """
  243. Test multiple pip mirrors. This test only works with pip < 7.0.0
  244. """
  245. ret = self.run_function("state.sls", mods="issue-5940-multiple-pip-mirrors")
  246. venv_dir = os.path.join(RUNTIME_VARS.TMP, "5940-multiple-pip-mirrors")
  247. self.addCleanup(shutil.rmtree, venv_dir, ignore_errors=True)
  248. try:
  249. self.assertSaltTrueReturn(ret)
  250. self.assertTrue(os.path.isfile(os.path.join(venv_dir, "bin", "pep8")))
  251. except (AssertionError, CommandExecutionError):
  252. pip_version = self.run_function("pip.version", [venv_dir])
  253. if salt.utils.versions.compare(ver1=pip_version, oper=">=", ver2="7.0.0"):
  254. self.skipTest(
  255. "the --mirrors arg has been deprecated and removed in pip==7.0.0"
  256. )
  257. @destructiveTest
  258. @skip_if_not_root
  259. @with_system_user(
  260. "issue-6912", on_existing="delete", delete=True, password="PassWord1!"
  261. )
  262. @with_tempdir()
  263. @skipIf(True, "SLOWTEST skip")
  264. def test_issue_6912_wrong_owner(self, temp_dir, username):
  265. # Setup virtual environment directory to be used throughout the test
  266. venv_dir = os.path.join(temp_dir, "6912-wrong-owner")
  267. # The virtual environment needs to be in a location that is accessible
  268. # by both the user running the test and the runas user
  269. if salt.utils.platform.is_windows():
  270. salt.utils.win_dacl.set_permissions(temp_dir, username, "full_control")
  271. else:
  272. uid = self.run_function("file.user_to_uid", [username])
  273. os.chown(temp_dir, uid, -1)
  274. # Create the virtual environment
  275. venv_create = self._create_virtualenv(
  276. venv_dir, user=username, password="PassWord1!"
  277. )
  278. if venv_create["retcode"] > 0:
  279. self.skipTest(
  280. "Failed to create testcase virtual environment: {0}"
  281. "".format(venv_create)
  282. )
  283. # pip install passing the package name in `name`
  284. ret = self.run_state(
  285. "pip.installed",
  286. name="pep8",
  287. user=username,
  288. bin_env=venv_dir,
  289. password="PassWord1!",
  290. )
  291. self.assertSaltTrueReturn(ret)
  292. if HAS_PWD:
  293. uid = pwd.getpwnam(username).pw_uid
  294. for globmatch in (
  295. os.path.join(venv_dir, "**", "pep8*"),
  296. os.path.join(venv_dir, "*", "**", "pep8*"),
  297. os.path.join(venv_dir, "*", "*", "**", "pep8*"),
  298. ):
  299. for path in glob.glob(globmatch):
  300. if HAS_PWD:
  301. self.assertEqual(uid, os.stat(path).st_uid)
  302. elif salt.utils.platform.is_windows():
  303. self.assertEqual(salt.utils.win_dacl.get_owner(path), username)
  304. @destructiveTest
  305. @skip_if_not_root
  306. @skipIf(salt.utils.platform.is_darwin(), "Test is flaky on macosx")
  307. @with_system_user(
  308. "issue-6912", on_existing="delete", delete=True, password="PassWord1!"
  309. )
  310. @with_tempdir()
  311. @skipIf(True, "SLOWTEST skip")
  312. def test_issue_6912_wrong_owner_requirements_file(self, temp_dir, username):
  313. # Setup virtual environment directory to be used throughout the test
  314. venv_dir = os.path.join(temp_dir, "6912-wrong-owner")
  315. # The virtual environment needs to be in a location that is accessible
  316. # by both the user running the test and the runas user
  317. if salt.utils.platform.is_windows():
  318. salt.utils.win_dacl.set_permissions(temp_dir, username, "full_control")
  319. else:
  320. uid = self.run_function("file.user_to_uid", [username])
  321. os.chown(temp_dir, uid, -1)
  322. # Create the virtual environment again as it should have been removed
  323. venv_create = self._create_virtualenv(
  324. venv_dir, user=username, password="PassWord1!"
  325. )
  326. if venv_create["retcode"] > 0:
  327. self.skipTest(
  328. "failed to create testcase virtual environment: {0}"
  329. "".format(venv_create)
  330. )
  331. # pip install using a requirements file
  332. req_filename = os.path.join(
  333. RUNTIME_VARS.TMP_STATE_TREE, "issue-6912-requirements.txt"
  334. )
  335. with salt.utils.files.fopen(req_filename, "wb") as reqf:
  336. reqf.write(b"pep8\n")
  337. ret = self.run_state(
  338. "pip.installed",
  339. name="",
  340. user=username,
  341. bin_env=venv_dir,
  342. requirements="salt://issue-6912-requirements.txt",
  343. password="PassWord1!",
  344. )
  345. self.assertSaltTrueReturn(ret)
  346. if HAS_PWD:
  347. uid = pwd.getpwnam(username).pw_uid
  348. for globmatch in (
  349. os.path.join(venv_dir, "**", "pep8*"),
  350. os.path.join(venv_dir, "*", "**", "pep8*"),
  351. os.path.join(venv_dir, "*", "*", "**", "pep8*"),
  352. ):
  353. for path in glob.glob(globmatch):
  354. if HAS_PWD:
  355. self.assertEqual(uid, os.stat(path).st_uid)
  356. elif salt.utils.platform.is_windows():
  357. self.assertEqual(salt.utils.win_dacl.get_owner(path), username)
  358. @skipIf(True, "SLOWTEST skip")
  359. def test_issue_6833_pip_upgrade_pip(self):
  360. # Create the testing virtualenv
  361. venv_dir = os.path.join(RUNTIME_VARS.TMP, "6833-pip-upgrade-pip")
  362. ret = self._create_virtualenv(venv_dir)
  363. self.assertEqual(
  364. ret["retcode"],
  365. 0,
  366. msg="Expected 'retcode' key did not match. Full return dictionary:\n{}".format(
  367. pprint.pformat(ret)
  368. ),
  369. )
  370. import salt.modules.virtualenv_mod
  371. msg = "New python executable"
  372. if salt.modules.virtualenv_mod.virtualenv_ver(venv_dir) >= (20, 0, 2):
  373. msg = "created virtual environment"
  374. self.assertIn(
  375. msg,
  376. ret["stdout"],
  377. msg="Expected STDOUT did not match. Full return dictionary:\n{}".format(
  378. pprint.pformat(ret)
  379. ),
  380. )
  381. # Let's install a fixed version pip over whatever pip was
  382. # previously installed
  383. ret = self.run_function(
  384. "pip.install", ["pip==8.0"], upgrade=True, bin_env=venv_dir
  385. )
  386. if not isinstance(ret, dict):
  387. self.fail(
  388. "The 'pip.install' command did not return the excepted dictionary. Output:\n{}".format(
  389. ret
  390. )
  391. )
  392. self.assertEqual(ret["retcode"], 0)
  393. self.assertIn("Successfully installed pip", ret["stdout"])
  394. # Let's make sure we have pip 8.0 installed
  395. self.assertEqual(
  396. self.run_function("pip.list", ["pip"], bin_env=venv_dir), {"pip": "8.0.0"}
  397. )
  398. # Now the actual pip upgrade pip test
  399. ret = self.run_state(
  400. "pip.installed", name="pip==8.0.1", upgrade=True, bin_env=venv_dir
  401. )
  402. if not isinstance(ret, dict):
  403. self.fail(
  404. "The 'pip.install' command did not return the excepted dictionary. Output:\n{}".format(
  405. ret
  406. )
  407. )
  408. self.assertSaltTrueReturn(ret)
  409. self.assertSaltStateChangesEqual(ret, {"pip==8.0.1": "Installed"})
  410. @skipIf(True, "SLOWTEST skip")
  411. def test_pip_installed_specific_env(self):
  412. # Create the testing virtualenv
  413. venv_dir = os.path.join(RUNTIME_VARS.TMP, "pip-installed-specific-env")
  414. # Let's write a requirements file
  415. requirements_file = os.path.join(
  416. RUNTIME_VARS.TMP_PRODENV_STATE_TREE, "prod-env-requirements.txt"
  417. )
  418. with salt.utils.files.fopen(requirements_file, "wb") as reqf:
  419. reqf.write(b"pep8\n")
  420. try:
  421. self._create_virtualenv(venv_dir)
  422. # The requirements file should not be found the base environment
  423. ret = self.run_state(
  424. "pip.installed",
  425. name="",
  426. bin_env=venv_dir,
  427. requirements="salt://prod-env-requirements.txt",
  428. )
  429. self.assertSaltFalseReturn(ret)
  430. self.assertInSaltComment(
  431. "'salt://prod-env-requirements.txt' not found", ret
  432. )
  433. # The requirements file must be found in the prod environment
  434. ret = self.run_state(
  435. "pip.installed",
  436. name="",
  437. bin_env=venv_dir,
  438. saltenv="prod",
  439. requirements="salt://prod-env-requirements.txt",
  440. )
  441. self.assertSaltTrueReturn(ret)
  442. self.assertInSaltComment(
  443. "Successfully processed requirements file "
  444. "salt://prod-env-requirements.txt",
  445. ret,
  446. )
  447. # We're using the base environment but we're passing the prod
  448. # environment as an url arg to salt://
  449. ret = self.run_state(
  450. "pip.installed",
  451. name="",
  452. bin_env=venv_dir,
  453. requirements="salt://prod-env-requirements.txt?saltenv=prod",
  454. )
  455. self.assertSaltTrueReturn(ret)
  456. self.assertInSaltComment("Requirements were already installed.", ret)
  457. finally:
  458. if os.path.isfile(requirements_file):
  459. os.unlink(requirements_file)
  460. @skipIf(
  461. salt.utils.platform.is_darwin() and six.PY2, "This test hangs on OS X on Py2"
  462. )
  463. @skipIf(True, "SLOWTEST skip")
  464. def test_22359_pip_installed_unless_does_not_trigger_warnings(self):
  465. # This test case should be moved to a format_call unit test specific to
  466. # the state internal keywords
  467. venv_dir = os.path.join(RUNTIME_VARS.TMP, "pip-installed-unless")
  468. venv_create = self._create_virtualenv(venv_dir)
  469. if venv_create["retcode"] > 0:
  470. self.skipTest(
  471. "Failed to create testcase virtual environment: {0}".format(venv_create)
  472. )
  473. false_cmd = RUNTIME_VARS.SHELL_FALSE_PATH
  474. if salt.utils.platform.is_windows():
  475. false_cmd = "exit 1 >nul"
  476. try:
  477. ret = self.run_state(
  478. "pip.installed",
  479. name="pep8",
  480. bin_env=venv_dir,
  481. unless=false_cmd,
  482. timeout=600,
  483. )
  484. self.assertSaltTrueReturn(ret)
  485. self.assertNotIn("warnings", next(six.itervalues(ret)))
  486. finally:
  487. if os.path.isdir(venv_dir):
  488. shutil.rmtree(venv_dir, ignore_errors=True)
  489. @skipIf(
  490. sys.version_info[:2] >= (3, 6),
  491. "Old version of virtualenv too old for python3.6",
  492. )
  493. @skipIf(salt.utils.platform.is_windows(), "Carbon does not install in Windows")
  494. @skipIf(True, "SLOWTEST skip")
  495. def test_46127_pip_env_vars(self):
  496. """
  497. Test that checks if env_vars passed to pip.installed are also passed
  498. to pip.freeze while checking for existing installations
  499. """
  500. # This issue is most easily checked while installing carbon
  501. # Much of the code here comes from the test_weird_install function above
  502. ographite = "/opt/graphite"
  503. if os.path.isdir(ographite):
  504. self.skipTest(
  505. "You already have '{0}'. This test would overwrite this "
  506. "directory".format(ographite)
  507. )
  508. try:
  509. os.makedirs(ographite)
  510. except OSError as err:
  511. if err.errno == errno.EACCES:
  512. # Permission denied
  513. self.skipTest(
  514. "You don't have the required permissions to run this test"
  515. )
  516. finally:
  517. if os.path.isdir(ographite):
  518. shutil.rmtree(ographite, ignore_errors=True)
  519. venv_dir = os.path.join(RUNTIME_VARS.TMP, "issue-46127-pip-env-vars")
  520. try:
  521. # We may be able to remove this, I had to add it because the custom
  522. # modules from the test suite weren't available in the jinja
  523. # context when running the call to state.sls that comes after.
  524. self.run_function("saltutil.sync_modules")
  525. # Since we don't have the virtualenv created, pip.installed will
  526. # throw an error.
  527. ret = self.run_function("state.sls", mods="issue-46127-pip-env-vars")
  528. self.assertSaltTrueReturn(ret)
  529. for key in six.iterkeys(ret):
  530. self.assertTrue(ret[key]["result"])
  531. if ret[key]["name"] != "carbon < 1.3":
  532. continue
  533. self.assertEqual(
  534. ret[key]["comment"], "All packages were successfully installed"
  535. )
  536. break
  537. else:
  538. raise Exception("Expected state did not run")
  539. # Run the state again. Now the already installed message should
  540. # appear
  541. ret = self.run_function("state.sls", mods="issue-46127-pip-env-vars")
  542. self.assertSaltTrueReturn(ret)
  543. # We cannot use assertInSaltComment here because we need to skip
  544. # some of the state return parts
  545. for key in six.iterkeys(ret):
  546. self.assertTrue(ret[key]["result"])
  547. # As we are re-running the formula, some states will not be run
  548. # and "name" may or may not be present, so we use .get() pattern
  549. if ret[key].get("name", "") != "carbon < 1.3":
  550. continue
  551. self.assertEqual(
  552. ret[key]["comment"], ("All packages were successfully installed")
  553. )
  554. break
  555. else:
  556. raise Exception("Expected state did not run")
  557. finally:
  558. if os.path.isdir(ographite):
  559. shutil.rmtree(ographite, ignore_errors=True)
  560. if os.path.isdir(venv_dir):
  561. shutil.rmtree(venv_dir)
  562. @pytest.mark.windows_whitelisted
  563. class PipStateInRequisiteTest(ModuleCase, SaltReturnAssertsMixin):
  564. @with_tempdir()
  565. @skipIf(True, "SLOWTEST skip")
  566. def test_issue_54755(self, tmpdir):
  567. """
  568. Verify github issue 54755 is resolved. This only fails when there is no
  569. pip module in the python environment. Since the test suite normally has
  570. a pip module this test will pass and is here for posterity. See also
  571. unit.states.test_pip_state.PipStateUtilsTest.test_pip_purge_method_with_pip
  572. and
  573. unit.states.test_pip_state.PipStateUtilsTest.test_pip_purge_method_without_pip
  574. Which also validate this issue and will pass/fail regardless of whether
  575. or not pip is installed.
  576. """
  577. file_path = os.path.join(tmpdir, "issue-54755")
  578. ret = self.run_function(
  579. "state.sls", mods="issue-54755", pillar={"file_path": file_path}
  580. )
  581. key = "file_|-issue-54755_|-{}_|-managed".format(file_path)
  582. assert key in ret
  583. assert ret[key]["result"] is True
  584. with salt.utils.files.fopen(file_path, "r") as fp:
  585. assert fp.read().strip() == "issue-54755"