test_win_snmp.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :synopsis: Unit Tests for Windows SNMP Module 'state.win_snmp'
  4. :platform: Windows
  5. :maturity: develop
  6. .. versionadded:: 2017.7.0
  7. '''
  8. # Import Python Libs
  9. from __future__ import absolute_import, unicode_literals, print_function
  10. # Import Salt Libs
  11. import salt.states.win_snmp as win_snmp
  12. from salt.ext import six
  13. # Import Salt Testing Libs
  14. from tests.support.mixins import LoaderModuleMockMixin
  15. from tests.support.unit import TestCase
  16. from tests.support.mock import (
  17. MagicMock,
  18. patch,
  19. )
  20. class WinSnmpTestCase(TestCase, LoaderModuleMockMixin):
  21. '''
  22. Test cases for salt.modules.win_snmp
  23. '''
  24. def setup_loader_modules(self):
  25. return {win_snmp: {}}
  26. def test_agent_settings(self):
  27. '''
  28. Test - Manage the SNMP sysContact, sysLocation, and sysServices settings.
  29. '''
  30. kwargs = {'name': 'agent-settings', 'contact': 'TestContact',
  31. 'location': 'TestLocation', 'services': ['Internet']}
  32. ret = {
  33. 'name': kwargs['name'],
  34. 'changes': {},
  35. 'comment': 'Agent settings already contain the provided values.',
  36. 'result': True
  37. }
  38. # Using this instead of dictionary comprehension in order to make pylint happy.
  39. get_ret = dict((key, value) for (key, value) in six.iteritems(kwargs) if key != 'name')
  40. mock_value_get = MagicMock(return_value=get_ret)
  41. mock_value_set = MagicMock(return_value=True)
  42. with patch.dict(win_snmp.__salt__, {'win_snmp.get_agent_settings': mock_value_get,
  43. 'win_snmp.set_agent_settings': mock_value_set}):
  44. with patch.dict(win_snmp.__opts__, {'test': False}):
  45. self.assertEqual(win_snmp.agent_settings(**kwargs), ret)
  46. def test_auth_traps_enabled(self):
  47. '''
  48. Test - Manage the sending of authentication traps.
  49. '''
  50. kwargs = {'name': 'auth-traps', 'status': True}
  51. ret = {
  52. 'name': kwargs['name'],
  53. 'changes': {
  54. 'old': False,
  55. 'new': True
  56. },
  57. 'comment': 'Set EnableAuthenticationTraps to contain the provided value.',
  58. 'result': True
  59. }
  60. mock_value_get = MagicMock(return_value=False)
  61. mock_value_set = MagicMock(return_value=True)
  62. with patch.dict(win_snmp.__salt__, {'win_snmp.get_auth_traps_enabled': mock_value_get,
  63. 'win_snmp.set_auth_traps_enabled': mock_value_set}):
  64. with patch.dict(win_snmp.__opts__, {'test': False}):
  65. self.assertEqual(win_snmp.auth_traps_enabled(**kwargs), ret)
  66. with patch.dict(win_snmp.__opts__, {'test': True}):
  67. ret['comment'] = 'EnableAuthenticationTraps will be changed.'
  68. ret['result'] = None
  69. self.assertEqual(win_snmp.auth_traps_enabled(**kwargs), ret)
  70. def test_community_names(self):
  71. '''
  72. Test - Manage the SNMP accepted community names and their permissions.
  73. '''
  74. kwargs = {'name': 'community-names', 'communities': {'TestCommunity': 'Read Create'}}
  75. ret = {
  76. 'name': kwargs['name'],
  77. 'changes': {},
  78. 'comment': 'Communities already contain the provided values.',
  79. 'result': True
  80. }
  81. mock_value_get = MagicMock(return_value=kwargs['communities'])
  82. mock_value_set = MagicMock(return_value=True)
  83. with patch.dict(win_snmp.__salt__, {'win_snmp.get_community_names': mock_value_get,
  84. 'win_snmp.set_community_names': mock_value_set}):
  85. with patch.dict(win_snmp.__opts__, {'test': False}):
  86. self.assertEqual(win_snmp.community_names(**kwargs), ret)