fix(bridge): update testcases for bridge APIs

This commit is contained in:
Shawn 2021-10-26 16:52:15 +08:00
parent 7996d8f709
commit e41736f5cb
3 changed files with 76 additions and 33 deletions

View File

@ -132,7 +132,7 @@ start_apps(Apps) ->
start_apps(Apps, Handler) when is_function(Handler) -> start_apps(Apps, Handler) when is_function(Handler) ->
%% Load all application code to beam vm first %% Load all application code to beam vm first
%% Because, minirest, ekka etc.. application will scan these modules %% Because, minirest, ekka etc.. application will scan these modules
lists:foreach(fun load/1, [emqx | Apps]), lists:foreach(fun load/1, [emqx_machine, emqx_conf, emqx | Apps]),
ekka:start(), ekka:start(),
lists:foreach(fun(App) -> start_app(App, Handler) end, [emqx | Apps]). lists:foreach(fun(App) -> start_app(App, Handler) end, [emqx | Apps]).

View File

@ -199,7 +199,7 @@ update(Type, Name, {_OldConf, Conf}) ->
?SLOG(info, #{msg => "update bridge", type => Type, name => Name, ?SLOG(info, #{msg => "update bridge", type => Type, name => Name,
config => Conf}), config => Conf}),
emqx_resource:recreate(resource_id(Type, Name), emqx_resource:recreate(resource_id(Type, Name),
emqx_bridge:resource_type(Type), Conf, []). emqx_bridge:resource_type(Type), parse_confs(Type, Conf), []).
remove(Type, Name, _Conf) -> remove(Type, Name, _Conf) ->
?SLOG(info, #{msg => "remove bridge", type => Type, name => Name}), ?SLOG(info, #{msg => "remove bridge", type => Type, name => Name}),

View File

@ -23,21 +23,30 @@
-define(CONF_DEFAULT, <<"bridges: {}">>). -define(CONF_DEFAULT, <<"bridges: {}">>).
all() -> all() ->
emqx_ct:all(?MODULE). emqx_common_test_helpers:all(?MODULE).
groups() -> groups() ->
[]. [].
suite() ->
[{timetrap,{seconds,30}}].
init_per_suite(Config) -> init_per_suite(Config) ->
application:load(emqx_machine), ok = emqx_config:put([emqx_dashboard], #{
ok = ekka:start(), default_username => <<"admin">>,
ok = emqx_ct_helpers:start_apps([emqx_bridge]), default_password => <<"public">>,
listeners => [#{
protocol => http,
port => 18083
}]
}),
ok = emqx_common_test_helpers:start_apps([emqx_bridge, emqx_dashboard]),
ok = emqx_config:init_load(emqx_bridge_schema, ?CONF_DEFAULT), ok = emqx_config:init_load(emqx_bridge_schema, ?CONF_DEFAULT),
Config. Config.
end_per_suite(_Config) -> end_per_suite(_Config) ->
ok = ekka:stop(), ok = ekka:stop(),
emqx_ct_helpers:stop_apps([emqx_bridge]), emqx_common_test_helpers:stop_apps([emqx_bridge, emqx_dashboard]),
ok. ok.
init_per_testcase(_, Config) -> init_per_testcase(_, Config) ->
@ -98,40 +107,74 @@ handle_fun_200_ok(Conn) ->
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
t_crud_apis(_) -> t_crud_apis(_) ->
start_http_server(9901, fun handle_fun_200_ok/1),
%% assert we there's no bridges at first %% assert we there's no bridges at first
{200, []} = emqx_bridge_api:list_bridges(get, #{}), {ok, 200, <<"[]">>} = request(get, uri(["bridges"]), []),
%% then we add a http bridge now %% then we add a http bridge now
{200, [Bridge]} = emqx_bridge_api:crud_bridges_cluster(put, {ok, 200, Bridge} = request(put, uri(["bridges", "http:test_bridge"]), ?HTTP_BRIDGE(?URL1)),
#{ bindings => #{id => <<"http:test_bridge">>}
, body => ?HTTP_BRIDGE(?URL1)
}),
%ct:pal("---bridge: ~p", [Bridge]), %ct:pal("---bridge: ~p", [Bridge]),
?assertMatch(#{ id := <<"http:test_bridge">> ?assertMatch([ #{ <<"id">> := <<"http:test_bridge">>
, bridge_type := http , <<"bridge_type">> := <<"http">>
, is_connected := _ , <<"is_connected">> := _
, node := _ , <<"node">> := _
, <<"url">> := ?URL1 , <<"url">> := ?URL1
}, Bridge), }], jsx:decode(Bridge)),
%% update the request-path of the bridge %% update the request-path of the bridge
{200, [Bridge2]} = emqx_bridge_api:crud_bridges_cluster(put, {ok, 200, Bridge2} = request(put, uri(["bridges", "http:test_bridge"]), ?HTTP_BRIDGE(?URL2)),
#{ bindings => #{id => <<"http:test_bridge">>} ?assertMatch([ #{ <<"id">> := <<"http:test_bridge">>
, body => ?HTTP_BRIDGE(?URL2) , <<"bridge_type">> := <<"http">>
}), , <<"is_connected">> := _
?assertMatch(#{ id := <<"http:test_bridge">> , <<"node">> := _
, bridge_type := http , <<"url">> := ?URL2
, is_connected := _ }], jsx:decode(Bridge2)),
, <<"url">> := ?URL2
}, Bridge2),
%% list all bridges again, assert Bridge2 is in it %% list all bridges again, assert Bridge2 is in it
{200, [Bridge2]} = emqx_bridge_api:list_bridges(get, #{}), {ok, 200, Bridge2Str} = request(get, uri(["bridges"]), []),
?assertMatch([ #{ <<"id">> := <<"http:test_bridge">>
, <<"bridge_type">> := <<"http">>
, <<"is_connected">> := _
, <<"node">> := _
, <<"url">> := ?URL2
}], jsx:decode(Bridge2Str)),
%% delete teh bridge %% delete the bridge
{200} = emqx_bridge_api:crud_bridges_cluster(delete, {ok,200,<<>>} = request(delete, uri(["bridges", "http:test_bridge"]), []),
#{ bindings => #{id => <<"http:test_bridge">>} {ok, 200, <<"[]">>} = request(get, uri(["bridges"]), []),
}),
{200, []} = emqx_bridge_api:list_bridges(get, #{}),
ok. ok.
%%--------------------------------------------------------------------
%% HTTP Request
%%--------------------------------------------------------------------
-define(HOST, "http://127.0.0.1:18083/").
-define(API_VERSION, "v5").
-define(BASE_PATH, "api").
request(Method, Url, Body) ->
Request = case Body of
[] -> {Url, [auth_header_()]};
_ -> {Url, [auth_header_()], "application/json", jsx:encode(Body)}
end,
ct:pal("Method: ~p, Request: ~p", [Method, Request]),
case httpc:request(Method, Request, [], [{body_format, binary}]) of
{error, socket_closed_remotely} ->
{error, socket_closed_remotely};
{ok, {{"HTTP/1.1", Code, _}, _Headers, Return} } ->
{ok, Code, Return};
{ok, {Reason, _, _}} ->
{error, Reason}
end.
uri() -> uri([]).
uri(Parts) when is_list(Parts) ->
NParts = [E || E <- Parts],
?HOST ++ filename:join([?BASE_PATH, ?API_VERSION | NParts]).
auth_header_() ->
Username = <<"admin">>,
Password = <<"public">>,
{ok, Token} = emqx_dashboard_admin:sign_token(Username, Password),
{"Authorization", "Bearer " ++ binary_to_list(Token)}.