test_virtualenv_mod.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Pedro Algarvio (pedro@algarvio.me)
  4. tests.integration.states.virtualenv
  5. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. """
  7. # Import Python libs
  8. from __future__ import absolute_import, print_function, unicode_literals
  9. import os
  10. import shutil
  11. # Import Salt libs
  12. import salt.utils.files
  13. import salt.utils.path
  14. import salt.utils.platform
  15. from salt.modules.virtualenv_mod import KNOWN_BINARY_NAMES
  16. # Import Salt Testing libs
  17. from tests.support.case import ModuleCase
  18. from tests.support.helpers import destructiveTest, skip_if_not_root
  19. from tests.support.mixins import SaltReturnAssertsMixin
  20. from tests.support.runtests import RUNTIME_VARS
  21. from tests.support.unit import skipIf
  22. @skipIf(
  23. salt.utils.path.which_bin(KNOWN_BINARY_NAMES) is None, "virtualenv not installed"
  24. )
  25. class VirtualenvTest(ModuleCase, SaltReturnAssertsMixin):
  26. @skipIf(salt.utils.platform.is_darwin(), "Test is flaky on macosx")
  27. @destructiveTest
  28. @skip_if_not_root
  29. @skipIf(True, "SLOWTEST skip")
  30. def test_issue_1959_virtualenv_runas(self):
  31. user = "issue-1959"
  32. self.assertSaltTrueReturn(self.run_state("user.present", name=user))
  33. uinfo = self.run_function("user.info", [user])
  34. if salt.utils.platform.is_darwin():
  35. # MacOS does not support createhome with user.present
  36. self.assertSaltTrueReturn(
  37. self.run_state(
  38. "file.directory",
  39. name=uinfo["home"],
  40. user=user,
  41. group=uinfo["groups"][0],
  42. dir_mode=755,
  43. )
  44. )
  45. venv_dir = os.path.join(RUNTIME_VARS.SYS_TMP_DIR, "issue-1959-virtualenv-runas")
  46. try:
  47. ret = self.run_function("state.sls", mods="issue-1959-virtualenv-runas")
  48. self.assertSaltTrueReturn(ret)
  49. # Lets check proper ownership
  50. statinfo = self.run_function("file.stats", [venv_dir])
  51. self.assertEqual(statinfo["user"], uinfo["name"])
  52. self.assertEqual(statinfo["uid"], uinfo["uid"])
  53. finally:
  54. if os.path.isdir(venv_dir):
  55. shutil.rmtree(venv_dir)
  56. self.assertSaltTrueReturn(
  57. self.run_state("user.absent", name=user, purge=True)
  58. )
  59. @skipIf(salt.utils.platform.is_darwin(), "Test is flaky on macosx")
  60. @skipIf(True, "SLOWTEST skip")
  61. def test_issue_2594_non_invalidated_cache(self):
  62. # Testing virtualenv directory
  63. venv_path = os.path.join(RUNTIME_VARS.TMP, "issue-2594-ve")
  64. if os.path.exists(venv_path):
  65. shutil.rmtree(venv_path)
  66. # Our virtualenv requirements file
  67. requirements_file_path = os.path.join(
  68. RUNTIME_VARS.TMP_STATE_TREE, "issue-2594-requirements.txt"
  69. )
  70. if os.path.exists(requirements_file_path):
  71. os.unlink(requirements_file_path)
  72. # Our state template
  73. template = [
  74. "{0}:".format(venv_path),
  75. " virtualenv.managed:",
  76. " - system_site_packages: False",
  77. " - clear: false",
  78. " - requirements: salt://issue-2594-requirements.txt",
  79. ]
  80. reqs = ["pep8==1.3.3", "zope.interface==4.7.1"]
  81. # Let's populate the requirements file, just pep-8 for now
  82. with salt.utils.files.fopen(requirements_file_path, "a") as fhw:
  83. fhw.write(reqs[0] + "\n")
  84. # Let's run our state!!!
  85. try:
  86. ret = self.run_function("state.template_str", ["\n".join(template)])
  87. self.assertSaltTrueReturn(ret)
  88. self.assertInSaltComment("Created new virtualenv", ret)
  89. self.assertSaltStateChangesEqual(ret, [reqs[0]], keys=("packages", "new"))
  90. except AssertionError:
  91. # Always clean up the tests temp files
  92. if os.path.exists(venv_path):
  93. shutil.rmtree(venv_path)
  94. if os.path.exists(requirements_file_path):
  95. os.unlink(requirements_file_path)
  96. raise
  97. # Let's make sure, it really got installed
  98. ret = self.run_function("pip.freeze", bin_env=venv_path)
  99. self.assertIn(reqs[0], ret)
  100. self.assertNotIn(reqs[1], ret)
  101. # Now let's update the requirements file, which is now cached.
  102. with salt.utils.files.fopen(requirements_file_path, "w") as fhw:
  103. fhw.write(reqs[1] + "\n")
  104. # Let's run our state!!!
  105. try:
  106. ret = self.run_function("state.template_str", ["\n".join(template)])
  107. self.assertSaltTrueReturn(ret)
  108. self.assertInSaltComment("virtualenv exists", ret)
  109. self.assertSaltStateChangesEqual(ret, [reqs[1]], keys=("packages", "new"))
  110. except AssertionError:
  111. # Always clean up the tests temp files
  112. if os.path.exists(venv_path):
  113. shutil.rmtree(venv_path)
  114. if os.path.exists(requirements_file_path):
  115. os.unlink(requirements_file_path)
  116. raise
  117. # Let's make sure, it really got installed
  118. ret = self.run_function("pip.freeze", bin_env=venv_path)
  119. self.assertIn(reqs[0], ret)
  120. self.assertIn(reqs[1], ret)
  121. # If we reached this point no assertion failed, so, cleanup!
  122. if os.path.exists(venv_path):
  123. shutil.rmtree(venv_path)
  124. if os.path.exists(requirements_file_path):
  125. os.unlink(requirements_file_path)