From 97a010542dca4058249f222fc2926c4b3feec1da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Fri, 15 Oct 2021 15:54:55 +0200 Subject: [PATCH 01/10] Replace ejabberd_config with mongoose_config The config is now stored in persistent terms. Other changes: - Removed checks like 'is_integer' for already validated options - Eliminated the error-prone and ambiguous default value 'undefined' - Removed obsolete code, e.g. handlers for non-existing hooks --- include/ejabberd_config.hrl | 4 +- include/mongoose.hrl | 9 +- src/acl.erl | 21 +- src/admin_extra/service_admin_extra_gdpr.erl | 2 +- src/auth/ejabberd_auth.erl | 19 +- src/auth/ejabberd_auth_anonymous.erl | 4 +- src/auth/ejabberd_auth_external.erl | 9 +- src/auth/ejabberd_auth_jwt.erl | 2 +- src/auth/ejabberd_auth_ldap.erl | 2 +- src/auth/ejabberd_auth_riak.erl | 3 +- src/auth/extauth.erl | 5 +- src/config/mongoose_config.erl | 145 ++++++++++++ src/config/mongoose_config_parser.erl | 14 +- src/config/mongoose_config_parser_toml.erl | 9 +- src/domain/mongoose_domain_api.erl | 4 +- src/ejabberd_app.erl | 31 +-- src/ejabberd_c2s.erl | 36 +-- src/ejabberd_config.erl | 224 ------------------ src/ejabberd_cowboy.erl | 9 +- src/ejabberd_ctl.erl | 5 +- src/ejabberd_listener.erl | 26 +- src/ejabberd_rdbms.erl | 2 +- src/ejabberd_router.erl | 5 +- src/ejabberd_s2s.erl | 29 ++- src/ejabberd_s2s_in.erl | 23 +- src/ejabberd_s2s_out.erl | 116 ++++----- src/ejabberd_service.erl | 8 +- src/ejabberd_sm.erl | 5 +- src/gen_mod.erl | 43 +--- .../mod_global_distrib_server_mgr.erl | 6 +- src/mam/mod_mam_meta.erl | 4 +- src/metrics/mongoose_metrics.erl | 4 +- src/mod_last.erl | 6 +- src/mod_muc_log.erl | 10 +- src/mod_register.erl | 5 +- src/mod_shared_roster_ldap.erl | 18 -- src/mod_vcard.erl | 19 -- src/mongoose_service.erl | 2 +- src/rdbms/mongoose_rdbms_odbc.erl | 2 +- src/sasl/cyrsasl.erl | 5 +- src/shaper.erl | 6 +- .../mongoose_system_metrics_collector.erl | 6 +- .../mongoose_system_metrics_sender.erl | 12 +- .../service_mongoose_system_metrics.erl | 15 +- src/translate.erl | 10 +- src/wpool/mongoose_wpool.erl | 4 +- 46 files changed, 349 insertions(+), 599 deletions(-) create mode 100644 src/config/mongoose_config.erl delete mode 100644 src/ejabberd_config.erl diff --git a/include/ejabberd_config.hrl b/include/ejabberd_config.hrl index 0e454dd753f..a3728ebe69b 100644 --- a/include/ejabberd_config.hrl +++ b/include/ejabberd_config.hrl @@ -20,8 +20,8 @@ -ifndef(MONGOOSEIM_EJABBERD_CONFIG_HRL). -define(MONGOOSEIM_EJABBERD_CONFIG_HRL, true). --record(local_config, {key :: mongoose_config_parser:key(), - value :: mongoose_config_parser:value() +-record(local_config, {key :: mongoose_config:key(), + value :: mongoose_config:value() }). -endif. diff --git a/include/mongoose.hrl b/include/mongoose.hrl index 71683975a77..9a9e09be3c8 100644 --- a/include/mongoose.hrl +++ b/include/mongoose.hrl @@ -22,11 +22,10 @@ %% If the ejabberd application description isn't loaded, returns atom: undefined -define(MONGOOSE_VERSION, element(2, application:get_key(mongooseim,vsn))). --define(MYHOSTS, ejabberd_config:get_local_option_or_default(hosts, [])). --define(ALL_HOST_TYPES, ejabberd_config:get_local_option_or_default(hosts, []) ++ - ejabberd_config:get_local_option_or_default(host_types, [])). --define(MYNAME, ejabberd_config:get_local_option(default_server_domain)). --define(MYLANG, ejabberd_config:get_local_option(language)). +-define(MYHOSTS, mongoose_config:get_opt(hosts, [])). +-define(ALL_HOST_TYPES, mongoose_config:get_opt(hosts, []) ++ mongoose_config:get_opt(host_types, [])). +-define(MYNAME, mongoose_config:get_opt(default_server_domain)). +-define(MYLANG, mongoose_config:get_opt(language, <<"en">>)). -define(CONFIG_PATH, "etc/mongooseim.toml"). diff --git a/src/acl.erl b/src/acl.erl index da1d925cee9..d9eeb43fe44 100644 --- a/src/acl.erl +++ b/src/acl.erl @@ -100,23 +100,22 @@ match_rule_for_host_type(_HostType, _, all, _, _Default) -> match_rule_for_host_type(_HostType, _, none, _, _Default) -> deny; match_rule_for_host_type(global, Domain, Rule, JID, Default) -> - case ejabberd_config:get_local_option({access, Rule, global}) of - undefined -> + case mongoose_config:lookup_opt({access, Rule, global}) of + {error, not_found} -> Default; - GACLs -> + {ok, GACLs} -> match_acls(GACLs, JID, global, Domain) end; match_rule_for_host_type(HostType, Domain, Rule, JID, Default) -> - GlobalACLs = ejabberd_config:get_local_option({access, Rule, global}), - HostACLs = ejabberd_config:get_local_option({access, Rule, HostType}), - case {GlobalACLs, HostACLs} of - {undefined, undefined} -> + case {mongoose_config:lookup_opt({access, Rule, global}), + mongoose_config:lookup_opt({access, Rule, HostType})} of + {{error, not_found}, {error, not_found}} -> Default; - {undefined, HostACLs} -> + {{error, not_found}, {ok, HostACLs}} -> match_acls(HostACLs, JID, HostType, Domain); - {GlobalACLs, undefined} -> + {{ok, GlobalACLs}, {error, not_found}} -> match_acls(GlobalACLs, JID, HostType, Domain); - {GlobalACLs, HostACLs} -> + {{ok, GlobalACLs}, {ok, HostACLs}} -> match_acls(merge_acls(GlobalACLs, HostACLs), JID, HostType, Domain) end. @@ -162,7 +161,7 @@ match_acl(Rule, JID, HostType, Domain) -> -spec get_acl_specs(rule(), host_type_or_global()) -> [aclspec()]. get_acl_specs(Rule, HostType) -> - ejabberd_config:get_local_option_or_default({acl, Rule, HostType}, []). + mongoose_config:get_opt({acl, Rule, HostType}, []). -spec is_server_valid(domain_or_global(), jid:lserver()) -> boolean(). is_server_valid(Domain, Domain) -> diff --git a/src/admin_extra/service_admin_extra_gdpr.erl b/src/admin_extra/service_admin_extra_gdpr.erl index dea24fa03a0..301a0605ef7 100644 --- a/src/admin_extra/service_admin_extra_gdpr.erl +++ b/src/admin_extra/service_admin_extra_gdpr.erl @@ -121,7 +121,7 @@ retrieve_logs(Username, Domain) -> -spec get_all_logs(gdpr:username(), gdpr:domain(), file:name()) -> [file:name()]. get_all_logs(Username, Domain, TmpDir) -> - OtherNodes = ejabberd_config:other_cluster_nodes(), + OtherNodes = mongoose_config:other_cluster_nodes(), LogFile = get_logs(Username, Domain, TmpDir), LogFilesFromOtherNodes = [get_logs_from_node(Node, Username, Domain, TmpDir) || Node <- OtherNodes], [LogFile | LogFilesFromOtherNodes]. diff --git a/src/auth/ejabberd_auth.erl b/src/auth/ejabberd_auth.erl index c5e20a73f4d..3fb5080bef1 100644 --- a/src/auth/ejabberd_auth.erl +++ b/src/auth/ejabberd_auth.erl @@ -122,23 +122,23 @@ hooks(HostType) -> ]. -spec set_opts(HostType :: mongooseim:host_type(), - KVs :: [tuple()]) -> {atomic|aborted, _}. + KVs :: [tuple()]) -> ok. set_opts(HostType, KVs) -> - OldOpts = ejabberd_config:get_local_option({auth_opts, HostType}), + OldOpts = mongoose_config:get_opt({auth_opts, HostType}), AccFunc = fun({K, V}, Acc) -> lists:keystore(K, 1, Acc, {K, V}) end, NewOpts = lists:foldl(AccFunc, OldOpts, KVs), - ejabberd_config:add_local_option({auth_opts, HostType}, NewOpts). + mongoose_config:set_opt({auth_opts, HostType}, NewOpts). -spec get_opt(HostType :: mongooseim:host_type(), Opt :: atom(), Default :: ejabberd:value()) -> undefined | ejabberd:value(). get_opt(HostType, Opt, Default) -> - case ejabberd_config:get_local_option({auth_opts, HostType}) of - undefined -> + case mongoose_config:lookup_opt({auth_opts, HostType}) of + {error, not_found} -> Default; - Opts -> + {ok, Opts} -> case lists:keyfind(Opt, 1, Opts) of {Opt, Value} -> Value; @@ -444,17 +444,12 @@ auth_modules_for_host_type(HostType) -> -spec auth_methods(mongooseim:host_type()) -> [atom()]. auth_methods(HostType) -> - Method = ejabberd_config:get_local_option({auth_method, HostType}), - get_auth_method_as_a_list(Method). + mongoose_config:get_opt({auth_method, HostType}, []). -spec auth_method_to_module(atom()) -> authmodule(). auth_method_to_module(Method) -> list_to_atom("ejabberd_auth_" ++ atom_to_list(Method)). -get_auth_method_as_a_list(undefined) -> []; -get_auth_method_as_a_list(AuthMethod) when is_list(AuthMethod) -> AuthMethod; -get_auth_method_as_a_list(AuthMethod) when is_atom(AuthMethod) -> [AuthMethod]. - -spec remove_domain(mongoose_hooks:simple_acc(), mongooseim:host_type(), jid:lserver()) -> mongoose_hooks:simple_acc(). remove_domain(Acc, HostType, Domain) -> diff --git a/src/auth/ejabberd_auth_anonymous.erl b/src/auth/ejabberd_auth_anonymous.erl index 05620c92a19..77ebd3601cd 100644 --- a/src/auth/ejabberd_auth_anonymous.erl +++ b/src/auth/ejabberd_auth_anonymous.erl @@ -86,7 +86,7 @@ stop(HostType) -> %% defaults to false -spec allow_multiple_connections(mongooseim:host_type()) -> boolean(). allow_multiple_connections(HostType) -> - ejabberd_config:get_local_option({allow_multiple_connections, HostType}) =:= true. + mongoose_config:get_opt({allow_multiple_connections, HostType}, false). does_user_exist(_, LUser, LServer) -> does_anonymous_user_exist(LUser, LServer). @@ -274,7 +274,7 @@ is_protocol_enabled(HostType, Protocol) -> %% @doc Returns the anonymous protocol to use, defaults to sasl_anon -spec anonymous_protocol(mongooseim:host_type()) -> sasl_anon | login_anon | both. anonymous_protocol(HostType) -> - ejabberd_config:get_local_option_or_default({anonymous_protocol, HostType}, sasl_anon). + mongoose_config:get_opt({anonymous_protocol, HostType}, sasl_anon). -spec supported_features() -> [atom()]. supported_features() -> [dynamic_domains]. diff --git a/src/auth/ejabberd_auth_external.erl b/src/auth/ejabberd_auth_external.erl index be2626efd92..a988bdae03a 100644 --- a/src/auth/ejabberd_auth_external.erl +++ b/src/auth/ejabberd_auth_external.erl @@ -214,11 +214,12 @@ supported_features() -> [dynamic_domains]. %%% Extauth cache management %%% +%% FIXME there is no such option in config spec -spec get_cache_option(mongooseim:host_type()) -> false | {true, CacheTime::integer()}. get_cache_option(HostType) -> - case ejabberd_config:get_local_option({extauth_cache, HostType}) of - CacheTime when is_integer(CacheTime) -> {true, CacheTime}; - _ -> false + case mongoose_config:lookup_opt({extauth_cache, HostType}) of + {ok, CacheTime} -> {true, CacheTime}; + {error, not_found} -> false end. -spec check_password_extauth(HostType :: mongooseim:host_type(), @@ -385,4 +386,4 @@ get_mod_last_configured(HostType) -> end. is_configured(HostType, Module) -> - lists:keymember(Module, 1, ejabberd_config:get_local_option({modules, HostType})). + lists:keymember(Module, 1, mongoose_config:get_opt({modules, HostType})). diff --git a/src/auth/ejabberd_auth_jwt.erl b/src/auth/ejabberd_auth_jwt.erl index ed8996a358c..ad44aa5ff00 100644 --- a/src/auth/ejabberd_auth_jwt.erl +++ b/src/auth/ejabberd_auth_jwt.erl @@ -55,7 +55,7 @@ start(HostType) -> JWTAlgorithm = ejabberd_auth:get_opt(HostType, jwt_algorithm), ejabberd_auth:set_opts(HostType, [{jwt_secret, JWTSecret}, - {jwt_algorithm, list_to_binary(JWTAlgorithm)}]), + {jwt_algorithm, list_to_binary(JWTAlgorithm)}]), ok. -spec stop(HostType :: mongooseim:host_type()) -> ok. diff --git a/src/auth/ejabberd_auth_ldap.erl b/src/auth/ejabberd_auth_ldap.erl index cf2e35ec182..46e7d668c93 100644 --- a/src/auth/ejabberd_auth_ldap.erl +++ b/src/auth/ejabberd_auth_ldap.erl @@ -438,7 +438,7 @@ result_attrs(#state{uids = UIDs, -spec parse_options(HostType :: mongooseim:host_type()) -> state(). parse_options(HostType) -> - Opts = ejabberd_config:get_local_option_or_default({auth_opts, HostType}, []), + Opts = mongoose_config:get_opt({auth_opts, HostType}, []), EldapID = eldap_utils:get_mod_opt(ldap_pool_tag, Opts, fun(A) when is_atom(A) -> A end, default), BindEldapID = eldap_utils:get_mod_opt(ldap_bind_pool_tag, Opts, diff --git a/src/auth/ejabberd_auth_riak.erl b/src/auth/ejabberd_auth_riak.erl index cc0b6dcd07b..5a3f476fe76 100644 --- a/src/auth/ejabberd_auth_riak.erl +++ b/src/auth/ejabberd_auth_riak.erl @@ -172,8 +172,7 @@ remove_user(HostType, LUser, LServer) -> -spec bucket_type(mongooseim:host_type(), jid:lserver()) -> {binary(), jid:lserver()}. bucket_type(HostType, LServer) -> - Opts = ejabberd_config:get_local_option_or_default({auth_opts, HostType}, []), - BucketType = proplists:get_value(bucket_type, Opts, <<"users">>), + BucketType = ejabberd_auth:get_opt(HostType, bucket_type, <<"users">>), {BucketType, LServer}. %% ----------------------------------------------------------------------------- diff --git a/src/auth/extauth.erl b/src/auth/extauth.erl index 462d36fd07b..08beb56ab59 100644 --- a/src/auth/extauth.erl +++ b/src/auth/extauth.erl @@ -131,10 +131,7 @@ random_instance(MaxNum) -> -spec get_instances(mongooseim:host_type()) -> integer(). get_instances(HostType) -> - case ejabberd_config:get_local_option({extauth_instances, HostType}) of - Num when is_integer(Num) -> Num; - _ -> 1 - end. + mongoose_config:get_opt({extauth_instances, HostType}, 1). -spec loop(port(), integer(), atom(), any()) -> no_return(). diff --git a/src/config/mongoose_config.erl b/src/config/mongoose_config.erl new file mode 100644 index 00000000000..f3501bd0a26 --- /dev/null +++ b/src/config/mongoose_config.erl @@ -0,0 +1,145 @@ +-module(mongoose_config). + +%% API +-export([start/0, + stop/0, + get_config_path/0, + lookup_opt/1, + get_opt/1, + get_opt/2]). + +%% Test API - currently used by some modules, such usage will be eliminated +%% Options set here are not cleaned up by stop/0 +-export([set_opt/2, + unset_opt/1]). + +%% Shell utilities intended for debugging and system inspection +-export([config_state/0, + config_states/0]). + +%% General-purpose utilities +-export([all_cluster_nodes/0, + other_cluster_nodes/0]). + +-ignore_xref([config_state/0, config_states/0, all_cluster_nodes/0]). + +-include("mongoose.hrl"). +-include("ejabberd_config.hrl"). + +-type key() :: atom() | host_type_key() | host_type_or_global_key(). +-type s2s_domain_key() :: {atom(), jid:lserver()}. +-type host_type_key() :: {atom() | s2s_domain_key(), mongooseim:host_type()}. +-type host_type_or_global_key() :: {shaper | access | acl, atom(), mongooseim:host_type() | global}. + +-type value() :: atom() + | binary() + | integer() + | string() + | [value()] + | tuple(). + +-export_type([key/0, value/0]). + +-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). + +-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 + _:_ -> + {error, not_started} + end. + +%% @doc Get the filename of the ejabberd configuration file. +%% The filename can be specified with: erl -config "/path/to/mongooseim.toml". +%% It can also be specified with the environment variable EJABBERD_CONFIG_PATH. +%% If not specified, the default value 'mongooseim.toml' is assumed. +-spec get_config_path() -> string(). +get_config_path() -> + DefaultPath = case os:getenv("EJABBERD_CONFIG_PATH") of + false -> ?CONFIG_PATH; + Path -> Path + end, + application:get_env(mongooseim, config, DefaultPath). + +-spec set_opts(mongoose_config_parser:state()) -> ok. +set_opts(State) -> + Opts = mongoose_config_parser:state_to_opts(State), + [set_opt(Key, Value) || #local_config{key = Key, value = Value} <- Opts], + ok. + +-spec unset_opts(mongoose_config_parser:state()) -> ok. +unset_opts(State) -> + Opts = mongoose_config_parser:state_to_opts(State), + [unset_opt(Key) || #local_config{key = Key} <- Opts], + ok. + +-spec set_opt(key(), value()) -> ok. +set_opt(Key, Value) -> + persistent_term:put({?MODULE, Key}, Value). + +-spec unset_opt(key()) -> boolean(). +unset_opt(Key) -> + persistent_term:erase({?MODULE, Key}). + +%% @doc Use instead of get_opt(Key, undefined) +-spec lookup_opt(key()) -> {ok, value()} | {error, not_found}. +lookup_opt(Key) -> + try persistent_term:get({?MODULE, Key}) of + Value -> {ok, Value} + catch + error:_ -> {error, not_found} + end. + +%% @doc Fails if the option does not exist +-spec get_opt(key()) -> value(). +get_opt(Key) -> + persistent_term:get({?MODULE, Key}). + +-spec get_opt(key(), value()) -> value(). +get_opt(Key, Default) -> + persistent_term:get({?MODULE, Key}, Default). + +-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(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. + +-spec all_cluster_nodes() -> [node()]. +all_cluster_nodes() -> + [node() | other_cluster_nodes()]. + +-spec other_cluster_nodes() -> [node()]. +other_cluster_nodes() -> + lists:filter(fun is_mongooseim_node/1, nodes()). + +-spec is_mongooseim_node(node()) -> boolean(). +is_mongooseim_node(Node) -> + Apps = rpc:call(Node, application, which_applications, []), + lists:keymember(mongooseim, 1, Apps). diff --git a/src/config/mongoose_config_parser.erl b/src/config/mongoose_config_parser.erl index 275b072cfe2..7bde2908d2e 100644 --- a/src/config/mongoose_config_parser.erl +++ b/src/config/mongoose_config_parser.erl @@ -29,19 +29,7 @@ -include("mongoose.hrl"). -include("ejabberd_config.hrl"). --type key() :: atom() | host_type_key() | host_type_or_global_key(). --type s2s_domain_key() :: {atom(), jid:lserver()}. --type host_type_key() :: {atom() | s2s_domain_key(), mongooseim:host_type()}. --type host_type_or_global_key() :: {shaper | access | acl, atom(), mongooseim:host_type() | global}. - --type value() :: atom() - | binary() - | integer() - | string() - | [value()] - | tuple(). - --export_type([state/0, key/0, value/0]). +-export_type([state/0]). -record(state, {opts = [] :: list(), hosts = [] :: [domain_name()], diff --git a/src/config/mongoose_config_parser_toml.erl b/src/config/mongoose_config_parser_toml.erl index 8e06a235945..7a96a8a033e 100644 --- a/src/config/mongoose_config_parser_toml.erl +++ b/src/config/mongoose_config_parser_toml.erl @@ -59,8 +59,8 @@ parse_file(FileName) -> -spec process(toml_section()) -> mongoose_config_parser:state(). process(Content) -> Config = parse(Content), - Hosts = get_key(Config, hosts), - HostTypes = get_key(Config, host_types), + Hosts = get_value(Config, hosts), + HostTypes = get_value(Config, host_types), {FOpts, Opts} = lists:partition(fun(Opt) -> is_function(Opt, 1) end, Config), HostsOpts = lists:flatmap(fun(F) -> lists:flatmap(F, Hosts) end, FOpts), HostTypesOpts = lists:flatmap(fun(F) -> lists:flatmap(F, HostTypes) end, FOpts), @@ -281,9 +281,8 @@ item_key(_, _) -> item. %% Processing of the parsed options --spec get_key(config_list(), mongoose_config_parser:key()) -> - [mongoose_config_parser:value()]. -get_key(Config, Key) -> +-spec get_value(config_list(), mongoose_config:key()) -> [mongoose_config:value()]. +get_value(Config, Key) -> FilterFn = fun(#local_config{key = K}) when K =:= Key -> true; (_) -> false end, diff --git a/src/domain/mongoose_domain_api.erl b/src/domain/mongoose_domain_api.erl index a4311ed9fee..e728ff7cebe 100644 --- a/src/domain/mongoose_domain_api.erl +++ b/src/domain/mongoose_domain_api.erl @@ -34,7 +34,7 @@ -spec init() -> ok | {error, term()}. init() -> Pairs = get_static_pairs(), - AllowedHostTypes = ejabberd_config:get_local_option_or_default(host_types, []), + AllowedHostTypes = mongoose_config:get_opt(host_types, []), mongoose_domain_core:start(Pairs, AllowedHostTypes), mongoose_subdomain_core:start(), mongoose_lazy_routing:start(). @@ -177,7 +177,7 @@ check_domain(Domain, HostType) -> %% Domains should be nameprepped using `jid:nameprep' -spec get_static_pairs() -> [pair()]. get_static_pairs() -> - [{H, H} || H <- ejabberd_config:get_local_option_or_default(hosts, [])]. + [{H, H} || H <- mongoose_config:get_opt(hosts, [])]. -spec register_subdomain(host_type(), subdomain_pattern(), mongoose_packet_handler:t()) -> diff --git a/src/ejabberd_app.erl b/src/ejabberd_app.erl index e68e1af1be4..a1a3356ce54 100644 --- a/src/ejabberd_app.erl +++ b/src/ejabberd_app.erl @@ -53,9 +53,8 @@ start(normal, _Args) -> mongoose_commands:init(), mongoose_service:start(), gen_mod:start(), - ejabberd_config:start(), - mongoose_logs:set_global_loglevel(ejabberd_config:get_local_option_or_default(loglevel, warning)), - connect_nodes(), + mongoose_config:start(), + mongoose_logs:set_global_loglevel(mongoose_config:get_opt(loglevel, warning)), mongoose_deprecations:start(), {ok, _} = Sup = ejabberd_sup:start_link(), mongoose_domain_api:init(), @@ -90,6 +89,7 @@ prep_stop(State) -> broadcast_c2s_shutdown(), mongoose_wpool:stop(), mongoose_metrics:remove_all_metrics(), + mongoose_config:stop(), State. %% All the processes were killed when this function is called @@ -120,10 +120,10 @@ db_init() -> start_modules() -> lists:foreach( fun(Host) -> - case ejabberd_config:get_local_option({modules, Host}) of - undefined -> + case mongoose_config:lookup_opt({modules, Host}) of + {error, not_found} -> ok; - Modules -> + {ok, Modules} -> gen_mod_deps:start_modules(Host, Modules) end end, ?ALL_HOST_TYPES). @@ -137,10 +137,10 @@ stop_modules() -> fun({Module, _Args}) -> gen_mod:stop_module_keep_config(Host, Module) end, - case ejabberd_config:get_local_option({modules, Host}) of - undefined -> + case mongoose_config:lookup_opt({modules, Host}) of + {error, not_found} -> ok; - Modules -> + {ok, Modules} -> lists:foreach(StopModuleFun, Modules) end end, ?ALL_HOST_TYPES). @@ -149,7 +149,7 @@ stop_modules() -> start_services() -> lists:foreach( fun({Service, Opts}) -> mongoose_service:ensure_loaded(Service, Opts) end, - ejabberd_config:get_local_option_or_default(services, []) + mongoose_config:get_opt(services, []) ). -spec stop_services() -> ok. @@ -159,17 +159,6 @@ stop_services() -> mongoose_service:loaded_services_with_opts() ). --spec connect_nodes() -> 'ok'. -connect_nodes() -> - case ejabberd_config:get_local_option(cluster_nodes) of - undefined -> - ok; - Nodes when is_list(Nodes) -> - lists:foreach(fun(Node) -> - net_kernel:connect_node(Node) - end, Nodes) - end. - -spec broadcast_c2s_shutdown() -> 'ok'. broadcast_c2s_shutdown() -> Children = supervisor:which_children(ejabberd_c2s_sup), diff --git a/src/ejabberd_c2s.erl b/src/ejabberd_c2s.erl index f02b5edbd52..340b1c4fdbf 100644 --- a/src/ejabberd_c2s.erl +++ b/src/ejabberd_c2s.erl @@ -253,7 +253,7 @@ init([{SockMod, Socket}, Opts]) -> access = Access, shaper = Shaper, ip = IP, - lang = default_language(), + lang = ?MYLANG, hibernate_after= HibernateAfter }, ?C2S_OPEN_TIMEOUT} @@ -276,10 +276,10 @@ wait_for_stream(timeout, StateData) -> wait_for_stream(closed, StateData) -> {stop, normal, StateData}; wait_for_stream(_UnexpectedItem, #state{server = Server} = StateData) -> - case ejabberd_config:get_local_option(hide_service_name) of + case mongoose_config:get_opt(hide_service_name, false) of true -> {stop, normal, StateData}; - _ -> + false -> send_header(StateData, Server, << "1.0">>, <<"">>), c2s_stream_error(mongoose_xmpp_errors:xml_not_well_formed(), StateData) end. @@ -306,7 +306,7 @@ handle_stream_start({xmlstreamstart, _Name, Attrs}, #state{} = S0) -> end. stream_start_error(Error, StateData) -> - send_header(StateData, ?MYNAME, <<"">>, default_language()), + send_header(StateData, ?MYNAME, <<"">>, ?MYLANG), c2s_stream_error(Error, StateData). -spec c2s_stream_error(Error, State) -> Result when @@ -332,7 +332,7 @@ stream_start_by_protocol_version(_Pre1_0, S) -> stream_start_error(mongoose_xmpp_errors:unsupported_version(), S). stream_start_negotiate_features(#state{} = S) -> - send_header(S, S#state.server, <<"1.0">>, default_language()), + send_header(S, S#state.server, <<"1.0">>, ?MYLANG), case {S#state.authenticated, S#state.resource} of {false, _} -> stream_start_features_before_auth(S); @@ -475,12 +475,6 @@ get_xml_lang(Attrs) -> <<>> end. -default_language() -> - case ?MYLANG of - undefined -> <<"en">>; - DL -> DL - end. - verify_opts(verify_none) -> [verify_none]; verify_opts(verify_peer) -> []. @@ -524,14 +518,11 @@ wait_for_feature_before_auth({xmlstreamelement, El}, StateData) -> {?NS_TLS, <<"starttls">>} when TLS == true, TLSEnabled == false, SockMod == gen_tcp -> - TLSOpts = case ejabberd_config:get_local_option( - {domain_certfile, StateData#state.host_type}) of - undefined -> + TLSOpts = case mongoose_config:lookup_opt({domain_certfile, StateData#state.host_type}) of + {error, not_found} -> StateData#state.tls_options; - CertFile -> - [{certfile, CertFile} | - lists:keydelete( - certfile, 1, StateData#state.tls_options)] + {ok, CertFile} -> + lists:keystore(certfile, 1, StateData#state.tls_options, {certfile, CertFile}) end, TLSSocket = mongoose_transport:starttls(StateData#state.sockmod, StateData#state.socket, @@ -815,8 +806,7 @@ do_open_session_common(Acc, JID, #state{host_type = HostType, {established, Acc1, NewStateData}. get_replaced_wait_timeout(HostType) -> - ejabberd_config:get_local_option_or_default({replaced_wait_timeout, HostType}, - default_replaced_wait_timeout()). + mongoose_config:get_opt({replaced_wait_timeout, HostType}, default_replaced_wait_timeout()). default_replaced_wait_timeout() -> 2000. @@ -2499,10 +2489,10 @@ fsm_limit_opts(Opts) -> {_, N} when is_integer(N) -> [{max_queue, N}]; _ -> - case ejabberd_config:get_local_option(max_fsm_queue) of - N when is_integer(N) -> + case mongoose_config:lookup_opt(max_fsm_queue) of + {ok, N} -> [{max_queue, N}]; - _ -> + {error, not_found} -> [] end end. diff --git a/src/ejabberd_config.erl b/src/ejabberd_config.erl deleted file mode 100644 index 663d7667e27..00000000000 --- a/src/ejabberd_config.erl +++ /dev/null @@ -1,224 +0,0 @@ -%%%---------------------------------------------------------------------- -%%% File : ejabberd_config.erl -%%% Author : Alexey Shchepin -%%% Purpose : Load config file -%%% Created : 14 Dec 2002 by Alexey Shchepin -%%% -%%% -%%% ejabberd, Copyright (C) 2002-2011 ProcessOne -%%% -%%% This program is free software; you can redistribute it and/or -%%% modify it under the terms of the GNU General Public License as -%%% published by the Free Software Foundation; either version 2 of the -%%% License, or (at your option) any later version. -%%% -%%% This program is distributed in the hope that it will be useful, -%%% but WITHOUT ANY WARRANTY; without even the implied warranty of -%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -%%% General Public License for more details. -%%% -%%% You should have received a copy of the GNU General Public License -%%% along with this program; if not, write to the Free Software -%%% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -%%% -%%%---------------------------------------------------------------------- - --module(ejabberd_config). --author('alexey@process-one.net'). - --export([start/0, - load_file/1, - add_local_option/2, - get_local_option/1, - del_local_option/1, - get_local_option_or_default/2]). - --export([get_local_config/0, - get_host_local_config/0, - get_config_path/0]). - -%% Helper function to get all options in the shell --export([get_categorized_options/0]). - -%% Introspection --export([config_state/0]). --export([config_states/0]). - --export([other_cluster_nodes/0]). - --ignore_xref([config_state/0, config_states/0, get_categorized_options/0, - get_host_local_config/0, get_local_config/0]). - --include("mongoose.hrl"). --include("ejabberd_config.hrl"). - --type state() :: mongoose_config_parser:state(). --type key() :: mongoose_config_parser:key(). --type value() :: mongoose_config_parser:value(). - --type categorized_options() :: #{local_config => list(), - host_config => list()}. - --spec start() -> ok. -start() -> - mnesia:create_table(local_config, - [{ram_copies, [node()]}, - {storage_properties, - [{ets, [{read_concurrency, true}]}]}, - {local_content, true}, - {attributes, record_info(fields, local_config)}]), - mnesia:add_table_copy(local_config, node(), ram_copies), - Config = get_config_path(), - ejabberd_config:load_file(Config), - %% This start time is used by mod_last: - add_local_option(node_start, {node_start, erlang:system_time(second)}), - ok. - - -%% @doc Get the filename of the ejabberd configuration file. -%% The filename can be specified with: erl -config "/path/to/mongooseim.toml". -%% It can also be specified with the environment variable EJABBERD_CONFIG_PATH. -%% If not specified, the default value 'mongooseim.toml' is assumed. --spec get_config_path() -> string(). -get_config_path() -> - DefaultPath = case os:getenv("EJABBERD_CONFIG_PATH") of - false -> - ?CONFIG_PATH; - Path -> - Path - end, - - application:get_env(mongooseim, config, DefaultPath). - -%% @doc Load the ejabberd configuration file. -%% It also includes additional configuration files and replaces macros. -%% This function will crash if finds some error in the configuration file. --spec load_file(File :: string()) -> ok. -load_file(File) -> - State = mongoose_config_parser:parse_file(File), - set_opts(State). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%% Process terms - --spec set_opts(state()) -> 'ok' | none(). -set_opts(State) -> - case mnesia:transaction(fun() -> do_set_opts(State) end) of - {atomic, _} -> ok; - {aborted, {no_exists, Table}} -> - handle_table_does_not_exist_error(Table) - end. - --spec do_set_opts(state()) -> 'ok' | none(). -do_set_opts(State) -> - Opts = mongoose_config_parser:state_to_opts(State), - lists:foreach(fun(R) -> mnesia:write(R) end, Opts). - --spec add_local_option(Opt :: key(), Val :: value()) -> {atomic|aborted, _}. -add_local_option(Opt, Val) -> - mnesia:transaction(fun() -> - mnesia:write(#local_config{key = Opt, - value = Val}) - end). - --spec del_local_option(Opt :: key()) -> {atomic | aborted, _}. -del_local_option(Opt) -> - mnesia:transaction(fun mnesia:delete/1, [{local_config, Opt}]). - --spec get_local_option(key()) -> value() | undefined. -get_local_option(Opt) -> - case ets:lookup(local_config, Opt) of - [#local_config{value = Val}] -> - Val; - _ -> - undefined - end. - --spec get_local_option_or_default(key(), value()) -> value(). -get_local_option_or_default(Opt, Default) -> - case get_local_option(Opt) of - undefined -> - Default; - Value -> - Value - end. - -handle_table_does_not_exist_error(Table) -> - MnesiaDirectory = mnesia:system_info(directory), - Msg = <<"Error reading Mnesia database spool files:~n" - "The Mnesia database couldn't read the spool file for the table.~n" - "ejabberd needs read and write access in the directory.~n" - "Maybe the problem is a change in the computer hostname,~n" - "or a change in the Erlang node name.~n" - "Check the ejabberd guide for details about changing the~n" - "computer hostname or Erlang node name.~n">>, - ?LOG_ERROR(#{what => error_reading_mnesia_db, text => Msg, - table => Table, directory => MnesiaDirectory, node => node()}), - exit("Error reading Mnesia database"). - -%% match all hosts --spec get_host_local_config() -> [{local_config, {term(), jid:server()}, term()}]. -get_host_local_config() -> - mnesia:dirty_match_object({local_config, {'_', '_'}, '_'}). - --spec get_local_config() -> [{local_config, term(), term()}]. -get_local_config() -> - Keys = lists:filter(fun is_not_host_specific/1, mnesia:dirty_all_keys(local_config)), - lists:flatten(lists:map(fun(Key) -> - mnesia:dirty_read(local_config, Key) - end, - Keys)). - --spec is_not_host_specific(atom() - | {atom(), jid:server()} - | {atom(), atom(), atom()}) -> boolean(). -is_not_host_specific(Key) when is_atom(Key) -> - true; -is_not_host_specific({Key, Host}) when is_atom(Key), is_binary(Host) -> - false; -is_not_host_specific({Key, PoolType, PoolName}) - when is_atom(Key), is_atom(PoolType), is_atom(PoolName) -> - true. - -%% @doc Returns current all options in memory, grouped by category. --spec get_categorized_options() -> categorized_options(). -get_categorized_options() -> - #{local_config => get_local_config(), - host_config => get_host_local_config()}. - -%% @doc Returns configs on disc and in memory for this node for inspection. -config_state() -> - ConfigFile = get_config_path(), - State = mongoose_config_parser:parse_file(ConfigFile), - #{mongoose_node => node(), - config_file => ConfigFile, - loaded_categorized_options => get_categorized_options(), - ondisc_config_state => State}. - -config_states() -> - config_states(all_cluster_nodes()). - -%% @doc Returns config states from all nodes in cluster -%% State from the local node comes as head of a list -config_states(Nodes) -> - {S, F} = rpc:multicall(Nodes, ?MODULE, config_state, [], 30000), - case F of - [] -> - S; - [_|_] -> - erlang:error(#{issue => config_state_failed, - cluster_nodes => Nodes, - failed_nodes => F}) - end. - -all_cluster_nodes() -> - [node()|other_cluster_nodes()]. - --spec other_cluster_nodes() -> [node()]. -other_cluster_nodes() -> - lists:filter(fun is_mongooseim_node/1, nodes()). - --spec is_mongooseim_node(node()) -> boolean(). -is_mongooseim_node(Node) -> - Apps = rpc:call(Node, application, which_applications, []), - lists:keymember(mongooseim, 1, Apps). diff --git a/src/ejabberd_cowboy.erl b/src/ejabberd_cowboy.erl index 26f6ce94903..0a7ff696ab7 100644 --- a/src/ejabberd_cowboy.erl +++ b/src/ejabberd_cowboy.erl @@ -120,10 +120,10 @@ handler({Port, IP, tcp}) -> -spec execute(cowboy_req:req(), cowboy_middleware:env()) -> {ok, cowboy_req:req(), cowboy_middleware:env()}. execute(Req, Env) -> - case ejabberd_config:get_local_option(cowboy_server_name) of - undefined -> + case mongoose_config:lookup_opt(cowboy_server_name) of + {error, not_found} -> {ok, Req, Env}; - ServerName -> + {ok, ServerName} -> {ok, cowboy_req:set_resp_header(<<"server">>, ServerName, Req), Env} end. @@ -226,8 +226,7 @@ get_routes([], Routes) -> get_routes([{Host, BasePath, Module} | Tail], Routes) -> get_routes([{Host, BasePath, Module, []} | Tail], Routes); get_routes([{Host, BasePath, Module, Opts} | Tail], Routes) -> - %% ejabberd_config tries to expand the atom '_' as a Macro, which fails. - %% To work around that, use "_" instead and translate it to '_' here. + %% "_" is used in TOML and translated to '_' here. CowboyHost = case Host of "_" -> '_'; _ -> Host diff --git a/src/ejabberd_ctl.erl b/src/ejabberd_ctl.erl index b52ca5e7c84..a2883d2dbae 100644 --- a/src/ejabberd_ctl.erl +++ b/src/ejabberd_ctl.erl @@ -247,10 +247,7 @@ process2(Args, Auth, AccessCommands) -> -spec get_accesscommands() -> [char() | tuple()]. get_accesscommands() -> - case ejabberd_config:get_local_option(mongooseimctl_access_commands) of - ACs when is_list(ACs) -> ACs; - _ -> [] - end. + mongoose_config:get_opt(mongooseimctl_access_commands, []). %%----------------------------- diff --git a/src/ejabberd_listener.erl b/src/ejabberd_listener.erl index 43c75c27f92..8f1aecfd541 100644 --- a/src/ejabberd_listener.erl +++ b/src/ejabberd_listener.erl @@ -64,10 +64,10 @@ init(_) -> -spec start_listeners() -> 'ignore' | {'ok', {{_, _, _}, [any()]}}. start_listeners() -> - case ejabberd_config:get_local_option(listen) of - undefined -> + case mongoose_config:lookup_opt(listen) of + {error, not_found} -> ignore; - Ls -> + {ok, Ls} -> Ls2 = lists:map( fun({Port, Module, Opts}) -> case start_listener(Port, Module, Opts) of @@ -258,7 +258,7 @@ opts_to_listener_args(PortIPProto, RawOpts) -> -spec stop_listeners() -> 'ok'. stop_listeners() -> - Ports = ejabberd_config:get_local_option(listen), + Ports = mongoose_config:get_opt(listen, []), lists:foreach( fun({PortIpNetp, Module, _Opts}) -> stop_listener(PortIpNetp, Module) @@ -283,15 +283,10 @@ add_listener(PortIPProto, Module, Opts) -> PortIP1 = {Port, IPT, Proto}, case start_listener(PortIP1, Module, Opts) of {ok, _Pid} -> - Ports = case ejabberd_config:get_local_option(listen) of - undefined -> - []; - Ls -> - Ls - end, + Ports = mongoose_config:get_opt(listen, []), Ports1 = lists:keydelete(PortIP1, 1, Ports), Ports2 = [{PortIP1, Module, Opts} | Ports1], - ejabberd_config:add_local_option(listen, Ports2), + mongoose_config:set_opt(listen, Ports2), ok; {error, Error} -> {error, Error} @@ -311,14 +306,9 @@ delete_listener(PortIPProto, Module, Opts) -> %% this one stops a listener and deletes it from configuration, used while reloading config {Port, IPT, _, _, Proto, _} = parse_listener_portip(PortIPProto, Opts), PortIP1 = {Port, IPT, Proto}, - Ports = case ejabberd_config:get_local_option(listen) of - undefined -> - []; - Ls -> - Ls - end, + Ports = mongoose_config:get_opt(listen, []), Ports1 = lists:keydelete(PortIP1, 1, Ports), - ejabberd_config:add_local_option(listen, Ports1), + mongoose_config:set_opt(listen, Ports1), stop_listener(PortIP1, Module). %%% diff --git a/src/ejabberd_rdbms.erl b/src/ejabberd_rdbms.erl index fe37e020345..11070f12945 100644 --- a/src/ejabberd_rdbms.erl +++ b/src/ejabberd_rdbms.erl @@ -36,7 +36,7 @@ start() -> compile_rdbms_type_helper() -> %% TODO This parameter should not be global, but pool-name parameterized - Type = ejabberd_config:get_local_option(rdbms_server_type), + Type = mongoose_config:get_opt(rdbms_server_type, generic), CodeStr = rdbms_type_helper(Type), {Mod, Code} = dynamic_compile:from_string(CodeStr), code:load_binary(Mod, "mongoose_rdbms_type.erl", Code). diff --git a/src/ejabberd_router.erl b/src/ejabberd_router.erl index 639da6879e4..972f4886c0f 100644 --- a/src/ejabberd_router.erl +++ b/src/ejabberd_router.erl @@ -515,10 +515,7 @@ routing_modules_list() -> mod_routing_machine:get_routing_module_list(). compile_routing_module() -> - Mods = case ejabberd_config:get_local_option(routing_modules) of - undefined -> default_routing_modules(); - Defined -> Defined - end, + Mods = mongoose_config:get_opt(routing_modules, default_routing_modules()), CodeStr = make_routing_module_source(Mods), {Mod, Code} = dynamic_compile:from_string(CodeStr), code:load_binary(Mod, "mod_routing_machine.erl", Code). diff --git a/src/ejabberd_s2s.erl b/src/ejabberd_s2s.erl index 0c3d6de04cf..c510220abb6 100644 --- a/src/ejabberd_s2s.erl +++ b/src/ejabberd_s2s.erl @@ -458,10 +458,10 @@ needed_connections_number(Ls, MaxS2SConnectionsNumber, -spec is_service(jid:jid(), jid:jid()) -> boolean(). is_service(From, To) -> LFromDomain = From#jid.lserver, - case ejabberd_config:get_local_option({route_subdomains, LFromDomain}) of - s2s -> % bypass RFC 3920 10.3 + case mongoose_config:lookup_opt({route_subdomains, LFromDomain}) of + {ok, s2s} -> % bypass RFC 3920 10.3 false; - _ -> + {error, not_found} -> Hosts = ?MYHOSTS, P = fun(ParentDomain) -> lists:member(ParentDomain, Hosts) end, lists:any(P, parent_domains(To#jid.lserver)) @@ -542,12 +542,15 @@ allow_host(MyServer, S2SHost) -> end. allow_host1(MyHost, S2SHost) -> - case ejabberd_config:get_local_option({{s2s_host, S2SHost}, MyHost}) of - deny -> false; - allow -> true; - _ -> - case ejabberd_config:get_local_option({s2s_default_policy, MyHost}) of - deny -> false; + case mongoose_config:lookup_opt({{s2s_host, S2SHost}, MyHost}) of + {ok, deny} -> + false; + {ok, allow} -> + true; + {error, not_found} -> + case mongoose_config:lookup_opt({s2s_default_policy, MyHost}) of + {ok, deny} -> + false; _ -> mongoose_hooks:s2s_allow_host(MyHost, S2SHost) /= deny end @@ -592,14 +595,14 @@ get_s2s_state(S2sPid)-> [{s2s_pid, S2sPid} | Infos]. get_or_generate_secret() -> - case ejabberd_config:get_local_option(s2s_shared) of - undefined -> + case mongoose_config:lookup_opt(s2s_shared) of + {error, not_found} -> generate_and_store_secret(); - Secret -> + {ok, Secret} -> Secret end. generate_and_store_secret() -> Secret = base16:encode(crypto:strong_rand_bytes(10)), - ejabberd_config:add_local_option(s2s_shared, Secret), + mongoose_config:set_opt(s2s_shared, Secret), Secret. diff --git a/src/ejabberd_s2s_in.erl b/src/ejabberd_s2s_in.erl index 877d85a3990..7155c83cb64 100644 --- a/src/ejabberd_s2s_in.erl +++ b/src/ejabberd_s2s_in.erl @@ -136,12 +136,12 @@ init([{SockMod, Socket}, Opts]) -> {value, {_, S}} -> S; _ -> none end, - UseTLS = ejabberd_config:get_local_option(s2s_use_starttls), + UseTLS = mongoose_config:get_opt(s2s_use_starttls, false), {StartTLS, TLSRequired, TLSCertVerify} = get_tls_params(UseTLS), - TLSOpts1 = case ejabberd_config:get_local_option(s2s_certfile) of - undefined -> + TLSOpts1 = case mongoose_config:lookup_opt(s2s_certfile) of + {error, not_found} -> []; - CertFile -> + {ok, CertFile} -> [{certfile, CertFile}] end, TLSOpts2 = lists:filter(fun({protocol_options, _}) -> true; @@ -251,16 +251,11 @@ wait_for_feature_request({xmlstreamelement, El}, StateData) -> SockMod == gen_tcp -> ?LOG_DEBUG(#{what => s2s_starttls}), Socket = StateData#state.socket, - TLSOpts = case ejabberd_config:get_local_option( - {domain_certfile, - StateData#state.server}) of - undefined -> + TLSOpts = case mongoose_config:lookup_opt({domain_certfile, StateData#state.server}) of + {error, not_found} -> StateData#state.tls_options; - CertFile -> - [{certfile, CertFile} | - lists:keydelete( - certfile, 1, - StateData#state.tls_options)] + {ok, CertFile} -> + lists:keystore(certfile, 1, StateData#state.tls_options, {certfile, CertFile}) end, TLSSocket = (StateData#state.sockmod):starttls( Socket, TLSOpts, @@ -718,8 +713,6 @@ handle_auth_res(_, _, StateData) -> {stop, normal, StateData}. -get_tls_params(undefined) -> - {false, false, false}; get_tls_params(false) -> {false, false, false}; get_tls_params(true) -> diff --git a/src/ejabberd_s2s_out.erl b/src/ejabberd_s2s_out.erl index d00813baa81..4933b9170f0 100644 --- a/src/ejabberd_s2s_out.erl +++ b/src/ejabberd_s2s_out.erl @@ -121,7 +121,7 @@ %% Maximum delay to wait before retrying to connect after a failed attempt. %% Specified in milliseconds. Default value is 5 minutes. --define(MAX_RETRY_DELAY, 300000). +-define(MAX_RETRY_DELAY, 300). -define(STREAM_HEADER(From, To, Other), <<"", @@ -187,8 +187,8 @@ init([From, Server, Type]) -> ?LOG_DEBUG(#{what => s2s_out_started, text => <<"New outgoing s2s connection">>, from => From, server => Server, type => Type}), - {TLS, TLSRequired} = case ejabberd_config:get_local_option(s2s_use_starttls) of - UseTls when (UseTls==undefined) or (UseTls==false) -> + {TLS, TLSRequired} = case mongoose_config:get_opt(s2s_use_starttls, false) of + UseTls when (UseTls==false) -> {false, false}; UseTls when (UseTls==true) or (UseTls==optional) -> {true, false}; @@ -196,17 +196,17 @@ init([From, Server, Type]) -> {true, true} end, UseV10 = TLS, - TLSOpts = case ejabberd_config:get_local_option(s2s_certfile) of - undefined -> + TLSOpts = case mongoose_config:lookup_opt(s2s_certfile) of + {error, not_found} -> [connect]; - CertFile -> + {ok, CertFile} -> [{certfile, CertFile}, connect] end, - TLSOpts2 = case ejabberd_config:get_local_option(s2s_ciphers) of - undefined -> - TLSOpts; - Ciphers -> - [{ciphers, Ciphers} | TLSOpts] + TLSOpts2 = case mongoose_config:lookup_opt(s2s_ciphers) of + {error, not_found} -> + TLSOpts; + {ok, Ciphers} -> + [{ciphers, Ciphers} | TLSOpts] end, {New, Verify} = case Type of new -> @@ -998,10 +998,7 @@ get_addr_port(Server) -> -spec srv_lookup(jid:server()) -> {'error', atom()} | {'ok', inet:hostent()}. srv_lookup(Server) -> - Options = case ejabberd_config:get_local_option(s2s_dns_options) of - L when is_list(L) -> L; - _ -> [] - end, + Options = mongoose_config:get_opt(s2s_dns_options, []), TimeoutMs = timer:seconds(proplists:get_value(timeout, Options, 10)), Retries = proplists:get_value(retries, Options, 2), srv_lookup(Server, TimeoutMs, Retries). @@ -1073,47 +1070,27 @@ get_addrs(Host, Family) -> -spec outgoing_s2s_port() -> integer(). outgoing_s2s_port() -> - case ejabberd_config:get_local_option(outgoing_s2s_port) of - Port when is_integer(Port) -> - Port; - undefined -> - 5269 - end. + mongoose_config:get_opt(outgoing_s2s_port, 5269). -spec outgoing_s2s_families() -> ['ipv4' | 'ipv6', ...]. outgoing_s2s_families() -> - case ejabberd_config:get_local_option(outgoing_s2s_families) of - Families when is_list(Families) -> - Families; - undefined -> - %% DISCUSSION: Why prefer IPv4 first? - %% - %% IPv4 connectivity will be available for everyone for - %% many years to come. So, there's absolutely no benefit - %% in preferring IPv6 connections which are flaky at best - %% nowadays. - %% - %% On the other hand content providers hesitate putting up - %% AAAA records for their sites due to the mentioned - %% quality of current IPv6 connectivity. Making IPv6 the a - %% `fallback' may avoid these problems elegantly. - [ipv4, ipv6] - end. - + %% DISCUSSION: Why prefer IPv4 first? + %% + %% IPv4 connectivity will be available for everyone for + %% many years to come. So, there's absolutely no benefit + %% in preferring IPv6 connections which are flaky at best + %% nowadays. + %% + %% On the other hand content providers hesitate putting up + %% AAAA records for their sites due to the mentioned + %% quality of current IPv6 connectivity. Making IPv6 the a + %% `fallback' may avoid these problems elegantly. + mongoose_config:get_opt(outgoing_s2s_families, [ipv4, ipv6]). -spec outgoing_s2s_timeout() -> non_neg_integer() | infinity. outgoing_s2s_timeout() -> - case ejabberd_config:get_local_option(outgoing_s2s_timeout) of - Timeout when is_integer(Timeout) -> - Timeout; - infinity -> - infinity; - undefined -> - %% 10 seconds - 10000 - end. - + mongoose_config:get_opt(outgoing_s2s_timeout, 10000). %% @doc Human readable S2S logging: Log only new outgoing connections as INFO %% Do not log dialback @@ -1170,12 +1147,7 @@ wait_before_reconnect(StateData) -> %% The default value is 5 minutes. %% The option {s2s_max_retry_delay, Seconds} can be used (in seconds). get_max_retry_delay() -> - case ejabberd_config:get_local_option(s2s_max_retry_delay) of - Seconds when is_integer(Seconds) -> - Seconds*1000; - _ -> - ?MAX_RETRY_DELAY - end. + mongoose_config:get_opt(s2s_max_retry_delay, ?MAX_RETRY_DELAY) * 1000. %% @doc Terminate s2s_out connections that are in state wait_before_retry @@ -1191,10 +1163,10 @@ terminate_if_waiting_delay(From, To) -> -spec fsm_limit_opts() -> [{'max_queue', integer()}]. fsm_limit_opts() -> - case ejabberd_config:get_local_option(max_fsm_queue) of - N when is_integer(N) -> + case mongoose_config:lookup_opt(max_fsm_queue) of + {ok, N} -> [{max_queue, N}]; - _ -> + {error, not_found} -> [] end. @@ -1216,14 +1188,14 @@ get_addr_list(Server) -> %% @doc Get IPs predefined for a given s2s domain in the configuration -spec get_predefined_addresses(jid:server()) -> [{inet:ip_address(), inet:port_number()}]. get_predefined_addresses(Server) -> - S2SAddr = ejabberd_config:get_local_option({s2s_addr, Server}), - do_get_predefined_addresses(S2SAddr). + case mongoose_config:lookup_opt({s2s_addr, Server}) of + {error, not_found} -> []; + {ok, S2SAddr} -> do_get_predefined_addresses(S2SAddr) + end. --spec do_get_predefined_addresses(undefined | string() | inet:ip_address() | +-spec do_get_predefined_addresses(string() | inet:ip_address() | {string() | inet:ip_address(), non_neg_integer()}) -> [{inet:ip_address(), non_neg_integer()}]. -do_get_predefined_addresses(undefined) -> - []; do_get_predefined_addresses({{_, _, _, _}, Port} = IP4Port) when is_integer(Port) -> [IP4Port]; do_get_predefined_addresses({{_, _, _, _, _, _, _, _}, Port} = IP6Port) when is_integer(Port) -> @@ -1274,22 +1246,18 @@ get_acc_with_new_tls(_, _, Acc) -> Acc. get_tls_opts_with_certfile(StateData) -> - case ejabberd_config:get_local_option( - {domain_certfile, StateData#state.myname}) of - undefined -> + case mongoose_config:lookup_opt({domain_certfile, StateData#state.myname}) of + {error, not_found} -> StateData#state.tls_options; - CertFile -> - [{certfile, CertFile} | - lists:keydelete( - certfile, 1, - StateData#state.tls_options)] + {ok, CertFile} -> + lists:keystore(certfile, 1, StateData#state.tls_options, {certfile, CertFile}) end. get_tls_opts_with_ciphers(TLSOpts) -> - case ejabberd_config:get_local_option(s2s_ciphers) of - undefined -> + case mongoose_config:lookup_opt(s2s_ciphers) of + {error, not_found} -> TLSOpts; - Ciphers -> + {ok, Ciphers} -> [{ciphers, Ciphers} | TLSOpts] end. diff --git a/src/ejabberd_service.erl b/src/ejabberd_service.erl index 419b0b0a19a..73e27858e69 100644 --- a/src/ejabberd_service.erl +++ b/src/ejabberd_service.erl @@ -447,10 +447,10 @@ fsm_limit_opts(Opts) -> {value, {_, N}} when is_integer(N) -> [{max_queue, N}]; _ -> - case ejabberd_config:get_local_option(max_fsm_queue) of - N when is_integer(N) -> + case mongoose_config:lookup_opt(max_fsm_queue) of + {ok, N} -> [{max_queue, N}]; - _ -> + {error, not_found} -> [] end end. @@ -521,7 +521,7 @@ unregister_routes(StateData) -> ejabberd_router:unregister_components(Routes). get_routes(#state{host=Subdomain, is_subdomain=true}) -> - Hosts = ejabberd_config:get_local_option(hosts), + Hosts = mongoose_config:get_opt(hosts, []), component_routes(Subdomain, Hosts); get_routes(#state{host=Host}) -> [Host]. diff --git a/src/ejabberd_sm.erl b/src/ejabberd_sm.erl index d9856f9ba56..44b10bd880f 100644 --- a/src/ejabberd_sm.erl +++ b/src/ejabberd_sm.erl @@ -491,10 +491,7 @@ node_cleanup(Acc, Node) -> %%-------------------------------------------------------------------- -spec init(_) -> {ok, state()}. init([]) -> - {Backend, Opts} = case ejabberd_config:get_local_option(sm_backend) of - undefined -> {mnesia, []}; - Value -> Value - end, + {Backend, Opts} = mongoose_config:get_opt(sm_backend, {mnesia, []}), {Mod, Code} = dynamic_compile:from_string(sm_backend(Backend)), code:load_binary(Mod, "ejabberd_sm_backend.erl", Code), diff --git a/src/gen_mod.erl b/src/gen_mod.erl index 455d185bf75..4c8c4f70e41 100644 --- a/src/gen_mod.erl +++ b/src/gen_mod.erl @@ -140,7 +140,7 @@ start_module(HostType, Module, Opts) -> start_module_for_host_type(HostType, Module, Opts0) -> {links, LinksBefore} = erlang:process_info(self(), links), Opts = proplists:unfold(Opts0), - set_module_opts_mnesia(HostType, Module, Opts), + set_module_opts_in_config(HostType, Module, Opts), ets:insert(ejabberd_modules, #ejabberd_module{module_host_type = {Module, HostType}, opts = Opts}), try @@ -169,7 +169,7 @@ start_module_for_host_type(HostType, Module, Opts0) -> end catch Class:Reason:StackTrace -> - del_module_mnesia(HostType, Module), + del_module_from_config(HostType, Module), ets:delete(ejabberd_modules, {Module, HostType}), ErrorText = io_lib:format("Problem starting the module ~p for " "host_type ~p~n options: ~p~n ~p: ~p~n~p", @@ -243,10 +243,8 @@ stop_module(HostType, Module) -> stop_module_for_host_type(HostType, Module) -> case stop_module_keep_config(HostType, Module) of {ok, Opts} -> - case del_module_mnesia(HostType, Module) of - {atomic, _} -> {ok, Opts}; - E -> {error, E} - end; + del_module_from_config(HostType, Module), + {ok, Opts}; {error, E} -> {error, E} end. @@ -467,37 +465,22 @@ hosts_and_opts_with_module(Module) -> opts = '$2'}, [], [{{'$1', '$2'}}]}]). --spec set_module_opts_mnesia(host_type(), module(), [any()]) -> - {'aborted', _} | {'atomic', _}. -set_module_opts_mnesia(HostType, Module, Opts0) -> +-spec set_module_opts_in_config(host_type(), module(), [any()]) -> ok. +set_module_opts_in_config(HostType, Module, Opts0) -> %% this function is not atomic! Opts = proplists:unfold(Opts0), - Modules = case ejabberd_config:get_local_option({modules, HostType}) of - undefined -> - []; - Ls -> - Ls - end, + Modules = mongoose_config:get_opt({modules, HostType}, []), Modules1 = lists:keydelete(Module, 1, Modules), Modules2 = [{Module, Opts} | Modules1], - ejabberd_config:add_local_option({modules, HostType}, Modules2). + mongoose_config:set_opt({modules, HostType}, Modules2). --spec del_module_mnesia(host_type(), module()) -> {'aborted', _} | {'atomic', _}. -del_module_mnesia(HostType, Module) -> +-spec del_module_from_config(host_type(), module()) -> ok. +del_module_from_config(HostType, Module) -> %% this function is not atomic! - Modules = case ejabberd_config:get_local_option({modules, HostType}) of - undefined -> - []; - Ls -> - Ls - end, - case lists:keydelete(Module, 1, Modules) of - [] -> - ejabberd_config:del_local_option({modules, HostType}); - OtherModules -> - ejabberd_config:add_local_option({modules, HostType}, OtherModules) - end. + Modules = mongoose_config:get_opt({modules, HostType}, []), + OtherModules = lists:keydelete(Module, 1, Modules), + mongoose_config:set_opt({modules, HostType}, OtherModules). -spec get_module_proc(binary() | string(), module()) -> atom(). %% TODO: diff --git a/src/global_distrib/mod_global_distrib_server_mgr.erl b/src/global_distrib/mod_global_distrib_server_mgr.erl index c298e7814e5..1c7f3348035 100644 --- a/src/global_distrib/mod_global_distrib_server_mgr.erl +++ b/src/global_distrib/mod_global_distrib_server_mgr.erl @@ -406,9 +406,9 @@ refresh_connections(#state{ server = Server, pending_endpoints = PendingEndpoint -spec get_endpoints(Server :: jid:lserver()) -> {ok, [mod_global_distrib_utils:endpoint()]}. get_endpoints(Server) -> {ok, EndpointsToResolve} = - case ejabberd_config:get_local_option({global_distrib_addr, Server}) of - undefined -> mod_global_distrib_mapping:endpoints(Server); - Endpoints -> {ok, Endpoints} + case mongoose_config:lookup_opt({global_distrib_addr, Server}) of + {error, not_found} -> mod_global_distrib_mapping:endpoints(Server); + {ok, Endpoints} -> {ok, Endpoints} end, Resolved = mod_global_distrib_utils:resolve_endpoints(EndpointsToResolve), {ok, Resolved}. diff --git a/src/mam/mod_mam_meta.erl b/src/mam/mod_mam_meta.erl index 7e712b81b89..456384f8d15 100644 --- a/src/mam/mod_mam_meta.erl +++ b/src/mam/mod_mam_meta.erl @@ -134,7 +134,7 @@ get_mam_module_configuration(Host, MamModule, DefaultValue) -> %% initialised on module startup but can be changed runtime via %% gen_mod interfaces. removed when module is stopped. %% - %% * local_config mnesia table - managed by ejabberd_config, but + %% * mongoose_config, but %% it's only gen_mod changing stored configuration of the modules. %% changes are done in the next way: configuration is stored when %% module is started, removed - when stopped, updated on module @@ -144,7 +144,7 @@ get_mam_module_configuration(Host, MamModule, DefaultValue) -> %% gen_mod interfaces and also (theoretically) modules can be stopped %% using gen_mod:stop_module_keep_config/2 interface, so local_config %% mnesia table is more preferable source of the configuration. - Modules = ejabberd_config:get_local_option({modules, Host}), + Modules = mongoose_config:get_opt({modules, Host}), case proplists:get_value(MamModule, Modules) of undefined -> case proplists:get_value(?MODULE, Modules) of diff --git a/src/metrics/mongoose_metrics.erl b/src/metrics/mongoose_metrics.erl index 58ba76cca94..638d19cf5e8 100644 --- a/src/metrics/mongoose_metrics.erl +++ b/src/metrics/mongoose_metrics.erl @@ -187,9 +187,9 @@ remove_all_metrics() -> %% Internal functions %% --------------------------------------------------------------------- --spec all_metrics_are_global() -> boolean() | undefined. +-spec all_metrics_are_global() -> boolean(). all_metrics_are_global() -> - ejabberd_config:get_local_option(all_metrics_are_global). + mongoose_config:get_opt(all_metrics_are_global, false). pick_by_all_metrics_are_global(WhenGlobal, WhenNot) -> case all_metrics_are_global() of diff --git a/src/mod_last.erl b/src/mod_last.erl index 9f83d58a129..12d4f3a95e5 100644 --- a/src/mod_last.erl +++ b/src/mod_last.erl @@ -171,10 +171,10 @@ process_local_iq(Acc, _From, _To, #iq{type = Type, sub_el = SubEl} = IQ, _Extra) -spec get_node_uptime() -> non_neg_integer(). get_node_uptime() -> - case ejabberd_config:get_local_option(node_start) of - {node_start, Seconds} when is_integer(Seconds) -> + case mongoose_config:lookup_opt(node_start) of + {ok, {node_start, Seconds}} -> erlang:system_time(second) - Seconds; - _Undefined -> + {error, not_found} -> trunc(element(1, erlang:statistics(wall_clock))/1000) end. diff --git a/src/mod_muc_log.erl b/src/mod_muc_log.erl index a36636caf8d..85a73496b2d 100644 --- a/src/mod_muc_log.erl +++ b/src/mod_muc_log.erl @@ -208,14 +208,6 @@ init([Host, Opts]) -> {TL1, TL2} = gen_mod:get_opt(top_link, Opts, {"/", "Home"}), TopLink = {list_to_binary(TL1), list_to_binary(TL2)}, NoFollow = gen_mod:get_opt(spam_prevention, Opts, true), - Lang = case ejabberd_config:get_local_option({language, Host}) of - undefined -> - case ejabberd_config:get_local_option(language) of - undefined -> <<"en">>; - L -> L - end; - L -> L - end, {ok, #logstate{host = Host, out_dir = OutDir, dir_type = DirType, @@ -223,7 +215,7 @@ init([Host, Opts]) -> file_format = FileFormat, css_file = CSSFile, access = AccessLog, - lang = Lang, + lang = ?MYLANG, timezone = Timezone, spam_prevention = NoFollow, top_link = TopLink}}. diff --git a/src/mod_register.erl b/src/mod_register.erl index dbe54aebf63..a6c95e51e86 100644 --- a/src/mod_register.erl +++ b/src/mod_register.erl @@ -412,10 +412,7 @@ send_registration_notification(JIDBin, Domain, Body) -> check_timeout(undefined) -> true; check_timeout(Source) -> - Timeout = case ejabberd_config:get_local_option(registration_timeout) of - undefined -> 600; - TO -> TO - end, + Timeout = mongoose_config:get_opt(registration_timeout, 600), case is_integer(Timeout) of true -> Priority = -(erlang:system_time(second)), diff --git a/src/mod_shared_roster_ldap.erl b/src/mod_shared_roster_ldap.erl index 81ba8474235..57fd756c8b0 100644 --- a/src/mod_shared_roster_ldap.erl +++ b/src/mod_shared_roster_ldap.erl @@ -43,8 +43,6 @@ get_jid_info/4, process_item/2, in_subscription/5, out_subscription/4]). --export([config_change/4]). - -ignore_xref([config_change/4, get_jid_info/4, get_subscription_lists/2, get_user_roster/2, in_subscription/5, out_subscription/4, process_item/2, start_link/2]). @@ -255,19 +253,6 @@ process_subscription(Direction, #jid{luser = LUser, lserver = LServer}, ToJID, _ false -> false end. -%%==================================================================== -%% config change hook -%%==================================================================== -%% react to "global" config change -config_change(Acc, Host, ldap, _NewConfig) -> - Proc = gen_mod:get_module_proc(Host, ?MODULE), - Mods = ejabberd_config:get_local_option({modules, Host}), - Opts = proplists:get_value(?MODULE, Mods, []), - ok = gen_server:call(Proc, {new_config, Host, Opts}), - Acc; -config_change(Acc, _, _, _) -> - Acc. - %%==================================================================== %% gen_server callbacks @@ -281,8 +266,6 @@ init([Host, Opts]) -> cache_tab:new(shared_roster_ldap_group, [{max_size, State#state.group_cache_size}, {lru, false}, {life_time, State#state.group_cache_validity}]), - ejabberd_hooks:add(host_config_update, Host, ?MODULE, - config_change, 50), ejabberd_hooks:add(roster_get, Host, ?MODULE, get_user_roster, 70), ejabberd_hooks:add(roster_in_subscription, Host, ?MODULE, @@ -308,7 +291,6 @@ handle_info(_Info, State) -> {noreply, State}. terminate(_Reason, State) -> Host = State#state.host, - ejabberd_hooks:delete(host_config_update, Host, ?MODULE, config_change, 50), ejabberd_hooks:delete(roster_get, Host, ?MODULE, get_user_roster, 70), ejabberd_hooks:delete(roster_in_subscription, Host, diff --git a/src/mod_vcard.erl b/src/mod_vcard.erl index 8b7f9c7982f..58c8d1f366b 100644 --- a/src/mod_vcard.erl +++ b/src/mod_vcard.erl @@ -78,8 +78,6 @@ -export([get_default_reported_fields/1]). -export([default_host/0]). --export([config_change/4]). - %% GDPR related -export([get_personal_data/3]). @@ -245,7 +243,6 @@ hooks2() -> [{remove_user, remove_user, 50}, {anonymous_purge_hook, remove_user, 50}, {remove_domain, remove_domain, 50}, - {host_config_update, config_change, 50}, {set_vcard, set_vcard, 50}, {get_personal_data, get_personal_data, 50}]. @@ -562,22 +559,6 @@ remove_user(Acc, User, Server) -> mod_vcard_backend:remove_user(HostType, LUser, LServer), Acc. -%% react to "global" config change -config_change(Acc, Host, ldap, _NewConfig) -> - case mod_vcard_backend:backend() of - mod_vcard_ldap -> - Mods = ejabberd_config:get_local_option({modules, Host}), - Opts = proplists:get_value(?MODULE, Mods, []), - gen_mod:stop_module(Host, ?MODULE), - gen_mod:start_module(Host, ?MODULE, Opts); - _ -> - ok - end, - %ok = gen_server:call(Proc, {new_config, Host, Opts}), - Acc; -config_change(Acc, _, _, _) -> - Acc. - %% ------------------------------------------------------------------ %% Internal %% ------------------------------------------------------------------ diff --git a/src/mongoose_service.erl b/src/mongoose_service.erl index cdd3a5f1be9..fedbad99ba3 100644 --- a/src/mongoose_service.erl +++ b/src/mongoose_service.erl @@ -81,7 +81,7 @@ config_spec(Service) -> -spec ensure_loaded(service()) -> ok. ensure_loaded(Service) -> - Options = ejabberd_config:get_local_option_or_default(services, []), + Options = mongoose_config:get_opt(services, []), start_service(Service, proplists:get_value(Service, Options)), ok. diff --git a/src/rdbms/mongoose_rdbms_odbc.erl b/src/rdbms/mongoose_rdbms_odbc.erl index 2f37b72f0b0..0c3327c5b49 100644 --- a/src/rdbms/mongoose_rdbms_odbc.erl +++ b/src/rdbms/mongoose_rdbms_odbc.erl @@ -239,7 +239,7 @@ map_param(Param, Mapper) -> -spec server_type() -> atom(). server_type() -> - ejabberd_config:get_local_option(rdbms_server_type). + mongoose_config:get_opt(rdbms_server_type, generic). -spec escape_binary(ServerType :: atom(), binary()) -> iodata(). escape_binary(pgsql, Bin) -> diff --git a/src/sasl/cyrsasl.erl b/src/sasl/cyrsasl.erl index bd6c34ec255..37d263d8577 100644 --- a/src/sasl/cyrsasl.erl +++ b/src/sasl/cyrsasl.erl @@ -143,10 +143,7 @@ server_step(State, ClientIn) -> -spec get_modules(binary()) -> [sasl_module()]. get_modules(HostType) -> - case ejabberd_config:get_local_option({sasl_mechanisms, HostType}) of - undefined -> ejabberd_config:get_local_option_or_default(sasl_mechanisms, default_modules()); - Modules -> Modules - end. + mongoose_config:get_opt({sasl_mechanisms, HostType}, default_modules()). default_modules() -> [cyrsasl_scram_sha512_plus, diff --git a/src/shaper.erl b/src/shaper.erl index 1d461913eb4..b7c2655d920 100644 --- a/src/shaper.erl +++ b/src/shaper.erl @@ -31,12 +31,12 @@ -spec new(atom()) -> shaper(). new(Name) -> - case ejabberd_config:get_local_option({shaper, Name, global}) of - {maxrate, MaxRatePerSecond} -> + case mongoose_config:lookup_opt({shaper, Name, global}) of + {ok, {maxrate, MaxRatePerSecond}} -> #shaper{max_rate = MaxRatePerSecond, tokens = MaxRatePerSecond, last_update = erlang:monotonic_time(millisecond)}; - _ -> none + {error, not_found} -> none end. %% @doc Update shaper. diff --git a/src/system_metrics/mongoose_system_metrics_collector.erl b/src/system_metrics/mongoose_system_metrics_collector.erl index 4e0cf80a729..29d1ad6e4e8 100644 --- a/src/system_metrics/mongoose_system_metrics_collector.erl +++ b/src/system_metrics/mongoose_system_metrics_collector.erl @@ -139,7 +139,7 @@ filter_unknown_api(ApiList) -> [Api || Api <- ApiList, lists:member(Api, AllowedToReport)]. get_service_option(Service) -> - Listen = ejabberd_config:get_local_option_or_default(listen, []), + Listen = mongoose_config:get_opt(listen, []), Result = [ Option || {_, S, Option} <- Listen, S == Service], lists:flatten(Result). @@ -193,7 +193,7 @@ check_tls_specific_option() -> end. get_outgoing_pools() -> - OutgoingPools = ejabberd_config:get_local_option_or_default(outgoing_pools, []), + OutgoingPools = mongoose_config:get_opt(outgoing_pools, []), [#{report_name => outgoing_pools, key => type, value => Type} || {Type, _, _, _, _} <- OutgoingPools]. @@ -225,7 +225,7 @@ calculate_stanza_rate(PrevReport, NewCount) -> end} || {Type, Count} <- NewCount]. get_config_type() -> - ConfigPath = ejabberd_config:get_config_path(), + ConfigPath = mongoose_config:get_config_path(), ConfigType = case filename:extension(ConfigPath) of ".toml" -> toml; ".cfg" -> cfg; diff --git a/src/system_metrics/mongoose_system_metrics_sender.erl b/src/system_metrics/mongoose_system_metrics_sender.erl index ac2d59711aa..20f5b15207a 100644 --- a/src/system_metrics/mongoose_system_metrics_sender.erl +++ b/src/system_metrics/mongoose_system_metrics_sender.erl @@ -38,15 +38,15 @@ send_reports(ReportsList) -> end, ReportsList). get_url() -> - ejabberd_config:get_local_option_or_default(google_analytics_url, ?BASE_URL). + mongoose_config:get_opt(google_analytics_url, ?BASE_URL). get_tracking_ids() -> - DevTrackingId = ejabberd_config:get_local_option_or_default(google_analytics_tracking_id, ?TRACKING_ID), - ExtraTrackingId = ejabberd_config:get_local_option(extra_google_analytics_tracking_id), - case ExtraTrackingId of - undefined -> [DevTrackingId]; - ExtraTrackingId -> [DevTrackingId, ExtraTrackingId] + DevTrackingId = mongoose_config:get_opt(google_analytics_tracking_id, ?TRACKING_ID), + case mongoose_config:lookup_opt(extra_google_analytics_tracking_id) of + {error, not_found} -> [DevTrackingId]; + {ok, ExtraTrackingId} -> [DevTrackingId, ExtraTrackingId] end. + % % https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#batch-limitations % % A maximum of 20 hits can be specified per request. -spec flush_reports(url(), [google_analytics_report()]) -> {ok, term()} | {error, term()}. diff --git a/src/system_metrics/service_mongoose_system_metrics.erl b/src/system_metrics/service_mongoose_system_metrics.erl index 3c239c5a44c..3d7fc0f848c 100644 --- a/src/system_metrics/service_mongoose_system_metrics.erl +++ b/src/system_metrics/service_mongoose_system_metrics.erl @@ -40,7 +40,7 @@ -spec verify_if_configured() -> ok | ignore. verify_if_configured() -> - Services = ejabberd_config:get_local_option_or_default(services, []), + Services = mongoose_config:get_opt(services, []), case proplists:is_defined(?MODULE, Services) of false -> %% Technically, notice level. @@ -139,17 +139,22 @@ get_client_id() -> -spec metrics_module_config(list()) -> {non_neg_integer(), non_neg_integer()}. metrics_module_config(Args) -> {InitialReport, ReportAfter} = get_timeouts(Args, os:getenv("CI")), - ExtraTrackingID = proplists:get_value(tracking_id, Args, undefined), - ejabberd_config:add_local_option(extra_google_analytics_tracking_id, ExtraTrackingID), + case proplists:lookup(tracking_id, Args) of + none -> + % There might be a leftover option + mongoose_config:unset_opt(extra_google_analytics_tracking_id); + {_, ExtraTrackingID} -> + mongoose_config:set_opt(extra_google_analytics_tracking_id, ExtraTrackingID) + end, {InitialReport, ReportAfter}. get_timeouts(Args, "true") -> - ejabberd_config:add_local_option(google_analytics_tracking_id, ?TRACKING_ID_CI), + mongoose_config:set_opt(google_analytics_tracking_id, ?TRACKING_ID_CI), I = proplists:get_value(initial_report, Args, timer:seconds(20)), R = proplists:get_value(periodic_report, Args, timer:minutes(5)), {I, R}; get_timeouts(Args, _) -> - ejabberd_config:add_local_option(google_analytics_tracking_id, ?TRACKING_ID), + mongoose_config:set_opt(google_analytics_tracking_id, ?TRACKING_ID), I = proplists:get_value(initial_report, Args, ?DEFAULT_INITIAL_REPORT), R = proplists:get_value(periodic_report, Args, ?DEFAULT_REPORT_AFTER), {I, R}. diff --git a/src/translate.erl b/src/translate.erl index 587d518059e..35cc9993cee 100644 --- a/src/translate.erl +++ b/src/translate.erl @@ -119,7 +119,7 @@ insert_translation(Lang, Msg, Trans) -> -spec get_default_server_lang_translation(binary()) -> binary(). get_default_server_lang_translation(Msg) -> - case get_translation(default_server_lang(), Msg) of + case get_translation(?MYLANG, Msg) of {ok, DefaultTrans} -> DefaultTrans; {error, not_found} -> Msg end. @@ -149,14 +149,6 @@ short_lang(LLang) -> [ShortLang | _] -> list_to_binary(ShortLang) end. --spec default_server_lang() -> ejabberd:lang(). -default_server_lang() -> - case ?MYLANG of - undefined -> <<"en">>; - <<"en">> -> <<"en">>; - Lang -> Lang - end. - -spec to_lower(binary()) -> binary(). to_lower(Bin) -> list_to_binary(string:to_lower(binary_to_list(Bin))). diff --git a/src/wpool/mongoose_wpool.erl b/src/wpool/mongoose_wpool.erl index 4555b570a17..bc6c76a7dba 100644 --- a/src/wpool/mongoose_wpool.erl +++ b/src/wpool/mongoose_wpool.erl @@ -121,7 +121,7 @@ ensure_started() -> end. start_configured_pools() -> - Pools = ejabberd_config:get_local_option_or_default(outgoing_pools, []), + Pools = mongoose_config:get_opt(outgoing_pools, []), start_configured_pools(Pools). start_configured_pools(PoolsIn) -> @@ -231,7 +231,7 @@ stop(PoolType, HostType, Tag) -> -spec is_configured(pool_type()) -> boolean(). is_configured(PoolType) -> - Pools = ejabberd_config:get_local_option_or_default(outgoing_pools, []), + Pools = mongoose_config:get_opt(outgoing_pools, []), lists:keymember(PoolType, 1, Pools). -spec get_worker(pool_type()) -> worker_result(). From 3ada8b1eee0313d5fd2448164be5e73a2473f075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Tue, 19 Oct 2021 08:22:44 +0200 Subject: [PATCH 02/10] Use mongoose_config in small tests - Do not mock the config as it is easier to just set the values --- test/acl_SUITE.erl | 26 +++------- test/auth_dummy_SUITE.erl | 16 +++--- test/auth_external_SUITE.erl | 27 ++++------ test/auth_http_SUITE.erl | 37 ++++---------- test/auth_jwt_SUITE.erl | 50 ++++--------------- test/commands_SUITE.erl | 22 +++----- test/commands_backend_SUITE.erl | 2 - test/component_reg_SUITE.erl | 9 +--- test/ejabberd_c2s_SUITE_mocks.erl | 13 +++-- test/ejabberd_hooks_SUITE.erl | 2 - test/ejabberd_sm_SUITE.erl | 39 ++++++++------- test/gen_hook_SUITE.erl | 3 -- test/gen_mod_SUITE.erl | 9 +--- test/mod_global_distrib_SUITE.erl | 8 +-- test/mod_mam_meta_SUITE.erl | 32 ++++-------- test/mod_websockets_SUITE.erl | 1 - test/mongoose_cleanup_SUITE.erl | 18 ++----- ...ig_SUITE.erl => mongoose_config_SUITE.erl} | 4 +- .../mongooseim.minimal.toml | 0 test/mongoose_rdbms_SUITE.erl | 18 +++---- test/mongoose_service_SUITE.erl | 21 +++----- test/mongoose_wpool_SUITE.erl | 7 +-- test/mongooseim_metrics_SUITE.erl | 20 +++----- test/roster_SUITE.erl | 3 -- test/router_SUITE.erl | 12 ++--- test/translate_SUITE.erl | 12 ++--- 26 files changed, 132 insertions(+), 279 deletions(-) rename test/{ejabberd_config_SUITE.erl => mongoose_config_SUITE.erl} (97%) rename test/{ejabberd_config_SUITE_data => mongoose_config_SUITE_data}/mongooseim.minimal.toml (100%) diff --git a/test/acl_SUITE.erl b/test/acl_SUITE.erl index d06b1141261..7668664be8b 100644 --- a/test/acl_SUITE.erl +++ b/test/acl_SUITE.erl @@ -34,15 +34,11 @@ host_type_test_cases() -> ]. init_per_suite(Config) -> - ok = mnesia:create_schema([node()]), - ok = mnesia:start(), {ok, _} = application:ensure_all_started(jid), Config. end_per_suite(_Config) -> mongoose_domain_api:stop(), - mnesia:stop(), - mnesia:delete_schema([node()]), meck:unload(), ok. @@ -318,16 +314,10 @@ different_specs_matching_the_same_user(Config) -> ok. set_acl(HostType, ACLName, ACLSpec) -> - ejabberd_config:add_local_option({acl, ACLName, HostType}, [ACLSpec]). + mongoose_config:set_opt({acl, ACLName, HostType}, [ACLSpec]). given_clean_config() -> - meck:unload(), - %% skip loading part - meck:new(ejabberd_config, [no_link, unstick, passthrough]), - meck:expect(ejabberd_config, load_file, fun(_File) -> ok end), - ejabberd_config:start(), - mnesia:clear_table(config), - mnesia:clear_table(local_config), + [persistent_term:erase(Key) || {Key = {mongoose_config, _}, _Value} <- persistent_term:get()], ok. given_registered_domains(Config, DomainsList) -> @@ -339,24 +329,24 @@ given_registered_domains(Config, DomainsList) -> end. register_static_domains(DomainsList) -> - ejabberd_config:add_local_option(hosts, DomainsList), - ejabberd_config:add_local_option(host_types, []), + mongoose_config:set_opt(hosts, DomainsList), + mongoose_config:set_opt(host_types, []), mongoose_domain_api:stop(), mongoose_domain_api:init(). register_dynamic_domains(DomainsList) -> - ejabberd_config:add_local_option(hosts, []), - ejabberd_config:add_local_option(host_types, [<<"test type">>, <<"empty type">>]), + mongoose_config:set_opt(hosts, []), + mongoose_config:set_opt(host_types, [<<"test type">>, <<"empty type">>]), mongoose_domain_api:stop(), mongoose_domain_api:init(), [mongoose_domain_core:insert(Domain, <<"test type">>, test) || Domain <- DomainsList]. %% ACLs might be an empty list set_host_rule(Rule, Host, ACLs) -> - ejabberd_config:add_local_option({access, Rule, Host}, ACLs), + mongoose_config:set_opt({access, Rule, Host}, ACLs), ok. %% ACLs might be an empty list set_global_rule(Rule, ACLs) -> - ejabberd_config:add_local_option({access, Rule, global}, ACLs), + mongoose_config:set_opt({access, Rule, global}, ACLs), ok. diff --git a/test/auth_dummy_SUITE.erl b/test/auth_dummy_SUITE.erl index b5ea6c920c0..357e1acc8e7 100644 --- a/test/auth_dummy_SUITE.erl +++ b/test/auth_dummy_SUITE.erl @@ -35,18 +35,14 @@ all() -> [ init_per_suite(C) -> {ok, _} = application:ensure_all_started(jid), - meck:new(ejabberd_config, [no_link]), - meck:expect(ejabberd_config, get_local_option, - fun({auth_method, ?HOST_TYPE}) -> - dummy; - ({auth_opts, ?HOST_TYPE}) -> - [{dummy_base_timeout, 5}, {dummy_variance, 10}] - end), + mongoose_config:set_opt({auth_method, ?HOST_TYPE}, [dummy]), + mongoose_config:set_opt({auth_opts, ?HOST_TYPE}, [{dummy_base_timeout, 5}, + {dummy_variance, 10}]), C. -end_per_suite(C) -> - meck:unload(), - C. +end_per_suite(_C) -> + mongoose_config:unset_opt({auth_method, ?HOST_TYPE}), + mongoose_config:unset_opt({auth_opts, ?HOST_TYPE}). %%-------------------------------------------------------------------- %% Authentication tests diff --git a/test/auth_external_SUITE.erl b/test/auth_external_SUITE.erl index 140ec71c58b..27158c3ccc9 100644 --- a/test/auth_external_SUITE.erl +++ b/test/auth_external_SUITE.erl @@ -29,14 +29,14 @@ init_per_suite(C) -> end_per_suite(C) -> C. -init_per_group(G, Config) -> - setup_meck(G, Config), +init_per_group(_G, Config) -> + set_opts(Config), ejabberd_auth_external:start(host_type()), Config. -end_per_group(G, Config) -> +end_per_group(_G, Config) -> ejabberd_auth_external:stop(host_type()), - unload_meck(G), + unset_opts(), Config. try_register_ok(_C) -> @@ -77,20 +77,13 @@ given_user_registered() -> ok = ejabberd_auth_external:try_register(host_type(), U, domain(), P), UP. -setup_meck(_G, Config) -> +set_opts(Config) -> DataDir = ?config(data_dir, Config), - meck:new(ejabberd_config, [no_link]), - meck:expect(ejabberd_config, get_local_option, - fun({auth_opts, ?HOST_TYPE}) -> - [{extauth_program, DataDir ++ "sample_external_auth.py"}]; - ({extauth_instances, ?HOST_TYPE}) -> - undefined; - ({extauth_cache, ?HOST_TYPE}) -> - undefined - end). - -unload_meck(_G) -> - meck:unload(ejabberd_config). + mongoose_config:set_opt({auth_opts, ?HOST_TYPE}, + [{extauth_program, DataDir ++ "sample_external_auth.py"}]). + +unset_opts() -> + mongoose_config:unset_opt({auth_opts, ?HOST_TYPE}). gen_user() -> U = random_binary(5), diff --git a/test/auth_http_SUITE.erl b/test/auth_http_SUITE.erl index afa735165a1..516e7e50683 100644 --- a/test/auth_http_SUITE.erl +++ b/test/auth_http_SUITE.erl @@ -66,7 +66,7 @@ suite() -> init_per_suite(Config) -> {ok, _} = application:ensure_all_started(jid), - meck_config(Config), + set_opts(Config), mim_ct_rest:start(?BASIC_AUTH, Config), % Separate process needs to do this, because this one will terminate % so will supervisor and children and ETS tables @@ -83,12 +83,12 @@ init_per_suite(Config) -> mongoose_wpool_http:init(), ejabberd_auth_http:start(?HOST_TYPE) end), - meck_cleanup(), Config. end_per_suite(Config) -> ejabberd_auth_http:stop(?HOST_TYPE), ok = mim_ct_rest:stop(), + unset_opts(), Config. init_per_group(cert_auth, Config) -> @@ -106,10 +106,9 @@ init_per_group(cert_auth, Config) -> init_per_group(GroupName, Config) -> Config2 = lists:keystore(scram_group, 1, Config, {scram_group, GroupName == auth_requests_scram}), - meck_config(Config2), + set_opts(Config2), mim_ct_rest:register(<<"alice">>, ?DOMAIN, do_scram(<<"makota">>, Config2)), mim_ct_rest:register(<<"bob">>, ?DOMAIN, do_scram(<<"niema5klepki">>, Config2)), - meck_cleanup(), Config2. end_per_group(cert_auth, Config) -> @@ -120,45 +119,36 @@ end_per_group(_GroupName, Config) -> Config. init_per_testcase(remove_user, Config) -> - meck_config(Config), mim_ct_rest:register(<<"toremove1">>, ?DOMAIN, do_scram(<<"pass">>, Config)), mim_ct_rest:register(<<"toremove2">>, ?DOMAIN, do_scram(<<"pass">>, Config)), Config; init_per_testcase(cert_auth_fail, Config) -> - meck_config(Config), Cert = proplists:get_value(pem_cert1, Config), mim_ct_rest:register(<<"cert_user">>, ?DOMAIN, Cert), Config; init_per_testcase(cert_auth_success, Config) -> - meck_config(Config), Cert1 = proplists:get_value(pem_cert1, Config), Cert2 = proplists:get_value(pem_cert2, Config), SeveralCerts = <>, mim_ct_rest:register(<<"cert_user">>, ?DOMAIN, SeveralCerts), Config; init_per_testcase(_CaseName, Config) -> - meck_config(Config), Config. end_per_testcase(try_register, Config) -> mim_ct_rest:remove_user(<<"nonexistent">>, ?DOMAIN), - meck_cleanup(), Config; end_per_testcase(remove_user, Config) -> mim_ct_rest:remove_user(<<"toremove1">>, ?DOMAIN), mim_ct_rest:remove_user(<<"toremove2">>, ?DOMAIN), - meck_cleanup(), Config; end_per_testcase(cert_auth_fail, Config) -> mim_ct_rest:remove_user(<<"cert_user">>, ?DOMAIN), - meck_cleanup(), Config; end_per_testcase(cert_auth_success, Config) -> mim_ct_rest:remove_user(<<"cert_user">>, ?DOMAIN), - meck_cleanup(), Config; end_per_testcase(_CaseName, Config) -> - meck_cleanup(), Config. %%-------------------------------------------------------------------- @@ -245,25 +235,18 @@ creds_with_cert(Config, Username) -> mongoose_credentials:extend(NewCreds, [{der_cert, Cert}, {username, Username}]). -meck_config(Config) -> - meck:unload(), +set_opts(Config) -> ScramOpts = case lists:keyfind(scram_group, 1, Config) of {_, false} -> [{password_format, plain}]; _ -> [] end, - meck:new(ejabberd_config), - meck:expect(ejabberd_config, get_local_option, - fun({auth_opts, ?HOST_TYPE}) -> - [ - {host, ?AUTH_HOST}, - {path_prefix, "/auth/"}, - {basic_auth, ?BASIC_AUTH} - ] ++ ScramOpts - end). + mongoose_config:set_opt({auth_opts, ?HOST_TYPE}, + [{host, ?AUTH_HOST}, + {path_prefix, "/auth/"}, + {basic_auth, ?BASIC_AUTH}] ++ ScramOpts). -meck_cleanup() -> - meck:validate(ejabberd_config), - meck:unload(ejabberd_config). +unset_opts() -> + mongoose_config:unset_opt({auth_opts, ?HOST_TYPE}). do_scram(Pass, Config) -> case lists:keyfind(scram_group, 1, Config) of diff --git a/test/auth_jwt_SUITE.erl b/test/auth_jwt_SUITE.erl index 177f4557748..0e5d273a2c1 100644 --- a/test/auth_jwt_SUITE.erl +++ b/test/auth_jwt_SUITE.erl @@ -45,15 +45,10 @@ suite() -> init_per_suite(Config) -> application:ensure_all_started(jid), - Self = self(), - ETSProcess = spawn(fun() -> ets_owner(Self) end), - wait_for_ets(), - meck_config(Config), - [{ets_process, ETSProcess} | Config]. + Config. end_per_suite(Config) -> - meck_cleanup(), - stop_ets(proplists:get_value(ets_process, Config)), + unset_auth_opts(), Config. init_per_group(public_key, Config) -> @@ -124,40 +119,15 @@ check_password_succeeds_for_pubkey_signed_token(C) -> %% Helpers %%-------------------------------------------------------------------- -ets_owner(Parent) -> - ets:new(jwt_meck, [public, named_table, set]), - Parent ! ets_ready, - receive stop -> ok end. - -wait_for_ets() -> - receive - ets_ready -> ok - after - 5000 -> ct:fail(cant_prepare_ets) - end. - -stop_ets(undefined) -> ok; -stop_ets(Pid) -> Pid ! stop. - set_auth_opts(SecretSource, Secret, Algorithm, Key) -> - ets:insert(jwt_meck, {auth_opts, [ - {jwt_secret_source, SecretSource}, - {jwt_secret, Secret}, - {jwt_algorithm, Algorithm}, - {jwt_username_key, Key} - ]}). - -meck_config(_Config) -> - meck:new(ejabberd_config, [no_link]), - meck:expect(ejabberd_config, get_local_option, - fun({Key, ?HOST_TYPE}) -> [{_, Data}] = ets:lookup(jwt_meck, Key), Data end), - meck:expect(ejabberd_config, add_local_option, - fun({Key, _}, Val) -> ets:insert(jwt_meck, {Key, Val}) end). - -meck_cleanup() -> - meck:validate(ejabberd_config), - meck:unload(ejabberd_config), - ets:delete(jwt_meck). + mongoose_config:set_opt({auth_opts, ?HOST_TYPE}, + [{jwt_secret_source, SecretSource}, + {jwt_secret, Secret}, + {jwt_algorithm, Algorithm}, + {jwt_username_key, Key}]). + +unset_auth_opts() -> + mongoose_config:unset_opt({auth_opts, ?HOST_TYPE}). generate_token(Alg, NbfDelta, Key) -> Now = erlang:system_time(second), diff --git a/test/commands_SUITE.erl b/test/commands_SUITE.erl index 75a96f16ec4..ca26b40cc1f 100644 --- a/test/commands_SUITE.erl +++ b/test/commands_SUITE.erl @@ -70,27 +70,21 @@ stop_helper_proc(C) -> Pid ! stop. init_per_testcase(_, C) -> - meck:new(ejabberd_config), - meck:expect(ejabberd_config, get_local_option, fun get_opt/1), - meck:expect(ejabberd_config, get_local_option_or_default, fun(K, _) -> get_opt(K) end), + [mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts()], meck:new(ejabberd_auth_dummy, [non_strict]), meck:expect(ejabberd_auth_dummy, get_password_s, fun(_, _) -> <<"">> end), meck:new(mongoose_domain_api), meck:expect(mongoose_domain_api, get_domain_host_type, fun(H) -> {ok, H} end), C. -end_per_testcase(_, C) -> - meck:unload(), - C. +end_per_testcase(_, _C) -> + [mongoose_config:unset_opt(Key) || {Key, _Value} <- opts()], + meck:unload(). -get_opt({auth_method, _}) -> - dummy; -get_opt({access, experts_only, _}) -> - [{allow, coder}, {allow, manager}, {deny, all}]; -get_opt({access, _, _}) -> - []; -get_opt({acl, coder, _}) -> - [{user, <<"zenek">>}]. +opts() -> + [{{auth_method, <<"localhost">>}, [dummy]}, + {{access, experts_only, <<"localhost">>}, [{allow, coder}, {allow, manager}, {deny, all}]}, + {{acl, coder, <<"localhost">>}, [{user, <<"zenek">>}]}]. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%% test methods diff --git a/test/commands_backend_SUITE.erl b/test/commands_backend_SUITE.erl index 779f68ee200..5b7c32c357e 100644 --- a/test/commands_backend_SUITE.erl +++ b/test/commands_backend_SUITE.erl @@ -102,7 +102,6 @@ setup(Module) -> %% you have to meck some stuff to get it working.... meck:expect(gen_hook, add_handler, fun(_, _, _, _, _) -> ok end), meck:expect(gen_hook, run_fold, fun(_, _, _, _) -> {ok, ok} end), - meck:expect(ejabberd_config, get_local_option, fun(_) -> undefined end), spawn(fun mc_holder/0), meck:expect(supervisor, start_child, fun(ejabberd_listeners, {_, {_, start_link, [_]}, transient, @@ -118,7 +117,6 @@ setup(Module) -> teardown() -> cowboy:stop_listener(ejabberd_cowboy:ref({?PORT, ?IP, tcp})), mongoose_commands:unregister(commands_new()), - meck:unload(ejabberd_config), meck:unload(ejabberd_auth), meck:unload(gen_hook), meck:unload(supervisor), diff --git a/test/component_reg_SUITE.erl b/test/component_reg_SUITE.erl index 6593114c628..0d97e2fe515 100644 --- a/test/component_reg_SUITE.erl +++ b/test/component_reg_SUITE.erl @@ -13,13 +13,7 @@ init_per_suite(C) -> {ok, _} = application:ensure_all_started(jid), ok = mnesia:create_schema([node()]), ok = mnesia:start(), - meck:new(ejabberd_config), - meck:expect(ejabberd_config, get_local_option, - fun(routing_modules) -> - [xmpp_router_a, xmpp_router_b, xmpp_router_c]; - (_) -> - undefined - end), + mongoose_config:set_opt(routing_modules, [xmpp_router_a, xmpp_router_b, xmpp_router_c]), meck:new(mongoose_domain_api, [no_link]), meck:expect(mongoose_domain_api, get_host_type, fun(_) -> {error, not_found} end), @@ -36,6 +30,7 @@ end_per_suite(_C) -> mnesia:stop(), mnesia:delete_schema([node()]), meck:unload(), + mongoose_config:unset_opt(routing_modules), ok. registering(_C) -> diff --git a/test/ejabberd_c2s_SUITE_mocks.erl b/test/ejabberd_c2s_SUITE_mocks.erl index d73326563e4..30b915533e1 100644 --- a/test/ejabberd_c2s_SUITE_mocks.erl +++ b/test/ejabberd_c2s_SUITE_mocks.erl @@ -38,9 +38,8 @@ setup() -> meck:new(gen_hook), meck:expect(gen_hook, run_fold, fun hookfold/4), - meck:new(ejabberd_config), - meck:expect(ejabberd_config, get_local_option, - fun default_local_option/1), + [mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts()], + meck:expect(acl, match_rule_for_host_type, fun(_, _, _, _) -> allow end), meck:new(mongoose_bin, [passthrough]), @@ -57,12 +56,12 @@ setup() -> teardown() -> + [mongoose_config:unset_opt(Key) || {Key, _Value} <- opts()], meck:unload(). -default_local_option(max_fsm_queue) -> 100; -default_local_option(default_server_domain) -> <<"localhost">>; -default_local_option({access, c2s_shaper, _}) -> []; -default_local_option(language) -> <<"en">>. +opts() -> + [{max_fsm_queue, 100}, + {default_server_domain, <<"localhost">>}]. mcred_get(dummy_creds, username) -> <<"cosmic_hippo">>; mcred_get(dummy_creds, auth_module) -> auuuthmodule. diff --git a/test/ejabberd_hooks_SUITE.erl b/test/ejabberd_hooks_SUITE.erl index e6b27a8deea..d62a16df800 100644 --- a/test/ejabberd_hooks_SUITE.erl +++ b/test/ejabberd_hooks_SUITE.erl @@ -237,8 +237,6 @@ exit_in_run_fold_is_ignored(_) -> const(N) -> fun(_) -> N end. given_hooks_started() -> - Fun = fun(all_metrics_are_global) -> false end, - given_module(ejabberd_config, get_local_option, Fun), gen_hook:start_link(). given_hook_added(HookName, ModName, FunName, Prio) -> diff --git a/test/ejabberd_sm_SUITE.erl b/test/ejabberd_sm_SUITE.erl index 2de981a0b36..f1a2487380d 100644 --- a/test/ejabberd_sm_SUITE.erl +++ b/test/ejabberd_sm_SUITE.erl @@ -107,7 +107,7 @@ end_per_testcase(_, Config) -> clean_sessions(Config), terminate_sm(), unload_meck(), - Config. + unset_opts(). open_session(C) -> {Sid, USR} = generate_random_user(<<"localhost">>), @@ -366,14 +366,11 @@ unique_count_while_removing_entries(C) -> unload_meck() -> meck:unload(acl), - meck:unload(ejabberd_config), meck:unload(gen_hook), meck:unload(ejabberd_commands), meck:unload(mongoose_domain_api). set_test_case_meck(MaxUserSessions) -> - meck:new(ejabberd_config, []), - meck:expect(ejabberd_config, get_local_option, fun(_) -> undefined end), meck:new(acl, []), meck:expect(acl, match_rule, fun(_, _, _) -> MaxUserSessions end), meck:new(gen_hook, []), @@ -585,31 +582,35 @@ try_to_reproduce_race_condition(Config) -> end. setup_sm(Config) -> - case proplists:get_value(backend, Config) of + set_opts(Config), + set_meck(), + ejabberd_sm:start_link(), + case ?config(backend, Config) of ejabberd_sm_redis -> - set_meck({redis, [{pool_size, 3}, {worker_config, [{host, "localhost"}, {port, 6379}]}]}), - ejabberd_sm:start_link(), mongoose_redis:cmd(["FLUSHALL"]); ejabberd_sm_mnesia -> - set_meck({mnesia, []}), - ejabberd_sm:start_link() + ok end. terminate_sm() -> gen_server:stop(ejabberd_sm). -set_meck(SMBackend) -> - meck:expect(ejabberd_config, get_local_option, fun(sm_backend) -> SMBackend; - (_) -> undefined - end), - meck:expect(ejabberd_config, get_local_option_or_default, - fun - (hosts, _Default) -> [<<"localhost">>]; - (host_types, Default) -> Default - end), - meck:expect(gen_hook, add_handler, fun(_, _, _, _, _) -> ok end), +set_opts(Config) -> + mongoose_config:set_opt(hosts, [<<"localhost">>]), + mongoose_config:set_opt(sm_backend, sm_backend(?config(backend, Config))). +sm_backend(ejabberd_sm_redis) -> + {redis, [{pool_size, 3}, {worker_config, [{host, "localhost"}, {port, 6379}]}]}; +sm_backend(ejabberd_sm_mnesia) -> + {mnesia, []}. + +set_meck() -> + meck:expect(gen_hook, add_handler, fun(_, _, _, _, _) -> ok end), meck:new(ejabberd_commands, []), meck:expect(ejabberd_commands, register_commands, fun(_) -> ok end), meck:expect(ejabberd_commands, unregister_commands, fun(_) -> ok end), ok. + +unset_opts() -> + mongoose_config:unset_opt(hosts), + mongoose_config:unset_opt(sm_backend). diff --git a/test/gen_hook_SUITE.erl b/test/gen_hook_SUITE.erl index fb7299432f2..daf882a5975 100644 --- a/test/gen_hook_SUITE.erl +++ b/test/gen_hook_SUITE.erl @@ -31,9 +31,6 @@ end_per_suite(Config) -> Config. init_per_testcase(_, Config) -> - Fun = fun(all_metrics_are_global) -> false end, - meck:new(ejabberd_config), - meck:expect(ejabberd_config, get_local_option, Fun), gen_hook:start_link(), Config. diff --git a/test/gen_mod_SUITE.erl b/test/gen_mod_SUITE.erl index e729b49c95d..7a4b8ffa30f 100644 --- a/test/gen_mod_SUITE.erl +++ b/test/gen_mod_SUITE.erl @@ -29,19 +29,14 @@ all() -> [start_and_stop]. init_per_testcase(_, Config) -> - meck:new(ejabberd_config, [passthrough]), - meck:expect(ejabberd_config, get_local_option_or_default, - fun(hosts, _) -> [<<"localhost">>, <<"localhost.bis">>] end), - meck:expect(ejabberd_config, get_local_option, fun(_) -> undefined end), - meck:expect(ejabberd_config, add_local_option, fun(_, _) -> {atomic, ok} end), - meck:expect(ejabberd_config, del_local_option, fun(_) -> {atomic, ok} end), + mongoose_config:set_opt(hosts, [<<"localhost">>, <<"localhost.bis">>]), meck:new(a_module, [non_strict]), meck:expect(a_module, start, fun(_, _) -> ok end), meck:expect(a_module, stop, fun(_) -> ok end), Config. end_per_testcase(_, Config) -> - meck:unload(ejabberd_config), + mongoose_config:unset_opt(hosts), meck:unload(a_module), Config. diff --git a/test/mod_global_distrib_SUITE.erl b/test/mod_global_distrib_SUITE.erl index eefcac42898..8de05c7d679 100644 --- a/test/mod_global_distrib_SUITE.erl +++ b/test/mod_global_distrib_SUITE.erl @@ -62,6 +62,7 @@ end_per_group(_GroupName, Config) -> Config. init_per_testcase(_CaseName, Config) -> + mongoose_config:set_opt(hosts, [global_host(), local_host()]), set_meck(), fake_start(), Config. @@ -69,6 +70,7 @@ init_per_testcase(_CaseName, Config) -> end_per_testcase(_CaseName, Config) -> fake_stop(), unset_meck(), + mongoose_config:unset_opt(hosts), Config. %%-------------------------------------------------------------------- @@ -129,11 +131,6 @@ fake_acc_to_component(From) -> %%-------------------------------------------------------------------- set_meck() -> - meck:new(ejabberd_config, []), - meck:expect(ejabberd_config, get_local_option_or_default, - fun(hosts, _) -> [global_host(), local_host()] end), - meck:expect(ejabberd_config, get_local_option, fun(_) -> undefined end), - meck:new(mongoose_metrics, []), meck:expect(mongoose_metrics, update, fun(_, _, _) -> ok end), @@ -145,7 +142,6 @@ set_meck() -> meck:expect(mod_global_distrib_mapping_backend, put_domain, fun(_) -> ok end). unset_meck() -> - meck:unload(ejabberd_config), meck:unload(mod_global_distrib_mapping_backend), meck:unload(mongoose_metrics). diff --git a/test/mod_mam_meta_SUITE.erl b/test/mod_mam_meta_SUITE.erl index 727dc2e03b0..18cb85e8ddf 100644 --- a/test/mod_mam_meta_SUITE.erl +++ b/test/mod_mam_meta_SUITE.erl @@ -22,12 +22,12 @@ all() -> [ %% Tests init_per_testcase(get_mam_module_configuration, Config) -> - meck_config(), + [mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts()], Config; init_per_testcase(_, Config) -> Config. end_per_testcase(get_mam_module_configuration, Config) -> - meck_cleanup(), + [mongoose_config:unset_opt(Key) || {Key, _Value} <- opts()], Config; end_per_testcase(_CaseName, Config) -> Config. @@ -179,25 +179,15 @@ get_mam_module_configuration(_Config) -> %% Helpers -meck_config() -> - meck:new(ejabberd_config), - meck:expect(ejabberd_config, get_local_option, - fun({modules, <<"no_config">>}) -> - []; - ({modules, <<"mod_mam_config">>}) -> - [{mod_mam, [here, is, some, config]}]; - ({modules, <<"meta_no_mod_mam_config">>}) -> - [{mod_mam_meta, [{backend, rdbms}, - {muc, []}]}]; - ({modules, <<"meta_valid_mod_mam_config">>}) -> - [{mod_mam_meta, [{backend, rdbms}, - cache_users, - {pm, [archive_groupchats]}]}] - end). - -meck_cleanup() -> - meck:validate(ejabberd_config), - meck:unload(ejabberd_config). +opts() -> + [{{modules, <<"no_config">>}, []}, + {{modules, <<"mod_mam_config">>}, [{mod_mam, [here, is, some, config]}]}, + {{modules, <<"meta_no_mod_mam_config">>}, [{mod_mam_meta, [{backend, rdbms}, + {muc, []}]}]}, + {{modules, <<"meta_valid_mod_mam_config">>}, [{mod_mam_meta, [{backend, rdbms}, + cache_users, + {pm, [archive_groupchats]}]}]} + ]. check_equal_deps(A, B) -> ?assertEqual(sort_deps(A), sort_deps(B)). diff --git a/test/mod_websockets_SUITE.erl b/test/mod_websockets_SUITE.erl index ac3148f2f57..ea589ecc3a9 100644 --- a/test/mod_websockets_SUITE.erl +++ b/test/mod_websockets_SUITE.erl @@ -61,7 +61,6 @@ setup() -> infinity, worker, [_]}) -> {ok, self()}; (A,B) -> meck:passthrough([A,B]) end), - meck:expect(ejabberd_config, get_local_option, fun(_) -> undefined end), %% Start websocket cowboy listening Opts = [{num_acceptors, 10}, diff --git a/test/mongoose_cleanup_SUITE.erl b/test/mongoose_cleanup_SUITE.erl index f0e67c6a541..ff1af9a62f7 100644 --- a/test/mongoose_cleanup_SUITE.erl +++ b/test/mongoose_cleanup_SUITE.erl @@ -55,10 +55,10 @@ end_per_testcase(T, Config) -> unload_meck(meck_mods(T)), Config. -meck_mods(bosh) -> [exometer, mod_bosh_socket, ejabberd_config]; -meck_mods(s2s) -> [exometer, ejabberd_commands, mongoose_bin, ejabberd_config]; -meck_mods(local) -> [exometer, ejabberd_config]; -meck_mods(_) -> [exometer, ejabberd_sm, ejabberd_local, ejabberd_config]. +meck_mods(bosh) -> [exometer, mod_bosh_socket]; +meck_mods(s2s) -> [exometer, ejabberd_commands, mongoose_bin]; +meck_mods(local) -> [exometer]; +meck_mods(_) -> [exometer, ejabberd_sm, ejabberd_local]. %% ----------------------------------------------------- %% Tests @@ -180,16 +180,6 @@ setup_meck([ejabberd_local | R]) -> meck:expect(ejabberd_local, register_iq_handler, fun(_A1, _A2, _A3) -> ok end), setup_meck(R); -setup_meck([ejabberd_config | R]) -> - meck:new(ejabberd_config), - meck:expect(ejabberd_config, get_local_option_or_default, - fun(_, Default) -> Default end), - meck:expect(ejabberd_config, get_local_option, - fun - (hosts) -> []; - (_) -> undefined - end), - setup_meck(R); setup_meck([ejabberd_commands | R]) -> meck:new(ejabberd_commands), meck:expect(ejabberd_commands, register_commands, fun(_) -> ok end), diff --git a/test/ejabberd_config_SUITE.erl b/test/mongoose_config_SUITE.erl similarity index 97% rename from test/ejabberd_config_SUITE.erl rename to test/mongoose_config_SUITE.erl index 6eeeee0219a..c90bb76bab3 100644 --- a/test/ejabberd_config_SUITE.erl +++ b/test/mongoose_config_SUITE.erl @@ -1,4 +1,4 @@ --module(ejabberd_config_SUITE). +-module(mongoose_config_SUITE). -compile([export_all, nowarn_export_all]). -include_lib("common_test/include/ct.hrl"). @@ -63,7 +63,7 @@ cluster_smoke(C) -> {ok, _} = start_ejabberd_with_config(C, "mongooseim.toml"), {ok, _} = start_remote_ejabberd_with_config(SlaveNode, C, "mongooseim.toml"), maybe_join_cluster(SlaveNode), - [_, _] = ejabberd_config:config_states(), + [State, State] = mongoose_config:config_states(), % cleanup ok = stop_ejabberd(), stop_remote_ejabberd(SlaveNode), diff --git a/test/ejabberd_config_SUITE_data/mongooseim.minimal.toml b/test/mongoose_config_SUITE_data/mongooseim.minimal.toml similarity index 100% rename from test/ejabberd_config_SUITE_data/mongooseim.minimal.toml rename to test/mongoose_config_SUITE_data/mongooseim.minimal.toml diff --git a/test/mongoose_rdbms_SUITE.erl b/test/mongoose_rdbms_SUITE.erl index c17148e1de3..e42c3fddb95 100644 --- a/test/mongoose_rdbms_SUITE.erl +++ b/test/mongoose_rdbms_SUITE.erl @@ -52,7 +52,7 @@ end_per_group(_, Config) -> init_per_testcase(does_backoff_increase_to_a_point, Config) -> DbType = ?config(db_type, Config), backend_module:create(mongoose_rdbms, DbType, []), - meck_config(DbType), + set_opts(), meck_db(DbType), meck_connection_error(DbType), meck_rand(), @@ -60,7 +60,7 @@ init_per_testcase(does_backoff_increase_to_a_point, Config) -> init_per_testcase(_, Config) -> DbType = ?config(db_type, Config), backend_module:create(mongoose_rdbms, DbType, []), - meck_config(DbType), + set_opts(), meck_db(DbType), [{db_opts, [{server, server(DbType)}, {keepalive_interval, ?KEEPALIVE_INTERVAL}, {start_interval, ?MAX_INTERVAL}]} | Config]. @@ -69,9 +69,11 @@ end_per_testcase(does_backoff_increase_to_a_point, Config) -> meck_unload_rand(), Db = ?config(db_type, Config), meck_config_and_db_unload(Db), + unset_opts(), Config; end_per_testcase(_, Config) -> meck_config_and_db_unload(?config(db_type, Config)), + unset_opts(), Config. %% Test cases @@ -128,12 +130,11 @@ meck_rand() -> meck_unload_rand() -> meck:unload(rand). -meck_config(Server) -> - meck:new(ejabberd_config, [no_link]), - meck:expect(ejabberd_config, get_local_option, - fun(max_fsm_queue) -> 1024; - (all_metrics_are_global) -> false - end). +set_opts() -> + mongoose_config:set_opt(max_fsm_queue, 1024). + +unset_opts() -> + mongoose_config:unset_opt(max_fsm_queue). meck_db(odbc) -> meck:new(eodbc, [no_link]), @@ -175,7 +176,6 @@ meck_error(pgsql) -> fun(_Ref, _Query) -> {error, {error, 2, 3, 4, <<"connection broken">>, 5}} end). meck_config_and_db_unload(DbType) -> - meck:unload(ejabberd_config), do_meck_unload(DbType). do_meck_unload(odbc) -> diff --git a/test/mongoose_service_SUITE.erl b/test/mongoose_service_SUITE.erl index 6d6a6be7e02..57439c9eeaf 100644 --- a/test/mongoose_service_SUITE.erl +++ b/test/mongoose_service_SUITE.erl @@ -53,22 +53,14 @@ service_deps() -> init_per_testcase(misconfigured, C) -> mongoose_service:start(), - meck:new(ejabberd_config, [passthrough]), - meck:expect(ejabberd_config, get_local_option_or_default, - fun(services, _) -> [{service_a, []}] end), + mongoose_config:set_opt(services, [{service_a, []}]), meck:new(service_a, [non_strict]), meck:expect(service_a, deps, fun() -> [service_b] end), C; init_per_testcase(module_deps, C) -> init_per_testcase(generic, C), - meck:expect(ejabberd_config, get_local_option, fun(_) -> undefined end), - meck:expect(ejabberd_config, add_local_option, fun(_, _) -> ok end), - % This fun needs the second clause because it overwrites the previous mock - meck:expect(ejabberd_config, get_local_option_or_default, - fun(hosts, _) -> [<<"localhost">>]; - (services, _) -> [{Serv, []} || Serv <- services()] - end), + mongoose_config:set_opt(hosts, [<<"localhost">>]), gen_mod:start(), meck:new(module_a, [non_strict]), meck:expect(module_a, deps, fun(_, _) -> [{service, service_d}, {service, service_h}] end), @@ -85,9 +77,7 @@ init_per_testcase(_, C) -> lists:map(fun(S) -> meck:expect(S, stop, fun() -> decrement(S) end) end, services()), - meck:new(ejabberd_config, [passthrough]), - meck:expect(ejabberd_config, get_local_option_or_default, - fun(services, _) -> [{Serv, []} || Serv <- services()] end), + mongoose_config:set_opt(services, [{Serv, []} || Serv <- services()]), lists:map(fun(Serv) -> meck:expect(Serv, deps, fun() -> proplists:get_value(Serv, service_deps()) end) end, @@ -95,16 +85,17 @@ init_per_testcase(_, C) -> C. end_per_testcase(misconfigured, C) -> + mongoose_config:unset_opt(services), meck:unload(service_a), - meck:unload(ejabberd_config), C; end_per_testcase(module_deps, C) -> + mongoose_config:unset_opt(hosts), meck:unload(module_a), end_per_testcase(generic, C); end_per_testcase(_, C) -> + mongoose_config:unset_opt(services), meck:unload(services()), - meck:unload(ejabberd_config), lists:foreach(fun mongoose_service:stop_service/1, services()), mongoose_service:stop(), C. diff --git a/test/mongoose_wpool_SUITE.erl b/test/mongoose_wpool_SUITE.erl index d0f63f1b6e7..e3538af935c 100644 --- a/test/mongoose_wpool_SUITE.erl +++ b/test/mongoose_wpool_SUITE.erl @@ -48,10 +48,7 @@ all() -> init_per_suite(Config) -> ok = meck:new(wpool, [no_link, passthrough]), ok = meck:new(mongoose_wpool, [no_link, passthrough]), - ok = meck:new(ejabberd_config, [no_link]), - meck:expect(ejabberd_config, get_local_option_or_default, - fun(hosts, _) -> [<<"a.com">>, <<"b.com">>, <<"c.eu">>]; - (host_types, _) -> [] end), + mongoose_config:set_opt(hosts, [<<"a.com">>, <<"b.com">>, <<"c.eu">>]), Self = self(), spawn(fun() -> register(test_helper, self()), @@ -64,8 +61,8 @@ init_per_suite(Config) -> end_per_suite(Config) -> meck:unload(wpool), - meck:unload(ejabberd_config), whereis(test_helper) ! stop, + mongoose_config:unset_opt(hosts), Config. init_per_testcase(_Case, Config) -> diff --git a/test/mongooseim_metrics_SUITE.erl b/test/mongooseim_metrics_SUITE.erl index 887dfb2915d..100815044da 100644 --- a/test/mongooseim_metrics_SUITE.erl +++ b/test/mongooseim_metrics_SUITE.erl @@ -76,14 +76,14 @@ end_per_suite(C) -> C. init_per_group(Group, C) -> - setup_meck(Group), + [mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts(Group)], mongoose_metrics:init(), C. -end_per_group(_Name, C) -> +end_per_group(Group, C) -> mongoose_metrics:remove_host_type_metrics(<<"localhost">>), mongoose_metrics:remove_host_type_metrics(global), - meck:unload(), + [mongoose_config:unset_opt(Key) || {Key, _Value} <- opts(Group)], C. init_per_testcase(CN, C) when tcp_connections_detected =:= CN; @@ -157,17 +157,9 @@ wait_for_update({ok, [{count,0}]}, N) -> timer:sleep(1000), wait_for_update(exometer:get_value([carbon, packets], count), N-1). -setup_meck(Group) -> - meck:new(ejabberd_config, [no_link]), - meck:expect(ejabberd_config, get_local_option, - fun(hosts) -> [<<"localhost">>]; - (all_metrics_are_global) -> Group =:= all_metrics_are_global - end), - meck:expect(ejabberd_config, get_local_option_or_default, - fun (hosts, _Def) -> [<<"localhost">>]; - (_, _) -> [] - end), - ok. +opts(Group) -> + [{hosts, [<<"localhost">>]}, + {all_metrics_are_global, Group =:= all_metrics_are_global}]. get_reporters_cfg(Port) -> [{reporters, [ diff --git a/test/roster_SUITE.erl b/test/roster_SUITE.erl index 501b58fc615..6e61f419f04 100644 --- a/test/roster_SUITE.erl +++ b/test/roster_SUITE.erl @@ -45,9 +45,6 @@ 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), - meck:new(ejabberd_config, [no_link, passthrough]), - meck:expect(ejabberd_config, get_local_option_or_default, - fun(hosts, Default) -> Default end), C. end_per_suite(C) -> diff --git a/test/router_SUITE.erl b/test/router_SUITE.erl index 37640873691..d46a387ee4a 100644 --- a/test/router_SUITE.erl +++ b/test/router_SUITE.erl @@ -41,13 +41,7 @@ end_per_suite(_C) -> ok. init_per_group(routing, Config) -> - meck:new(ejabberd_config), - meck:expect(ejabberd_config, get_local_option, - fun(routing_modules) -> - [xmpp_router_a, xmpp_router_b, xmpp_router_c]; - (_) -> - undefined - end), + mongoose_config:set_opt(routing_modules, [xmpp_router_a, xmpp_router_b, xmpp_router_c]), gen_hook:start_link(), ejabberd_router:start_link(), Config; @@ -55,7 +49,9 @@ init_per_group(schema, Config) -> remove_component_tables(), Config. -end_per_group(_GroupName, _Config) -> +end_per_group(routing, _Config) -> + mongoose_config:unset_opt(routing_modules); +end_per_group(schema, _Config) -> ok. init_per_testcase(_CaseName, Config) -> diff --git a/test/translate_SUITE.erl b/test/translate_SUITE.erl index a8b4a54faa3..7351d7940f6 100644 --- a/test/translate_SUITE.erl +++ b/test/translate_SUITE.erl @@ -12,12 +12,11 @@ all() -> end_per_testcase(_, C) -> - catch meck:unload(ejabberd_config), + mongoose_config:unset_opt(language), C. test_undefined_translation(_Config) -> - %% given - if undefined it should be english(pass through) - given_default_language(undefined), + %% given - no language set, defaults to English given_loaded_translations(), %% then ?assertEqual(<<"undef">>, translate:translate(<<"en">>, <<"undef">>)), @@ -71,8 +70,5 @@ test_portuguese_translation(_Config)-> given_loaded_translations() -> translate:start(). -given_default_language(Lang) -> - (catch meck:unload()), - meck:new(ejabberd_config), - meck:expect(ejabberd_config, get_local_option, fun(language) -> Lang end), - ok. +given_default_language(Language) -> + mongoose_config:set_opt(language, Language). From 5a56e9b1ae8f54067813890b8b1515200c518c6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Wed, 20 Oct 2021 18:55:19 +0200 Subject: [PATCH 03/10] Use mongoose_config in big test helpers Also: make the config backup/change/restore helpers more consistent. --- big_tests/tests/dynamic_modules.erl | 2 +- big_tests/tests/mongoose_helper.erl | 105 +++++++++++++++------------- big_tests/tests/rest_helper.erl | 8 +-- big_tests/tests/s2s_helper.erl | 17 +++-- 4 files changed, 73 insertions(+), 59 deletions(-) diff --git a/big_tests/tests/dynamic_modules.erl b/big_tests/tests/dynamic_modules.erl index 167502de368..63340bd0fd9 100644 --- a/big_tests/tests/dynamic_modules.erl +++ b/big_tests/tests/dynamic_modules.erl @@ -121,7 +121,7 @@ start_running(Config) -> stop_running(Mod, Config) -> ModL = atom_to_list(Mod), HostType = domain_helper:host_type(mim), - Modules = rpc(mim(), ejabberd_config, get_local_option, [{modules, HostType}]), + Modules = rpc(mim(), mongoose_config, get_opt, [{modules, HostType}]), Filtered = lists:filter(fun({Module, _}) -> ModuleL = atom_to_list(Module), case lists:sublist(ModuleL, 1, length(ModL)) of diff --git a/big_tests/tests/mongoose_helper.erl b/big_tests/tests/mongoose_helper.erl index 8c2046a204d..8cc4cbc3a72 100644 --- a/big_tests/tests/mongoose_helper.erl +++ b/big_tests/tests/mongoose_helper.erl @@ -38,16 +38,14 @@ -export([wait_for_route_message_count/2]). -export([wait_for_pid_to_die/1]). -export([supports_sasl_module/1]). --export([backup_auth_config/1, restore_auth_config/1]). --export([backup_sasl_mechanisms_config/1, restore_sasl_mechanisms_config/1]). --export([set_sasl_mechanisms/2]). --export([set_store_password/1]). +-export([auth_opts_with_password_format/1]). -export([get_listener_opts/2]). -export([restart_listener_with_opts/3]). -export([should_minio_be_running/1]). -export([new_mongoose_acc/1]). -export([print_debug_info_for_module/1]). --export([backup_and_set_config_option/3, restore_config_option/2]). +-export([backup_and_set_config/2, backup_and_set_config_option/3, change_config_option/3]). +-export([restore_config/1, restore_config_option/2]). -import(distributed_helper, [mim/0, rpc/4]). @@ -68,7 +66,7 @@ mnesia_or_rdbms_backend() -> -spec auth_modules() -> [atom()]. auth_modules() -> - Hosts = rpc(mim(), ejabberd_config, get_local_option, [hosts]), + Hosts = rpc(mim(), mongoose_config, get_opt, [hosts]), lists:flatmap( fun(Host) -> rpc(mim(), ejabberd_auth, auth_modules, [Host]) @@ -452,47 +450,19 @@ supports_sasl_module(Module) -> HostType = domain_helper:host_type(), rpc(mim(), ejabberd_auth, supports_sasl_module, [HostType, Module]). -backup_auth_config(Config) -> - HostType = domain_helper:host_type(), - AuthOpts = rpc(mim(), ejabberd_config, get_local_option, [{auth_opts, HostType}]), - [{auth_opts, AuthOpts} | Config]. - -backup_sasl_mechanisms_config(Config) -> - XMPPDomain = escalus_ejabberd:unify_str_arg(ct:get_config({hosts, mim, domain})), - GlobalSASLMechanisms = rpc(mim(), ejabberd_config, get_local_option, [sasl_mechanisms]), - HostSASLMechanisms = rpc(mim(), ejabberd_config, get_local_option, [{sasl_mechanisms, XMPPDomain}]), - [{global_sasl_mechanisms, GlobalSASLMechanisms}, - {host_sasl_mechanisms, HostSASLMechanisms} | Config]. - -restore_auth_config(Config) -> - HostType = domain_helper:host_type(), - AuthOpts = proplists:get_value(auth_opts, Config), - rpc(mim(), ejabberd_config, add_local_option, [{auth_opts, HostType}, AuthOpts]). - -restore_sasl_mechanisms_config(Config) -> - XMPPDomain = escalus_ejabberd:unify_str_arg(ct:get_config({hosts, mim, domain})), - GlobalSASLMechanisms = proplists:get_value(global_sasl_mechanisms, Config), - HostSASLMechanisms = proplists:get_value(host_sasl_mechanisms, Config), - rpc(mim(), ejabberd_config, add_local_option, [sasl_mechanisms, GlobalSASLMechanisms]), - rpc(mim(), ejabberd_config, add_local_option, [{sasl_mechanisms, XMPPDomain}, HostSASLMechanisms]). - -set_sasl_mechanisms(GlobalOrHostSASLMechanisms, Mechanisms) -> - rpc(mim(), ejabberd_config, add_local_option, [GlobalOrHostSASLMechanisms, Mechanisms]). - -set_store_password(Type) -> +auth_opts_with_password_format(Type) -> HostType = domain_helper:host_type(mim), - AuthOpts = rpc(mim(), ejabberd_config, get_local_option, [{auth_opts, HostType}]), - NewAuthOpts = build_new_auth_opts(Type, AuthOpts), - rpc(mim(), ejabberd_config, add_local_option, [{auth_opts, HostType}, NewAuthOpts]). + AuthOpts = rpc(mim(), mongoose_config, get_opt, [{auth_opts, HostType}]), + build_new_auth_opts(Type, AuthOpts). build_new_auth_opts(scram, AuthOpts) -> NewAuthOpts0 = lists:keystore(password_format, 1, AuthOpts, {password_format, scram}), - lists:keystore(password_format, 1, NewAuthOpts0, {scram_iterations, 64}); + lists:keystore(scram_iterations, 1, NewAuthOpts0, {scram_iterations, 64}); build_new_auth_opts(Type, AuthOpts) -> lists:keystore(password_format, 1, AuthOpts, {password_format, Type}). get_listener_opts(#{} = Spec, Port) -> - Listeners = rpc(Spec, ejabberd_config, get_local_option, [listen]), + Listeners = rpc(Spec, mongoose_config, get_opt, [listen]), [Item || {{ListenerPort, _, _}, _, _} = Item <- Listeners, ListenerPort =:= Port]. restart_listener_with_opts(Spec, Listener, NewOpts) -> @@ -516,15 +486,54 @@ print_debug_info_for_module(Module) -> ct:pal("hosts_and_opts=~p~n iq_handlers=~p~n", [ModConfig, IqConfig]). +%% Backing up and changing configuration options - API + +backup_and_set_config(Config, Options) -> + Backup = get_config_backup(Config), + NewBackup = maps:fold(fun(K, V, Bkp) -> + do_backup_and_set_config_option(Bkp, K, V) + end, Backup, Options), + set_config_backup(Config, NewBackup). + backup_and_set_config_option(Config, Option, NewValue) -> - OriginalValue = rpc(mim(), ejabberd_config, get_local_option, [Option]), - rpc(mim(), ejabberd_config, add_local_option, [Option, NewValue]), - [{{config_backup, Option}, OriginalValue} | Config]. + Backup = get_config_backup(Config), + NewBackup = do_backup_and_set_config_option(Backup, Option, NewValue), + set_config_backup(Config, NewBackup). -restore_config_option(Config, Option) -> - case ?config({config_backup, Option}, Config) of - undefined -> - rpc(mim(), ejabberd_config, del_local_option, [Option]); - Value -> - rpc(mim(), ejabberd_config, add_local_option, [Option, Value]) +%% @doc Change a config option that is already backed up +change_config_option(Config, Option, NewValue) -> + case maps:is_key(Option, get_config_backup(Config)) of + true -> + rpc(mim(), mongoose_config, set_opt, [Option, NewValue]); + false -> + error({"Cannot change an option that is not backed up", Option, NewValue}) end. + +restore_config(Config) -> + Options = get_config_backup(Config), + %% TODO replace with maps:foreach when dropping OTP 23 + maps:map(fun do_restore_config_option/2, Options), + ok. + +restore_config_option(Config, Option) -> + Options = get_config_backup(Config), + do_restore_config_option(Option, maps:get(Option, Options)). + +%% Backing up and changing configuration options - helpers + +get_config_backup(Config) -> + proplists:get_value(config_backup, Config, #{}). + +set_config_backup(Config, Backup) -> + lists:keystore(config_backup, 1, Config, {config_backup, Backup}). + +do_backup_and_set_config_option(ConfigBackup, Option, NewValue) -> + OriginalValue = rpc(mim(), mongoose_config, lookup_opt, [Option]), + rpc(mim(), mongoose_config, set_opt, [Option, NewValue]), + %% If Option has already been backed up, keep the original value + maps:merge(#{Option => OriginalValue}, ConfigBackup). + +do_restore_config_option(Option, {ok, Value}) -> + rpc(mim(), mongoose_config, set_opt, [Option, Value]); +do_restore_config_option(Option, {error, not_found}) -> + rpc(mim(), mongoose_config, unset_opt, [Option]). diff --git a/big_tests/tests/rest_helper.erl b/big_tests/tests/rest_helper.erl index 27812ff1095..8a911942c5f 100644 --- a/big_tests/tests/rest_helper.erl +++ b/big_tests/tests/rest_helper.erl @@ -248,7 +248,7 @@ report_errors(Client, Path, Method, Headers, Body, get_port(_Role, _Node, #{port := Port}) -> Port; get_port(Role, Node, _Params) -> - Listeners = rpc(Node, ejabberd_config, get_local_option, [listen]), + Listeners = rpc(Node, mongoose_config, get_opt, [listen]), [{PortIpNet, ejabberd_cowboy, _Opts}] = lists:filter(fun(Config) -> is_roles_config(Config, Role) end, Listeners), case PortIpNet of @@ -259,7 +259,7 @@ get_port(Role, Node, _Params) -> -spec get_ssl_status(Role :: role(), Server :: distributed_helper:rpc_spec()) -> boolean(). get_ssl_status(Role, Node) -> - Listeners = rpc(Node, ejabberd_config, get_local_option, [listen]), + Listeners = rpc(Node, mongoose_config, get_opt, [listen]), [{_PortIpNet, _Module, Opts}] = lists:filter(fun (Opts) -> is_roles_config(Opts, Role) end, Listeners), lists:keymember(ssl, 1, Opts). @@ -273,13 +273,13 @@ change_admin_creds(Creds) -> -spec stop_admin_listener() -> 'ok' | {'error', 'not_found' | 'restarting' | 'running' | 'simple_one_for_one'}. stop_admin_listener() -> - Listeners = rpc(mim(), ejabberd_config, get_local_option, [listen]), + Listeners = rpc(mim(), mongoose_config, get_opt, [listen]), [{PortIpNet, Module, _Opts}] = lists:filter(fun (Opts) -> is_roles_config(Opts, admin) end, Listeners), rpc(mim(), ejabberd_listener, stop_listener, [PortIpNet, Module]). -spec start_admin_listener(Creds :: {binary(), binary()}) -> {'error', pid()} | {'ok', _}. start_admin_listener(Creds) -> - Listeners = rpc(mim(), ejabberd_config, get_local_option, [listen]), + Listeners = rpc(mim(), mongoose_config, get_opt, [listen]), [{PortIpNet, Module, Opts}] = lists:filter(fun (Opts) -> is_roles_config(Opts, admin) end, Listeners), NewOpts = insert_creds(Opts, Creds), rpc(mim(), ejabberd_listener, start_listener, [PortIpNet, Module, NewOpts]). diff --git a/big_tests/tests/s2s_helper.erl b/big_tests/tests/s2s_helper.erl index a05828cc788..80c717d2f8b 100644 --- a/big_tests/tests/s2s_helper.erl +++ b/big_tests/tests/s2s_helper.erl @@ -25,13 +25,13 @@ suite(Config) -> require_rpc_nodes([mim, fed]) ++ Config. init_s2s(Config) -> - Node1S2SCertfile = rpc(mim(), ejabberd_config, get_local_option, [s2s_certfile]), - Node1S2SUseStartTLS = rpc(mim(), ejabberd_config, get_local_option, [s2s_use_starttls]), + Node1S2SCertfile = rpc(mim(), mongoose_config, get_opt, [s2s_certfile, undefined]), + Node1S2SUseStartTLS = rpc(mim(), mongoose_config, get_opt, [s2s_use_starttls, undefined]), Node1S2SPort = ct:get_config({hosts, mim, incoming_s2s_port}), [Node1S2SListener] = mongoose_helper:get_listener_opts(mim(), Node1S2SPort), - Node2S2SCertfile = rpc(fed(), ejabberd_config, get_local_option, [s2s_certfile]), - Node2S2SUseStartTLS = rpc(fed(), ejabberd_config, get_local_option, [s2s_use_starttls]), + Node2S2SCertfile = rpc(fed(), mongoose_config, get_opt, [s2s_certfile, undefined]), + Node2S2SUseStartTLS = rpc(fed(), mongoose_config, get_opt, [s2s_use_starttls, undefined]), Node2S2SPort = ct:get_config({hosts, fed, incoming_s2s_port}), [Node2S2SListener] = mongoose_helper:get_listener_opts(fed(), Node2S2SPort), S2S = #s2s_opts{node1_s2s_certfile = Node1S2SCertfile, @@ -115,8 +115,13 @@ configure_s2s(#s2s_opts{node1_s2s_certfile = Certfile1, restart_s2s(S2SOpts). configure_s2s(#{} = Spec, Certfile, StartTLS) -> - rpc(Spec, ejabberd_config, add_local_option, [s2s_certfile, Certfile]), - rpc(Spec, ejabberd_config, add_local_option, [s2s_use_starttls, StartTLS]). + set_or_unset_opt(Spec, s2s_certfile, Certfile), + set_or_unset_opt(Spec, s2s_use_starttls, StartTLS). + +set_or_unset_opt(Spec, Opt, undefined) -> + rpc(Spec, mongoose_config, unset_opt, [Opt]); +set_or_unset_opt(Spec, Opt, Value) -> + rpc(Spec, mongoose_config, set_opt, [Opt, Value]). restart_s2s(#s2s_opts{node1_s2s_listener = Node1S2SListener, node2_s2s_listener = Node2S2SListener}) -> From 35f06556093645f88fc4417df3ca6b8a32a9c86c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Wed, 20 Oct 2021 18:54:42 +0200 Subject: [PATCH 04/10] Use mongoose_config in big tests --- big_tests/tests/accounts_SUITE.erl | 54 ++++------ big_tests/tests/connect_SUITE.erl | 18 +--- big_tests/tests/login_SUITE.erl | 100 +++++++++--------- big_tests/tests/mam_SUITE.erl | 53 +++------- big_tests/tests/metrics_register_SUITE.erl | 6 +- big_tests/tests/mongooseimctl_SUITE.erl | 7 +- big_tests/tests/oauth_SUITE.erl | 17 ++- big_tests/tests/sasl_SUITE.erl | 9 +- .../service_mongoose_system_metrics_SUITE.erl | 17 ++- big_tests/tests/shared_roster_SUITE.erl | 2 +- 10 files changed, 116 insertions(+), 167 deletions(-) diff --git a/big_tests/tests/accounts_SUITE.erl b/big_tests/tests/accounts_SUITE.erl index f13695f2fab..6cf83d24ce4 100644 --- a/big_tests/tests/accounts_SUITE.erl +++ b/big_tests/tests/accounts_SUITE.erl @@ -91,8 +91,8 @@ init_per_group(bad_cancelation, Config) -> init_per_group(change_account_details, Config) -> [{escalus_user_db, {module, escalus_ejabberd}} |Config]; init_per_group(change_account_details_store_plain, Config) -> - Config1 = mongoose_helper:backup_auth_config(Config), - mongoose_helper:set_store_password(plain), + AuthOpts = mongoose_helper:auth_opts_with_password_format(plain), + Config1 = mongoose_helper:backup_and_set_config_option(Config, auth_opts, AuthOpts), [{escalus_user_db, {module, escalus_ejabberd}} |Config1]; init_per_group(registration_timeout, Config) -> set_registration_timeout(Config); @@ -102,8 +102,7 @@ init_per_group(users_number_estimate, Config) -> AuthOpts = get_auth_opts(), Key = rdbms_users_number_estimate, NewAuthOpts = lists:keystore(Key, 1, AuthOpts, {Key, true}), - set_auth_opts(NewAuthOpts), - [{auth_opts, AuthOpts} | Config]; + set_auth_opts(Config, NewAuthOpts); init_per_group(_GroupName, Config) -> Config. @@ -112,7 +111,7 @@ end_per_group(change_account_details, Config) -> [{escalus_user_db, xmpp} | Config]; end_per_group(change_account_details_store_plain, Config) -> escalus_fresh:clean(), - mongoose_helper:restore_auth_config(Config), + mongoose_helper:restore_config(Config), [{escalus_user_db, xmpp} | Config]; end_per_group(bad_cancelation, Config) -> escalus:delete_users(Config, escalus:get_users([alice])); @@ -121,18 +120,19 @@ end_per_group(registration_timeout, Config) -> end_per_group(utilities, Config) -> escalus:delete_users(Config, escalus:get_users([alice, bob])); end_per_group(users_number_estimate, Config) -> - StoredAuthOpts = ?config(auth_opts, Config), - set_auth_opts(StoredAuthOpts); + mongoose_helper:restore_config(Config); end_per_group(_GroupName, Config) -> Config. get_auth_opts() -> - rpc(mim(), ejabberd_config, get_local_option, [{auth_opts, host_type()}]). + rpc(mim(), mongoose_config, get_opt, [{auth_opts, host_type()}]). -set_auth_opts(AuthOpts) -> +set_auth_opts(Config, AuthOpts) -> rpc(mim(), ejabberd_auth, stop, [host_type()]), - rpc(mim(), ejabberd_config, add_local_option, [{auth_opts, host_type()}, AuthOpts]), - rpc(mim(), ejabberd_auth, start, [host_type()]). + Config1 = mongoose_helper:backup_and_set_config_option(Config, {auth_opts, host_type()}, + AuthOpts), + rpc(mim(), ejabberd_auth, start, [host_type()]), + Config1. init_per_testcase(admin_notify, Config) -> [{_, AdminSpec}] = escalus_users:get_users([admin]), @@ -145,8 +145,8 @@ init_per_testcase(not_allowed_registration_cancelation, Config) -> reload_mod_register_option(Config, access, {access, none}), escalus:init_per_testcase(not_allowed_registration_cancelation, Config); init_per_testcase(registration_failure_timeout, Config) -> - ok = deny_everyone_registration(), - escalus:init_per_testcase(registration_failure_timeout, Config); + Config1 = deny_everyone_registration(Config), + escalus:init_per_testcase(registration_failure_timeout, Config1); init_per_testcase(CaseName, Config) when CaseName =:= list_selected_users; CaseName =:= count_selected_users -> case mongoose_helper:auth_modules() of @@ -175,7 +175,7 @@ end_per_testcase(registration_timeout, Config) -> escalus:delete_users(Config, escalus:get_users([alice, bob])), escalus:end_per_testcase(registration_timeout, Config); end_per_testcase(registration_failure_timeout, Config) -> - ok = allow_everyone_registration(), + mongoose_helper:restore_config_option(Config, {access, register, global}), escalus:end_per_testcase(registration_failure_timeout, Config); end_per_testcase(CaseName, Config) -> escalus:end_per_testcase(CaseName, Config). @@ -405,28 +405,14 @@ strong_pwd() -> <<"Sup3r","c4li","fr4g1","l1571c","3xp1","4l1","d0c10u5">>. set_registration_timeout(Config) -> - Record = {local_config, registration_timeout, ?REGISTRATION_TIMEOUT}, - OldTimeout = rpc(mim(), ejabberd_config, get_local_option, [registration_timeout]), - true = rpc(mim(), ets, insert, [local_config, Record]), - [ {old_timeout, OldTimeout} | Config ]. + mongoose_helper:backup_and_set_config_option(Config, registration_timeout, + ?REGISTRATION_TIMEOUT). restore_registration_timeout(Config) -> - {old_timeout, OldTimeout} = proplists:lookup(old_timeout, Config), - Record = {local_config, registration_timeout, OldTimeout}, - true = rpc(mim(), ets, insert, [local_config, Record]), - proplists:delete(old_timeout, Config). - -deny_everyone_registration() -> - ok = change_registration_settings_for_everyone(deny). - -allow_everyone_registration() -> - ok = change_registration_settings_for_everyone(allow). - -change_registration_settings_for_everyone(Rule) - when allow =:= Rule; deny =:= Rule -> - {atomic,ok} = rpc(mim(), ejabberd_config, add_local_option, - [{access, register, global}, [{Rule, all}]]), - ok. + mongoose_helper:restore_config_option(Config, registration_timeout). + +deny_everyone_registration(Config) -> + mongoose_helper:backup_and_set_config_option(Config, {access, register, global}, [{deny, all}]). has_registered_element(Stanza) -> [#xmlel{name = <<"registered">>}] =:= exml_query:paths(Stanza, diff --git a/big_tests/tests/connect_SUITE.erl b/big_tests/tests/connect_SUITE.erl index 6484a510905..a3b8b6d070d 100644 --- a/big_tests/tests/connect_SUITE.erl +++ b/big_tests/tests/connect_SUITE.erl @@ -185,26 +185,18 @@ end_per_group(_, Config) -> Config. init_per_testcase(close_connection_if_service_type_is_hidden = CN, Config) -> - OptName = hide_service_name, - mongoose_helper:successful_rpc(ejabberd_config, add_local_option, [OptName, true]), - escalus:init_per_testcase(CN, Config); + Config1 = mongoose_helper:backup_and_set_config_option(Config, hide_service_name, true), + escalus:init_per_testcase(CN, Config1); init_per_testcase(replaced_session_cannot_terminate = CN, Config) -> S = escalus_users:get_server(Config, alice), OptKey = {replaced_wait_timeout, S}, - {atomic, _} = rpc(mim(), ejabberd_config, add_local_option, [OptKey, 1]), - escalus:init_per_testcase(CN, [{opt_to_del, OptKey} | Config]); + Config1 = mongoose_helper:backup_and_set_config_option(Config, OptKey, 1), + escalus:init_per_testcase(CN, Config1); init_per_testcase(CaseName, Config) -> escalus:init_per_testcase(CaseName, Config). -end_per_testcase(close_connection_if_service_type_is_hidden = CN, Config) -> - OptName = hide_service_name, - mongoose_helper:successful_rpc(ejabberd_config, del_local_option, [OptName]), - escalus:end_per_testcase(CN, Config); -end_per_testcase(replaced_session_cannot_terminate = CN, Config) -> - {_, OptKey} = lists:keyfind(opt_to_del, 1, Config), - {atomic, _} = rpc(mim(), ejabberd_config, del_local_option, [OptKey]), - escalus:end_per_testcase(CN, Config); end_per_testcase(CaseName, Config) -> + mongoose_helper:restore_config(Config), escalus:end_per_testcase(CaseName, Config). %%-------------------------------------------------------------------- diff --git a/big_tests/tests/login_SUITE.erl b/big_tests/tests/login_SUITE.erl index 699141fe2dc..e7af1e4822d 100644 --- a/big_tests/tests/login_SUITE.erl +++ b/big_tests/tests/login_SUITE.erl @@ -113,70 +113,82 @@ suite() -> %%-------------------------------------------------------------------- init_per_suite(Config) -> - Config0 = mongoose_helper:backup_auth_config(Config), - Config1 = mongoose_helper:backup_sasl_mechanisms_config(Config0), - mongoose_helper:set_store_password(scram), - escalus:init_per_suite(Config1). + escalus:init_per_suite(Config). end_per_suite(Config) -> escalus_fresh:clean(), - mongoose_helper:restore_auth_config(Config), - mongoose_helper:restore_sasl_mechanisms_config(Config), escalus:end_per_suite(Config). -init_per_group(login_digest, ConfigIn) -> - Config = mongoose_helper:backup_sasl_mechanisms_config(ConfigIn), - mongoose_helper:set_store_password(plain), +init_per_group(login_digest = GroupName, ConfigIn) -> + Config = backup_and_set_options(GroupName, ConfigIn), case mongoose_helper:supports_sasl_module(cyrsasl_digest) of false -> - mongoose_helper:set_store_password(scram), + mongoose_helper:restore_config(Config), {skip, "digest password type not supported"}; true -> - Config1 = configure_digest(Config), - escalus:create_users(Config1, escalus:get_users([alice, bob])) + escalus:create_users(Config, escalus:get_users([alice, bob])) end; -init_per_group(GroupName, Config) when - GroupName == login_scram; GroupName == login_scram_store_plain -> +init_per_group(GroupName, ConfigIn) + when GroupName == login_scram; + GroupName == login_scram_store_plain -> + Config = backup_and_set_options(GroupName, ConfigIn), case are_sasl_scram_modules_supported() of false -> + mongoose_helper:restore_config(Config), {skip, "scram password type not supported"}; true -> - config_password_format(GroupName), Config2 = escalus:create_users(Config, escalus:get_users([alice, bob, neustradamus])), assert_password_format(GroupName, Config2) end; -init_per_group(login_scram_tls, Config) -> +init_per_group(login_scram_tls = GroupName, ConfigIn) -> + Config = backup_and_set_options(GroupName, ConfigIn), case are_sasl_scram_modules_supported() of false -> + mongoose_helper:restore_config(Config), {skip, "scram password type not supported"}; true -> Config1 = config_ejabberd_node_tls(Config), - config_password_format(login_scram_tls), Config2 = create_tls_users(Config1), assert_password_format(scram, Config2) end; -init_per_group(login_specific_scram, Config) -> +init_per_group(login_specific_scram = GroupName, ConfigIn) -> + Config = backup_and_set_options(GroupName, ConfigIn), case are_sasl_scram_modules_supported() of false -> + mongoose_helper:restore_config(Config), {skip, "scram password type not supported"}; true -> escalus:create_users(Config, escalus:get_users([alice, bob, neustradamus])) end; -init_per_group(_GroupName, Config) -> +init_per_group(GroupName, ConfigIn) -> + Config = backup_and_set_options(GroupName, ConfigIn), escalus:create_users(Config, escalus:get_users([alice, bob])). +backup_and_set_options(GroupName, Config) -> + Options = config_options(GroupName), + mongoose_helper:backup_and_set_config(Config, Options). + +config_options(login_digest) -> + #{{auth_opts, host_type()} => mongoose_helper:auth_opts_with_password_format(plain), + {sasl_mechanisms, host_type()} => [cyrsasl_digest]}; +config_options(login_scram_store_plain) -> + #{{auth_opts, host_type()} => mongoose_helper:auth_opts_with_password_format(plain)}; +config_options(_GroupName) -> + #{{auth_opts, host_type()} => mongoose_helper:auth_opts_with_password_format(scram)}. + end_per_group(login_digest, Config) -> - mongoose_helper:set_store_password(scram), - mongoose_helper:restore_sasl_mechanisms_config(Config), + mongoose_helper:restore_config(Config), escalus:delete_users(Config, escalus:get_users([alice, bob])); end_per_group(GroupName, Config) when GroupName == login_scram; GroupName == login_specific_scram -> - mongoose_helper:set_store_password(scram), + mongoose_helper:restore_config(Config), escalus:delete_users(Config, escalus:get_users([alice, bob, neustradamus])); end_per_group(login_scram_tls, Config) -> + mongoose_helper:restore_config(Config), restore_c2s(Config), delete_tls_users(Config); end_per_group(_GroupName, Config) -> + mongoose_helper:restore_config(Config), escalus:delete_users(Config, escalus:get_users([alice, bob])). init_per_testcase(CaseName, Config) when @@ -189,7 +201,7 @@ init_per_testcase(CaseName, Config) when end; init_per_testcase(message_zlib_limit, Config) -> Listeners = [Listener - || {Listener, _, _} <- rpc(mim(), ejabberd_config, get_local_option, [listen])], + || {Listener, _, _} <- rpc(mim(), mongoose_config, get_opt, [listen])], [{_U, Props}] = escalus_users:get_users([hacker]), Port = proplists:get_value(port, Props), case lists:keymember(Port, 1, Listeners) of @@ -346,9 +358,9 @@ log_non_existent(Config) -> {error, {connection_step_failed, _, R}} = escalus_client:start(Config, UserSpec, <<"res">>), R. -blocked_user(_Config) -> +blocked_user(Config) -> [{_, Spec}] = escalus_users:get_users([alice]), - set_acl_for_blocking(Spec), + Config1 = set_acl_for_blocking(Config, Spec), try {ok, _Alice, _Spec2, _Features} = escalus_connection:start(Spec), ct:fail("Alice authenticated but shouldn't") @@ -356,7 +368,7 @@ blocked_user(_Config) -> error:{assertion_failed, assert, is_iq_result, Stanza, _Bin} -> <<"cancel">> = exml_query:path(Stanza, [{element, <<"error">>}, {attr, <<"type">>}]) after - unset_acl_for_blocking() + unset_acl_for_blocking(Config1) end, ok. @@ -398,11 +410,6 @@ config_ejabberd_node_tls(Config) -> mongoose_helper:restart_listener_with_opts(mim(), C2SListener, NewOpts), [{c2s_listener, C2SListener} | Config]. -configure_digest(Config) -> - mongoose_helper:set_sasl_mechanisms(sasl_mechanisms, [cyrsasl_digest]), - mongoose_helper:set_store_password(plain), - Config. - create_tls_users(Config) -> Config1 = escalus:create_users(Config, escalus:get_users([alice, neustradamus])), Users = proplists:get_value(escalus_users, Config1, []), @@ -417,11 +424,6 @@ create_tls_users(Config) -> delete_tls_users(Config) -> escalus:delete_users(Config, escalus:get_users([alice, neustradamus])). -config_password_format(GN) when GN == login_scram; GN == login_scram_tls -> - mongoose_helper:set_store_password(scram); -config_password_format(_) -> - mongoose_helper:set_store_password(plain). - assert_password_format(GroupName, Config) -> Users = proplists:get_value(escalus_users, Config), [verify_format(GroupName, User) || User <- Users], @@ -452,34 +454,36 @@ do_verify_format(login_scram, _Password, SPassword) -> do_verify_format(_, Password, SPassword) -> Password = SPassword. -set_acl_for_blocking(Spec) -> +set_acl_for_blocking(Config, Spec) -> User = proplists:get_value(username, Spec), LUser = jid:nodeprep(User), - rpc(mim(), ejabberd_config, add_local_option, [{acl, blocked, host_type()}, [{user, LUser}]]). + mongoose_helper:backup_and_set_config_option(Config, {acl, blocked, host_type()}, + [{user, LUser}]). -unset_acl_for_blocking() -> - rpc(mim(), ejabberd_config, del_local_option, [{acl, blocked, host_type()}]). +unset_acl_for_blocking(Config) -> + mongoose_helper:restore_config_option(Config, {acl, blocked, host_type()}). configure_and_log_scram(Config, Sha, Mech) -> - mongoose_helper:set_store_password({scram, [Sha]}), - assert_password_format({scram, Sha}, Config), + set_scram_sha(Config, Sha), log_one([{escalus_auth_method, Mech} | Config]). configure_and_log_scram_plus(Config, Sha, Mech) -> - mongoose_helper:set_store_password({scram, [Sha]}), - assert_password_format({scram, Sha}, Config), + set_scram_sha(Config, Sha), log_one_scram_plus([{escalus_auth_method, Mech} | Config]). configure_and_fail_log_scram(Config, Sha, Mech) -> - mongoose_helper:set_store_password({scram, [Sha]}), - assert_password_format({scram, Sha}, Config), + set_scram_sha(Config, Sha), {expected_challenge, _, _} = fail_log_one([{escalus_auth_method, Mech} | Config]). configure_scram_plus_and_fail_log_scram(Config, Sha, Mech) -> - mongoose_helper:set_store_password({scram, [Sha]}), - assert_password_format({scram, Sha}, Config), + set_scram_sha(Config, Sha), {expected_challenge, _, _} = fail_log_one_scram_plus([{escalus_auth_method, Mech} | Config]). +set_scram_sha(Config, Sha) -> + NewAuthOpts = mongoose_helper:auth_opts_with_password_format({scram, [Sha]}), + mongoose_helper:change_config_option(Config, {auth_opts, host_type()}, NewAuthOpts), + assert_password_format({scram, Sha}, Config). + fail_log_one(Config) -> [{alice, UserSpec}] = escalus_users:get_users([alice]), {error, {connection_step_failed, _, R}} = escalus_client:start(Config, UserSpec, <<"res">>), diff --git a/big_tests/tests/mam_SUITE.erl b/big_tests/tests/mam_SUITE.erl index 5bf5d78c9c1..f41f1c60959 100644 --- a/big_tests/tests/mam_SUITE.erl +++ b/big_tests/tests/mam_SUITE.erl @@ -521,9 +521,9 @@ suite() -> init_per_suite(Config) -> muc_helper:load_muc(), - disable_sessions_limit(disable_shaping( + increase_limits( delete_users([{escalus_user_db, {module, escalus_ejabberd}} - | escalus:init_per_suite(Config)]))). + | escalus:init_per_suite(Config)])). end_per_suite(Config) -> muc_helper:unload_muc(), @@ -532,7 +532,8 @@ end_per_suite(Config) -> %% and this function kicks them without waiting... mongoose_helper:kick_everyone(), %% so we don't have sessions anymore and other tests will not fail - escalus:end_per_suite(restore_sessions_limit(restore_shaping(Config))). + mongoose_helper:restore_config(Config), + escalus:end_per_suite(Config). user_names() -> [alice, bob, kate, carol]. @@ -543,44 +544,16 @@ create_users(Config) -> delete_users(Config) -> escalus:delete_users(Config, escalus:get_users(user_names())). -disable_shaping(Config) -> - OldShaper = get_shaper(), - set_shaper({{maxrate, 10000}, {maxrate, 10000000}, {maxrate, 10000000}}), - [{old_mam_shaper, OldShaper}|Config]. +increase_limits(Config) -> + Config1 = mongoose_helper:backup_and_set_config(Config, increased_limits()), + rpc_apply(shaper_srv, reset_all_shapers, [host_type()]), + Config1. -restore_shaping(Config) -> - OldShaper = proplists:get_value(old_mam_shaper, Config), - set_shaper(OldShaper), - Config. - -get_shaper() -> - Mam = rpc_apply(ejabberd_config, get_local_option, [{shaper, mam_shaper, global}]), - Norm = rpc_apply(ejabberd_config, get_local_option, [{shaper, normal, global}]), - Fast = rpc_apply(ejabberd_config, get_local_option, [{shaper, fast, global}]), - {Mam, Norm, Fast}. - -set_shaper({Mam, Norm, Fast}) -> - rpc_apply(ejabberd_config, add_local_option, [{shaper, mam_shaper, global}, Mam]), - rpc_apply(ejabberd_config, add_local_option, [{shaper, normal, global}, Norm]), - rpc_apply(ejabberd_config, add_local_option, [{shaper, fast, global}, Fast]), - rpc_apply(shaper_srv, reset_all_shapers, [host_type()]). - -disable_sessions_limit(Config) -> - OldLimit = get_sessions_limit(), - set_sessions_limit([{10000, all}]), - [{old_sessions_limit, OldLimit}|Config]. - -restore_sessions_limit(Config) -> - OldLimit = proplists:get_value(old_sessions_limit, Config), - set_sessions_limit(OldLimit), - Config. - -get_sessions_limit() -> - rpc_apply(ejabberd_config, get_local_option, [{access, max_user_sessions, global}]). - -set_sessions_limit(NewLimit) -> - rpc_apply(ejabberd_config, add_local_option, - [{access, max_user_sessions, global}, NewLimit]). +increased_limits() -> + #{{shaper, mam_shaper, global} => {maxrate, 10000}, + {shaper, normal, global} => {maxrate, 10000000}, + {shaper, fast, global} => {maxrate, 10000000}, + {access, max_user_sessions, global} => [{10000, all}]}. init_per_group(mam04, Config) -> [{props, mam04_props()}|Config]; diff --git a/big_tests/tests/metrics_register_SUITE.erl b/big_tests/tests/metrics_register_SUITE.erl index aba502d4341..2dbd948223b 100644 --- a/big_tests/tests/metrics_register_SUITE.erl +++ b/big_tests/tests/metrics_register_SUITE.erl @@ -67,10 +67,10 @@ init_per_testcase(unregister, Config) -> escalus_users:create_user(Config, Alice), Config; init_per_testcase(registered_users, Config) -> - case rpc(mim(), ejabberd_config, get_local_option, [{auth_method, host_type()}]) of - external -> + case rpc(mim(), mongoose_config, lookup_opt, [{auth_method, host_type()}]) of + {ok, external} -> {skip, "counter not supported with ejabberd_auth_external"}; - anonymous -> + {ok, anonymous} -> {skip, "counter not supported with anonymous authentication"}; _ -> Config diff --git a/big_tests/tests/mongooseimctl_SUITE.erl b/big_tests/tests/mongooseimctl_SUITE.erl index cebff250125..f1a3626c6e5 100644 --- a/big_tests/tests/mongooseimctl_SUITE.erl +++ b/big_tests/tests/mongooseimctl_SUITE.erl @@ -276,8 +276,9 @@ init_per_testcase(check_password_hash, Config) -> true -> {skip, not_fully_supported_with_ldap}; false -> - Config1 = mongoose_helper:backup_auth_config(Config), - mongoose_helper:set_store_password(plain), + AuthOpts = mongoose_helper:auth_opts_with_password_format(plain), + Config1 = mongoose_helper:backup_and_set_config_option(Config, {auth_opts, host_type()}, + AuthOpts), Config2 = escalus:create_users(Config1, escalus:get_users([carol])), escalus:init_per_testcase(check_password_hash, Config2) end; @@ -305,7 +306,7 @@ end_per_testcase(delete_old_users, Config) -> end, Users), escalus:end_per_testcase(delete_old_users, Config); end_per_testcase(check_password_hash, Config) -> - mongoose_helper:restore_auth_config(Config), + mongoose_helper:restore_config(Config), escalus:delete_users(Config, escalus:get_users([carol])); end_per_testcase(CaseName, Config) -> %% Because kick_session fails with unexpected stanza received: diff --git a/big_tests/tests/oauth_SUITE.erl b/big_tests/tests/oauth_SUITE.erl index edbe92942ba..ab21a49dcc8 100644 --- a/big_tests/tests/oauth_SUITE.erl +++ b/big_tests/tests/oauth_SUITE.erl @@ -113,16 +113,20 @@ init_per_group(GroupName, Config0) -> commands -> ejabberd_node_utils:init(Config0); _ -> Config0 end, - Config1 = mongoose_helper:backup_auth_config(Config), - config_password_format(GroupName), + AuthOpts = mongoose_helper:auth_opts_with_password_format(password_format(GroupName)), + HostType = domain_helper:host_type(), + Config1 = mongoose_helper:backup_and_set_config_option(Config, {auth_opts, HostType}, AuthOpts), Config2 = escalus:create_users(Config1, escalus:get_users([bob, alice])), assert_password_format(GroupName, Config2). +password_format(login_scram) -> scram; +password_format(_) -> plain. + end_per_group(cleanup, Config) -> - mongoose_helper:restore_auth_config(Config), + mongoose_helper:restore_config(Config), escalus:delete_users(Config, escalus:get_users([alice])); end_per_group(_GroupName, Config) -> - mongoose_helper:restore_auth_config(Config), + mongoose_helper:restore_config(Config), escalus:delete_users(Config, escalus:get_users([bob, alice])). init_per_testcase(check_for_oauth_with_mod_auth_token_not_loaded, Config) -> @@ -370,11 +374,6 @@ extract_tokens(#xmlel{name = <<"iq">>, children = [#xmlel{name = <<"items">>} = RTD = exml_query:path(Items, [{element, <<"refresh_token">>}, cdata]), {base64:decode(ATD), base64:decode(RTD)}. -config_password_format(login_scram) -> - monggose_helper:set_store_password(scram); -config_password_format(_) -> - mongoose_helper:set_store_password(plain). - assert_password_format(GroupName, Config) -> Users = proplists:get_value(escalus_users, Config), [verify_format(GroupName, User) || User <- Users], diff --git a/big_tests/tests/sasl_SUITE.erl b/big_tests/tests/sasl_SUITE.erl index 8a79bff52c3..db0a5668889 100644 --- a/big_tests/tests/sasl_SUITE.erl +++ b/big_tests/tests/sasl_SUITE.erl @@ -101,15 +101,10 @@ set_sasl_mechanisms(Key, Config) -> fun(_, M) -> M =:= ?MODULE end]), %% configure the mechanism - Mechs = rpc(mim(), ejabberd_config, get_local_option, [Key]), - rpc(mim(), ejabberd_config, add_local_option, [Key, [?MODULE]]), - [{mechs, Mechs} | Config]. + mongoose_helper:backup_and_set_config_option(Config, Key, [?MODULE]). reset_sasl_mechanisms(Key, Config) -> - case ?config(mechs, Config) of - undefined -> rpc(mim(), ejabberd_config, del_local_option, [Key]); - Mechs -> rpc(mim(), ejabberd_config, add_local_option, [Key, Mechs]) - end, + mongoose_helper:restore_config_option(Config, Key), rpc(mim(), meck, unload, [ejabberd_auth]). assert_is_failure_with_text(#xmlel{name = <<"failure">>, diff --git a/big_tests/tests/service_mongoose_system_metrics_SUITE.erl b/big_tests/tests/service_mongoose_system_metrics_SUITE.erl index de4f5c3c597..eb8b66e478b 100644 --- a/big_tests/tests/service_mongoose_system_metrics_SUITE.erl +++ b/big_tests/tests/service_mongoose_system_metrics_SUITE.erl @@ -236,7 +236,7 @@ system_metrics_are_reported_to_google_analytics_when_mim_starts(_Config) -> all_event_have_the_same_client_id(). tracking_id_is_correctly_configured(_Config) -> - TrackingId = distributed_helper:rpc(mim(), ejabberd_config, get_local_option, [google_analytics_tracking_id]), + TrackingId = distributed_helper:rpc(mim(), mongoose_config, get_opt, [google_analytics_tracking_id]), case os:getenv("CI") of "true" -> ?assertEqual(?TRACKING_ID_CI, TrackingId); @@ -395,7 +395,7 @@ enable_system_metrics(Node) -> enable_system_metrics(Node, Timers) -> UrlArgs = [google_analytics_url, ?SERVER_URL], - {atomic, ok} = mongoose_helper:successful_rpc(Node, ejabberd_config, add_local_option, UrlArgs), + ok = mongoose_helper:successful_rpc(Node, mongoose_config, set_opt, UrlArgs), start_system_metrics_module(Node, Timers). enable_system_metrics_with_configurable_tracking_id(Node) -> @@ -407,7 +407,7 @@ start_system_metrics_module(Node, Args) -> disable_system_metrics(Node) -> distributed_helper:rpc(Node, mongoose_service, stop_service, [service_mongoose_system_metrics]), - mongoose_helper:successful_rpc(Node, ejabberd_config, del_local_option, [ google_analytics_url ]). + mongoose_helper:successful_rpc(Node, mongoose_config, unset_opt, [ google_analytics_url ]). delete_prev_client_id(Node) -> mongoose_helper:successful_rpc(Node, mnesia, delete_table, [service_mongoose_system_metrics]). @@ -427,17 +427,16 @@ system_metrics_service_is_disabled(Node) -> configure_additional_tracking_id(Node) -> TrackingIdArgs = [extra_google_analytics_tracking_id, ?TRACKING_ID_EXTRA], - {atomic, ok} = mongoose_helper:successful_rpc(Node, ejabberd_config, add_local_option, TrackingIdArgs). + ok = mongoose_helper:successful_rpc(Node, mongoose_config, set_opt, TrackingIdArgs). remove_additional_tracking_id(Node) -> mongoose_helper:successful_rpc( - Node, ejabberd_config, del_local_option, [ extra_google_analytics_tracking_id ]). + Node, mongoose_config, unset_opt, [ extra_google_analytics_tracking_id ]). remove_service_from_config(Service) -> - Services = distributed_helper:rpc( - mim3(), ejabberd_config, get_local_option_or_default, [services, []]), - NewServices = proplists:delete(Service, Services), - distributed_helper:rpc(mim3(), ejabberd_config, add_local_option, [services, NewServices]). + Services = distributed_helper:rpc(mim3(), mongoose_config, get_opt, [services]), + NewServices = proplists:delete(Service, Services), + distributed_helper:rpc(mim3(), mongoose_config, set_opt, [services, NewServices]). events_are_reported_to_additional_tracking_id() -> Tab = ets:tab2list(?ETS_TABLE), diff --git a/big_tests/tests/shared_roster_SUITE.erl b/big_tests/tests/shared_roster_SUITE.erl index 2f38cdb9e72..a9f87daf496 100644 --- a/big_tests/tests/shared_roster_SUITE.erl +++ b/big_tests/tests/shared_roster_SUITE.erl @@ -165,7 +165,7 @@ stop_roster_module(_) -> get_auth_method() -> XMPPDomain = domain(), - case rpc(mim(), ejabberd_config, get_local_option, [{auth_method, XMPPDomain}]) of + case rpc(mim(), mongoose_config, get_opt, [{auth_method, XMPPDomain}]) of [Method|_] -> Method; _ -> From a362d8616a21a4c96e277f6be9057b610be29725 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Thu, 21 Oct 2021 11:28:25 +0200 Subject: [PATCH 05/10] Remove obsolete vcard_update This module was handling the old versions of ejabberd --- big_tests/tests/gdpr_SUITE.erl | 4 +- big_tests/tests/sm_SUITE.erl | 35 +++------ big_tests/tests/vcard_SUITE.erl | 34 ++++----- big_tests/tests/vcard_helper.erl | 8 +++ big_tests/tests/vcard_simple_SUITE.erl | 2 +- big_tests/tests/vcard_update.erl | 98 -------------------------- 6 files changed, 37 insertions(+), 144 deletions(-) create mode 100644 big_tests/tests/vcard_helper.erl delete mode 100644 big_tests/tests/vcard_update.erl diff --git a/big_tests/tests/gdpr_SUITE.erl b/big_tests/tests/gdpr_SUITE.erl index d7f23e0bc51..316f77549cf 100644 --- a/big_tests/tests/gdpr_SUITE.erl +++ b/big_tests/tests/gdpr_SUITE.erl @@ -254,14 +254,14 @@ init_per_testcase(CN, Config) when CN =:= retrieve_inbox_muc; Config0; init_per_testcase(retrieve_vcard = CN, Config) -> - case vcard_update:is_vcard_ldap() of + case vcard_helper:is_vcard_ldap() of true -> {skip, skipped_for_simplicity_for_now}; % TODO: Fix the case for LDAP as well _ -> escalus:init_per_testcase(CN, Config) end; init_per_testcase(remove_vcard = CN, Config) -> - case vcard_update:is_vcard_ldap() of + case vcard_helper:is_vcard_ldap() of true -> {skip, skipped_for_simplicity_for_now}; % TODO: Fix the case for LDAP as well _ -> diff --git a/big_tests/tests/sm_SUITE.erl b/big_tests/tests/sm_SUITE.erl index 87d99e1378f..dd9431d5683 100644 --- a/big_tests/tests/sm_SUITE.erl +++ b/big_tests/tests/sm_SUITE.erl @@ -13,9 +13,6 @@ require_rpc_nodes/1, rpc/4]). --import(vcard_update, [discard_vcard_update/1, - server_string/1]). - -import(escalus_stanza, [setattr/3]). -import(domain_helper, [host_type/0]). @@ -364,7 +361,6 @@ h_ok_after_a_chat(ConfigIn) -> Config = escalus_users:update_userspec(ConfigIn, alice, stream_management, true), escalus:fresh_story(Config, [{alice,1}, {bob,1}], fun(Alice, Bob) -> - NDiscarded = discard_vcard_update(Alice), escalus:send(Alice, escalus_stanza:chat_to(Bob, <<"Hi, Bob!">>)), escalus:assert(is_chat_message, [<<"Hi, Bob!">>], escalus:wait_for_stanza(Bob)), @@ -380,7 +376,7 @@ h_ok_after_a_chat(ConfigIn) -> escalus:send(Alice, escalus_stanza:sm_request()), escalus:assert(is_sm_ack, [3], escalus:wait_for_stanza(Alice)), %% Ack, so that unacked messages don't go into offline store. - escalus:send(Alice, escalus_stanza:sm_ack(3 + NDiscarded)) + escalus:send(Alice, escalus_stanza:sm_ack(3)) end). h_non_given_closes_stream_gracefully(ConfigIn) -> @@ -438,7 +434,7 @@ server_requests_ack(Config, N) -> {Alice, _} = given_fresh_user(Config, alice), %% ack request after initial presence maybe_assert_ack_request(1, N, Alice), - StanzasRec = maybe_discard_vcard_update(1, N, Alice), + StanzasRec = 1, ct:print("discarded"), escalus:send(Bob, escalus_stanza:chat_to(Alice, <<"Hi, Alice!">>)), escalus:assert(is_chat_message, [<<"Hi, Alice!">>], @@ -455,14 +451,6 @@ maybe_assert_ack_request(StanzasRec, AckRequests, Alice) -> end, StanzasRec. -maybe_discard_vcard_update(StanzasRec, AckFreq, Alice) -> - case discard_vcard_update(Alice) of - 0 -> - StanzasRec; - 1 -> - maybe_assert_ack_request(StanzasRec + 1, AckFreq, Alice) - end. - server_requests_ack_freq_2(Config) -> Config1 = escalus_users:update_userspec(Config, alice, manual_ack, true), server_requests_ack(Config1, 2). @@ -514,7 +502,6 @@ resend_unacked_on_reconnection(Config) -> Messages = [<<"msg-1">>, <<"msg-2">>, <<"msg-3">>], {Bob, _} = given_fresh_user(Config, bob), {Alice, AliceSpec0} = given_fresh_user(Config, alice), - discard_vcard_update(Alice), %% Bob sends some messages to Alice. [escalus:send(Bob, escalus_stanza:chat_to(Alice, Msg)) || Msg <- Messages], @@ -796,7 +783,7 @@ session_established(Config) -> AliceSpec = [{manual_ack, true} | escalus_fresh:create_fresh_user(Config, alice)], {Alice, _} = given_fresh_user_with_spec(AliceSpec), - {ok, C2SPid} = get_session_pid(AliceSpec, server_string("escalus-default-resource")), + {ok, C2SPid} = get_session_pid(AliceSpec, <<"escalus-default-resource">>), assert_no_offline_msgs(AliceSpec), assert_c2s_state(C2SPid, session_established), escalus_connection:stop(Alice). @@ -924,7 +911,6 @@ resume_session(Config) -> %% Resume the session. Steps = connection_steps_to_stream_resumption(SMID, 2), {ok, Alice, _} = escalus_connection:start(AliceSpec, Steps), - NDiscarded = discard_vcard_update(Alice), %% Alice receives the unacked messages from the previous %% interrupted session. Stanzas = [escalus_connection:get_stanza(Alice, {msg, I}) @@ -932,7 +918,7 @@ resume_session(Config) -> [escalus:assert(is_chat_message, [Msg], Stanza) || {Msg, Stanza} <- lists:zip(Messages, Stanzas)], %% Alice acks the received messages. - escalus_connection:send(Alice, escalus_stanza:sm_ack(5 + NDiscarded)), + escalus_connection:send(Alice, escalus_stanza:sm_ack(5)), escalus_connection:stop(Alice) end). @@ -997,7 +983,6 @@ resume_session_kills_old_C2S_gracefully(Config) -> escalus_connection:send(Alice, InitialPresence), Presence = escalus_connection:get_stanza(Alice, presence1), escalus:assert(is_presence, Presence), - discard_vcard_update(Alice), %% Monitor the C2S process and disconnect Alice. MonitorRef = erlang:monitor(process, C2SPid), @@ -1066,11 +1051,10 @@ buffer_unacked_messages_and_die(Config, AliceSpec, Bob, Messages) -> escalus_connection:send(Alice, InitialPresence), Presence = escalus_connection:get_stanza(Alice, presence1), escalus:assert(is_presence, Presence), - Res = server_string("escalus-default-resource"), + Res = <<"escalus-default-resource">>, {ok, C2SPid} = get_session_pid(AliceSpec, Res), escalus_connection:send(Alice, escalus_stanza:presence(<<"available">>)), _Presence = escalus_connection:get_stanza(Alice, presence2), - discard_vcard_update(Alice), %% Bobs sends some messages to Alice. [escalus:send(Bob, escalus_stanza:chat_to(JID, Msg)) || Msg <- Messages], @@ -1220,7 +1204,7 @@ messages_are_properly_flushed_during_resumption(Config) -> escalus:assert(is_presence, Presence), SMH = escalus_connection:get_sm_h(Alice), escalus_client:kill_connection(Config, Alice), - {ok, C2SPid} = get_session_pid(AliceSpec, server_string("escalus-default-resource")), + {ok, C2SPid} = get_session_pid(AliceSpec, <<"escalus-default-resource">>), ok = rpc(mim(), sys, suspend, [C2SPid]), % WHEN new session requests resumption @@ -1266,7 +1250,7 @@ messages_are_properly_flushed_during_resumption_p1_fsm_old(Config) -> escalus:assert(is_presence, Presence), SMH = escalus_connection:get_sm_h(Alice), escalus_client:kill_connection(Config, Alice), - {ok, C2SPid} = get_session_pid(AliceSpec, server_string("escalus-default-resource")), + {ok, C2SPid} = get_session_pid(AliceSpec, <<"escalus-default-resource">>), ok = rpc(mim(), sys, suspend, [C2SPid]), %% send some dummy event. ignored by c2s but ensures that @@ -1495,9 +1479,8 @@ get_sid_by_stream_id(SMID) -> rpc(mim(), ?MOD_SM, get_sid, [SMID]). get_us_from_spec(UserSpec) -> - ConfigUS = [proplists:get_value(username, UserSpec), - proplists:get_value(server, UserSpec)], - [U, S] = [server_string(V) || V <- ConfigUS], + U = proplists:get_value(username, UserSpec), + S = proplists:get_value(server, UserSpec), {U, S}. clear_session_table() -> diff --git a/big_tests/tests/vcard_SUITE.erl b/big_tests/tests/vcard_SUITE.erl index 132bd4c9607..ee03343c0ca 100644 --- a/big_tests/tests/vcard_SUITE.erl +++ b/big_tests/tests/vcard_SUITE.erl @@ -158,7 +158,7 @@ end_per_suite(Config) -> init_per_group(Group, Config) when Group == rw; Group == params_limited_infinity -> restart_vcard_mod(Config, Group), - case vcard_update:is_vcard_ldap() of + case vcard_helper:is_vcard_ldap() of true -> {skip, ldap_vcard_is_readonly}; _ -> @@ -462,7 +462,7 @@ search_wildcard(Config) -> ExpectedItemTups = get_search_results(Config, [<<"bobb@", Domain/binary>>, <<"aliceb@", Domain/binary>>]), - case vcard_update:is_vcard_ldap() of + case vcard_helper:is_vcard_ldap() of true -> 3 = length(ItemTups); _ -> @@ -498,7 +498,7 @@ search_rsm_pages(Config) -> escalus:assert(is_iq_result, Res1), RSMCount1 = get_rsm_count(Res1), - case vcard_update:is_vcard_ldap() of + case vcard_helper:is_vcard_ldap() of true -> <<"3">> = RSMCount1; false -> @@ -526,7 +526,7 @@ search_rsm_pages(Config) -> escalus:assert(is_iq_result, Res2), RSMCount2 = get_rsm_count(Res2), - case vcard_update:is_vcard_ldap() of + case vcard_helper:is_vcard_ldap() of true -> <<"3">> = RSMCount2; false -> @@ -538,7 +538,7 @@ search_rsm_pages(Config) -> Config, [<<"bobb@", SecDomain/binary>>, <<"aliceb@", SecDomain/binary>>]), - case vcard_update:is_vcard_ldap() of + case vcard_helper:is_vcard_ldap() of true -> ignore; _ -> @@ -575,7 +575,7 @@ search_rsm_forward(Config) -> escalus:assert(is_iq_result, Res1), RSMCount1 = get_rsm_count(Res1), - case vcard_update:is_vcard_ldap() of + case vcard_helper:is_vcard_ldap() of true -> <<"3">> = RSMCount1; false -> @@ -604,7 +604,7 @@ search_rsm_forward(Config) -> escalus:assert(is_iq_result, Res2), RSMCount2 = get_rsm_count(Res2), - case vcard_update:is_vcard_ldap() of + case vcard_helper:is_vcard_ldap() of true -> <<"3">> = RSMCount2; false -> @@ -617,7 +617,7 @@ search_rsm_forward(Config) -> Config, [<<"bobb@", SecDomain/binary>>, <<"aliceb@", SecDomain/binary>>]), - case vcard_update:is_vcard_ldap() of + case vcard_helper:is_vcard_ldap() of true -> ignore; _ -> @@ -645,7 +645,7 @@ search_rsm_forward(Config) -> escalus:assert(is_iq_result, Res3), RSMCount2 = get_rsm_count(Res3), - case vcard_update:is_vcard_ldap() of + case vcard_helper:is_vcard_ldap() of true -> <<"3">> = RSMCount2, [_] = search_result_item_tuples(Res3); @@ -684,7 +684,7 @@ search_rsm_backward(Config) -> escalus:assert(is_iq_result, Res1), RSMCount1 = get_rsm_count(Res1), - case vcard_update:is_vcard_ldap() of + case vcard_helper:is_vcard_ldap() of true -> <<"3">> = RSMCount1; false -> @@ -713,7 +713,7 @@ search_rsm_backward(Config) -> escalus:assert(is_iq_result, Res2), RSMCount2 = get_rsm_count(Res2), - case vcard_update:is_vcard_ldap() of + case vcard_helper:is_vcard_ldap() of true -> <<"3">> = RSMCount2; false -> @@ -726,7 +726,7 @@ search_rsm_backward(Config) -> Config, [<<"bobb@", SecDomain/binary>>, <<"aliceb@", SecDomain/binary>>]), - case vcard_update:is_vcard_ldap() of + case vcard_helper:is_vcard_ldap() of true -> ignore; _ -> @@ -754,7 +754,7 @@ search_rsm_backward(Config) -> escalus:assert(is_iq_result, Res3), RSMCount2 = get_rsm_count(Res3), - case vcard_update:is_vcard_ldap() of + case vcard_helper:is_vcard_ldap() of true -> <<"3">> = RSMCount2, [_] = search_result_item_tuples(Res3); @@ -795,7 +795,7 @@ search_rsm_count(Config) -> 0 = length(ItemTups), RSMCount = get_rsm_count(Res), - case vcard_update:is_vcard_ldap() of + case vcard_helper:is_vcard_ldap() of true -> <<"3">> = RSMCount; false -> @@ -1417,7 +1417,7 @@ maybe_add_jabberd_id(JabberId) -> maybe_add([{<<"JABBERID">>, JabberId}]). maybe_add(Elems) -> - case vcard_update:is_vcard_ldap() of + case vcard_helper:is_vcard_ldap() of true -> []; _ -> @@ -1471,7 +1471,7 @@ get_first_name_search_field() -> get_search_field(<<"first">>, <<"givenName">>). get_search_field(Default, LDAP) -> - case vcard_update:is_vcard_ldap() of + case vcard_helper:is_vcard_ldap() of true -> LDAP; _ -> @@ -1520,7 +1520,7 @@ reported_fields() -> ] ++ maybe_add_jabberd_field(). maybe_add_jabberd_field() -> - case vcard_update:is_vcard_ldap() of + case vcard_helper:is_vcard_ldap() of true -> []; _ -> diff --git a/big_tests/tests/vcard_helper.erl b/big_tests/tests/vcard_helper.erl new file mode 100644 index 00000000000..143ac922dc2 --- /dev/null +++ b/big_tests/tests/vcard_helper.erl @@ -0,0 +1,8 @@ +-module(vcard_helper). +-compile([export_all, nowarn_export_all]). + +-import(distributed_helper, [rpc/4, mim/0]). + +is_vcard_ldap() -> + ldap == rpc(mim(), gen_mod, get_module_opt, + [ct:get_config({hosts, mim, domain}), mod_vcard, backend, mnesia]). diff --git a/big_tests/tests/vcard_simple_SUITE.erl b/big_tests/tests/vcard_simple_SUITE.erl index fd9c3df0ece..b04719db298 100644 --- a/big_tests/tests/vcard_simple_SUITE.erl +++ b/big_tests/tests/vcard_simple_SUITE.erl @@ -29,7 +29,7 @@ -define(EL(Element, Name), exml_query:path(Element, [{element, Name}])). -define(EL_CD(Element, Name), exml_query:path(Element, [{element, Name}, cdata])). --import(vcard_update, [is_vcard_ldap/0]). +-import(vcard_helper, [is_vcard_ldap/0]). -import(distributed_helper, [mim/0, require_rpc_nodes/1, diff --git a/big_tests/tests/vcard_update.erl b/big_tests/tests/vcard_update.erl deleted file mode 100644 index f37b95ef9ba..00000000000 --- a/big_tests/tests/vcard_update.erl +++ /dev/null @@ -1,98 +0,0 @@ --module(vcard_update). --compile([export_all, nowarn_export_all]). - --include_lib("exml/include/exml.hrl"). --include_lib("escalus/include/escalus.hrl"). --include_lib("escalus/include/escalus_xmlns.hrl"). - --import(distributed_helper, [mim/0, - rpc/4]). - --type escalus_client() :: #client{}. - --spec discard_vcard_update(User) -> NDiscarded when - User :: escalus_client(), - NDiscarded :: non_neg_integer(). -discard_vcard_update(User) -> - discard_vcard_update(User, - {mod_vcard_xupdate, has_mod_vcard_xupdate()}). - -discard_vcard_update(User, {mod_vcard_xupdate, true}) -> - do_discard_vcard_update(User), - 1; -discard_vcard_update(_, _) -> - 0. - -do_discard_vcard_update(Client) -> - Presence = escalus_connection:get_stanza(Client, discard_vcard_update), - escalus:assert(fun is_vcard_update/1, Presence). - -is_vcard_update(#xmlel{name = <<"presence">>} = Stanza) -> - case exml_query:subelement(Stanza, <<"x">>) of - undefined -> false; - X -> - escalus_pred:has_ns(?NS_VCARD_UPDATE, X) - end; -is_vcard_update(_) -> - false. - -has_mod_vcard_xupdate() -> - Server = ct:get_config({hosts, mim, domain}), - rpc(mim(), gen_mod, is_loaded, [server_string(Server), mod_vcard_xupdate]). - -server_string(BString) when is_binary(BString) -> - case server_string_type() of - binary -> BString; - list -> binary_to_list(BString) - end; -server_string(String) when is_list(String) -> - case server_string_type() of - list -> String; - binary -> list_to_binary(String) - end. - --spec server_string_type() -> list | binary | no_return(). -server_string_type() -> - try - is_ejabberd_community() andalso throw(binary), - is_mongooseim() andalso throw(binary), - try_ejabberd_v2(), - error(server_string_type_unknown) - catch - throw:binary -> binary; - throw:list -> list - end. - --spec is_ejabberd_community() -> boolean(). -is_ejabberd_community() -> - Apps = rpc(mim(), application, which_applications, []), - case lists:keyfind(ejabberd, 1, Apps) of - {ejabberd, "ejabberd", "community" ++ _} -> - true; - _ -> - false - end. - --spec is_mongooseim() -> boolean(). -is_mongooseim() -> - Apps = rpc(mim(), application, which_applications, []), - case lists:keyfind(ejabberd, 1, Apps) of - {ejabberd, "ejabberd", "2.1.8+mim" ++ _} -> - true; - _ -> - false - end. - --spec try_ejabberd_v2() -> no_return(). -try_ejabberd_v2() -> - [{local_config, hosts, [XMPPDomain | _]}] = rpc(mim(), ets, lookup, [local_config, hosts]), - case XMPPDomain of - BString when is_binary(BString) -> - throw(binary); - String when is_list(String) -> - throw(list) - end. - -is_vcard_ldap()-> - ldap == rpc(mim(), gen_mod, get_module_opt, - [ct:get_config({hosts, mim, domain}), mod_vcard, backend, mnesia]). From a4dfc7b6c2897fe16629d9557630d17cb27e6b8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Thu, 21 Oct 2021 11:29:48 +0200 Subject: [PATCH 06/10] Do not test global sasl_mechanisms This option is set only for host types --- big_tests/tests/sasl_SUITE.erl | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/big_tests/tests/sasl_SUITE.erl b/big_tests/tests/sasl_SUITE.erl index db0a5668889..227281132bd 100644 --- a/big_tests/tests/sasl_SUITE.erl +++ b/big_tests/tests/sasl_SUITE.erl @@ -35,12 +35,10 @@ %%-------------------------------------------------------------------- all() -> - [{group, domain_config}, - {group, node_config}]. + [{group, host_type_config}]. groups() -> - [{domain_config, [sequence], all_tests()}, - {node_config, [sequence], all_tests()}]. + [{host_type_config, [sequence], all_tests()}]. all_tests() -> [text_response]. @@ -88,11 +86,9 @@ text_response(Config) -> %% Helpers %%-------------------------------------------------------------------- -mech_option_key(domain_config) -> +mech_option_key(host_type_config) -> HostType = domain_helper:host_type(), - {sasl_mechanisms, HostType}; -mech_option_key(node_config) -> - sasl_mechanisms. + {sasl_mechanisms, HostType}. set_sasl_mechanisms(Key, Config) -> %% pretend that an auth module is set for this mechanism From f2e2c4abc7a87b1919699220e287b3e100bc3823 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Thu, 21 Oct 2021 13:17:22 +0200 Subject: [PATCH 07/10] Add more unit tests for mongoose_config --- test/mongoose_config_SUITE.erl | 85 +++++++++++++++++++++++++--------- 1 file changed, 64 insertions(+), 21 deletions(-) diff --git a/test/mongoose_config_SUITE.erl b/test/mongoose_config_SUITE.erl index c90bb76bab3..27aff5db415 100644 --- a/test/mongoose_config_SUITE.erl +++ b/test/mongoose_config_SUITE.erl @@ -5,18 +5,21 @@ -include_lib("eunit/include/eunit.hrl"). -import(ejabberd_helper, [start_ejabberd/1, + start_ejabberd_with_config/2, stop_ejabberd/0, use_config_file/2, copy/2, data/2]). all() -> - [smoke, + [get_opt, + lookup_opt, + load_from_file, {group, cluster}]. groups() -> [ - {cluster, [], [cluster_smoke]} + {cluster, [], [cluster_load_from_file]} ]. init_per_suite(Config) -> @@ -24,6 +27,8 @@ init_per_suite(Config) -> Config. end_per_suite(_Config) -> + [persistent_term:erase(Key) || {Key = {mongoose_config, _}, _Value} <- persistent_term:get()], + persistent_term:erase(mongoose_config_state), mnesia:stop(), mnesia:delete_schema([node()]), ok. @@ -49,33 +54,70 @@ end_per_group(_GroupName, _Config) -> %% Tests %% -smoke(Config) -> - % when - start_ejabberd_with_config(Config, "mongooseim.minimal.toml"), - % then - ?assert(lists:keymember(mongooseim, 1, application:which_applications())), - % cleanup - ok = stop_ejabberd(). - -cluster_smoke(C) -> - SlaveNode = slave_node(C), - copy(data(C, "mongooseim.minimal.toml"), data(C, "mongooseim.toml")), - {ok, _} = start_ejabberd_with_config(C, "mongooseim.toml"), - {ok, _} = start_remote_ejabberd_with_config(SlaveNode, C, "mongooseim.toml"), +get_opt(_Config) -> + ?assertError(badarg, 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)), + mongoose_config:set_opt(get_me, you_got_me_again), + ?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)), + ?assertEqual(default_value, mongoose_config:get_opt(get_me, default_value)). + +lookup_opt(_Config) -> + ?assertEqual({error, not_found}, mongoose_config:lookup_opt(look_me_up)), + mongoose_config:set_opt(look_me_up, here_i_am), + ?assertEqual({ok, here_i_am}, mongoose_config:lookup_opt(look_me_up)), + mongoose_config:unset_opt(look_me_up), + ?assertEqual({error, not_found}, mongoose_config:lookup_opt(look_me_up)). + +load_from_file(Config) -> + use_config_file(Config, "mongooseim.minimal.toml"), + ok = mongoose_config:start(), + State = mongoose_config:config_state(), + check_loaded_config(State), + + ok = mongoose_config:stop(), + check_removed_config(), + + %% Try to stop it again + {error, not_started} = mongoose_config:stop(). + +cluster_load_from_file(Config) -> + SlaveNode = slave_node(Config), + copy(data(Config, "mongooseim.minimal.toml"), data(Config, "mongooseim.toml")), + + %% Start clustered MongooseIM and check the loaded 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(), - % cleanup + check_loaded_config(State), + ok = stop_ejabberd(), stop_remote_ejabberd(SlaveNode), - ok. + check_removed_config(). %% %% Helpers %% -start_ejabberd_with_config(Config, ConfigFile) -> - use_config_file(Config, ConfigFile), - {ok, _} = start_ejabberd(Config). +check_loaded_config(State) -> + Opts = lists:sort(mongoose_config_parser:state_to_opts(State)), + ExpectedOpts = minimal_config_opts(), + ?assertEqual(ExpectedOpts, Opts), + [?assertEqual(Val, mongoose_config:get_opt(Key)) || {{_, Key}, Val} <- ExpectedOpts]. + +check_removed_config() -> + Opts = minimal_config_opts(), + ?assertError(badarg, mongoose_config:config_state()), + [?assertError(badarg, mongoose_config:get_opt(Key)) || {{_, Key}, _} <- Opts]. + +minimal_config_opts() -> + [{local_config, default_server_domain, <<"localhost">>}, + {local_config, hosts, [<<"localhost">>]}]. start_slave_node(Config) -> SlaveNode = do_start_slave_node(), @@ -120,7 +162,7 @@ slave_name() -> 'mim_slave'. start_remote_ejabberd_with_config(RemoteNode, C, ConfigFile) -> - rpc:call(RemoteNode, ?MODULE, start_ejabberd_with_config, [C, ConfigFile]). + rpc:call(RemoteNode, ejabberd_helper, start_ejabberd_with_config, [C, ConfigFile]). stop_remote_ejabberd(SlaveNode) -> rpc:call(SlaveNode, ejabberd_helper, stop_ejabberd, []). @@ -137,3 +179,4 @@ maybe_join_cluster(SlaveNode) -> {already_joined, _} -> ok end. + From b901865dddb68e9d20471fabf55b6f0e61ef9259 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Mon, 25 Oct 2021 16:41:52 +0200 Subject: [PATCH 08/10] Move cluster utilities to mongoose_cluster --- src/admin_extra/service_admin_extra_gdpr.erl | 2 +- src/config/mongoose_config.erl | 21 ++------------------ src/mongoose_cluster.erl | 17 ++++++++++++++++ 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/admin_extra/service_admin_extra_gdpr.erl b/src/admin_extra/service_admin_extra_gdpr.erl index 301a0605ef7..17095968a77 100644 --- a/src/admin_extra/service_admin_extra_gdpr.erl +++ b/src/admin_extra/service_admin_extra_gdpr.erl @@ -121,7 +121,7 @@ retrieve_logs(Username, Domain) -> -spec get_all_logs(gdpr:username(), gdpr:domain(), file:name()) -> [file:name()]. get_all_logs(Username, Domain, TmpDir) -> - OtherNodes = mongoose_config:other_cluster_nodes(), + OtherNodes = mongoose_cluster:other_cluster_nodes(), LogFile = get_logs(Username, Domain, TmpDir), LogFilesFromOtherNodes = [get_logs_from_node(Node, Username, Domain, TmpDir) || Node <- OtherNodes], [LogFile | LogFilesFromOtherNodes]. diff --git a/src/config/mongoose_config.erl b/src/config/mongoose_config.erl index f3501bd0a26..c91b9f2da25 100644 --- a/src/config/mongoose_config.erl +++ b/src/config/mongoose_config.erl @@ -17,11 +17,7 @@ -export([config_state/0, config_states/0]). -%% General-purpose utilities --export([all_cluster_nodes/0, - other_cluster_nodes/0]). - --ignore_xref([config_state/0, config_states/0, all_cluster_nodes/0]). +-ignore_xref([config_state/0, config_states/0]). -include("mongoose.hrl"). -include("ejabberd_config.hrl"). @@ -115,7 +111,7 @@ config_state() -> -spec config_states() -> [mongoose_config_parser:state()]. config_states() -> - config_states(all_cluster_nodes()). + config_states(mongoose_cluster:all_cluster_nodes()). -spec config_states([node()]) -> [mongoose_config_parser:state()]. %% @doc Returns config states from all nodes in cluster @@ -130,16 +126,3 @@ config_states(Nodes) -> cluster_nodes => Nodes, failed_nodes => FailedNodes}) end. - --spec all_cluster_nodes() -> [node()]. -all_cluster_nodes() -> - [node() | other_cluster_nodes()]. - --spec other_cluster_nodes() -> [node()]. -other_cluster_nodes() -> - lists:filter(fun is_mongooseim_node/1, nodes()). - --spec is_mongooseim_node(node()) -> boolean(). -is_mongooseim_node(Node) -> - Apps = rpc:call(Node, application, which_applications, []), - lists:keymember(mongooseim, 1, Apps). diff --git a/src/mongoose_cluster.erl b/src/mongoose_cluster.erl index 68effb091aa..d072d0ed6f2 100644 --- a/src/mongoose_cluster.erl +++ b/src/mongoose_cluster.erl @@ -6,6 +6,10 @@ -export([join/1, leave/0, remove_from_cluster/1, is_node_alive/1]). +-export([all_cluster_nodes/0, other_cluster_nodes/0]). + +-ignore_xref([all_cluster_nodes/0]). + -include("mongoose.hrl"). -dialyzer({[no_match, no_return], set_extra_db_nodes/1}). @@ -63,6 +67,14 @@ do_remove_from_cluster(Node) -> NodeAlive andalso error({node_is_alive, Node}), remove_dead_from_cluster(Node). +-spec all_cluster_nodes() -> [node()]. +all_cluster_nodes() -> + [node() | other_cluster_nodes()]. + +-spec other_cluster_nodes() -> [node()]. +other_cluster_nodes() -> + lists:filter(fun is_mongooseim_node/1, nodes()). + %% %% Helpers %% @@ -87,6 +99,11 @@ is_node_alive(Node) -> false end. +-spec is_mongooseim_node(node()) -> boolean(). +is_mongooseim_node(Node) -> + Apps = rpc:call(Node, application, which_applications, []), + lists:keymember(mongooseim, 1, Apps). + is_app_running(App) -> lists:keymember(App, 1, application:which_applications()). From ec8f1c3f7e2a6d0b631139d6d0d0c4e72dd5f2e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Mon, 25 Oct 2021 16:43:17 +0200 Subject: [PATCH 09/10] Fix time unit --- src/ejabberd_s2s_out.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ejabberd_s2s_out.erl b/src/ejabberd_s2s_out.erl index 4933b9170f0..de756e3a7de 100644 --- a/src/ejabberd_s2s_out.erl +++ b/src/ejabberd_s2s_out.erl @@ -120,7 +120,7 @@ -define(TCP_SEND_TIMEOUT, 15000). %% Maximum delay to wait before retrying to connect after a failed attempt. -%% Specified in milliseconds. Default value is 5 minutes. +%% Specified in seconds. Default value is 5 minutes. -define(MAX_RETRY_DELAY, 300). -define(STREAM_HEADER(From, To, Other), From 397ca5f8b4982e5b72e9751e07e40b144d0f5ade Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Mon, 25 Oct 2021 16:44:28 +0200 Subject: [PATCH 10/10] Simplify sm test case --- big_tests/tests/sm_SUITE.erl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/big_tests/tests/sm_SUITE.erl b/big_tests/tests/sm_SUITE.erl index dd9431d5683..095988221e7 100644 --- a/big_tests/tests/sm_SUITE.erl +++ b/big_tests/tests/sm_SUITE.erl @@ -434,12 +434,10 @@ server_requests_ack(Config, N) -> {Alice, _} = given_fresh_user(Config, alice), %% ack request after initial presence maybe_assert_ack_request(1, N, Alice), - StanzasRec = 1, - ct:print("discarded"), escalus:send(Bob, escalus_stanza:chat_to(Alice, <<"Hi, Alice!">>)), escalus:assert(is_chat_message, [<<"Hi, Alice!">>], escalus:wait_for_stanza(Alice)), - maybe_assert_ack_request(StanzasRec + 1, N, Alice). + maybe_assert_ack_request(2, N, Alice). maybe_assert_ack_request(StanzasRec, AckRequests, Alice) -> ct:print("StanzasRec: ~p, AckRequests: ~p", [StanzasRec, AckRequests]),