1
0

test_incron.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. )
  14. # Import Salt Libs
  15. import salt.modules.incron as incron
  16. class IncronTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. Test cases for salt.modules.incron
  19. '''
  20. def setup_loader_modules(self):
  21. return {incron: {}}
  22. # 'write_incron_file' function tests: 1
  23. def test_write_incron_file(self):
  24. '''
  25. Test if it writes the contents of a file to a user's crontab
  26. '''
  27. mock = MagicMock(return_value=0)
  28. with patch.dict(incron.__salt__, {'cmd.retcode': mock}), \
  29. patch('salt.modules.incron._get_incron_cmdstr',
  30. MagicMock(return_value='incrontab')):
  31. self.assertTrue(incron.write_incron_file('cybage',
  32. '/home/cybage/new_cron'))
  33. # 'write_cron_file_verbose' function tests: 1
  34. def test_write_cron_file_verbose(self):
  35. '''
  36. Test if it writes the contents of a file to a user's crontab and
  37. return error message on error
  38. '''
  39. mock = MagicMock(return_value=True)
  40. with patch.dict(incron.__salt__, {'cmd.run_all': mock}), \
  41. patch('salt.modules.incron._get_incron_cmdstr',
  42. MagicMock(return_value='incrontab')):
  43. self.assertTrue(incron.write_incron_file_verbose
  44. ('cybage', '/home/cybage/new_cron'))
  45. # 'raw_system_incron' function tests: 1
  46. def test_raw_system_incron(self):
  47. '''
  48. Test if it return the contents of the system wide incrontab
  49. '''
  50. with patch('salt.modules.incron._read_file',
  51. MagicMock(return_value='salt')):
  52. self.assertEqual(incron.raw_system_incron(), 'salt')
  53. # 'raw_incron' function tests: 1
  54. def test_raw_incron(self):
  55. '''
  56. Test if it return the contents of the user's incrontab
  57. '''
  58. mock = MagicMock(return_value='incrontab')
  59. with patch.dict(incron.__grains__, {'os_family': mock}):
  60. mock = MagicMock(return_value='salt')
  61. with patch.dict(incron.__salt__, {'cmd.run_stdout': mock}):
  62. self.assertEqual(incron.raw_incron('cybage'), 'salt')
  63. # 'list_tab' function tests: 1
  64. def test_list_tab(self):
  65. '''
  66. Test if it return the contents of the specified user's incrontab
  67. '''
  68. mock = MagicMock(return_value='incrontab')
  69. with patch.dict(incron.__grains__, {'os_family': mock}):
  70. mock = MagicMock(return_value='salt')
  71. with patch.dict(incron.__salt__, {'cmd.run_stdout': mock}):
  72. self.assertDictEqual(incron.list_tab('cybage'),
  73. {'pre': ['salt'], 'crons': []})
  74. # 'set_job' function tests: 1
  75. def test_set_job(self):
  76. '''
  77. Test if it sets a cron job up for a specified user.
  78. '''
  79. self.assertEqual(incron.set_job('cybage', '/home/cybage', 'TO_MODIFY',
  80. 'echo "$$ $@ $# $% $&"'),
  81. 'Invalid mask type: TO_MODIFY')
  82. val = {'pre': [], 'crons': [{'path': '/home/cybage',
  83. 'mask': 'IN_MODIFY',
  84. 'cmd': 'echo "SALT"'}]}
  85. with patch.object(incron, 'list_tab',
  86. MagicMock(return_value=val)):
  87. self.assertEqual(incron.set_job('cybage', '/home/cybage',
  88. 'IN_MODIFY',
  89. 'echo "SALT"'), 'present')
  90. with patch.object(incron, 'list_tab',
  91. MagicMock(return_value={'pre': ['salt'],
  92. 'crons': []})):
  93. mock = MagicMock(return_value='incrontab')
  94. with patch.dict(incron.__grains__, {'os_family': mock}):
  95. with patch.object(incron, '_write_incron_lines',
  96. MagicMock(return_value={'retcode': True,
  97. 'stderr': 'error'})):
  98. self.assertEqual(incron.set_job('cybage', '/home/cybage',
  99. 'IN_MODIFY',
  100. 'echo "SALT"'), 'error')
  101. with patch.object(incron, 'list_tab',
  102. MagicMock(return_value={'pre': ['salt'],
  103. 'crons': []})):
  104. mock = MagicMock(return_value='incrontab')
  105. with patch.dict(incron.__grains__, {'os_family': mock}):
  106. with patch.object(incron, '_write_incron_lines',
  107. MagicMock(return_value={'retcode': False,
  108. 'stderr': 'error'})):
  109. self.assertEqual(incron.set_job('cybage', '/home/cybage',
  110. 'IN_MODIFY',
  111. 'echo "SALT"'), 'new')
  112. val = {'pre': [], 'crons': [{'path': '/home/cybage',
  113. 'mask': 'IN_MODIFY,IN_DELETE',
  114. 'cmd': 'echo "SALT"'}]}
  115. with patch.object(incron, 'list_tab',
  116. MagicMock(return_value=val)):
  117. mock = MagicMock(return_value='incrontab')
  118. with patch.dict(incron.__grains__, {'os_family': mock}):
  119. with patch.object(incron, '_write_incron_lines',
  120. MagicMock(return_value={'retcode': False,
  121. 'stderr': 'error'})):
  122. self.assertEqual(incron.set_job('cybage', '/home/cybage',
  123. 'IN_DELETE',
  124. 'echo "SALT"'), 'updated')
  125. # 'rm_job' function tests: 1
  126. def test_rm_job(self):
  127. '''
  128. Test if it remove a cron job for a specified user. If any of the
  129. day/time params are specified, the job will only be removed if
  130. the specified params match.
  131. '''
  132. self.assertEqual(incron.rm_job('cybage', '/home/cybage', 'TO_MODIFY',
  133. 'echo "$$ $@ $# $% $&"'),
  134. 'Invalid mask type: TO_MODIFY')
  135. with patch.object(incron, 'list_tab',
  136. MagicMock(return_value={'pre': ['salt'],
  137. 'crons': []})):
  138. mock = MagicMock(return_value='incrontab')
  139. with patch.dict(incron.__grains__, {'os_family': mock}):
  140. with patch.object(incron, '_write_incron_lines',
  141. MagicMock(return_value={'retcode': True,
  142. 'stderr': 'error'})):
  143. self.assertEqual(incron.rm_job('cybage', '/home/cybage',
  144. 'IN_MODIFY',
  145. 'echo "SALT"'), 'error')
  146. with patch.object(incron, 'list_tab',
  147. MagicMock(return_value={'pre': ['salt'],
  148. 'crons': []})):
  149. mock = MagicMock(return_value='incrontab')
  150. with patch.dict(incron.__grains__, {'os_family': mock}):
  151. with patch.object(incron, '_write_incron_lines',
  152. MagicMock(return_value={'retcode': False,
  153. 'stderr': 'error'})):
  154. self.assertEqual(incron.rm_job('cybage', '/home/cybage',
  155. 'IN_MODIFY',
  156. 'echo "SALT"'), 'absent')