From 65d1c452a03a5512af0f051b5e00baee808b09f4 Mon Sep 17 00:00:00 2001 From: Michael Klishin Date: Wed, 2 Nov 2022 14:07:10 +0400 Subject: [PATCH] Adopt new helpers that assert on plugin presence and state In many tests, we do not care what is the complete set of plugins running on a node (or present, or in a specific state). We only care that a few select ones are running, are of the expected version, in a certain state, and so on. So list comparison assertions are counterproductive and lead to test interference that is difficult to track down. In many cases we can do more fine grained assertions and ignore the rest of the plugins present on the node. References #6289, #6020. (cherry picked from commit 6ebbfaa0a40a11fda3b887af61b788e420a7322a) --- .../plugins/list_plugins_command_test.exs | 5 +- deps/rabbitmq_cli/test/test_helper.exs | 54 +++++++++++++++++-- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/deps/rabbitmq_cli/test/plugins/list_plugins_command_test.exs b/deps/rabbitmq_cli/test/plugins/list_plugins_command_test.exs index 6a594e083691..b1aadfa2851e 100644 --- a/deps/rabbitmq_cli/test/plugins/list_plugins_command_test.exs +++ b/deps/rabbitmq_cli/test/plugins/list_plugins_command_test.exs @@ -179,8 +179,7 @@ defmodule ListPluginsCommandTest do plugins: actual_plugins } = @command.run([".*"], Map.merge(context[:opts], %{verbose: true})) - assert_plugin_states(expected_plugins, actual_plugins) - assert Enum.all?(actual_plugins, fn p -> Keyword.fetch(p, :description) != :error end) + assert_plugin_states(actual_plugins, expected_plugins) end test "run: reports plugin names in minimal mode", context do @@ -373,8 +372,6 @@ defmodule ListPluginsCommandTest do plugins: actual_plugins } = @command.run([".*"], opts) - IO.inspect(actual_plugins) - IO.inspect(expected_plugins) assert_plugin_states(actual_plugins, expected_plugins) end diff --git a/deps/rabbitmq_cli/test/test_helper.exs b/deps/rabbitmq_cli/test/test_helper.exs index 93f7538c213a..a736041569dd 100644 --- a/deps/rabbitmq_cli/test/test_helper.exs +++ b/deps/rabbitmq_cli/test/test_helper.exs @@ -607,7 +607,20 @@ defmodule TestHelper do end # Asserts that for every expected plugin, there is an actually - # present plugin on the node that has the same name, version, + # present plugin on the node that has the same name. + # + # If there are more activated plugins than expected, + # this is considered acceptable. + def assert_plugin_presence(actual, expected) do + assert Enum.all?(expected, fn ep -> + Enum.find(actual, false, fn ap -> + ap[:name] == ep[:name] + end) + end) + end + + # Asserts that for every expected plugin, there is an actually + # present plugin on the node that has the same name, # activation state and running/stopped state. # # If there are more activated plugins than expected, @@ -615,10 +628,41 @@ defmodule TestHelper do def assert_plugin_states(actual, expected) do assert Enum.all?(expected, fn ep -> Enum.find(actual, false, fn ap -> - ap.name == ep.name and - ap.version == ep.version and - ap.enabled == ep.enabled and - ap.running == ep.running + ap[:name] == ep[:name] and + ap[:enabled] == ep[:enabled] and + ap[:running] == ep[:running] + end) + end) + end + + # Asserts that for every expected plugin, there is an actually + # present plugin on the node that has the same name, version, + # activation state. + # + # If there are more activated plugins than expected, + # this is considered acceptable. + def assert_plugin_states_and_versions(actual, expected) do + assert Enum.all?(expected, fn ep -> + Enum.find(actual, false, fn ap -> + ap[:name] == ep[:name] and + ap[:version] == ep[:version] and + ap[:enabled] == ep[:enabled] + end) + end) + end + + # Asserts that for every expected plugin, there is an actually + # present plugin on the node that has the same name, dependencies, + # activation state. + # + # If there are more activated plugins than expected, + # this is considered acceptable. + def assert_plugin_states_and_dependencies(actual, expected) do + assert Enum.all?(expected, fn ep -> + Enum.find(actual, false, fn ap -> + ap[:name] == ep[:name] and + Enum.sort(ap[:dependencies]) == Enum.sort(ep[:dependencies]) and + ap[:enabled] == ep[:enabled] end) end) end