test_control.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. from __future__ import absolute_import, unicode_literals
  2. import pytest
  3. from case import Mock, patch
  4. from celery.bin.base import Error
  5. from celery.bin.control import _RemoteControl, control, inspect, status
  6. from celery.five import WhateverIO
  7. class test_RemoteControl:
  8. def test_call_interface(self):
  9. with pytest.raises(NotImplementedError):
  10. _RemoteControl(app=self.app).call()
  11. class test_inspect:
  12. def test_usage(self):
  13. assert inspect(app=self.app).usage('foo')
  14. def test_command_info(self):
  15. i = inspect(app=self.app)
  16. assert i.get_command_info(
  17. 'ping', help=True, color=i.colored.red, app=self.app,
  18. )
  19. def test_list_commands_color(self):
  20. i = inspect(app=self.app)
  21. assert i.list_commands(help=True, color=i.colored.red, app=self.app)
  22. assert i.list_commands(help=False, color=None, app=self.app)
  23. def test_epilog(self):
  24. assert inspect(app=self.app).epilog
  25. def test_do_call_method_sql_transport_type(self):
  26. self.app.connection = Mock()
  27. conn = self.app.connection.return_value = Mock(name='Connection')
  28. conn.transport.driver_type = 'sql'
  29. i = inspect(app=self.app)
  30. with pytest.raises(i.Error):
  31. i.do_call_method(['ping'])
  32. def test_say_directions(self):
  33. i = inspect(self.app)
  34. i.out = Mock()
  35. i.quiet = True
  36. i.say_chat('<-', 'hello out')
  37. i.out.assert_not_called()
  38. i.say_chat('->', 'hello in')
  39. i.out.assert_called()
  40. i.quiet = False
  41. i.out.reset_mock()
  42. i.say_chat('<-', 'hello out', 'body')
  43. i.out.assert_called()
  44. @patch('celery.app.control.Control.inspect')
  45. def test_run(self, real):
  46. out = WhateverIO()
  47. i = inspect(app=self.app, stdout=out)
  48. with pytest.raises(Error):
  49. i.run()
  50. with pytest.raises(Error):
  51. i.run('help')
  52. with pytest.raises(Error):
  53. i.run('xyzzybaz')
  54. i.run('ping')
  55. real.assert_called()
  56. i.run('ping', destination='foo,bar')
  57. assert real.call_args[1]['destination'], ['foo' == 'bar']
  58. assert real.call_args[1]['timeout'] == 0.2
  59. callback = real.call_args[1]['callback']
  60. callback({'foo': {'ok': 'pong'}})
  61. assert 'OK' in out.getvalue()
  62. with patch('celery.bin.control.dumps') as dumps:
  63. i.run('ping', json=True)
  64. dumps.assert_called()
  65. instance = real.return_value = Mock()
  66. instance._request.return_value = None
  67. with pytest.raises(Error):
  68. i.run('ping')
  69. out.seek(0)
  70. out.truncate()
  71. i.quiet = True
  72. i.say_chat('<-', 'hello')
  73. assert not out.getvalue()
  74. class test_control:
  75. def control(self, patch_call, *args, **kwargs):
  76. kwargs.setdefault('app', Mock(name='app'))
  77. c = control(*args, **kwargs)
  78. if patch_call:
  79. c.call = Mock(name='control.call')
  80. return c
  81. def test_call(self):
  82. i = self.control(False)
  83. i.call('foo', arguments={'kw': 2})
  84. i.app.control.broadcast.assert_called_with(
  85. 'foo', arguments={'kw': 2}, reply=True)
  86. class test_status:
  87. @patch('celery.bin.control.inspect')
  88. def test_run(self, inspect_):
  89. out, err = WhateverIO(), WhateverIO()
  90. ins = inspect_.return_value = Mock()
  91. ins.run.return_value = []
  92. s = status(self.app, stdout=out, stderr=err)
  93. with pytest.raises(Error):
  94. s.run()
  95. ins.run.return_value = ['a', 'b', 'c']
  96. s.run()
  97. assert '3 nodes online' in out.getvalue()
  98. s.run(quiet=True)