Skip to content

Commit a100cba

Browse files
the-mikedavismergify[bot]
authored andcommitted
Test peer discovery cleanup
(cherry picked from commit f11198a)
1 parent f94fc25 commit a100cba

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
%% This Source Code Form is subject to the terms of the Mozilla Public
2+
%% License, v. 2.0. If a copy of the MPL was not distributed with this
3+
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
%%
5+
%% Copyright (c) 2007-2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
6+
%%
7+
8+
-module(peer_discovery_cleanup_SUITE).
9+
10+
-compile([export_all, nowarn_export_all]).
11+
12+
-include_lib("common_test/include/ct.hrl").
13+
-include_lib("eunit/include/eunit.hrl").
14+
-include_lib("amqp_client/include/amqp_client.hrl").
15+
-include_lib("rabbitmq_ct_helpers/include/rabbit_assert.hrl").
16+
17+
all() ->
18+
[
19+
{group, all_tests}
20+
].
21+
22+
groups() ->
23+
[
24+
{all_tests, [], all_tests()}
25+
].
26+
27+
all_tests() ->
28+
[
29+
cleanup_queues
30+
].
31+
32+
%% -------------------------------------------------------------------
33+
%% Testsuite setup/teardown.
34+
%% -------------------------------------------------------------------
35+
36+
37+
init_per_suite(Config) ->
38+
rabbit_ct_helpers:log_environment(),
39+
rabbit_ct_helpers:run_setup_steps(
40+
Config,
41+
[fun rabbit_ct_broker_helpers:configure_dist_proxy/1]).
42+
43+
end_per_suite(Config) ->
44+
rabbit_ct_helpers:run_teardown_steps(Config).
45+
46+
init_per_group(_Group, Config) ->
47+
Config.
48+
49+
end_per_group(_Group, Config) ->
50+
Config.
51+
52+
init_per_testcase(Testcase, Config) ->
53+
Hostname = <<(atom_to_binary(Testcase, utf8))/binary,
54+
("." ?MODULE_STRING ".local")>>,
55+
Env = [{rabbit,
56+
[{cluster_formation,
57+
[{peer_discovery_backend, rabbit_peer_discovery_dns},
58+
{peer_discovery_dns, [{hostname, Hostname}]},
59+
%% Enable cleanup but set the interval high. Test cases should
60+
%% call rabbit_peer_discovery_cleanup:check_cluster/0 to force
61+
%% cleanup evaluation.
62+
{node_cleanup, [{cleanup_interval, 3600},
63+
{cleanup_only_log_warning, false}]}]}]}],
64+
Config1 = rabbit_ct_helpers:merge_app_env(Config, Env),
65+
Config2 = rabbit_ct_helpers:set_config(Config1,
66+
[
67+
{rmq_nodes_count, 3},
68+
{rmq_nodes_clustered, false},
69+
{rmq_nodename_suffix, Testcase},
70+
{net_ticktime, 5}
71+
]),
72+
rabbit_ct_helpers:testcase_started(Config, Testcase),
73+
rabbit_ct_helpers:run_steps(Config2,
74+
rabbit_ct_broker_helpers:setup_steps() ++
75+
[fun setup_meck/1,
76+
fun mock_list_nodes/1,
77+
fun rabbit_ct_broker_helpers:cluster_nodes/1] ++
78+
rabbit_ct_client_helpers:setup_steps()).
79+
80+
end_per_testcase(Testcase, Config) ->
81+
rabbit_ct_helpers:run_steps(Config,
82+
rabbit_ct_client_helpers:teardown_steps() ++
83+
rabbit_ct_broker_helpers:teardown_steps()),
84+
rabbit_ct_helpers:testcase_finished(Config, Testcase).
85+
86+
%% ---------------------------------------------------------------------------
87+
%% Test Cases
88+
%% ---------------------------------------------------------------------------
89+
90+
cleanup_queues(Config) ->
91+
%% Happy path: unreachable nodes not recognized by the backend are cleaned
92+
%% up.
93+
[A, B, C] = rabbit_ct_broker_helpers:get_node_configs(Config, nodename),
94+
{Conn, Ch} = rabbit_ct_client_helpers:open_connection_and_channel(Config),
95+
96+
QQ = <<"quorum-queue">>,
97+
declare_queue(Ch, QQ, [{<<"x-queue-type">>, longstr, <<"quorum">>}]),
98+
99+
%% Remove node C from peer discovery responses.
100+
mock_list_nodes(Config, {ok, [A, B]}),
101+
%% Make node C unreachable.
102+
rabbit_ct_broker_helpers:block_traffic_between(A, C),
103+
rabbit_ct_broker_helpers:block_traffic_between(B, C),
104+
Ts1 = erlang:system_time(millisecond),
105+
?awaitMatch([C],
106+
rabbit_ct_broker_helpers:rpc(Config, A,
107+
rabbit_nodes,
108+
list_unreachable, []),
109+
30_000, 1_000),
110+
ct:log(?LOW_IMPORTANCE, "Node C became unreachable in ~bms",
111+
[erlang:system_time(millisecond) - Ts1]),
112+
113+
ok = rabbit_ct_broker_helpers:rpc(Config, A,
114+
rabbit_peer_discovery_cleanup,
115+
check_cluster, []),
116+
117+
%% Node C should be removed from the quorum queue members.
118+
?assertEqual(
119+
lists:sort([A, B]),
120+
begin
121+
Info = rpc:call(A, rabbit_quorum_queue, infos,
122+
[rabbit_misc:r(<<"/">>, queue, QQ)]),
123+
lists:sort(proplists:get_value(members, Info))
124+
end),
125+
126+
%% Cleanup.
127+
ok = rabbit_ct_client_helpers:close_connection_and_channel(Conn, Ch).
128+
129+
%%%
130+
%%% Implementation
131+
%%%
132+
133+
setup_meck(Config) ->
134+
rabbit_ct_broker_helpers:setup_meck(Config),
135+
rabbit_ct_broker_helpers:rpc_all(Config, meck, new,
136+
[rabbit_peer_discovery_dns,
137+
[no_link, passthrough]]),
138+
Config.
139+
140+
mock_list_nodes(Config) ->
141+
Nodes = rabbit_ct_broker_helpers:get_node_configs(Config, nodename),
142+
mock_list_nodes(Config, {ok, Nodes}).
143+
144+
mock_list_nodes(Config, Response) ->
145+
rabbit_ct_broker_helpers:rpc_all(Config, meck, expect,
146+
[rabbit_peer_discovery_dns,
147+
list_nodes, 0, Response]),
148+
Config.
149+
150+
declare_queue(Ch, QueueName, Args)
151+
when is_pid(Ch), is_binary(QueueName), is_list(Args) ->
152+
#'queue.declare_ok'{} = amqp_channel:call(
153+
Ch, #'queue.declare'{
154+
queue = QueueName,
155+
durable = true,
156+
arguments = Args}).

0 commit comments

Comments
 (0)