Fix merge_defaults implementations

...and don't default inside run methods.

Defaults must go on left-hand-side of `Map.merge`, or they'll clobber
user-defined settings.

Remove some unnecessary pattern matching.

Issue #92

[#127359637]
This commit is contained in:
Andrew Bruce 2016-08-15 11:24:53 +01:00
parent 4e45a52dd4
commit 541ad4249c
20 changed files with 81 additions and 59 deletions

View File

@ -21,7 +21,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ClearParameterCommand do
def switches(), do: [] def switches(), do: []
def aliases(), do: [] def aliases(), do: []
def merge_defaults(args, opts) do def merge_defaults(args, opts) do
default_opts = Map.merge(opts, %{vhost: "/"}) default_opts = Map.merge(%{vhost: "/"}, opts)
{args, default_opts} {args, default_opts}
end end

View File

@ -16,10 +16,13 @@
defmodule RabbitMQ.CLI.Ctl.Commands.ClearPermissionsCommand do defmodule RabbitMQ.CLI.Ctl.Commands.ClearPermissionsCommand do
@behaviour RabbitMQ.CLI.CommandBehaviour @behaviour RabbitMQ.CLI.CommandBehaviour
@default_vhost "/"
@flags [:vhost] @flags [:vhost]
def merge_defaults(args, opts), do: {args, opts} def merge_defaults(args, opts) do
default_opts = Map.merge(%{vhost: "/"}, opts)
{args, default_opts}
end
def validate([], _) do def validate([], _) do
{:validation_failure, :not_enough_args} {:validation_failure, :not_enough_args}
end end
@ -36,10 +39,6 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ClearPermissionsCommand do
:rabbit_auth_backend_internal, :clear_permissions, [username, vhost]) :rabbit_auth_backend_internal, :clear_permissions, [username, vhost])
end end
def run([username], %{node: _} = opts) do
run([username], Map.merge(opts, %{vhost: @default_vhost}))
end
def usage, do: "clear_permissions [-p vhost] <username>" def usage, do: "clear_permissions [-p vhost] <username>"
def banner([username], %{vhost: vhost}), do: "Clearing permissions for user \"#{username}\" in vhost \"#{vhost}\" ..." def banner([username], %{vhost: vhost}), do: "Clearing permissions for user \"#{username}\" in vhost \"#{vhost}\" ..."

View File

@ -31,7 +31,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ListConsumersCommand do
end end
end end
def merge_defaults([], opts) do def merge_defaults([], opts) do
{Enum.map(@info_keys, &Atom.to_string/1), opts} {Enum.map(@info_keys, &Atom.to_string/1), Map.merge(%{vhost: "/"}, opts)}
end end
def merge_defaults(args, opts), do: {args, opts} def merge_defaults(args, opts), do: {args, opts}
@ -57,13 +57,6 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ListConsumersCommand do
RpcStream.receive_list_items(node_name, :rabbit_amqqueue, :emit_consumers_all, RpcStream.receive_list_items(node_name, :rabbit_amqqueue, :emit_consumers_all,
[nodes, vhost], timeout, info_keys) [nodes, vhost], timeout, info_keys)
end end
def run(args, %{node: _node_name, timeout: _timeout} = opts) do
run(args, Map.merge(default_opts, opts))
end
defp default_opts() do
%{vhost: "/"}
end
def banner(_, %{vhost: vhost}), do: "Listing consumers on vhost #{vhost} ..." def banner(_, %{vhost: vhost}), do: "Listing consumers on vhost #{vhost} ..."
end end

View File

@ -28,10 +28,14 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ListExchangesCommand do
err -> err err -> err
end end
end end
def merge_defaults([], opts) do def merge_defaults([], opts) do
{~w(name type), Map.merge(opts, default_opts())} merge_defaults(~w(name type), opts)
end
def merge_defaults(args, opts) do
{args, Map.merge(%{vhost: "/"}, opts)}
end end
def merge_defaults(args, opts), do: {args, opts}
def switches(), do: [] def switches(), do: []
def aliases(), do: [] def aliases(), do: []
@ -57,9 +61,5 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ListExchangesCommand do
info_keys) info_keys)
end end
defp default_opts() do
%{vhost: "/"}
end
def banner(_,_), do: "Listing exchanges ..." def banner(_,_), do: "Listing exchanges ..."
end end

