test: make all emqx_bridge_greptimedb_SUITE tests passing

This commit is contained in:
Dennis Zhuang 2023-07-20 20:10:29 +08:00 committed by firest
parent c6a7f3e2ad
commit 4921856950
2 changed files with 58 additions and 40 deletions

View File

@ -81,7 +81,7 @@ on_query(InstId, {send_message, Data}, _State = #{write_syntax := SyntaxLines, c
#{batch => false, mode => sync, error => ErrorPoints} #{batch => false, mode => sync, error => ErrorPoints}
), ),
log_error_points(InstId, ErrorPoints), log_error_points(InstId, ErrorPoints),
ErrorPoints {error, ErrorPoints}
end. end.
%% Once a Batched Data trans to points failed. %% Once a Batched Data trans to points failed.
@ -463,7 +463,7 @@ parse_timestamp([TsBin]) ->
continue_lines_to_points(Data, Item, Rest, ResultPointsAcc, ErrorPointsAcc) -> continue_lines_to_points(Data, Item, Rest, ResultPointsAcc, ErrorPointsAcc) ->
case line_to_point(Data, Item) of case line_to_point(Data, Item) of
#{fields := Fields} when map_size(Fields) =:= 0 -> {_, [#{fields := Fields}]} when map_size(Fields) =:= 0 ->
%% greptimedb client doesn't like empty field maps... %% greptimedb client doesn't like empty field maps...
ErrorPointsAcc1 = [{error, no_fields} | ErrorPointsAcc], ErrorPointsAcc1 = [{error, no_fields} | ErrorPointsAcc],
lines_to_points(Data, Rest, ResultPointsAcc, ErrorPointsAcc1); lines_to_points(Data, Rest, ResultPointsAcc, ErrorPointsAcc1);

View File

