test_sysctl.py 5.2 KB

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