test_incron.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.incron as incron
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.mock import MagicMock, patch
  12. from tests.support.unit import TestCase
  13. class IncronTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.incron
  16. """
  17. def setup_loader_modules(self):
  18. return {incron: {}}
  19. # 'present' function tests: 1
  20. def test_present(self):
  21. """
  22. Test to verifies that the specified incron job is present
  23. for the specified user.
  24. """
  25. name = "salt"
  26. path = "/home/user"
  27. mask = "IN_MODIFY"
  28. cmd = 'echo "$$ $@"'
  29. ret = {"name": name, "result": None, "comment": "", "changes": {}}
  30. comt4 = "Incron {0} for user root failed to commit with error \nabsent".format(
  31. name
  32. )
  33. mock_dict = MagicMock(
  34. return_value={"crons": [{"path": path, "cmd": cmd, "mask": mask}]}
  35. )
  36. mock = MagicMock(side_effect=["present", "new", "updated", "absent"])
  37. with patch.dict(
  38. incron.__salt__, {"incron.list_tab": mock_dict, "incron.set_job": mock}
  39. ):
  40. with patch.dict(incron.__opts__, {"test": True}):
  41. comt = "Incron {0} is set to be added".format(name)
  42. ret.update({"comment": comt})
  43. self.assertDictEqual(incron.present(name, path, mask, cmd), ret)
  44. with patch.dict(incron.__opts__, {"test": False}):
  45. comt = "Incron {0} already present".format(name)
  46. ret.update({"comment": comt, "result": True})
  47. self.assertDictEqual(incron.present(name, path, mask, cmd), ret)
  48. comt = "Incron {0} added to root's incrontab".format(name)
  49. ret.update({"comment": comt, "changes": {"root": "salt"}})
  50. self.assertDictEqual(incron.present(name, path, mask, cmd), ret)
  51. comt = "Incron {0} updated".format(name)
  52. ret.update({"comment": comt})
  53. self.assertDictEqual(incron.present(name, path, mask, cmd), ret)
  54. ret.update({"comment": comt4, "result": False, "changes": {}})
  55. self.assertDictEqual(incron.present(name, path, mask, cmd), ret)
  56. # 'absent' function tests: 1
  57. def test_absent(self):
  58. """
  59. Test to verifies that the specified incron job is absent
  60. for the specified user.
  61. """
  62. name = "salt"
  63. path = "/home/user"
  64. mask = "IN_MODIFY"
  65. cmd = 'echo "$$ $@"'
  66. ret = {"name": name, "result": True, "comment": "", "changes": {}}
  67. comt4 = "Incron {0} for user root failed to commit with error new".format(name)
  68. mock_dict = MagicMock(
  69. return_value={"crons": [{"path": path, "cmd": cmd, "mask": mask}]}
  70. )
  71. mock = MagicMock(side_effect=["absent", "removed", "new"])
  72. with patch.dict(
  73. incron.__salt__, {"incron.list_tab": mock_dict, "incron.rm_job": mock}
  74. ):
  75. with patch.dict(incron.__opts__, {"test": True}):
  76. comt = "Incron {0} is absent".format(name)
  77. ret.update({"comment": comt})
  78. self.assertDictEqual(incron.absent(name, path, mask, cmd), ret)
  79. with patch.dict(incron.__opts__, {"test": False}):
  80. comt = "Incron {0} already absent".format(name)
  81. ret.update({"comment": comt, "result": True})
  82. self.assertDictEqual(incron.absent(name, path, mask, cmd), ret)
  83. comt = "Incron {0} removed from root's crontab".format(name)
  84. ret.update({"comment": comt, "changes": {"root": "salt"}})
  85. self.assertDictEqual(incron.absent(name, path, mask, cmd), ret)
  86. ret.update({"comment": comt4, "result": False, "changes": {}})
  87. self.assertDictEqual(incron.absent(name, path, mask, cmd), ret)