chore(gw): add cases for emqx_gateway_conf

This commit is contained in:
JianBo He 2021-09-17 19:03:37 +08:00
parent f68dfff0d6
commit d163e99d58
3 changed files with 64 additions and 16 deletions

View File

@ -27,9 +27,11 @@ start(_StartType, _StartArgs) ->
emqx_gateway_cli:load(), emqx_gateway_cli:load(),
load_default_gateway_applications(), load_default_gateway_applications(),
load_gateway_by_default(), load_gateway_by_default(),
emqx_gateway_conf:load(),
{ok, Sup}. {ok, Sup}.
stop(_State) -> stop(_State) ->
emqx_gateway_conf:unload(),
emqx_gateway_cli:unload(), emqx_gateway_cli:unload(),
ok. ok.

View File

@ -132,7 +132,7 @@ pre_config_update({load_gateway, GwName, Conf}, RawConf) ->
undefined -> undefined ->
{ok, emqx_map_lib:deep_merge(RawConf, #{GwName => Conf})}; {ok, emqx_map_lib:deep_merge(RawConf, #{GwName => Conf})};
_ -> _ ->
{error, alredy_exist} {error, already_exist}
end; end;
pre_config_update({update_gateway, GwName, Conf}, RawConf) -> pre_config_update({update_gateway, GwName, Conf}, RawConf) ->
case maps:get(GwName, RawConf, undefined) of case maps:get(GwName, RawConf, undefined) of
@ -155,7 +155,7 @@ pre_config_update({add_listener, GwName, {LType, LName}, Conf}, RawConf) ->
RawConf, RawConf,
#{GwName => #{<<"listeners">> => NListener}})}; #{GwName => #{<<"listeners">> => NListener}})};
_ -> _ ->
{error, alredy_exist} {error, already_exist}
end; end;
pre_config_update({update_listener, GwName, {LType, LName}, Conf}, RawConf) -> pre_config_update({update_listener, GwName, {LType, LName}, Conf}, RawConf) ->
case emqx_map_lib:deep_get( case emqx_map_lib:deep_get(
@ -181,7 +181,7 @@ pre_config_update({add_authn, GwName, Conf}, RawConf) ->
RawConf, RawConf,
#{GwName => #{<<"authentication">> => Conf}})}; #{GwName => #{<<"authentication">> => Conf}})};
_ -> _ ->
{error, alredy_exist} {error, already_exist}
end; end;
pre_config_update({add_authn, GwName, {LType, LName}, Conf}, RawConf) -> pre_config_update({add_authn, GwName, {LType, LName}, Conf}, RawConf) ->
case emqx_map_lib:deep_get( case emqx_map_lib:deep_get(
@ -198,7 +198,7 @@ pre_config_update({add_authn, GwName, {LType, LName}, Conf}, RawConf) ->
#{LType => #{LName => NListener}}}}, #{LType => #{LName => NListener}}}},
{ok, emqx_map_lib:deep_merge(RawConf, NGateway)}; {ok, emqx_map_lib:deep_merge(RawConf, NGateway)};
_ -> _ ->
{error, alredy_exist} {error, already_exist}
end end
end; end;
pre_config_update({update_authn, GwName, Conf}, RawConf) -> pre_config_update({update_authn, GwName, Conf}, RawConf) ->
@ -251,10 +251,15 @@ pre_config_update(UnknownReq, _RawConf) ->
post_config_update(Req, NewConfig, OldConfig, _AppEnvs) -> post_config_update(Req, NewConfig, OldConfig, _AppEnvs) ->
[_Tag, GwName0|_] = tuple_to_list(Req), [_Tag, GwName0|_] = tuple_to_list(Req),
GwName = binary_to_existing_atom(GwName0), GwName = binary_to_existing_atom(GwName0),
SubConf = maps:get(GwName, NewConfig),
case maps:get(GwName, OldConfig, undefined) of case {maps:get(GwName, NewConfig, undefined),
undefined -> maps:get(GwName, OldConfig, undefined)} of
emqx_gateway:load(GwName, SubConf); {undefined, undefined} ->
_ -> ok; %% nothing to change
emqx_gateway:update(GwName, SubConf) {undefined, Old} when is_map(Old) ->
emqx_gateway:unload(GwName);
{New, undefined} when is_map(New) ->
emqx_gateway:load(GwName, New);
{New, Old} when is_map(New), is_map(Old) ->
emqx_gateway:update(GwName, New)
end. end.

View File

@ -29,15 +29,17 @@ all() ->
emqx_ct:all(?MODULE). emqx_ct:all(?MODULE).
init_per_suite(Conf) -> init_per_suite(Conf) ->
emqx_ct_helpers:start_apps([]), emqx_ct_helpers:start_apps([emqx_gateway]),
Conf. Conf.
end_per_suite(_Conf) -> end_per_suite(_Conf) ->
emqx_ct_helpers:stop_apps([]). emqx_ct_helpers:stop_apps([emqx_gateway]).
init_per_testcase(_CaseName, Conf) -> init_per_testcase(_CaseName, Conf) ->
emqx_gateway_conf:unload(), emqx_gateway_conf:unload(),
emqx_config:put([gateway], #{}), emqx_config:put([gateway], #{}),
emqx_config:put_raw([gateway], #{}),
emqx_config:init_load(emqx_gateway_schema, <<"gateway {}">>),
emqx_gateway_conf:load(), emqx_gateway_conf:load(),
Conf. Conf.
@ -45,9 +47,48 @@ init_per_testcase(_CaseName, Conf) ->
%% Cases %% Cases
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
t_load_gateway(_) -> -define(CONF_STOMP1, #{listeners => #{tcp => #{default => #{bind => 61613}}}}).
ok = emqx_gateway_conf:load_gateway(stomp, #{listeners => #{ tcp => #{default => #{bind => 7993}}}}), -define(CONF_STOMP2, #{listeners => #{tcp => #{default => #{bind => 61614}}}}).
A = emqx:get_config([gateway, stomp]), t_load_remove_gateway(_) ->
io:format(standard_error, "-~p~n", [A]), ok = emqx_gateway_conf:load_gateway(stomp, ?CONF_STOMP1),
{error, {pre_config_update, emqx_gateway_conf, already_exist}} =
emqx_gateway_conf:load_gateway(stomp, ?CONF_STOMP1),
assert_confs(?CONF_STOMP1, emqx:get_config([gateway, stomp])),
ok = emqx_gateway_conf:update_gateway(stomp, ?CONF_STOMP2),
assert_confs(?CONF_STOMP2, emqx:get_config([gateway, stomp])),
ok = emqx_gateway_conf:remove_gateway(stomp),
ok = emqx_gateway_conf:remove_gateway(stomp),
{error, {pre_config_update, emqx_gateway_conf, not_found}} =
emqx_gateway_conf:update_gateway(stomp, ?CONF_STOMP2),
?assertException(error, {config_not_found, [gateway,stomp]},
emqx:get_config([gateway, stomp])),
ok. ok.
%%--------------------------------------------------------------------
%% Utils
assert_confs(Expected, Effected) ->
case do_assert_confs(Expected, Effected) of
false ->
io:format(standard_error, "Expected config: ~p,\n"
"Effected config: ~p",
[Expected, Effected]),
exit(conf_not_match);
true ->
ok
end.
do_assert_confs(Expected, Effected) when is_map(Expected),
is_map(Effected) ->
Ks1 = maps:keys(Expected),
lists:all(fun(K) ->
do_assert_confs(maps:get(K, Expected),
maps:get(K, Effected, undefined))
end, Ks1);
do_assert_confs(Expected, Effected) ->
Expected =:= Effected.