1
0

test_virtualenv_mod.py 5.3 KB

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