1
0

test_incron.py 4.3 KB

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