@ -9,6 +9,7 @@
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
-include_lib("common_test/include/ct.hrl"). -include_lib("common_test/include/ct.hrl").
-include_lib("snabbkaffe/include/snabbkaffe.hrl"). -include_lib("snabbkaffe/include/snabbkaffe.hrl").
-include_lib("emqx/include/logger.hrl").
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% CT boilerplate %% CT boilerplate
@ -284,7 +285,8 @@ send_message(Config, Payload) ->
Name = ?config(greptimedb_name, Config), Name = ?config(greptimedb_name, Config),
Type = greptimedb_type_bin(?config(greptimedb_type, Config)), Type = greptimedb_type_bin(?config(greptimedb_type, Config)),
BridgeId = emqx_bridge_resource:bridge_id(Type, Name), BridgeId = emqx_bridge_resource:bridge_id(Type, Name),
emqx_bridge:send_message(BridgeId, Payload). Resp = emqx_bridge:send_message(BridgeId, Payload),
Resp.
query_by_clientid(Topic, ClientId, Config) -> query_by_clientid(Topic, ClientId, Config) ->
GreptimedbHost = ?config(greptimedb_host, Config), GreptimedbHost = ?config(greptimedb_host, Config),
@ -308,7 +310,7 @@ query_by_clientid(Topic, ClientId, Config) ->
{"Authorization", "Basic Z3JlcHRpbWVfdXNlcjpncmVwdGltZV9wd2Q="}, {"Authorization", "Basic Z3JlcHRpbWVfdXNlcjpncmVwdGltZV9wd2Q="},
{"Content-Type", "application/x-www-form-urlencoded"} {"Content-Type", "application/x-www-form-urlencoded"}
], ],
Body = <<"sql=select * from ", Topic/binary, " where clientid='", ClientId/binary, "'">>, Body = <<"sql=select * from \"", Topic/binary, "\" where clientid='", ClientId/binary, "'">>,
{ok, 200, _Headers, RawBody0} = {ok, 200, _Headers, RawBody0} =
ehttpc:request( ehttpc:request(
EHttpcPoolName, EHttpcPoolName,
@ -317,29 +319,49 @@ query_by_clientid(Topic, ClientId, Config) ->
_Timeout = 10_000, _Timeout = 10_000,
_Retry = 0 _Retry = 0
), ),
#{
<<"code">> := 0,
<<"output">> := [
#{
<<"records">> := #{
<<"rows">> := Rows,
<<"schema">> := Schema
}
}
]
} = emqx_utils_json:decode(RawBody0, [return_maps]),
case Schema of case emqx_utils_json:decode(RawBody0, [return_maps]) of
null -> #{
#{}; <<"code">> := 0,
#{<<"column_schemas">> := ColumnsSchemas} -> <<"output">> := [
Columns = lists:map(fun(#{<<"name">> := Name}) -> Name end, ColumnsSchemas), #{
index_by_field(Rows, Columns) <<"records">> := #{
<<"rows">> := Rows,
<<"schema">> := Schema
}
}
]
} ->
make_row(Schema, Rows);
#{
<<"code">> := Code,
<<"error">> := Error
} ->
GreptimedbName = ?config(greptimedb_name, Config),
Type = greptimedb_type_bin(?config(greptimedb_type, Config)),
BridgeId = emqx_bridge_resource:bridge_id(Type, GreptimedbName),
?SLOG(error, #{
msg => io_lib:format("Failed to query: ~p, ~p", [Code, Error]),
connector => BridgeId,
reason => Error
}),
%% TODO(dennis): check the error by code
case binary:match(Error, <<"Table not found">>) of
nomatch ->
{error, Error};
_ ->
%% Table not found
#{}
end
end. end.
index_by_field([], Columns) -> make_row(null, _Rows) ->
#{}; #{};
index_by_field([Row], Columns) -> make_row(_Schema, []) ->
#{};
make_row(#{<<"column_schemas">> := ColumnsSchemas}, [Row]) ->
Columns = lists:map(fun(#{<<"name">> := Name}) -> Name end, ColumnsSchemas),
maps:from_list(lists:zip(Columns, Row)). maps:from_list(lists:zip(Columns, Row)).
assert_persisted_data(ClientId, Expected, PersistedData) -> assert_persisted_data(ClientId, Expected, PersistedData) ->
@ -784,26 +806,22 @@ t_write_failure(Config) ->
emqx_common_test_helpers:with_failure(down, ProxyName, ProxyHost, ProxyPort, fun() -> emqx_common_test_helpers:with_failure(down, ProxyName, ProxyHost, ProxyPort, fun() ->
case QueryMode of case QueryMode of
sync -> sync ->
{_, {ok, _}} = ?wait_async_action(
?wait_async_action( ?assertMatch(
?assertMatch( {error, {resource_error, #{reason := timeout}}},
{error, {resource_error, #{reason := timeout}}}, send_message(Config, SentData)
send_message(Config, SentData) ),
), #{?snk_kind := greptimedb_connector_do_query_failure, action := nack},
#{?snk_kind := handle_async_reply, action := nack}, 16_000
1_000 )
)
end end
end), end),
fun(Trace0) -> fun(Trace) ->
case QueryMode of case QueryMode of
sync -> sync ->
Trace = ?of_kind(handle_async_reply, Trace0), ?assertMatch(
?assertMatch([_ | _], Trace), [#{error := _} | _],
[#{result := Result} | _] = Trace, ?of_kind(greptimedb_connector_do_query_failure, Trace)
?assert(
not emqx_bridge_greptimedb_connector:is_unrecoverable_error(Result),
#{got => Result}
) )
end, end,
ok ok
@ -841,7 +859,7 @@ t_missing_field(Config) ->
?match_n_events(NEvents, #{ ?match_n_events(NEvents, #{
?snk_kind := greptimedb_connector_send_query_error ?snk_kind := greptimedb_connector_send_query_error
}), }),
_Timeout1 = 10_000 _Timeout1 = 16_000
), ),
ok ok
end, end,