2016-02-03 23:01:32 +08:00
|
|
|
## The contents of this file are subject to the Mozilla Public License
|
|
|
|
## Version 1.1 (the "License"); you may not use this file except in
|
|
|
|
## compliance with the License. You may obtain a copy of the License
|
|
|
|
## at http://www.mozilla.org/MPL/
|
|
|
|
##
|
|
|
|
## Software distributed under the License is distributed on an "AS IS"
|
|
|
|
## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
|
|
|
|
## the License for the specific language governing rights and
|
|
|
|
## limitations under the License.
|
|
|
|
##
|
|
|
|
## The Original Code is RabbitMQ.
|
|
|
|
##
|
|
|
|
## The Initial Developer of the Original Code is GoPivotal, Inc.
|
|
|
|
## Copyright (c) 2007-2016 Pivotal Software, Inc. All rights reserved.
|
|
|
|
|
|
|
|
|
2016-02-03 22:42:45 +08:00
|
|
|
defmodule RabbitMQCtlTest do
|
2016-02-03 06:11:09 +08:00
|
|
|
use ExUnit.Case, async: false
|
|
|
|
import ExUnit.CaptureIO
|
2016-03-23 00:21:12 +08:00
|
|
|
import ExitCodes
|
2016-04-02 01:58:47 +08:00
|
|
|
import TestHelper
|
2016-03-09 01:05:25 +08:00
|
|
|
|
2016-04-30 05:58:36 +08:00
|
|
|
|
|
|
|
setup_all do
|
|
|
|
:net_kernel.start([:rabbitmqctl, :shortnames])
|
|
|
|
:net_kernel.connect_node(get_rabbit_hostname)
|
|
|
|
|
|
|
|
on_exit([], fn ->
|
|
|
|
:erlang.disconnect_node(get_rabbit_hostname)
|
|
|
|
:net_kernel.stop()
|
|
|
|
end)
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
2016-03-09 01:05:25 +08:00
|
|
|
## ------------------------ Error Messages ------------------------------------
|
|
|
|
|
2016-02-26 06:15:06 +08:00
|
|
|
test "print error message on a bad connection" do
|
2016-02-03 06:11:09 +08:00
|
|
|
command = ["status", "-n", "sandwich@pastrami"]
|
2016-03-22 01:59:22 +08:00
|
|
|
assert capture_io(fn ->
|
2016-03-23 00:21:12 +08:00
|
|
|
error_check(command, exit_unavailable)
|
|
|
|
end) =~ ~r/unable to connect to node 'sandwich@pastrami'\: nodedown/
|
2016-02-03 06:11:09 +08:00
|
|
|
end
|
|
|
|
|
2016-03-09 01:05:25 +08:00
|
|
|
test "print timeout message when an RPC call times out" do
|
2016-03-22 01:59:22 +08:00
|
|
|
command = ["list_users", "-t", "0"]
|
|
|
|
assert capture_io(fn ->
|
2016-03-23 00:21:12 +08:00
|
|
|
error_check(command, exit_tempfail)
|
|
|
|
end) =~ ~r/Error: {timeout, 0}/
|
2016-03-09 01:05:25 +08:00
|
|
|
end
|
|
|
|
|
2016-04-02 01:58:47 +08:00
|
|
|
test "print an authentication error message when auth is refused" do
|
|
|
|
add_user "kirk", "khaaaaaan"
|
|
|
|
command = ["authenticate_user", "kirk", "makeitso"]
|
|
|
|
assert capture_io(
|
|
|
|
fn -> error_check(command, exit_dataerr)
|
|
|
|
end) =~ ~r/Error: failed to authenticate user "kirk"/
|
|
|
|
delete_user "kirk"
|
|
|
|
end
|
|
|
|
|
2016-03-09 01:05:25 +08:00
|
|
|
## ------------------------ Malformed Commands --------------------------------
|
|
|
|
|
2016-02-03 06:11:09 +08:00
|
|
|
test "Empty command shows usage message" do
|
2016-03-23 00:21:12 +08:00
|
|
|
command = []
|
|
|
|
assert capture_io(fn ->
|
|
|
|
error_check(command, exit_ok)
|
|
|
|
end) =~ ~r/Usage:\n/
|
2016-02-03 06:11:09 +08:00
|
|
|
end
|
|
|
|
|
2016-03-22 01:59:22 +08:00
|
|
|
test "Empty command with options shows usage, but is ok" do
|
2016-03-23 00:21:12 +08:00
|
|
|
command = ["-n", "sandwich@pastrami"]
|
|
|
|
assert capture_io(fn ->
|
|
|
|
error_check(command, exit_ok)
|
|
|
|
end) =~ ~r/Usage:\n/
|
2016-02-03 06:11:09 +08:00
|
|
|
end
|
2016-03-05 05:35:54 +08:00
|
|
|
|
2016-04-30 05:58:36 +08:00
|
|
|
test "Short names without host connect properly" do
|
|
|
|
command = ["status", "-n", "rabbit"]
|
|
|
|
capture_io(fn -> error_check(command, exit_ok) end)
|
|
|
|
end
|
|
|
|
|
2016-03-22 01:59:22 +08:00
|
|
|
test "Unimplemented command shows usage message and returns error" do
|
2016-03-23 00:21:12 +08:00
|
|
|
command = ["not_real"]
|
|
|
|
assert capture_io(fn ->
|
|
|
|
error_check(command, exit_usage)
|
|
|
|
end) =~ ~r/Usage\:/
|
2016-03-05 05:35:54 +08:00
|
|
|
end
|
2016-03-08 06:45:50 +08:00
|
|
|
|
2016-04-27 00:20:11 +08:00
|
|
|
test "Extraneous arguments return a usage error" do
|
2016-03-23 00:21:12 +08:00
|
|
|
command = ["status", "extra"]
|
2016-04-27 02:38:23 +08:00
|
|
|
assert capture_io(fn ->
|
|
|
|
error_check(command, exit_usage)
|
2016-04-30 02:08:42 +08:00
|
|
|
end) =~ ~r/Given:\n\t.*\nUsage:\n\tstatus/
|
2016-03-23 00:21:12 +08:00
|
|
|
end
|
2016-03-22 01:59:22 +08:00
|
|
|
|
2016-04-27 00:20:11 +08:00
|
|
|
test "Insufficient arguments return a usage error" do
|
2016-03-23 00:21:12 +08:00
|
|
|
command = ["list_user_permissions"]
|
2016-04-27 02:38:23 +08:00
|
|
|
assert capture_io(fn ->
|
|
|
|
error_check(command, exit_usage)
|
2016-04-30 02:08:42 +08:00
|
|
|
end) =~ ~r/Given:\n\t.*\nUsage:\n\tlist_user_permissions/
|
2016-04-27 00:20:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
test "A bad argument returns a data error" do
|
|
|
|
command = ["set_disk_free_limit", "2097152bytes"]
|
2016-03-23 00:21:12 +08:00
|
|
|
capture_io(fn -> error_check(command, exit_dataerr) end)
|
|
|
|
end
|
2016-03-22 02:29:50 +08:00
|
|
|
|
2016-03-23 00:21:12 +08:00
|
|
|
test "An errored command returns an error code" do
|
|
|
|
command = ["delete_user", "voldemort"]
|
|
|
|
capture_io(fn -> error_check(command, exit_software) end)
|
|
|
|
end
|
2016-03-22 01:59:22 +08:00
|
|
|
|
2016-03-09 01:05:25 +08:00
|
|
|
## ------------------------- Default Flags ------------------------------------
|
|
|
|
|
2016-03-08 06:45:50 +08:00
|
|
|
test "an empty node option is filled with the default rabbit node" do
|
|
|
|
assert RabbitMQCtl.autofill_defaults(%{})[:node] ==
|
|
|
|
TestHelper.get_rabbit_hostname
|
|
|
|
end
|
|
|
|
|
|
|
|
test "a non-empty node option is not overwritten" do
|
|
|
|
assert RabbitMQCtl.autofill_defaults(%{node: :jake@thedog})[:node] ==
|
|
|
|
:jake@thedog
|
|
|
|
end
|
|
|
|
|
|
|
|
test "an empty timeout option is set to infinity" do
|
|
|
|
assert RabbitMQCtl.autofill_defaults(%{})[:timeout] == :infinity
|
|
|
|
end
|
|
|
|
|
|
|
|
test "a non-empty timeout option is not overridden" do
|
|
|
|
assert RabbitMQCtl.autofill_defaults(%{timeout: 60})[:timeout] == 60
|
|
|
|
end
|
2016-03-22 01:59:22 +08:00
|
|
|
|
2016-03-28 23:17:25 +08:00
|
|
|
test "other parameters are not overridden by the default" do
|
|
|
|
assert RabbitMQCtl.autofill_defaults(%{param: "quack"})[:param] == "quack"
|
|
|
|
end
|
|
|
|
|
2016-03-23 00:21:12 +08:00
|
|
|
defp error_check(cmd_line, code) do
|
2016-04-02 00:13:01 +08:00
|
|
|
assert catch_exit(RabbitMQCtl.main(cmd_line)) == {:shutdown, code}
|
2016-03-23 00:21:12 +08:00
|
|
|
end
|
2016-02-03 06:11:09 +08:00
|
|
|
end
|