Add timeouts to list_user_permissions, refactor a bunch of tests.

This commit is contained in:
Brandon Shroyer 2016-03-08 12:05:25 -05:00
parent f697e93d02
commit 86b09639ec
9 changed files with 111 additions and 84 deletions

View File

@ -18,8 +18,8 @@ defmodule EnvironmentCommand do
import Helpers
def environment([_head|_], _), do: HelpCommand.help
def environment([], %{node: _} = options) do
options[:node]
def environment([], %{node: node_name}) do
node_name
|> Helpers.parse_node
|> :rabbit_misc.rpc_call(:rabbit, :environment, [])
end

View File

@ -17,14 +17,17 @@
defmodule ListUserPermissionsCommand do
import Helpers
def list_user_permissions([username], options) do
options[:node]
|> parse_node
def list_user_permissions([], _), do: HelpCommand.help
def list_user_permissions([_|_] = cmds, _) when length(cmds) != 1, do: HelpCommand.help
def list_user_permissions([username], %{node: node_name, timeout: time_out}) do
node_name
|> Helpers.parse_node
|> :rabbit_misc.rpc_call(
:rabbit_auth_backend_internal,
:list_user_permissions,
[username]
)
:rabbit_auth_backend_internal,
:list_user_permissions,
[username],
time_out
)
end
def usage do

View File

@ -39,6 +39,12 @@ defmodule RabbitMQCtl do
IO.puts "Error: unable to connect to node '#{target_node}': nodedown"
end
defp print_timeout_error(options) do
timeout = options[:timeout] || :infinity
IO.puts "Error: {timeout, #{timeout}}"
end
def autofill_defaults(%{} = options) do
options
|> autofill_node
@ -52,7 +58,6 @@ defmodule RabbitMQCtl do
defp run_command(_, []), do: HelpCommand.help
defp run_command(options, [cmd | arguments]) do
connect_to_rabbitmq(options[:node])
IO.inspect([cmd | arguments])
{result, _} = Code.eval_string(
"#{command_string(cmd)}(args, opts)",
[args: arguments, opts: options]
@ -60,6 +65,7 @@ defmodule RabbitMQCtl do
case result do
{:badrpc, :nodedown} -> print_nodedown_error(options)
{:badrpc, :timeout} -> print_timeout_error(options)
_ -> IO.inspect result
end
end

View File

@ -18,8 +18,8 @@ defmodule StatusCommand do
import Helpers
def status([_head|_], _), do: HelpCommand.help
def status([], %{node: _} = options) do
options[:node]
def status([], %{node: node_name}) do
node_name
|> Helpers.parse_node
|> :rabbit_misc.rpc_call(:rabbit, :status, [])
end

View File

@ -31,14 +31,19 @@ defmodule EnvironmentCommandTest do
{:ok, opts: %{node: context[:target]}}
end
@tag target: get_rabbit_hostname
test "environment request on default RabbitMQ node", context do
assert EnvironmentCommand.environment([], context[:opts])[:kernel] != nil
assert EnvironmentCommand.environment([], context[:opts])[:rabbit] != nil
end
test "with extra arguments, environment prints usage" do
assert capture_io(fn ->
EnvironmentCommand.environment(["extra"], %{}) end) =~ ~r/Usage:/
end
@tag target: get_rabbit_hostname
test "environment request on a named, active RMQ node is successful", context do
assert EnvironmentCommand.environment([], context[:opts])[:kernel] != nil
assert EnvironmentCommand.environment([], context[:opts])[:rabbit] != nil
end
@tag target: :jake@thedog
test "environment request on nonexistent RabbitMQ node returns nodedown", context do
assert EnvironmentCommand.environment([], context[:opts]) == {:badrpc, :nodedown}
end
end

View File

@ -1,49 +0,0 @@
## 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.
defmodule FlagTest do
use ExUnit.Case, async: false
import ExUnit.CaptureIO
import Helpers
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
on_exit([], fn -> :net_kernel.stop() end)
:ok
end
setup node_context do
:ok
end
test "status request with no specified hostname is successful" do
assert capture_io(fn -> RabbitMQCtl.main(["status"]) end) =~ ~r/'RabbitMQ'/
end
@tag target: get_rabbit_hostname()
test "status request on a named, active RMQ node is successful", node_context do
assert capture_io(fn ->
RabbitMQCtl.main(["status", "-n", "#{node_context[:target]}"])
end) =~ ~r/'RabbitMQ'/
end
@tag target: "jake@thedog"
test "status request on nonexistent RabbitMQ node returns nodedown", node_context do
assert capture_io(fn ->
RabbitMQCtl.main(["status", "--node=#{node_context[:target]}"])
end) =~ ~r/unable to connect to node 'jake@thedog': nodedown/
end
end

View File

@ -17,6 +17,7 @@
defmodule ListUserPermissionsCommandTest do
use ExUnit.Case, async: false
import TestHelper
import ExUnit.CaptureIO
setup_all do
:net_kernel.start([:rabbitmqctl, :shortnames])
@ -24,9 +25,9 @@ defmodule ListUserPermissionsCommandTest do
:ok
end
setup default_context do
:net_kernel.connect_node(get_rabbit_hostname)
on_exit(default_context, fn -> :erlang.disconnect_node(get_rabbit_hostname) end)
setup context do
:net_kernel.connect_node(context[:target])
on_exit(context, fn -> :erlang.disconnect_node(context[:target]) end)
default_result = [
[
@ -37,20 +38,64 @@ defmodule ListUserPermissionsCommandTest do
]
]
no_such_user_result = {:error, {:no_such_user, default_context[:username]}}
no_such_user_result = {:error, {:no_such_user, context[:username]}}
{:ok, result: default_result, no_such_user: no_such_user_result}
{
:ok,
opts: %{node: context[:target], timeout: context[:test_timeout]},
result: default_result,
no_such_user: no_such_user_result,
timeout: {:badrpc, :timeout}
}
end
@tag username: "guest"
test "valid user returns a list of permissions", default_context do
assert ListUserPermissionsCommand.list_user_permissions(
[default_context[:username]], []) == default_context[:result]
## -------------------------------- Usage -------------------------------------
test "wrong number of arguments results in usage print" do
assert capture_io(fn ->
ListUserPermissionsCommand.list_user_permissions([],%{})
end) =~ ~r/Usage:/
assert capture_io(fn ->
ListUserPermissionsCommand.list_user_permissions(["guest", "extra"],%{})
end) =~ ~r/Usage:/
end
@tag username: "interloper"
test "invalid user returns a no-such-user error", default_context do
## ------------------------------- Username -----------------------------------
@tag target: get_rabbit_hostname, test_timeout: :infinity, username: "guest"
test "valid user returns a list of permissions", context do
assert ListUserPermissionsCommand.list_user_permissions(
[default_context[:username]], []) == default_context[:no_such_user]
[context[:username]], context[:opts]) == context[:result]
end
@tag target: get_rabbit_hostname, test_timeout: :infinity, username: "interloper"
test "invalid user returns a no-such-user error", context do
assert ListUserPermissionsCommand.list_user_permissions(
[context[:username]], context[:opts]) == context[:no_such_user]
end
## --------------------------------- Flags ------------------------------------
@tag target: :jake@thedog, test_timeout: :infinity, username: "guest"
test "invalid or inactive RabbitMQ node returns a bad RPC error", context do
assert ListUserPermissionsCommand.list_user_permissions(
[context[:username]], context[:opts]) == {:badrpc, :nodedown}
end
@tag target: get_rabbit_hostname, test_timeout: 30, username: "guest"
test "long user-defined timeout doesn't interfere with operation", context do
assert ListUserPermissionsCommand.list_user_permissions(
[context[:username]],
context[:opts]
) == context[:result]
end
@tag target: get_rabbit_hostname, test_timeout: 0, username: "guest"
test "timeout causes command to return a bad RPC", context do
assert ListUserPermissionsCommand.list_user_permissions(
[context[:username]],
context[:opts]
) == context[:timeout]
end
end

