test_events.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import importlib
  2. from functools import wraps
  3. from case import patch, skip
  4. from celery.bin import events
  5. def _old_patch(module, name, mocked):
  6. module = importlib.import_module(module)
  7. def _patch(fun):
  8. @wraps(fun)
  9. def __patched(*args, **kwargs):
  10. prev = getattr(module, name)
  11. setattr(module, name, mocked)
  12. try:
  13. return fun(*args, **kwargs)
  14. finally:
  15. setattr(module, name, prev)
  16. return __patched
  17. return _patch
  18. class MockCommand:
  19. executed = []
  20. def execute_from_commandline(self, **kwargs):
  21. self.executed.append(True)
  22. def proctitle(prog, info=None):
  23. proctitle.last = (prog, info)
  24. proctitle.last = () # noqa: E305
  25. class test_events:
  26. def setup(self):
  27. self.ev = events.events(app=self.app)
  28. @_old_patch('celery.events.dumper', 'evdump',
  29. lambda **kw: 'me dumper, you?')
  30. @_old_patch('celery.bin.events', 'set_process_title', proctitle)
  31. def test_run_dump(self):
  32. assert self.ev.run(dump=True), 'me dumper == you?'
  33. assert 'celery events:dump' in proctitle.last[0]
  34. @skip.unless_module('curses', import_errors=(ImportError, OSError))
  35. def test_run_top(self):
  36. @_old_patch('celery.events.cursesmon', 'evtop',
  37. lambda **kw: 'me top, you?')
  38. @_old_patch('celery.bin.events', 'set_process_title', proctitle)
  39. def _inner():
  40. assert self.ev.run(), 'me top == you?'
  41. assert 'celery events:top' in proctitle.last[0]
  42. return _inner()
  43. @_old_patch('celery.events.snapshot', 'evcam',
  44. lambda *a, **k: (a, k))
  45. @_old_patch('celery.bin.events', 'set_process_title', proctitle)
  46. def test_run_cam(self):
  47. a, kw = self.ev.run(camera='foo.bar.baz', logfile='logfile')
  48. assert a[0] == 'foo.bar.baz'
  49. assert kw['freq'] == 1.0
  50. assert kw['maxrate'] is None
  51. assert kw['loglevel'] == 'INFO'
  52. assert kw['logfile'] == 'logfile'
  53. assert 'celery events:cam' in proctitle.last[0]
  54. @patch('celery.events.snapshot.evcam')
  55. @patch('celery.bin.events.detached')
  56. def test_run_cam_detached(self, detached, evcam):
  57. self.ev.prog_name = 'celery events'
  58. self.ev.run_evcam('myapp.Camera', detach=True)
  59. detached.assert_called()
  60. evcam.assert_called()
  61. def test_get_options(self):
  62. assert not self.ev.get_options()
  63. @_old_patch('celery.bin.events', 'events', MockCommand)
  64. def test_main(self):
  65. MockCommand.executed = []
  66. events.main()
  67. assert MockCommand.executed