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. # Import Python libs
  8. from __future__ import absolute_import, print_function, unicode_literals
  9. import os
  10. import shutil
  11. # Import Salt Testing libs
  12. from tests.support.case import ModuleCase
  13. from tests.support.unit import skipIf
  14. from tests.support.helpers import destructiveTest, skip_if_not_root
  15. from tests.support.mixins import SaltReturnAssertsMixin
  16. from tests.support.runtests import RUNTIME_VARS
  17. # Import Salt libs
  18. import salt.utils.files
  19. import salt.utils.path
  20. import salt.utils.platform
  21. from salt.modules.virtualenv_mod import KNOWN_BINARY_NAMES
  22. @skipIf(salt.utils.path.which_bin(KNOWN_BINARY_NAMES) is None, 'virtualenv not installed')
  23. class VirtualenvTest(ModuleCase, SaltReturnAssertsMixin):
  24. @skipIf(salt.utils.platform.is_darwin(), 'Test is flaky on macosx')
  25. @destructiveTest
  26. @skip_if_not_root
  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(self.run_state('file.directory', name=uinfo['home'], user=user, group=uinfo['groups'][0], dir_mode=755))
  34. venv_dir = os.path.join(
  35. RUNTIME_VARS.SYS_TMP_DIR, 'issue-1959-virtualenv-runas'
  36. )
  37. try:
  38. ret = self.run_function(
  39. 'state.sls', mods='issue-1959-virtualenv-runas'
  40. )
  41. self.assertSaltTrueReturn(ret)
  42. # Lets check proper ownership
  43. statinfo = self.run_function('file.stats', [venv_dir])
  44. self.assertEqual(statinfo['user'], uinfo['name'])
  45. self.assertEqual(statinfo['uid'], uinfo['uid'])
  46. finally:
  47. if os.path.isdir(venv_dir):
  48. shutil.rmtree(venv_dir)
  49. self.assertSaltTrueReturn(self.run_state('user.absent', name=user, purge=True))
  50. @skipIf(salt.utils.platform.is_darwin(), 'Test is flaky on macosx')
  51. def test_issue_2594_non_invalidated_cache(self):
  52. # Testing virtualenv directory
  53. venv_path = os.path.join(RUNTIME_VARS.TMP, 'issue-2594-ve')
  54. if os.path.exists(venv_path):
  55. shutil.rmtree(venv_path)
  56. # Our virtualenv requirements file
  57. requirements_file_path = os.path.join(
  58. RUNTIME_VARS.TMP_STATE_TREE, 'issue-2594-requirements.txt'
  59. )
  60. if os.path.exists(requirements_file_path):
  61. os.unlink(requirements_file_path)
  62. # Our state template
  63. template = [
  64. '{0}:'.format(venv_path),
  65. ' virtualenv.managed:',
  66. ' - system_site_packages: False',
  67. ' - clear: false',
  68. ' - requirements: salt://issue-2594-requirements.txt',
  69. ]
  70. # Let's populate the requirements file, just pep-8 for now
  71. with salt.utils.files.fopen(requirements_file_path, 'a') as fhw:
  72. fhw.write('pep8==1.3.3\n')
  73. # Let's run our state!!!
  74. try:
  75. ret = self.run_function(
  76. 'state.template_str', ['\n'.join(template)]
  77. )
  78. self.assertSaltTrueReturn(ret)
  79. self.assertInSaltComment('Created new virtualenv', ret)
  80. self.assertSaltStateChangesEqual(
  81. ret, ['pep8==1.3.3'], keys=('packages', 'new')
  82. )
  83. except AssertionError:
  84. # Always clean up the tests temp files
  85. if os.path.exists(venv_path):
  86. shutil.rmtree(venv_path)
  87. if os.path.exists(requirements_file_path):
  88. os.unlink(requirements_file_path)
  89. raise
  90. # Let's make sure, it really got installed
  91. ret = self.run_function('pip.freeze', bin_env=venv_path)
  92. self.assertIn('pep8==1.3.3', ret)
  93. self.assertNotIn('zope.interface==4.0.1', ret)
  94. # Now let's update the requirements file, which is now cached.
  95. with salt.utils.files.fopen(requirements_file_path, 'w') as fhw:
  96. fhw.write('zope.interface==4.0.1\n')
  97. # Let's run our state!!!
  98. try:
  99. ret = self.run_function(
  100. 'state.template_str', ['\n'.join(template)]
  101. )
  102. self.assertSaltTrueReturn(ret)
  103. self.assertInSaltComment('virtualenv exists', ret)
  104. self.assertSaltStateChangesEqual(
  105. ret, ['zope.interface==4.0.1'], keys=('packages', 'new')
  106. )
  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('pep8==1.3.3', ret)
  117. self.assertIn('zope.interface==4.0.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)