test(emqx_bridge_cassandra): add connector test case for Cassandra configured without authentication

This commit is contained in:
Serge Tupchii 2023-09-01 16:55:53 +03:00
parent f0c75d97e1
commit 607705518b
4 changed files with 1314 additions and 53 deletions

View File

@ -1,4 +0,0 @@
ARG CASSANDRA_TAG=3.11.6
FROM cassandra:${CASSANDRA_TAG}
COPY cassandra.yaml /etc/cassandra/cassandra.yaml
CMD ["cassandra", "-f"]

File diff suppressed because it is too large Load Diff

View File

@ -1,21 +1,13 @@
version: '3.9' version: '3.9'
services: x-cassandra: &cassandra
cassandra_server:
container_name: cassandra
build:
context: ./cassandra
args:
CASSANDRA_TAG: ${CASSANDRA_TAG}
image: emqx-cassandra
restart: always restart: always
image: cassandra:${CASSANDRA_TAG:-3.11.6}
environment: environment:
CASSANDRA_BROADCAST_ADDRESS: "1.2.3.4" CASSANDRA_BROADCAST_ADDRESS: "1.2.3.4"
CASSANDRA_RPC_ADDRESS: "0.0.0.0" CASSANDRA_RPC_ADDRESS: "0.0.0.0"
HEAP_NEWSIZE: "128M" HEAP_NEWSIZE: "128M"
MAX_HEAP_SIZE: "2048M" MAX_HEAP_SIZE: "2048M"
volumes:
- ./certs:/certs
#ports: #ports:
# - "9042:9042" # - "9042:9042"
# - "9142:9142" # - "9142:9142"
@ -30,3 +22,17 @@ services:
tail -f /cassandra.log tail -f /cassandra.log
networks: networks:
- emqx_bridge - emqx_bridge
services:
cassandra_server:
<<: *cassandra
container_name: cassandra
volumes:
- ./certs:/certs
- ./cassandra/cassandra.yaml:/etc/cassandra/cassandra.yaml
cassandra_noauth_server:
<<: *cassandra
container_name: cassandra_noauth
volumes:
- ./certs:/certs
- ./cassandra/cassandra_noauth.yaml:/etc/cassandra/cassandra.yaml

View File

