1
0

test_virtualenv_mod.py 5.2 KB

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