|
| 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-2024 Broadcom. All Rights Reserved. The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. All rights reserved. |
| 6 | +%% |
| 7 | + |
| 8 | +%% Test suite for the prevent_startup_if_node_was_reset feature. |
| 9 | +%% This feature helps detect potential data loss scenarios by maintaining |
| 10 | +%% a marker file to track if a node has been initialized before. |
| 11 | + |
| 12 | +-module(prevent_startup_if_node_was_reset_SUITE). |
| 13 | + |
| 14 | +-include_lib("common_test/include/ct.hrl"). |
| 15 | +-include_lib("eunit/include/eunit.hrl"). |
| 16 | + |
| 17 | +-compile(export_all). |
| 18 | + |
| 19 | +all() -> |
| 20 | + [ |
| 21 | + {group, single_node_mnesia}, |
| 22 | + {group, single_node_khepri} |
| 23 | + ]. |
| 24 | + |
| 25 | +groups() -> |
| 26 | + [ |
| 27 | + {single_node_mnesia, [], [ |
| 28 | + prevent_startup_if_node_was_reset_disabled, |
| 29 | + prevent_startup_if_node_was_reset_enabled |
| 30 | + ]}, |
| 31 | + {single_node_khepri, [], [ |
| 32 | + prevent_startup_if_node_was_reset_disabled, |
| 33 | + prevent_startup_if_node_was_reset_enabled |
| 34 | + ]} |
| 35 | + ]. |
| 36 | + |
| 37 | +%% ------------------------------------------------------------------- |
| 38 | +%% Testsuite setup/teardown. |
| 39 | +%% ------------------------------------------------------------------- |
| 40 | + |
| 41 | +init_per_suite(Config) -> |
| 42 | + rabbit_ct_helpers:log_environment(), |
| 43 | + rabbit_ct_helpers:run_setup_steps(Config). |
| 44 | + |
| 45 | +end_per_suite(Config) -> |
| 46 | + rabbit_ct_helpers:run_teardown_steps(Config). |
| 47 | + |
| 48 | +init_per_group(Groupname, Config) -> |
| 49 | + Config0 = rabbit_ct_helpers:set_config(Config, [ |
| 50 | + {metadata_store, meta_store(Groupname)}, |
| 51 | + {rmq_nodes_clustered, false}, |
| 52 | + {rmq_nodename_suffix, Groupname}, |
| 53 | + {rmq_nodes_count, 1} |
| 54 | + ]), |
| 55 | + rabbit_ct_helpers:run_steps( |
| 56 | + Config0, |
| 57 | + rabbit_ct_broker_helpers:setup_steps() ++ |
| 58 | + rabbit_ct_client_helpers:setup_steps() |
| 59 | + ). |
| 60 | + |
| 61 | +end_per_group(_, Config) -> |
| 62 | + rabbit_ct_helpers:run_steps( |
| 63 | + Config, |
| 64 | + rabbit_ct_client_helpers:teardown_steps() ++ |
| 65 | + rabbit_ct_broker_helpers:teardown_steps() |
| 66 | + ). |
| 67 | + |
| 68 | +init_per_testcase(Testcase, Config) -> |
| 69 | + rabbit_ct_helpers:testcase_started(Config, Testcase), |
| 70 | + Config. |
| 71 | + |
| 72 | +end_per_testcase(Testcase, Config) -> |
| 73 | + rabbit_ct_helpers:testcase_finished(Config, Testcase). |
| 74 | + |
| 75 | +%% ------------------------------------------------------------------- |
| 76 | +%% Test cases |
| 77 | +%% ------------------------------------------------------------------- |
| 78 | + |
| 79 | +prevent_startup_if_node_was_reset_disabled(Config) -> |
| 80 | + % When feature is disabled (default), node should start normally |
| 81 | + DataDir = rabbit_ct_broker_helpers:get_node_config(Config, 0, data_dir), |
| 82 | + MarkerFile = filename:join(DataDir, "node_initialized.marker"), |
| 83 | + % Setting is disabled so no marker file should be present |
| 84 | + ?assertNot(filelib:is_file(MarkerFile)), |
| 85 | + |
| 86 | + % Restarting the node should work fine |
| 87 | + ok = stop_app(Config), |
| 88 | + set_env(Config, false), |
| 89 | + ok = start_app(Config), |
| 90 | + % Still no marker file |
| 91 | + ?assertNot(filelib:is_file(MarkerFile)), |
| 92 | + ok. |
| 93 | + |
| 94 | +prevent_startup_if_node_was_reset_enabled(Config) -> |
| 95 | + DataDir = rabbit_ct_broker_helpers:get_node_config(Config, 0, data_dir), |
| 96 | + MarkerFile = filename:join(DataDir, "node_initialized.marker"), |
| 97 | + |
| 98 | + ok = stop_app(Config), |
| 99 | + set_env(Config, true), |
| 100 | + ok = start_app(Config), |
| 101 | + % Setting is enabled so marker file should be present after initial startup |
| 102 | + ?assert(filelib:is_file(MarkerFile)), |
| 103 | + |
| 104 | + % Restarting the node should be fine, as there is a marker file |
| 105 | + % and corresponding schema data (consistent state) |
| 106 | + |
| 107 | + ok = stop_app(Config), |
| 108 | + ok = start_app(Config), |
| 109 | + |
| 110 | + SchemaFile = schema_file(Config), |
| 111 | + |
| 112 | + ?assert(filelib:is_file(MarkerFile)), |
| 113 | + |
| 114 | + % Stop the node and remove the present schema to simulate data loss |
| 115 | + ok = stop_app(Config), |
| 116 | + file:delete(SchemaFile), |
| 117 | + % Node should fail to start because marker exists but schema is missing, |
| 118 | + % indicating potential data loss or corruption |
| 119 | + ?assertMatch( |
| 120 | + {error, 69, _}, |
| 121 | + start_app(Config) |
| 122 | + ), |
| 123 | + ok. |
| 124 | + |
| 125 | +%% ------------------------------------------------------------------- |
| 126 | +%% Internal helpers |
| 127 | +%% ------------------------------------------------------------------- |
| 128 | + |
| 129 | +stop_app(Config) -> |
| 130 | + Node = rabbit_ct_broker_helpers:get_node_config(Config, 0, nodename), |
| 131 | + case rabbit_ct_broker_helpers:rabbitmqctl(Config, Node, ["stop_app"]) of |
| 132 | + {ok, _} -> ok; |
| 133 | + Error -> Error |
| 134 | + end. |
| 135 | + |
| 136 | +start_app(Config) -> |
| 137 | + Node = rabbit_ct_broker_helpers:get_node_config(Config, 0, nodename), |
| 138 | + case rabbit_ct_broker_helpers:rabbitmqctl(Config, Node, ["start_app"]) of |
| 139 | + {ok, _} -> ok; |
| 140 | + Error -> Error |
| 141 | + end. |
| 142 | + |
| 143 | +maybe_enable_prevent_startup_if_node_was_reset(Config, prevent_startup_if_node_was_reset_enabled) -> |
| 144 | + rabbit_ct_helpers:merge_app_env( |
| 145 | + Config, {rabbit, [{prevent_startup_if_node_was_reset, true}]} |
| 146 | + ); |
| 147 | +maybe_enable_prevent_startup_if_node_was_reset(Config, _) -> |
| 148 | + Config. |
| 149 | + |
| 150 | +meta_store(single_node_mnesia) -> |
| 151 | + mnesia; |
| 152 | +meta_store(single_node_khepri) -> |
| 153 | + khepri. |
| 154 | + |
| 155 | +schema_file(Config) -> |
| 156 | + DataDir = rabbit_ct_broker_helpers:get_node_config(Config, 0, data_dir), |
| 157 | + MetaStore = rabbit_ct_helpers:get_config(Config, metadata_store), |
| 158 | + case MetaStore of |
| 159 | + mnesia -> |
| 160 | + filename:join(DataDir, "schema.DAT"); |
| 161 | + khepri -> |
| 162 | + NodeName = rabbit_ct_broker_helpers:get_node_config(Config, 0, nodename), |
| 163 | + filename:join([DataDir, "coordination", NodeName, "names.dets"]) |
| 164 | + end. |
| 165 | + |
| 166 | +set_env(Config, Bool) -> |
| 167 | + Node = rabbit_ct_broker_helpers:get_node_config(Config, 0, nodename), |
| 168 | + ok = rpc:call(Node, application, set_env, [rabbit, prevent_startup_if_node_was_reset, Bool]). |
0 commit comments