test_camqadm.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. from __future__ import absolute_import
  2. from __future__ import with_statement
  3. from mock import Mock, patch
  4. from celery import Celery
  5. from celery.bin.camqadm import (
  6. AMQPAdmin,
  7. AMQShell,
  8. dump_message,
  9. AMQPAdminCommand,
  10. camqadm,
  11. main,
  12. )
  13. from celery.tests.utils import AppCase, WhateverIO
  14. class test_AMQShell(AppCase):
  15. def setup(self):
  16. self.fh = WhateverIO()
  17. self.app = Celery(broker="memory://", set_as_current=False)
  18. self.adm = self.create_adm()
  19. self.shell = AMQShell(connect=self.adm.connect, out=self.fh)
  20. def create_adm(self, *args, **kwargs):
  21. return AMQPAdmin(app=self.app, out=self.fh, *args, **kwargs)
  22. def test_queue_declare(self):
  23. self.shell.onecmd("queue.declare foo")
  24. self.assertIn("ok", self.fh.getvalue())
  25. def test_missing_command(self):
  26. self.shell.onecmd("foo foo")
  27. self.assertIn("unknown syntax", self.fh.getvalue())
  28. def RV(self):
  29. raise Exception(self.fh.getvalue())
  30. def test_missing_namespace(self):
  31. self.shell.onecmd("ns.cmd arg")
  32. self.assertIn("unknown syntax", self.fh.getvalue())
  33. def test_help(self):
  34. self.shell.onecmd("help")
  35. self.assertIn("Example:", self.fh.getvalue())
  36. def test_help_command(self):
  37. self.shell.onecmd("help queue.declare")
  38. self.assertIn("passive:no", self.fh.getvalue())
  39. def test_help_unknown_command(self):
  40. self.shell.onecmd("help foo.baz")
  41. self.assertIn("unknown syntax", self.fh.getvalue())
  42. def test_exit(self):
  43. with self.assertRaises(SystemExit):
  44. self.shell.onecmd("exit")
  45. self.assertIn("don't leave!", self.fh.getvalue())
  46. def test_note_silent(self):
  47. self.shell.silent = True
  48. self.shell.note("foo bar")
  49. self.assertNotIn("foo bar", self.fh.getvalue())
  50. def test_reconnect(self):
  51. self.shell.onecmd("queue.declare foo")
  52. self.shell.needs_reconnect = True
  53. self.shell.onecmd("queue.delete foo")
  54. def test_completenames(self):
  55. self.assertEqual(self.shell.completenames("queue.dec"),
  56. ["queue.declare"])
  57. self.assertEqual(self.shell.completenames("declare"),
  58. ["queue.declare", "exchange.declare"])
  59. def test_empty_line(self):
  60. self.shell.emptyline = Mock()
  61. self.shell.default = Mock()
  62. self.shell.onecmd("")
  63. self.shell.emptyline.assert_called_with()
  64. self.shell.onecmd("foo")
  65. self.shell.default.assert_called_with("foo")
  66. def test_respond(self):
  67. self.shell.respond({"foo": "bar"})
  68. self.assertIn("foo", self.fh.getvalue())
  69. def test_prompt(self):
  70. self.assertTrue(self.shell.prompt)
  71. def test_no_returns(self):
  72. self.shell.onecmd("queue.declare foo")
  73. self.shell.onecmd("exchange.declare bar direct yes")
  74. self.shell.onecmd("queue.bind foo bar baz")
  75. self.shell.onecmd("basic.ack 1")
  76. def test_dump_message(self):
  77. m = Mock()
  78. m.body = "the quick brown fox"
  79. m.properties = {"a": 1}
  80. m.delivery_info = {"exchange": "bar"}
  81. self.assertTrue(dump_message(m))
  82. def test_dump_message_no_message(self):
  83. self.assertIn("No messages in queue", dump_message(None))
  84. def test_note(self):
  85. self.adm.silent = True
  86. self.adm.note("FOO")
  87. self.assertNotIn("FOO", self.fh.getvalue())
  88. def test_run(self):
  89. a = self.create_adm("queue.declare foo")
  90. a.run()
  91. self.assertIn("ok", self.fh.getvalue())
  92. def test_run_loop(self):
  93. a = self.create_adm()
  94. a.Shell = Mock()
  95. shell = a.Shell.return_value = Mock()
  96. shell.cmdloop = Mock()
  97. a.run()
  98. shell.cmdloop.assert_called_with()
  99. shell.cmdloop.side_effect = KeyboardInterrupt()
  100. a.run()
  101. self.assertIn("bibi", self.fh.getvalue())
  102. @patch("celery.bin.camqadm.AMQPAdminCommand")
  103. def test_main(self, Command):
  104. c = Command.return_value = Mock()
  105. main()
  106. c.execute_from_commandline.assert_called_with()
  107. @patch("celery.bin.camqadm.AMQPAdmin")
  108. def test_camqadm(self, cls):
  109. c = cls.return_value = Mock()
  110. camqadm()
  111. c.run.assert_called_with()
  112. @patch("celery.bin.camqadm.AMQPAdmin")
  113. def test_AMQPAdminCommand(self, cls):
  114. c = cls.return_value = Mock()
  115. camqadm()
  116. c.run.assert_called_with()
  117. x = AMQPAdminCommand(app=self.app)
  118. x.run()
  119. self.assertIs(cls.call_args[1]["app"], self.app)
  120. c.run.assert_called_with()