test_virtualenv_mod.py 3.7 KB

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