rabbitmq-server/deps/rabbitmq_cli/test/plugins/enable_plugins_command_test...

262 lines
11 KiB
Elixir
Raw Normal View History

2016-06-23 01:00:21 +08:00
## 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.
2017-01-10 22:42:40 +08:00
## Copyright (c) 2007-2017 Pivotal Software, Inc. All rights reserved.
2016-06-23 01:00:21 +08:00
defmodule EnablePluginsCommandTest do
use ExUnit.Case, async: false
import TestHelper
@command RabbitMQ.CLI.Plugins.Commands.EnableCommand
@vhost "test1"
@user "guest"
@root "/"
@default_timeout :infinity
setup_all do
2016-11-15 01:52:08 +08:00
RabbitMQ.CLI.Core.Distribution.start()
2016-06-23 01:00:21 +08:00
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])
rabbitmq_home = :rabbit_misc.rpc_call(node, :code, :lib_dir, [:rabbit])
2016-06-23 01:00:21 +08:00
{:ok, [enabled_plugins]} = :file.consult(plugins_file)
opts = %{enabled_plugins_file: plugins_file,
plugins_dir: plugins_dir,
rabbitmq_home: rabbitmq_home,
online: true, offline: false,
all: false}
2016-06-23 01:00:21 +08:00
on_exit(fn ->
2016-12-07 01:14:40 +08:00
set_enabled_plugins(enabled_plugins, :online, get_rabbit_hostname, opts)
2016-06-23 01:00:21 +08:00
end)
:erlang.disconnect_node(node)
2016-06-23 01:00:21 +08:00
{:ok, opts: opts}
end
setup context do
:net_kernel.connect_node(get_rabbit_hostname)
2016-12-07 01:14:40 +08:00
set_enabled_plugins([:rabbitmq_stomp, :rabbitmq_federation],
:online,
get_rabbit_hostname,
2016-06-23 01:00:21 +08:00
context[:opts])
on_exit([], fn ->
:erlang.disconnect_node(get_rabbit_hostname)
end)
{
:ok,
opts: Map.merge(context[:opts], %{
node: get_rabbit_hostname,
})
}
end
test "validate: specifying both --online and --offline is reported as invalid", context do
assert match?(
{:validation_failure, {:bad_argument, _}},
@command.validate(["a"], Map.merge(context[:opts], %{online: true, offline: true}))
)
end
test "validate: not specifying a plugins to enable is reported as invalid", context do
2016-06-23 01:00:21 +08:00
assert match?(
{:validation_failure, :not_enough_arguments},
@command.validate([], Map.merge(context[:opts], %{online: true, offline: false}))
2016-06-23 01:00:21 +08:00
)
end
test "validate: not specifying an enabled_plugins_file is reported as an error", context do
2016-06-23 01:00:21 +08:00
assert @command.validate(["a"], Map.delete(context[:opts], :enabled_plugins_file)) ==
{:validation_failure, :no_plugins_file}
end
test "validate: not specifying a plugins_dir is reported as an error", context do
2016-06-23 01:00:21 +08:00
assert @command.validate(["a"], Map.delete(context[:opts], :plugins_dir)) ==
{:validation_failure, :no_plugins_dir}
end
2016-10-27 19:29:25 +08:00
test "validate: specifying a non-existent enabled_plugins_file is fine", context do
assert @command.validate(["a"], Map.merge(context[:opts], %{enabled_plugins_file: "none"})) == :ok
2016-06-23 01:00:21 +08:00
end
test "validate: specifying a non-existent plugins_dir is reported as an error", context do
2016-06-23 01:00:21 +08:00
assert @command.validate(["a"], Map.merge(context[:opts], %{plugins_dir: "none"})) ==
2016-07-11 00:47:27 +08:00
{:validation_failure, :plugins_dir_does_not_exist}
2016-06-23 01:00:21 +08:00
end
test "validate: failure to load the rabbit application is reported as an error", context do
2016-06-23 01:00:21 +08:00
assert {:validation_failure, {:unable_to_load_rabbit, _}} =
@command.validate(["a"], Map.delete(context[:opts], :rabbitmq_home))
end
test "if node is unaccessible, writes enabled plugins file and reports implicitly enabled plugin list", context do
# Clears enabled plugins file
2016-12-07 01:14:40 +08:00
set_enabled_plugins([], :offline, :nonode, context[:opts])
2016-06-23 01:00:21 +08:00
2016-12-07 20:55:00 +08:00
assert {:stream, test_stream} =
@command.run(["rabbitmq_stomp"], Map.merge(context[:opts], %{node: :nonode}))
assert [[:amqp_client, :rabbitmq_stomp],
%{mode: :offline, enabled: [:amqp_client, :rabbitmq_stomp], set: [:amqp_client, :rabbitmq_stomp]}] ==
Enum.to_list(test_stream)
check_plugins_enabled([:rabbitmq_stomp], context)
assert [:amqp_client, :rabbitmq_federation, :rabbitmq_stomp] ==
2016-08-09 23:26:05 +08:00
currently_active_plugins(context)
2016-06-23 01:00:21 +08:00
end
test "in offline mode, writes enabled plugins and reports implicitly enabled plugin list", context do
# Clears enabled plugins file
2016-12-07 01:14:40 +08:00
set_enabled_plugins([], :offline, :nonode, context[:opts])
2016-06-23 01:00:21 +08:00
2016-12-07 20:55:00 +08:00
assert {:stream, test_stream} =
@command.run(["rabbitmq_stomp"], Map.merge(context[:opts], %{offline: true, online: false}))
assert [[:amqp_client, :rabbitmq_stomp],
%{mode: :offline, enabled: [:amqp_client, :rabbitmq_stomp], set: [:amqp_client, :rabbitmq_stomp]}] ==
Enum.to_list(test_stream)
check_plugins_enabled([:rabbitmq_stomp], context)
assert_equal_sets(
[:amqp_client, :rabbitmq_federation, :rabbitmq_stomp],
currently_active_plugins(context))
2016-06-23 01:00:21 +08:00
end
test "adds additional plugins to those already enabled", context do
# Clears enabled plugins file
2016-12-07 01:14:40 +08:00
set_enabled_plugins([], :offline, :nonode, context[:opts])
2016-06-23 01:00:21 +08:00
2016-12-07 20:55:00 +08:00
assert {:stream, test_stream0} =
@command.run(["rabbitmq_stomp"], Map.merge(context[:opts], %{offline: true, online: false}))
assert [[:amqp_client, :rabbitmq_stomp],
%{mode: :offline, enabled: [:amqp_client, :rabbitmq_stomp], set: [:amqp_client, :rabbitmq_stomp]}] ==
Enum.to_list(test_stream0)
check_plugins_enabled([:rabbitmq_stomp], context)
2016-12-07 20:55:00 +08:00
assert {:stream, test_stream1} =
@command.run(["rabbitmq_federation"], Map.merge(context[:opts], %{offline: true, online: false}))
assert [[:amqp_client, :rabbitmq_federation, :rabbitmq_stomp],
%{mode: :offline, enabled: [:rabbitmq_federation],
set: [:amqp_client, :rabbitmq_federation, :rabbitmq_stomp]}] ==
Enum.to_list(test_stream1)
check_plugins_enabled([:rabbitmq_stomp, :rabbitmq_federation], context)
2016-06-23 01:00:21 +08:00
end
test "updates plugin list and starts newly enabled plugins", context do
# Clears enabled plugins file and stop all plugins
2016-12-07 01:14:40 +08:00
set_enabled_plugins([], :online, context[:opts][:node], context[:opts])
2016-06-23 01:00:21 +08:00
2016-12-07 20:55:00 +08:00
assert {:stream, test_stream0} =
@command.run(["rabbitmq_stomp"], context[:opts])
assert [[:amqp_client, :rabbitmq_stomp],
%{mode: :online,
started: [:amqp_client, :rabbitmq_stomp], stopped: [],
enabled: [:amqp_client, :rabbitmq_stomp],
set: [:amqp_client, :rabbitmq_stomp]}] ==
Enum.to_list(test_stream0)
2016-08-23 21:28:34 +08:00
check_plugins_enabled([:rabbitmq_stomp], context)
assert_equal_sets([:amqp_client, :rabbitmq_stomp], currently_active_plugins(context))
2016-06-23 01:00:21 +08:00
2016-12-07 20:55:00 +08:00
{:stream, test_stream1} =
@command.run(["rabbitmq_federation"], context[:opts])
assert [[:amqp_client, :rabbitmq_federation, :rabbitmq_stomp],
%{mode: :online,
started: [:rabbitmq_federation], stopped: [],
enabled: [:rabbitmq_federation],
set: [:amqp_client, :rabbitmq_federation, :rabbitmq_stomp]}] ==
Enum.to_list(test_stream1)
check_plugins_enabled([:rabbitmq_stomp, :rabbitmq_federation], context)
assert_equal_sets([:amqp_client, :rabbitmq_federation, :rabbitmq_stomp], currently_active_plugins(context))
2016-06-23 01:00:21 +08:00
end
test "can enable multiple plugins at once", context do
# Clears plugins file and stop all plugins
2016-12-07 01:14:40 +08:00
set_enabled_plugins([], :online, context[:opts][:node], context[:opts])
2016-06-23 01:00:21 +08:00
2016-12-07 20:55:00 +08:00
assert {:stream, test_stream} =
@command.run(["rabbitmq_stomp", "rabbitmq_federation"], context[:opts])
assert [[:amqp_client, :rabbitmq_federation, :rabbitmq_stomp],
%{mode: :online,
started: [:amqp_client, :rabbitmq_federation, :rabbitmq_stomp], stopped: [],
enabled: [:amqp_client, :rabbitmq_federation, :rabbitmq_stomp],
set: [:amqp_client, :rabbitmq_federation, :rabbitmq_stomp]}] ==
Enum.to_list(test_stream)
check_plugins_enabled([:rabbitmq_stomp, :rabbitmq_federation], context)
assert_equal_sets([:amqp_client, :rabbitmq_federation, :rabbitmq_stomp], currently_active_plugins(context))
2016-06-23 01:00:21 +08:00
end
test "does not enable an already implicitly enabled plugin", context do
# Clears enabled plugins file and stop all plugins
2016-12-07 01:14:40 +08:00
set_enabled_plugins([:rabbitmq_federation], :online, context[:opts][:node], context[:opts])
2016-12-07 20:55:00 +08:00
assert {:stream, test_stream} =
@command.run(["amqp_client"], context[:opts])
assert [[:amqp_client, :rabbitmq_federation],
%{mode: :online,
started: [], stopped: [],
enabled: [],
set: [:amqp_client, :rabbitmq_federation]}] ==
Enum.to_list(test_stream)
check_plugins_enabled([:rabbitmq_federation], context)
assert [:amqp_client, :rabbitmq_federation] ==
2016-08-09 23:26:05 +08:00
currently_active_plugins(context)
2016-06-23 01:00:21 +08:00
end
test "validation: does not enable plugins with unmet version requirements", context do
set_enabled_plugins([], :online, context[:opts][:node], context[:opts])
plugins_directory = fixture_plugins_path("plugins_with_version_requirements")
opts = get_opts_with_plugins_directories(context, [plugins_directory])
switch_plugins_directories(context[:opts][:plugins_dir], opts[:plugins_dir])
:ok = @command.validate(["mock_rabbitmq_plugin_for_3_7"], opts)
{:validation_failure, _version_error} =
@command.validate(["mock_rabbitmq_plugin_for_3_8"], opts)
end
test "validation: does not enable plugins with unmet version requirements even when enabling all plugins", context do
set_enabled_plugins([], :online, context[:opts][:node], context[:opts])
plugins_directory = fixture_plugins_path("plugins_with_version_requirements")
opts = get_opts_with_plugins_directories(context, [plugins_directory])
opts = Map.merge(opts, %{all: true})
switch_plugins_directories(context[:opts][:plugins_dir], opts[:plugins_dir])
{:validation_failure, _version_error} = @command.validate([], opts)
end
defp check_plugins_enabled(plugins, context) do
{:ok, [xs]} = :file.consult(context[:opts][:enabled_plugins_file])
assert_equal_sets(plugins, xs)
end
defp assert_equal_sets(a, b) do
asorted = Enum.sort(a)
bsorted = Enum.sort(b)
assert asorted == bsorted
end
2016-06-23 01:00:21 +08:00
end