tests for list plugins command

This commit is contained in:
Daniil Fedotov 2016-06-21 18:11:46 +01:00
parent 2415ee58aa
commit 963f6a2ff9
5 changed files with 146 additions and 19 deletions

View File

@ -28,9 +28,12 @@ defmodule RabbitMQ.CLI.Plugins.Commands.ListCommand do
def switches(), do: [verbose: :boolean,
minimal: :boolean,
enabled: :boolean,
'implicitly-enabled': :boolean]
implicitly_enabled: :boolean,
rabbitmq_home: :string,
enabled_plugins_file: :string,
plugins_dir: :string]
def aliases(), do: [v: :verbose, m: :minimal,
'E': :enabled, e: :'implicitly-enabled']
'E': :enabled, e: :implicitly_enabled]
def validate(args, _) when length(args) > 1 do
{:validation_failure, :too_many_args}
@ -44,7 +47,7 @@ defmodule RabbitMQ.CLI.Plugins.Commands.ListCommand do
:ok
|> validate_step(fn() -> PluginHelpers.require_rabbit(opts) end)
|> validate_step(fn() -> PluginHelpers.enabled_plugins_file(opts) end)
|> validate_step(fn() -> PluginHelpers.plugins_dist_dir(opts) end)
|> validate_step(fn() -> PluginHelpers.plugins_dir(opts) end)
end
def validate_step(:ok, step) do
@ -66,7 +69,7 @@ defmodule RabbitMQ.CLI.Plugins.Commands.ListCommand do
def run([pattern], %{node: node_name} = opts) do
%{verbose: verbose, minimal: minimal,
enabled: only_enabled,
'implicitly-enabled': all_enabled} = opts
implicitly_enabled: all_enabled} = opts
all = PluginHelpers.list(opts)
enabled = PluginHelpers.read_enabled(opts)
@ -152,7 +155,7 @@ defmodule RabbitMQ.CLI.Plugins.Commands.ListCommand do
defp default_opts() do
%{minimal: false, verbose: false,
enabled: false, 'implicitly-enabled': false}
enabled: false, implicitly_enabled: false}
end
end

View File

@ -16,14 +16,14 @@
defmodule RabbitMQ.CLI.Plugins.Helpers do
def list(opts) do
{:ok, dir} = plugins_dist_dir(opts)
{:ok, dir} = plugins_dir(opts)
add_all_to_path(dir)
:rabbit_plugins.list(String.to_char_list(dir))
:rabbit_plugins.list(to_char_list(dir))
end
def read_enabled(opts) do
{:ok, enabled} = enabled_plugins_file(opts)
:rabbit_plugins.read_enabled(String.to_char_list(enabled))
:rabbit_plugins.read_enabled(to_char_list(enabled))
end
def enabled_plugins_file(opts) do
@ -37,25 +37,30 @@ defmodule RabbitMQ.CLI.Plugins.Helpers do
end
end
def plugins_dist_dir(opts) do
case opts[:plugins_dist_dir] || System.get_env("RABBITMQ_PLUGINS_DIR") do
def plugins_dir(opts) do
case opts[:plugins_dir] || System.get_env("RABBITMQ_PLUGINS_DIR") do
nil -> {:error, :no_plugins_dir};
dir ->
case File.dir?(dir) do
true -> {:ok, dir};
false -> {:error, :plugins_dist_dir_not_exists}
false -> {:error, :plugins_dir_not_exists}
end
end
end
def require_rabbit(opts) do
home = opts[:rabbitmq_home] || System.get_env("RABBITMQ_HOME")
path = :filename.join(home, "ebin")
Code.append_path(path)
case Application.load(:rabbit) do
:ok -> :ok;
{:error, {:already_loaded, :rabbit}} -> :ok;
{:error, err} -> {:error, {:unable_to_load_rabbit, err}}
case home do
nil ->
{:error, {:unable_to_load_rabbit, :no_rabbit_home}};
_ ->
path = Path.join(home, "ebin")
Code.append_path(path)
case Application.load(:rabbit) do
:ok -> :ok;
{:error, {:already_loaded, :rabbit}} -> :ok;
{:error, err} -> {:error, {:unable_to_load_rabbit, err}}
end
end
end
@ -84,7 +89,7 @@ defmodule RabbitMQ.CLI.Plugins.Helpers do
all = list(opts)
case plugins -- all do
[] ->
case :rabbit_file.write_term_file(String.to_char_list(plugins_file), [plugins]) do
case :rabbit_file.write_term_file(to_char_list(plugins_file), [plugins]) do
:ok ->
{:ok, :rabbit_plugins.dependencies(false, plugins, all)};
{:error, reason} ->

View File

@ -173,7 +173,6 @@ defmodule RabbitMQCtl do
defp print_standard_messages({:bad_option, _} = result, unparsed_command) do
{[cmd | _], _, _} = parse(unparsed_command)
IO.puts "Error: invalid options for this command."
IO.puts "Given:\n\t#{unparsed_command |> Enum.join(" ")}"
HelpCommand.run([cmd], %{})

