test(apply rule trace): add test case for republish and console print

This commit is contained in:
Kjell Winblad 2024-05-02 18:46:38 +02:00
parent b9cd75eef0
commit e32745bca6
2 changed files with 73 additions and 12 deletions

View File

@ -252,11 +252,14 @@ create_rule_and_action_http(BridgeType, RuleTopic, Config) ->
create_rule_and_action_http(BridgeType, RuleTopic, Config, Opts) ->
BridgeName = ?config(bridge_name, Config),
BridgeId = emqx_bridge_resource:bridge_id(BridgeType, BridgeName),
create_rule_and_action(BridgeId, RuleTopic, Opts).
create_rule_and_action(Action, RuleTopic, Opts) ->
SQL = maps:get(sql, Opts, <<"SELECT * FROM \"", RuleTopic/binary, "\"">>),
Params = #{
enable => true,
sql => SQL,
actions => [BridgeId]
actions => [Action]
},
Path = emqx_mgmt_api_test_util:api_path(["rules"]),
AuthHeader = emqx_mgmt_api_test_util:auth_header_(),

View File

@ -26,7 +26,24 @@
-define(CONF_DEFAULT, <<"rule_engine {rules {}}">>).
all() ->
emqx_common_test_helpers:all(?MODULE).
[
emqx_common_test_helpers:all(?MODULE),
{group, republish},
{group, console_print}
].
groups() ->
[
{republish, [], basic_tests()},
{console_print, [], basic_tests()}
].
basic_tests() ->
[
t_basic_apply_rule_trace_ruleid,
t_basic_apply_rule_trace_clientid,
t_basic_apply_rule_trace_ruleid_stop_after_render
].
init_per_suite(Config) ->
application:load(emqx_conf),
@ -50,6 +67,12 @@ init_per_suite(Config) ->
emqx_mgmt_api_test_util:init_suite(),
[{apps, Apps} | Config].
init_per_group(GroupName, Config) ->
[{group_name, GroupName} | Config].
end_per_group(_GroupName, Config) ->
Config.
end_per_suite(Config) ->
Apps = ?config(apps, Config),
emqx_mgmt_api_test_util:end_suite(),
@ -67,28 +90,58 @@ end_per_testcase(_TestCase, _Config) ->
ok.
t_basic_apply_rule_trace_ruleid(Config) ->
basic_apply_rule_test_helper(Config, ruleid, false).
basic_apply_rule_test_helper(get_action(Config), ruleid, false).
t_basic_apply_rule_trace_clientid(Config) ->
basic_apply_rule_test_helper(Config, clientid, false).
basic_apply_rule_test_helper(get_action(Config), clientid, false).
t_basic_apply_rule_trace_ruleid_stop_after_render(Config) ->
basic_apply_rule_test_helper(Config, ruleid, true).
basic_apply_rule_test_helper(get_action(Config), ruleid, true).
basic_apply_rule_test_helper(Config, TraceType, StopAfterRender) ->
get_action(Config) ->
case ?config(group_name, Config) of
republish ->
republish_action();
console_print ->
console_print_action();
_ ->
make_http_bridge(Config)
end.
make_http_bridge(Config) ->
HTTPServerConfig = ?config(http_server, Config),
emqx_bridge_http_test_lib:make_bridge(HTTPServerConfig),
#{status := connected} = emqx_bridge_v2:health_check(
http, emqx_bridge_http_test_lib:bridge_name()
),
BridgeName = ?config(bridge_name, Config),
emqx_bridge_resource:bridge_id(http, BridgeName).
republish_action() ->
#{
<<"args">> =>
#{
<<"mqtt_properties">> => #{},
<<"payload">> => <<"MY PL">>,
<<"qos">> => 0,
<<"retain">> => false,
<<"topic">> => <<"rule_apply_test_SUITE">>,
<<"user_properties">> => <<>>
},
<<"function">> => <<"republish">>
}.
console_print_action() ->
#{<<"function">> => <<"console">>}.
basic_apply_rule_test_helper(Action, TraceType, StopAfterRender) ->
%% Create Rule
RuleTopic = iolist_to_binary([<<"my_rule_topic/">>, atom_to_binary(?FUNCTION_NAME)]),
SQL = <<"SELECT payload.id as id FROM \"", RuleTopic/binary, "\"">>,
{ok, #{<<"id">> := RuleId}} =
emqx_bridge_testlib:create_rule_and_action_http(
http,
emqx_bridge_testlib:create_rule_and_action(
Action,
RuleTopic,
Config,
#{sql => SQL}
),
ClientId = <<"c_emqx">>,
@ -127,9 +180,14 @@ basic_apply_rule_test_helper(Config, TraceType, StopAfterRender) ->
io:format("THELOG:~n~s", [Bin]),
?assertNotEqual(nomatch, binary:match(Bin, [<<"rule_activated">>])),
?assertNotEqual(nomatch, binary:match(Bin, [<<"SQL_yielded_result">>])),
?assertNotEqual(nomatch, binary:match(Bin, [<<"bridge_action">>])),
?assertNotEqual(nomatch, binary:match(Bin, [<<"action_template_rendered">>])),
?assertNotEqual(nomatch, binary:match(Bin, [<<"QUERY_ASYNC">>]))
case Action of
A when is_binary(A) ->
?assertNotEqual(nomatch, binary:match(Bin, [<<"bridge_action">>])),
?assertNotEqual(nomatch, binary:match(Bin, [<<"QUERY_ASYNC">>]));
_ ->
?assertNotEqual(nomatch, binary:match(Bin, [<<"call_action_function">>]))
end,
?assertNotEqual(nomatch, binary:match(Bin, [<<"action_template_rendered">>]))
end
),
case StopAfterRender of