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

test: cover the updating with bad sql teamplate

Jianbo He 1 год назад
Родитель
Сommit
f6b9613275

+ 15 - 0
apps/emqx_bridge/test/emqx_bridge_testlib.erl

@@ -190,6 +190,21 @@ update_bridge_api(Config, Overrides) ->
     ct:pal("bridge update result: ~p", [Res]),
     Res.
 
+get_bridge_api(Config) ->
+    BridgeType = ?config(bridge_type, Config),
+    Name = ?config(bridge_name, Config),
+    BridgeId = emqx_bridge_resource:bridge_id(BridgeType, Name),
+    Path = emqx_mgmt_api_test_util:api_path(["bridges", BridgeId]),
+    AuthHeader = emqx_mgmt_api_test_util:auth_header_(),
+    ct:pal("getting bridge (via http)", []),
+    Res =
+        case emqx_mgmt_api_test_util:request_api(get, Path, "", AuthHeader) of
+            {ok, Body0} -> {ok, emqx_utils_json:decode(Body0, [return_maps])};
+            Error -> Error
+        end,
+    ct:pal("bridge result: ~p", [Res]),
+    Res.
+
 delete_bridge_http_api_v1(Opts) ->
     #{type := Type, name := Name} = Opts,
     BridgeId = emqx_bridge_resource:bridge_id(Type, Name),

+ 61 - 2
apps/emqx_bridge_cassandra/test/emqx_bridge_cassandra_SUITE.erl

@@ -802,7 +802,7 @@ t_update_action_sql(Config) ->
         connect_and_get_payload(Config, "select qos from mqtt.mqtt_msg_test2 where topic = 't/a'")
     ),
 
-    %% Update the SQL Teamplate
+    %% Update a correct SQL Teamplate
     {ok, _} =
         emqx_bridge_testlib:update_bridge_api(
             Config,
@@ -832,6 +832,65 @@ t_update_action_sql(Config) ->
 
     ?assertEqual(
         0,
-        connect_and_get_payload(Config, "select qos from mqtt.mqtt_msg_test2 where topic = 't/a'")
+        connect_and_get_payload(Config, "select qos from mqtt.mqtt_msg_test2 where topic = 't/b'")
+    ),
+
+    %% Update a wrong SQL Teamplate
+    BadSQL =
+        <<"insert into mqtt_msg_test2(topic, qos, payload, bad_col_name) values (${topic}, ${qos}, ${payload}, ${timestamp})">>,
+    {ok, Body} =
+        emqx_bridge_testlib:update_bridge_api(
+            Config,
+            #{
+                <<"cql">> => BadSQL
+            }
+        ),
+    ?assertMatch(#{<<"status">> := <<"connecting">>}, Body),
+    Error1 = maps:get(<<"status_reason">>, Body),
+    case re:run(Error1, <<"Undefined column name bad_col_name">>, [{capture, none}]) of
+        match ->
+            ok;
+        nomatch ->
+            ct:fail(#{
+                expected_pattern => "undefined_column",
+                got => Error1
+            })
+    end,
+    %% assert that although there was an error returned, the invliad SQL is actually put
+    {ok, BridgeResp} = emqx_bridge_testlib:get_bridge_api(Config),
+    #{<<"cql">> := FetchedSQL} = BridgeResp,
+    ?assertEqual(FetchedSQL, BadSQL),
+
+    %% Update again with a correct SQL Teamplate
+    {ok, _} =
+        emqx_bridge_testlib:update_bridge_api(
+            Config,
+            #{
+                <<"cql">> => <<
+                    "insert into mqtt_msg_test2(topic, qos, payload, arrived) "
+                    "values (${topic}, ${qos}, ${payload}, ${timestamp})"
+                >>
+            }
+        ),
+
+    RuleTopic2 = <<"t/c">>,
+    Opts2 = #{
+        sql => <<"select * from \"", RuleTopic2/binary, "\"">>
+    },
+    {ok, _} = emqx_bridge_testlib:create_rule_and_action_http(
+        BridgeType, RuleTopic2, Config, Opts2
+    ),
+
+    Msg2 = emqx_message:make(RuleTopic2, Payload),
+    {_, {ok, _}} =
+        ?wait_async_action(
+            emqx:publish(Msg2),
+            #{?snk_kind := cassandra_connector_query_return},
+            10_000
+        ),
+
+    ?assertEqual(
+        0,
+        connect_and_get_payload(Config, "select qos from mqtt.mqtt_msg_test2 where topic = 't/c'")
     ),
     ok.