test_celeryd_detach.py 4.7 KB

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