diff --git a/src/config/mongoose_config.erl b/src/config/mongoose_config.erl index 1b6cbdceaa5..344e7cf95a4 100644 --- a/src/config/mongoose_config.erl +++ b/src/config/mongoose_config.erl @@ -8,15 +8,14 @@ get_opt/2, get_opt/1]). -%% Test API, do not use outside of test suites, options set here are not cleaned up by stop/0 --export([set_opt/2, +%% Test API, do not use outside of test suites +-export([set_opts/1, + get_opts/0, + erase_opts/0, + set_opt/2, unset_opt/1]). -%% Shell utilities intended for debugging and system inspection --export([config_state/0, - config_states/0]). - --ignore_xref([set_opt/2, unset_opt/1, config_state/0, config_states/0]). +-ignore_xref([set_opts/1, get_opts/0, erase_opts/0, set_opt/2, unset_opt/1]). -include("mongoose.hrl"). @@ -34,19 +33,15 @@ -spec start() -> ok. start() -> Path = get_config_path(), - State = mongoose_config_parser:parse_file(Path), - persistent_term:put(mongoose_config_state, State), - set_opts(State). + Opts = mongoose_config_parser:parse_file(Path), + set_opts(maps:from_list(Opts)). -spec stop() -> ok | {error, not_started}. stop() -> - try persistent_term:get(mongoose_config_state) of - State -> - unset_opts(State), - persistent_term:erase(mongoose_config_state), - ok - catch - _:_ -> + case erase_opts() of + true -> + ok; + false -> {error, not_started} end. @@ -62,32 +57,33 @@ get_config_path() -> end, application:get_env(mongooseim, config, DefaultPath). --spec set_opts(mongoose_config_parser:state()) -> ok. -set_opts(State) -> - Opts = mongoose_config_parser:get_opts(State), - lists:foreach(fun({Key, Value}) -> set_opt(Key, Value) end, Opts). +-spec set_opts(#{key() => value()}) -> ok. +set_opts(Opts) -> + persistent_term:put(?MODULE, Opts). --spec unset_opts(mongoose_config_parser:state()) -> ok. -unset_opts(State) -> - Opts = mongoose_config_parser:get_opts(State), - lists:foreach(fun unset_opt/1, proplists:get_keys(Opts)). +-spec get_opts() -> #{key() => value()}. +get_opts() -> + persistent_term:get(?MODULE). + +-spec erase_opts() -> boolean(). +erase_opts() -> + persistent_term:erase(?MODULE). -spec set_opt(key() | key_path(), value()) -> ok. -set_opt([Key], Value) -> - set_opt(Key, Value); -set_opt([Key | Rest], Value) -> - set_opt(Key, set_nested_opt(get_opt(Key), Rest, Value)); +set_opt(KeyPath, Value) when is_list(KeyPath) -> + Opts = persistent_term:get(?MODULE), + NewOpts = set_nested_opt(Opts, KeyPath, Value), + persistent_term:put(?MODULE, NewOpts); set_opt(Key, Value) -> - persistent_term:put({?MODULE, Key}, Value). + set_opt([Key], Value). -spec unset_opt(key() | key_path()) -> ok. -unset_opt([Key]) -> - unset_opt(Key); -unset_opt([Key | Rest]) -> - set_opt(Key, unset_nested_opt(get_opt(Key), Rest)); +unset_opt(KeyPath) when is_list(KeyPath) -> + Opts = persistent_term:get(?MODULE), + NewOpts = unset_nested_opt(Opts, KeyPath), + persistent_term:put(?MODULE, NewOpts); unset_opt(Key) -> - persistent_term:erase({?MODULE, Key}), - ok. + unset_opt([Key]). %% @doc Use instead of get_opt(Key, undefined) -spec lookup_opt(key() | key_path()) -> {ok, value()} | {error, not_found}. @@ -95,7 +91,6 @@ lookup_opt(Key) -> try get_opt(Key) of Value -> {ok, Value} catch - error:badarg -> {error, not_found}; % missing persistent term error:{badkey, _} -> {error, not_found} % missing map key end. @@ -105,39 +100,16 @@ get_opt(Key, Default) -> try get_opt(Key) catch - error:badarg -> Default; % missing persistent term error:{badkey, _} -> Default % missing map key end. %% @doc Fails if the option does not exist -spec get_opt(key() | key_path()) -> value(). -get_opt([Key | Rest]) -> - Config = persistent_term:get({?MODULE, Key}), - lists:foldl(fun maps:get/2, Config, Rest); +get_opt(KeyPath) when is_list(KeyPath) -> + Opts = persistent_term:get(?MODULE), + lists:foldl(fun maps:get/2, Opts, KeyPath); get_opt(Key) -> - persistent_term:get({?MODULE, Key}). - --spec config_state() -> mongoose_config_parser:state(). -config_state() -> - persistent_term:get(mongoose_config_state). - --spec config_states() -> [mongoose_config_parser:state()]. -config_states() -> - config_states(mongoose_cluster:all_cluster_nodes()). - --spec config_states([node()]) -> [mongoose_config_parser:state()]. -%% @doc Returns config states from all nodes in cluster -%% State from the local node comes as head of a list -config_states(Nodes) -> - {States, FailedNodes} = rpc:multicall(Nodes, ?MODULE, config_state, [], 30000), - case FailedNodes of - [] -> - States; - [_|_] -> - erlang:error(#{issue => config_state_failed, - cluster_nodes => Nodes, - failed_nodes => FailedNodes}) - end. + get_opt([Key]). %% Internal functions diff --git a/src/config/mongoose_config_parser.erl b/src/config/mongoose_config_parser.erl index 462893132d1..00b6a74df46 100644 --- a/src/config/mongoose_config_parser.erl +++ b/src/config/mongoose_config_parser.erl @@ -7,7 +7,12 @@ -export([parse_file/1]). %% state API --export([build_state/3, get_opts/1]). +-export([build_state/3]). + +%% only for tests +-export([get_opts/1]). + +-ignore_xref([get_opts/1]). -callback parse_file(FileName :: string()) -> state(). @@ -25,11 +30,12 @@ %% Parser API --spec parse_file(FileName :: string()) -> state(). +-spec parse_file(FileName :: string()) -> opts(). parse_file(FileName) -> ParserModule = parser_module(filename:extension(FileName)), - try - ParserModule:parse_file(FileName) + try ParserModule:parse_file(FileName) of + State -> + get_opts(State) catch error:{config_error, ExitMsg, Errors} -> halt_with_msg(ExitMsg, Errors) diff --git a/src/pubsub/mod_pubsub.erl b/src/pubsub/mod_pubsub.erl index 97535de3b47..9c3e5f076cf 100644 --- a/src/pubsub/mod_pubsub.erl +++ b/src/pubsub/mod_pubsub.erl @@ -4078,7 +4078,7 @@ host_to_host_type(Host) -> -spec tree(HostType :: mongooseim:host_type() | host()) -> module() | nodetree_virtual. tree(HostType) -> try gen_mod:get_module_opt(HostType, ?MODULE, nodetree) - catch error:badarg -> + catch error:{badkey, _} -> %todo remove when pubsub supports dynamic domains HT = host_to_host_type(HostType), gen_mod:get_module_opt(HT, ?MODULE, nodetree) diff --git a/test/acl_SUITE.erl b/test/acl_SUITE.erl index 73a83c3a156..9ad9d604bea 100644 --- a/test/acl_SUITE.erl +++ b/test/acl_SUITE.erl @@ -47,10 +47,11 @@ end_per_group(_Group, Config) -> Config. init_per_testcase(_TC, Config) -> + mongoose_config:set_opts(#{}), Config. end_per_testcase(_TC, _Config) -> - clean_config(). + mongoose_config:erase_opts(). host_type() -> <<"test host type">>. @@ -287,10 +288,6 @@ different_specs_matching_the_same_user(Config) -> acl(Spec) -> [maps:merge(#{match => current_domain}, Spec)]. -clean_config() -> - [persistent_term:erase(Key) || {Key = {mongoose_config, _}, _Value} <- persistent_term:get()], - ok. - given_registered_domains(Config, DomainsList) -> case proplists:get_value(dynamic_domains, Config, false) of true -> diff --git a/test/auth_dummy_SUITE.erl b/test/auth_dummy_SUITE.erl index 96f8b46162d..dbbf00dbfa1 100644 --- a/test/auth_dummy_SUITE.erl +++ b/test/auth_dummy_SUITE.erl @@ -35,13 +35,13 @@ all() -> [ init_per_suite(C) -> {ok, _} = application:ensure_all_started(jid), - mongoose_config:set_opt({auth, ?HOST_TYPE}, #{methods => [dummy], - dummy => #{base_time => 5, - variance => 10}}), + AuthOpts = #{methods => [dummy], + dummy => #{base_time => 5, variance => 10}}, + mongoose_config:set_opts(#{{auth, ?HOST_TYPE} => AuthOpts}), C. end_per_suite(_C) -> - mongoose_config:unset_opt({auth, ?HOST_TYPE}). + mongoose_config:erase_opts(). %%-------------------------------------------------------------------- %% Authentication tests diff --git a/test/auth_external_SUITE.erl b/test/auth_external_SUITE.erl index 0397681317c..6ca882c5526 100644 --- a/test/auth_external_SUITE.erl +++ b/test/auth_external_SUITE.erl @@ -79,12 +79,12 @@ given_user_registered() -> set_opts(Config) -> DataDir = ?config(data_dir, Config), - mongoose_config:set_opt({auth, ?HOST_TYPE}, - #{external => #{program => DataDir ++ "sample_external_auth.py", - instances => 1}}). + mongoose_config:set_opts(#{{auth, ?HOST_TYPE} => + #{external => #{program => DataDir ++ "sample_external_auth.py", + instances => 1}}}). unset_opts() -> - mongoose_config:unset_opt({auth, ?HOST_TYPE}). + mongoose_config:erase_opts(). gen_user() -> U = random_binary(5), diff --git a/test/auth_http_SUITE.erl b/test/auth_http_SUITE.erl index f66f9385f5d..40d8e0e35da 100644 --- a/test/auth_http_SUITE.erl +++ b/test/auth_http_SUITE.erl @@ -244,13 +244,13 @@ set_opts(Config) -> _ -> scram end, HttpOpts = #{basic_auth => ?BASIC_AUTH}, - mongoose_config:set_opt({auth, ?HOST_TYPE}, #{methods => [http], - password => #{format => PasswordFormat, - scram_iterations => 10}, - http => HttpOpts}). + mongoose_config:set_opts(#{{auth, ?HOST_TYPE} => #{methods => [http], + password => #{format => PasswordFormat, + scram_iterations => 10}, + http => HttpOpts}}). unset_opts() -> - mongoose_config:unset_opt({auth, ?HOST_TYPE}). + mongoose_config:erase_opts(). do_scram(Pass, Config) -> case lists:keyfind(scram_group, 1, Config) of diff --git a/test/auth_internal_SUITE.erl b/test/auth_internal_SUITE.erl index 81b3a9eb7ed..e611a730544 100644 --- a/test/auth_internal_SUITE.erl +++ b/test/auth_internal_SUITE.erl @@ -12,16 +12,16 @@ init_per_suite(C) -> application:ensure_all_started(jid), ok = mnesia:create_schema([node()]), ok = mnesia:start(), - mongoose_config:set_opt({auth, host_type()}, #{methods => [internal], - internal => #{}, - password => #{format => scram, - scram_iterations => 10}}), + AuthOpts = #{methods => [internal], + internal => #{}, + password => #{format => scram, scram_iterations => 10}}, + mongoose_config:set_opts(#{{auth, host_type()} => AuthOpts}), ejabberd_auth_internal:start(host_type()), C. end_per_suite(_C) -> ejabberd_auth_internal:stop(host_type()), - mongoose_config:unset_opt({auth, host_type()}), + mongoose_config:erase_opts(), mnesia:stop(), mnesia:delete_schema([node()]). diff --git a/test/auth_jwt_SUITE.erl b/test/auth_jwt_SUITE.erl index 71829c6cc15..f672818c127 100644 --- a/test/auth_jwt_SUITE.erl +++ b/test/auth_jwt_SUITE.erl @@ -124,12 +124,12 @@ check_password_succeeds_for_pubkey_signed_token(C) -> %%-------------------------------------------------------------------- set_auth_opts(Secret, Algorithm, Key) -> - mongoose_config:set_opt({auth, ?HOST_TYPE}, #{jwt => #{secret => Secret, - algorithm => Algorithm, - username_key => Key}}). + mongoose_config:set_opts(#{{auth, ?HOST_TYPE} => #{jwt => #{secret => Secret, + algorithm => Algorithm, + username_key => Key}}}). unset_auth_opts() -> - mongoose_config:unset_opt({auth, ?HOST_TYPE}). + mongoose_config:erase_opts(). generate_token(Alg, NbfDelta, Key) -> Now = erlang:system_time(second), diff --git a/test/auth_tokens_SUITE.erl b/test/auth_tokens_SUITE.erl index 4324b0c4011..9c5be975e12 100644 --- a/test/auth_tokens_SUITE.erl +++ b/test/auth_tokens_SUITE.erl @@ -41,13 +41,14 @@ groups() -> init_per_suite(C) -> {ok, _} = application:ensure_all_started(jid), - mongoose_config:set_opt({modules, host_type()}, - #{?TESTED => config_parser_helper:default_mod_config(?TESTED)}), + mongoose_config:set_opts(opts()), C. -end_per_suite(C) -> - mongoose_config:unset_opt({modules, host_type()}), - C. +end_per_suite(_C) -> + mongoose_config:erase_opts(). + +opts() -> + #{{modules, host_type()} => #{?TESTED => config_parser_helper:default_mod_config(?TESTED)}}. init_per_testcase(Test, Config) when Test =:= serialize_deserialize_property; diff --git a/test/component_reg_SUITE.erl b/test/component_reg_SUITE.erl index 6fb0d6b4914..3a68f99bb1d 100644 --- a/test/component_reg_SUITE.erl +++ b/test/component_reg_SUITE.erl @@ -13,7 +13,7 @@ init_per_suite(C) -> {ok, _} = application:ensure_all_started(jid), ok = mnesia:create_schema([node()]), ok = mnesia:start(), - [mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts()], + mongoose_config:set_opts(opts()), meck:new(mongoose_domain_api, [no_link]), meck:expect(mongoose_domain_api, get_host_type, fun(_) -> {error, not_found} end), @@ -31,13 +31,12 @@ end_per_suite(_C) -> mnesia:stop(), mnesia:delete_schema([node()]), meck:unload(), - [mongoose_config:unset_opt(Key) || {Key, _Value} <- opts()], - ok. + mongoose_config:erase_opts(). opts() -> - [{all_metrics_are_global, false}, - {component_backend, mnesia}, - {routing_modules, [xmpp_router_a, xmpp_router_b, xmpp_router_c]}]. + #{all_metrics_are_global => false, + component_backend => mnesia, + routing_modules => [xmpp_router_a, xmpp_router_b, xmpp_router_c]}. registering(_C) -> Dom = <<"aaa.bbb.com">>, diff --git a/test/config_parser_SUITE.erl b/test/config_parser_SUITE.erl index 97b09bbf2f1..9b6935c543f 100644 --- a/test/config_parser_SUITE.erl +++ b/test/config_parser_SUITE.erl @@ -3040,8 +3040,7 @@ test_config_file(Config, File) -> ExpectedOpts = config_parser_helper:options(File), TOMLPath = ejabberd_helper:data(Config, File ++ ".toml"), - State = mongoose_config_parser:parse_file(TOMLPath), - TOMLOpts = mongoose_config_parser:get_opts(State), + TOMLOpts = mongoose_config_parser:parse_file(TOMLPath), %% Save the parsed TOML options %% - for debugging diff --git a/test/ejabberd_sm_SUITE.erl b/test/ejabberd_sm_SUITE.erl index db9e66dac3a..a2631ebb89b 100644 --- a/test/ejabberd_sm_SUITE.erl +++ b/test/ejabberd_sm_SUITE.erl @@ -115,7 +115,7 @@ end_per_testcase(_, Config) -> clean_sessions(Config), terminate_sm(), unload_meck(), - unset_opts(Config). + mongoose_config:erase_opts(). open_session(C) -> {Sid, USR} = generate_random_user(<<"localhost">>), @@ -610,7 +610,7 @@ is_redis_running() -> end. setup_sm(Config) -> - set_opts(Config), + mongoose_config:set_opts(opts(Config)), set_meck(), ejabberd_sm:start_link(), case ?config(backend, Config) of @@ -625,17 +625,11 @@ setup_sm(Config) -> terminate_sm() -> gen_server:stop(ejabberd_sm). -set_opts(Config) -> - [mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts(Config)]. - -unset_opts(Config) -> - [mongoose_config:unset_opt(Key) || {Key, _Value} <- opts(Config)]. - opts(Config) -> - [{hosts, [<<"localhost">>]}, - {host_types, []}, - {all_metrics_are_global, false}, - {sm_backend, sm_backend(?config(backend, Config))}]. + #{hosts => [<<"localhost">>], + host_types => [], + all_metrics_are_global => false, + sm_backend => sm_backend(?config(backend, Config))}. sm_backend(ejabberd_sm_redis) -> redis; sm_backend(ejabberd_sm_mnesia) -> mnesia; diff --git a/test/event_pusher_sns_SUITE.erl b/test/event_pusher_sns_SUITE.erl index 43d27040d5d..36b7d152196 100644 --- a/test/event_pusher_sns_SUITE.erl +++ b/test/event_pusher_sns_SUITE.erl @@ -197,18 +197,18 @@ sns_config(_) -> common_sns_opts(). start_modules(SNSExtra) -> - [mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts(SNSExtra)], + mongoose_config:set_opts(opts(SNSExtra)), mongoose_modules:start(). stop_modules() -> mongoose_modules:stop(), - [mongoose_config:unset_opt(Key) || {Key, _} <- opts(#{})]. + mongoose_config:erase_opts(). opts(SNSExtra) -> - [{hosts, [host_type()]}, - {host_types, []}, - {all_metrics_are_global, false}, - {{modules, host_type()}, modules(SNSExtra)}]. + #{hosts => [host_type()], + host_types => [], + all_metrics_are_global => false, + {modules, host_type()} => modules(SNSExtra)}. modules(SNSExtra) -> gen_mod_deps:resolve_deps(host_type(), #{mod_event_pusher => module_opts(SNSExtra)}). diff --git a/test/gen_hook_SUITE.erl b/test/gen_hook_SUITE.erl index 7b8c10d10a0..822362f8c88 100644 --- a/test/gen_hook_SUITE.erl +++ b/test/gen_hook_SUITE.erl @@ -24,11 +24,11 @@ all() -> init_per_suite(Config) -> application:ensure_all_started(exometer_core), - mongoose_config:set_opt(all_metrics_are_global, false), + mongoose_config:set_opts(#{all_metrics_are_global => false}), Config. end_per_suite(Config) -> - mongoose_config:unset_opt(all_metrics_are_global), + mongoose_config:erase_opts(), application:stop(exometer_core), Config. diff --git a/test/gen_mod_SUITE.erl b/test/gen_mod_SUITE.erl index 038a6067d8f..16e68a5c1db 100644 --- a/test/gen_mod_SUITE.erl +++ b/test/gen_mod_SUITE.erl @@ -37,12 +37,12 @@ all() -> hosts_and_opts_with_module]. init_per_testcase(_, Config) -> - [mongoose_config:set_opt(Opt, Val) || {Opt, Val} <- opts()], + mongoose_config:set_opts(opts()), [setup_meck(Module) || Module <- [a_module, b_module]], Config. end_per_testcase(_, Config) -> - [mongoose_config:unset_opt(Opt) || Opt <- opts()], + mongoose_config:erase_opts(), [meck:unload(Module) || Module <- [a_module, b_module]], Config. @@ -113,8 +113,8 @@ setup_meck(Module) -> meck:expect(Module, stop, fun(_) -> ok end). opts() -> - [{hosts, [host(a), host(b)]}, - {host_types, []}, - {services, #{}}, - {{modules, host(a)}, #{a_module => #{}}}, - {{modules, host(b)}, #{b_module => #{k => v}}}]. + #{hosts => [host(a), host(b)], + host_types => [], + services => #{}, + {modules, host(a)} => #{a_module => #{}}, + {modules, host(b)} => #{b_module => #{k => v}}}. diff --git a/test/keystore_SUITE.erl b/test/keystore_SUITE.erl index aee458e2d86..0f1de970402 100644 --- a/test/keystore_SUITE.erl +++ b/test/keystore_SUITE.erl @@ -28,21 +28,21 @@ end_per_suite(_C) -> init_per_testcase(_, Config) -> mock_mongoose_metrics(), Config1 = async_helper:start(Config, gen_hook, start_link, []), - [mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts()], + mongoose_config:set_opts(opts()), Config1. end_per_testcase(_, C) -> mongoose_modules:stop(), - [mongoose_config:unset_opt(Key) || {Key, _Value} <- opts()], + mongoose_config:erase_opts(), meck:unload(mongoose_metrics), async_helper:stop_all(C), mnesia:delete_table(key). opts() -> - [{hosts, hosts()}, - {host_types, []}, - {all_metrics_are_global, false}] - ++ [{{modules, Host}, #{}} || Host <- hosts()]. + maps:from_list([{hosts, hosts()}, + {host_types, []}, + {all_metrics_are_global, false} | + [{{modules, Host}, #{}} || Host <- hosts()]]). hosts() -> [<<"localhost">>, <<"first.com">>, <<"second.com">>]. diff --git a/test/mod_global_distrib_SUITE.erl b/test/mod_global_distrib_SUITE.erl index 0ba0522205f..d714899b16a 100644 --- a/test/mod_global_distrib_SUITE.erl +++ b/test/mod_global_distrib_SUITE.erl @@ -65,7 +65,7 @@ end_per_group(_GroupName, Config) -> init_per_testcase(_CaseName, Config) -> set_meck(), - [mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts()], + mongoose_config:set_opts(opts()), mongoose_domain_sup:start_link(), mim_ct_sup:start_link(ejabberd_sup), gen_hook:start_link(), @@ -74,15 +74,15 @@ init_per_testcase(_CaseName, Config) -> end_per_testcase(_CaseName, Config) -> mongoose_modules:stop(), - [mongoose_config:unset_opt(Key) || {Key, _Value} <- opts()], + mongoose_config:erase_opts(), unset_meck(), Config. opts() -> - [{hosts, hosts()}, - {host_types, []}, - {all_metrics_are_global, false} | - [{{modules, HostType}, modules(HostType)} || HostType <- hosts()]]. + maps:from_list([{hosts, hosts()}, + {host_types, []}, + {all_metrics_are_global, false} | + [{{modules, HostType}, modules(HostType)} || HostType <- hosts()]]). hosts() -> [global_host(), local_host()]. diff --git a/test/mod_websockets_SUITE.erl b/test/mod_websockets_SUITE.erl index 024dbae43bc..3b44dab699d 100644 --- a/test/mod_websockets_SUITE.erl +++ b/test/mod_websockets_SUITE.erl @@ -51,8 +51,8 @@ setup() -> fun(mongoose_listener_sup, _) -> {ok, self()}; (A, B) -> meck:passthrough([A, B]) end), + mongoose_config:set_opts(#{default_server_name => <<"localhost">>}), %% Start websocket cowboy listening - Handlers = [config([listen, http, handlers, mod_bosh], #{host => '_', path => "/http-bind"}), config([listen, http, handlers, mod_websockets], @@ -70,6 +70,7 @@ setup() -> teardown() -> meck:unload(), cowboy:stop_listener(ejabberd_cowboy:ref({?PORT, ?IP, tcp})), + mongoose_config:erase_opts(), application:stop(cowboy), %% Do not stop jid, Erlang 21 does not like to reload nifs ok. diff --git a/test/mongoose_cleanup_SUITE.erl b/test/mongoose_cleanup_SUITE.erl index a87edc0fc5e..f3543dc6240 100644 --- a/test/mongoose_cleanup_SUITE.erl +++ b/test/mongoose_cleanup_SUITE.erl @@ -57,11 +57,11 @@ init_per_suite(Config) -> {ok, _} = application:ensure_all_started(jid), ok = mnesia:create_schema([node()]), ok = mnesia:start(), - [mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts()], + mongoose_config:set_opts(opts()), Config. end_per_suite(Config) -> - [mongoose_config:unset_opt(Key) || {Key, _Value} <- opts()], + mongoose_config:erase_opts(), mnesia:stop(), mnesia:delete_schema([node()]), Config. @@ -116,11 +116,11 @@ needs_component(TestCase) -> lists:member(TestCase, component_cases()). opts() -> - [{hosts, [?HOST]}, - {host_types, []}, - {all_metrics_are_global, false}, - {s2s_backend, mnesia}, - {{modules, ?HOST}, #{}}]. + #{hosts => [?HOST], + host_types => [], + all_metrics_are_global => false, + s2s_backend => mnesia, + {modules, ?HOST} => #{}}. meck_mods(bosh) -> [exometer, mod_bosh_socket]; meck_mods(s2s) -> [exometer, ejabberd_commands, mongoose_bin]; diff --git a/test/mongoose_config_SUITE.erl b/test/mongoose_config_SUITE.erl index 3600611d4fb..50ebd37e482 100644 --- a/test/mongoose_config_SUITE.erl +++ b/test/mongoose_config_SUITE.erl @@ -14,25 +14,25 @@ all() -> groups() -> [ - {opts, [parallel], [get_opt, - lookup_opt, - get_path, - lookup_path, - set_short_path, - set_long_path, - unset_path, - load_from_file]}, + {opts, [get_opt, + lookup_opt, + get_path, + lookup_path, + set_short_path, + set_long_path, + unset_path, + load_from_file]}, {cluster, [], [cluster_load_from_file]} ]. init_per_suite(Config) -> mnesia:start(), %% TODO Remove this call when possible (We still need it for s2s) {ok, _} = application:ensure_all_started(jid), + mongoose_config:set_opts(#{}), Config. end_per_suite(_Config) -> - [persistent_term:erase(Key) || {Key = {mongoose_config, _}, _Value} <- persistent_term:get()], - persistent_term:erase(mongoose_config_state), + mongoose_config:erase_opts(), mnesia:stop(), mnesia:delete_schema([node()]), ok. @@ -59,7 +59,7 @@ end_per_group(_GroupName, _Config) -> %% get_opt(_Config) -> - ?assertError(badarg, mongoose_config:get_opt(get_me)), + ?assertError({badkey, get_me}, mongoose_config:get_opt(get_me)), ?assertEqual(default_value, mongoose_config:get_opt(get_me, default_value)), mongoose_config:set_opt(get_me, you_got_me), ?assertEqual(you_got_me, mongoose_config:get_opt(get_me)), @@ -67,7 +67,7 @@ get_opt(_Config) -> ?assertEqual(you_got_me_again, mongoose_config:get_opt(get_me)), ?assertEqual(you_got_me_again, mongoose_config:get_opt(get_me, default_value)), mongoose_config:unset_opt(get_me), - ?assertError(badarg, mongoose_config:get_opt(get_me)), + ?assertError({badkey, get_me}, mongoose_config:get_opt(get_me)), ?assertEqual(default_value, mongoose_config:get_opt(get_me, default_value)). lookup_opt(_Config) -> @@ -78,14 +78,14 @@ lookup_opt(_Config) -> ?assertEqual({error, not_found}, mongoose_config:lookup_opt(look_me_up)). get_path(_Config) -> - ?assertError(badarg, mongoose_config:get_opt([root])), - ?assertError(badarg, mongoose_config:get_opt([root, branch])), + ?assertError({badkey, root}, mongoose_config:get_opt([root])), + ?assertError({badkey, root}, mongoose_config:get_opt([root, branch])), mongoose_config:set_opt(root, #{branch => leaf}), ?assertEqual(#{branch => leaf}, mongoose_config:get_opt([root])), ?assertEqual(leaf, mongoose_config:get_opt([root, branch])), ?assertError({badmap, leaf}, mongoose_config:get_opt([root, branch, leaf])), mongoose_config:unset_opt(root), - ?assertError(badarg, mongoose_config:get_opt([root])). + ?assertError({badkey, root}, mongoose_config:get_opt([root])). lookup_path(_Config) -> ?assertEqual({error, not_found}, mongoose_config:lookup_opt([basement])), @@ -106,9 +106,9 @@ set_short_path(_Config) -> ?assertEqual(2, mongoose_config:get_opt(a)). set_long_path(_Config) -> - ?assertError(badarg, mongoose_config:set_opt([one, two, three], 4)), + ?assertError({badkey, one}, mongoose_config:set_opt([one, two, three], 4)), mongoose_config:set_opt([one], #{}), - ?assertError({badkey, _}, mongoose_config:set_opt([one, two, three], 4)), + ?assertError({badkey, two}, mongoose_config:set_opt([one, two, three], 4)), mongoose_config:set_opt([one, two], #{}), mongoose_config:set_opt([one, two, three], 4), ?assertEqual(#{two => #{three => 4}}, mongoose_config:get_opt(one)), @@ -124,15 +124,15 @@ unset_path(_Config) -> mongoose_config:unset_opt([foo, bar, baz]), % no error for a non-existing key ?assertEqual(#{bar => #{}}, mongoose_config:get_opt(foo)), mongoose_config:unset_opt([foo]), - ?assertError(badarg, mongoose_config:get_opt(foo)), - ?assertError(badarg, mongoose_config:unset_opt([foo, bar])), + ?assertError({badkey, foo}, mongoose_config:get_opt(foo)), + ?assertError({badkey, foo}, mongoose_config:unset_opt([foo, bar])), mongoose_config:unset_opt([foo]). % no error for a non-existing key load_from_file(Config) -> use_config_file(Config, "mongooseim.minimal.toml"), ok = mongoose_config:start(), - State = mongoose_config:config_state(), - check_loaded_config(State), + Opts = mongoose_config:get_opts(), + check_loaded_config(Opts), ok = mongoose_config:stop(), check_removed_config(), @@ -148,8 +148,8 @@ cluster_load_from_file(Config) -> {ok, _} = start_ejabberd_with_config(Config, "mongooseim.toml"), {ok, _} = start_remote_ejabberd_with_config(SlaveNode, Config, "mongooseim.toml"), maybe_join_cluster(SlaveNode), - [State, State] = mongoose_config:config_states(), - check_loaded_config(State), + check_loaded_config(mongoose_config:get_opts()), + check_loaded_config(rpc:call(SlaveNode, mongoose_config, get_opts, [])), ok = mongooseim:stop(), stop_remote_ejabberd(SlaveNode), @@ -159,40 +159,35 @@ cluster_load_from_file(Config) -> %% Helpers %% -check_loaded_config(State) -> - Opts = lists:sort(mongoose_config_parser:get_opts(State)), - ExpectedOpts = lists:sort(minimal_config_opts()), - ?assertEqual(ExpectedOpts, Opts), - [?assertEqual(Val, mongoose_config:get_opt(Key)) || {Key, Val} <- ExpectedOpts]. +check_loaded_config(Opts) -> + ?assertEqual(minimal_config_opts(), Opts). check_removed_config() -> - Opts = minimal_config_opts(), - ?assertError(badarg, mongoose_config:config_state()), - [?assertError(badarg, mongoose_config:get_opt(Key)) || {Key, _} <- Opts]. + ?assertError(badarg, mongoose_config:get_opts()). minimal_config_opts() -> - [{all_metrics_are_global, false}, - {default_server_domain, <<"localhost">>}, - {hide_service_name, false}, - {host_types, []}, - {hosts, [<<"localhost">>]}, - {internal_databases, #{mnesia => #{}}}, - {language, <<"en">>}, - {listen, []}, - {loglevel, warning}, - {mongooseimctl_access_commands, #{}}, - {outgoing_pools, []}, - {rdbms_server_type, generic}, - {registration_timeout, 600}, - {routing_modules, mongoose_router:default_routing_modules()}, - {services, #{}}, - {sm_backend, mnesia}, - {component_backend, mnesia}, - {s2s_backend, mnesia}, - {{auth, <<"localhost">>}, config_parser_helper:default_auth()}, - {{modules, <<"localhost">>}, #{}}, - {{replaced_wait_timeout, <<"localhost">>}, 2000}, - {{s2s, <<"localhost">>}, config_parser_helper:default_s2s()}]. + #{all_metrics_are_global => false, + default_server_domain => <<"localhost">>, + hide_service_name => false, + host_types => [], + hosts => [<<"localhost">>], + internal_databases => #{mnesia => #{}}, + language => <<"en">>, + listen => [], + loglevel => warning, + mongooseimctl_access_commands => #{}, + outgoing_pools => [], + rdbms_server_type => generic, + registration_timeout => 600, + routing_modules => mongoose_router:default_routing_modules(), + services => #{}, + sm_backend => mnesia, + component_backend => mnesia, + s2s_backend => mnesia, + {auth, <<"localhost">>} => config_parser_helper:default_auth(), + {modules, <<"localhost">>} => #{}, + {replaced_wait_timeout, <<"localhost">>} => 2000, + {s2s, <<"localhost">>} => config_parser_helper:default_s2s()}. start_slave_node(Config) -> SlaveNode = do_start_slave_node(), diff --git a/test/mongoose_modules_SUITE.erl b/test/mongoose_modules_SUITE.erl index c40f6584f05..4a1117c7fe2 100644 --- a/test/mongoose_modules_SUITE.erl +++ b/test/mongoose_modules_SUITE.erl @@ -18,12 +18,11 @@ all() -> replaces_modules_with_same_deps]. init_per_suite(C) -> - [mongoose_config:set_opt(Opt, Val) || {Opt, Val} <- opts()], + mongoose_config:set_opts(opts()), C. end_per_suite(_C) -> - [mongoose_config:unset_opt(Opt) || {Opt, _} <- opts()], - ok. + mongoose_config:erase_opts(). init_per_testcase(_TC, C) -> meck:new(gen_mod, [passthrough]), @@ -156,8 +155,7 @@ check_modules(ExpectedModules) -> ?assertEqual(ExpectedModules, gen_mod:loaded_modules_with_opts(?HOST)). opts() -> - [{hosts, [?HOST]}, - {host_types, []}]. + #{hosts => [?HOST], host_types => []}. set_modules(Modules) -> mongoose_config:set_opt({modules, ?HOST}, Modules). diff --git a/test/mongoose_rdbms_SUITE.erl b/test/mongoose_rdbms_SUITE.erl index 62b5634e75e..0d7c26db043 100644 --- a/test/mongoose_rdbms_SUITE.erl +++ b/test/mongoose_rdbms_SUITE.erl @@ -135,14 +135,14 @@ meck_unload_rand() -> meck:unload(rand). set_opts() -> - [mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts()]. + mongoose_config:set_opts(opts()). unset_opts() -> - [mongoose_config:unset_opt(Key) || {Key, _Value} <- opts()]. + mongoose_config:erase_opts(). opts() -> - [{all_metrics_are_global, false}, - {max_fsm_queue, 1024}]. + #{all_metrics_are_global => false, + max_fsm_queue => 1024}. meck_db(odbc) -> meck:new(eodbc, [no_link]), diff --git a/test/mongoose_service_SUITE.erl b/test/mongoose_service_SUITE.erl index a8454e7a7cc..7d3388f0a44 100644 --- a/test/mongoose_service_SUITE.erl +++ b/test/mongoose_service_SUITE.erl @@ -25,7 +25,7 @@ init_per_testcase(_TC, C) -> C. end_per_testcase(_, _C) -> - mongoose_config:unset_opt(services), + mongoose_config:erase_opts(), meck:unload(test_services()). %% Test cases @@ -133,7 +133,7 @@ replaces_services_with_same_deps(_Config) -> %% Helpers set_services(Services) -> - mongoose_config:set_opt(services, Services). + mongoose_config:set_opts(#{services => Services}). get_services() -> mongoose_config:get_opt(services). diff --git a/test/mongoose_wpool_SUITE.erl b/test/mongoose_wpool_SUITE.erl index aa0b9c762cb..e6ccd65efd6 100644 --- a/test/mongoose_wpool_SUITE.erl +++ b/test/mongoose_wpool_SUITE.erl @@ -48,7 +48,7 @@ all() -> init_per_suite(Config) -> ok = meck:new(wpool, [no_link, passthrough]), ok = meck:new(mongoose_wpool, [no_link, passthrough]), - [mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts()], + mongoose_config:set_opts(opts()), Self = self(), spawn(fun() -> register(test_helper, self()), @@ -62,12 +62,11 @@ init_per_suite(Config) -> end_per_suite(Config) -> meck:unload(wpool), whereis(test_helper) ! stop, - [mongoose_config:unset_opt(Key) || {Key, _Value} <- opts()], + mongoose_config:erase_opts(), Config. opts() -> - [{hosts, [<<"a.com">>, <<"b.com">>, <<"c.eu">>]}, - {host_types, []}]. + #{hosts => [<<"a.com">>, <<"b.com">>, <<"c.eu">>], host_types => []}. init_per_testcase(_Case, Config) -> cleanup_pools(), diff --git a/test/mongooseim_metrics_SUITE.erl b/test/mongooseim_metrics_SUITE.erl index 433893e74aa..ce826016559 100644 --- a/test/mongooseim_metrics_SUITE.erl +++ b/test/mongooseim_metrics_SUITE.erl @@ -68,16 +68,15 @@ end_per_suite(C) -> C. init_per_group(Group, C) -> - [mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts(Group)], + mongoose_config:set_opts(opts(Group)), mongoose_metrics:init(), mongoose_metrics:init_mongooseim_metrics(), C. -end_per_group(Group, C) -> +end_per_group(_Group, _C) -> mongoose_metrics:remove_host_type_metrics(<<"localhost">>), mongoose_metrics:remove_host_type_metrics(global), - [mongoose_config:unset_opt(Key) || {Key, _Value} <- opts(Group)], - C. + mongoose_config:erase_opts(). init_per_testcase(CN, C) when tcp_connections_detected =:= CN; tcp_metric_varies_with_tcp_variations =:= CN -> @@ -165,9 +164,9 @@ wait_for_update({ok, [{count,0}]}, N) -> wait_for_update(exometer:get_value([carbon, packets], count), N-1). opts(Group) -> - [{hosts, [<<"localhost">>]}, - {host_types, []}, - {all_metrics_are_global, Group =:= all_metrics_are_global}]. + #{hosts => [<<"localhost">>], + host_types => [], + all_metrics_are_global => Group =:= all_metrics_are_global}. get_reporters_cfg(Port) -> [{reporters, [ diff --git a/test/muc_light_SUITE.erl b/test/muc_light_SUITE.erl index 47a4825e6d3..1dfb7843959 100644 --- a/test/muc_light_SUITE.erl +++ b/test/muc_light_SUITE.erl @@ -51,7 +51,7 @@ end_per_group(_, Config) -> Config. init_per_testcase(codec_calls, Config) -> - [mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts()], + mongoose_config:set_opts(opts()), meck_mongoose_subdomain_core(), ok = mnesia:create_schema([node()]), ok = mnesia:start(), @@ -73,17 +73,17 @@ end_per_testcase(codec_calls, Config) -> mnesia:delete_schema([node()]), application:stop(exometer_core), meck:unload(), - [mongoose_config:unset_opt(Key) || {Key, _Value} <- opts()], + mongoose_config:erase_opts(), Config; end_per_testcase(_, Config) -> Config. opts() -> - [{hosts, [host_type()]}, - {host_types, []}, - {all_metrics_are_global, false}, - {component_backend, mnesia}, - {{modules, host_type()}, #{mod_muc_light => default_mod_config(mod_muc_light)}}]. + #{hosts => [host_type()], + host_types => [], + all_metrics_are_global => false, + component_backend => mnesia, + {modules, host_type()} => #{mod_muc_light => default_mod_config(mod_muc_light)}}. %% ------------------------------------------------------------------ %% Test cases diff --git a/test/privacy_SUITE.erl b/test/privacy_SUITE.erl index 04759b3d5da..7f4ee78b459 100644 --- a/test/privacy_SUITE.erl +++ b/test/privacy_SUITE.erl @@ -37,11 +37,9 @@ init_per_suite(C) -> ok = mnesia:create_schema([node()]), ok = mnesia:start(), {ok, _} = application:ensure_all_started(exometer_core), - mongoose_config:set_opt(all_metrics_are_global, false), C. end_per_suite(_C) -> - mongoose_config:unset_opt(all_metrics_are_global), mnesia:stop(), mnesia:delete_schema([node()]), application:stop(exometer_core), @@ -49,19 +47,20 @@ end_per_suite(_C) -> init_per_testcase(_, C) -> gen_hook:start_link(), - [mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts()], + mongoose_config:set_opts(opts()), mongoose_modules:start(), C. end_per_testcase(_, _) -> mongoose_modules:stop(), - [mongoose_config:unset_opt(Key) || {Key, _Value} <- opts()]. + mongoose_config:erase_opts(). opts() -> - [{hosts, [<<"localhost">>]}, - {host_types, []}, - {{modules, <<"localhost">>}, - #{mod_privacy => config_parser_helper:default_mod_config(mod_privacy)}}]. + #{hosts => [<<"localhost">>], + host_types => [], + all_metrics_are_global => false, + {modules, <<"localhost">>} => + #{mod_privacy => config_parser_helper:default_mod_config(mod_privacy)}}. check_with_allowed(_C) -> Acc = mongoose_acc:new(?ACC_PARAMS#{element => message()}), diff --git a/test/roster_SUITE.erl b/test/roster_SUITE.erl index 92665aa61e0..4548e67e5c3 100644 --- a/test/roster_SUITE.erl +++ b/test/roster_SUITE.erl @@ -37,15 +37,14 @@ init_per_suite(C) -> meck:expect(gen_iq_handler, remove_iq_handler_for_domain, fun(_, _, _) -> ok end), meck:new(mongoose_domain_api, [no_link]), meck:expect(mongoose_domain_api, get_domain_host_type, fun(_) -> {ok, host_type()} end), - [mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts()], + mongoose_config:set_opts(opts()), C. -end_per_suite(C) -> - [mongoose_config:unset_opt(Key) || {Key, _Value} <- opts()], +end_per_suite(_C) -> + mongoose_config:erase_opts(), meck:unload(), mnesia:stop(), - mnesia:delete_schema([node()]), - C. + mnesia:delete_schema([node()]). init_per_testcase(_TC, C) -> init_ets(), @@ -62,10 +61,10 @@ end_per_testcase(_TC, C) -> C. opts() -> - [{hosts, [host_type()]}, - {host_types, []}, - {all_metrics_are_global, false}, - {{modules, host_type()}, #{mod_roster => config_parser_helper:default_mod_config(mod_roster)}}]. + #{hosts => [host_type()], + host_types => [], + all_metrics_are_global => false, + {modules, host_type()} => #{mod_roster => config_parser_helper:default_mod_config(mod_roster)}}. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%% TESTS %%%%%%%%%%%%%%%%%%%%%%% diff --git a/test/router_SUITE.erl b/test/router_SUITE.erl index 1b3ec4cf718..3e46a99dbfe 100644 --- a/test/router_SUITE.erl +++ b/test/router_SUITE.erl @@ -37,14 +37,13 @@ end_per_suite(_C) -> ok. init_per_group(routing, Config) -> - RoutingModules = [xmpp_router_a, xmpp_router_b, xmpp_router_c], - mongoose_config:set_opt(routing_modules, xmpp_router:expand_routing_modules(RoutingModules)), + mongoose_config:set_opts(opts()), gen_hook:start_link(), ejabberd_router:start_link(), Config. end_per_group(routing, _Config) -> - mongoose_config:unset_opt(routing_modules). + mongoose_config:erase_opts(). init_per_testcase(_CaseName, Config) -> Config. @@ -170,3 +169,9 @@ resend_as_error(From0, To0, Acc0, Packet0) -> {Acc1, Packet1} = jlib:make_error_reply(Acc0, Packet0, #xmlel{}), Acc2 = ejabberd_router:route(To0, From0, Acc1, Packet1), {done, Acc2}. + +opts() -> + RoutingModules = [xmpp_router_a, xmpp_router_b, xmpp_router_c], + #{all_metrics_are_global => false, + component_backend => mnesia, + routing_modules => xmpp_router:expand_routing_modules(RoutingModules)}. diff --git a/test/translate_SUITE.erl b/test/translate_SUITE.erl index cdd15e7ab6c..3760cd13beb 100644 --- a/test/translate_SUITE.erl +++ b/test/translate_SUITE.erl @@ -10,9 +10,8 @@ all() -> ]. -end_per_testcase(_, C) -> - mongoose_config:unset_opt(language), - C. +end_per_testcase(_, _C) -> + mongoose_config:erase_opts(). test_english_translation(_Config) -> %% given @@ -61,4 +60,4 @@ given_loaded_translations() -> translate:start(). given_default_language(Language) -> - mongoose_config:set_opt(language, Language). + mongoose_config:set_opts(#{language => Language}).