test(gw): more testcases for emqx_gateway_metrics

This commit is contained in:
JianBo He 2022-01-06 18:26:02 +08:00
parent a829b0b9d0
commit 3caf0822c4
7 changed files with 117 additions and 21 deletions

View File

@ -175,15 +175,13 @@ gateway(_) ->
]). ]).
'gateway-metrics'([Name]) -> 'gateway-metrics'([Name]) ->
Tab = emqx_gateway_metrics:tabname(Name), case emqx_gateway_metrics:lookup(atom(Name)) of
case ets:info(Tab) of
undefined -> undefined ->
print("Bad Gateway Name.\n"); print("Bad Gateway Name.\n");
_ -> Metrics ->
lists:foreach( lists:foreach(
fun({K, V}) -> fun({K, V}) -> print("~-30s: ~w\n", [K, V]) end,
print("~-30s: ~w\n", [K, V]) Metrics)
end, lists:sort(ets:tab2list(Tab)))
end; end;
'gateway-metrics'(_) -> 'gateway-metrics'(_) ->

View File

@ -20,7 +20,6 @@
-include_lib("emqx_gateway/include/emqx_gateway.hrl"). -include_lib("emqx_gateway/include/emqx_gateway.hrl").
%% APIs %% APIs
-export([start_link/1]). -export([start_link/1]).
@ -30,6 +29,8 @@
, dec/3 , dec/3
]). ]).
-export([lookup/1]).
%% gen_server callbacks %% gen_server callbacks
-export([ init/1 -export([ init/1
, handle_call/3 , handle_call/3
@ -67,6 +68,16 @@ dec(GwName, Name) ->
dec(GwName, Name, Oct) -> dec(GwName, Name, Oct) ->
inc(GwName, Name, -Oct). inc(GwName, Name, -Oct).
-spec lookup(gateway_name())
-> undefined
| [{Name :: atom(), integer()}].
lookup(GwName) ->
Tab = emqx_gateway_metrics:tabname(GwName),
case ets:info(Tab) of
undefined -> undefined;
_ -> lists:sort(ets:tab2list(Tab))
end.
tabname(GwName) -> tabname(GwName) ->
list_to_atom(lists:concat([emqx_gateway_, GwName, '_metrics'])). list_to_atom(lists:concat([emqx_gateway_, GwName, '_metrics'])).

View File

@ -291,9 +291,8 @@ is_running(ListenerId, #{<<"bind">> := ListenOn0}) ->
end. end.
%% same with emqx_authentication:global_chain/1 %% same with emqx_authentication:global_chain/1
global_chain(mqtt) -> -spec global_chain(GatewayName :: atom()) -> atom().
'mqtt:global'; global_chain('mqttsn') ->
global_chain('mqtt-sn') ->
'mqtt-sn:global'; 'mqtt-sn:global';
global_chain(coap) -> global_chain(coap) ->
'coap:global'; 'coap:global';

View File

@ -30,9 +30,7 @@
%% this parses to #{}, will not cause config cleanup %% this parses to #{}, will not cause config cleanup
%% so we will need call emqx_config:erase %% so we will need call emqx_config:erase
-define(CONF_DEFAULT, <<" -define(CONF_DEFAULT, <<"gateway {}">>).
gateway {}
">>).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Setup %% Setup
@ -307,6 +305,10 @@ t_listeners_authn(_) ->
{200, ConfResp3} = request(get, Path), {200, ConfResp3} = request(get, Path),
assert_confs(AuthConf2, ConfResp3), assert_confs(AuthConf2, ConfResp3),
{204, _} = request(delete, Path),
%% FIXME: 204?
{204, _} = request(get, Path),
{204, _} = request(delete, "/gateway/stomp"). {204, _} = request(delete, "/gateway/stomp").
t_listeners_authn_data_mgmt(_) -> t_listeners_authn_data_mgmt(_) ->
@ -340,32 +342,32 @@ t_listeners_authn_data_mgmt(_) ->
{200, {200,
#{data := [UserRespd1]} } = request( #{data := [UserRespd1]} } = request(
get, get,
"/gateway/stomp/listeners/stomp:tcp:def/authentication/users"), Path ++ "/users"),
assert_confs(UserRespd1, User1), assert_confs(UserRespd1, User1),
{200, UserRespd2} = request( {200, UserRespd2} = request(
get, get,
"/gateway/stomp/listeners/stomp:tcp:def/authentication/users/test"), Path ++ "/users/test"),
assert_confs(UserRespd2, User1), assert_confs(UserRespd2, User1),
{200, UserRespd3} = request( {200, UserRespd3} = request(
put, put,
"/gateway/stomp/listeners/stomp:tcp:def/authentication/users/test", Path ++ "/users/test",
#{password => <<"654321">>, is_superuser => true}), #{password => <<"654321">>, is_superuser => true}),
assert_confs(UserRespd3, User1#{is_superuser => true}), assert_confs(UserRespd3, User1#{is_superuser => true}),
{200, UserRespd4} = request( {200, UserRespd4} = request(
get, get,
"/gateway/stomp/listeners/stomp:tcp:def/authentication/users/test"), Path ++ "/users/test"),
assert_confs(UserRespd4, User1#{is_superuser => true}), assert_confs(UserRespd4, User1#{is_superuser => true}),
{204, _} = request( {204, _} = request(
delete, delete,
"/gateway/stomp/listeners/stomp:tcp:def/authentication/users/test"), Path ++ "/users/test"),
{200, #{data := []}} = request( {200, #{data := []}} = request(
get, get,
"/gateway/stomp/listeners/stomp:tcp:def/authentication/users"), Path ++ "/users"),
{204, _} = request(delete, "/gateway/stomp"). {204, _} = request(delete, "/gateway/stomp").
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------

View File

@ -124,7 +124,17 @@ t_gateway_list(_) ->
"Gateway(name=lwm2m, status=unloaded)\n" "Gateway(name=lwm2m, status=unloaded)\n"
"Gateway(name=mqttsn, status=unloaded)\n" "Gateway(name=mqttsn, status=unloaded)\n"
"Gateway(name=stomp, status=unloaded)\n" "Gateway(name=stomp, status=unloaded)\n"
, acc_print()). , acc_print()),
emqx_gateway_cli:gateway(["load", "mqttsn", ?CONF_MQTTSN]),
?assertEqual("ok\n", acc_print()),
emqx_gateway_cli:gateway(["list"]),
%% TODO: assert it.
_ = acc_print(),
emqx_gateway_cli:gateway(["unload", "mqttsn"]),
?assertEqual("ok\n", acc_print()).
t_gateway_load_unload_lookup(_) -> t_gateway_load_unload_lookup(_) ->
emqx_gateway_cli:gateway(["lookup", "mqttsn"]), emqx_gateway_cli:gateway(["lookup", "mqttsn"]),

View File

@ -0,0 +1,76 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2022 EMQ Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------
-module(emqx_gateway_metrics_SUITE).
-include_lib("eunit/include/eunit.hrl").
-compile(export_all).
-compile(nowarn_export_all).
-define(GWNAME, mqttsn).
-define(METRIC, 'ct.test.metrics_name').
-define(CONF_DEFAULT, <<"gateway {}">>).
%%--------------------------------------------------------------------
%% setups
%%--------------------------------------------------------------------
all() -> emqx_common_test_helpers:all(?MODULE).
init_per_suite(Conf) ->
emqx_config:erase(gateway),
emqx_config:init_load(emqx_gateway_schema, ?CONF_DEFAULT),
emqx_common_test_helpers:start_apps([]),
Conf.
end_per_suite(_Conf) ->
emqx_common_test_helpers:stop_apps([]).
init_per_testcase(_TestCase, Conf) ->
{ok, Pid} = emqx_gateway_metrics:start_link(?GWNAME),
[{metrics, Pid} | Conf].
end_per_testcase(_TestCase, Conf) ->
Pid = proplists:get_value(metrics, Conf),
gen_server:stop(Pid),
Conf.
%%--------------------------------------------------------------------
%% cases
%%--------------------------------------------------------------------
t_inc_dec(_) ->
ok = emqx_gateway_metrics:inc(?GWNAME, ?METRIC),
ok = emqx_gateway_metrics:inc(?GWNAME, ?METRIC),
?assertEqual(
[{?METRIC, 2}],
emqx_gateway_metrics:lookup(?GWNAME)),
ok = emqx_gateway_metrics:dec(?GWNAME, ?METRIC),
ok = emqx_gateway_metrics:dec(?GWNAME, ?METRIC),
?assertEqual(
[{?METRIC, 0}],
emqx_gateway_metrics:lookup(?GWNAME)).
t_handle_unexpected_msg(Conf) ->
Pid = proplists:get_value(metrics, Conf),
_ = Pid ! unexpected_info,
ok = gen_server:cast(Pid, unexpected_cast),
ok = gen_server:call(Pid, unexpected_call),
ok.