View File

@ -18,7 +18,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ListParametersCommand do
@behaviour RabbitMQ.CLI.CommandBehaviour @behaviour RabbitMQ.CLI.CommandBehaviour
@flags [:vhost] @flags [:vhost]
def merge_defaults([], opts) do def merge_defaults([], opts) do
{[], Map.merge(opts, %{vhost: "/"})} {[], Map.merge(%{vhost: "/"}, opts)}
end end
def switches(), do: [] def switches(), do: []

View File

@ -19,7 +19,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ListPermissionsCommand do
@flags [:vhost] @flags [:vhost]
def merge_defaults(args, opts) do def merge_defaults(args, opts) do
{args, Map.merge(opts, %{vhost: "/"})} {args, Map.merge(%{vhost: "/"}, opts)}
end end
def switches(), do: [] def switches(), do: []

View File

@ -42,7 +42,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.PurgeQueueCommand do
end end
def merge_defaults(args, opts) do def merge_defaults(args, opts) do
{args, Map.merge(opts, %{vhost: "/"})} {args, Map.merge(%{vhost: "/"}, opts)}
end end
def validate(args, _) when length(args) > 1 do def validate(args, _) when length(args) > 1 do

View File

@ -17,7 +17,10 @@
defmodule RabbitMQ.CLI.Ctl.Commands.SetPermissionsCommand do defmodule RabbitMQ.CLI.Ctl.Commands.SetPermissionsCommand do
@behaviour RabbitMQ.CLI.CommandBehaviour @behaviour RabbitMQ.CLI.CommandBehaviour
@flags [:vhost] @flags [:vhost]
def merge_defaults(args, opts), do: {args, opts}
def merge_defaults(args, opts) do
{args, Map.merge(%{vhost: "/"}, opts)}
end
def switches(), do: [] def switches(), do: []
def aliases(), do: [] def aliases(), do: []
@ -42,11 +45,6 @@ defmodule RabbitMQ.CLI.Ctl.Commands.SetPermissionsCommand do
) )
end end
def run([_, _, _, _] = args, %{node: _} = opts) do
default_opts = Map.merge(opts, %{vhost: "/"})
run(args, default_opts)
end
def usage, do: "set_permissions [-p <vhost>] <user> <conf> <write> <read>" def usage, do: "set_permissions [-p <vhost>] <user> <conf> <write> <read>"
def flags, do: @flags def flags, do: @flags

View File

@ -15,9 +15,7 @@
defmodule RabbitMQ.CLI.Ctl.Commands.TraceOffCommand do defmodule RabbitMQ.CLI.Ctl.Commands.TraceOffCommand do
@behaviour RabbitMQ.CLI.CommandBehaviour @behaviour RabbitMQ.CLI.CommandBehaviour
@default_vhost "/"
@flags [:vhost] @flags [:vhost]
def validate([_|_], _), do: {:validation_failure, :too_many_args} def validate([_|_], _), do: {:validation_failure, :too_many_args}
@ -25,7 +23,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.TraceOffCommand do
def switches(), do: [] def switches(), do: []
def aliases(), do: [] def aliases(), do: []
def merge_defaults(_, opts) do def merge_defaults(_, opts) do
{[], Map.merge(opts, %{vhost: @default_vhost})} {[], Map.merge(%{vhost: "/"}, opts)}
end end
def run([], %{node: node_name, vhost: vhost}) do def run([], %{node: node_name, vhost: vhost}) do

View File

