test: refactor to use hocon and schema

This commit is contained in:
Thales Macedo Garitezi 2022-09-02 11:05:24 -03:00
parent 275171d217
commit f1048babd8
1 changed files with 106 additions and 85 deletions

View File

@ -40,15 +40,16 @@ init_per_group(Type = rs, Config) ->
MongoPort = list_to_integer(os:getenv("MONGO_RS_PORT", "27017")), MongoPort = list_to_integer(os:getenv("MONGO_RS_PORT", "27017")),
case emqx_common_test_helpers:is_tcp_server_available(MongoHost, MongoPort) of case emqx_common_test_helpers:is_tcp_server_available(MongoHost, MongoPort) of
true -> true ->
_ = application:load(emqx_ee_bridge), ensure_loaded(),
_ = emqx_ee_bridge:module_info(),
ok = emqx_common_test_helpers:start_apps([emqx_conf, emqx_bridge]), ok = emqx_common_test_helpers:start_apps([emqx_conf, emqx_bridge]),
emqx_mgmt_api_test_util:init_suite(), emqx_mgmt_api_test_util:init_suite(),
MongoConfig = mongo_config(MongoHost, MongoPort, Type), {Name, MongoConfig} = mongo_config(MongoHost, MongoPort, Type),
[ [
{mongo_host, MongoHost}, {mongo_host, MongoHost},
{mongo_port, MongoPort}, {mongo_port, MongoPort},
{mongo_config, MongoConfig} {mongo_config, MongoConfig},
{mongo_type, Type},
{mongo_name, Name}
| Config | Config
]; ];
false -> false ->
@ -59,15 +60,16 @@ init_per_group(Type = sharded, Config) ->
MongoPort = list_to_integer(os:getenv("MONGO_SHARDED_PORT", "27017")), MongoPort = list_to_integer(os:getenv("MONGO_SHARDED_PORT", "27017")),
case emqx_common_test_helpers:is_tcp_server_available(MongoHost, MongoPort) of case emqx_common_test_helpers:is_tcp_server_available(MongoHost, MongoPort) of
true -> true ->
_ = application:load(emqx_ee_bridge), ensure_loaded(),
_ = emqx_ee_bridge:module_info(),
ok = emqx_common_test_helpers:start_apps([emqx_conf, emqx_bridge]), ok = emqx_common_test_helpers:start_apps([emqx_conf, emqx_bridge]),
emqx_mgmt_api_test_util:init_suite(), emqx_mgmt_api_test_util:init_suite(),
MongoConfig = mongo_config(MongoHost, MongoPort, Type), {Name, MongoConfig} = mongo_config(MongoHost, MongoPort, Type),
[ [
{mongo_host, MongoHost}, {mongo_host, MongoHost},
{mongo_port, MongoPort}, {mongo_port, MongoPort},
{mongo_config, MongoConfig} {mongo_config, MongoConfig},
{mongo_type, Type},
{mongo_name, Name}
| Config | Config
]; ];
false -> false ->
@ -78,15 +80,16 @@ init_per_group(Type = single, Config) ->
MongoPort = list_to_integer(os:getenv("MONGO_SINGLE_PORT", "27017")), MongoPort = list_to_integer(os:getenv("MONGO_SINGLE_PORT", "27017")),
case emqx_common_test_helpers:is_tcp_server_available(MongoHost, MongoPort) of case emqx_common_test_helpers:is_tcp_server_available(MongoHost, MongoPort) of
true -> true ->
_ = application:load(emqx_ee_bridge), ensure_loaded(),
_ = emqx_ee_bridge:module_info(),
ok = emqx_common_test_helpers:start_apps([emqx_conf, emqx_bridge]), ok = emqx_common_test_helpers:start_apps([emqx_conf, emqx_bridge]),
emqx_mgmt_api_test_util:init_suite(), emqx_mgmt_api_test_util:init_suite(),
MongoConfig = mongo_config(MongoHost, MongoPort, Type), {Name, MongoConfig} = mongo_config(MongoHost, MongoPort, Type),
[ [
{mongo_host, MongoHost}, {mongo_host, MongoHost},
{mongo_port, MongoPort}, {mongo_port, MongoPort},
{mongo_config, MongoConfig} {mongo_config, MongoConfig},
{mongo_type, Type},
{mongo_name, Name}
| Config | Config
]; ];
false -> false ->
@ -118,65 +121,84 @@ end_per_testcase(_Testcase, Config) ->
%% Helper fns %% Helper fns
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
mongo_config(MongoHost0, MongoPort0, rs) -> ensure_loaded() ->
MongoHost = list_to_binary(MongoHost0), _ = application:load(emqx_ee_bridge),
MongoPort = integer_to_binary(MongoPort0), _ = emqx_ee_bridge:module_info(),
Servers = <<MongoHost/binary, ":", MongoPort/binary>>, ok.
Name = atom_to_binary(?MODULE),
#{
<<"type">> => <<"mongodb_rs">>,
<<"name">> => Name,
<<"enable">> => true,
<<"collection">> => <<"mycol">>,
<<"servers">> => Servers,
<<"database">> => <<"mqtt">>,
<<"w_mode">> => <<"safe">>,
<<"replica_set_name">> => <<"rs0">>
};
mongo_config(MongoHost0, MongoPort0, sharded) ->
MongoHost = list_to_binary(MongoHost0),
MongoPort = integer_to_binary(MongoPort0),
Servers = <<MongoHost/binary, ":", MongoPort/binary>>,
Name = atom_to_binary(?MODULE),
#{
<<"type">> => <<"mongodb_sharded">>,
<<"name">> => Name,
<<"enable">> => true,
<<"collection">> => <<"mycol">>,
<<"servers">> => Servers,
<<"database">> => <<"mqtt">>,
<<"w_mode">> => <<"safe">>
};
mongo_config(MongoHost0, MongoPort0, single) ->
MongoHost = list_to_binary(MongoHost0),
MongoPort = integer_to_binary(MongoPort0),
Server = <<MongoHost/binary, ":", MongoPort/binary>>,
Name = atom_to_binary(?MODULE),
#{
<<"type">> => <<"mongodb_single">>,
<<"name">> => Name,
<<"enable">> => true,
<<"collection">> => <<"mycol">>,
<<"server">> => Server,
<<"database">> => <<"mqtt">>,
<<"w_mode">> => <<"safe">>
}.
create_bridge(Config0 = #{<<"type">> := Type, <<"name">> := Name}) -> mongo_type_bin(rs) ->
Config = maps:without( <<"mongodb_rs">>;
[ mongo_type_bin(sharded) ->
<<"type">>, <<"mongodb_sharded">>;
<<"name">> mongo_type_bin(single) ->
], <<"mongodb_single">>.
Config0
), mongo_config(MongoHost, MongoPort0, rs = Type) ->
emqx_bridge:create(Type, Name, Config). MongoPort = integer_to_list(MongoPort0),
Servers = MongoHost ++ ":" ++ MongoPort,
Name = atom_to_binary(?MODULE),
ConfigString =
io_lib:format(
"bridges.mongodb_rs.~s {\n"
" enable = true\n"
" collection = mycol\n"
" replica_set_name = rs0\n"
" servers = [~p]\n"
" w_mode = safe\n"
" database = mqtt\n"
"}",
[Name, Servers]
),
{Name, parse_and_check(ConfigString, Type, Name)};
mongo_config(MongoHost, MongoPort0, sharded = Type) ->
MongoPort = integer_to_list(MongoPort0),
Servers = MongoHost ++ ":" ++ MongoPort,
Name = atom_to_binary(?MODULE),
ConfigString =
io_lib:format(
"bridges.mongodb_sharded.~s {\n"
" enable = true\n"
" collection = mycol\n"
" servers = [~p]\n"
" w_mode = safe\n"
" database = mqtt\n"
"}",
[Name, Servers]
),
{Name, parse_and_check(ConfigString, Type, Name)};
mongo_config(MongoHost, MongoPort0, single = Type) ->
MongoPort = integer_to_list(MongoPort0),
Server = MongoHost ++ ":" ++ MongoPort,
Name = atom_to_binary(?MODULE),
ConfigString =
io_lib:format(
"bridges.mongodb_single.~s {\n"
" enable = true\n"
" collection = mycol\n"
" server = ~p\n"
" w_mode = safe\n"
" database = mqtt\n"
"}",
[Name, Server]
),
{Name, parse_and_check(ConfigString, Type, Name)}.
parse_and_check(ConfigString, Type, Name) ->
{ok, RawConf} = hocon:binary(ConfigString, #{format => map}),
TypeBin = mongo_type_bin(Type),
hocon_tconf:check_plain(emqx_bridge_schema, RawConf, #{required => false, atom_key => false}),
#{<<"bridges">> := #{TypeBin := #{Name := Config}}} = RawConf,
Config.
create_bridge(Config) ->
Type = mongo_type_bin(?config(mongo_type, Config)),
Name = ?config(mongo_name, Config),
MongoConfig = ?config(mongo_config, Config),
emqx_bridge:create(Type, Name, MongoConfig).
delete_bridge(Config) -> delete_bridge(Config) ->
#{ Type = mongo_type_bin(?config(mongo_type, Config)),
<<"type">> := Type, Name = ?config(mongo_name, Config),
<<"name">> := Name
} = ?config(mongo_config, Config),
emqx_bridge:remove(Type, Name). emqx_bridge:remove(Type, Name).
create_bridge_http(Params) -> create_bridge_http(Params) ->
@ -188,11 +210,9 @@ create_bridge_http(Params) ->
end. end.
clear_db(Config) -> clear_db(Config) ->
#{ Type = mongo_type_bin(?config(mongo_type, Config)),
<<"name">> := Name, Name = ?config(mongo_name, Config),
<<"type">> := Type, #{<<"collection">> := Collection} = ?config(mongo_config, Config),
<<"collection">> := Collection
} = ?config(mongo_config, Config),
ResourceID = emqx_bridge_resource:resource_id(Type, Name), ResourceID = emqx_bridge_resource:resource_id(Type, Name),
{ok, _, #{state := #{poolname := PoolName}}} = emqx_resource:get_instance(ResourceID), {ok, _, #{state := #{poolname := PoolName}}} = emqx_resource:get_instance(ResourceID),
Selector = #{}, Selector = #{},
@ -202,19 +222,15 @@ clear_db(Config) ->
ok. ok.
find_all(Config) -> find_all(Config) ->
#{ Type = mongo_type_bin(?config(mongo_type, Config)),
<<"name">> := Name, Name = ?config(mongo_name, Config),
<<"type">> := Type, #{<<"collection">> := Collection} = ?config(mongo_config, Config),
<<"collection">> := Collection
} = ?config(mongo_config, Config),
ResourceID = emqx_bridge_resource:resource_id(Type, Name), ResourceID = emqx_bridge_resource:resource_id(Type, Name),
emqx_resource:query(ResourceID, {find, Collection, #{}, #{}}). emqx_resource:query(ResourceID, {find, Collection, #{}, #{}}).
send_message(Config, Payload) -> send_message(Config, Payload) ->
#{ Name = ?config(mongo_name, Config),
<<"name">> := Name, Type = mongo_type_bin(?config(mongo_type, Config)),
<<"type">> := Type
} = ?config(mongo_config, Config),
BridgeID = emqx_bridge_resource:bridge_id(Type, Name), BridgeID = emqx_bridge_resource:bridge_id(Type, Name),
emqx_bridge:send_message(BridgeID, Payload). emqx_bridge:send_message(BridgeID, Payload).
@ -223,10 +239,9 @@ send_message(Config, Payload) ->
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
t_setup_via_config_and_publish(Config) -> t_setup_via_config_and_publish(Config) ->
MongoConfig = ?config(mongo_config, Config),
?assertMatch( ?assertMatch(
{ok, _}, {ok, _},
create_bridge(MongoConfig) create_bridge(Config)
), ),
Val = erlang:unique_integer(), Val = erlang:unique_integer(),
ok = send_message(Config, #{key => Val}), ok = send_message(Config, #{key => Val}),
@ -237,7 +252,13 @@ t_setup_via_config_and_publish(Config) ->
ok. ok.
t_setup_via_http_api_and_publish(Config) -> t_setup_via_http_api_and_publish(Config) ->
MongoConfig = ?config(mongo_config, Config), Type = mongo_type_bin(?config(mongo_type, Config)),
Name = ?config(mongo_name, Config),
MongoConfig0 = ?config(mongo_config, Config),
MongoConfig = MongoConfig0#{
<<"name">> => Name,
<<"type">> => Type
},
?assertMatch( ?assertMatch(
{ok, _}, {ok, _},
create_bridge_http(MongoConfig) create_bridge_http(MongoConfig)