test_celeryd_detach.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from __future__ import absolute_import
  2. from __future__ import with_statement
  3. from mock import Mock, patch
  4. from celery import current_app
  5. from celery.bin.celeryd_detach import (
  6. detach,
  7. detached_celeryd,
  8. main,
  9. )
  10. from celery.tests.utils import Case, override_stdouts
  11. if not current_app.IS_WINDOWS:
  12. class test_detached(Case):
  13. @patch('celery.bin.celeryd_detach.detached')
  14. @patch('os.execv')
  15. @patch('celery.bin.celeryd_detach.logger')
  16. @patch('celery.app.log.Logging.setup_logging_subsystem')
  17. def test_execs(self, setup_logs, logger, execv, detached):
  18. context = detached.return_value = Mock()
  19. context.__enter__ = Mock()
  20. context.__exit__ = Mock()
  21. detach('/bin/boo', ['a', 'b', 'c'], logfile='/var/log',
  22. pidfile='/var/pid')
  23. detached.assert_called_with('/var/log', '/var/pid', None, None, 0,
  24. None, False)
  25. execv.assert_called_with('/bin/boo', ['/bin/boo', 'a', 'b', 'c'])
  26. execv.side_effect = Exception('foo')
  27. r = detach('/bin/boo', ['a', 'b', 'c'], logfile='/var/log',
  28. pidfile='/var/pid')
  29. context.__enter__.assert_called_with()
  30. self.assertTrue(logger.critical.called)
  31. setup_logs.assert_called_with('ERROR', '/var/log')
  32. self.assertEqual(r, 1)
  33. class test_PartialOptionParser(Case):
  34. def test_parser(self):
  35. x = detached_celeryd()
  36. p = x.Parser('celeryd_detach')
  37. options, values = p.parse_args(['--logfile=foo', '--fake', '--enable',
  38. 'a', 'b', '-c1', '-d', '2'])
  39. self.assertEqual(options.logfile, 'foo')
  40. self.assertEqual(values, ['a', 'b'])
  41. self.assertEqual(p.leftovers, ['--enable', '-c1', '-d', '2'])
  42. with override_stdouts():
  43. with self.assertRaises(SystemExit):
  44. p.parse_args(['--logfile'])
  45. p.get_option('--logfile').nargs = 2
  46. with self.assertRaises(SystemExit):
  47. p.parse_args(['--logfile=a'])
  48. with self.assertRaises(SystemExit):
  49. p.parse_args(['--fake=abc'])
  50. assert p.get_option('--logfile').nargs == 2
  51. p.parse_args(['--logfile=a', 'b'])
  52. p.get_option('--logfile').nargs = 1
  53. class test_Command(Case):
  54. argv = ['--autoscale=10,2', '-c', '1',
  55. '--logfile=/var/log', '-lDEBUG',
  56. '--', '.disable_rate_limits=1']
  57. def test_parse_options(self):
  58. x = detached_celeryd()
  59. o, v, l = x.parse_options('cd', self.argv)
  60. self.assertEqual(o.logfile, '/var/log')
  61. self.assertEqual(l, ['--autoscale=10,2', '-c', '1',
  62. '-lDEBUG', '--logfile=/var/log',
  63. '--pidfile=celeryd.pid'])
  64. x.parse_options('cd', []) # no args
  65. @patch('sys.exit')
  66. @patch('celery.bin.celeryd_detach.detach')
  67. def test_execute_from_commandline(self, detach, exit):
  68. x = detached_celeryd()
  69. x.execute_from_commandline(self.argv)
  70. self.assertTrue(exit.called)
  71. detach.assert_called_with(path=x.execv_path, uid=None, gid=None,
  72. umask=0, fake=False, logfile='/var/log', pidfile='celeryd.pid',
  73. argv=['-m', 'celery.bin.celeryd', '-c', '1', '-lDEBUG',
  74. '--logfile=/var/log', '--pidfile=celeryd.pid',
  75. '--', '.disable_rate_limits=1'],
  76. )
  77. @patch('celery.bin.celeryd_detach.detached_celeryd')
  78. def test_main(self, command):
  79. c = command.return_value = Mock()
  80. main()
  81. c.execute_from_commandline.assert_called_with()