@ -16,15 +16,13 @@
defmodule RabbitMQ.CLI.Ctl.Commands.TraceOnCommand do defmodule RabbitMQ.CLI.Ctl.Commands.TraceOnCommand do
@behaviour RabbitMQ.CLI.CommandBehaviour @behaviour RabbitMQ.CLI.CommandBehaviour
@default_vhost "/"
@flags [:vhost] @flags [:vhost]
def validate([_|_], _), do: {:validation_failure, :too_many_args} def validate([_|_], _), do: {:validation_failure, :too_many_args}
def validate(_, _), do: :ok def validate(_, _), do: :ok
def merge_defaults([], %{node: _} = opts) do def merge_defaults(_, opts) do
{[], Map.merge(opts, %{vhost: @default_vhost})} {[], Map.merge(%{vhost: "/"}, opts)}
end end
def merge_defaults(args, opts), do: {args, opts}
def switches(), do: [] def switches(), do: []
def aliases(), do: [] def aliases(), do: []

View File

@ -58,6 +58,11 @@ defmodule ClearParameterCommandTest do
assert @command.merge_defaults([], %{}) == {[], %{vhost: "/"}} assert @command.merge_defaults([], %{}) == {[], %{vhost: "/"}}
end end
test "merge_defaults: defaults can be overridden" do
assert @command.merge_defaults([], %{}) == {[], %{vhost: "/"}}
assert @command.merge_defaults([], %{vhost: "non_default"}) == {[], %{vhost: "non_default"}}
end
test "validate: argument validation" do test "validate: argument validation" do
assert @command.validate(["one", "two"], %{}) == :ok assert @command.validate(["one", "two"], %{}) == :ok
assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} assert @command.validate([], %{}) == {:validation_failure, :not_enough_args}

View File

@ -43,11 +43,15 @@ defmodule ClearPermissionsTest do
{ {
:ok, :ok,
opts: %{node: get_rabbit_hostname}, opts: %{node: get_rabbit_hostname, vhost: context[:vhost]}
vhost_options: %{node: get_rabbit_hostname, vhost: context[:vhost]}
} }
end end
test "merge_defaults: defaults can be overridden" do
assert @command.merge_defaults([], %{}) == {[], %{vhost: "/"}}
assert @command.merge_defaults([], %{vhost: "non_default"}) == {[], %{vhost: "non_default"}}
end
test "validate: argument count validates" do test "validate: argument count validates" do
assert @command.validate(["one"], %{}) == :ok assert @command.validate(["one"], %{}) == :ok
assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} assert @command.validate([], %{}) == {:validation_failure, :not_enough_args}
@ -59,7 +63,7 @@ defmodule ClearPermissionsTest do
assert @command.run([context[:user]], context[:opts]) == {:error, {:no_such_user, context[:user]}} assert @command.run([context[:user]], context[:opts]) == {:error, {:no_such_user, context[:user]}}
end end
@tag user: @user @tag user: @user, vhost: @default_vhost
test "run: a valid username clears permissions", context do test "run: a valid username clears permissions", context do
assert @command.run([context[:user]], context[:opts]) == :ok assert @command.run([context[:user]], context[:opts]) == :ok
@ -70,14 +74,14 @@ defmodule ClearPermissionsTest do
test "run: on an invalid node, return a badrpc message" do test "run: on an invalid node, return a badrpc message" do
bad_node = :jake@thedog bad_node = :jake@thedog
arg = ["some_name"] arg = ["some_name"]
opts = %{node: bad_node} opts = %{node: bad_node, vhost: ""}
assert @command.run(arg, opts) == {:badrpc, :nodedown} assert @command.run(arg, opts) == {:badrpc, :nodedown}
end end
@tag user: @user, vhost: @specific_vhost @tag user: @user, vhost: @specific_vhost
test "run: on a valid specified vhost, clear permissions", context do test "run: on a valid specified vhost, clear permissions", context do
assert @command.run([context[:user]], context[:vhost_options]) == :ok assert @command.run([context[:user]], context[:opts]) == :ok
assert list_permissions(context[:vhost]) assert list_permissions(context[:vhost])
|> Enum.filter(fn(record) -> record[:user] == context[:user] end) == [] |> Enum.filter(fn(record) -> record[:user] == context[:user] end) == []
@ -85,12 +89,12 @@ defmodule ClearPermissionsTest do
@tag user: @user, vhost: "bad_vhost" @tag user: @user, vhost: "bad_vhost"
test "run: on an invalid vhost, return no_such_vhost error", context do test "run: on an invalid vhost, return no_such_vhost error", context do
assert @command.run([context[:user]], context[:vhost_options]) == {:error, {:no_such_vhost, context[:vhost]}} assert @command.run([context[:user]], context[:opts]) == {:error, {:no_such_vhost, context[:vhost]}}
end end
@tag user: @user, vhost: @specific_vhost @tag user: @user, vhost: @specific_vhost
test "banner", context do test "banner", context do
s = @command.banner([context[:user]], context[:vhost_options]) s = @command.banner([context[:user]], context[:opts])
assert s =~ ~r/Clearing permissions/ assert s =~ ~r/Clearing permissions/
assert s =~ ~r/\"#{context[:user]}\"/ assert s =~ ~r/\"#{context[:user]}\"/

