Skip to content

Commit

Permalink
extending mongoose_domain_core_SUITE
Browse files Browse the repository at this point in the history
  • Loading branch information
DenysGonchar committed Mar 27, 2021
1 parent b8e04cb commit 86e8fd8
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 40 deletions.
9 changes: 5 additions & 4 deletions src/domain/mongoose_domain_core.erl
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,12 @@ handle_delete(Domain) ->
case is_static(Domain) of
true ->
%% Ignore any static domains
?LOG_ERROR(#{what => domain_static_but_was_in_db, domain => Domain});
?LOG_ERROR(#{what => domain_static_but_was_in_db, domain => Domain}),
{error, static};
false ->
ets:delete(?TABLE, Domain)
end,
ok.
ets:delete(?TABLE, Domain),
ok
end.

handle_insert(Domain, HostType, Source) ->
case is_host_type_allowed(HostType) of
Expand Down
145 changes: 109 additions & 36 deletions test/mongoose_domain_core_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,123 @@

-compile(export_all).

-include_lib("eunit/include/eunit.hrl").

-define(STATIC_PAIRS, [{<<"example.cfg">>, <<"type #1">>},
{<<"erlang-solutions.com">>, <<"type #2">>},
{<<"erlang-solutions.local">>, <<"static type">>}, %% not allowed type
{<<"example.org">>, <<"type #2">>}]).
-define(ALLOWED_TYPES, [<<"type #1">>, <<"type #2">>, <<"type #3">>]).

-define(assertEqualLists(L1, L2), ?assertEqual(lists:sort(L1), lists:sort(L2))).

all() ->
[
core_lookup_works,
core_lookup_not_found,
core_static_domain,
core_cannot_insert_static,
core_get_all_static,
core_get_domains_by_host_type
].

init_per_suite(Config) ->
Pairs = [{<<"example.cfg">>, <<"type1">>},
{<<"erlang-solutions.com">>, <<"type2">>},
{<<"erlang-solutions.local">>, <<"type2">>}],
CommonTypes = [<<"type1">>, <<"type2">>, <<"dbgroup">>, <<"dbgroup2">>, <<"cfggroup">>],
mongoose_domain_core:start(Pairs, CommonTypes),
[can_get_init_arguments,
lookup_works,
double_insert_double_remove_works,
static_domain_check,
cannot_delete_static,
cannot_insert_static_domain,
cannot_insert_if_host_type_not_configured,
get_all_static,
get_domains_by_host_type,
host_type_check,
can_get_outdated_domains].

init_per_testcase(_, Config) ->
mongoose_domain_core:start(?STATIC_PAIRS, ?ALLOWED_TYPES),
Config.

end_per_suite(Config) ->
end_per_testcase(_, Config) ->
mongoose_domain_core:stop(),
Config.

core_lookup_works(_) ->
{ok, <<"type1">>} = mongoose_domain_core:get_host_type(<<"example.cfg">>).
can_get_init_arguments(_) ->
[?STATIC_PAIRS, ?ALLOWED_TYPES] = mongoose_domain_core:get_start_args().

lookup_works(_) ->
{ok, <<"type #1">>} = mongoose_domain_core:get_host_type(<<"example.cfg">>),
{ok, <<"static type">>} = mongoose_domain_core:get_host_type(<<"erlang-solutions.local">>),
{error, not_found} = mongoose_domain_core:get_host_type(<<"some.domain">>),
ok = mongoose_domain_core:insert(<<"some.domain">>, <<"type #3">>, dummy_src),
{ok, <<"type #3">>} = mongoose_domain_core:get_host_type(<<"some.domain">>),
ok = mongoose_domain_core:delete(<<"some.domain">>),
{error, not_found} = mongoose_domain_core:get_host_type(<<"some.domain">>).

core_lookup_not_found(_) ->
{error, not_found} = mongoose_domain_core:get_host_type(<<"example.missing">>).
double_insert_double_remove_works(_) ->
{error, not_found} = mongoose_domain_core:get_host_type(<<"some.domain">>),
ok = mongoose_domain_core:insert(<<"some.domain">>, <<"type #3">>, dummy_src),
ok = mongoose_domain_core:insert(<<"some.domain">>, <<"type #3">>, dummy_src),
{ok, <<"type #3">>} = mongoose_domain_core:get_host_type(<<"some.domain">>),
ok = mongoose_domain_core:delete(<<"some.domain">>),
ok = mongoose_domain_core:delete(<<"some.domain">>),
{error, not_found} = mongoose_domain_core:get_host_type(<<"some.domain">>).

core_static_domain(_) ->
true = mongoose_domain_core:is_static(<<"example.cfg">>).
static_domain_check(_) ->
true = mongoose_domain_core:is_static(<<"example.cfg">>),
false = mongoose_domain_core:is_static(<<"some.domain">>), %% not configured yet
ok = mongoose_domain_core:insert(<<"some.domain">>, <<"type #1">>, dummy_src),
false = mongoose_domain_core:is_static(<<"some.domain">>).

core_cannot_insert_static(_) ->
{error, static} = mongoose_domain_core:insert(<<"example.cfg">>, <<"type1">>, dummy_src).
cannot_delete_static(_) ->
{error, static} = mongoose_domain_core:delete(<<"example.cfg">>),
{error, static} = mongoose_domain_core:delete(<<"erlang-solutions.local">>).

cannot_insert_static_domain(_) ->
{error, static} = mongoose_domain_core:insert(<<"example.cfg">>, <<"type #1">>, dummy_src),
{error, static} = mongoose_domain_core:insert(<<"erlang-solutions.local">>, <<"type #3">>,
dummy_src).

cannot_insert_if_host_type_not_configured(_) ->
{ok, StaticHostType} = mongoose_domain_core:get_host_type(<<"erlang-solutions.local">>),
{error, unknown_host_type} = mongoose_domain_core:insert(<<"erlang-solutions.local">>,
StaticHostType, dummy_src),
{error, unknown_host_type} = mongoose_domain_core:insert(<<"erlang-solutions.local">>,
<<"invalid type">>, dummy_src).

%% See also db_get_all_static
core_get_all_static(_) ->
%% Could be in any order
[{<<"erlang-solutions.com">>, <<"type2">>},
{<<"erlang-solutions.local">>, <<"type2">>},
{<<"example.cfg">>, <<"type1">>}] =
lists:sort(mongoose_domain_core:get_all_static()).

core_get_domains_by_host_type(_) ->
[<<"erlang-solutions.com">>, <<"erlang-solutions.local">>] =
lists:sort(mongoose_domain_core:get_domains_by_host_type(<<"type2">>)),
[<<"example.cfg">>] = mongoose_domain_core:get_domains_by_host_type(<<"type1">>),
[] = mongoose_domain_core:get_domains_by_host_type(<<"type6">>).
get_all_static(_) ->
ok = mongoose_domain_core:insert(<<"some.domain">>, <<"type #1">>, dummy_src),
?assertEqualLists(?STATIC_PAIRS, mongoose_domain_core:get_all_static()).

get_domains_by_host_type(_) ->
?assertEqualLists([<<"erlang-solutions.com">>, <<"example.org">>],
lists:sort(mongoose_domain_core:get_domains_by_host_type(<<"type #2">>))),
[<<"example.cfg">>] = mongoose_domain_core:get_domains_by_host_type(<<"type #1">>),
[<<"erlang-solutions.local">>] = mongoose_domain_core:get_domains_by_host_type(<<"static type">>),
[] = mongoose_domain_core:get_domains_by_host_type(<<"invalid type">>),
%% just no configured domains for this host type
[] = mongoose_domain_core:get_domains_by_host_type(<<"type #3">>),
ok = mongoose_domain_core:insert(<<"some.domain">>, <<"type #3">>, dummy_src),
[<<"some.domain">>] = mongoose_domain_core:get_domains_by_host_type(<<"type #3">>).

host_type_check(_) ->
{ok, StaticHostType} = mongoose_domain_core:get_host_type(<<"erlang-solutions.local">>),
false = mongoose_domain_core:is_host_type_allowed(StaticHostType),
true = mongoose_domain_core:is_host_type_allowed(<<"type #1">>),
true = mongoose_domain_core:is_host_type_allowed(<<"type #3">>),
false = mongoose_domain_core:is_host_type_allowed(<<"invalid_type">>).

can_get_outdated_domains(_) ->
[] = mongoose_domain_core:get_all_outdated(dummy_src),
[] = mongoose_domain_core:get_all_outdated(another_dummy_src),
ok = mongoose_domain_core:insert(<<"some.domain">>, <<"type #3">>, dummy_src),
ok = mongoose_domain_core:insert(<<"another.domain">>, <<"type #3">>, dummy_src),
[] = mongoose_domain_core:get_all_outdated(dummy_src),
?assertEqualLists([{<<"some.domain">>, <<"type #3">>}, {<<"another.domain">>, <<"type #3">>}],
mongoose_domain_core:get_all_outdated(another_dummy_src)),
%% reinserting record with another source
ok = mongoose_domain_core:insert(<<"another.domain">>, <<"type #3">>, another_dummy_src),
[{<<"another.domain">>, <<"type #3">>}] = mongoose_domain_core:get_all_outdated(dummy_src),
[{<<"some.domain">>, <<"type #3">>}] = mongoose_domain_core:get_all_outdated(another_dummy_src),
ok = mongoose_domain_core:insert(<<"some.domain">>, <<"type #3">>, another_dummy_src),
[] = mongoose_domain_core:get_all_outdated(another_dummy_src),
?assertEqualLists([{<<"some.domain">>, <<"type #3">>}, {<<"another.domain">>, <<"type #3">>}],
mongoose_domain_core:get_all_outdated(dummy_src)),
%% try to remove domains
ok = mongoose_domain_core:delete(<<"some.domain">>),
[{<<"another.domain">>, <<"type #3">>}] = mongoose_domain_core:get_all_outdated(dummy_src),
[] = mongoose_domain_core:get_all_outdated(another_dummy_src),
ok = mongoose_domain_core:delete(<<"another.domain">>),
[] = mongoose_domain_core:get_all_outdated(dummy_src),
[] = mongoose_domain_core:get_all_outdated(another_dummy_src).

0 comments on commit 86e8fd8

Please sign in to comment.