New command: rabbitmq-plugins directories

Closes rabbitmq/rabbitmq-cli#261.

[#160792758]
This commit is contained in:
Michael Klishin 2018-10-02 03:16:25 +03:00
parent fa5d020caf
commit 1b56fc5957
2 changed files with 249 additions and 0 deletions

View File

@ -0,0 +1,119 @@
## 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 Pivotal Software, Inc.
## Copyright (c) 2007-2018 Pivotal Software, Inc. All rights reserved.
defmodule RabbitMQ.CLI.Plugins.Commands.DirectoriesCommand do
alias RabbitMQ.CLI.Plugins.Helpers, as: PluginHelpers
alias RabbitMQ.CLI.Core.{Helpers, Validators}
@behaviour RabbitMQ.CLI.CommandBehaviour
def formatter(), do: RabbitMQ.CLI.Formatters.String
def merge_defaults(args, opts) do
{args, Map.merge(%{online: true, offline: false}, opts)}
end
def distribution(%{offline: true}), do: :none
def distribution(%{offline: false}), do: :cli
def switches(), do: [online: :boolean,
offline: :boolean]
def validate(_, %{online: true, offline: true}) do
{:validation_failure, {:bad_argument, "Cannot set both online and offline"}}
end
def validate(_, %{online: false, offline: false}) do
{:validation_failure, {:bad_argument, "Cannot set online and offline to false"}}
end
def validate([_ | _], _) do
{:validation_failure, :too_many_args}
end
def validate([], _) do
:ok
end
def validate_execution_environment(args, %{offline: true} = opts) do
Validators.chain([&Helpers.require_rabbit_and_plugins/2,
&PluginHelpers.enabled_plugins_file/2,
&Helpers.plugins_dir/2],
[args, opts])
end
def validate_execution_environment(args, %{offline: false} = opts) do
Validators.node_is_running(args, opts)
end
def validate_execution_environment(args, %{online: true} = opts) do
Validators.node_is_running(args, opts)
end
def usage, do: "directories [--offline] [--online]"
def banner([], %{online: false, offline: true}) do
"Listing plugin directories..."
end
def banner([], %{online: true, offline: false, node: node}) do
"Listing plugin directories used by node #{node}"
end
def run([], %{online: true, node: node_name}) do
do_run fn(key) ->
:rabbit_misc.rpc_call(node_name, :rabbit_plugins, key, [])
end
end
def run([], %{offline: false, node: node_name}) do
do_run fn(key) ->
:rabbit_misc.rpc_call(node_name, :rabbit_plugins, key, [])
end
end
def run([], %{offline: true}) do
do_run fn(key) ->
apply(:rabbit_plugins, key, [])
end
end
def output({:ok, _map} = res, %{formatter: "json"}) do
res
end
def output({:ok, map}, _opts) do
s = """
Plugin archives directory: #{Map.get(map, :plugins_dist_dir)}
Plugin expansion directory: #{Map.get(map, :plugins_expand_dir)}
Enabled plugins file: #{Map.get(map, :enabled_plugins_file)}
"""
{:ok, String.trim_trailing(s)}
end
def output({:error, err}, _opts) do
{:error, RabbitMQ.CLI.Core.ExitCodes.exit_software, err}
end
use RabbitMQ.CLI.DefaultOutput
defp do_run(fun) do
# return an error or an {:ok, map} tuple
Enum.reduce([:plugins_dist_dir, :plugins_expand_dir, :enabled_plugins_file], {:ok, %{}},
fn _, {:error, err} -> {:error, err}
key, {:ok, acc} ->
case fun.(key) do
{:error, err} -> {:error, err}
val -> {:ok, Map.put(acc, key, :rabbit_data_coercion.to_binary(val))}
end
end)
end
end

View File

@ -0,0 +1,130 @@
## 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-2017 Pivotal Software, Inc. All rights reserved.
defmodule DirectoriesCommandTest do
use ExUnit.Case, async: false
import TestHelper
@command RabbitMQ.CLI.Plugins.Commands.DirectoriesCommand
setup_all do
RabbitMQ.CLI.Core.Distribution.start()
node = get_rabbit_hostname()
{: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, plugins_expand_dir} = :rabbit_misc.rpc_call(node,
:application, :get_env,
[:rabbit, :plugins_expand_dir])
rabbitmq_home = :rabbit_misc.rpc_call(node, :code, :lib_dir, [:rabbit])
{:ok, opts: %{
plugins_file: plugins_file,
plugins_dir: plugins_dir,
plugins_expand_dir: plugins_expand_dir,
rabbitmq_home: rabbitmq_home,
}}
end
setup context do
{
:ok,
opts: Map.merge(context[:opts], %{
node: get_rabbit_hostname(),
timeout: 1000
})
}
end
test "validate: providing no arguments passes validation", context do
assert @command.validate([], context[:opts]) == :ok
end
test "validate: providing --online passes validation", context do
assert @command.validate([], Map.merge(%{online: true}, context[:opts])) == :ok
end
test "validate: providing --offline passes validation", context do
assert @command.validate([], Map.merge(%{offline: true}, context[:opts])) == :ok
end
test "validate: providing any arguments fails validation", context do
assert @command.validate(["a", "b", "c"], context[:opts]) ==
{:validation_failure, :too_many_args}
end
test "validate: setting both --online and --offline to false fails validation", context do
assert @command.validate([], Map.merge(context[:opts], %{online: false, offline: false})) ==
{:validation_failure, {:bad_argument, "Cannot set online and offline to false"}}
end
test "validate: setting both --online and --offline to true fails validation", context do
assert @command.validate([], Map.merge(context[:opts], %{online: true, offline: true})) ==
{:validation_failure, {:bad_argument, "Cannot set both online and offline"}}
end
test "validate_execution_environment: when --offline is used, not specifying an enabled_plugins_file fails validation", context do
opts = context[:opts] |> Map.merge(%{offline: true}) |> Map.delete(:enabled_plugins_file)
assert @command.validate_execution_environment([], opts) == {:validation_failure, :no_plugins_file}
end
test "validate_execution_environment: when --offline is used, not specifying a plugins_dir fails validation", context do
opts = context[:opts] |> Map.merge(%{offline: true}) |> Map.delete(:plugins_dir)
assert @command.validate_execution_environment([], opts) == {:validation_failure, :no_plugins_dir}
end
test "validate_execution_environment: when --offline is used, specifying a non-existent enabled_plugins_file passes validation", context do
opts = context[:opts] |> Map.merge(%{offline: true, enabled_plugins_file: "none"})
assert @command.validate_execution_environment([], opts) == :ok
end
test "validate_execution_environment: when --offline is used, specifying a non-existent plugins_dir fails validation", context do
opts = context[:opts] |> Map.merge(%{offline: true, plugins_dir: "none"})
assert @command.validate_execution_environment([], opts) == {:validation_failure, :plugins_dir_does_not_exist}
end
test "validate_execution_environment: when --online is used, not specifying an enabled_plugins_file passes validation", context do
opts = context[:opts] |> Map.merge(%{online: true}) |> Map.delete(:enabled_plugins_file)
assert @command.validate_execution_environment([], opts) == :ok
end
test "validate_execution_environment: when --online is used, not specifying a plugins_dir passes validation", context do
opts = context[:opts] |> Map.merge(%{online: true}) |> Map.delete(:plugins_dir)
assert @command.validate_execution_environment([], opts) == :ok
end
test "validate_execution_environment: when --online is used, specifying a non-existent enabled_plugins_file passes validation", context do
opts = context[:opts] |> Map.merge(%{online: true, enabled_plugins_file: "none"})
assert @command.validate_execution_environment([], opts) == :ok
end
test "validate_execution_environment: when --online is used, specifying a non-existent plugins_dir passes validation", context do
opts = context[:opts] |> Map.merge(%{online: true, plugins_dir: "none"})
assert @command.validate_execution_environment([], opts) == :ok
end
test "run: when --online is used, lists plugin directories", context do
opts = Map.merge(context[:opts], %{online: true})
assert @command.run([], opts) == {:ok, %{plugins_dist_dir: to_string(Map.get(opts, :plugins_dir)),
plugins_expand_dir: to_string(Map.get(opts, :plugins_expand_dir)),
enabled_plugins_file: to_string(Map.get(opts, :plugins_file))}}
end
end