test_schedule.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.schedule as schedule
  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 ScheduleTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.schedule
  16. """
  17. def setup_loader_modules(self):
  18. return {schedule: {}}
  19. # 'present' function tests: 1
  20. def test_present(self):
  21. """
  22. Test to ensure a job is present in the schedule.
  23. """
  24. name = "job1"
  25. ret = {"name": name, "changes": {}, "result": False, "comment": ""}
  26. mock_dict = MagicMock(side_effect=[ret, []])
  27. mock_mod = MagicMock(return_value=ret)
  28. mock_lst = MagicMock(side_effect=[{name: {}}, {name: {}}, {}, {}])
  29. with patch.dict(
  30. schedule.__salt__,
  31. {
  32. "schedule.list": mock_lst,
  33. "schedule.build_schedule_item": mock_dict,
  34. "schedule.modify": mock_mod,
  35. "schedule.add": mock_mod,
  36. },
  37. ):
  38. self.assertDictEqual(schedule.present(name), ret)
  39. with patch.dict(schedule.__opts__, {"test": False}):
  40. self.assertDictEqual(schedule.present(name), ret)
  41. self.assertDictEqual(schedule.present(name), ret)
  42. with patch.dict(schedule.__opts__, {"test": True}):
  43. ret.update({"result": True})
  44. self.assertDictEqual(schedule.present(name), ret)
  45. # 'absent' function tests: 1
  46. def test_absent(self):
  47. """
  48. Test to ensure a job is absent from the schedule.
  49. """
  50. name = "job1"
  51. ret = {"name": name, "changes": {}, "result": False, "comment": ""}
  52. mock_mod = MagicMock(return_value=ret)
  53. mock_lst = MagicMock(side_effect=[{name: {}}, {}])
  54. with patch.dict(
  55. schedule.__salt__, {"schedule.list": mock_lst, "schedule.delete": mock_mod}
  56. ):
  57. with patch.dict(schedule.__opts__, {"test": False}):
  58. self.assertDictEqual(schedule.absent(name), ret)
  59. with patch.dict(schedule.__opts__, {"test": True}):
  60. comt = "Job job1 not present in schedule"
  61. ret.update({"comment": comt, "result": True})
  62. self.assertDictEqual(schedule.absent(name), ret)