Skip to content

Commit 3499d3f

Browse files
authored
Merge pull request #4593 from esl/tomasz/mim-2509-small_tests-redis
[CI] Enable using Redis TLS connection in small_tests
2 parents 1be94d0 + 02fbe7b commit 3499d3f

File tree

1 file changed

+52
-11
lines changed

1 file changed

+52
-11
lines changed

test/ejabberd_sm_SUITE.erl

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
-define(B(C), (proplists:get_value(backend, C))).
1212
-define(MAX_USER_SESSIONS, 2).
1313

14-
-import(config_parser_helper, [default_config/1]).
14+
-import(config_parser_helper, [config/2, default_config/1]).
1515

1616
all() -> [{group, mnesia}, {group, redis}, {group, cets}].
1717

@@ -79,13 +79,13 @@ init_per_group(cets, Config) ->
7979
{ok, Pid} = cets_discovery:start(DiscoOpts),
8080
[{backend, ejabberd_sm_cets}, {cets_disco_pid, Pid} | Config].
8181

82-
init_redis_group(true, Config) ->
82+
init_redis_group({true, ConnType}, Config) ->
8383
Self = self(),
8484
proc_lib:spawn(fun() ->
8585
register(test_helper, self()),
8686
mongoose_wpool:ensure_started(),
8787
% This would be started via outgoing_pools in normal case
88-
Pool = default_config([outgoing_pools, redis, default]),
88+
Pool = config([outgoing_pools, redis, default], redis_pool_config(ConnType)),
8989
mongoose_wpool:start_configured_pools([Pool], [], []),
9090
Self ! ready,
9191
receive stop -> ok end
@@ -95,6 +95,11 @@ init_redis_group(true, Config) ->
9595
init_redis_group(_, _) ->
9696
{skip, "redis not running"}.
9797

98+
redis_pool_config(plain) ->
99+
#{};
100+
redis_pool_config(tls) ->
101+
#{conn_opts => #{tls => #{verify_mode => none}}}.
102+
98103
end_per_group(mnesia, Config) ->
99104
mnesia:stop(),
100105
mnesia:delete_schema([node()]),
@@ -675,17 +680,53 @@ n(Node) ->
675680

676681

677682
is_redis_running() ->
683+
ct:log("Checking if Redis is running..."),
684+
% Try plain connection first (for local development)
678685
case eredis:start_link([{host, "127.0.0.1"}]) of
679686
{ok, Client} ->
680-
Result = eredis:q(Client, [<<"PING">>], 5000),
681-
eredis:stop(Client),
682-
case Result of
683-
{ok,<<"PONG">>} ->
684-
true;
685-
_ ->
686-
false
687+
ct:log("Plain TCP connection to Redis succeeded"),
688+
case check_redis_ping(Client, plain) of
689+
{true, plain} ->
690+
{true, plain};
691+
false ->
692+
%% tcp_closed case handled inside check_redis_ping returns false
693+
is_redis_running_tls()
687694
end;
688-
_ ->
695+
PlainError ->
696+
ct:log("Plain TCP connection to Redis failed: ~p, trying TLS...", [PlainError]),
697+
is_redis_running_tls()
698+
end.
699+
700+
is_redis_running_tls() ->
701+
ct:log("Attempting TLS connection to Redis on 127.0.0.1:6379"),
702+
try
703+
TlsOpts = just_tls:make_client_opts(#{verify_mode => none}),
704+
try_redis_tls_connection(TlsOpts)
705+
catch
706+
Error ->
707+
ct:log("TLS connection attempt failed with exception: ~p", [Error]),
708+
false
709+
end.
710+
711+
try_redis_tls_connection(TlsOpts) ->
712+
case eredis:start_link([{host, "127.0.0.1"}, {port, 6379}, {tls, TlsOpts}]) of
713+
{ok, Client} ->
714+
ct:log("TLS connection to Redis succeeded"),
715+
check_redis_ping(Client, tls);
716+
TlsError ->
717+
ct:log("TLS connection to Redis failed: ~p", [TlsError]),
718+
false
719+
end.
720+
721+
check_redis_ping(Client, ConnType) ->
722+
Result = eredis:q(Client, [<<"PING">>], 5000),
723+
eredis:stop(Client),
724+
case Result of
725+
{ok, <<"PONG">>} ->
726+
ct:log("Redis ~p connection: PING successful", [ConnType]),
727+
{true, ConnType};
728+
Error ->
729+
ct:log("Redis ~p connection: PING failed with ~p", [ConnType, Error]),
689730
false
690731
end.
691732

0 commit comments

Comments
 (0)