View File

@ -7,6 +7,7 @@ defmodule ListConsumersCommandTest do
@vhost "test1" @vhost "test1"
@user "guest" @user "guest"
@default_timeout :infinity @default_timeout :infinity
@info_keys ~w(queue_name channel_pid consumer_tag ack_required prefetch_count arguments)
setup_all do setup_all do
RabbitMQ.CLI.Distribution.start() RabbitMQ.CLI.Distribution.start()
@ -36,9 +37,9 @@ defmodule ListConsumersCommandTest do
} }
end end
test "merge_defaults: all keys merged in if none specificed" do test "merge_defaults: defaults can be overridden" do
assert @command.merge_defaults([], %{}) assert @command.merge_defaults([], %{}) == {@info_keys, %{vhost: "/"}}
== {~w(queue_name channel_pid consumer_tag ack_required prefetch_count arguments), %{}} assert @command.merge_defaults([], %{vhost: "non_default"}) == {@info_keys, %{vhost: "non_default"}}
end end
test "validate: returns bad_info_key on a single bad arg", context do test "validate: returns bad_info_key on a single bad arg", context do

View File

@ -55,6 +55,11 @@ defmodule ListExchangesCommandTest do
== {["name", "type"], %{vhost: "/"}} == {["name", "type"], %{vhost: "/"}}
end end
test "merge_defaults: defaults can be overridden" do
assert @command.merge_defaults([], %{}) == {["name", "type"], %{vhost: "/"}}
assert @command.merge_defaults([], %{vhost: "non_default"}) == {["name", "type"], %{vhost: "non_default"}}
end
test "validate: returns bad_info_key on a single bad arg", context do test "validate: returns bad_info_key on a single bad arg", context do
assert @command.validate(["quack"], context[:opts]) == assert @command.validate(["quack"], context[:opts]) ==
{:validation_failure, {:bad_info_key, [:quack]}} {:validation_failure, {:bad_info_key, [:quack]}}

View File

@ -76,6 +76,11 @@ defmodule ListParametersCommandTest do
} }
end end
test "merge_defaults: defaults can be overridden" do
assert @command.merge_defaults([], %{}) == {[], %{vhost: "/"}}
assert @command.merge_defaults([], %{vhost: "non_default"}) == {[], %{vhost: "non_default"}}
end
test "validate: wrong number of arguments leads to an arg count error" do test "validate: wrong number of arguments leads to an arg count error" do
assert @command.validate(["this", "is", "too", "many"], %{}) == {:validation_failure, :too_many_args} assert @command.validate(["this", "is", "too", "many"], %{}) == {:validation_failure, :too_many_args}
end end

View File

@ -56,6 +56,11 @@ defmodule ListPermissionsCommandTest do
assert {[], %{vhost: "/"}} == @command.merge_defaults([], %{}) assert {[], %{vhost: "/"}} == @command.merge_defaults([], %{})
end end
test "merge_defaults: defaults can be overridden" do
assert @command.merge_defaults([], %{}) == {[], %{vhost: "/"}}
assert @command.merge_defaults([], %{vhost: "non_default"}) == {[], %{vhost: "non_default"}}
end
test "validate: invalid parameters yield an arg count error" do test "validate: invalid parameters yield an arg count error" do
assert @command.validate(["extra"], %{}) == {:validation_failure, :too_many_args} assert @command.validate(["extra"], %{}) == {:validation_failure, :too_many_args}
end end

View File

