Skip to content

Commit

Permalink
Use the new mongoose_config API in tests
Browse files Browse the repository at this point in the history
- Set the whole config at once
- Erase the whole config

Also

- Add some options that were not needed only because of incomplete
  cleanup of previous tests
  • Loading branch information
chrzaszcz committed Aug 17, 2023
1 parent 8becf79 commit 6b846c6
Show file tree
Hide file tree
Showing 28 changed files with 177 additions and 193 deletions.
7 changes: 2 additions & 5 deletions test/acl_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ end_per_group(_Group, Config) ->
Config.

init_per_testcase(_TC, Config) ->
mongoose_config:set_opts(#{}),
Config.

end_per_testcase(_TC, _Config) ->
clean_config().
mongoose_config:erase_opts().

host_type() ->
<<"test host type">>.
Expand Down Expand Up @@ -287,10 +288,6 @@ different_specs_matching_the_same_user(Config) ->
acl(Spec) ->
[maps:merge(#{match => current_domain}, Spec)].

clean_config() ->
[persistent_term:erase(Key) || {Key = {mongoose_config, _}, _Value} <- persistent_term:get()],
ok.

given_registered_domains(Config, DomainsList) ->
case proplists:get_value(dynamic_domains, Config, false) of
true ->
Expand Down
8 changes: 4 additions & 4 deletions test/auth_dummy_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ all() -> [

init_per_suite(C) ->
{ok, _} = application:ensure_all_started(jid),
mongoose_config:set_opt({auth, ?HOST_TYPE}, #{methods => [dummy],
dummy => #{base_time => 5,
variance => 10}}),
AuthOpts = #{methods => [dummy],
dummy => #{base_time => 5, variance => 10}},
mongoose_config:set_opts(#{{auth, ?HOST_TYPE} => AuthOpts}),
C.

end_per_suite(_C) ->
mongoose_config:unset_opt({auth, ?HOST_TYPE}).
mongoose_config:erase_opts().

%%--------------------------------------------------------------------
%% Authentication tests
Expand Down
8 changes: 4 additions & 4 deletions test/auth_external_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ given_user_registered() ->

set_opts(Config) ->
DataDir = ?config(data_dir, Config),
mongoose_config:set_opt({auth, ?HOST_TYPE},
#{external => #{program => DataDir ++ "sample_external_auth.py",
instances => 1}}).
mongoose_config:set_opts(#{{auth, ?HOST_TYPE} =>
#{external => #{program => DataDir ++ "sample_external_auth.py",
instances => 1}}}).

unset_opts() ->
mongoose_config:unset_opt({auth, ?HOST_TYPE}).
mongoose_config:erase_opts().

gen_user() ->
U = random_binary(5),
Expand Down
10 changes: 5 additions & 5 deletions test/auth_http_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,13 @@ set_opts(Config) ->
_ -> scram
end,
HttpOpts = #{basic_auth => ?BASIC_AUTH},
mongoose_config:set_opt({auth, ?HOST_TYPE}, #{methods => [http],
password => #{format => PasswordFormat,
scram_iterations => 10},
http => HttpOpts}).
mongoose_config:set_opts(#{{auth, ?HOST_TYPE} => #{methods => [http],
password => #{format => PasswordFormat,
scram_iterations => 10},
http => HttpOpts}}).

unset_opts() ->
mongoose_config:unset_opt({auth, ?HOST_TYPE}).
mongoose_config:erase_opts().