@ -7,15 +7,17 @@
-compile(nowarn_export_all). -compile(nowarn_export_all).
-compile(export_all). -compile(export_all).
-include_lib("common_test/include/ct.hrl").
-include("emqx_bridge_cassandra.hrl"). -include("emqx_bridge_cassandra.hrl").
-include("emqx_connector/include/emqx_connector.hrl"). -include("emqx_connector/include/emqx_connector.hrl").
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
-include_lib("emqx/include/emqx.hrl"). -include_lib("emqx/include/emqx.hrl").
-include_lib("stdlib/include/assert.hrl"). -include_lib("stdlib/include/assert.hrl").
%% Cassandra server defined at `.ci/docker-compose-file/docker-compose-cassandra-tcp.yaml` %% Cassandra servers are defined at `.ci/docker-compose-file/docker-compose-cassandra.yaml`
%% You can change it to `127.0.0.1`, if you run this SUITE locally %% You can change it to `127.0.0.1`, if you run this SUITE locally
-define(CASSANDRA_HOST, "cassandra"). -define(CASSANDRA_HOST, "cassandra").
-define(CASSANDRA_HOST_NOAUTH, "cassandra_noauth").
-define(CASSANDRA_RESOURCE_MOD, emqx_bridge_cassandra_connector). -define(CASSANDRA_RESOURCE_MOD, emqx_bridge_cassandra_connector).
%% This test SUITE requires a running cassandra instance. If you don't want to %% This test SUITE requires a running cassandra instance. If you don't want to
@ -32,40 +34,58 @@
-define(CASSA_PASSWORD, <<"cassandra">>). -define(CASSA_PASSWORD, <<"cassandra">>).
all() -> all() ->
emqx_common_test_helpers:all(?MODULE). [
{group, auth},
{group, noauth}
].
groups() -> groups() ->
[]. TCs = emqx_common_test_helpers:all(?MODULE),
[
{auth, TCs},
{noauth, TCs}
].
cassandra_servers() -> cassandra_servers(CassandraHost) ->
lists:map( lists:map(
fun(#{hostname := Host, port := Port}) -> fun(#{hostname := Host, port := Port}) ->
{Host, Port} {Host, Port}
end, end,
emqx_schema:parse_servers( emqx_schema:parse_servers(
iolist_to_binary([?CASSANDRA_HOST, ":", erlang:integer_to_list(?CASSANDRA_DEFAULT_PORT)]), iolist_to_binary([CassandraHost, ":", erlang:integer_to_list(?CASSANDRA_DEFAULT_PORT)]),
#{default_port => ?CASSANDRA_DEFAULT_PORT} #{default_port => ?CASSANDRA_DEFAULT_PORT}
) )
). ).
init_per_suite(Config) -> init_per_suite(Config) ->
case
emqx_common_test_helpers:is_tcp_server_available(?CASSANDRA_HOST, ?CASSANDRA_DEFAULT_PORT)
of
true ->
ok = emqx_common_test_helpers:start_apps([emqx_conf]), ok = emqx_common_test_helpers:start_apps([emqx_conf]),
ok = emqx_connector_test_helpers:start_apps([emqx_resource]), ok = emqx_connector_test_helpers:start_apps([emqx_resource]),
{ok, _} = application:ensure_all_started(emqx_connector), {ok, _} = application:ensure_all_started(emqx_connector),
Config.
init_per_group(Group, Config) ->
{CassandraHost, AuthOpts} =
case Group of
auth ->
{?CASSANDRA_HOST, [{username, ?CASSA_USERNAME}, {password, ?CASSA_PASSWORD}]};
noauth ->
{?CASSANDRA_HOST_NOAUTH, []}
end,
case emqx_common_test_helpers:is_tcp_server_available(CassandraHost, ?CASSANDRA_DEFAULT_PORT) of
true ->
%% keyspace `mqtt` must be created in advance %% keyspace `mqtt` must be created in advance
{ok, Conn} = {ok, Conn} =
ecql:connect([ ecql:connect([
{nodes, cassandra_servers()}, {nodes, cassandra_servers(CassandraHost)},
{username, ?CASSA_USERNAME},
{password, ?CASSA_PASSWORD},
{keyspace, "mqtt"} {keyspace, "mqtt"}
| AuthOpts
]), ]),
ecql:close(Conn), ecql:close(Conn),
Config; [
{cassa_host, CassandraHost},
{cassa_auth_opts, AuthOpts}
| Config
];
false -> false ->
case os:getenv("IS_CI") of case os:getenv("IS_CI") of
"yes" -> "yes" ->
@ -75,6 +95,9 @@ init_per_suite(Config) ->
end end
end. end.
end_per_group(_Group, _Config) ->
ok.
end_per_suite(_Config) -> end_per_suite(_Config) ->
ok = emqx_common_test_helpers:stop_apps([emqx_conf]), ok = emqx_common_test_helpers:stop_apps([emqx_conf]),
ok = emqx_connector_test_helpers:stop_apps([emqx_resource]), ok = emqx_connector_test_helpers:stop_apps([emqx_resource]),
@ -90,10 +113,10 @@ end_per_testcase(_, _Config) ->
%% cases %% cases
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
t_lifecycle(_Config) -> t_lifecycle(Config) ->
perform_lifecycle_check( perform_lifecycle_check(
<<"emqx_connector_cassandra_SUITE">>, <<"emqx_connector_cassandra_SUITE">>,
cassandra_config() cassandra_config(Config)
). ).
show(X) -> show(X) ->
@ -168,25 +191,25 @@ perform_lifecycle_check(ResourceId, InitialConfig) ->
%% utils %% utils
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
cassandra_config() -> cassandra_config(Config) ->
Config = Host = ?config(cassa_host, Config),
#{ AuthOpts = maps:from_list(?config(cassa_auth_opts, Config)),
CassConfig =
AuthOpts#{
auto_reconnect => true, auto_reconnect => true,
keyspace => <<"mqtt">>, keyspace => <<"mqtt">>,
username => ?CASSA_USERNAME,
password => ?CASSA_PASSWORD,
pool_size => 8, pool_size => 8,
servers => iolist_to_binary( servers => iolist_to_binary(
io_lib:format( io_lib:format(
"~s:~b", "~s:~b",
[ [
?CASSANDRA_HOST, Host,
?CASSANDRA_DEFAULT_PORT ?CASSANDRA_DEFAULT_PORT
] ]
) )
) )
}, },
#{<<"config">> => Config}. #{<<"config">> => CassConfig}.
test_query_no_params() -> test_query_no_params() ->
{query, <<"SELECT count(1) AS T FROM system.local">>}. {query, <<"SELECT count(1) AS T FROM system.local">>}.