chore(gw): add cases for emqx_gateway_conf
This commit is contained in:
parent
f68dfff0d6
commit
d163e99d58
|
@ -27,9 +27,11 @@ start(_StartType, _StartArgs) ->
|
|||
emqx_gateway_cli:load(),
|
||||
load_default_gateway_applications(),
|
||||
load_gateway_by_default(),
|
||||
emqx_gateway_conf:load(),
|
||||
{ok, Sup}.
|
||||
|
||||
stop(_State) ->
|
||||
emqx_gateway_conf:unload(),
|
||||
emqx_gateway_cli:unload(),
|
||||
ok.
|
||||
|
||||
|
|
|
@ -132,7 +132,7 @@ pre_config_update({load_gateway, GwName, Conf}, RawConf) ->
|
|||
undefined ->
|
||||
{ok, emqx_map_lib:deep_merge(RawConf, #{GwName => Conf})};
|
||||
_ ->
|
||||
{error, alredy_exist}
|
||||
{error, already_exist}
|
||||
end;
|
||||
pre_config_update({update_gateway, GwName, Conf}, RawConf) ->
|
||||
case maps:get(GwName, RawConf, undefined) of
|
||||
|
@ -155,7 +155,7 @@ pre_config_update({add_listener, GwName, {LType, LName}, Conf}, RawConf) ->
|
|||
RawConf,
|
||||
#{GwName => #{<<"listeners">> => NListener}})};
|
||||
_ ->
|
||||
{error, alredy_exist}
|
||||
{error, already_exist}
|
||||
end;
|
||||
pre_config_update({update_listener, GwName, {LType, LName}, Conf}, RawConf) ->
|
||||
case emqx_map_lib:deep_get(
|
||||
|
@ -181,7 +181,7 @@ pre_config_update({add_authn, GwName, Conf}, RawConf) ->
|
|||
RawConf,
|
||||
#{GwName => #{<<"authentication">> => Conf}})};
|
||||
_ ->
|
||||
{error, alredy_exist}
|
||||
{error, already_exist}
|
||||
end;
|
||||
pre_config_update({add_authn, GwName, {LType, LName}, Conf}, RawConf) ->
|
||||
case emqx_map_lib:deep_get(
|
||||
|
@ -198,7 +198,7 @@ pre_config_update({add_authn, GwName, {LType, LName}, Conf}, RawConf) ->
|
|||
#{LType => #{LName => NListener}}}},
|
||||
{ok, emqx_map_lib:deep_merge(RawConf, NGateway)};
|
||||
_ ->
|
||||
{error, alredy_exist}
|
||||
{error, already_exist}
|
||||
end
|
||||
end;
|
||||
pre_config_update({update_authn, GwName, Conf}, RawConf) ->
|
||||
|
@ -251,10 +251,15 @@ pre_config_update(UnknownReq, _RawConf) ->
|
|||
post_config_update(Req, NewConfig, OldConfig, _AppEnvs) ->
|
||||
[_Tag, GwName0|_] = tuple_to_list(Req),
|
||||
GwName = binary_to_existing_atom(GwName0),
|
||||
SubConf = maps:get(GwName, NewConfig),
|
||||
case maps:get(GwName, OldConfig, undefined) of
|
||||
undefined ->
|
||||
emqx_gateway:load(GwName, SubConf);
|
||||
_ ->
|
||||
emqx_gateway:update(GwName, SubConf)
|
||||
|
||||
case {maps:get(GwName, NewConfig, undefined),
|
||||
maps:get(GwName, OldConfig, undefined)} of
|
||||
{undefined, undefined} ->
|
||||
ok; %% nothing to change
|
||||
{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.
|
||||
|
|
|
@ -29,15 +29,17 @@ all() ->
|
|||
emqx_ct:all(?MODULE).
|
||||
|
||||
init_per_suite(Conf) ->
|
||||
emqx_ct_helpers:start_apps([]),
|
||||
emqx_ct_helpers:start_apps([emqx_gateway]),
|
||||
Conf.
|
||||
|
||||
end_per_suite(_Conf) ->
|
||||
emqx_ct_helpers:stop_apps([]).
|
||||
emqx_ct_helpers:stop_apps([emqx_gateway]).
|
||||
|
||||
init_per_testcase(_CaseName, Conf) ->
|
||||
emqx_gateway_conf:unload(),
|
||||
emqx_config:put([gateway], #{}),
|
||||
emqx_config:put_raw([gateway], #{}),
|
||||
emqx_config:init_load(emqx_gateway_schema, <<"gateway {}">>),
|
||||
emqx_gateway_conf:load(),
|
||||
Conf.
|
||||
|
||||
|
@ -45,9 +47,48 @@ init_per_testcase(_CaseName, Conf) ->
|
|||
%% Cases
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
t_load_gateway(_) ->
|
||||
ok = emqx_gateway_conf:load_gateway(stomp, #{listeners => #{ tcp => #{default => #{bind => 7993}}}}),
|
||||
-define(CONF_STOMP1, #{listeners => #{tcp => #{default => #{bind => 61613}}}}).
|
||||
-define(CONF_STOMP2, #{listeners => #{tcp => #{default => #{bind => 61614}}}}).
|
||||
|
||||
A = emqx:get_config([gateway, stomp]),
|
||||
io:format(standard_error, "-~p~n", [A]),
|
||||
t_load_remove_gateway(_) ->
|
||||
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.
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% 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.
|
||||
|
|
Loading…
Reference in New Issue