|
| 1 | +%% The contents of this file are subject to the Mozilla Public License |
| 2 | +%% Version 1.1 (the "License"); you may not use this file except in |
| 3 | +%% compliance with the License. You may obtain a copy of the License at |
| 4 | +%% http://www.mozilla.org/MPL/ |
| 5 | +%% |
| 6 | +%% Software distributed under the License is distributed on an "AS IS" |
| 7 | +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
| 8 | +%% License for the specific language governing rights and limitations |
| 9 | +%% under the License. |
| 10 | +%% |
| 11 | +%% The Original Code is RabbitMQ. |
| 12 | +%% |
| 13 | +%% The Initial Developer of the Original Code is GoPivotal, Inc. |
| 14 | +%% Copyright (c) 2016 Pivotal Software, Inc. All rights reserved. |
| 15 | +%% |
| 16 | + |
| 17 | +-module(config_schema_SUITE). |
| 18 | + |
| 19 | +-include_lib("common_test/include/ct.hrl"). |
| 20 | +-include_lib("amqp_client/include/amqp_client.hrl"). |
| 21 | + |
| 22 | +-compile(export_all). |
| 23 | + |
| 24 | +all() -> |
| 25 | + [ |
| 26 | + {group, non_parallel_tests} |
| 27 | + ]. |
| 28 | + |
| 29 | +groups() -> |
| 30 | + [ |
| 31 | + {non_parallel_tests, [], [ |
| 32 | + run_snippets |
| 33 | + ]} |
| 34 | + ]. |
| 35 | + |
| 36 | +%% ------------------------------------------------------------------- |
| 37 | +%% Testsuite setup/teardown. |
| 38 | +%% ------------------------------------------------------------------- |
| 39 | + |
| 40 | +init_per_suite(Config) -> |
| 41 | + rabbit_ct_helpers:log_environment(), |
| 42 | + rabbit_ct_helpers:run_setup_steps(Config). |
| 43 | + |
| 44 | +end_per_suite(Config) -> |
| 45 | + rabbit_ct_helpers:run_teardown_steps(Config). |
| 46 | + |
| 47 | +init_per_group(_, Config) -> |
| 48 | + Config. |
| 49 | + |
| 50 | +end_per_group(_, Config) -> |
| 51 | + Config. |
| 52 | + |
| 53 | +init_per_testcase(Testcase, Config) -> |
| 54 | + rabbit_ct_helpers:testcase_started(Config, Testcase), |
| 55 | + Config1 = rabbit_ct_helpers:set_config(Config, [ |
| 56 | + {rmq_nodename_suffix, Testcase} |
| 57 | + ]), |
| 58 | + Config2 = case Testcase of |
| 59 | + run_snippets -> |
| 60 | + SchemaDir = filename:join(?config(priv_dir, Config1), "schema"), |
| 61 | + ResultsDir = filename:join(?config(priv_dir, Config1), "results"), |
| 62 | + Snippets = filename:join(?config(data_dir, Config1), |
| 63 | + "snippets.config"), |
| 64 | + ok = file:make_dir(SchemaDir), |
| 65 | + ok = file:make_dir(ResultsDir), |
| 66 | + rabbit_ct_helpers:set_config(Config1, [ |
| 67 | + {schema_dir, SchemaDir}, |
| 68 | + {results_dir, ResultsDir}, |
| 69 | + {conf_snippets, Snippets} |
| 70 | + ]) |
| 71 | + end, |
| 72 | + rabbit_ct_helpers:run_steps(Config2, |
| 73 | + rabbit_ct_broker_helpers:setup_steps() ++ |
| 74 | + rabbit_ct_client_helpers:setup_steps()). |
| 75 | + |
| 76 | +end_per_testcase(Testcase, Config) -> |
| 77 | + Config1 = rabbit_ct_helpers:run_steps(Config, |
| 78 | + rabbit_ct_client_helpers:teardown_steps() ++ |
| 79 | + rabbit_ct_broker_helpers:teardown_steps()), |
| 80 | + rabbit_ct_helpers:testcase_finished(Config1, Testcase). |
| 81 | + |
| 82 | +%% ------------------------------------------------------------------- |
| 83 | +%% Testcases. |
| 84 | +%% ------------------------------------------------------------------- |
| 85 | + |
| 86 | +run_snippets(Config) -> |
| 87 | + passed = rabbit_ct_broker_helpers:rpc(Config, 0, |
| 88 | + ?MODULE, run_snippets1, [Config]). |
| 89 | + |
| 90 | +run_snippets1(Config) -> |
| 91 | + prepare_plugin_schemas(Config), |
| 92 | + {ok, [Snippets]} = file:consult(?config(conf_snippets, Config)), |
| 93 | + lists:map( |
| 94 | + fun({N, S, C, P}) -> ok = test_snippet(Config, {integer_to_list(N), S, []}, C, P); |
| 95 | + ({N, S, A, C, P}) -> ok = test_snippet(Config, {integer_to_list(N), S, A}, C, P) |
| 96 | + end, |
| 97 | + Snippets), |
| 98 | + passed. |
| 99 | + |
| 100 | +test_snippet(Config, Snippet, Expected, _Plugins) -> |
| 101 | + {ConfFile, AdvancedFile} = write_snippet(Config, Snippet), |
| 102 | + {ok, GeneratedFile} = generate_config(Config, ConfFile, AdvancedFile), |
| 103 | + {ok, [Generated]} = file:consult(GeneratedFile), |
| 104 | + Gen = deepsort(Generated), |
| 105 | + Exp = deepsort(Expected), |
| 106 | + case Exp of |
| 107 | + Gen -> ok; |
| 108 | + _ -> |
| 109 | + error({config_mismatch, Snippet, Exp, Gen}) |
| 110 | + end. |
| 111 | + |
| 112 | +write_snippet(Config, {Name, Conf, Advanced}) -> |
| 113 | + ResultsDir = ?config(results_dir, Config), |
| 114 | + file:make_dir(filename:join(ResultsDir, Name)), |
| 115 | + ConfFile = filename:join([ResultsDir, Name, "config.conf"]), |
| 116 | + AdvancedFile = filename:join([ResultsDir, Name, "advanced.config"]), |
| 117 | + |
| 118 | + file:write_file(ConfFile, Conf), |
| 119 | + rabbit_file:write_term_file(AdvancedFile, [Advanced]), |
| 120 | + {ConfFile, AdvancedFile}. |
| 121 | + |
| 122 | +generate_config(Config, ConfFile, AdvancedFile) -> |
| 123 | + SchemaDir = ?config(schema_dir, Config), |
| 124 | + ResultsDir = ?config(results_dir, Config), |
| 125 | + Rabbitmqctl = ?config(rabbitmqctl_cmd, Config), |
| 126 | + ScriptDir = filename:dirname(Rabbitmqctl), |
| 127 | + ct:pal("ConfFile=~p ScriptDir=~p SchemaDir=~p AdvancedFile=~p", [ConfFile, ScriptDir, SchemaDir, AdvancedFile]), |
| 128 | + rabbit_config:generate_config_file([ConfFile], ResultsDir, ScriptDir, |
| 129 | + SchemaDir, AdvancedFile). |
| 130 | + |
| 131 | +prepare_plugin_schemas(Config) -> |
| 132 | + SchemaDir = ?config(schema_dir, Config), |
| 133 | + DepsDir = ?config(erlang_mk_depsdir, Config), |
| 134 | + Files = filelib:wildcard( |
| 135 | + filename:join(DepsDir, "*/priv/schema/*.schema")), |
| 136 | + [ file:copy(File, filename:join([SchemaDir, filename:basename(File)])) |
| 137 | + || File <- Files ]. |
| 138 | + |
| 139 | +deepsort(List) -> |
| 140 | + case is_proplist(List) of |
| 141 | + true -> |
| 142 | + lists:keysort(1, lists:map(fun({K, V}) -> {K, deepsort(V)}; |
| 143 | + (V) -> V end, |
| 144 | + List)); |
| 145 | + false -> |
| 146 | + case is_list(List) of |
| 147 | + true -> lists:sort(List); |
| 148 | + false -> List |
| 149 | + end |
| 150 | + end. |
| 151 | + |
| 152 | +is_proplist([{_Key, _Val}|_] = List) -> lists:all(fun({_K, _V}) -> true; (_) -> false end, List); |
| 153 | +is_proplist(_) -> false. |
0 commit comments