Skip to content

Commit

Permalink
Add more unit tests for mongoose_config
Browse files Browse the repository at this point in the history
  • Loading branch information
chrzaszcz committed Oct 21, 2021
1 parent 08d1a5c commit 0848d94
Showing 1 changed file with 47 additions and 10 deletions.
57 changes: 47 additions & 10 deletions test/mongoose_config_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@
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) ->
{ok, _} = application:ensure_all_started(jid),
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.
Expand All @@ -49,21 +53,50 @@ end_per_group(_GroupName, _Config) ->
%% Tests
%%

smoke(Config) ->
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) ->
% when
start_ejabberd_with_config(Config, "mongooseim.minimal.toml"),
% then
?assert(lists:keymember(mongooseim, 1, application:which_applications())),
State = mongoose_config: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],
% 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"),
ok = stop_ejabberd(),
?assertError(badarg, mongoose_config:config_state()),
[?assertError(badarg, mongoose_config:get_opt(Key)) || {{_, Key}, _} <- ExpectedOpts].

cluster_load_from_file(Config) ->
SlaveNode = slave_node(Config),
copy(data(Config, "mongooseim.minimal.toml"), data(Config, "mongooseim.toml")),
{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(),
Opts = lists:sort(mongoose_config_parser:state_to_opts(State)),
ExpectedOpts = minimal_config_opts(),
?assertEqual(ExpectedOpts, Opts),
% cleanup
ok = stop_ejabberd(),
stop_remote_ejabberd(SlaveNode),
Expand Down Expand Up @@ -137,3 +170,7 @@ maybe_join_cluster(SlaveNode) ->
{already_joined, _} ->
ok
end.

minimal_config_opts() ->
[{local_config, default_server_domain, <<"localhost">>},
{local_config, hosts, [<<"localhost">>]}].

0 comments on commit 0848d94

Please sign in to comment.