test_events.py 2.6 KB

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