Add timeouts to list_user_permissions, refactor a bunch of tests.
This commit is contained in:
parent
f697e93d02
commit
86b09639ec
|
|
@ -18,8 +18,8 @@ defmodule EnvironmentCommand do
|
||||||
import Helpers
|
import Helpers
|
||||||
|
|
||||||
def environment([_head|_], _), do: HelpCommand.help
|
def environment([_head|_], _), do: HelpCommand.help
|
||||||
def environment([], %{node: _} = options) do
|
def environment([], %{node: node_name}) do
|
||||||
options[:node]
|
node_name
|
||||||
|> Helpers.parse_node
|
|> Helpers.parse_node
|
||||||
|> :rabbit_misc.rpc_call(:rabbit, :environment, [])
|
|> :rabbit_misc.rpc_call(:rabbit, :environment, [])
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -17,14 +17,17 @@
|
||||||
defmodule ListUserPermissionsCommand do
|
defmodule ListUserPermissionsCommand do
|
||||||
import Helpers
|
import Helpers
|
||||||
|
|
||||||
def list_user_permissions([username], options) do
|
def list_user_permissions([], _), do: HelpCommand.help
|
||||||
options[:node]
|
def list_user_permissions([_|_] = cmds, _) when length(cmds) != 1, do: HelpCommand.help
|
||||||
|> parse_node
|
def list_user_permissions([username], %{node: node_name, timeout: time_out}) do
|
||||||
|
node_name
|
||||||
|
|> Helpers.parse_node
|
||||||
|> :rabbit_misc.rpc_call(
|
|> :rabbit_misc.rpc_call(
|
||||||
:rabbit_auth_backend_internal,
|
:rabbit_auth_backend_internal,
|
||||||
:list_user_permissions,
|
:list_user_permissions,
|
||||||
[username]
|
[username],
|
||||||
)
|
time_out
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def usage do
|
def usage do
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,12 @@ defmodule RabbitMQCtl do
|
||||||
IO.puts "Error: unable to connect to node '#{target_node}': nodedown"
|
IO.puts "Error: unable to connect to node '#{target_node}': nodedown"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp print_timeout_error(options) do
|
||||||
|
timeout = options[:timeout] || :infinity
|
||||||
|
|
||||||
|
IO.puts "Error: {timeout, #{timeout}}"
|
||||||
|
end
|
||||||
|
|
||||||
def autofill_defaults(%{} = options) do
|
def autofill_defaults(%{} = options) do
|
||||||
options
|
options
|
||||||
|> autofill_node
|
|> autofill_node
|
||||||
|
|
@ -52,7 +58,6 @@ defmodule RabbitMQCtl do
|
||||||
defp run_command(_, []), do: HelpCommand.help
|
defp run_command(_, []), do: HelpCommand.help
|
||||||
defp run_command(options, [cmd | arguments]) do
|
defp run_command(options, [cmd | arguments]) do
|
||||||
connect_to_rabbitmq(options[:node])
|
connect_to_rabbitmq(options[:node])
|
||||||
IO.inspect([cmd | arguments])
|
|
||||||
{result, _} = Code.eval_string(
|
{result, _} = Code.eval_string(
|
||||||
"#{command_string(cmd)}(args, opts)",
|
"#{command_string(cmd)}(args, opts)",
|
||||||
[args: arguments, opts: options]
|
[args: arguments, opts: options]
|
||||||
|
|
@ -60,6 +65,7 @@ defmodule RabbitMQCtl do
|
||||||
|
|
||||||
case result do
|
case result do
|
||||||
{:badrpc, :nodedown} -> print_nodedown_error(options)
|
{:badrpc, :nodedown} -> print_nodedown_error(options)
|
||||||
|
{:badrpc, :timeout} -> print_timeout_error(options)
|
||||||
_ -> IO.inspect result
|
_ -> IO.inspect result
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,8 @@ defmodule StatusCommand do
|
||||||
import Helpers
|
import Helpers
|
||||||
|
|
||||||
def status([_head|_], _), do: HelpCommand.help
|
def status([_head|_], _), do: HelpCommand.help
|
||||||
def status([], %{node: _} = options) do
|
def status([], %{node: node_name}) do
|
||||||
options[:node]
|
node_name
|
||||||
|> Helpers.parse_node
|
|> Helpers.parse_node
|
||||||
|> :rabbit_misc.rpc_call(:rabbit, :status, [])
|
|> :rabbit_misc.rpc_call(:rabbit, :status, [])
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -31,14 +31,19 @@ defmodule EnvironmentCommandTest do
|
||||||
{:ok, opts: %{node: context[:target]}}
|
{:ok, opts: %{node: context[:target]}}
|
||||||
end
|
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
|
test "with extra arguments, environment prints usage" do
|
||||||
assert capture_io(fn ->
|
assert capture_io(fn ->
|
||||||
EnvironmentCommand.environment(["extra"], %{}) end) =~ ~r/Usage:/
|
EnvironmentCommand.environment(["extra"], %{}) end) =~ ~r/Usage:/
|
||||||
end
|
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
|
end
|
||||||
|
|
|
||||||
|
|
@ -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
|
|
||||||
|
|
@ -17,6 +17,7 @@
|
||||||
defmodule ListUserPermissionsCommandTest do
|
defmodule ListUserPermissionsCommandTest do
|
||||||
use ExUnit.Case, async: false
|
use ExUnit.Case, async: false
|
||||||
import TestHelper
|
import TestHelper
|
||||||
|
import ExUnit.CaptureIO
|
||||||
|
|
||||||
setup_all do
|
setup_all do
|
||||||
:net_kernel.start([:rabbitmqctl, :shortnames])
|
:net_kernel.start([:rabbitmqctl, :shortnames])
|
||||||
|
|
@ -24,9 +25,9 @@ defmodule ListUserPermissionsCommandTest do
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
|
||||||
setup default_context do
|
setup context do
|
||||||
:net_kernel.connect_node(get_rabbit_hostname)
|
:net_kernel.connect_node(context[:target])
|
||||||
on_exit(default_context, fn -> :erlang.disconnect_node(get_rabbit_hostname) end)
|
on_exit(context, fn -> :erlang.disconnect_node(context[:target]) end)
|
||||||
|
|
||||||
default_result = [
|
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
|
end
|
||||||
|
|
||||||
@tag username: "guest"
|
## -------------------------------- Usage -------------------------------------
|
||||||
test "valid user returns a list of permissions", default_context do
|
|
||||||
assert ListUserPermissionsCommand.list_user_permissions(
|
test "wrong number of arguments results in usage print" do
|
||||||
[default_context[:username]], []) == default_context[:result]
|
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
|
end
|
||||||
|
|
||||||
@tag username: "interloper"
|
## ------------------------------- Username -----------------------------------
|
||||||
test "invalid user returns a no-such-user error", default_context do
|
|
||||||
|
@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(
|
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
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -24,11 +24,21 @@ defmodule RabbitMQCtlTest do
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
## ------------------------ Error Messages ------------------------------------
|
||||||
|
|
||||||
test "print error message on a bad connection" do
|
test "print error message on a bad connection" do
|
||||||
command = ["status", "-n", "sandwich@pastrami"]
|
command = ["status", "-n", "sandwich@pastrami"]
|
||||||
assert capture_io(fn -> RabbitMQCtl.main(command) end) =~ ~r/unable to connect to node 'sandwich@pastrami'\: nodedown/
|
assert capture_io(fn -> RabbitMQCtl.main(command) end) =~ ~r/unable to connect to node 'sandwich@pastrami'\: nodedown/
|
||||||
end
|
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
|
test "Empty command shows usage message" do
|
||||||
assert capture_io(fn -> RabbitMQCtl.main([]) end) =~ ~r/Usage\:/
|
assert capture_io(fn -> RabbitMQCtl.main([]) end) =~ ~r/Usage\:/
|
||||||
end
|
end
|
||||||
|
|
@ -41,6 +51,8 @@ defmodule RabbitMQCtlTest do
|
||||||
assert capture_io(fn -> RabbitMQCtl.main(["not_real"]) end) =~ ~r/Usage\:/
|
assert capture_io(fn -> RabbitMQCtl.main(["not_real"]) end) =~ ~r/Usage\:/
|
||||||
end
|
end
|
||||||
|
|
||||||
|
## ------------------------- Default Flags ------------------------------------
|
||||||
|
|
||||||
test "an empty node option is filled with the default rabbit node" do
|
test "an empty node option is filled with the default rabbit node" do
|
||||||
assert RabbitMQCtl.autofill_defaults(%{})[:node] ==
|
assert RabbitMQCtl.autofill_defaults(%{})[:node] ==
|
||||||
TestHelper.get_rabbit_hostname
|
TestHelper.get_rabbit_hostname
|
||||||
|
|
|
||||||
|
|
@ -31,14 +31,19 @@ defmodule StatusCommandTest do
|
||||||
{:ok, opts: %{node: context[:target]}}
|
{:ok, opts: %{node: context[:target]}}
|
||||||
end
|
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
|
@tag target: get_rabbit_hostname
|
||||||
test "with extra arguments, status prints usage", context do
|
test "with extra arguments, status prints usage", context do
|
||||||
assert capture_io(fn ->
|
assert capture_io(fn ->
|
||||||
StatusCommand.status(["extra"], context[:opts]) end) =~ ~r/Usage:/
|
StatusCommand.status(["extra"], context[:opts]) end) =~ ~r/Usage:/
|
||||||
end
|
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
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue