test: add actual test for haproxy

This commit is contained in:
Stefan Strigler 2023-01-30 18:07:06 +01:00
parent 6bde1173af
commit 0b1483040a
1 changed files with 57 additions and 40 deletions

View File

@ -19,31 +19,12 @@
-compile(nowarn_export_all). -compile(nowarn_export_all).
-compile(export_all). -compile(export_all).
-import(
emqx_common_test_http,
[
request_api/3
]
).
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
-include("emqx_dashboard.hrl"). -include("emqx_dashboard.hrl").
-define(HOST, "http://127.0.0.1:18083").
-define(BASE_PATH, "/").
all() -> all() ->
emqx_common_test_helpers:all(?MODULE). emqx_common_test_helpers:all(?MODULE).
end_suite() ->
end_suite([]).
end_suite(Apps) ->
application:unload(emqx_management),
mnesia:clear_table(?ADMIN),
emqx_common_test_helpers:stop_apps(Apps ++ [emqx_dashboard]).
init_per_suite(Config) -> init_per_suite(Config) ->
emqx_common_test_helpers:start_apps( emqx_common_test_helpers:start_apps(
[emqx_management, emqx_dashboard], [emqx_management, emqx_dashboard],
@ -51,33 +32,69 @@ init_per_suite(Config) ->
), ),
Config. Config.
end_per_suite(_Config) ->
emqx_common_test_helpers:stop_apps([emqx_dashboard, emqx_management]),
mria:stop().
set_special_configs(emqx_dashboard) -> set_special_configs(emqx_dashboard) ->
emqx_dashboard_api_test_helpers:set_default_config(<<"admin">>, true), emqx_dashboard_api_test_helpers:set_default_config(<<"admin">>, true),
ok; ok;
set_special_configs(_) -> set_special_configs(_) ->
ok. ok.
disabled_t_status(_) -> end_per_suite(Config) ->
%% no easy way since httpc doesn't support emulating the haproxy protocol application:unload(emqx_management),
{ok, 200, _Res} = http_get(["status"]), mnesia:clear_table(?ADMIN),
emqx_common_test_helpers:stop_apps([emqx_dashboard, emqx_management]),
mria:stop(),
Config.
t_status(_Config) ->
ProxyInfo = #{
version => 1,
command => proxy,
transport_family => ipv4,
transport_protocol => stream,
src_address => {127, 0, 0, 1},
src_port => 444,
dest_address => {192, 168, 0, 1},
dest_port => 443
},
{ok, Socket} = gen_tcp:connect(
"localhost",
18083,
[binary, {active, false}, {packet, raw}]
),
ok = gen_tcp:send(Socket, ranch_proxy_header:header(ProxyInfo)),
{ok, Token} = emqx_dashboard_admin:sign_token(<<"admin">>, <<"public">>),
ok = gen_tcp:send(
Socket,
"GET /status HTTP/1.1\r\n"
"Host: localhost\r\n"
"Authorization: Bearer " ++ binary_to_list(Token) ++
"\r\n"
"\r\n"
),
{_, 200, _, Rest0} = cow_http:parse_status_line(raw_recv_head(Socket)),
{Headers, Body0} = cow_http:parse_headers(Rest0),
{_, LenBin} = lists:keyfind(<<"content-length">>, 1, Headers),
Len = binary_to_integer(LenBin),
Body =
if
byte_size(Body0) =:= Len ->
Body0;
true ->
{ok, Body1} = gen_tcp:recv(Socket, Len - byte_size(Body0), 5000),
<<Body0/bits, Body1/bits>>
end,
?assertMatch({match, _}, re:run(Body, "Node .+ is started\nemqx is running")),
ok. ok.
%%------------------------------------------------------------------------------ raw_recv_head(Socket) ->
%% Internal functions {ok, Data} = gen_tcp:recv(Socket, 0, 10000),
%%------------------------------------------------------------------------------ raw_recv_head(Socket, Data).
http_get(Parts) ->
request_api(get, api_path(Parts), auth_header_()).
auth_header_() -> raw_recv_head(Socket, Buffer) ->
auth_header_(<<"admin">>, <<"public">>). case binary:match(Buffer, <<"\r\n\r\n">>) of
nomatch ->
auth_header_(Username, Password) -> {ok, Data} = gen_tcp:recv(Socket, 0, 10000),
{ok, Token} = emqx_dashboard_admin:sign_token(Username, Password), raw_recv_head(Socket, <<Buffer/binary, Data/binary>>);
{"Authorization", "Bearer " ++ binary_to_list(Token)}. {_, _} ->
Buffer
api_path(Parts) -> end.
?HOST ++ filename:join([?BASE_PATH | Parts]).