test_environ.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import os
  5. import salt.modules.environ as envmodule
  6. import salt.modules.reg
  7. # Import salt libs
  8. import salt.states.environ as envstate
  9. import salt.utils.platform
  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, skipIf
  14. class TestEnvironState(TestCase, LoaderModuleMockMixin):
  15. def setup_loader_modules(self):
  16. loader_globals = {
  17. "__env__": "base",
  18. "__opts__": {"test": False},
  19. "__salt__": {
  20. "environ.setenv": envmodule.setenv,
  21. "reg.read_value": salt.modules.reg.read_value,
  22. },
  23. }
  24. return {envstate: loader_globals, envmodule: loader_globals}
  25. def setUp(self):
  26. patcher = patch.dict(os.environ, {"INITIAL": "initial"}, clear=True)
  27. patcher.start()
  28. def reset_environ(patcher):
  29. patcher.stop()
  30. self.addCleanup(reset_environ, patcher)
  31. def test_setenv(self):
  32. """
  33. test that a subsequent calls of setenv changes nothing
  34. """
  35. ret = envstate.setenv("test", "value")
  36. self.assertEqual(ret["changes"], {"test": "value"})
  37. ret = envstate.setenv("test", "other")
  38. self.assertEqual(ret["changes"], {"test": "other"})
  39. # once again with the same value
  40. ret = envstate.setenv("test", "other")
  41. self.assertEqual(ret["changes"], {})
  42. @skipIf(not salt.utils.platform.is_windows(), "Windows only")
  43. def test_setenv_permanent(self):
  44. """
  45. test that we can set perminent environment variables (requires pywin32)
  46. """
  47. with patch.dict(
  48. envmodule.__salt__,
  49. {"reg.set_value": MagicMock(), "reg.delete_value": MagicMock()},
  50. ):
  51. ret = envstate.setenv("test", "value", permanent=True)
  52. self.assertEqual(ret["changes"], {"test": "value"})
  53. envmodule.__salt__["reg.set_value"].assert_called_with(
  54. "HKCU", "Environment", "test", "value"
  55. )
  56. ret = envstate.setenv("test", False, false_unsets=True, permanent=True)
  57. self.assertEqual(ret["changes"], {"test": None})
  58. envmodule.__salt__["reg.delete_value"].assert_called_with(
  59. "HKCU", "Environment", "test"
  60. )
  61. def test_setenv_dict(self):
  62. """
  63. test that setenv can be invoked with dict
  64. """
  65. ret = envstate.setenv("notimportant", {"test": "value"})
  66. self.assertEqual(ret["changes"], {"test": "value"})
  67. def test_setenv_int(self):
  68. """
  69. test that setenv can not be invoked with int
  70. (actually it's anything other than strings and dict)
  71. """
  72. ret = envstate.setenv("test", 1)
  73. self.assertEqual(ret["result"], False)
  74. def test_setenv_unset(self):
  75. """
  76. test that ``false_unsets`` option removes variable from environment
  77. """
  78. ret = envstate.setenv("test", "value")
  79. self.assertEqual(ret["changes"], {"test": "value"})
  80. ret = envstate.setenv("notimportant", {"test": False}, false_unsets=True)
  81. self.assertEqual(ret["changes"], {"test": None})
  82. self.assertEqual(envstate.os.environ, {"INITIAL": "initial"})
  83. def test_setenv_clearall(self):
  84. """
  85. test that ``clear_all`` option sets other values to ''
  86. """
  87. ret = envstate.setenv("test", "value", clear_all=True)
  88. self.assertEqual(ret["changes"], {"test": "value", "INITIAL": ""})
  89. if salt.utils.platform.is_windows():
  90. self.assertEqual(envstate.os.environ, {"TEST": "value", "INITIAL": ""})
  91. else:
  92. self.assertEqual(envstate.os.environ, {"test": "value", "INITIAL": ""})
  93. def test_setenv_clearall_with_unset(self):
  94. """
  95. test that ``clear_all`` option combined with ``false_unsets``
  96. unsets other values from environment
  97. """
  98. ret = envstate.setenv("test", "value", false_unsets=True, clear_all=True)
  99. self.assertEqual(ret["changes"], {"test": "value", "INITIAL": None})
  100. if salt.utils.platform.is_windows():
  101. self.assertEqual(envstate.os.environ, {"TEST": "value"})
  102. else:
  103. self.assertEqual(envstate.os.environ, {"test": "value"})
  104. def test_setenv_unset_multi(self):
  105. """
  106. test basically same things that above tests but with multiple values passed
  107. """
  108. ret = envstate.setenv("notimportant", {"foo": "bar"})
  109. self.assertEqual(ret["changes"], {"foo": "bar"})
  110. with patch.dict(envstate.__salt__, {"reg.read_value": MagicMock()}):
  111. ret = envstate.setenv(
  112. "notimportant", {"test": False, "foo": "baz"}, false_unsets=True
  113. )
  114. self.assertEqual(ret["changes"], {"test": None, "foo": "baz"})
  115. if salt.utils.platform.is_windows():
  116. self.assertEqual(envstate.os.environ, {"INITIAL": "initial", "FOO": "baz"})
  117. else:
  118. self.assertEqual(envstate.os.environ, {"INITIAL": "initial", "foo": "baz"})
  119. with patch.dict(envstate.__salt__, {"reg.read_value": MagicMock()}):
  120. ret = envstate.setenv("notimportant", {"test": False, "foo": "bax"})
  121. self.assertEqual(ret["changes"], {"test": "", "foo": "bax"})
  122. if salt.utils.platform.is_windows():
  123. self.assertEqual(
  124. envstate.os.environ, {"INITIAL": "initial", "FOO": "bax", "TEST": ""}
  125. )
  126. else:
  127. self.assertEqual(
  128. envstate.os.environ, {"INITIAL": "initial", "foo": "bax", "test": ""}
  129. )
  130. def test_setenv_test_mode(self):
  131. """
  132. test that imitating action returns good values
  133. """
  134. with patch.dict(envstate.__opts__, {"test": True}):
  135. ret = envstate.setenv("test", "value")
  136. self.assertEqual(ret["changes"], {"test": "value"})
  137. ret = envstate.setenv("INITIAL", "initial")
  138. self.assertEqual(ret["changes"], {})