test(emqx_ee_connector): rework and improve influxdb tests

This commit is contained in:
Erik Timan 2023-02-09 17:12:01 +01:00
parent cfd0e9ebdd
commit bc6c653c9f
1 changed files with 62 additions and 26 deletions

View File

@ -117,40 +117,64 @@ perform_lifecycle_check(PoolName, InitialConfig) ->
% Should not even be able to get the resource data out of ets now unlike just stopping. % Should not even be able to get the resource data out of ets now unlike just stopping.
?assertEqual({error, not_found}, emqx_resource:get_instance(PoolName)). ?assertEqual({error, not_found}, emqx_resource:get_instance(PoolName)).
t_tls_opts(Config) -> t_tls_verify_none(Config) ->
PoolName = <<"emqx_ee_connector_influxdb_SUITE">>, PoolName = <<"emqx_ee_connector_influxdb_SUITE">>,
Host = ?config(influxdb_tls_host, Config), Host = ?config(influxdb_tls_host, Config),
Port = ?config(influxdb_tls_port, Config), Port = ?config(influxdb_tls_port, Config),
VerifyNoneStatus = perform_tls_opts_check( InitialConfig = influxdb_config(Host, Port, true, "verify_none"),
PoolName, influxdb_config(Host, Port, true, "verify_none") ValidStatus = perform_tls_opts_check(PoolName, InitialConfig, valid),
), ?assertEqual(connected, ValidStatus),
?assertEqual(connected, VerifyNoneStatus), InvalidStatus = perform_tls_opts_check(PoolName, InitialConfig, fail),
VerifyPeerStatus = perform_tls_opts_check( ?assertEqual(disconnected, InvalidStatus),
PoolName, influxdb_config(Host, Port, true, "verify_peer")
),
?assertEqual(disconnected, VerifyPeerStatus),
ok. ok.
perform_tls_opts_check(PoolName, InitialConfig) -> t_tls_verify_peer(Config) ->
PoolName = <<"emqx_ee_connector_influxdb_SUITE">>,
Host = ?config(influxdb_tls_host, Config),
Port = ?config(influxdb_tls_port, Config),
InitialConfig = influxdb_config(Host, Port, true, "verify_peer"),
ValidStatus = perform_tls_opts_check(PoolName, InitialConfig, valid),
?assertEqual(connected, ValidStatus),
InvalidStatus = perform_tls_opts_check(PoolName, InitialConfig, fail),
?assertEqual(disconnected, InvalidStatus),
ok.
perform_tls_opts_check(PoolName, InitialConfig, VerifyReturn) ->
{ok, #{config := CheckedConfig}} = {ok, #{config := CheckedConfig}} =
emqx_resource:check_config(?INFLUXDB_RESOURCE_MOD, InitialConfig), emqx_resource:check_config(?INFLUXDB_RESOURCE_MOD, InitialConfig),
% We need to add a write_syntax to the config since the connector % Meck handling of TLS opt handling so that we can inject custom
% expects this % verification returns
FullConfig = CheckedConfig#{write_syntax => influxdb_write_syntax()}, meck:new(emqx_tls_lib, [passthrough, no_link]),
{ok, #{ meck:expect(
config := #{ssl := #{enable := SslEnabled}}, emqx_tls_lib,
status := Status to_client_opts,
}} = emqx_resource:create_local( fun(Opts) ->
PoolName, Verify = {verify_fun, {custom_verify(), {return, VerifyReturn}}},
?CONNECTOR_RESOURCE_GROUP, [Verify | meck:passthrough([Opts])]
?INFLUXDB_RESOURCE_MOD, end
FullConfig,
#{}
), ),
?assert(SslEnabled), try
% Stop and remove the resource in one go. % We need to add a write_syntax to the config since the connector
?assertEqual(ok, emqx_resource:remove_local(PoolName)), % expects this
Status. FullConfig = CheckedConfig#{write_syntax => influxdb_write_syntax()},
{ok, #{
config := #{ssl := #{enable := SslEnabled}},
status := Status
}} = emqx_resource:create_local(
PoolName,
?CONNECTOR_RESOURCE_GROUP,
?INFLUXDB_RESOURCE_MOD,
FullConfig,
#{}
),
?assert(SslEnabled),
?assert(meck:validate(emqx_tls_lib)),
% Stop and remove the resource in one go.
?assertEqual(ok, emqx_resource:remove_local(PoolName)),
Status
after
meck:unload(emqx_tls_lib)
end.
% %%------------------------------------------------------------------------------ % %%------------------------------------------------------------------------------
% %% Helpers % %% Helpers
@ -178,6 +202,18 @@ influxdb_config(Host, Port, SslEnabled, Verify) ->
{ok, ResourceConfig} = hocon:binary(RawConfig), {ok, ResourceConfig} = hocon:binary(RawConfig),
#{<<"config">> => ResourceConfig}. #{<<"config">> => ResourceConfig}.
custom_verify() ->
fun
(_, {bad_cert, unknown_ca} = Event, {return, Return} = UserState) ->
ct:pal("Call to custom verify fun. Event: ~p UserState: ~p", [Event, UserState]),
{Return, UserState};
(_, Event, UserState) ->
ct:pal("Unexpected call to custom verify fun. Event: ~p UserState: ~p", [
Event, UserState
]),
{fail, unexpected_call_to_verify_fun}
end.
influxdb_write_syntax() -> influxdb_write_syntax() ->
[ [
#{ #{