fix(bridge): update the testcases for HTTP bridges
This commit is contained in:
parent
7d64013edd
commit
339749764d
|
@ -50,7 +50,7 @@ req_schema() ->
|
||||||
resp_schema() ->
|
resp_schema() ->
|
||||||
#{'oneOf' := Schema} = req_schema(),
|
#{'oneOf' := Schema} = req_schema(),
|
||||||
AddMetadata = fun(Prop) ->
|
AddMetadata = fun(Prop) ->
|
||||||
Prop#{is_connected => #{type => boolean},
|
Prop#{status => #{type => string, enum => [connected, disconnected, connecting]},
|
||||||
id => #{type => string},
|
id => #{type => string},
|
||||||
bridge_type => #{type => string, enum => ?TYPES},
|
bridge_type => #{type => string, enum => ?TYPES},
|
||||||
node => #{type => string}}
|
node => #{type => string}}
|
||||||
|
@ -206,9 +206,9 @@ crud_bridges(_, delete, #{bindings := #{id := Id}}) ->
|
||||||
|
|
||||||
manage_bridges(post, #{bindings := #{node := Node, id := Id, operation := Op}}) ->
|
manage_bridges(post, #{bindings := #{node := Node, id := Id, operation := Op}}) ->
|
||||||
OperFun =
|
OperFun =
|
||||||
fun (<<"start">>) -> start_bridge;
|
fun (<<"start">>) -> start;
|
||||||
(<<"stop">>) -> stop_bridge;
|
(<<"stop">>) -> stop;
|
||||||
(<<"restart">>) -> restart_bridge
|
(<<"restart">>) -> restart
|
||||||
end,
|
end,
|
||||||
?TRY_PARSE_ID(Id,
|
?TRY_PARSE_ID(Id,
|
||||||
case rpc_call(binary_to_atom(Node, latin1), emqx_bridge, OperFun(Op),
|
case rpc_call(binary_to_atom(Node, latin1), emqx_bridge, OperFun(Op),
|
||||||
|
@ -219,12 +219,12 @@ manage_bridges(post, #{bindings := #{node := Node, id := Id, operation := Op}})
|
||||||
end).
|
end).
|
||||||
|
|
||||||
format_resp(#{id := Id, raw_config := RawConf, resource_data := #{mod := Mod, status := Status}}) ->
|
format_resp(#{id := Id, raw_config := RawConf, resource_data := #{mod := Mod, status := Status}}) ->
|
||||||
IsConnected = fun(started) -> true; (_) -> false end,
|
IsConnected = fun(started) -> connected; (_) -> disconnected end,
|
||||||
RawConf#{
|
RawConf#{
|
||||||
id => Id,
|
id => Id,
|
||||||
node => node(),
|
node => node(),
|
||||||
bridge_type => emqx_bridge:bridge_type(Mod),
|
bridge_type => emqx_bridge:bridge_type(Mod),
|
||||||
is_connected => IsConnected(Status)
|
status => IsConnected(Status)
|
||||||
}.
|
}.
|
||||||
|
|
||||||
rpc_call(Node, Fun, Args) ->
|
rpc_call(Node, Fun, Args) ->
|
||||||
|
|
|
@ -55,8 +55,10 @@ init_per_testcase(_, Config) ->
|
||||||
end_per_testcase(_, _Config) ->
|
end_per_testcase(_, _Config) ->
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
-define(URL1, <<"http://localhost:9901/path1">>).
|
-define(URL(PORT, PATH), list_to_binary(
|
||||||
-define(URL2, <<"http://localhost:9901/path2">>).
|
io_lib:format("http://localhost:~s/~s",
|
||||||
|
[integer_to_list(PORT), PATH]))).
|
||||||
|
|
||||||
-define(HTTP_BRIDGE(URL),
|
-define(HTTP_BRIDGE(URL),
|
||||||
#{
|
#{
|
||||||
<<"url">> => URL,
|
<<"url">> => URL,
|
||||||
|
@ -73,11 +75,26 @@ end_per_testcase(_, _Config) ->
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% HTTP server for testing
|
%% HTTP server for testing
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
start_http_server(Port, HandleFun) ->
|
start_http_server(HandleFun) ->
|
||||||
|
Parent = self(),
|
||||||
spawn_link(fun() ->
|
spawn_link(fun() ->
|
||||||
{ok, Sock} = gen_tcp:listen(Port, [{active, false}]),
|
{Port, Sock} = listen_on_random_port(),
|
||||||
|
Parent ! {port, Port},
|
||||||
loop(Sock, HandleFun)
|
loop(Sock, HandleFun)
|
||||||
end).
|
end),
|
||||||
|
receive
|
||||||
|
{port, Port} -> Port
|
||||||
|
after
|
||||||
|
2000 -> error({timeout, start_http_server})
|
||||||
|
end.
|
||||||
|
|
||||||
|
listen_on_random_port() ->
|
||||||
|
Min = 1024, Max = 65000,
|
||||||
|
Port = rand:uniform(Max - Min) + Min,
|
||||||
|
case gen_tcp:listen(Port, [{active, false}, {reuseaddr, true}]) of
|
||||||
|
{ok, Sock} -> {Port, Sock};
|
||||||
|
{error, eaddrinuse} -> listen_on_random_port()
|
||||||
|
end.
|
||||||
|
|
||||||
loop(Sock, HandleFun) ->
|
loop(Sock, HandleFun) ->
|
||||||
{ok, Conn} = gen_tcp:accept(Sock),
|
{ok, Conn} = gen_tcp:accept(Sock),
|
||||||
|
@ -107,46 +124,50 @@ handle_fun_200_ok(Conn) ->
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
|
||||||
t_crud_apis(_) ->
|
t_crud_apis(_) ->
|
||||||
start_http_server(9901, fun handle_fun_200_ok/1),
|
Port = start_http_server(fun handle_fun_200_ok/1),
|
||||||
%% assert we there's no bridges at first
|
%% assert we there's no bridges at first
|
||||||
{ok, 200, <<"[]">>} = request(get, uri(["bridges"]), []),
|
{ok, 200, <<"[]">>} = request(get, uri(["bridges"]), []),
|
||||||
|
|
||||||
%% then we add a http bridge now
|
%% then we add a http bridge now
|
||||||
%% PUT /bridges/:id will create or update a bridge
|
%% PUT /bridges/:id will create or update a bridge
|
||||||
{ok, 200, Bridge} = request(put, uri(["bridges", "http:test_bridge"]), ?HTTP_BRIDGE(?URL1)),
|
URL1 = ?URL(Port, "path1"),
|
||||||
|
{ok, 200, Bridge} = request(put, uri(["bridges", "http:test_bridge"]),
|
||||||
|
?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">> := _
|
, <<"status">> := _
|
||||||
, <<"node">> := _
|
, <<"node">> := _
|
||||||
, <<"url">> := ?URL1
|
, <<"url">> := URL1
|
||||||
}], jsx:decode(Bridge)),
|
}], jsx:decode(Bridge)),
|
||||||
|
|
||||||
%% update the request-path of the bridge
|
%% update the request-path of the bridge
|
||||||
{ok, 200, Bridge2} = request(put, uri(["bridges", "http:test_bridge"]), ?HTTP_BRIDGE(?URL2)),
|
URL2 = ?URL(Port, "path2"),
|
||||||
|
{ok, 200, Bridge2} = request(put, uri(["bridges", "http:test_bridge"]),
|
||||||
|
?HTTP_BRIDGE(URL2)),
|
||||||
?assertMatch([ #{ <<"id">> := <<"http:test_bridge">>
|
?assertMatch([ #{ <<"id">> := <<"http:test_bridge">>
|
||||||
, <<"bridge_type">> := <<"http">>
|
, <<"bridge_type">> := <<"http">>
|
||||||
, <<"is_connected">> := _
|
, <<"status">> := _
|
||||||
, <<"node">> := _
|
, <<"node">> := _
|
||||||
, <<"url">> := ?URL2
|
, <<"url">> := URL2
|
||||||
}], jsx:decode(Bridge2)),
|
}], jsx:decode(Bridge2)),
|
||||||
|
|
||||||
%% list all bridges again, assert Bridge2 is in it
|
%% list all bridges again, assert Bridge2 is in it
|
||||||
{ok, 200, Bridge2Str} = request(get, uri(["bridges"]), []),
|
{ok, 200, Bridge2Str} = request(get, uri(["bridges"]), []),
|
||||||
?assertMatch([ #{ <<"id">> := <<"http:test_bridge">>
|
?assertMatch([ #{ <<"id">> := <<"http:test_bridge">>
|
||||||
, <<"bridge_type">> := <<"http">>
|
, <<"bridge_type">> := <<"http">>
|
||||||
, <<"is_connected">> := _
|
, <<"status">> := _
|
||||||
, <<"node">> := _
|
, <<"node">> := _
|
||||||
, <<"url">> := ?URL2
|
, <<"url">> := URL2
|
||||||
}], jsx:decode(Bridge2Str)),
|
}], jsx:decode(Bridge2Str)),
|
||||||
|
|
||||||
%% get the bridge by id
|
%% get the bridge by id
|
||||||
{ok, 200, Bridge3Str} = request(get, uri(["bridges", "http:test_bridge"]), []),
|
{ok, 200, Bridge3Str} = request(get, uri(["bridges", "http:test_bridge"]), []),
|
||||||
?assertMatch([#{ <<"id">> := <<"http:test_bridge">>
|
?assertMatch([#{ <<"id">> := <<"http:test_bridge">>
|
||||||
, <<"bridge_type">> := <<"http">>
|
, <<"bridge_type">> := <<"http">>
|
||||||
, <<"is_connected">> := _
|
, <<"status">> := _
|
||||||
, <<"node">> := _
|
, <<"node">> := _
|
||||||
, <<"url">> := ?URL2
|
, <<"url">> := URL2
|
||||||
}], jsx:decode(Bridge3Str)),
|
}], jsx:decode(Bridge3Str)),
|
||||||
|
|
||||||
%% delete the bridge
|
%% delete the bridge
|
||||||
|
@ -154,21 +175,53 @@ t_crud_apis(_) ->
|
||||||
{ok, 200, <<"[]">>} = request(get, uri(["bridges"]), []),
|
{ok, 200, <<"[]">>} = request(get, uri(["bridges"]), []),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
t_change_is_connnected_to_status() ->
|
|
||||||
error(not_implimented).
|
|
||||||
|
|
||||||
t_start_stop_bridges(_) ->
|
t_start_stop_bridges(_) ->
|
||||||
start_http_server(9901, fun handle_fun_200_ok/1),
|
Port = start_http_server(fun handle_fun_200_ok/1),
|
||||||
{ok, 200, Bridge} = request(put, uri(["bridges", "http:test_bridge"]), ?HTTP_BRIDGE(?URL1)),
|
URL1 = ?URL(Port, "abc"),
|
||||||
?assertMatch( #{ <<"id">> := <<"http:test_bridge">>
|
{ok, 200, Bridge} = request(put, uri(["bridges", "http:test_bridge"]), ?HTTP_BRIDGE(URL1)),
|
||||||
|
%ct:pal("the bridge ==== ~p", [Bridge]),
|
||||||
|
?assertMatch( [#{ <<"id">> := <<"http:test_bridge">>
|
||||||
, <<"bridge_type">> := <<"http">>
|
, <<"bridge_type">> := <<"http">>
|
||||||
, <<"is_connected">> := true
|
, <<"status">> := <<"connected">>
|
||||||
, <<"node">> := _
|
, <<"node">> := _
|
||||||
, <<"url">> := ?URL1
|
, <<"url">> := URL1
|
||||||
}, jsx:decode(Bridge)),
|
}], jsx:decode(Bridge)),
|
||||||
{ok, 200, <<>>} = request(put,
|
%% stop it
|
||||||
|
{ok, 200, <<>>} = request(post,
|
||||||
uri(["nodes", node(), "bridges", "http:test_bridge", "operation", "stop"]),
|
uri(["nodes", node(), "bridges", "http:test_bridge", "operation", "stop"]),
|
||||||
?HTTP_BRIDGE(?URL1)).
|
<<"">>),
|
||||||
|
{ok, 200, Bridge2} = request(get, uri(["bridges", "http:test_bridge"]), []),
|
||||||
|
?assertMatch([#{ <<"id">> := <<"http:test_bridge">>
|
||||||
|
, <<"status">> := <<"disconnected">>
|
||||||
|
}], jsx:decode(Bridge2)),
|
||||||
|
%% start again
|
||||||
|
{ok, 200, <<>>} = request(post,
|
||||||
|
uri(["nodes", node(), "bridges", "http:test_bridge", "operation", "start"]),
|
||||||
|
<<"">>),
|
||||||
|
{ok, 200, Bridge3} = request(get, uri(["bridges", "http:test_bridge"]), []),
|
||||||
|
?assertMatch([#{ <<"id">> := <<"http:test_bridge">>
|
||||||
|
, <<"status">> := <<"connected">>
|
||||||
|
}], jsx:decode(Bridge3)),
|
||||||
|
%% restart an already started bridge
|
||||||
|
{ok, 200, <<>>} = request(post,
|
||||||
|
uri(["nodes", node(), "bridges", "http:test_bridge", "operation", "restart"]),
|
||||||
|
<<"">>),
|
||||||
|
{ok, 200, Bridge3} = request(get, uri(["bridges", "http:test_bridge"]), []),
|
||||||
|
?assertMatch([#{ <<"id">> := <<"http:test_bridge">>
|
||||||
|
, <<"status">> := <<"connected">>
|
||||||
|
}], jsx:decode(Bridge3)),
|
||||||
|
%% stop it again
|
||||||
|
{ok, 200, <<>>} = request(post,
|
||||||
|
uri(["nodes", node(), "bridges", "http:test_bridge", "operation", "stop"]),
|
||||||
|
<<"">>),
|
||||||
|
%% restart a stopped bridge
|
||||||
|
{ok, 200, <<>>} = request(post,
|
||||||
|
uri(["nodes", node(), "bridges", "http:test_bridge", "operation", "restart"]),
|
||||||
|
<<"">>),
|
||||||
|
{ok, 200, Bridge4} = request(get, uri(["bridges", "http:test_bridge"]), []),
|
||||||
|
?assertMatch([#{ <<"id">> := <<"http:test_bridge">>
|
||||||
|
, <<"status">> := <<"connected">>
|
||||||
|
}], jsx:decode(Bridge4)).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% HTTP Request
|
%% HTTP Request
|
||||||
|
|
Loading…
Reference in New Issue