rabbitmq-server/deps/rabbitmq_cli/test/rabbitmqctl_test.exs

141 lines
4.3 KiB
Elixir
Raw Normal View History

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
import ExitCodes
import TestHelper
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
## ------------------------ Error Messages ------------------------------------
test "print error message on a bad connection" do
2016-02-03 06:11:09 +08:00
command = ["status", "-n", "sandwich@pastrami"]
assert capture_io(fn ->
error_check(command, exit_unavailable)
end) =~ ~r/unable to connect to node 'sandwich@pastrami'\: nodedown/
2016-02-03 06:11:09 +08:00
end
test "print timeout message when an RPC call times out" do
command = ["list_users", "-t", "0"]
assert capture_io(fn ->
error_check(command, exit_tempfail)
end) =~ ~r/Error: {timeout, 0}/
end
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
## ------------------------ Malformed Commands --------------------------------
2016-02-03 06:11:09 +08:00
test "Empty command shows usage message" do
command = []
assert capture_io(fn ->
error_check(command, exit_ok)
end) =~ ~r/Usage:\n/
2016-02-03 06:11:09 +08:00
end
test "Empty command with options shows usage, but is ok" do
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-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
test "Unimplemented command shows usage message and returns error" do
command = ["not_real"]
assert capture_io(fn ->
error_check(command, exit_usage)
end) =~ ~r/Usage\:/
end
2016-03-08 06:45:50 +08:00
test "Extraneous arguments return a usage error" do
command = ["status", "extra"]
assert capture_io(fn ->
error_check(command, exit_usage)
end) =~ ~r/Given:\n\t.*\nUsage:\n\tstatus/
end
test "Insufficient arguments return a usage error" do
command = ["list_user_permissions"]
assert capture_io(fn ->
error_check(command, exit_usage)
end) =~ ~r/Given:\n\t.*\nUsage:\n\tlist_user_permissions/
end
test "A bad argument returns a data error" do
command = ["set_disk_free_limit", "2097152bytes"]
capture_io(fn -> error_check(command, exit_dataerr) end)
end
test "An errored command returns an error code" do
command = ["delete_user", "voldemort"]
capture_io(fn -> error_check(command, exit_software) end)
end
## ------------------------- 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
test "other parameters are not overridden by the default" do
assert RabbitMQCtl.autofill_defaults(%{param: "quack"})[:param] == "quack"
end
defp error_check(cmd_line, code) do
assert catch_exit(RabbitMQCtl.main(cmd_line)) == {:shutdown, code}
end
2016-02-03 06:11:09 +08:00
end