test_virtualenv_mod.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Rahul Handay <rahulha@saltstack.com>
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import os
  8. # Import Salt Testing Libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.unit import TestCase
  11. from tests.support.mock import (
  12. MagicMock,
  13. patch,
  14. )
  15. # Import Salt Libs
  16. import salt.states.virtualenv_mod as virtualenv_mod
  17. class VirtualenvModTestCase(TestCase, LoaderModuleMockMixin):
  18. '''
  19. Validate the virtualenv_mod state
  20. '''
  21. def setup_loader_modules(self):
  22. return {virtualenv_mod: {'__env__': 'base'}}
  23. def test_managed(self):
  24. '''
  25. Test to create a virtualenv and optionally manage it with pip
  26. '''
  27. ret = {'name': 'salt',
  28. 'changes': {},
  29. 'result': False,
  30. 'comment': ''}
  31. ret.update({'comment': 'Virtualenv was not detected on this system'})
  32. self.assertDictEqual(virtualenv_mod.managed('salt'), ret)
  33. mock1 = MagicMock(return_value='True')
  34. mock = MagicMock(return_value=False)
  35. mock2 = MagicMock(return_value='1.1')
  36. with patch.dict(virtualenv_mod.__salt__, {"virtualenv.create": True,
  37. "cp.is_cached": mock,
  38. "cp.cache_file": mock,
  39. "cp.hash_file": mock,
  40. "pip.freeze": mock1,
  41. "cmd.run_stderr": mock1,
  42. "pip.version": mock2}):
  43. mock = MagicMock(side_effect=[True, True, True, False, True, True])
  44. with patch.object(os.path, 'exists', mock):
  45. ret.update({'comment': "pip requirements file"
  46. " 'salt://a' not found"})
  47. self.assertDictEqual(virtualenv_mod.managed('salt', None,
  48. 'salt://a'), ret)
  49. with patch.dict(virtualenv_mod.__opts__, {"test": True}):
  50. ret.update({'changes': {'cleared_packages': 'True',
  51. 'old': 'True'},
  52. 'comment': 'Virtualenv salt is set to'
  53. ' be cleared', 'result': None})
  54. self.assertDictEqual(virtualenv_mod.managed('salt',
  55. clear=1), ret)
  56. ret.update({'comment': 'Virtualenv salt is already'
  57. ' created', 'changes': {},
  58. 'result': True})
  59. self.assertDictEqual(virtualenv_mod.managed('salt'), ret)
  60. ret.update({'comment': 'Virtualenv salt is set to'
  61. ' be created', 'result': None})
  62. self.assertDictEqual(virtualenv_mod.managed('salt'), ret)
  63. with patch.dict(virtualenv_mod.__opts__, {"test": False}):
  64. ret.update({'comment': "The 'use_wheel' option is"
  65. " only supported in pip between 1.4 and 9.0.3."
  66. " The version of pip detected was 1.1.",
  67. 'result': False})
  68. self.assertDictEqual(virtualenv_mod.managed('salt',
  69. use_wheel=1),
  70. ret)
  71. ret.update({'comment': 'virtualenv exists',
  72. 'result': True})
  73. self.assertDictEqual(virtualenv_mod.managed('salt'), ret)