test_celery.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. from __future__ import absolute_import, unicode_literals
  2. import sys
  3. import pytest
  4. from case import Mock, patch
  5. from celery import __main__
  6. from celery.bin import celery as mod
  7. from celery.bin.base import Error
  8. from celery.bin.celery import (CeleryCommand, Command, determine_exit_status,
  9. help)
  10. from celery.bin.celery import main as mainfun
  11. from celery.bin.celery import multi, report
  12. from celery.five import WhateverIO
  13. from celery.platforms import EX_FAILURE, EX_OK, EX_USAGE
  14. class test__main__:
  15. def test_main(self):
  16. with patch('celery.__main__.maybe_patch_concurrency') as mpc:
  17. with patch('celery.bin.celery.main') as main:
  18. __main__.main()
  19. mpc.assert_called_with()
  20. main.assert_called_with()
  21. def test_main__multi(self):
  22. with patch('celery.__main__.maybe_patch_concurrency') as mpc:
  23. with patch('celery.bin.celery.main') as main:
  24. prev, sys.argv = sys.argv, ['foo', 'multi']
  25. try:
  26. __main__.main()
  27. mpc.assert_not_called()
  28. main.assert_called_with()
  29. finally:
  30. sys.argv = prev
  31. class test_Command:
  32. def test_Error_repr(self):
  33. x = Error('something happened')
  34. assert x.status is not None
  35. assert x.reason
  36. assert str(x)
  37. def setup(self):
  38. self.out = WhateverIO()
  39. self.err = WhateverIO()
  40. self.cmd = Command(self.app, stdout=self.out, stderr=self.err)
  41. def test_error(self):
  42. self.cmd.out = Mock()
  43. self.cmd.error('FOO')
  44. self.cmd.out.assert_called()
  45. def test_out(self):
  46. f = Mock()
  47. self.cmd.out('foo', f)
  48. def test_call(self):
  49. def ok_run():
  50. pass
  51. self.cmd.run = ok_run
  52. assert self.cmd() == EX_OK
  53. def error_run():
  54. raise Error('error', EX_FAILURE)
  55. self.cmd.run = error_run
  56. assert self.cmd() == EX_FAILURE
  57. def test_run_from_argv(self):
  58. with pytest.raises(NotImplementedError):
  59. self.cmd.run_from_argv('prog', ['foo', 'bar'])
  60. def test_pretty_list(self):
  61. assert self.cmd.pretty([])[1] == '- empty -'
  62. assert 'bar', self.cmd.pretty(['foo' in 'bar'][1])
  63. def test_pretty_dict(self, text='the quick brown fox'):
  64. assert 'OK' in str(self.cmd.pretty({'ok': text})[0])
  65. assert 'ERROR' in str(self.cmd.pretty({'error': text})[0])
  66. def test_pretty(self):
  67. assert 'OK' in str(self.cmd.pretty('the quick brown'))
  68. assert 'OK' in str(self.cmd.pretty(object()))
  69. assert 'OK' in str(self.cmd.pretty({'foo': 'bar'}))
  70. class test_report:
  71. def test_run(self):
  72. out = WhateverIO()
  73. r = report(app=self.app, stdout=out)
  74. assert r.run() == EX_OK
  75. assert out.getvalue()
  76. class test_help:
  77. def test_run(self):
  78. out = WhateverIO()
  79. h = help(app=self.app, stdout=out)
  80. h.parser = Mock()
  81. assert h.run() == EX_USAGE
  82. assert out.getvalue()
  83. assert h.usage('help')
  84. h.parser.print_help.assert_called_with()
  85. class test_CeleryCommand:
  86. def test_execute_from_commandline(self):
  87. x = CeleryCommand(app=self.app)
  88. x.handle_argv = Mock()
  89. x.handle_argv.return_value = 1
  90. with pytest.raises(SystemExit):
  91. x.execute_from_commandline()
  92. x.handle_argv.return_value = True
  93. with pytest.raises(SystemExit):
  94. x.execute_from_commandline()
  95. x.handle_argv.side_effect = KeyboardInterrupt()
  96. with pytest.raises(SystemExit):
  97. x.execute_from_commandline()
  98. x.respects_app_option = True
  99. with pytest.raises(SystemExit):
  100. x.execute_from_commandline(['celery', 'multi'])
  101. assert not x.respects_app_option
  102. x.respects_app_option = True
  103. with pytest.raises(SystemExit):
  104. x.execute_from_commandline(['manage.py', 'celery', 'multi'])
  105. assert not x.respects_app_option
  106. def test_with_pool_option(self):
  107. x = CeleryCommand(app=self.app)
  108. assert x.with_pool_option(['celery', 'events']) is None
  109. assert x.with_pool_option(['celery', 'worker'])
  110. assert x.with_pool_option(['manage.py', 'celery', 'worker'])
  111. def test_load_extensions_no_commands(self):
  112. with patch('celery.bin.celery.Extensions') as Ext:
  113. ext = Ext.return_value = Mock(name='Extension')
  114. ext.load.return_value = None
  115. x = CeleryCommand(app=self.app)
  116. x.load_extension_commands()
  117. def test_load_extensions_commands(self):
  118. with patch('celery.bin.celery.Extensions') as Ext:
  119. prev, mod.command_classes = list(mod.command_classes), Mock()
  120. try:
  121. ext = Ext.return_value = Mock(name='Extension')
  122. ext.load.return_value = ['foo', 'bar']
  123. x = CeleryCommand(app=self.app)
  124. x.load_extension_commands()
  125. mod.command_classes.append.assert_called_with(
  126. ('Extensions', ['foo', 'bar'], 'magenta'),
  127. )
  128. finally:
  129. mod.command_classes = prev
  130. def test_determine_exit_status(self):
  131. assert determine_exit_status('true') == EX_OK
  132. assert determine_exit_status('') == EX_FAILURE
  133. def test_relocate_args_from_start(self):
  134. x = CeleryCommand(app=self.app)
  135. assert x._relocate_args_from_start(None) == []
  136. relargs1 = x._relocate_args_from_start([
  137. '-l', 'debug', 'worker', '-c', '3', '--foo',
  138. ])
  139. assert relargs1 == ['worker', '-c', '3', '--foo', '-l', 'debug']
  140. relargs2 = x._relocate_args_from_start([
  141. '--pool=gevent', '-l', 'debug', 'worker', '--foo', '-c', '3',
  142. ])
  143. assert relargs2 == [
  144. 'worker', '--foo', '-c', '3',
  145. '--pool=gevent', '-l', 'debug',
  146. ]
  147. assert x._relocate_args_from_start(['foo', '--foo=1']) == [
  148. 'foo', '--foo=1',
  149. ]
  150. def test_register_command(self):
  151. prev, CeleryCommand.commands = dict(CeleryCommand.commands), {}
  152. try:
  153. fun = Mock(name='fun')
  154. CeleryCommand.register_command(fun, name='foo')
  155. assert CeleryCommand.commands['foo'] is fun
  156. finally:
  157. CeleryCommand.commands = prev
  158. def test_handle_argv(self):
  159. x = CeleryCommand(app=self.app)
  160. x.execute = Mock()
  161. x.handle_argv('celery', [])
  162. x.execute.assert_called_with('help', ['help'])
  163. x.handle_argv('celery', ['start', 'foo'])
  164. x.execute.assert_called_with('start', ['start', 'foo'])
  165. def test_execute(self):
  166. x = CeleryCommand(app=self.app)
  167. Help = x.commands['help'] = Mock()
  168. help = Help.return_value = Mock()
  169. x.execute('fooox', ['a'])
  170. help.run_from_argv.assert_called_with(x.prog_name, [], command='help')
  171. help.reset()
  172. x.execute('help', ['help'])
  173. help.run_from_argv.assert_called_with(x.prog_name, [], command='help')
  174. Dummy = x.commands['dummy'] = Mock()
  175. dummy = Dummy.return_value = Mock()
  176. exc = dummy.run_from_argv.side_effect = Error(
  177. 'foo', status='EX_FAILURE',
  178. )
  179. x.on_error = Mock(name='on_error')
  180. help.reset()
  181. x.execute('dummy', ['dummy'])
  182. x.on_error.assert_called_with(exc)
  183. dummy.run_from_argv.assert_called_with(
  184. x.prog_name, [], command='dummy',
  185. )
  186. help.run_from_argv.assert_called_with(
  187. x.prog_name, [], command='help',
  188. )
  189. exc = dummy.run_from_argv.side_effect = x.UsageError('foo')
  190. x.on_usage_error = Mock()
  191. x.execute('dummy', ['dummy'])
  192. x.on_usage_error.assert_called_with(exc)
  193. def test_on_usage_error(self):
  194. x = CeleryCommand(app=self.app)
  195. x.error = Mock()
  196. x.on_usage_error(x.UsageError('foo'), command=None)
  197. x.error.assert_called()
  198. x.on_usage_error(x.UsageError('foo'), command='dummy')
  199. def test_prepare_prog_name(self):
  200. x = CeleryCommand(app=self.app)
  201. main = Mock(name='__main__')
  202. main.__file__ = '/opt/foo.py'
  203. with patch.dict(sys.modules, __main__=main):
  204. assert x.prepare_prog_name('__main__.py') == '/opt/foo.py'
  205. assert x.prepare_prog_name('celery') == 'celery'
  206. class test_multi:
  207. def test_get_options(self):
  208. assert multi(app=self.app).get_options() is None
  209. def test_run_from_argv(self):
  210. with patch('celery.bin.multi.MultiTool') as MultiTool:
  211. m = MultiTool.return_value = Mock()
  212. multi(self.app).run_from_argv('celery', ['arg'], command='multi')
  213. m.execute_from_commandline.assert_called_with(['multi', 'arg'])
  214. class test_main:
  215. @patch('celery.bin.celery.CeleryCommand')
  216. def test_main(self, Command):
  217. cmd = Command.return_value = Mock()
  218. mainfun()
  219. cmd.execute_from_commandline.assert_called_with(None)
  220. @patch('celery.bin.celery.CeleryCommand')
  221. def test_main_KeyboardInterrupt(self, Command):
  222. cmd = Command.return_value = Mock()
  223. cmd.execute_from_commandline.side_effect = KeyboardInterrupt()
  224. mainfun()
  225. cmd.execute_from_commandline.assert_called_with(None)