Skip to content

Commit

Permalink
Adopt new helpers that assert on plugin presence and state
Browse files Browse the repository at this point in the history
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 6ebbfaa)
  • Loading branch information
michaelklishin authored and mergify[bot] committed Nov 3, 2022
1 parent 643569a commit 65d1c45
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
5 changes: 1 addition & 4 deletions deps/rabbitmq_cli/test/plugins/list_plugins_command_test.exs
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
54 changes: 49 additions & 5 deletions deps/rabbitmq_cli/test/test_helper.exs
Expand Up @@ -607,18 +607,62 @@ 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,
# this is considered acceptable.
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
Expand Down

0 comments on commit 65d1c45

Please sign in to comment.