Просмотр исходного кода

test(gw): more testcases for emqx_gateway_metrics

JianBo He 4 лет назад
Родитель
Сommit
3caf0822c4

+ 4 - 6
apps/emqx_gateway/src/emqx_gateway_cli.erl

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

+ 12 - 1
apps/emqx_gateway/src/emqx_gateway_metrics.erl

@@ -20,7 +20,6 @@
 
 -include_lib("emqx_gateway/include/emqx_gateway.hrl").
 
-
 %% APIs
 -export([start_link/1]).
 
@@ -30,6 +29,8 @@
         , dec/3
         ]).
 
+-export([lookup/1]).
+
 %% gen_server callbacks
 -export([ init/1
         , handle_call/3
@@ -67,6 +68,16 @@ dec(GwName, Name) ->
 dec(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) ->
     list_to_atom(lists:concat([emqx_gateway_, GwName, '_metrics'])).
 

+ 2 - 3
apps/emqx_gateway/src/emqx_gateway_utils.erl

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

+ 1 - 1
apps/emqx_gateway/test/emqx_gateway_SUITE.erl

@@ -81,7 +81,7 @@ t_start_stop_update(_) ->
            ?GWNAME, #{enable => false, idle_timeout => 2000}),
     #{status := stopped,
       config := #{idle_timeout := 2000}} = emqx_gateway:lookup(?GWNAME),
-    
+
     ok = emqx_gateway:update(
            ?GWNAME, #{enable => true, idle_timeout => 3000}),
     #{status := running,

+ 11 - 9
apps/emqx_gateway/test/emqx_gateway_api_SUITE.erl

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

+ 11 - 1
apps/emqx_gateway/test/emqx_gateway_cli_SUITE.erl

@@ -124,7 +124,17 @@ t_gateway_list(_) ->
       "Gateway(name=lwm2m, status=unloaded)\n"
       "Gateway(name=mqttsn, 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(_) ->
     emqx_gateway_cli:gateway(["lookup", "mqttsn"]),

+ 76 - 0
apps/emqx_gateway/test/emqx_gateway_metrics_SUITE.erl

@@ -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.