do_scram(Pass, Config) ->
case lists:keyfind(scram_group, 1, Config) of
Expand Down
10 changes: 5 additions & 5 deletions test/auth_internal_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ init_per_suite(C) ->
application:ensure_all_started(jid),
ok = mnesia:create_schema([node()]),
ok = mnesia:start(),
mongoose_config:set_opt({auth, host_type()}, #{methods => [internal],
internal => #{},
password => #{format => scram,
scram_iterations => 10}}),
AuthOpts = #{methods => [internal],
internal => #{},
password => #{format => scram, scram_iterations => 10}},
mongoose_config:set_opts(#{{auth, host_type()} => AuthOpts}),
ejabberd_auth_internal:start(host_type()),
C.

end_per_suite(_C) ->
ejabberd_auth_internal:stop(host_type()),
mongoose_config:unset_opt({auth, host_type()}),
mongoose_config:erase_opts(),
mnesia:stop(),
mnesia:delete_schema([node()]).

Expand Down
8 changes: 4 additions & 4 deletions test/auth_jwt_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ check_password_succeeds_for_pubkey_signed_token(C) ->
%%--------------------------------------------------------------------

set_auth_opts(Secret, Algorithm, Key) ->
mongoose_config:set_opt({auth, ?HOST_TYPE}, #{jwt => #{secret => Secret,
algorithm => Algorithm,
username_key => Key}}).
mongoose_config:set_opts(#{{auth, ?HOST_TYPE} => #{jwt => #{secret => Secret,
algorithm => Algorithm,
username_key => Key}}}).

unset_auth_opts() ->
mongoose_config:unset_opt({auth, ?HOST_TYPE}).
mongoose_config:erase_opts().

generate_token(Alg, NbfDelta, Key) ->
Now = erlang:system_time(second),
Expand Down
11 changes: 6 additions & 5 deletions test/auth_tokens_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ groups() ->

init_per_suite(C) ->
{ok, _} = application:ensure_all_started(jid),
mongoose_config:set_opt({modules, host_type()},
#{?TESTED => config_parser_helper:default_mod_config(?TESTED)}),
mongoose_config:set_opts(opts()),
C.

end_per_suite(C) ->
mongoose_config:unset_opt({modules, host_type()}),
C.
end_per_suite(_C) ->
mongoose_config:erase_opts().

opts() ->
#{{modules, host_type()} => #{?TESTED => config_parser_helper:default_mod_config(?TESTED)}}.

init_per_testcase(Test, Config)
when Test =:= serialize_deserialize_property;
Expand Down
11 changes: 5 additions & 6 deletions test/component_reg_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ init_per_suite(C) ->
{ok, _} = application:ensure_all_started(jid),
ok = mnesia:create_schema([node()]),
ok = mnesia:start(),
[mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts()],
mongoose_config:set_opts(opts()),
meck:new(mongoose_domain_api, [no_link]),
meck:expect(mongoose_domain_api, get_host_type,
fun(_) -> {error, not_found} end),
Expand All @@ -31,13 +31,12 @@ end_per_suite(_C) ->
mnesia:stop(),
mnesia:delete_schema([node()]),
meck:unload(),
[mongoose_config:unset_opt(Key) || {Key, _Value} <- opts()],
ok.
mongoose_config:erase_opts().

opts() ->
[{all_metrics_are_global, false},
{component_backend, mnesia},
{routing_modules, [xmpp_router_a, xmpp_router_b, xmpp_router_c]}].
#{all_metrics_are_global => false,
component_backend => mnesia,
routing_modules => [xmpp_router_a, xmpp_router_b, xmpp_router_c]}.

registering(_C) ->
Dom = <<"aaa.bbb.com">>,
Expand Down
3 changes: 1 addition & 2 deletions test/config_parser_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3040,8 +3040,7 @@ test_config_file(Config, File) ->
ExpectedOpts = config_parser_helper:options(File),

TOMLPath = ejabberd_helper:data(Config, File ++ ".toml"),
State = mongoose_config_parser:parse_file(TOMLPath),
TOMLOpts = mongoose_config_parser:get_opts(State),
TOMLOpts = mongoose_config_parser:parse_file(TOMLPath),

%% Save the parsed TOML options
%% - for debugging
Expand Down
18 changes: 6 additions & 12 deletions test/ejabberd_sm_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ end_per_testcase(_, Config) ->
clean_sessions(Config),
terminate_sm(),
unload_meck(),
unset_opts(Config).
mongoose_config:erase_opts().

open_session(C) ->
{Sid, USR} = generate_random_user(<<"localhost">>),
Expand Down Expand Up @@ -610,7 +610,7 @@ is_redis_running() ->
end.

