test(modules/rewrite): do not set fake config
the hand-crafted fake raw config does not pass hocon schema check this commit changes to use HOCON text as input for both test cases
This commit is contained in:
parent
729f89f514
commit
18fb18be89
|
@ -22,16 +22,22 @@
|
||||||
-include_lib("emqx/include/emqx_mqtt.hrl").
|
-include_lib("emqx/include/emqx_mqtt.hrl").
|
||||||
-include_lib("eunit/include/eunit.hrl").
|
-include_lib("eunit/include/eunit.hrl").
|
||||||
|
|
||||||
-define(RULES, [#{action => publish,
|
-define(REWRITE, <<"""
|
||||||
source_topic => <<"x/#">>,
|
rewrite: {
|
||||||
re => <<"^x/y/(.+)$">>,
|
rules : [
|
||||||
dest_topic => <<"z/y/$1">>
|
{
|
||||||
},
|
action : publish
|
||||||
#{action => subscribe,
|
source_topic : \"x/#\"
|
||||||
source_topic => <<"y/+/z/#">>,
|
re : \"^x/y/(.+)$\"
|
||||||
re => <<"^y/(.+)/z/(.+)$">>,
|
dest_topic : \"z/y/$1\"
|
||||||
dest_topic => <<"y/z/$2">>}
|
},
|
||||||
]).
|
{
|
||||||
|
action : subscribe
|
||||||
|
source_topic : \"y/+/z/#\"
|
||||||
|
re : \"^y/(.+)/z/(.+)$\"
|
||||||
|
dest_topic : \"y/z/$2\"
|
||||||
|
}
|
||||||
|
]}""">>).
|
||||||
|
|
||||||
all() -> emqx_ct:all(?MODULE).
|
all() -> emqx_ct:all(?MODULE).
|
||||||
|
|
||||||
|
@ -45,11 +51,7 @@ end_per_suite(_Config) ->
|
||||||
|
|
||||||
%% Test case for emqx_mod_write
|
%% Test case for emqx_mod_write
|
||||||
t_mod_rewrite(_Config) ->
|
t_mod_rewrite(_Config) ->
|
||||||
%% important! let emqx_schema include the current app!
|
ok = emqx_config:init_load(emqx_modules_schema, ?REWRITE),
|
||||||
meck:new(emqx_schema, [non_strict, passthrough, no_history, no_link]),
|
|
||||||
meck:expect(emqx_schema, includes, fun() -> ["rewrite"] end ),
|
|
||||||
meck:expect(emqx_schema, extra_schema_fields, fun(FieldName) -> emqx_modules_schema:fields(FieldName) end),
|
|
||||||
ok = emqx_config:update([rewrite, rules], ?RULES),
|
|
||||||
ok = emqx_rewrite:enable(),
|
ok = emqx_rewrite:enable(),
|
||||||
{ok, C} = emqtt:start_link([{clientid, <<"rewrite_client">>}]),
|
{ok, C} = emqtt:start_link([{clientid, <<"rewrite_client">>}]),
|
||||||
{ok, _} = emqtt:connect(C),
|
{ok, _} = emqtt:connect(C),
|
||||||
|
@ -58,7 +60,7 @@ t_mod_rewrite(_Config) ->
|
||||||
SubOrigTopics = [<<"y/a/z/b">>, <<"y/def">>],
|
SubOrigTopics = [<<"y/a/z/b">>, <<"y/def">>],
|
||||||
SubDestTopics = [<<"y/z/b">>, <<"y/def">>],
|
SubDestTopics = [<<"y/z/b">>, <<"y/def">>],
|
||||||
%% Sub Rules
|
%% Sub Rules
|
||||||
{ok, _Props, _} = emqtt:subscribe(C, [{Topic, ?QOS_1} || Topic <- SubOrigTopics]),
|
{ok, _Props1, _} = emqtt:subscribe(C, [{Topic, ?QOS_1} || Topic <- SubOrigTopics]),
|
||||||
timer:sleep(100),
|
timer:sleep(100),
|
||||||
Subscriptions = emqx_broker:subscriptions(<<"rewrite_client">>),
|
Subscriptions = emqx_broker:subscriptions(<<"rewrite_client">>),
|
||||||
?assertEqual(SubDestTopics, [Topic || {Topic, _SubOpts} <- Subscriptions]),
|
?assertEqual(SubDestTopics, [Topic || {Topic, _SubOpts} <- Subscriptions]),
|
||||||
|
@ -72,7 +74,7 @@ t_mod_rewrite(_Config) ->
|
||||||
timer:sleep(100),
|
timer:sleep(100),
|
||||||
?assertEqual([], emqx_broker:subscriptions(<<"rewrite_client">>)),
|
?assertEqual([], emqx_broker:subscriptions(<<"rewrite_client">>)),
|
||||||
%% Pub Rules
|
%% Pub Rules
|
||||||
{ok, _Props, _} = emqtt:subscribe(C, [{Topic, ?QOS_1} || Topic <- PubDestTopics]),
|
{ok, _Props2, _} = emqtt:subscribe(C, [{Topic, ?QOS_1} || Topic <- PubDestTopics]),
|
||||||
RecvTopics2 = [begin
|
RecvTopics2 = [begin
|
||||||
ok = emqtt:publish(C, Topic, <<"payload">>),
|
ok = emqtt:publish(C, Topic, <<"payload">>),
|
||||||
{ok, #{topic := RecvTopic}} = receive_publish(100),
|
{ok, #{topic := RecvTopic}} = receive_publish(100),
|
||||||
|
@ -82,11 +84,15 @@ t_mod_rewrite(_Config) ->
|
||||||
{ok, _, _} = emqtt:unsubscribe(C, PubDestTopics),
|
{ok, _, _} = emqtt:unsubscribe(C, PubDestTopics),
|
||||||
|
|
||||||
ok = emqtt:disconnect(C),
|
ok = emqtt:disconnect(C),
|
||||||
ok = emqx_rewrite:disable(),
|
ok = emqx_rewrite:disable().
|
||||||
meck:unload(emqx_schema).
|
|
||||||
|
|
||||||
t_rewrite_rule(_Config) ->
|
t_rewrite_rule(_Config) ->
|
||||||
{PubRules, SubRules} = emqx_rewrite:compile(?RULES),
|
{ok, Rewite} = hocon:binary(?REWRITE),
|
||||||
|
#{rewrite := #{rules := Rules}} =
|
||||||
|
hocon_schema:check_plain(emqx_modules_schema, Rewite,
|
||||||
|
#{atom_key => true},
|
||||||
|
["rewrite"]),
|
||||||
|
{PubRules, SubRules} = emqx_rewrite:compile(Rules),
|
||||||
?assertEqual(<<"z/y/2">>, emqx_rewrite:match_and_rewrite(<<"x/y/2">>, PubRules)),
|
?assertEqual(<<"z/y/2">>, emqx_rewrite:match_and_rewrite(<<"x/y/2">>, PubRules)),
|
||||||
?assertEqual(<<"x/1/2">>, emqx_rewrite:match_and_rewrite(<<"x/1/2">>, PubRules)),
|
?assertEqual(<<"x/1/2">>, emqx_rewrite:match_and_rewrite(<<"x/1/2">>, PubRules)),
|
||||||
?assertEqual(<<"y/z/b">>, emqx_rewrite:match_and_rewrite(<<"y/a/z/b">>, SubRules)),
|
?assertEqual(<<"y/z/b">>, emqx_rewrite:match_and_rewrite(<<"y/a/z/b">>, SubRules)),
|
||||||
|
|
Loading…
Reference in New Issue