test_virtualenv_mod.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. def test_issue_2594_non_invalidated_cache(self):
  51. # Testing virtualenv directory
  52. venv_path = os.path.join(RUNTIME_VARS.TMP, 'issue-2594-ve')
  53. if os.path.exists(venv_path):
  54. shutil.rmtree(venv_path)
  55. # Our virtualenv requirements file
  56. requirements_file_path = os.path.join(
  57. RUNTIME_VARS.TMP_STATE_TREE, 'issue-2594-requirements.txt'
  58. )
  59. if os.path.exists(requirements_file_path):
  60. os.unlink(requirements_file_path)
  61. # Our state template
  62. template = [
  63. '{0}:'.format(venv_path),
  64. ' virtualenv.managed:',
  65. ' - system_site_packages: False',
  66. ' - clear: false',
  67. ' - requirements: salt://issue-2594-requirements.txt',
  68. ]
  69. # Let's populate the requirements file, just pep-8 for now
  70. with salt.utils.files.fopen(requirements_file_path, 'a') as fhw:
  71. fhw.write('pep8==1.3.3\n')
  72. # Let's run our state!!!
  73. try:
  74. ret = self.run_function(
  75. 'state.template_str', ['\n'.join(template)]
  76. )
  77. self.assertSaltTrueReturn(ret)
  78. self.assertInSaltComment('Created new virtualenv', ret)
  79. self.assertSaltStateChangesEqual(
  80. ret, ['pep8==1.3.3'], keys=('packages', 'new')
  81. )
  82. except AssertionError:
  83. # Always clean up the tests temp files
  84. if os.path.exists(venv_path):
  85. shutil.rmtree(venv_path)
  86. if os.path.exists(requirements_file_path):
  87. os.unlink(requirements_file_path)
  88. raise
  89. # Let's make sure, it really got installed
  90. ret = self.run_function('pip.freeze', bin_env=venv_path)
  91. self.assertIn('pep8==1.3.3', ret)
  92. self.assertNotIn('zope.interface==4.0.1', ret)
  93. # Now let's update the requirements file, which is now cached.
  94. with salt.utils.files.fopen(requirements_file_path, 'w') as fhw:
  95. fhw.write('zope.interface==4.0.1\n')
  96. # Let's run our state!!!
  97. try:
  98. ret = self.run_function(
  99. 'state.template_str', ['\n'.join(template)]
  100. )
  101. self.assertSaltTrueReturn(ret)
  102. self.assertInSaltComment('virtualenv exists', ret)
  103. self.assertSaltStateChangesEqual(
  104. ret, ['zope.interface==4.0.1'], keys=('packages', 'new')
  105. )
  106. except AssertionError:
  107. # Always clean up the tests temp files
  108. if os.path.exists(venv_path):
  109. shutil.rmtree(venv_path)
  110. if os.path.exists(requirements_file_path):
  111. os.unlink(requirements_file_path)
  112. raise
  113. # Let's make sure, it really got installed
  114. ret = self.run_function('pip.freeze', bin_env=venv_path)
  115. self.assertIn('pep8==1.3.3', ret)
  116. self.assertIn('zope.interface==4.0.1', ret)
  117. # If we reached this point no assertion failed, so, cleanup!
  118. if os.path.exists(venv_path):
  119. shutil.rmtree(venv_path)
  120. if os.path.exists(requirements_file_path):
  121. os.unlink(requirements_file_path)