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

Add test cases for emqx_bridge, emqx_mod_rewrite (#1914)

Gilbert 7 лет назад
Родитель
Сommit
7544a21e25
4 измененных файлов с 121 добавлено и 2 удалено
  1. 1 1
      Makefile
  2. 0 1
      src/emqx_mod_rewrite.erl
  3. 57 0
      test/emqx_bridge_SUITE.erl
  4. 63 0
      test/emqx_mod_rewrite_tests.erl

+ 1 - 1
Makefile

@@ -40,7 +40,7 @@ CT_SUITES = emqx emqx_client emqx_zone emqx_banned emqx_connection emqx_session
 			emqx_keepalive emqx_lib emqx_metrics emqx_mod emqx_mod_sup emqx_mqtt_caps \
 			emqx_mqtt_props emqx_mqueue emqx_net emqx_pqueue emqx_router emqx_sm \
 			emqx_tables emqx_time emqx_topic emqx_trie emqx_vm emqx_mountpoint \
-		 	emqx_listeners emqx_protocol emqx_pool emqx_shared_sub
+		 	emqx_listeners emqx_protocol emqx_pool emqx_shared_sub emqx_bridge
 
 CT_NODE_NAME = emqxct@127.0.0.1
 CT_OPTS = -cover test/ct.cover.spec -erl_args -name $(CT_NODE_NAME)

+ 0 - 1
src/emqx_mod_rewrite.erl

@@ -75,4 +75,3 @@ compile(Rules) ->
                   {ok, MP} = re:compile(Re),
                   {rewrite, Topic, MP, Dest}
               end, Rules).
-

+ 57 - 0
test/emqx_bridge_SUITE.erl

@@ -0,0 +1,57 @@
+%% Copyright (c) 2018 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_bridge_SUITE).
+
+-compile(export_all).
+-compile(nowarn_export_all).
+
+-include_lib("eunit/include/eunit.hrl").
+-include_lib("common_test/include/ct.hrl").
+
+all() ->
+    [bridge_test].
+
+init_per_suite(Config) ->
+    emqx_ct_broker_helpers:run_setup_steps(),
+    Config.
+
+end_per_suite(_Config) ->
+    emqx_ct_broker_helpers:run_teardown_steps().
+
+bridge_test(_) ->
+    {ok, _Pid} = emqx_bridge:start_link(emqx, []),
+    #{msg := <<"start bridge successfully">>}
+        = emqx_bridge:start_bridge(emqx),
+    test_forwards(),
+    test_subscriptions(0),
+    test_subscriptions(1),
+    test_subscriptions(2),
+    #{msg := <<"stop bridge successfully">>}
+        = emqx_bridge:stop_bridge(emqx),
+    ok.
+
+test_forwards() ->
+    emqx_bridge:add_forward(emqx, <<"test_forwards">>),
+    [<<"test_forwards">>] = emqx_bridge:show_forwards(emqx),
+    emqx_bridge:del_forward(emqx, <<"test_forwards">>),
+    [] = emqx_bridge:show_forwards(emqx),
+    ok.
+
+test_subscriptions(QoS) ->
+    emqx_bridge:add_subscription(emqx, <<"test_subscriptions">>, QoS),
+    [{<<"test_subscriptions">>, QoS}] = emqx_bridge:show_subscriptions(emqx),
+    emqx_bridge:del_subscription(emqx, <<"test_subscriptions">>),
+    [] = emqx_bridge:show_subscriptions(emqx),
+    ok.

+ 63 - 0
test/emqx_mod_rewrite_tests.erl

@@ -0,0 +1,63 @@
+%% Copyright (c) 2018 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_mod_rewrite_tests).
+
+-include_lib("emqx.hrl").
+-include_lib("eunit/include/eunit.hrl").
+
+
+rules() ->
+    Rawrules1 = "x/# ^x/y/(.+)$ z/y/$1",
+    Rawrules2 = "y/+/z/# ^y/(.+)/z/(.+)$ y/z/$2",
+    Rawrules = [Rawrules1, Rawrules2],
+    Rules = lists:map(fun(Rule) ->
+                              [Topic, Re, Dest] = string:tokens(Rule, " "),
+                              {rewrite,
+                               list_to_binary(Topic),
+                               list_to_binary(Re),
+                               list_to_binary(Dest)}
+                      end, Rawrules),
+    lists:map(fun({rewrite, Topic, Re, Dest}) ->
+                      {ok, MP} = re:compile(Re),
+                      {rewrite, Topic, MP, Dest}
+              end, Rules).
+
+rewrite_subscribe_test() ->
+    Rules = rules(),
+    io:format("Rules: ~p",[Rules]),
+    ?assertEqual({ok, [{<<"test">>, opts}]},
+                 emqx_mod_rewrite:rewrite_subscribe(credentials, [{<<"test">>, opts}], Rules)),
+    ?assertEqual({ok, [{<<"z/y/test">>, opts}]},
+                 emqx_mod_rewrite:rewrite_subscribe(credentials, [{<<"x/y/test">>, opts}], Rules)),
+    ?assertEqual({ok, [{<<"y/z/test_topic">>, opts}]},
+                 emqx_mod_rewrite:rewrite_subscribe(credentials, [{<<"y/test/z/test_topic">>, opts}], Rules)).
+
+rewrite_unsubscribe_test() ->
+    Rules = rules(),
+    ?assertEqual({ok, [{<<"test">>, opts}]},
+                 emqx_mod_rewrite:rewrite_subscribe(credentials, [{<<"test">>, opts}], Rules)),
+    ?assertEqual({ok, [{<<"z/y/test">>, opts}]},
+                 emqx_mod_rewrite:rewrite_subscribe(credentials, [{<<"x/y/test">>, opts}], Rules)),
+    ?assertEqual({ok, [{<<"y/z/test_topic">>, opts}]},
+                 emqx_mod_rewrite:rewrite_subscribe(credentials, [{<<"y/test/z/test_topic">>, opts}], Rules)).
+
+rewrite_publish_test() ->
+    Rules = rules(),
+    ?assertMatch({ok, #message{topic = <<"test">>}},
+                 emqx_mod_rewrite:rewrite_publish(#message{topic = <<"test">>}, Rules)),
+    ?assertMatch({ok, #message{topic = <<"z/y/test">>}},
+                 emqx_mod_rewrite:rewrite_publish(#message{topic = <<"x/y/test">>}, Rules)),
+    ?assertMatch({ok, #message{topic = <<"y/z/test_topic">>}},
+                 emqx_mod_rewrite:rewrite_publish(#message{topic = <<"y/test/z/test_topic">>}, Rules)).