test_celeryd_detach.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from __future__ import absolute_import, unicode_literals
  2. import pytest
  3. from case import Mock, mock, patch
  4. from celery.bin.celeryd_detach import detach, detached_celeryd, main
  5. from celery.platforms import IS_WINDOWS
  6. if not IS_WINDOWS:
  7. class test_detached:
  8. @patch('celery.bin.celeryd_detach.detached')
  9. @patch('os.execv')
  10. @patch('celery.bin.celeryd_detach.logger')
  11. @patch('celery.app.log.Logging.setup_logging_subsystem')
  12. def test_execs(self, setup_logs, logger, execv, detached):
  13. context = detached.return_value = Mock()
  14. context.__enter__ = Mock()
  15. context.__exit__ = Mock()
  16. detach('/bin/boo', ['a', 'b', 'c'], logfile='/var/log',
  17. pidfile='/var/pid', hostname='foo@example.com')
  18. detached.assert_called_with(
  19. '/var/log', '/var/pid', None, None, None, None, False,
  20. after_forkers=False,
  21. )
  22. execv.assert_called_with('/bin/boo', ['/bin/boo', 'a', 'b', 'c'])
  23. r = detach('/bin/boo', ['a', 'b', 'c'],
  24. logfile='/var/log', pidfile='/var/pid',
  25. executable='/bin/foo', app=self.app)
  26. execv.assert_called_with('/bin/foo', ['/bin/foo', 'a', 'b', 'c'])
  27. execv.side_effect = Exception('foo')
  28. r = detach(
  29. '/bin/boo', ['a', 'b', 'c'],
  30. logfile='/var/log', pidfile='/var/pid',
  31. hostname='foo@example.com', app=self.app)
  32. context.__enter__.assert_called_with()
  33. logger.critical.assert_called()
  34. setup_logs.assert_called_with(
  35. 'ERROR', '/var/log', hostname='foo@example.com')
  36. assert r == 1
  37. self.patching('celery.current_app')
  38. from celery import current_app
  39. r = detach(
  40. '/bin/boo', ['a', 'b', 'c'],
  41. logfile='/var/log', pidfile='/var/pid',
  42. hostname='foo@example.com', app=None)
  43. current_app.log.setup_logging_subsystem.assert_called_with(
  44. 'ERROR', '/var/log', hostname='foo@example.com',
  45. )
  46. class test_PartialOptionParser:
  47. def test_parser(self):
  48. x = detached_celeryd(self.app)
  49. p = x.create_parser('celeryd_detach')
  50. options, leftovers = p.parse_known_args([
  51. '--logfile=foo', '--fake', '--enable',
  52. 'a', 'b', '-c1', '-d', '2',
  53. ])
  54. assert options.logfile == 'foo'
  55. assert leftovers, ['--enable', '-c1', '-d' == '2']
  56. options, leftovers = p.parse_known_args([
  57. '--fake', '--enable',
  58. '--pidfile=/var/pid/foo.pid',
  59. 'a', 'b', '-c1', '-d', '2',
  60. ])
  61. assert options.pidfile == '/var/pid/foo.pid'
  62. with mock.stdouts():
  63. with pytest.raises(SystemExit):
  64. p.parse_args(['--logfile'])
  65. p._option_string_actions['--logfile'].nargs = 2
  66. with pytest.raises(SystemExit):
  67. p.parse_args(['--logfile=a'])
  68. with pytest.raises(SystemExit):
  69. p.parse_args(['--fake=abc'])
  70. assert p._option_string_actions['--logfile'].nargs == 2
  71. p.parse_args(['--logfile', 'a', 'b'])
  72. class test_Command:
  73. argv = [
  74. '--foobar=10,2', '-c', '1',
  75. '--logfile=/var/log', '-lDEBUG',
  76. '--', '.disable_rate_limits=1',
  77. ]
  78. def test_parse_options(self):
  79. x = detached_celeryd(app=self.app)
  80. _, argv = x._split_command_line_config(self.argv)
  81. o, l = x.parse_options('cd', argv)
  82. assert o.logfile == '/var/log'
  83. assert l == [
  84. '--foobar=10,2', '-c', '1',
  85. '-lDEBUG', '--logfile=/var/log',
  86. '--pidfile=celeryd.pid',
  87. ]
  88. x.parse_options('cd', []) # no args
  89. @patch('sys.exit')
  90. @patch('celery.bin.celeryd_detach.detach')
  91. def test_execute_from_commandline(self, detach, exit):
  92. x = detached_celeryd(app=self.app)
  93. x.execute_from_commandline(self.argv)
  94. exit.assert_called()
  95. detach.assert_called_with(
  96. path=x.execv_path, uid=None, gid=None,
  97. umask=None, fake=False, logfile='/var/log', pidfile='celeryd.pid',
  98. workdir=None, executable=None, hostname=None,
  99. argv=x.execv_argv + [
  100. '-c', '1', '-lDEBUG',
  101. '--logfile=/var/log', '--pidfile=celeryd.pid',
  102. '--', '.disable_rate_limits=1'
  103. ],
  104. app=self.app,
  105. )
  106. @patch('celery.bin.celeryd_detach.detached_celeryd')
  107. def test_main(self, command):
  108. c = command.return_value = Mock()
  109. main(self.app)
  110. c.execute_from_commandline.assert_called_with()