test_multi.py 13 KB

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