setup_sm(Config) ->
set_opts(Config),
mongoose_config:set_opts(opts(Config)),
set_meck(),
ejabberd_sm:start_link(),
case ?config(backend, Config) of
Expand All @@ -625,17 +625,11 @@ setup_sm(Config) ->
terminate_sm() ->
gen_server:stop(ejabberd_sm).

set_opts(Config) ->
[mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts(Config)].

unset_opts(Config) ->
[mongoose_config:unset_opt(Key) || {Key, _Value} <- opts(Config)].

opts(Config) ->
[{hosts, [<<"localhost">>]},
{host_types, []},
{all_metrics_are_global, false},
{sm_backend, sm_backend(?config(backend, Config))}].
#{hosts => [<<"localhost">>],
host_types => [],
all_metrics_are_global => false,
sm_backend => sm_backend(?config(backend, Config))}.

sm_backend(ejabberd_sm_redis) -> redis;
sm_backend(ejabberd_sm_mnesia) -> mnesia;
Expand Down
12 changes: 6 additions & 6 deletions test/event_pusher_sns_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,18 @@ sns_config(_) ->
common_sns_opts().

start_modules(SNSExtra) ->
[mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts(SNSExtra)],
mongoose_config:set_opts(opts(SNSExtra)),
mongoose_modules:start().

