rabbitmq-server/deps/rabbitmq_cli/test/ctl/clear_parameter_command_tes...

139 lines
4.4 KiB
Elixir
Raw Normal View History

## This Source Code Form is subject to the terms of the Mozilla Public
## License, v. 2.0. If a copy of the MPL was not distributed with this
## file, You can obtain one at https://mozilla.org/MPL/2.0/.
2016-05-21 01:00:25 +08:00
##
2020-03-10 22:39:56 +08:00
## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved.
2016-05-21 01:00:25 +08:00
defmodule ClearParameterCommandTest do
use ExUnit.Case, async: false
import TestHelper
@command RabbitMQ.CLI.Ctl.Commands.ClearParameterCommand
2016-05-21 01:00:25 +08:00
@vhost "test1"
@root "/"
@component_name "federation-upstream"
@key "reconnect-delay"
@value "{\"uri\":\"amqp://127.0.0.1:5672\"}"
2016-05-21 01:00:25 +08:00
setup_all do
2016-11-15 01:52:08 +08:00
RabbitMQ.CLI.Core.Distribution.start()
2016-05-21 01:00:25 +08:00
add_vhost @vhost
enable_federation_plugin()
2016-05-21 01:00:25 +08:00
on_exit([], fn ->
delete_vhost @vhost
end)
:ok
end
setup context do
on_exit(context, fn ->
clear_parameter context[:vhost], context[:component_name], context[:key]
end)
{
:ok,
opts: %{
2017-01-24 20:33:48 +08:00
node: get_rabbit_hostname()
2016-05-21 01:00:25 +08:00
}
}
end
test "merge_defaults: adds default vhost if missing" do
2016-06-01 22:07:51 +08:00
assert @command.merge_defaults([], %{}) == {[], %{vhost: "/"}}
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
2016-08-23 21:28:34 +08:00
assert @command.validate(["one", "two"], %{}) == :ok
2016-06-01 22:07:51 +08:00
assert @command.validate([], %{}) == {:validation_failure, :not_enough_args}
assert @command.validate(["insufficient"], %{}) == {:validation_failure, :not_enough_args}
assert @command.validate(["this", "is", "many"], %{}) == {:validation_failure, :too_many_args}
2016-05-21 01:00:25 +08:00
end
@tag component_name: @component_name, key: @key, vhost: @vhost
test "run: returns error, if parameter does not exist", context do
2016-05-23 17:22:25 +08:00
vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]})
2016-05-21 01:00:25 +08:00
2016-06-01 22:07:51 +08:00
assert @command.run(
[context[:component_name], context[:key]],
vhost_opts
) == {:error_string, 'Parameter does not exist'}
2016-05-21 01:00:25 +08:00
end
2016-12-17 02:16:07 +08:00
test "run: throws a badrpc when instructed to contact an unreachable RabbitMQ node" do
opts = %{node: :jake@thedog, vhost: "/", timeout: 200}
assert match?({:badrpc, _}, @command.run([@component_name, @key], opts))
2016-05-21 01:00:25 +08:00
end
@tag component_name: @component_name, key: @key, vhost: @vhost
test "run: returns ok and clears parameter, if it exists", context do
2016-05-23 17:22:25 +08:00
vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]})
2016-05-21 01:00:25 +08:00
set_parameter(context[:vhost], context[:component_name], context[:key], @value)
2016-06-01 22:07:51 +08:00
assert @command.run(
[context[:component_name], context[:key]],
vhost_opts
) == :ok
2016-05-21 01:00:25 +08:00
assert_parameter_empty(context)
end
@tag component_name: "bad-component-name", key: @key, value: @value, vhost: @root
test "run: an invalid component_name returns a 'parameter does not exist' error", context do
vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]})
2016-06-01 22:07:51 +08:00
assert @command.run(
[context[:component_name], context[:key]],
vhost_opts
) == {:error_string, 'Parameter does not exist'}
2016-05-21 01:00:25 +08:00
assert list_parameters(context[:vhost]) == []
end
@tag component_name: @component_name, key: @key, value: @value, vhost: "bad-vhost"
test "run: an invalid vhost returns a 'parameter does not exist' error", context do
2016-05-23 17:22:25 +08:00
vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]})
2016-05-21 01:00:25 +08:00
2016-06-01 22:07:51 +08:00
assert @command.run(
[context[:component_name], context[:key]],
vhost_opts
) == {:error_string, 'Parameter does not exist'}
2016-05-21 01:00:25 +08:00
end
@tag component_name: @component_name, key: @key, value: @value, vhost: @vhost
test "banner", context do
2016-05-23 17:22:25 +08:00
vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]})
2016-05-21 01:00:25 +08:00
set_parameter(context[:vhost], context[:component_name], context[:key], @value)
2016-06-01 22:07:51 +08:00
s = @command.banner(
[context[:component_name], context[:key]],
vhost_opts
)
2016-06-01 22:07:51 +08:00
assert s =~ ~r/Clearing runtime parameter/
assert s =~ ~r/"#{context[:key]}"/
assert s =~ ~r/"#{context[:component_name]}"/
assert s =~ ~r/"#{context[:vhost]}"/
2016-05-21 01:00:25 +08:00
end
defp assert_parameter_empty(context) do
parameter = context[:vhost]
|> list_parameters
|> Enum.filter(fn(param) ->
param[:component_name] == context[:component_name] and
param[:key] == context[:key]
end)
assert parameter === []
end
end