Skip to content

Commit b558316

Browse files
Refactor this test in attempt to help a scoping issue on Concourse
(cherry picked from commit 83ae131)
1 parent 4ae24a8 commit b558316

File tree

2 files changed

+45
-42
lines changed

2 files changed

+45
-42
lines changed

deps/rabbitmq_cli/test/ctl/close_all_user_connections_command_test.exs

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22
## License, v. 2.0. If a copy of the MPL was not distributed with this
33
## file, You can obtain one at https://mozilla.org/MPL/2.0/.
44
##
5-
## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved.
5+
## Copyright (c) 2007-2021 VMware, Inc. or its affiliates. All rights reserved.
66

77
defmodule CloseAllUserConnectionsCommandTest do
88
use ExUnit.Case, async: false
99
import TestHelper
1010

11-
# alias RabbitMQ.CLI.Ctl.RpcStream
12-
13-
@helpers RabbitMQ.CLI.Core.Helpers
14-
1511
@command RabbitMQ.CLI.Ctl.Commands.CloseAllUserConnectionsCommand
1612

13+
@vhost "/"
14+
1715
setup_all do
1816
RabbitMQ.CLI.Core.Distribution.start()
1917

@@ -26,6 +24,13 @@ defmodule CloseAllUserConnectionsCommandTest do
2624
:ok
2725
end
2826

27+
setup context do
28+
{:ok, opts: %{
29+
node: get_rabbit_hostname(),
30+
timeout: context[:test_timeout] || 30000
31+
}}
32+
end
33+
2934
test "validate: with an invalid number of arguments returns an arg count error", context do
3035
assert @command.validate(["username", "explanation", "extra"], context[:opts]) ==
3136
{:validation_failure, :too_many_args}
@@ -39,42 +44,40 @@ defmodule CloseAllUserConnectionsCommandTest do
3944
end
4045

4146
test "run: a close connections request on a user with open connections", context do
42-
with_connection("/", fn _ ->
43-
node = @helpers.normalise_node(context[:node], :shortnames)
44-
await_condition(fn ->
45-
conns = fetch_user_connections("guest", context)
46-
length(conns) > 0
47-
end, 10000)
48-
49-
# make sure there is a connection to close
47+
Application.ensure_all_started(:amqp)
48+
# open a localhost connection with default username
49+
{:ok, _conn} = AMQP.Connection.open(virtual_host: @vhost)
50+
51+
await_condition(fn ->
5052
conns = fetch_user_connections("guest", context)
51-
assert length(conns) > 0
52-
53-
# make sure closing yeti's connections doesn't affect guest's connections
54-
assert :ok == @command.run(["yeti", "test"], %{node: node})
55-
Process.sleep(500)
53+
length(conns) > 0
54+
end, 10000)
55+
56+
# make sure there is a connection to close
57+
conns = fetch_user_connections("guest", context)
58+
assert length(conns) > 0
59+
60+
# make sure closing yeti's connections doesn't affect guest's connections
61+
assert :ok == @command.run(["yeti", "test"], context[:opts])
62+
Process.sleep(500)
63+
conns = fetch_user_connections("guest", context)
64+
assert length(conns) > 0
65+
66+
# finally, make sure we can close guest's connections
67+
assert :ok == @command.run(["guest", "test"], context[:opts])
68+
await_condition(fn ->
5669
conns = fetch_user_connections("guest", context)
57-
assert length(conns) > 0
70+
length(conns) == 0
71+
end, 10000)
5872

59-
# finally, make sure we can close guest's connections
60-
assert :ok == @command.run(["guest", "test"], %{node: node})
61-
await_condition(fn ->
62-
conns = fetch_user_connections("guest", context)
63-
length(conns) == 0
64-
end, 10000)
65-
66-
conns = fetch_user_connections("guest", context)
67-
assert length(conns) == 0
68-
end)
73+
conns = fetch_user_connections("guest", context)
74+
assert length(conns) == 0
6975
end
7076

71-
test "run: a close connections request on for a non existing user returns successfully",
72-
context do
77+
test "run: a close connections request on for a non existing user returns successfully", context do
7378
assert match?(
7479
:ok,
75-
@command.run(["yeti", "test"], %{
76-
node: @helpers.normalise_node(context[:node], :shortnames)
77-
})
80+
@command.run(["yeti", "test"], context[:opts])
7881
)
7982
end
8083

@@ -84,12 +87,4 @@ defmodule CloseAllUserConnectionsCommandTest do
8487
assert s =~ ~r/user username/
8588
assert s =~ ~r/reason: some reason/
8689
end
87-
88-
defp fetch_user_connections(username, context) do
89-
node = @helpers.normalise_node(context[:node], :shortnames)
90-
91-
:rabbit_misc.rpc_call(node, :rabbit_connection_tracking, :list_of_user, [
92-
username
93-
])
94-
end
9590
end

deps/rabbitmq_cli/test/test_helper.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,14 @@ defmodule TestHelper do
359359
end
360360
end
361361

362+
def fetch_user_connections(username, context) do
363+
node = Helpers.normalise_node(context[:node], :shortnames)
364+
365+
:rabbit_misc.rpc_call(node, :rabbit_connection_tracking, :list_of_user, [
366+
username
367+
])
368+
end
369+
362370
def close_all_connections(node) do
363371
# we intentionally use connections_local/0 here because connections/0,
364372
# the cluster-wide version, loads some bits around cluster membership

0 commit comments

Comments
 (0)