test_sysrc.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Rahul Handay <rahulha@saltstack.com>
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch,
  13. )
  14. # Import Salt Libs
  15. import salt.states.sysrc as sysrc
  16. class SysrcTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. Validate the sysrc state
  19. '''
  20. def setup_loader_modules(self):
  21. return {sysrc: {}}
  22. def test_managed(self):
  23. '''
  24. Test to ensure a sysrc variable is set to a specific value.
  25. '''
  26. ret = {'name': 'salt',
  27. 'changes': {},
  28. 'result': True,
  29. 'comment': ''}
  30. mock = MagicMock(side_effect=[{'key1': {'salt': 'stack'}}, None, None])
  31. mock1 = MagicMock(return_value=True)
  32. with patch.dict(sysrc.__salt__, {"sysrc.get": mock,
  33. "sysrc.set": mock1}):
  34. ret.update({'comment': 'salt is already set to the desired'
  35. ' value.'})
  36. self.assertDictEqual(sysrc.managed('salt', 'stack'), ret)
  37. with patch.dict(sysrc.__opts__, {"test": True}):
  38. ret.update({'changes': {'new': 'salt = stack will be set.',
  39. 'old': None}, 'comment': 'The value'
  40. ' of "salt" will be changed!', 'result': None})
  41. self.assertDictEqual(sysrc.managed('salt', 'stack'), ret)
  42. with patch.dict(sysrc.__opts__, {"test": False}):
  43. ret.update({'changes': {'new': True, 'old': None},
  44. 'comment': 'The value of "salt" was changed!',
  45. 'result': True})
  46. self.assertDictEqual(sysrc.managed('salt', 'stack'), ret)
  47. def test_absent(self):
  48. '''
  49. Test to ensure a sysrc variable is absent.
  50. '''
  51. ret = {'name': 'salt',
  52. 'changes': {},
  53. 'result': True,
  54. 'comment': ''}
  55. mock = MagicMock(side_effect=[None, True, True])
  56. mock1 = MagicMock(return_value=True)
  57. with patch.dict(sysrc.__salt__, {"sysrc.get": mock,
  58. "sysrc.remove": mock1}):
  59. ret.update({'comment': '"salt" is already absent.'})
  60. self.assertDictEqual(sysrc.absent('salt'), ret)
  61. with patch.dict(sysrc.__opts__, {"test": True}):
  62. ret.update({'changes': {'new': '"salt" will be removed.',
  63. 'old': True},
  64. 'comment': '"salt" will be removed!',
  65. 'result': None})
  66. self.assertDictEqual(sysrc.absent('salt'), ret)
  67. with patch.dict(sysrc.__opts__, {"test": False}):
  68. ret.update({'changes': {'new': True, 'old': True},
  69. 'comment': '"salt" was removed!',
  70. 'result': True})
  71. self.assertDictEqual(sysrc.absent('salt'), ret)