test_logrotate.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.modules.logrotate as logrotate
  8. # Import Salt Libs
  9. from salt.exceptions import SaltInvocationError
  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. PARSE_CONF = {
  15. "include files": {"rsyslog": ["/var/log/syslog"]},
  16. "rotate": 1,
  17. "/var/log/wtmp": {"rotate": 1},
  18. }
  19. class LogrotateTestCase(TestCase, LoaderModuleMockMixin):
  20. """
  21. Test cases for salt.modules.logrotate
  22. """
  23. def setup_loader_modules(self):
  24. return {logrotate: {}}
  25. # 'show_conf' function tests: 1
  26. def test_show_conf(self):
  27. """
  28. Test if it show parsed configuration
  29. """
  30. with patch("salt.modules.logrotate._parse_conf", MagicMock(return_value=True)):
  31. self.assertTrue(logrotate.show_conf())
  32. # 'set_' function tests: 4
  33. def test_set(self):
  34. """
  35. Test if it set a new value for a specific configuration line
  36. """
  37. with patch(
  38. "salt.modules.logrotate._parse_conf", MagicMock(return_value=PARSE_CONF)
  39. ), patch.dict(
  40. logrotate.__salt__, {"file.replace": MagicMock(return_value=True)}
  41. ):
  42. self.assertTrue(logrotate.set_("rotate", "2"))
  43. def test_set_failed(self):
  44. """
  45. Test if it fails to set a new value for a specific configuration line
  46. """
  47. with patch(
  48. "salt.modules.logrotate._parse_conf", MagicMock(return_value=PARSE_CONF)
  49. ):
  50. kwargs = {"key": "/var/log/wtmp", "value": 2}
  51. self.assertRaises(SaltInvocationError, logrotate.set_, **kwargs)
  52. def test_set_setting(self):
  53. """
  54. Test if it set a new value for a specific configuration line
  55. """
  56. with patch.dict(
  57. logrotate.__salt__, {"file.replace": MagicMock(return_value=True)}
  58. ), patch(
  59. "salt.modules.logrotate._parse_conf", MagicMock(return_value=PARSE_CONF)
  60. ):
  61. self.assertTrue(logrotate.set_("/var/log/wtmp", "rotate", "2"))
  62. def test_set_setting_failed(self):
  63. """
  64. Test if it fails to set a new value for a specific configuration line
  65. """
  66. with patch(
  67. "salt.modules.logrotate._parse_conf", MagicMock(return_value=PARSE_CONF)
  68. ):
  69. kwargs = {"key": "rotate", "value": "/var/log/wtmp", "setting": "2"}
  70. self.assertRaises(SaltInvocationError, logrotate.set_, **kwargs)