test_multi.py 13 KB

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