@ -42,6 +42,11 @@ defmodule PurgeQueueCommandTest do
}} }}
end end
test "merge_defaults: defaults can be overridden" do
assert @command.merge_defaults([], %{}) == {[], %{vhost: "/"}}
assert @command.merge_defaults([], %{vhost: "non_default"}) == {[], %{vhost: "non_default"}}
end
@tag test_timeout: 15 @tag test_timeout: 15
test "request to an existent queue on active node succeeds", context do test "request to an existent queue on active node succeeds", context do
add_vhost @vhost add_vhost @vhost

View File

@ -48,11 +48,17 @@ defmodule SetPermissionsCommandTest do
{ {
:ok, :ok,
opts: %{ opts: %{
node: get_rabbit_hostname node: get_rabbit_hostname,
vhost: context[:vhost]
} }
} }
end end
test "merge_defaults: defaults can be overridden" do
assert @command.merge_defaults([], %{}) == {[], %{vhost: "/"}}
assert @command.merge_defaults([], %{vhost: "non_default"}) == {[], %{vhost: "non_default"}}
end
test "validate: wrong number of arguments leads to an arg count error" do test "validate: wrong number of arguments leads to an arg count error" do
assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} assert @command.validate([], %{}) == {:validation_failure, :not_enough_args}
assert @command.validate(["insufficient"], %{}) == {:validation_failure, :not_enough_args} assert @command.validate(["insufficient"], %{}) == {:validation_failure, :not_enough_args}
@ -76,21 +82,11 @@ defmodule SetPermissionsCommandTest do
test "run: An invalid rabbitmq node throws a badrpc" do test "run: An invalid rabbitmq node throws a badrpc" do
target = :jake@thedog target = :jake@thedog
:net_kernel.connect_node(target) :net_kernel.connect_node(target)
opts = %{node: target} opts = %{node: target, vhost: @vhost}
assert @command.run([@user, ".*", ".*", ".*"], opts) == {:badrpc, :nodedown} assert @command.run([@user, ".*", ".*", ".*"], opts) == {:badrpc, :nodedown}
end end
@tag user: @user, vhost: @root
test "run: a well-formed command with no vhost runs against the default", context do
assert @command.run(
[context[:user], "^#{context[:user]}-.*", ".*", ".*"],
context[:opts]
) == :ok
assert List.first(list_permissions(context[:vhost]))[:configure] == "^#{context[:user]}-.*"
end
@tag user: "interloper", vhost: @root @tag user: "interloper", vhost: @root
test "run: an invalid user returns a no-such-user error", context do test "run: an invalid user returns a no-such-user error", context do
assert @command.run( assert @command.run(

View File

@ -43,6 +43,11 @@ defmodule TraceOffCommandTest do
{:ok, opts: %{node: get_rabbit_hostname, vhost: context[:vhost]}} {:ok, opts: %{node: get_rabbit_hostname, vhost: context[:vhost]}}
end end
test "merge_defaults: defaults can be overridden" do
assert @command.merge_defaults([], %{}) == {[], %{vhost: "/"}}
assert @command.merge_defaults([], %{vhost: "non_default"}) == {[], %{vhost: "non_default"}}
end
test "validate: wrong number of arguments triggers arg count error" do test "validate: wrong number of arguments triggers arg count error" do
assert @command.validate(["extra"], %{}) == {:validation_failure, :too_many_args} assert @command.validate(["extra"], %{}) == {:validation_failure, :too_many_args}
end end

View File

@ -51,6 +51,11 @@ defmodule TraceOnCommandTest do
trace_off(@default_vhost) trace_off(@default_vhost)
end end
test "merge_defaults: defaults can be overridden" do
assert @command.merge_defaults([], %{}) == {[], %{vhost: "/"}}
assert @command.merge_defaults([], %{vhost: "non_default"}) == {[], %{vhost: "non_default"}}
end
test "validate: wrong number of arguments triggers arg count error" do test "validate: wrong number of arguments triggers arg count error" do
assert @command.validate(["extra"], %{}) == {:validation_failure, :too_many_args} assert @command.validate(["extra"], %{}) == {:validation_failure, :too_many_args}
end end