test_win_snmp.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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, print_function, unicode_literals
  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.mock import MagicMock, patch
  16. from tests.support.unit import TestCase
  17. class WinSnmpTestCase(TestCase, LoaderModuleMockMixin):
  18. """
  19. Test cases for salt.modules.win_snmp
  20. """
  21. def setup_loader_modules(self):
  22. return {win_snmp: {}}
  23. def test_agent_settings(self):
  24. """
  25. Test - Manage the SNMP sysContact, sysLocation, and sysServices settings.
  26. """
  27. kwargs = {
  28. "name": "agent-settings",
  29. "contact": "TestContact",
  30. "location": "TestLocation",
  31. "services": ["Internet"],
  32. }
  33. ret = {
  34. "name": kwargs["name"],
  35. "changes": {},
  36. "comment": "Agent settings already contain the provided values.",
  37. "result": True,
  38. }
  39. # Using this instead of dictionary comprehension in order to make pylint happy.
  40. get_ret = dict(
  41. (key, value) for (key, value) in six.iteritems(kwargs) if key != "name"
  42. )
  43. mock_value_get = MagicMock(return_value=get_ret)
  44. mock_value_set = MagicMock(return_value=True)
  45. with patch.dict(
  46. win_snmp.__salt__,
  47. {
  48. "win_snmp.get_agent_settings": mock_value_get,
  49. "win_snmp.set_agent_settings": mock_value_set,
  50. },
  51. ):
  52. with patch.dict(win_snmp.__opts__, {"test": False}):
  53. self.assertEqual(win_snmp.agent_settings(**kwargs), ret)
  54. def test_auth_traps_enabled(self):
  55. """
  56. Test - Manage the sending of authentication traps.
  57. """
  58. kwargs = {"name": "auth-traps", "status": True}
  59. ret = {
  60. "name": kwargs["name"],
  61. "changes": {"old": False, "new": True},
  62. "comment": "Set EnableAuthenticationTraps to contain the provided value.",
  63. "result": True,
  64. }
  65. mock_value_get = MagicMock(return_value=False)
  66. mock_value_set = MagicMock(return_value=True)
  67. with patch.dict(
  68. win_snmp.__salt__,
  69. {
  70. "win_snmp.get_auth_traps_enabled": mock_value_get,
  71. "win_snmp.set_auth_traps_enabled": mock_value_set,
  72. },
  73. ):
  74. with patch.dict(win_snmp.__opts__, {"test": False}):
  75. self.assertEqual(win_snmp.auth_traps_enabled(**kwargs), ret)
  76. with patch.dict(win_snmp.__opts__, {"test": True}):
  77. ret["comment"] = "EnableAuthenticationTraps will be changed."
  78. ret["result"] = None
  79. self.assertEqual(win_snmp.auth_traps_enabled(**kwargs), ret)
  80. def test_community_names(self):
  81. """
  82. Test - Manage the SNMP accepted community names and their permissions.
  83. """
  84. kwargs = {
  85. "name": "community-names",
  86. "communities": {"TestCommunity": "Read Create"},
  87. }
  88. ret = {
  89. "name": kwargs["name"],
  90. "changes": {},
  91. "comment": "Communities already contain the provided values.",
  92. "result": True,
  93. }
  94. mock_value_get = MagicMock(return_value=kwargs["communities"])
  95. mock_value_set = MagicMock(return_value=True)
  96. with patch.dict(
  97. win_snmp.__salt__,
  98. {
  99. "win_snmp.get_community_names": mock_value_get,
  100. "win_snmp.set_community_names": mock_value_set,
  101. },
  102. ):
  103. with patch.dict(win_snmp.__opts__, {"test": False}):
  104. self.assertEqual(win_snmp.community_names(**kwargs), ret)