test_celeryd_detach.py 4.6 KB

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