|
| 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 | +-module(definition_import_SUITE). |
| 9 | + |
| 10 | +-include_lib("rabbitmq_ct_helpers/include/rabbit_assert.hrl"). |
| 11 | +-include_lib("common_test/include/ct.hrl"). |
| 12 | +-include_lib("eunit/include/eunit.hrl"). |
| 13 | + |
| 14 | +-compile(export_all). |
| 15 | + |
| 16 | +all() -> |
| 17 | + [ |
| 18 | + {group, roundtrip} |
| 19 | + ]. |
| 20 | + |
| 21 | +groups() -> |
| 22 | + [ |
| 23 | + {roundtrip, [], [ |
| 24 | + export_import_round_trip |
| 25 | + ]} |
| 26 | + ]. |
| 27 | + |
| 28 | +%% ------------------------------------------------------------------- |
| 29 | +%% Test suite setup/teardown. |
| 30 | +%% ------------------------------------------------------------------- |
| 31 | + |
| 32 | +init_per_suite(Config) -> |
| 33 | + rabbit_ct_helpers:log_environment(), |
| 34 | + inets:start(), |
| 35 | + Config. |
| 36 | +end_per_suite(Config) -> |
| 37 | + Config. |
| 38 | + |
| 39 | +init_per_group(Group, Config) -> |
| 40 | + Config1 = rabbit_ct_helpers:set_config(Config, [ |
| 41 | + {rmq_nodename_suffix, Group} |
| 42 | + ]), |
| 43 | + rabbit_ct_helpers:run_setup_steps(Config1, rabbit_ct_broker_helpers:setup_steps()). |
| 44 | + |
| 45 | +end_per_group(_, Config) -> |
| 46 | + rabbit_ct_helpers:run_teardown_steps(Config, rabbit_ct_broker_helpers:teardown_steps()). |
| 47 | + |
| 48 | +init_per_testcase(Testcase, Config) -> |
| 49 | + rabbit_ct_helpers:testcase_started(Config, Testcase). |
| 50 | + |
| 51 | +end_per_testcase(Testcase, Config) -> |
| 52 | + rabbit_ct_helpers:testcase_finished(Config, Testcase). |
| 53 | + |
| 54 | +%% |
| 55 | +%% Tests |
| 56 | +%% |
| 57 | + |
| 58 | +export_import_round_trip(Config) -> |
| 59 | + case rabbit_ct_helpers:is_mixed_versions() of |
| 60 | + false -> |
| 61 | + import_file_case(Config, "case1"), |
| 62 | + Defs = export(Config), |
| 63 | + import_raw(Config, rabbit_json:encode(Defs)); |
| 64 | + _ -> |
| 65 | + %% skip the test in mixed version mode |
| 66 | + {skip, "Should not run in mixed version environments"} |
| 67 | + end. |
| 68 | + |
| 69 | +%% |
| 70 | +%% Implementation |
| 71 | +%% |
| 72 | + |
| 73 | +import_file_case(Config, CaseName) -> |
| 74 | + CasePath = filename:join([ |
| 75 | + ?config(data_dir, Config), |
| 76 | + CaseName ++ ".json" |
| 77 | + ]), |
| 78 | + rabbit_ct_broker_helpers:rpc(Config, 0, ?MODULE, run_import_case, [CasePath]), |
| 79 | + ok. |
| 80 | + |
| 81 | + |
| 82 | +import_raw(Config, Body) -> |
| 83 | + case rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_definitions, import_raw, [Body]) of |
| 84 | + ok -> ok; |
| 85 | + {error, E} -> |
| 86 | + ct:pal("Import of JSON definitions ~tp failed: ~tp~n", [Body, E]), |
| 87 | + ct:fail({expected_failure, Body, E}) |
| 88 | + end. |
| 89 | + |
| 90 | +export(Config) -> |
| 91 | + rabbit_ct_broker_helpers:rpc(Config, 0, ?MODULE, run_export, []). |
| 92 | + |
| 93 | +run_export() -> |
| 94 | + rabbit_definitions:all_definitions(). |
| 95 | + |
| 96 | +run_directory_import_case(Path, Expected) -> |
| 97 | + ct:pal("Will load definitions from files under ~tp~n", [Path]), |
| 98 | + Result = rabbit_definitions:maybe_load_definitions_from(true, Path), |
| 99 | + case Expected of |
| 100 | + ok -> |
| 101 | + ok = Result; |
| 102 | + error -> |
| 103 | + ?assertMatch({error, {failed_to_import_definitions, _, _}}, Result) |
| 104 | + end. |
| 105 | + |
| 106 | +run_import_case(Path) -> |
| 107 | + {ok, Body} = file:read_file(Path), |
| 108 | + ct:pal("Successfully loaded a definition to import from ~tp~n", [Path]), |
| 109 | + case rabbit_definitions:import_raw(Body) of |
| 110 | + ok -> ok; |
| 111 | + {error, E} -> |
| 112 | + ct:pal("Import case ~tp failed: ~tp~n", [Path, E]), |
| 113 | + ct:fail({expected_failure, Path, E}) |
| 114 | + end. |
| 115 | + |
| 116 | +run_invalid_import_case(Path) -> |
| 117 | + {ok, Body} = file:read_file(Path), |
| 118 | + ct:pal("Successfully loaded a definition file at ~tp~n", [Path]), |
| 119 | + case rabbit_definitions:import_raw(Body) of |
| 120 | + ok -> |
| 121 | + ct:pal("Expected import case ~tp to fail~n", [Path]), |
| 122 | + ct:fail({expected_failure, Path}); |
| 123 | + {error, _E} -> ok |
| 124 | + end. |
| 125 | + |
| 126 | +run_invalid_import_case_if_unchanged(Path) -> |
| 127 | + Mod = rabbit_definitions_import_local_filesystem, |
| 128 | + ct:pal("Successfully loaded a definition to import from ~tp~n", [Path]), |
| 129 | + case rabbit_definitions:maybe_load_definitions_from_local_filesystem_if_unchanged(Mod, false, Path) of |
| 130 | + ok -> |
| 131 | + ct:pal("Expected import case ~tp to fail~n", [Path]), |
| 132 | + ct:fail({expected_failure, Path}); |
| 133 | + {error, _E} -> ok |
| 134 | + end. |
| 135 | + |
| 136 | +queue_lookup(Config, VHost, Name) -> |
| 137 | + rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_amqqueue, lookup, [rabbit_misc:r(VHost, queue, Name)]). |
| 138 | + |
| 139 | +vhost_lookup(Config, VHost) -> |
| 140 | + rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_vhost, lookup, [VHost]). |
| 141 | + |
| 142 | +user_lookup(Config, User) -> |
| 143 | + rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_auth_backend_internal, lookup_user, [User]). |
| 144 | + |
| 145 | +delete_vhost(Config, VHost) -> |
| 146 | + rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_vhost, delete, [VHost, <<"CT tests">>]). |
0 commit comments