Skip to content

Commit

Permalink
Update big test helpers with the new listener config format
Browse files Browse the repository at this point in the history
  • Loading branch information
chrzaszcz committed Jan 12, 2022
1 parent 46b1f64 commit 183a787
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 58 deletions.
33 changes: 16 additions & 17 deletions big_tests/tests/domain_rest_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
patch_custom/4]).

%% Handler
-export([start_listener/0,
start_listener/1,
stop_listener/0]).
-export([start_listener/1,
stop_listener/1]).

-import(distributed_helper, [mim/0, mim2/0, require_rpc_nodes/1, rpc/4]).

Expand Down Expand Up @@ -98,21 +97,21 @@ patch_custom(Config, Role, Path, Body) ->
body => Body }).

%% REST handler setup
start_listener() ->
start_listener(#{}).

stop_listener() ->
PortIpNet = {?TEST_PORT, {127,0,0,1}, tcp},
ListenerModule = ejabberd_cowboy,
rpc(mim(), ejabberd_listener, stop_listener, [PortIpNet, ListenerModule]).

start_listener(Params) ->
PortIpNet = {?TEST_PORT, {127,0,0,1}, tcp},
ListenerModule = ejabberd_cowboy,
ListenerOpts = [{modules, [domain_handler(Params)]},
{transport_options, transport_options()}],
rpc(mim(), ejabberd_listener, start_listener,
[PortIpNet, ListenerModule, ListenerOpts]).
rpc(mim(), ejabberd_listener, start_listener, [listener_opts(Params)]).

stop_listener(Params) ->
rpc(mim(), ejabberd_listener, stop_listener, [listener_opts(Params)]).

listener_opts(Params) ->
#{port => ?TEST_PORT,
ip_tuple => {127, 0, 0, 1},
ip_address => "127.0.0.1",
ip_version => 4,
proto => tcp,
module => ejabberd_cowboy,
modules => [domain_handler(Params)],
transport_options => transport_options()}.

transport_options() ->
[{max_connections, 1024}, {num_acceptors, 10}].
Expand Down
17 changes: 9 additions & 8 deletions big_tests/tests/mongoose_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
-export([wait_for_pid_to_die/1]).
-export([supports_sasl_module/1]).
-export([auth_opts_with_password_format/1]).
-export([get_listener_opts/2]).
-export([restart_listener_with_opts/3]).
-export([get_listeners/2]).
-export([restart_listener/2]).
-export([should_minio_be_running/1]).
-export([new_mongoose_acc/1]).
-export([print_debug_info_for_module/1]).
Expand Down Expand Up @@ -492,14 +492,15 @@ build_new_password_opts(scram, PassOpts) ->
build_new_password_opts(Type, PassOpts) ->
PassOpts#{format => Type}.

