test_cmd.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 os.path
  8. # Import Salt Testing Libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.unit import TestCase
  11. from tests.support.mock import (
  12. MagicMock,
  13. patch)
  14. from salt.exceptions import CommandExecutionError
  15. # Import Salt Libs
  16. import salt.states.cmd as cmd
  17. class CmdTestCase(TestCase, LoaderModuleMockMixin):
  18. '''
  19. Test cases for salt.states.cmd
  20. '''
  21. def setup_loader_modules(self):
  22. return {cmd: {'__env__': 'base'}}
  23. # 'mod_run_check' function tests: 1
  24. def test_mod_run_check(self):
  25. '''
  26. Test to execute the onlyif and unless logic.
  27. '''
  28. cmd_kwargs = {}
  29. creates = '/tmp'
  30. mock = MagicMock(return_value=1)
  31. with patch.dict(cmd.__salt__, {'cmd.retcode': mock}):
  32. with patch.dict(cmd.__opts__, {'test': True}):
  33. ret = {'comment': 'onlyif condition is false', 'result': True,
  34. 'skip_watch': True}
  35. self.assertDictEqual(cmd.mod_run_check(cmd_kwargs, '', '', creates), ret)
  36. self.assertDictEqual(cmd.mod_run_check(cmd_kwargs, {}, '', creates), ret)
  37. mock = MagicMock(return_value=1)
  38. with patch.dict(cmd.__salt__, {'cmd.retcode': mock}):
  39. with patch.dict(cmd.__opts__, {'test': True}):
  40. ret = {'comment': 'onlyif condition is false: ', 'result': True,
  41. 'skip_watch': True}
  42. self.assertDictEqual(cmd.mod_run_check(cmd_kwargs, [''], '', creates), ret)
  43. mock = MagicMock(return_value=0)
  44. with patch.dict(cmd.__salt__, {'cmd.retcode': mock}):
  45. ret = {'comment': 'unless condition is true', 'result': True,
  46. 'skip_watch': True}
  47. self.assertDictEqual(cmd.mod_run_check(cmd_kwargs, None, '', creates), ret)
  48. self.assertDictEqual(cmd.mod_run_check(cmd_kwargs, None, [''], creates), ret)
  49. self.assertDictEqual(cmd.mod_run_check(cmd_kwargs, None, True, creates), ret)
  50. with patch.object(os.path, 'exists',
  51. MagicMock(sid_effect=[True, True, False])):
  52. ret = {'comment': '/tmp exists', 'result': True}
  53. self.assertDictEqual(cmd.mod_run_check(cmd_kwargs, None, None, creates), ret)
  54. ret = {'comment': 'All files in creates exist', 'result': True}
  55. self.assertDictEqual(cmd.mod_run_check(cmd_kwargs, None, None, [creates]), ret)
  56. self.assertTrue(cmd.mod_run_check(cmd_kwargs, None, None, {}))
  57. # 'wait' function tests: 1
  58. def test_wait(self):
  59. '''
  60. Test to run the given command only if the watch statement calls it.
  61. '''
  62. name = 'cmd.script'
  63. ret = {'name': name,
  64. 'result': True,
  65. 'changes': {},
  66. 'comment': ''}
  67. self.assertDictEqual(cmd.wait(name), ret)
  68. # 'wait_script' function tests: 1
  69. def test_wait_script(self):
  70. '''
  71. Test to download a script from a remote source and execute it
  72. only if a watch statement calls it.
  73. '''
  74. name = 'cmd.script'
  75. ret = {'name': name,
  76. 'result': True,
  77. 'changes': {},
  78. 'comment': ''}
  79. self.assertDictEqual(cmd.wait_script(name), ret)
  80. # 'run' function tests: 1
  81. def test_run(self):
  82. '''
  83. Test to run a command if certain circumstances are met.
  84. '''
  85. name = 'cmd.script'
  86. ret = {'name': name,
  87. 'result': False,
  88. 'changes': {},
  89. 'comment': ''}
  90. with patch.dict(cmd.__opts__, {'test': False}):
  91. comt = ("Invalidly-formatted 'env' parameter. See documentation.")
  92. ret.update({'comment': comt})
  93. self.assertDictEqual(cmd.run(name, env='salt'), ret)
  94. with patch.dict(cmd.__grains__, {'shell': 'shell'}):
  95. with patch.dict(cmd.__opts__, {'test': False}):
  96. mock = MagicMock(side_effect=[CommandExecutionError,
  97. {'retcode': 1}])
  98. with patch.dict(cmd.__salt__, {'cmd.run_all': mock}):
  99. ret.update({'comment': '', 'result': False})
  100. self.assertDictEqual(cmd.run(name), ret)
  101. ret.update({'comment': 'Command "cmd.script" run',
  102. 'result': False, 'changes': {'retcode': 1}})
  103. self.assertDictEqual(cmd.run(name), ret)
  104. with patch.dict(cmd.__opts__, {'test': True}):
  105. comt = ('Command "cmd.script" would have been executed')
  106. ret.update({'comment': comt, 'result': None, 'changes': {}})
  107. self.assertDictEqual(cmd.run(name), ret)
  108. mock = MagicMock(return_value=1)
  109. with patch.dict(cmd.__salt__, {'cmd.retcode': mock}):
  110. with patch.dict(cmd.__opts__, {'test': False}):
  111. comt = ('onlyif condition is false')
  112. ret.update({'comment': comt, 'result': True,
  113. 'skip_watch': True})
  114. self.assertDictEqual(cmd.run(name, onlyif=''), ret)
  115. def test_run_root(self):
  116. '''
  117. Test to run a command with a different root
  118. '''
  119. name = 'cmd.script'
  120. ret = {'name': name,
  121. 'result': False,
  122. 'changes': {},
  123. 'comment': ''}
  124. with patch.dict(cmd.__grains__, {'shell': 'shell'}):
  125. with patch.dict(cmd.__opts__, {'test': False}):
  126. mock = MagicMock(side_effect=[CommandExecutionError,
  127. {'retcode': 1}])
  128. with patch.dict(cmd.__salt__, {'cmd.run_chroot': mock}):
  129. ret.update({'comment': '', 'result': False})
  130. self.assertDictEqual(cmd.run(name, root='/mnt'), ret)
  131. ret.update({'comment': 'Command "cmd.script" run',
  132. 'result': False, 'changes': {'retcode': 1}})
  133. self.assertDictEqual(cmd.run(name, root='/mnt'), ret)
  134. # 'script' function tests: 1
  135. def test_script(self):
  136. '''
  137. Test to download a script and execute it with specified arguments.
  138. '''
  139. name = 'cmd.script'
  140. ret = {'name': name,
  141. 'result': False,
  142. 'changes': {},
  143. 'comment': ''}
  144. with patch.dict(cmd.__opts__, {'test': False}):
  145. comt = ("Invalidly-formatted 'env' parameter. See documentation.")
  146. ret.update({'comment': comt})
  147. self.assertDictEqual(cmd.script(name, env='salt'), ret)
  148. with patch.dict(cmd.__grains__, {'shell': 'shell'}):
  149. with patch.dict(cmd.__opts__, {'test': True}):
  150. comt = ("Command 'cmd.script' would have been executed")
  151. ret.update({'comment': comt, 'result': None, 'changes': {}})
  152. self.assertDictEqual(cmd.script(name), ret)
  153. with patch.dict(cmd.__opts__, {'test': False}):
  154. mock = MagicMock(side_effect=[CommandExecutionError,
  155. {'retcode': 1}])
  156. with patch.dict(cmd.__salt__, {'cmd.script': mock}):
  157. ret.update({'comment': '', 'result': False})
  158. self.assertDictEqual(cmd.script(name), ret)
  159. ret.update({'comment': "Command 'cmd.script' run",
  160. 'result': False, 'changes': {'retcode': 1}})
  161. self.assertDictEqual(cmd.script(name), ret)
  162. mock = MagicMock(return_value=1)
  163. with patch.dict(cmd.__salt__, {'cmd.retcode': mock}):
  164. with patch.dict(cmd.__opts__, {'test': False}):
  165. comt = ('onlyif condition is false')
  166. ret.update({'comment': comt, 'result': True,
  167. 'skip_watch': True, 'changes': {}})
  168. self.assertDictEqual(cmd.script(name, onlyif=''), ret)
  169. # 'call' function tests: 1
  170. def test_call(self):
  171. '''
  172. Test to invoke a pre-defined Python function with arguments
  173. specified in the state declaration.
  174. '''
  175. name = 'cmd.script'
  176. # func = 'myfunc'
  177. ret = {'name': name,
  178. 'result': False,
  179. 'changes': {},
  180. 'comment': ''}
  181. flag = None
  182. def func():
  183. '''
  184. Mock func method
  185. '''
  186. if flag:
  187. return {}
  188. else:
  189. return []
  190. with patch.dict(cmd.__grains__, {'shell': 'shell'}):
  191. flag = True
  192. self.assertDictEqual(cmd.call(name, func), ret)
  193. flag = False
  194. comt = ('onlyif condition is false')
  195. ret.update({'comment': '', 'result': False,
  196. 'changes': {'retval': []}})
  197. self.assertDictEqual(cmd.call(name, func), ret)
  198. mock = MagicMock(return_value=1)
  199. with patch.dict(cmd.__salt__, {'cmd.retcode': mock}):
  200. with patch.dict(cmd.__opts__, {'test': True}):
  201. comt = ('onlyif condition is false')
  202. ret.update({'comment': comt, 'skip_watch': True,
  203. 'result': True, 'changes': {}})
  204. self.assertDictEqual(cmd.call(name, func, onlyif=''), ret)
  205. # 'wait_call' function tests: 1
  206. def test_wait_call(self):
  207. '''
  208. Test to run wait_call.
  209. '''
  210. name = 'cmd.script'
  211. func = 'myfunc'
  212. ret = {'name': name,
  213. 'result': True,
  214. 'changes': {},
  215. 'comment': ''}
  216. self.assertDictEqual(cmd.wait_call(name, func), ret)
  217. # 'mod_watch' function tests: 1
  218. def test_mod_watch(self):
  219. '''
  220. Test to execute a cmd function based on a watch call
  221. '''
  222. name = 'cmd.script'
  223. ret = {'name': name,
  224. 'result': False,
  225. 'changes': {},
  226. 'comment': ''}
  227. def func():
  228. '''
  229. Mock func method
  230. '''
  231. return {}
  232. with patch.dict(cmd.__grains__, {'shell': 'shell'}):
  233. with patch.dict(cmd.__opts__, {'test': False}):
  234. mock = MagicMock(return_value={'retcode': 1})
  235. with patch.dict(cmd.__salt__, {'cmd.run_all': mock}):
  236. self.assertDictEqual(cmd.mod_watch(name, sfun='wait',
  237. stateful=True), ret)
  238. comt = ('Command "cmd.script" run')
  239. ret.update({'comment': comt, 'changes': {'retcode': 1}})
  240. self.assertDictEqual(cmd.mod_watch(name, sfun='wait',
  241. stateful=False), ret)
  242. with patch.dict(cmd.__salt__, {'cmd.script': mock}):
  243. ret.update({'comment': '', 'changes': {}})
  244. self.assertDictEqual(cmd.mod_watch(name, sfun='script',
  245. stateful=True), ret)
  246. comt = ("Command 'cmd.script' run")
  247. ret.update({'comment': comt, 'changes': {'retcode': 1}})
  248. self.assertDictEqual(cmd.mod_watch(name, sfun='script',
  249. stateful=False), ret)
  250. with patch.dict(cmd.__salt__, {'cmd.script': mock}):
  251. ret.update({'comment': '', 'changes': {}})
  252. self.assertDictEqual(cmd.mod_watch(name, sfun='call',
  253. func=func), ret)
  254. comt = ('cmd.call needs a named parameter func')
  255. ret.update({'comment': comt})
  256. self.assertDictEqual(cmd.mod_watch(name, sfun='call'), ret)
  257. comt = ('cmd.salt does not work with the watch requisite,'
  258. ' please use cmd.wait or cmd.wait_script')
  259. ret.update({'comment': comt})
  260. self.assertDictEqual(cmd.mod_watch(name, sfun='salt'), ret)