CLI: tests and refactoring for 'diagnostics check_if_cluster_has_classic_queue_mirroring_policy'

This commit is contained in:
Michael Klishin 2023-11-06 01:51:23 -05:00
parent 827b495d1a
commit cbe2756cbd
2 changed files with 84 additions and 10 deletions

View File

@ -44,29 +44,35 @@ defmodule RabbitMQ.CLI.Diagnostics.Commands.CheckIfClusterHasClassicQueueMirrori
case {policies, op_policies} do
{[], []} ->
:ok
true
{_, _} when is_list(policies) and is_list(op_policies) ->
{:ok, policies, op_policies}
{false, policies, op_policies}
{{:badrpc, _} = left, _} ->
left
{_, {:badrpc, _} = right} ->
right
other ->
other
end
end
def output(:ok, %{formatter: "json"}) do
def output(true, %{formatter: "json"}) do
{:ok, %{"result" => "ok"}}
end
def output(:ok, %{silent: true}) do
def output(true, %{silent: true}) do
{:ok, :check_passed}
end
def output(:ok, %{}) do
{:ok, "Cluster reported no policies enabling classic queue mirroring"}
def output(true, %{}) do
{:ok, "Cluster reported no policies that enable classic queue mirroring"}
end
def output({:ok, ps, op_ps}, %{formatter: "json"})
def output({false, ps, op_ps}, %{formatter: "json"})
when is_list(ps) and is_list(op_ps) do
{:error, :check_failed,
%{
@ -77,17 +83,16 @@ defmodule RabbitMQ.CLI.Diagnostics.Commands.CheckIfClusterHasClassicQueueMirrori
}}
end
def output({:ok, ps, op_ps}, %{silent: true}) when is_list(ps) and is_list(op_ps) do
def output({false, ps, op_ps}, %{silent: true}) when is_list(ps) and is_list(op_ps) do
{:error, :check_failed}
end
def output({:ok, ps, op_ps}, _) when is_list(ps) and is_list(op_ps) do
def output({false, ps, op_ps}, _) when is_list(ps) and is_list(op_ps) do
lines = policy_lines(ps)
op_lines = op_policy_lines(op_ps)
{:error, :check_failed, Enum.join(Enum.concat(lines, op_lines), line_separator())}
end
use RabbitMQ.CLI.DefaultOutput
def help_section(), do: :observability_and_health_checks

View File

@ -0,0 +1,69 @@
## 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/.
##
## Copyright (c) 2007-2023 VMware, Inc. or its affiliates. All rights reserved.
defmodule CheckIfClusterHasClassicQueueMirroringPolicyCommandTest do
use ExUnit.Case, async: false
import TestHelper
@command RabbitMQ.CLI.Diagnostics.Commands.CheckIfClusterHasClassicQueueMirroringPolicyCommand
@policy_name "cmq-policy-8373"
@policy_value "{\"ha-mode\":\"all\"}"
setup_all do
RabbitMQ.CLI.Core.Distribution.start()
start_rabbitmq_app()
on_exit([], fn ->
start_rabbitmq_app()
clear_policy("/", @policy_name)
end)
:ok
end
setup context do
{:ok,
opts: %{
node: get_rabbit_hostname(),
timeout: context[:test_timeout] || 30000
}}
end
test "merge_defaults: nothing to do" do
assert @command.merge_defaults([], %{}) == {[], %{}}
end
test "validate: treats positional arguments as a failure" do
assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args}
end
test "validate: treats empty positional arguments and default switches as a success" do
assert @command.validate([], %{}) == :ok
end
@tag test_timeout: 3000
test "run: targeting an unreachable node throws a badrpc", context do
assert match?(
{:badrpc, _},
@command.run([], Map.merge(context[:opts], %{node: :jake@thedog}))
)
end
test "run: when there are no policies that enable CMQ mirroring, reports success", context do
clear_policy("/", @policy_name)
assert @command.run([], context[:opts])
end
test "output: when the result is true, returns successfully", context do
assert match?({:ok, _}, @command.output(true, context[:opts]))
end
# this is a check command
test "output: when the result is false, returns an error", context do
assert match?({:error, _}, @command.output(false, context[:opts]))
end
end