View File

@ -24,11 +24,21 @@ defmodule RabbitMQCtlTest do
:ok
end
## ------------------------ Error Messages ------------------------------------
test "print error message on a bad connection" do
command = ["status", "-n", "sandwich@pastrami"]
assert capture_io(fn -> RabbitMQCtl.main(command) end) =~ ~r/unable to connect to node 'sandwich@pastrami'\: nodedown/
end
test "print timeout message when an RPC call times out" do
command = ["status", "-n", "sandwich@pastrami"]
assert capture_io(fn -> RabbitMQCtl.main(command) end) =~ ~r/unable to connect to node 'sandwich@pastrami'\: nodedown/
end
## ------------------------ Malformed Commands --------------------------------
test "Empty command shows usage message" do
assert capture_io(fn -> RabbitMQCtl.main([]) end) =~ ~r/Usage\:/
end
@ -41,6 +51,8 @@ defmodule RabbitMQCtlTest do
assert capture_io(fn -> RabbitMQCtl.main(["not_real"]) end) =~ ~r/Usage\:/
end
## ------------------------- Default Flags ------------------------------------
test "an empty node option is filled with the default rabbit node" do
assert RabbitMQCtl.autofill_defaults(%{})[:node] ==
TestHelper.get_rabbit_hostname

View File

@ -31,14 +31,19 @@ defmodule StatusCommandTest do
{:ok, opts: %{node: context[:target]}}
end
@tag target: get_rabbit_hostname
test "status request on default RabbitMQ node", context do
assert StatusCommand.status([], context[:opts])[:pid] != nil
end
@tag target: get_rabbit_hostname
test "with extra arguments, status prints usage", context do
assert capture_io(fn ->
StatusCommand.status(["extra"], context[:opts]) end) =~ ~r/Usage:/
end
@tag target: get_rabbit_hostname
test "status request on a named, active RMQ node is successful", context do
assert StatusCommand.status([], context[:opts])[:pid] != nil
end
@tag target: :jake@thedog
test "status request on nonexistent RabbitMQ node returns nodedown", context do
assert StatusCommand.status([], context[:opts]) != nil
end
end