test_multi.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. from __future__ import absolute_import, unicode_literals
  2. import pytest
  3. import signal
  4. import sys
  5. from case import Mock, patch
  6. from celery.bin.multi import main, MultiTool, __doc__ as doc
  7. from celery.five import WhateverIO
  8. class test_MultiTool:
  9. def setup(self):
  10. self.fh = WhateverIO()
  11. self.env = {}
  12. self.t = MultiTool(env=self.env, fh=self.fh)
  13. self.t.cluster_from_argv = Mock(name='cluster_from_argv')
  14. self.t._cluster_from_argv = Mock(name='cluster_from_argv')
  15. self.t.Cluster = Mock(name='Cluster')
  16. self.t.carp = Mock(name='.carp')
  17. self.t.usage = Mock(name='.usage')
  18. self.t.splash = Mock(name='.splash')
  19. self.t.say = Mock(name='.say')
  20. self.t.ok = Mock(name='.ok')
  21. self.cluster = self.t.Cluster.return_value
  22. def _cluster_from_argv(argv):
  23. p = self.t.OptionParser(argv)
  24. p.parse()
  25. return p, self.cluster
  26. self.t.cluster_from_argv.return_value = self.cluster
  27. self.t._cluster_from_argv.side_effect = _cluster_from_argv
  28. def test_findsig(self):
  29. self.assert_sig_argument(['a', 'b', 'c', '-1'], 1)
  30. self.assert_sig_argument(['--foo=1', '-9'], 9)
  31. self.assert_sig_argument(['-INT'], signal.SIGINT)
  32. self.assert_sig_argument([], signal.SIGTERM)
  33. self.assert_sig_argument(['-s'], signal.SIGTERM)
  34. self.assert_sig_argument(['-log'], signal.SIGTERM)
  35. def assert_sig_argument(self, args, expected):
  36. p = self.t.OptionParser(args)
  37. p.parse()
  38. assert self.t._find_sig_argument(p) == expected
  39. def test_execute_from_commandline(self):
  40. self.t.call_command = Mock(name='call_command')
  41. self.t.execute_from_commandline(
  42. 'multi start --verbose 10 --foo'.split(),
  43. cmd='X',
  44. )
  45. assert self.t.cmd == 'X'
  46. assert self.t.prog_name == 'multi'
  47. self.t.call_command.assert_called_with('start', ['10', '--foo'])
  48. def test_execute_from_commandline__arguments(self):
  49. assert self.t.execute_from_commandline('multi'.split())
  50. assert self.t.execute_from_commandline('multi -bar'.split())
  51. def test_call_command(self):
  52. cmd = self.t.commands['foo'] = Mock(name='foo')
  53. self.t.retcode = 303
  54. assert (self.t.call_command('foo', ['1', '2', '--foo=3']) is
  55. cmd.return_value)
  56. cmd.assert_called_with('1', '2', '--foo=3')
  57. def test_call_command__error(self):
  58. assert self.t.call_command('asdqwewqe', ['1', '2']) == 1
  59. self.t.carp.assert_called()
  60. def test_handle_reserved_options(self):
  61. assert self.t._handle_reserved_options(
  62. ['a', '-q', 'b', '--no-color', 'c']) == ['a', 'b', 'c']
  63. def test_start(self):
  64. self.cluster.start.return_value = [0, 0, 1, 0]
  65. assert self.t.start('10', '-A', 'proj')
  66. self.t.splash.assert_called_with()
  67. self.t.cluster_from_argv.assert_called_with(('10', '-A', 'proj'))
  68. self.cluster.start.assert_called_with()
  69. def test_start__exitcodes(self):
  70. self.cluster.start.return_value = [0, 0, 0]
  71. assert not self.t.start('foo', 'bar', 'baz')
  72. self.cluster.start.assert_called_with()
  73. self.cluster.start.return_value = [0, 1, 0]
  74. assert self.t.start('foo', 'bar', 'baz')
  75. def test_stop(self):
  76. self.t.stop('10', '-A', 'proj', retry=3)
  77. self.t.splash.assert_called_with()
  78. self.t._cluster_from_argv.assert_called_with(('10', '-A', 'proj'))
  79. self.cluster.stop.assert_called_with(retry=3, sig=signal.SIGTERM)
  80. def test_stopwait(self):
  81. self.t.stopwait('10', '-A', 'proj', retry=3)
  82. self.t.splash.assert_called_with()
  83. self.t._cluster_from_argv.assert_called_with(('10', '-A', 'proj'))
  84. self.cluster.stopwait.assert_called_with(retry=3, sig=signal.SIGTERM)
  85. def test_restart(self):
  86. self.cluster.restart.return_value = [0, 0, 1, 0]
  87. self.t.restart('10', '-A', 'proj')
  88. self.t.splash.assert_called_with()
  89. self.t._cluster_from_argv.assert_called_with(('10', '-A', 'proj'))
  90. self.cluster.restart.assert_called_with(sig=signal.SIGTERM)
  91. def test_names(self):
  92. self.t.cluster_from_argv.return_value = [Mock(), Mock()]
  93. self.t.cluster_from_argv.return_value[0].name = 'x'
  94. self.t.cluster_from_argv.return_value[1].name = 'y'
  95. self.t.names('10', '-A', 'proj')
  96. self.t.say.assert_called()
  97. def test_get(self):
  98. node = self.cluster.find.return_value = Mock(name='node')
  99. node.argv = ['A', 'B', 'C']
  100. assert (self.t.get('wanted', '10', '-A', 'proj') is
  101. self.t.ok.return_value)
  102. self.cluster.find.assert_called_with('wanted')
  103. self.t.cluster_from_argv.assert_called_with(('10', '-A', 'proj'))
  104. self.t.ok.assert_called_with(' '.join(node.argv))
  105. def test_get__KeyError(self):
  106. self.cluster.find.side_effect = KeyError()
  107. assert self.t.get('wanted', '10', '-A', 'proj')
  108. def test_show(self):
  109. nodes = self.t.cluster_from_argv.return_value = [
  110. Mock(name='n1'),
  111. Mock(name='n2'),
  112. ]
  113. nodes[0].argv_with_executable = ['python', 'foo', 'bar']
  114. nodes[1].argv_with_executable = ['python', 'xuzzy', 'baz']
  115. assert self.t.show('10', '-A', 'proj') is self.t.ok.return_value
  116. self.t.ok.assert_called_with(
  117. '\n'.join(' '.join(node.argv_with_executable) for node in nodes))
  118. def test_kill(self):
  119. self.t.kill('10', '-A', 'proj')
  120. self.t.splash.assert_called_with()
  121. self.t.cluster_from_argv.assert_called_with(('10', '-A', 'proj'))
  122. self.cluster.kill.assert_called_with()
  123. def test_expand(self):
  124. node1 = Mock(name='n1')
  125. node2 = Mock(name='n2')
  126. node1.expander.return_value = 'A'
  127. node2.expander.return_value = 'B'
  128. nodes = self.t.cluster_from_argv.return_value = [node1, node2]
  129. assert self.t.expand('%p', '10') is self.t.ok.return_value
  130. self.t.cluster_from_argv.assert_called_with(('10',))
  131. for node in nodes:
  132. node.expander.assert_called_with('%p')
  133. self.t.ok.assert_called_with('A\nB')
  134. def test_note(self):
  135. self.t.quiet = True
  136. self.t.note('foo')
  137. self.t.say.assert_not_called()
  138. self.t.quiet = False
  139. self.t.note('foo')
  140. self.t.say.assert_called_with('foo', newline=True)
  141. def test_splash(self):
  142. x = MultiTool()
  143. x.note = Mock()
  144. x.nosplash = True
  145. x.splash()
  146. x.note.assert_not_called()
  147. x.nosplash = False
  148. x.splash()
  149. x.note.assert_called()
  150. def test_Cluster(self):
  151. m = MultiTool()
  152. c = m.cluster_from_argv(['A', 'B', 'C'])
  153. assert c.env is m.env
  154. assert c.cmd == 'celery worker'
  155. assert c.on_stopping_preamble == m.on_stopping_preamble
  156. assert c.on_send_signal == m.on_send_signal
  157. assert c.on_still_waiting_for == m.on_still_waiting_for
  158. assert c.on_still_waiting_progress == m.on_still_waiting_progress
  159. assert c.on_still_waiting_end == m.on_still_waiting_end
  160. assert c.on_node_start == m.on_node_start
  161. assert c.on_node_restart == m.on_node_restart
  162. assert c.on_node_shutdown_ok == m.on_node_shutdown_ok
  163. assert c.on_node_status == m.on_node_status
  164. assert c.on_node_signal_dead == m.on_node_signal_dead
  165. assert c.on_node_signal == m.on_node_signal
  166. assert c.on_node_down == m.on_node_down
  167. assert c.on_child_spawn == m.on_child_spawn
  168. assert c.on_child_signalled == m.on_child_signalled
  169. assert c.on_child_failure == m.on_child_failure
  170. def test_on_stopping_preamble(self):
  171. self.t.on_stopping_preamble([])
  172. def test_on_send_signal(self):
  173. self.t.on_send_signal(Mock(), Mock())
  174. def test_on_still_waiting_for(self):
  175. self.t.on_still_waiting_for([Mock(), Mock()])
  176. def test_on_still_waiting_for__empty(self):
  177. self.t.on_still_waiting_for([])
  178. def test_on_still_waiting_progress(self):
  179. self.t.on_still_waiting_progress([])
  180. def test_on_still_waiting_end(self):
  181. self.t.on_still_waiting_end()
  182. def test_on_node_signal_dead(self):
  183. self.t.on_node_signal_dead(Mock())
  184. def test_on_node_start(self):
  185. self.t.on_node_start(Mock())
  186. def test_on_node_restart(self):
  187. self.t.on_node_restart(Mock())
  188. def test_on_node_down(self):
  189. self.t.on_node_down(Mock())
  190. def test_on_node_shutdown_ok(self):
  191. self.t.on_node_shutdown_ok(Mock())
  192. def test_on_node_status__FAIL(self):
  193. self.t.on_node_status(Mock(), 1)
  194. self.t.say.assert_called_with(self.t.FAILED, newline=True)
  195. def test_on_node_status__OK(self):
  196. self.t.on_node_status(Mock(), 0)
  197. self.t.say.assert_called_with(self.t.OK, newline=True)
  198. def test_on_node_signal(self):
  199. self.t.on_node_signal(Mock(), Mock())
  200. def test_on_child_spawn(self):
  201. self.t.on_child_spawn(Mock(), Mock(), Mock())
  202. def test_on_child_signalled(self):
  203. self.t.on_child_signalled(Mock(), Mock())
  204. def test_on_child_failure(self):
  205. self.t.on_child_failure(Mock(), Mock())
  206. def test_constant_strings(self):
  207. assert self.t.OK
  208. assert self.t.DOWN
  209. assert self.t.FAILED
  210. class test_MultiTool_functional:
  211. def setup(self):
  212. self.fh = WhateverIO()
  213. self.env = {}
  214. self.t = MultiTool(env=self.env, fh=self.fh)
  215. def test_note(self):
  216. self.t.note('hello world')
  217. assert self.fh.getvalue() == 'hello world\n'
  218. def test_note_quiet(self):
  219. self.t.quiet = True
  220. self.t.note('hello world')
  221. assert not self.fh.getvalue()
  222. def test_carp(self):
  223. self.t.say = Mock()
  224. self.t.carp('foo')
  225. self.t.say.assert_called_with('foo', True, self.t.stderr)
  226. def test_info(self):
  227. self.t.verbose = True
  228. self.t.info('hello info')
  229. assert self.fh.getvalue() == 'hello info\n'
  230. def test_info_not_verbose(self):
  231. self.t.verbose = False
  232. self.t.info('hello info')
  233. assert not self.fh.getvalue()
  234. def test_error(self):
  235. self.t.carp = Mock()
  236. self.t.usage = Mock()
  237. assert self.t.error('foo') == 1
  238. self.t.carp.assert_called_with('foo')
  239. self.t.usage.assert_called_with()
  240. self.t.carp = Mock()
  241. assert self.t.error() == 1
  242. self.t.carp.assert_not_called()
  243. def test_nosplash(self):
  244. self.t.nosplash = True
  245. self.t.splash()
  246. assert not self.fh.getvalue()
  247. def test_splash(self):
  248. self.t.nosplash = False
  249. self.t.splash()
  250. assert 'celery multi' in self.fh.getvalue()
  251. def test_usage(self):
  252. self.t.usage()
  253. assert self.fh.getvalue()
  254. def test_help(self):
  255. self.t.help([])
  256. assert doc in self.fh.getvalue()
  257. def test_expand(self):
  258. self.t.expand('foo%n', 'ask', 'klask', 'dask')
  259. assert self.fh.getvalue() == 'fooask\nfooklask\nfoodask\n'
  260. @patch('celery.apps.multi.gethostname')
  261. def test_get(self, gethostname):
  262. gethostname.return_value = 'e.com'
  263. self.t.get('xuzzy@e.com', 'foo', 'bar', 'baz')
  264. assert not self.fh.getvalue()
  265. self.t.get('foo@e.com', 'foo', 'bar', 'baz')
  266. assert self.fh.getvalue()
  267. @patch('celery.apps.multi.gethostname')
  268. def test_names(self, gethostname):
  269. gethostname.return_value = 'e.com'
  270. self.t.names('foo', 'bar', 'baz')
  271. assert 'foo@e.com\nbar@e.com\nbaz@e.com' in self.fh.getvalue()
  272. def test_execute_from_commandline(self):
  273. start = self.t.commands['start'] = Mock()
  274. self.t.error = Mock()
  275. self.t.execute_from_commandline(['multi', 'start', 'foo', 'bar'])
  276. self.t.error.assert_not_called()
  277. start.assert_called_with('foo', 'bar')
  278. self.t.error = Mock()
  279. self.t.execute_from_commandline(['multi', 'frob', 'foo', 'bar'])
  280. self.t.error.assert_called_with('Invalid command: frob')
  281. self.t.error = Mock()
  282. self.t.execute_from_commandline(['multi'])
  283. self.t.error.assert_called_with()
  284. self.t.error = Mock()
  285. self.t.execute_from_commandline(['multi', '-foo'])
  286. self.t.error.assert_called_with()
  287. self.t.execute_from_commandline(
  288. ['multi', 'start', 'foo',
  289. '--nosplash', '--quiet', '-q', '--verbose', '--no-color'],
  290. )
  291. assert self.t.nosplash
  292. assert self.t.quiet
  293. assert self.t.verbose
  294. assert self.t.no_color
  295. @patch('celery.bin.multi.MultiTool')
  296. def test_main(self, MultiTool):
  297. m = MultiTool.return_value = Mock()
  298. with pytest.raises(SystemExit):
  299. main()
  300. m.execute_from_commandline.assert_called_with(sys.argv)