stop_modules() ->
mongoose_modules:stop(),
[mongoose_config:unset_opt(Key) || {Key, _} <- opts(#{})].
mongoose_config:erase_opts().

opts(SNSExtra) ->
[{hosts, [host_type()]},
{host_types, []},
{all_metrics_are_global, false},
{{modules, host_type()}, modules(SNSExtra)}].
#{hosts => [host_type()],
host_types => [],
all_metrics_are_global => false,
{modules, host_type()} => modules(SNSExtra)}.

modules(SNSExtra) ->
gen_mod_deps:resolve_deps(host_type(), #{mod_event_pusher => module_opts(SNSExtra)}).
Expand Down
4 changes: 2 additions & 2 deletions test/gen_hook_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ all() ->

init_per_suite(Config) ->
application:ensure_all_started(exometer_core),
mongoose_config:set_opt(all_metrics_are_global, false),
mongoose_config:set_opts(#{all_metrics_are_global => false}),
Config.

end_per_suite(Config) ->
mongoose_config:unset_opt(all_metrics_are_global),
mongoose_config:erase_opts(),
application:stop(exometer_core),
Config.

Expand Down
14 changes: 7 additions & 7 deletions test/gen_mod_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ all() ->
hosts_and_opts_with_module].

init_per_testcase(_, Config) ->
[mongoose_config:set_opt(Opt, Val) || {Opt, Val} <- opts()],
mongoose_config:set_opts(opts()),
[setup_meck(Module) || Module <- [a_module, b_module]],
Config.

end_per_testcase(_, Config) ->
[mongoose_config:unset_opt(Opt) || Opt <- opts()],
mongoose_config:erase_opts(),
[meck:unload(Module) || Module <- [a_module, b_module]],
Config.

Expand Down Expand Up @@ -113,8 +113,8 @@ setup_meck(Module) ->
meck:expect(Module, stop, fun(_) -> ok end).

opts() ->
[{hosts, [host(a), host(b)]},
{host_types, []},
{services, #{}},
{{modules, host(a)}, #{a_module => #{}}},
{{modules, host(b)}, #{b_module => #{k => v}}}].
#{hosts => [host(a), host(b)],
host_types => [],
services => #{},
{modules, host(a)} => #{a_module => #{}},
{modules, host(b)} => #{b_module => #{k => v}}}.
12 changes: 6 additions & 6 deletions test/keystore_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ end_per_suite(_C) ->
init_per_testcase(_, Config) ->
mock_mongoose_metrics(),
Config1 = async_helper:start(Config, gen_hook, start_link, []),
[mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts()],
mongoose_config:set_opts(opts()),
Config1.

end_per_testcase(_, C) ->
mongoose_modules:stop(),
[mongoose_config:unset_opt(Key) || {Key, _Value} <- opts()],
mongoose_config:erase_opts(),
meck:unload(mongoose_metrics),
async_helper:stop_all(C),
mnesia:delete_table(key).

opts() ->
[{hosts, hosts()},
{host_types, []},
{all_metrics_are_global, false}]
++ [{{modules, Host}, #{}} || Host <- hosts()].
maps:from_list([{hosts, hosts()},
{host_types, []},
{all_metrics_are_global, false} |
[{{modules, Host}, #{}} || Host <- hosts()]]).

hosts() ->
[<<"localhost">>, <<"first.com">>, <<"second.com">>].
Expand Down
12 changes: 6 additions & 6 deletions test/mod_global_distrib_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ end_per_group(_GroupName, Config) ->

init_per_testcase(_CaseName, Config) ->
set_meck(),
[mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts()],
mongoose_config:set_opts(opts()),
mongoose_domain_sup:start_link(),
mim_ct_sup:start_link(ejabberd_sup),
gen_hook:start_link(),
Expand All @@ -74,15 +74,15 @@ init_per_testcase(_CaseName, Config) ->

end_per_testcase(_CaseName, Config) ->
mongoose_modules:stop(),
[mongoose_config:unset_opt(Key) || {Key, _Value} <- opts()],
mongoose_config:erase_opts(),
unset_meck(),
Config.

opts() ->
[{hosts, hosts()},
{host_types, []},
{all_metrics_are_global, false} |
[{{modules, HostType}, modules(HostType)} || HostType <- hosts()]].
maps:from_list([{hosts, hosts()},
{host_types, []},
{all_metrics_are_global, false} |
[{{modules, HostType}, modules(HostType)} || HostType <- hosts()]]).

hosts() ->
[global_host(), local_host()].
Expand Down
3 changes: 2 additions & 1 deletion test/mod_websockets_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ setup() ->
fun(mongoose_listener_sup, _) -> {ok, self()};
(A, B) -> meck:passthrough([A, B])
end),
mongoose_config:set_opts(#{default_server_name => <<"localhost">>}),
%% Start websocket cowboy listening

Handlers = [config([listen, http, handlers, mod_bosh],
#{host => '_', path => "/http-bind"}),
config([listen, http, handlers, mod_websockets],
Expand All @@ -70,6 +70,7 @@ setup() ->
teardown() ->
meck:unload(),
cowboy:stop_listener(ejabberd_cowboy:ref({?PORT, ?IP, tcp})),
mongoose_config:erase_opts(),
application:stop(cowboy),
%% Do not stop jid, Erlang 21 does not like to reload nifs
ok.
Expand Down
14 changes: 7 additions & 7 deletions test/mongoose_cleanup_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ init_per_suite(Config) ->
{ok, _} = application:ensure_all_started(jid),
ok = mnesia:create_schema([node()]),
ok = mnesia:start(),
[mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts()],
mongoose_config:set_opts(opts()),
Config.

end_per_suite(Config) ->
[mongoose_config:unset_opt(Key) || {Key, _Value} <- opts()],
mongoose_config:erase_opts(),
mnesia:stop(),
mnesia:delete_schema([node()]),
Config.
Expand Down Expand Up @@ -116,11 +116,11 @@ needs_component(TestCase) ->
lists:member(TestCase, component_cases()).

opts() ->
[{hosts, [?HOST]},
{host_types, []},
{all_metrics_are_global, false},
{s2s_backend, mnesia},
{{modules, ?HOST}, #{}}].
#{hosts => [?HOST],
host_types => [],
all_metrics_are_global => false,
s2s_backend => mnesia,
{modules, ?HOST} => #{}}.

meck_mods(bosh) -> [exometer, mod_bosh_socket];
meck_mods(s2s) -> [exometer, ejabberd_commands, mongoose_bin];
Expand Down
Loading

0 comments on commit 6b846c6

Please sign in to comment.