Skip to content

Commit

Permalink
Merge pull request #4226 from esl/feature/instrument
Browse files Browse the repository at this point in the history
Feature/instrument
  • Loading branch information
JanuszJakubiec authored Sep 9, 2024
2 parents 1680efa + a66c1b8 commit aafbc75
Show file tree
Hide file tree
Showing 233 changed files with 8,421 additions and 4,741 deletions.
2 changes: 1 addition & 1 deletion .circleci/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ jobs:
- run:
name: Build GraphQL static docs
command: |
npm install --prefix=$HOME/.local --global spectaql
npm install --global cheerio@1.0.0-rc.12 spectaql
npx spectaql -t doc/graphql-api -f admin-graphql-doc.html doc/graphql-api/Admin-GraphQL_spectaql.yml
npx spectaql -C -J -t doc/graphql-api -f user-graphql-doc.html doc/graphql-api/User-GraphQL_spectaql.yml
- run:
Expand Down
2 changes: 1 addition & 1 deletion big_tests/default.spec
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
{suites, "tests", mam_send_message_SUITE}.
{suites, "tests", metrics_api_SUITE}.
{suites, "tests", metrics_c2s_SUITE}.
{suites, "tests", metrics_register_SUITE}.
{suites, "tests", metrics_roster_SUITE}.
{suites, "tests", metrics_session_SUITE}.
{suites, "tests", mod_blocking_SUITE}.
Expand Down Expand Up @@ -108,6 +107,7 @@
{suites, "tests", sic_SUITE}.
{suites, "tests", smart_markers_SUITE}.
{suites, "tests", sm_SUITE}.
{suites, "tests", system_probes_SUITE}.
{suites, "tests", vcard_SUITE}.
{suites, "tests", vcard_simple_SUITE}.
{suites, "tests", websockets_SUITE}.
Expand Down
4 changes: 3 additions & 1 deletion big_tests/dynamic_domains.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{host_type, <<"test type">>},
{secondary_domain, <<"domain.example.org">>},
{secondary_host_type, <<"test type">>},
{anonymous_host_type, <<"anonymous">>},
{dynamic_domains, [{<<"test type">>, [<<"domain.example.com">>, <<"domain.example.org">>]},
{<<"anonymous">>, [<<"anonymous.example.com">>]}]},
{muc_service, <<"groupchats.domain.example.com">>},
Expand Down Expand Up @@ -119,7 +120,8 @@
{wspath, <<"/ws-xmpp">>}]},
{admin, [
{username, <<"admin">>},
{server, <<"localhost">>},
{server, <<"domain.example.com">>},
{host, <<"localhost">>},
{password, <<"bruce_almighty">>}]},
{secure_joe, [
{username, <<"secure_joe">>},
Expand Down
3 changes: 1 addition & 2 deletions big_tests/dynamic_domains.spec
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@

{suites, "tests", metrics_c2s_SUITE}.

{suites, "tests", metrics_register_SUITE}.

{suites, "tests", metrics_roster_SUITE}.

{suites, "tests", metrics_session_SUITE}.
Expand Down Expand Up @@ -154,6 +152,7 @@

{suites, "tests", smart_markers_SUITE}.
{suites, "tests", sm_SUITE}.
{suites, "tests", system_probes_SUITE}.
{suites, "tests", vcard_SUITE}.
{suites, "tests", vcard_simple_SUITE}.
{suites, "tests", websockets_SUITE}.
Expand Down
2 changes: 1 addition & 1 deletion big_tests/test.config
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
{vars, "mim1"},
{cluster, mim},
{secondary_domain, <<"localhost.bis">>},
{reloaded_domain, <<"sogndal">>},
{secondary_host_type, <<"localhost.bis">>},
{anonymous_host_type, <<"anonymous.localhost">>},
{muc_service, <<"muc.localhost">>},
{muc_service_pattern, <<"muc.@HOST@">>},
{muc_light_service, <<"muclight.localhost">>},
Expand Down
49 changes: 30 additions & 19 deletions big_tests/tests/accounts_SUITE.erl
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
-module(accounts_SUITE).
-compile([export_all, nowarn_export_all]).

-include_lib("escalus/include/escalus.hrl").
-include_lib("escalus/include/escalus_xmlns.hrl").

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

-include_lib("exml/include/exml.hrl").

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

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

-import(auth_helper, [assert_event/2]).
-import(domain_helper, [domain/0, host_type/0]).

%%--------------------------------------------------------------------
Expand All @@ -37,6 +30,7 @@ all() ->

groups() ->
[{register, [parallel], [register,
unregister,
already_registered,
registration_conflict,
check_unregistered]},
Expand Down Expand Up @@ -68,13 +62,15 @@ change_password_tests() ->
%%--------------------------------------------------------------------

init_per_suite(Config1) ->
instrument_helper:start(instrument_helper:declared_events(ejabberd_auth)),
ok = dynamic_modules:ensure_modules(host_type(), required_modules()),
Config2 = [{mod_register_options, mod_register_options()} | Config1],
escalus:init_per_suite([{escalus_user_db, xmpp} | Config2]).

end_per_suite(Config) ->
escalus_fresh:clean(),
escalus:end_per_suite(Config).
escalus:end_per_suite(Config),
instrument_helper:stop().

required_modules() ->
[{mod_register, mod_register_options()}].
Expand Down Expand Up @@ -178,23 +174,37 @@ end_per_testcase(CaseName, Config) ->

register(Config) ->
[{Name1, _UserSpec1}, {Name2, _UserSpec2}] = escalus_users:get_users([alice, bob]),
escalus_fresh:create_users(Config, escalus:get_users([Name1, Name2])).
Config1 = escalus_fresh:create_users(Config, escalus:get_users([Name1, Name2])),
assert_event(auth_register_user, escalus_users:get_jid(Config1, Name1)),
assert_event(auth_register_user, escalus_users:get_jid(Config1, Name2)),
assert_event(auth_try_register, escalus_users:get_jid(Config1, Name1)),
assert_event(auth_try_register, escalus_users:get_jid(Config1, Name2)),
assert_event(auth_does_user_exist, escalus_users:get_jid(Config1, Name1)),
assert_event(auth_does_user_exist, escalus_users:get_jid(Config1, Name2)).

unregister(Config) ->
UserSpec = escalus_fresh:freshen_spec(Config, alice),
escalus_users:create_user(Config, {alice, UserSpec}),
escalus_users:delete_user(Config, {alice, UserSpec}),
assert_event(auth_unregister_user, escalus_users:get_jid(Config, UserSpec)).

already_registered(Config) ->
escalus_fresh:story(Config, [{alice, 1}], fun(Alice) ->
escalus:send(Alice, escalus_stanza:get_registration_fields()),
Stanza = escalus:wait_for_stanza(Alice),
escalus:assert(is_iq_result, Stanza),
true = has_registered_element(Stanza)
escalus_fresh:story(Config, [{alice, 1}], fun already_registered_story/1).

already_registered_story(Alice) ->
AliceJid = escalus_utils:get_short_jid(Alice),
assert_event(auth_register_user, AliceJid), % one event expected
escalus:send(Alice, escalus_stanza:get_registration_fields()),
Stanza = escalus:wait_for_stanza(Alice),
escalus:assert(is_iq_result, Stanza),
true = has_registered_element(Stanza),
assert_event(auth_register_user, AliceJid). % still one event - nothing new

end).
registration_conflict(Config) ->
[Alice] = escalus_users:get_users([alice]),
{ok, result, _Stanza} = escalus_users:create_user(Config, Alice),
{ok, conflict, _Raw} = escalus_users:create_user(Config, Alice).



admin_notify(Config) ->
[{Name1, UserSpec1}, {Name2, UserSpec2}] = escalus_users:get_users([alice, bob]),
[{_, AdminSpec}] = escalus_users:get_users([admin]),
Expand All @@ -204,6 +214,7 @@ admin_notify(Config) ->

rpc(mim(), ejabberd_auth, try_register, [mongoose_helper:make_jid(AdminU, AdminS), AdminP]),
escalus:story(Config, [{admin, 1}], fun(Admin) ->
assert_event(auth_authorize, escalus_utils:get_jid(Admin)),
escalus:create_users(Config, escalus:get_users([Name1, Name2])),

Predicates = [
Expand Down
10 changes: 9 additions & 1 deletion big_tests/tests/amp_big_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ init_per_suite(Config) ->
{Mod, Code} = dynamic_compile:from_string(amp_test_helper_code()),
rpc(mim(), code, load_binary, [Mod, "amp_test_helper.erl", Code]),
setup_meck(suite),
instrument_helper:start(declared_events()),
escalus:init_per_suite(ConfigWithHooks).

amp_test_helper_code() ->
Expand All @@ -131,10 +132,17 @@ amp_test_helper_code() ->
" _ -> meck:passthrough([Socket, Data])\n"
" end.\n".

declared_events() ->
[ % tested by privacy helpers
{mod_privacy_set, #{host_type => host_type()}},
{mod_privacy_get, #{host_type => host_type()}}
].

end_per_suite(C) ->
teardown_meck(suite),
escalus_fresh:clean(),
escalus:end_per_suite(C).
escalus:end_per_suite(C),
instrument_helper:stop().

init_per_group(GroupName, Config) ->
Config1 = case lists:member(GroupName, main_group_names()) of
Expand Down
25 changes: 22 additions & 3 deletions big_tests/tests/anonymous_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ suite() ->
%% Init & teardown
%%--------------------------------------------------------------------
init_per_suite(Config) ->
instrument_helper:start(declared_events()),
escalus:init_per_suite(Config).

end_per_suite(Config) ->
escalus:end_per_suite(Config).
escalus:end_per_suite(Config),
instrument_helper:stop().

init_per_testcase(CaseName, Config0) ->
NewUsers = escalus_ct:get_config(escalus_anon_users),
Expand All @@ -67,23 +69,27 @@ end_per_testcase(CaseName, Config) ->
connection_is_registered_with_sasl_anon(Config) ->
escalus:story(Config, [{jon, 1}], fun(Jon) ->
JID = jid:from_binary(escalus_client:short_jid(Jon)),
assert_event(auth_anonymous_register_user, JID),
OrigName = escalus_users:get_username(Config, jon),
?assertNotEqual(OrigName, JID#jid.luser),
F = fun() -> rpc(mim(), ejabberd_auth, does_user_exist, [JID]) end,
true = F(),
escalus_connection:kill(Jon),
mongoose_helper:wait_until(F, false)
mongoose_helper:wait_until(F, false),
assert_event(auth_anonymous_unregister_user, JID)
end).

connection_is_registered_with_login(Config) ->
escalus:story(Config, [{anna, 1}], fun(Anna) ->
JID = jid:from_binary(escalus_client:short_jid(Anna)),
assert_event(auth_anonymous_register_user, JID),
OrigName = escalus_users:get_username(Config, anna),
?assertEqual(OrigName, JID#jid.luser),
F = fun() -> rpc(mim(), ejabberd_auth, does_user_exist, [JID]) end,
true = F(),
escalus_connection:kill(Anna),
mongoose_helper:wait_until(F, false)
mongoose_helper:wait_until(F, false),
assert_event(auth_anonymous_unregister_user, JID)
end).

messages_story(Config) ->
Expand All @@ -94,3 +100,16 @@ messages_story(Config) ->
%% Below's dirty, but there is no other easy way...
escalus_assert:is_chat_message(<<"Hi!">>, Stanza)
end).

%% Helpers

declared_events() ->
instrument_helper:declared_events(ejabberd_auth_anonymous, [host_type()]).

host_type() ->
domain_helper:anonymous_host_type().

assert_event(EventName, #jid{luser = LUser, lserver = LServer}) ->
instrument_helper:assert_one(
EventName, #{host_type => host_type()},
fun(M) -> M =:= #{count => 1, user => LUser, server => LServer} end).
27 changes: 27 additions & 0 deletions big_tests/tests/auth_helper.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-module(auth_helper).
-export([assert_event/2]).

-include_lib("jid/include/jid.hrl").
-import(domain_helper, [host_type/0]).

assert_event(EventName, BinJid)
when EventName =:= auth_unregister_user; EventName =:= auth_register_user ->
#jid{luser = LUser, lserver = LServer} = jid:from_binary(BinJid),
F = fun(M) ->
M =:= #{count => 1, user => LUser, server => LServer}
end,
instrument_helper:assert_one(EventName, #{host_type => host_type()}, F);
assert_event(EventName, BinJid)
when EventName =:= auth_authorize ->
#jid{lserver = LServer} = jid:from_binary(BinJid),
F = fun(#{time := Time, count := 1, server := Server}) ->
(Time > 0) and (Server =:= LServer)
end,
%% Note: this could match events from other tests because there is no user name
instrument_helper:assert(EventName, #{host_type => host_type()}, F);
assert_event(EventName, BinJid) ->
#jid{luser = LUser, lserver = LServer} = jid:from_binary(BinJid),
F = fun(#{time := Time, count := 1, user := User, server := Server}) ->
(Time > 0) and (User =:= LUser) and (Server =:= LServer)
end,
instrument_helper:assert_one(EventName, #{host_type => host_type()}, F).
41 changes: 26 additions & 15 deletions big_tests/tests/auth_methods_for_c2s_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
-include_lib("exml/include/exml.hrl").

-import(distributed_helper, [mim/0, rpc/4]).
-import(auth_helper, [assert_event/2]).

all() ->
[
{group, two_methods_enabled},
{group, metrics}
{group, instrumentation}
].

groups() ->
Expand All @@ -20,29 +21,37 @@ groups() ->
cannot_login_with_not_allowed_method,
can_login_to_another_listener
]},
{metrics, [],
{instrumentation, [],
[
metrics_incremented_on_user_connect
instrumentation_incremented_on_user_connect
]}
].

suite() ->
distributed_helper:require_rpc_nodes([mim]) ++ escalus:suite().

init_per_suite(Config) ->
escalus:init_per_suite(Config).

end_per_suite(Config) ->
escalus:end_per_suite(Config).

init_per_group(metrics, Config) ->
set_auth_mod(Config);
init_per_group(instrumentation, Config) ->
Config2 = set_auth_mod(Config),
instrument_helper:start(events()),
Config2;
init_per_group(_, Config0) ->
Config1 = ejabberd_node_utils:init(Config0),
ejabberd_node_utils:backup_config_file(Config1),
Config2 = modify_config_and_restart(Config1),
instrument_helper:start(events()),
escalus_cleaner:start(Config2).

end_per_group(metrics, _Config) ->
end_per_group(instrumentation, _Config) ->
instrument_helper:stop(),
escalus_fresh:clean();
end_per_group(_, Config) ->
instrument_helper:stop(),
ejabberd_node_utils:restore_config_file(Config),
ejabberd_node_utils:restart_application(mongooseim),
escalus_fresh:clean().
Expand Down Expand Up @@ -93,24 +102,24 @@ can_login_to_another_listener(Config) ->
TlsPort = ct:get_config({hosts, mim, c2s_tls_port}),
Spec2 = [{port, TlsPort}, {ssl, true}, {ssl_opts, [{verify, verify_none}]},
{password, <<"wrong">>} | Spec],
{ok, _, _} = escalus_connection:start(Spec2).
{ok, Conn, _} = escalus_connection:start(Spec2),
assert_event(auth_authorize, escalus_utils:get_jid(Conn)).

metrics_incremented_on_user_connect(ConfigIn) ->
instrumentation_incremented_on_user_connect(Config) ->
F = fun(Alice, Bob) ->
Body = <<"Hello Bob">>,
escalus:send(Alice, escalus_stanza:chat_to(Bob, Body)),
escalus:assert(is_chat_message, [Body], escalus:wait_for_stanza(Bob))
escalus:assert(is_chat_message, [Body], escalus:wait_for_stanza(Bob)),
assert_event(auth_authorize, escalus_utils:get_jid(Alice)),
assert_event(auth_authorize, escalus_utils:get_jid(Bob))
end,
HostType = domain_helper:host_type(),
HostTypePrefix = domain_helper:make_metrics_prefix(HostType),
MongooseMetrics = [{[HostTypePrefix, backends, auth, authorize], changed}],
Config = [{mongoose_metrics, MongooseMetrics} | ConfigIn],
escalus_fresh:story(Config, [{alice, 1}, {bob, 1}], F).

%% Helpers
%% If dummy backend is enabled, it is not possible to create new users
%% (we check if an user does exist before registering the user).
register_user(Config, Spec) ->
%% Calls backend module directly, so instrumentation is not called in this case.
Mod = proplists:get_value(auth_mod, Config),
#{username := User, server := Server,
password := Password} = maps:from_list(Spec),
Expand All @@ -119,5 +128,7 @@ register_user(Config, Spec) ->
HostType = domain_helper:host_type(),
ok = rpc(mim(), Mod, try_register,
[HostType, LUser, LServer, Password]),
fun() -> rpc(mim(), Mod, remove_user,
[HostType, LUser, LServer]) end.
fun() -> rpc(mim(), Mod, remove_user, [HostType, LUser, LServer]) end.

events() ->
[{auth_authorize, #{host_type => domain_helper:host_type()}}].
Loading

0 comments on commit aafbc75

Please sign in to comment.