test(emqx_authn): add status and metrics test to emqx_authn_api

This commit is contained in:
EMQ-YangM 2022-03-11 12:39:04 +08:00
parent dce602c251
commit 48b167bf46
2 changed files with 39 additions and 9 deletions

View File

@ -552,7 +552,7 @@ authenticators(get, _Params) ->
list_authenticators([authentication]). list_authenticators([authentication]).
authenticator(get, #{bindings := #{id := AuthenticatorID}}) -> authenticator(get, #{bindings := #{id := AuthenticatorID}}) ->
list_authenticator([authentication], AuthenticatorID); list_authenticator(?GLOBAL, [authentication], AuthenticatorID);
authenticator(put, #{bindings := #{id := AuthenticatorID}, body := Config}) -> authenticator(put, #{bindings := #{id := AuthenticatorID}, body := Config}) ->
update_authenticator([authentication], ?GLOBAL, AuthenticatorID, Config); update_authenticator([authentication], ?GLOBAL, AuthenticatorID, Config);
@ -576,8 +576,8 @@ listener_authenticators(get, #{bindings := #{listener_id := ListenerID}}) ->
listener_authenticator(get, #{bindings := #{listener_id := ListenerID, id := AuthenticatorID}}) -> listener_authenticator(get, #{bindings := #{listener_id := ListenerID, id := AuthenticatorID}}) ->
with_listener(ListenerID, with_listener(ListenerID,
fun(Type, Name, _) -> fun(Type, Name, ChainName) ->
list_authenticator([listeners, Type, Name, authentication], list_authenticator(ChainName, [listeners, Type, Name, authentication],
AuthenticatorID) AuthenticatorID)
end); end);
listener_authenticator(put, listener_authenticator(put,
@ -752,11 +752,11 @@ list_authenticators(ConfKeyPath) ->
|| AuthenticatorConfig <- AuthenticatorsConfig], || AuthenticatorConfig <- AuthenticatorsConfig],
{200, NAuthenticators}. {200, NAuthenticators}.
list_authenticator(ConfKeyPath, AuthenticatorID) -> list_authenticator(ChainName, ConfKeyPath, AuthenticatorID) ->
AuthenticatorsConfig = get_raw_config_with_defaults(ConfKeyPath), AuthenticatorsConfig = get_raw_config_with_defaults(ConfKeyPath),
case find_config(AuthenticatorID, AuthenticatorsConfig) of case find_config(AuthenticatorID, AuthenticatorsConfig) of
{ok, AuthenticatorConfig} -> {ok, AuthenticatorConfig} ->
Status_And_Metrics = lookup_from_all_nodes(?GLOBAL, AuthenticatorID), Status_And_Metrics = lookup_from_all_nodes(ChainName, AuthenticatorID),
Fun = fun ({Key, Val}, Map) -> maps:put(Key, Val, Map) end, Fun = fun ({Key, Val}, Map) -> maps:put(Key, Val, Map) end,
AppendList = [{id, AuthenticatorID}, {status_and_metrics, Status_And_Metrics}], AppendList = [{id, AuthenticatorID}, {status_and_metrics, Status_And_Metrics}],
{200, lists:foldl(Fun, convert_certs(AuthenticatorConfig), AppendList)}; {200, lists:foldl(Fun, convert_certs(AuthenticatorConfig), AppendList)};
@ -862,7 +862,9 @@ restructure_map(#{counters := #{failed := Failed, matched := Match, success := S
rate => Rate, rate => Rate,
rate_last5m => Rate5m, rate_last5m => Rate5m,
rate_max => RateMax rate_max => RateMax
}. };
restructure_map(Error) ->
Error.
error_msg(Code, Msg) when is_binary(Msg) -> error_msg(Code, Msg) when is_binary(Msg) ->
#{code => Code, message => Msg}; #{code => Code, message => Msg};

View File

@ -164,10 +164,38 @@ test_authenticator(PathPrefix) ->
post, post,
uri(PathPrefix ++ [?CONF_NS]), uri(PathPrefix ++ [?CONF_NS]),
ValidConfig0), ValidConfig0),
{ok, 200, _} = request( {ok, 200, Res} = request(
get, get,
uri(PathPrefix ++ [?CONF_NS, "password_based:http"])), uri(PathPrefix ++ [?CONF_NS, "password-based:http"])),
{ok, RList} = emqx_json:safe_decode(Res),
Snd = fun ({_, Val}) -> Val end,
LookupVal =
fun(FixVal) ->
fun(List, RestJson) ->
case List of
[Name] -> Snd(lists:keyfind(Name, 1, RestJson));
[Name | NS] -> (FixVal(FixVal))(NS, Snd(lists:keyfind(Name, 1, RestJson)))
end
end
end,
LookFun = fun (List) -> (LookupVal(LookupVal)) (List, RList) end,
MetricsList = [{<<"failed">>, 0},
{<<"matched">>, 0},
{<<"rate">>, 0.0},
{<<"rate_last5m">>, 0.0},
{<<"rate_max">>, 0.0},
{<<"success">>, 0}],
EqualFun = fun ({M, V}) ->
?assertEqual(V, LookFun([<<"status_and_metrics">>,
<<"metrics">>,
M]
)
) end,
lists:map(EqualFun, MetricsList),
?assertEqual(<<"connected">>,
LookFun([<<"status_and_metrics">>,
<<"status">>
])),
{ok, 404, _} = request( {ok, 404, _} = request(
get, get,
uri(PathPrefix ++ [?CONF_NS, "password_based:redis"])), uri(PathPrefix ++ [?CONF_NS, "password_based:redis"])),