From ad6c993bae7d83e19eefe1ad520546a321460dcc Mon Sep 17 00:00:00 2001 From: Mikhail Uvarov Date: Mon, 15 Jan 2024 15:15:09 +0100 Subject: [PATCH] Use mod_config_with_auto_backend/1 to reduce code duplication --- big_tests/tests/disco_and_caps_SUITE.erl | 10 +++------- big_tests/tests/gdpr_SUITE.erl | 4 +--- big_tests/tests/graphql_offline_SUITE.erl | 14 ++++++-------- big_tests/tests/graphql_private_SUITE.erl | 13 ++++++------- big_tests/tests/last_SUITE.erl | 12 ++++++------ big_tests/tests/mod_blocking_SUITE.erl | 11 ++++------- big_tests/tests/mod_event_pusher_http_SUITE.erl | 11 +++++------ big_tests/tests/offline_SUITE.erl | 17 +++++++---------- big_tests/tests/pep_SUITE.erl | 7 ++----- big_tests/tests/privacy_SUITE.erl | 10 ++-------- big_tests/tests/private_SUITE.erl | 16 +++++++--------- test/common/config_parser_helper.erl | 9 +++++++++ 12 files changed, 58 insertions(+), 76 deletions(-) diff --git a/big_tests/tests/disco_and_caps_SUITE.erl b/big_tests/tests/disco_and_caps_SUITE.erl index 766b59602f9..731668e1a0c 100644 --- a/big_tests/tests/disco_and_caps_SUITE.erl +++ b/big_tests/tests/disco_and_caps_SUITE.erl @@ -5,7 +5,7 @@ -include_lib("escalus/include/escalus_xmlns.hrl"). -import(domain_helper, [host_type/0, domain/0]). --import(config_parser_helper, [default_mod_config/1, mod_config/2]). +-import(config_parser_helper, [default_mod_config/1, mod_config/2, mod_config_with_auto_backend/1]). all() -> [{group, disco_with_caps}, @@ -196,14 +196,10 @@ user_can_query_server_info(Config) -> %% Helpers required_modules(disco_with_caps) -> - HostType = domain_helper:host_type(), - Backend = mongoose_helper:get_backend_mnesia_rdbms(HostType), - [{mod_caps, config_parser_helper:mod_config(mod_caps, #{backend => Backend})}, + [{mod_caps, mod_config_with_auto_backend(mod_caps)}, {mod_disco, default_mod_config(mod_disco)}]; required_modules(disco_with_caps_and_extra_features) -> - HostType = domain_helper:host_type(), - Backend = mongoose_helper:get_backend_mnesia_rdbms(HostType), - [{mod_caps, config_parser_helper:mod_config(mod_caps, #{backend => Backend})}, + [{mod_caps, mod_config_with_auto_backend(mod_caps)}, {mod_disco, mod_config(mod_disco, extra_disco_opts())}]; required_modules(disco_with_extra_features) -> [{mod_disco, mod_config(mod_disco, extra_disco_opts())}]. diff --git a/big_tests/tests/gdpr_SUITE.erl b/big_tests/tests/gdpr_SUITE.erl index 9601a51311e..3794e4db724 100644 --- a/big_tests/tests/gdpr_SUITE.erl +++ b/big_tests/tests/gdpr_SUITE.erl @@ -416,9 +416,7 @@ pubsub_required_modules(Plugins) -> host => HostPattern, nodetree => nodetree_tree, plugins => Plugins}), - HostType = domain_helper:host_type(), - Backend = mongoose_helper:get_backend_mnesia_rdbms(HostType), - [{mod_caps, config_parser_helper:mod_config(mod_caps, #{backend => Backend})}, + [{mod_caps, config_parser_helper:mod_config_with_auto_backend(mod_caps)}, {mod_pubsub, PubsubConfig}]. is_mim2_started() -> diff --git a/big_tests/tests/graphql_offline_SUITE.erl b/big_tests/tests/graphql_offline_SUITE.erl index 6100a704651..388f1db7e84 100644 --- a/big_tests/tests/graphql_offline_SUITE.erl +++ b/big_tests/tests/graphql_offline_SUITE.erl @@ -7,7 +7,7 @@ -import(domain_helper, [host_type/0, domain/0]). -import(graphql_helper, [execute_command/4, get_ok_value/2, get_err_code/1, user_to_bin/1, get_unauthorized/1, get_not_loaded/1, get_coercion_err_msg/1]). --import(config_parser_helper, [mod_config/2]). +-import(config_parser_helper, [mod_config_with_auto_backend/1]). -import(mongooseimctl_helper, [mongooseimctl/3, rpc_call/3]). -include_lib("eunit/include/eunit.hrl"). @@ -70,9 +70,9 @@ init_per_suite(Config) -> Config2 = ejabberd_node_utils:init(mim(), Config1), escalus:init_per_suite(Config2). --spec create_config(atom()) -> [{mod_offline, gen_mod:module_opts()}]. -create_config(Backend) -> - [{mod_offline, mod_config(mod_offline, #{backend => Backend})}]. +-spec create_config() -> [{mod_offline, gen_mod:module_opts()}]. +create_config() -> + [{mod_offline, mod_config_with_auto_backend(mod_offline)}]. end_per_suite(Config) -> dynamic_modules:restore_modules(Config), @@ -87,10 +87,8 @@ init_per_group(domain_admin, Config) -> init_per_group(GroupName, Config) when GroupName =:= admin_offline; GroupName =:= domain_admin_offline -> HostType = host_type(), - Backend = mongoose_helper:get_backend_mnesia_rdbms(HostType), - ModConfig = create_config(Backend), - dynamic_modules:ensure_modules(HostType, ModConfig), - [{backend, Backend} | escalus:init_per_suite(Config)]; + dynamic_modules:ensure_modules(HostType, create_config()), + escalus:init_per_suite(Config); init_per_group(admin_offline_not_configured, Config) -> dynamic_modules:ensure_modules(host_type(), [{mod_offline, stopped}]), Config; diff --git a/big_tests/tests/graphql_private_SUITE.erl b/big_tests/tests/graphql_private_SUITE.erl index c956e68f27d..dfcf551fb7b 100644 --- a/big_tests/tests/graphql_private_SUITE.erl +++ b/big_tests/tests/graphql_private_SUITE.erl @@ -5,6 +5,7 @@ -import(distributed_helper, [mim/0, require_rpc_nodes/1]). -import(graphql_helper, [execute_user_command/5, execute_command/4, get_ok_value/2, get_err_code/1, user_to_bin/1, get_unauthorized/1, get_not_loaded/1, get_coercion_err_msg/1]). +-import(config_parser_helper, [mod_config_with_auto_backend/2]). -include_lib("eunit/include/eunit.hrl"). -include_lib("exml/include/exml.hrl"). @@ -67,11 +68,11 @@ init_per_suite(Config0) -> HostType = domain_helper:host_type(), Config1 = dynamic_modules:save_modules(HostType, Config0), Config2 = ejabberd_node_utils:init(mim(), Config1), - Backend = mongoose_helper:get_backend_mnesia_rdbms(HostType), - escalus:init_per_suite([{backend, Backend} | Config2]). + escalus:init_per_suite(Config2). -create_config(Backend) -> - [{mod_private, #{backend => Backend, iqdisc => one_queue}}]. +create_config() -> + [{mod_private, + mod_config_with_auto_backend(mod_private, #{iqdisc => one_queue})}]. end_per_suite(Config) -> dynamic_modules:restore_modules(Config), @@ -95,9 +96,7 @@ init_per_group(Group, Config) when Group =:= admin_private_not_configured; ensure_private_started(Config) -> HostType = domain_helper:host_type(), - Backend = mongoose_helper:get_backend_mnesia_rdbms(HostType), - ModConfig = create_config(Backend), - dynamic_modules:ensure_modules(HostType, ModConfig), + dynamic_modules:ensure_modules(HostType, create_config()), Config. ensure_private_stopped(Config) -> diff --git a/big_tests/tests/last_SUITE.erl b/big_tests/tests/last_SUITE.erl index a762ea05fc9..55487fea8c7 100644 --- a/big_tests/tests/last_SUITE.erl +++ b/big_tests/tests/last_SUITE.erl @@ -20,6 +20,8 @@ -include_lib("escalus/include/escalus_xmlns.hrl"). -include_lib("exml/include/exml.hrl"). +-import(config_parser_helper, [mod_config_with_auto_backend/2]). + %%-------------------------------------------------------------------- %% Suite configuration %%-------------------------------------------------------------------- @@ -45,9 +47,8 @@ suite() -> init_per_suite(Config0) -> HostType = domain_helper:host_type(), Config1 = dynamic_modules:save_modules(HostType, Config0), - Backend = mongoose_helper:get_backend_mnesia_rdbms(HostType), - dynamic_modules:ensure_modules(HostType, required_modules(Backend)), - escalus:init_per_suite([{backend, Backend} | Config1]). + dynamic_modules:ensure_modules(HostType, required_modules()), + escalus:init_per_suite(Config1). end_per_suite(Config) -> dynamic_modules:restore_modules(Config), @@ -190,6 +191,5 @@ answer_last_activity(IQ = #xmlel{name = <<"iq">>}) -> {<<"seconds">>, <<"0">>}]} ]}. -required_modules(Backend) -> - [{mod_last, #{backend => Backend, - iqdisc => one_queue}}]. +required_modules() -> + [{mod_last, mod_config_with_auto_backend(mod_last, #{iqdisc => one_queue})}]. diff --git a/big_tests/tests/mod_blocking_SUITE.erl b/big_tests/tests/mod_blocking_SUITE.erl index d0f2268c175..94b0a9da55f 100644 --- a/big_tests/tests/mod_blocking_SUITE.erl +++ b/big_tests/tests/mod_blocking_SUITE.erl @@ -23,6 +23,8 @@ -include_lib("eunit/include/eunit.hrl"). -include_lib("escalus/include/escalus_xmlns.hrl"). +-import(config_parser_helper, [mod_config_with_auto_backend/1]). + -define(SLEEP_TIME, 50). %%-------------------------------------------------------------------- @@ -99,14 +101,9 @@ suite() -> init_per_suite(Config0) -> HostType = domain_helper:host_type(), Config1 = dynamic_modules:save_modules(HostType, Config0), - Backend = mongoose_helper:get_backend_mnesia_rdbms(HostType), - ModConfig = [{mod_blocking, set_opts(Backend)}], + ModConfig = [{mod_blocking, mod_config_with_auto_backend(mod_blocking)}], dynamic_modules:ensure_modules(HostType, ModConfig), - [{backend, Backend} | - escalus:init_per_suite(Config1)]. - -set_opts(Backend) -> - #{backend => Backend}. + escalus:init_per_suite(Config1). end_per_suite(Config) -> escalus_fresh:clean(), diff --git a/big_tests/tests/mod_event_pusher_http_SUITE.erl b/big_tests/tests/mod_event_pusher_http_SUITE.erl index ff8e7f09d69..a0fffa5d655 100644 --- a/big_tests/tests/mod_event_pusher_http_SUITE.erl +++ b/big_tests/tests/mod_event_pusher_http_SUITE.erl @@ -22,7 +22,7 @@ -import(domain_helper, [host_type/0]). --import(config_parser_helper, [config/2, mod_config/2, mod_event_pusher_http_handler/0]). +-import(config_parser_helper, [config/2, mod_config_with_auto_backend/1, mod_event_pusher_http_handler/0]). %%%=================================================================== %%% Suite configuration @@ -178,16 +178,15 @@ stop_pool() -> set_modules(Config0, ExtraHandlerOpts) -> Config = dynamic_modules:save_modules(host_type(), Config0), - Backend = mongoose_helper:get_backend_mnesia_rdbms(host_type()), - ModOffline = create_offline_config(Backend), + ModOffline = create_offline_config(), Handler = maps:merge(mod_event_pusher_http_handler(), ExtraHandlerOpts), ModOpts = #{http => #{handlers => [Handler]}}, dynamic_modules:ensure_modules(host_type(), [{mod_event_pusher, ModOpts} | ModOffline]), Config. --spec create_offline_config(atom()) -> [{mod_offline, gen_mod:module_opts()}]. -create_offline_config(Backend) -> - [{mod_offline, mod_config(mod_offline, #{backend => Backend})}]. +-spec create_offline_config() -> [{mod_offline, gen_mod:module_opts()}]. +create_offline_config() -> + [{mod_offline, mod_config_with_auto_backend(mod_offline)}]. start_http_listener(simple_message, Prefix) -> http_helper:start(http_notifications_port(), Prefix, fun process_notification/1); diff --git a/big_tests/tests/offline_SUITE.erl b/big_tests/tests/offline_SUITE.erl index 7f40b71d11f..b7e4d8bae5c 100644 --- a/big_tests/tests/offline_SUITE.erl +++ b/big_tests/tests/offline_SUITE.erl @@ -19,7 +19,7 @@ -import(domain_helper, [host_type/0]). -import(mongoose_helper, [wait_for_n_offline_messages/2]). --import(config_parser_helper, [mod_config/2]). +-import(config_parser_helper, [mod_config/2, mod_config_with_auto_backend/1]). %%%=================================================================== %%% Suite configuration @@ -60,15 +60,12 @@ suite() -> init_per_suite(Config0) -> HostType = domain_helper:host_type(), Config1 = dynamic_modules:save_modules(HostType, Config0), - Backend = mongoose_helper:get_backend_mnesia_rdbms(HostType), - ModConfig = create_config(Backend), - dynamic_modules:ensure_modules(HostType, ModConfig), - [{backend, Backend} | - escalus:init_per_suite(Config1)]. - --spec create_config(atom()) -> [{mod_offline, gen_mod:module_opts()}]. -create_config(Backend) -> - [{mod_offline, mod_config(mod_offline, #{backend => Backend})}]. + dynamic_modules:ensure_modules(HostType, create_config()), + escalus:init_per_suite(Config1). + +-spec create_config() -> [{mod_offline, gen_mod:module_opts()}]. +create_config() -> + [{mod_offline, mod_config_with_auto_backend(mod_offline)}]. end_per_suite(Config) -> escalus_fresh:clean(), diff --git a/big_tests/tests/pep_SUITE.erl b/big_tests/tests/pep_SUITE.erl index a79f6354902..afab8d177a8 100644 --- a/big_tests/tests/pep_SUITE.erl +++ b/big_tests/tests/pep_SUITE.erl @@ -504,9 +504,7 @@ field_spec({Var, Value}) when is_list(Value) -> #{var => Var, values => Value}; field_spec({Var, Value}) -> #{var => Var, values => [Value]}. required_modules() -> - HostType = domain_helper:host_type(), - Backend = mongoose_helper:get_backend_mnesia_rdbms(HostType), - [{mod_caps, config_parser_helper:mod_config(mod_caps, #{backend => Backend})}, + [{mod_caps, config_parser_helper:mod_config_with_auto_backend(mod_caps)}, {mod_pubsub, mod_config(mod_pubsub, #{plugins => [<<"dag">>, <<"pep">>], nodetree => nodetree_dag, backend => mongoose_helper:mnesia_or_rdbms_backend(), @@ -514,8 +512,7 @@ required_modules() -> host => subhost_pattern("pubsub.@HOST@")})}]. required_modules(cache_tests) -> HostType = domain_helper:host_type(), - Backend = mongoose_helper:get_backend_mnesia_rdbms(HostType), - [{mod_caps, config_parser_helper:mod_config(mod_caps, #{backend => Backend})}, + [{mod_caps, config_parser_helper:mod_config_with_auto_backend(mod_caps)}, {mod_pubsub, mod_config(mod_pubsub, #{plugins => [<<"dag">>, <<"pep">>], nodetree => nodetree_dag, backend => mongoose_helper:mnesia_or_rdbms_backend(), diff --git a/big_tests/tests/privacy_SUITE.erl b/big_tests/tests/privacy_SUITE.erl index 69bd83f9910..188c8c5482f 100644 --- a/big_tests/tests/privacy_SUITE.erl +++ b/big_tests/tests/privacy_SUITE.erl @@ -93,15 +93,9 @@ suite() -> init_per_suite(Config0) -> HostType = domain_helper:host_type(), Config1 = dynamic_modules:save_modules(HostType, Config0), - Backend = mongoose_helper:get_backend_mnesia_rdbms(HostType), - ModConfig = [{mod_privacy, set_opts(Backend)}], + ModConfig = [{mod_privacy, config_parser_helper:mod_config_with_auto_backend(mod_privacy)}], dynamic_modules:ensure_modules(HostType, ModConfig), - [{escalus_no_stanzas_after_story, true}, - {backend, Backend} | - escalus:init_per_suite(Config1)]. - -set_opts(Backend) -> - config_parser_helper:mod_config(mod_privacy, #{backend => Backend}). + [{escalus_no_stanzas_after_story, true} | escalus:init_per_suite(Config1)]. end_per_suite(Config) -> escalus_fresh:clean(), diff --git a/big_tests/tests/private_SUITE.erl b/big_tests/tests/private_SUITE.erl index 6260675cff7..95927a17eb6 100644 --- a/big_tests/tests/private_SUITE.erl +++ b/big_tests/tests/private_SUITE.erl @@ -20,6 +20,8 @@ -include_lib("escalus/include/escalus.hrl"). -include_lib("common_test/include/ct.hrl"). +-import(config_parser_helper, [mod_config_with_auto_backend/2]). + %%-------------------------------------------------------------------- %% Suite configuration %%-------------------------------------------------------------------- @@ -47,13 +49,12 @@ suite() -> init_per_suite(Config0) -> HostType = domain_helper:host_type(), Config1 = dynamic_modules:save_modules(HostType, Config0), - Backend = mongoose_helper:get_backend_mnesia_rdbms(HostType), - ModConfig = create_config(Backend), - dynamic_modules:ensure_modules(HostType, ModConfig), - escalus:init_per_suite([{backend, Backend} | Config1]). + dynamic_modules:ensure_modules(HostType, create_config()), + escalus:init_per_suite(Config1). -create_config(Backend) -> - [{mod_private, #{backend => Backend, iqdisc => one_queue}}]. +create_config() -> + [{mod_private, + mod_config_with_auto_backend(mod_private, #{iqdisc => one_queue})}]. end_per_suite(Config) -> dynamic_modules:restore_modules(Config), @@ -175,6 +176,3 @@ check_body_rec(Element, [Name | Names]) -> [Child] = Element#xmlel.children, Name = Child#xmlel.name, check_body_rec(Child, Names). - -required_modules(Backend) -> - [{mod_private, [{backend, Backend}]}]. diff --git a/test/common/config_parser_helper.erl b/test/common/config_parser_helper.erl index 10650a9e30b..f63a2b72627 100644 --- a/test/common/config_parser_helper.erl +++ b/test/common/config_parser_helper.erl @@ -822,6 +822,15 @@ default_pool_conn_opts(_Type) -> mod_config(Module, ExtraOpts) -> maps:merge(default_mod_config(Module), ExtraOpts). +%% Selects backend automatically depending on which databases are available +mod_config_with_auto_backend(Module) -> + mod_config_with_auto_backend(Module, #{}). + +mod_config_with_auto_backend(Module, ExtraOpts) -> + HostType = domain_helper:host_type(), + Backend = mongoose_helper:get_backend_mnesia_rdbms(HostType), + mod_config(Module, ExtraOpts#{backend => Backend}). + default_mod_config(mod_adhoc) -> #{iqdisc => one_queue, report_commands_node => false}; default_mod_config(mod_auth_token) ->