Skip to content

Commit

Permalink
Use mod_config_with_auto_backend/1 to reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
arcusfelis committed Jan 15, 2024
1 parent 4e259b1 commit ad6c993
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 76 deletions.
10 changes: 3 additions & 7 deletions big_tests/tests/disco_and_caps_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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())}].
Expand Down
4 changes: 1 addition & 3 deletions big_tests/tests/gdpr_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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() ->
Expand Down
14 changes: 6 additions & 8 deletions big_tests/tests/graphql_offline_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand Down Expand Up @@ -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),
Expand All @@ -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;
Expand Down
13 changes: 6 additions & 7 deletions big_tests/tests/graphql_private_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand Down Expand Up @@ -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),
Expand All @@ -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) ->
Expand Down
12 changes: 6 additions & 6 deletions big_tests/tests/last_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
%%--------------------------------------------------------------------
Expand All @@ -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),
Expand Down Expand Up @@ -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})}].
11 changes: 4 additions & 7 deletions big_tests/tests/mod_blocking_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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).

%%--------------------------------------------------------------------
Expand Down Expand Up @@ -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(),
Expand Down
11 changes: 5 additions & 6 deletions big_tests/tests/mod_event_pusher_http_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 7 additions & 10 deletions big_tests/tests/offline_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down
7 changes: 2 additions & 5 deletions big_tests/tests/pep_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -504,18 +504,15 @@ 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(),
pep_mapping => #{},
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(),
Expand Down
10 changes: 2 additions & 8 deletions big_tests/tests/privacy_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
16 changes: 7 additions & 9 deletions big_tests/tests/private_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
%%--------------------------------------------------------------------
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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}]}].
9 changes: 9 additions & 0 deletions test/common/config_parser_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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) ->
Expand Down

0 comments on commit ad6c993

Please sign in to comment.