View File

@ -56,6 +56,13 @@ defmodule RabbitMQCtl.MixfileBase do
make = find_gnu_make()
[
# {
# :rabbit,
# git: "https://github.com/rabbitmq/rabbitmq-server.git",
# branch: "master",
# compile: make,
# only: :test
# },
{
:rabbit_common,
git: "https://github.com/rabbitmq/rabbitmq-common.git",

View File

@ -0,0 +1,113 @@
defmodule ListPluginsCommandTest do
use ExUnit.Case, async: false
import TestHelper
@command RabbitMQ.CLI.Plugins.Commands.ListCommand
@vhost "test1"
@user "guest"
@root "/"
@default_timeout :infinity
#RABBITMQ_PLUGINS_DIR=~/dev/master/deps RABBITMQ_ENABLED_PLUGINS_FILE=/var/folders/cl/jnydxpf92rg76z05m12hlly80000gq/T/rabbitmq-test-instances/rabbit/enabled_plugins RABBITMQ_HOME=~/dev/master/deps/rabbit ./rabbitmq-plugins list_plugins
setup_all do
RabbitMQ.CLI.Distribution.start()
node = get_rabbit_hostname
:net_kernel.connect_node(node)
{:ok, plugins_file} = :rabbit_misc.rpc_call(node,
:application, :get_env,
[:rabbit, :enabled_plugins_file])
{:ok, plugins_dir} = :rabbit_misc.rpc_call(node,
:application, :get_env,
[:rabbit, :plugins_dir])
{:ok, rabbitmq_home} = :rabbit_misc.rpc_call(node, :file, :get_cwd, [])
:erlang.disconnect_node(node)
:net_kernel.stop()
{:ok, opts: %{enabled_plugins_file: plugins_file,
plugins_dir: plugins_dir,
rabbitmq_home: rabbitmq_home,
minimal: false, verbose: false,
enabled: false, implicitly_enabled: false}}
end
setup context do
RabbitMQ.CLI.Distribution.start()
:net_kernel.connect_node(get_rabbit_hostname)
on_exit([], fn ->
:erlang.disconnect_node(get_rabbit_hostname)
:net_kernel.stop()
end)
{
:ok,
opts: Map.merge(context[:opts], %{
node: get_rabbit_hostname,
})
}
end
test "validate: specifying both --minimal and --verbose is reported as invalid", context do
assert match?(
{:validation_failure, {:bad_argument, _}},
@command.validate([], Map.merge(context[:opts], %{minimal: true, verbose: true}))
)
end
test "validate: specifying multiple patterns is reported as an error", context do
assert @command.validate(["a", "b", "c"], context[:opts]) ==
{:validation_failure, :too_many_args}
end
test "validate: specifying multiple patterns is reported as an error", context do
assert @command.validate(["a", "b", "c"], context[:opts]) ==
{:validation_failure, :too_many_args}
end
test "validate: not specifying enabled_plugins_file is reported as an error", context do
assert @command.validate(["a"], Map.delete(context[:opts], :enabled_plugins_file)) ==
{:validation_failure, :no_plugins_file}
end
test "validate: not specifying plugins_dir is reported as an error", context do
assert @command.validate(["a"], Map.delete(context[:opts], :plugins_dir)) ==
{:validation_failure, :no_plugins_dir}
end
test "validate: specifying non existent enabled_plugins_file is reported as an error", context do
assert @command.validate(["a"], Map.merge(context[:opts], %{enabled_plugins_file: "none"})) ==
{:validation_failure, :plugins_file_not_exists}
end
test "validate: specifying non existent plugins_dir is reported as an error", context do
assert @command.validate(["a"], Map.merge(context[:opts], %{plugins_dir: "none"})) ==
{:validation_failure, :plugins_dir_not_exists}
end
test "validate: failure to load rabbit application is reported as an error", context do
assert {:validation_failure, {:unable_to_load_rabbit, _}} =
@command.validate(["a"], Map.delete(context[:opts], :rabbitmq_home))
end
test "will report list of plugins from file for stopped node", context do
assert @command.run([".*"], Map.merge(context[:opts], %{node: :nonode, minimal: true})) ==
%{status: :node_down, plugins: [:amqp_client, :rabbitmq_federation, :rabbitmq_metronome]}
end
test "will report list of started plugins for started node", context do
node = context[:opts][:node]
:ok = :rabbit_misc.rpc_call(node, :application, :stop, [:rabbitmq_metronome])
assert %{status: :running,
plugins: [%{name: :amqp_client, enabled: :enabled, running: true},
%{name: :rabbitmq_federation, enabled: :enabled, running: true},
%{name: :rabbitmq_metronome, enabled: :enabled, running: false}]} =
@command.run([".*"], context[:opts])
end
end