test_environ.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Rupesh Tare <rupesht@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.modules.environ as environ
  17. class EnvironTestCase(TestCase, LoaderModuleMockMixin):
  18. '''
  19. Test cases for salt.modules.environ
  20. '''
  21. def setup_loader_modules(self):
  22. return {environ: {}}
  23. def test_setval(self):
  24. '''
  25. Test for set a single salt process environment variable. Returns True
  26. on success.
  27. '''
  28. mock = MagicMock(return_value=None)
  29. with patch.dict(os.environ, {}):
  30. self.assertEqual(environ.setval('key', False, True), None)
  31. mock = MagicMock(side_effect=Exception())
  32. with patch.dict(os.environ, {}):
  33. self.assertFalse(environ.setval('key', False, True))
  34. mock_environ = {}
  35. with patch.dict(os.environ, mock_environ):
  36. self.assertEqual(environ.setval('key', False), '')
  37. with patch.dict(os.environ, mock_environ):
  38. self.assertFalse(environ.setval('key', True))
  39. def test_set_val_permanent(self):
  40. with patch.dict(os.environ, {}), \
  41. patch.dict(environ.__salt__, {'reg.set_value': MagicMock(),
  42. 'reg.delete_value': MagicMock()}), \
  43. patch('salt.utils.platform.is_windows', MagicMock(return_value=True)):
  44. environ.setval('key', 'Test', permanent=True)
  45. environ.__salt__['reg.set_value'].assert_called_with('HKCU', 'Environment', 'key', 'Test')
  46. environ.setval('key', False, false_unsets=True, permanent=True)
  47. environ.__salt__['reg.set_value'].asset_not_called()
  48. environ.__salt__['reg.delete_value'].assert_called_with('HKCU', 'Environment', 'key')
  49. key = r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
  50. environ.setval('key', 'Test', permanent='HKLM')
  51. environ.__salt__['reg.set_value'].assert_called_with('HKLM', key, 'key', 'Test')
  52. def test_setenv(self):
  53. '''
  54. Set multiple salt process environment variables from a dict.
  55. Returns a dict.
  56. '''
  57. mock_environ = {'KEY': 'value'}
  58. with patch.dict(os.environ, mock_environ):
  59. self.assertFalse(environ.setenv('environ'))
  60. with patch.dict(os.environ, mock_environ):
  61. self.assertFalse(environ.setenv({'A': True},
  62. False,
  63. True,
  64. False))
  65. with patch.dict(os.environ, mock_environ):
  66. mock_setval = MagicMock(return_value=None)
  67. with patch.object(environ, 'setval', mock_setval):
  68. self.assertEqual(environ.setenv({}, False, True, False)['KEY'],
  69. None)
  70. def test_get(self):
  71. '''
  72. Get a single salt process environment variable.
  73. '''
  74. self.assertFalse(environ.get(True))
  75. self.assertEqual(environ.get('key'), '')
  76. def test_has_value(self):
  77. '''
  78. Determine whether the key exists in the current salt process
  79. environment dictionary. Optionally compare the current value
  80. of the environment against the supplied value string.
  81. '''
  82. mock_environ = {}
  83. with patch.dict(os.environ, mock_environ):
  84. self.assertFalse(environ.has_value(True))
  85. os.environ['salty'] = 'yes'
  86. self.assertTrue(environ.has_value('salty', 'yes'))
  87. os.environ['too_salty'] = 'no'
  88. self.assertFalse(environ.has_value('too_salty', 'yes'))
  89. self.assertFalse(environ.has_value('key', 'value'))
  90. os.environ['key'] = 'value'
  91. self.assertTrue(environ.has_value('key'))
  92. def test_item(self):
  93. '''
  94. Get one or more salt process environment variables.
  95. Returns a dict.
  96. '''
  97. self.assertEqual(environ.item(None), {})
  98. def test_items(self):
  99. '''
  100. Return a dict of the entire environment set for the salt process
  101. '''
  102. self.assertNotEqual(list(environ.items()), [])