test_sysctl.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@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. from salt.exceptions import CommandExecutionError
  15. # Import Salt Libs
  16. import salt.states.sysctl as sysctl
  17. class SysctlTestCase(TestCase, LoaderModuleMockMixin):
  18. '''
  19. Test cases for salt.states.sysctl
  20. '''
  21. def setup_loader_modules(self):
  22. return {sysctl: {}}
  23. # 'present' function tests: 1
  24. def test_present(self):
  25. '''
  26. Test to ensure that the named sysctl value is set
  27. in memory and persisted to the named configuration file.
  28. '''
  29. name = 'net.ipv4.conf.all.rp_filter'
  30. value = '1'
  31. config = '/etc/sysctl.conf'
  32. comment = ('Sysctl option {0} might be changed, we failed to check '
  33. 'config file at {1}. The file is either unreadable, or '
  34. 'missing.'.format(name, config))
  35. ret = {'name': name, 'result': None, 'changes': {}, 'comment': comment}
  36. comment_empty = ('Sysctl option {0} would be changed to {1}'
  37. ''.format(name, value))
  38. comment1 = ('Sysctl option {0} set to be changed to {1}'
  39. .format(name, value))
  40. comment2 = ('Sysctl value is currently set on the running system but '
  41. 'not in a config file. Sysctl option {0} set to be '
  42. 'changed to 2 in config file.'.format(name))
  43. comt3 = ('Sysctl value {0} is present in configuration file but is not '
  44. 'present in the running config. The value {0} is set to be '
  45. 'changed to {1}'.format(name, value))
  46. comt4 = ('Sysctl value {0} = {1} is already set'.format(name, value))
  47. comt5 = ('Sysctl option {0} would be changed to {1}'
  48. .format(name, value))
  49. comt6 = ('Failed to set {0} to {1}: '.format(name, value))
  50. comt7 = ('Sysctl value {0} = {1} is already set'.format(name, value))
  51. def mock_current(config_file=None):
  52. '''
  53. Mock return value for __salt__.
  54. '''
  55. if config_file is None:
  56. return {name: '2'}
  57. return ['']
  58. def mock_config(config_file=None):
  59. '''
  60. Mock return value for __salt__.
  61. '''
  62. if config_file is None:
  63. return {'salt': '2'}
  64. return [name]
  65. def mock_both(config_file=None):
  66. '''
  67. Mock return value for __salt__.
  68. '''
  69. if config_file is None:
  70. return {name: value}
  71. return [name]
  72. with patch.dict(sysctl.__opts__, {'test': True}):
  73. mock = MagicMock(return_value=None)
  74. with patch.dict(sysctl.__salt__, {'sysctl.show': mock}):
  75. self.assertDictEqual(sysctl.present(name, value), ret)
  76. mock = MagicMock(return_value=[])
  77. with patch.dict(sysctl.__salt__, {'sysctl.show': mock}):
  78. ret.update({'comment': comment_empty})
  79. self.assertDictEqual(sysctl.present(name, value), ret)
  80. with patch.dict(sysctl.__salt__, {'sysctl.show': mock_current}):
  81. ret.update({'comment': comment1})
  82. self.assertDictEqual(sysctl.present(name, value), ret)
  83. ret.update({'comment': comment2})
  84. self.assertDictEqual(sysctl.present(name, '2'), ret)
  85. with patch.dict(sysctl.__salt__, {'sysctl.show': mock_config}):
  86. ret.update({'comment': comt3})
  87. self.assertDictEqual(sysctl.present(name, value), ret)
  88. mock = MagicMock(return_value=value)
  89. with patch.dict(sysctl.__salt__, {'sysctl.show': mock_both,
  90. 'sysctl.get': mock}):
  91. ret.update({'comment': comt4, 'result': True})
  92. self.assertDictEqual(sysctl.present(name, value), ret)
  93. mock = MagicMock(return_value=[True])
  94. with patch.dict(sysctl.__salt__, {'sysctl.show': mock}):
  95. ret.update({'comment': comt5, 'result': None})
  96. self.assertDictEqual(sysctl.present(name, value), ret)
  97. with patch.dict(sysctl.__opts__, {'test': False}):
  98. mock = MagicMock(side_effect=CommandExecutionError)
  99. with patch.dict(sysctl.__salt__, {'sysctl.persist': mock}):
  100. ret.update({'comment': comt6, 'result': False})
  101. self.assertDictEqual(sysctl.present(name, value), ret)
  102. mock = MagicMock(return_value='Already set')
  103. with patch.dict(sysctl.__salt__, {'sysctl.persist': mock}):
  104. ret.update({'comment': comt7, 'result': True})
  105. self.assertDictEqual(sysctl.present(name, value), ret)