get_listener_opts(#{} = Spec, Port) ->
get_listeners(#{} = Spec, Pattern) ->
Keys = maps:keys(Pattern),
Listeners = rpc(Spec, mongoose_config, get_opt, [listen]),
[Item || {{ListenerPort, _, _}, _, _} = Item <- Listeners, ListenerPort =:= Port].
lists:filter(fun(Listener) -> maps:with(Keys, Listener) =:= Pattern end, Listeners).

restart_listener_with_opts(Spec, Listener, NewOpts) ->
{PortIPProto, Module, _Opts} = Listener,
rpc(Spec, ejabberd_listener, stop_listener, [PortIPProto, Module]),
rpc(Spec, ejabberd_listener, start_listener, [PortIPProto, Module, NewOpts]).
%% 'port', 'ip_tuple' and 'proto' options need to stay unchanged for a successful restart
restart_listener(Spec, Listener) ->
rpc(Spec, ejabberd_listener, stop_listener, [Listener]),
rpc(Spec, ejabberd_listener, start_listener, [Listener]).

should_minio_be_running(Config) ->
case proplists:get_value(preset, Config, undefined) of
Expand Down
40 changes: 17 additions & 23 deletions big_tests/tests/rest_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -249,20 +249,14 @@ get_port(_Role, _Node, #{port := Port}) ->
Port;
get_port(Role, Node, _Params) ->
Listeners = rpc(Node, mongoose_config, get_opt, [listen]),
[{PortIpNet, ejabberd_cowboy, _Opts}] =
lists:filter(fun(Config) -> is_roles_config(Config, Role) end, Listeners),
case PortIpNet of
{Port, _Host, _Net} -> Port;
{Port, _Host} -> Port;
Port -> Port
end.
[#{port := Port}] = lists:filter(fun(Config) -> is_roles_config(Config, Role) end, Listeners),
Port.

-spec get_ssl_status(Role :: role(), Server :: distributed_helper:rpc_spec()) -> boolean().
get_ssl_status(Role, Node) ->
Listeners = rpc(Node, mongoose_config, get_opt, [listen]),
[{_PortIpNet, _Module, Opts}] =
lists:filter(fun (Opts) -> is_roles_config(Opts, Role) end, Listeners),
lists:keymember(ssl, 1, Opts).
[Opts] = lists:filter(fun (Opts) -> is_roles_config(Opts, Role) end, Listeners),
maps:is_key(ssl, Opts).

% @doc Changes the control credentials for admin by restarting the listener
% with new options.
Expand All @@ -274,22 +268,22 @@ change_admin_creds(Creds) ->
-spec stop_admin_listener() -> 'ok' | {'error', 'not_found' | 'restarting' | 'running' | 'simple_one_for_one'}.
stop_admin_listener() ->
Listeners = rpc(mim(), mongoose_config, get_opt, [listen]),
[{PortIpNet, Module, _Opts}] = lists:filter(fun (Opts) -> is_roles_config(Opts, admin) end, Listeners),
rpc(mim(), ejabberd_listener, stop_listener, [PortIpNet, Module]).
[Opts] = lists:filter(fun (Opts) -> is_roles_config(Opts, admin) end, Listeners),
rpc(mim(), ejabberd_listener, stop_listener, [Opts]).

-spec start_admin_listener(Creds :: {binary(), binary()}) -> {'error', pid()} | {'ok', _}.
start_admin_listener(Creds) ->
Listeners = rpc(mim(), mongoose_config, get_opt, [listen]),
[{PortIpNet, Module, Opts}] = lists:filter(fun (Opts) -> is_roles_config(Opts, admin) end, Listeners),
[Opts] = lists:filter(fun (Opts) -> is_roles_config(Opts, admin) end, Listeners),
NewOpts = insert_creds(Opts, Creds),
rpc(mim(), ejabberd_listener, start_listener, [PortIpNet, Module, NewOpts]).
rpc(mim(), ejabberd_listener, start_listener, [NewOpts]).

insert_creds(Opts, Creds) ->
Modules = proplists:get_value(modules, Opts),
insert_creds(Opts = #{modules := Modules}, Creds) ->
{Host, Path, mongoose_api_admin, PathOpts} = lists:keyfind(mongoose_api_admin, 3, Modules),
NewPathOpts = inject_creds_to_opts(PathOpts, Creds),
NewModules = lists:keyreplace(mongoose_api_admin, 3, Modules, {Host, Path, mongoose_api_admin, NewPathOpts}),
lists:keyreplace(modules, 1, Opts, {modules, NewModules}).
NewModules = lists:keyreplace(mongoose_api_admin, 3, Modules,
{Host, Path, mongoose_api_admin, NewPathOpts}),
Opts#{modules := NewModules}.

inject_creds_to_opts(PathOpts, any) ->
lists:keydelete(auth, 1, PathOpts);
Expand All @@ -305,12 +299,12 @@ inject_creds_to_opts(PathOpts, Creds) ->
% This is determined based on modules used. If there is any mongoose_api_admin module used,
% it is admin config. If not and there is at least one mongoose_api_client* module used,
% it's clients.
is_roles_config({_PortIpNet, ejabberd_cowboy, Opts}, admin) ->
{value, {modules, Modules}} = lists:keysearch(modules, 1, Opts),
is_roles_config(#{module := ejabberd_cowboy, modules := Modules}, admin) ->
lists:any(fun({_, _Path, Mod, _Args}) -> Mod == mongoose_api_admin; (_) -> false end, Modules);
is_roles_config({_PortIpNet, ejabberd_cowboy, Opts}, client) ->
{value, {modules, ModulesConfs}} = lists:keysearch(modules, 1, Opts),
ModulesTokens = lists:map(fun({_, _Path, Mod, _}) -> string:tokens(atom_to_list(Mod), "_"); (_) -> [] end, ModulesConfs),
is_roles_config(#{module := ejabberd_cowboy, modules := Modules}, client) ->
ModulesTokens = lists:map(fun({_, _Path, Mod, _}) -> string:tokens(atom_to_list(Mod), "_");
(_) -> []
end, Modules),
lists:any(fun(["mongoose", "client", "api" | _T]) -> true; (_) -> false end, ModulesTokens);
is_roles_config(_, _) -> false.

Expand Down
19 changes: 9 additions & 10 deletions big_tests/tests/s2s_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
require_rpc_nodes/1,
rpc/4]).

-include_lib("escalus/include/escalus.hrl").
-include_lib("common_test/include/ct.hrl").

-record(s2s_opts, {
Expand All @@ -28,12 +27,14 @@ init_s2s(Config) ->
Node1S2SCertfile = rpc(mim(), mongoose_config, get_opt, [s2s_certfile, undefined]),
Node1S2SUseStartTLS = rpc(mim(), mongoose_config, get_opt, [s2s_use_starttls, undefined]),
Node1S2SPort = ct:get_config({hosts, mim, incoming_s2s_port}),
[Node1S2SListener] = mongoose_helper:get_listener_opts(mim(), Node1S2SPort),
[Node1S2SListener] = mongoose_helper:get_listeners(mim(), #{port => Node1S2SPort,
module => ejabberd_s2s_in}),

Node2S2SCertfile = rpc(fed(), mongoose_config, get_opt, [s2s_certfile, undefined]),
Node2S2SUseStartTLS = rpc(fed(), mongoose_config, get_opt, [s2s_use_starttls, undefined]),
Node2S2SPort = ct:get_config({hosts, fed, incoming_s2s_port}),
[Node2S2SListener] = mongoose_helper:get_listener_opts(fed(), Node2S2SPort),
[Node2S2SListener] = mongoose_helper:get_listeners(fed(), #{port => Node2S2SPort,
module => ejabberd_s2s_in}),
S2S = #s2s_opts{node1_s2s_certfile = Node1S2SCertfile,
node1_s2s_use_starttls = Node1S2SUseStartTLS,
node1_s2s_listener = Node1S2SListener,
Expand Down Expand Up @@ -97,13 +98,13 @@ configure_s2s(node1_tls_required_node2_tls_false, Config) ->
Config;
configure_s2s(node1_tls_optional_node2_tls_required_trusted_with_cachain, Config) ->
S2S = ?config(s2s_opts, Config),
{S2SPortIPProto, Mod, Opts} = S2S#s2s_opts.node2_s2s_listener,
S2SListener = #{tls := TLSOpts} = S2S#s2s_opts.node2_s2s_listener,
CACertFile = filename:join([path_helper:repo_dir(Config),
"tools", "ssl", "ca", "cacert.pem"]),
NewOpts = [{cafile, CACertFile} | Opts],
NewTLSOpts = lists:keystore(cafile, 1, TLSOpts, {cafile, CACertFile}),
configure_s2s(S2S#s2s_opts{node2_s2s_use_starttls = required_trusted,
node2_s2s_listener = {S2SPortIPProto, Mod, NewOpts}
}),
node2_s2s_listener = S2SListener#{tls => NewTLSOpts}
}),
Config.

configure_s2s(#s2s_opts{node1_s2s_certfile = Certfile1,
Expand Down Expand Up @@ -137,6 +138,4 @@ restart_s2s(#{} = Spec, S2SListener) ->
[rpc(Spec, erlang, exit, [Pid, kill]) ||
{_, Pid, _, _} <- ChildrenIn],

{_PortIPProto, ejabberd_s2s_in, Opts} = S2SListener,
mongoose_helper:restart_listener_with_opts(Spec, S2SListener, Opts).

mongoose_helper:restart_listener(Spec, S2SListener).

0 comments on commit 183a787

Please sign in to comment.