Skip to content

Commit

Permalink
Merge pull request #12066 from rabbitmq/md/experimental-ff-ctl-fixes
Browse files Browse the repository at this point in the history
Return non-0 exit code when failing to enable an experimental feature flag
  • Loading branch information
michaelklishin authored Aug 20, 2024
2 parents 4a3fa4d + dce8135 commit 6ed8190
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.EnableFeatureFlagCommand do
def run(["all"], %{node: node_name, experimental: experimental}) do
case experimental do
true ->
{:error, RabbitMQ.CLI.Core.ExitCodes.exit_usage(), "`--experiemntal` flag is not allowed when enabling all feature flags.\nUse --experimental with a specific feature flag if you want to enable an experimental feature."}
{:error, RabbitMQ.CLI.Core.ExitCodes.exit_usage(), "`--experimental` flag is not allowed when enabling all feature flags.\nUse --experimental with a specific feature flag if you want to enable an experimental feature."}
false ->
case :rabbit_misc.rpc_call(node_name, :rabbit_feature_flags, :enable_all, []) do
{:badrpc, _} = err -> err
Expand All @@ -40,7 +40,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.EnableFeatureFlagCommand do
])} do
{_, {:badrpc, _} = err} -> err
{false, :experimental} ->
IO.puts("Feature flag #{feature_flag} is experimental. If you understand the risk, use --experimental to enable it.")
{:error, RabbitMQ.CLI.Core.ExitCodes.exit_usage(), "Feature flag #{feature_flag} is experimental. If you understand the risk, use --experimental to enable it."}
_ ->
case :rabbit_misc.rpc_call(node_name, :rabbit_feature_flags, :enable, [
String.to_atom(feature_flag)
Expand Down
27 changes: 26 additions & 1 deletion deps/rabbitmq_cli/test/ctl/enable_feature_flag_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ defmodule EnableFeatureFlagCommandTest do

@command RabbitMQ.CLI.Ctl.Commands.EnableFeatureFlagCommand
@feature_flag :ff_from_enable_ff_testsuite
@experimental_flag :ff_from_enable_ff_testsuite_experimental
@usage_exit_code RabbitMQ.CLI.Core.ExitCodes.exit_usage()

setup_all do
RabbitMQ.CLI.Core.Distribution.start()
Expand All @@ -22,6 +24,11 @@ defmodule EnableFeatureFlagCommandTest do
desc: ~c"My feature flag",
provided_by: :EnableFeatureFlagCommandTest,
stability: :stable
},
@experimental_flag => %{
desc: ~c"An **experimental** feature!",
provided_by: :EnableFeatureFlagCommandTest,
stability: :experimental
}
}

Expand All @@ -35,7 +42,9 @@ defmodule EnableFeatureFlagCommandTest do

{
:ok,
opts: %{node: get_rabbit_hostname(), experimental: false}, feature_flag: @feature_flag
opts: %{node: get_rabbit_hostname(), experimental: false},
feature_flag: @feature_flag,
experimental_flag: @experimental_flag
}
end

Expand Down Expand Up @@ -63,6 +72,16 @@ defmodule EnableFeatureFlagCommandTest do
assert match?({:badrpc, _}, @command.run(["na"], opts))
end

test "run: enabling an experimental flag requires '--experimental'", context do
experimental_flag = Atom.to_string(context[:experimental_flag])
assert match?(
{:error, @usage_exit_code, _},
@command.run([experimental_flag], context[:opts])
)
opts = Map.put(context[:opts], :experimental, true)
assert @command.run([experimental_flag], opts) == :ok
end

test "run: enabling the same feature flag twice is idempotent", context do
enable_feature_flag(context[:feature_flag])
assert @command.run([Atom.to_string(context[:feature_flag])], context[:opts]) == :ok
Expand All @@ -75,6 +94,12 @@ defmodule EnableFeatureFlagCommandTest do
assert list_feature_flags(:enabled) |> Map.has_key?(context[:feature_flag])
end

test "run: enabling all feature flags with '--experimental' returns an error", context do
enable_feature_flag(context[:feature_flag])
opts = Map.put(context[:opts], :experimental, true)
assert match?({:error, @usage_exit_code, _}, @command.run(["all"], opts))
end

test "banner", context do
assert @command.banner([context[:feature_flag]], context[:opts]) =~
~r/Enabling feature flag \"#{context[:feature_flag]}\" \.\.\./
Expand Down

0 comments on commit 6ed8190

Please sign in to comment.