Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
chrzaszcz committed Mar 29, 2022
1 parent f1bca29 commit 13858db
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 108 deletions.
231 changes: 143 additions & 88 deletions big_tests/tests/service_mongoose_system_metrics_SUITE.erl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
-module(service_mongoose_system_metrics_SUITE).

-compile([export_all, nowarn_export_all]).

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

-define(SERVER_URL, "http://localhost:8765").
Expand All @@ -18,45 +19,7 @@
ev = "",
el = "" }).

%% API
-export([
all/0,
suite/0,
groups/0,
init_per_suite/1,
end_per_suite/1,
init_per_group/2,
end_per_group/2,
init_per_testcase/2,
end_per_testcase/2
]).

-export([
system_metrics_are_not_reported_when_not_allowed/1,
periodic_report_available/1,
all_clustered_mongooses_report_the_same_client_id/1,
system_metrics_are_reported_to_google_analytics_when_mim_starts/1,
system_metrics_are_reported_to_configurable_google_analytics/1,
system_metrics_are_reported_to_a_json_file/1,
module_backend_is_reported/1,
mongoose_version_is_reported/1,
cluster_uptime_is_reported/1,
xmpp_components_are_reported/1,
api_are_reported/1,
transport_mechanisms_are_reported/1,
outgoing_pools_are_reported/1,
xmpp_stanzas_counts_are_reported/1,
config_type_is_reported/1
]).

-export([
just_removed_from_config_logs_question/1,
in_config_unmodified_logs_request_for_agreement/1,
in_config_with_explicit_no_report_goes_off_silently/1,
in_config_with_explicit_reporting_goes_on_silently/1
]).

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

Expand All @@ -66,6 +29,7 @@
common/1]).

-import(domain_helper, [host_type/0]).
-import(config_parser_helper, [mod_config/2, config/2]).

suite() ->
require_rpc_nodes([mim]).
Expand All @@ -78,7 +42,6 @@ all() ->
system_metrics_are_reported_to_google_analytics_when_mim_starts,
system_metrics_are_reported_to_configurable_google_analytics,
system_metrics_are_reported_to_a_json_file,
module_backend_is_reported,
mongoose_version_is_reported,
cluster_uptime_is_reported,
xmpp_components_are_reported,
Expand All @@ -87,11 +50,16 @@ all() ->
outgoing_pools_are_reported,
xmpp_stanzas_counts_are_reported,
config_type_is_reported,
{group, module_opts},
{group, log_transparency}
].

groups() ->
[
{module_opts, [], [
module_opts_are_reported,
rdbms_backend_is_reported
]},
{log_transparency, [], [
just_removed_from_config_logs_question,
in_config_unmodified_logs_request_for_agreement,
Expand Down Expand Up @@ -127,13 +95,17 @@ end_per_suite(Config) ->
%%--------------------------------------------------------------------
%% Init & teardown
%%--------------------------------------------------------------------
init_per_group(module_opts, Config) ->
dynamic_modules:save_modules(host_type(), Config);
init_per_group(log_transparency, Config) ->
logger_ct_backend:start(),
logger_ct_backend:capture(warning),
Config;
init_per_group(_GroupName, Config) ->
Config.

end_per_group(module_opts, Config) ->
dynamic_modules:restore_modules(Config);
end_per_group(log_transparency, Config) ->
logger_ct_backend:stop_capture(),
Config;
Expand All @@ -160,17 +132,26 @@ init_per_testcase(xmpp_components_are_reported, Config) ->
Config1 = get_components(common(Config), Config),
enable_system_metrics(mim()),
Config1;
init_per_testcase(module_backend_is_reported, Config) ->
create_events_collection(),
DefModVCardConfig = config_parser_helper:default_mod_config(mod_vcard),
dynamic_modules:ensure_modules(host_type(), [{mod_vcard, DefModVCardConfig}]),
enable_system_metrics(mim()),
Config;
init_per_testcase(xmpp_stanzas_counts_are_reported = CN, Config) ->
create_events_collection(),
enable_system_metrics(mim()),
Config1 = escalus:create_users(Config, escalus:get_users([alice, bob])),
escalus:init_per_testcase(CN, Config1);
init_per_testcase(rdbms_backend_is_reported = CN, Config) ->
case mongoose_helper:is_rdbms_enabled(host_type()) of
false ->
{skip, "RDBMS is not available"};
true ->
create_events_collection(),
dynamic_modules:ensure_modules(host_type(), required_modules(CN)),
enable_system_metrics(mim())
end,
Config;
init_per_testcase(module_opts_are_reported = CN, Config) ->
create_events_collection(),
dynamic_modules:ensure_modules(host_type(), required_modules(CN)),
enable_system_metrics(mim()),
Config;
init_per_testcase(_TestcaseName, Config) ->
create_events_collection(),
enable_system_metrics(mim()),
Expand Down Expand Up @@ -215,62 +196,84 @@ periodic_report_available(_Config) ->
true).

all_clustered_mongooses_report_the_same_client_id(_Config) ->
mongoose_helper:wait_until(fun hosts_count_is_reported/0, true),
mongoose_helper:wait_until(fun is_hosts_count_reported/0, true),
all_event_have_the_same_client_id().

system_metrics_are_reported_to_google_analytics_when_mim_starts(_Config) ->
mongoose_helper:wait_until(fun hosts_count_is_reported/0, true),
mongoose_helper:wait_until(fun modules_are_reported/0, true),
mongoose_helper:wait_until(fun is_hosts_count_reported/0, true),
mongoose_helper:wait_until(fun are_modules_reported/0, true),
events_are_reported_to_primary_tracking_id(),
all_event_have_the_same_client_id().

system_metrics_are_reported_to_configurable_google_analytics(_Config) ->
mongoose_helper:wait_until(fun hosts_count_is_reported/0, true),
mongoose_helper:wait_until(fun modules_are_reported/0, true),
mongoose_helper:wait_until(fun is_hosts_count_reported/0, true),
mongoose_helper:wait_until(fun are_modules_reported/0, true),
events_are_reported_to_both_tracking_ids(),
all_event_have_the_same_client_id().

system_metrics_are_reported_to_a_json_file(_Config) ->
ReportFilePath = distributed_helper:rpc(mim(), mongoose_system_metrics_file, location, []),
ReportLastModified = distributed_helper:rpc(mim(), filelib, last_modified, [ReportFilePath]),
ReportFilePath = rpc(mim(), mongoose_system_metrics_file, location, []),
ReportLastModified = rpc(mim(), filelib, last_modified, [ReportFilePath]),
Fun = fun() ->
ReportLastModified < distributed_helper:rpc(mim(), filelib, last_modified, [ReportFilePath])
ReportLastModified < rpc(mim(), filelib, last_modified, [ReportFilePath])
end,
mongoose_helper:wait_until(Fun, true),
%% now we read the content of the file and check if it's a valid JSON
{ok, File} = distributed_helper:rpc(mim(), file, read_file, [ReportFilePath]),
{ok, File} = rpc(mim(), file, read_file, [ReportFilePath]),
jiffy:decode(File).

module_backend_is_reported(_Config) ->
mongoose_helper:wait_until(fun modules_are_reported/0, true),
mongoose_helper:wait_until(fun mod_vcard_backend_is_reported/0, true).
module_opts_are_reported(_Config) ->
mongoose_helper:wait_until(fun are_modules_reported/0, true),
Backend = mongoose_helper:mnesia_or_rdbms_backend(),
check_module_backend(mod_bosh, mnesia),
check_module_backend(mod_event_pusher, push),
check_module_backend(mod_event_pusher_push, Backend),
check_module_backend(mod_last, Backend),
check_module_backend(mod_muc, Backend),
check_module_backend(mod_muc_light, Backend),
check_module_backend(mod_offline, Backend),
check_module_backend(mod_privacy, Backend),
check_module_backend(mod_private, Backend),
check_module_backend(mod_pubsub, Backend),
check_module_opt(mod_push_service_mongoosepush, api_version, <<"\"v3\"">>),
check_module_backend(mod_roster, Backend),
check_module_backend(mod_vcard, Backend).

rdbms_backend_is_reported(_Config) ->
mongoose_helper:wait_until(fun are_modules_reported/0, true),
check_module_backend(mod_auth_token, rdbms),
check_module_backend(mod_inbox, rdbms),
check_module_backend(mod_mam_meta, rdbms).

check_module_backend(Module, Backend) ->
check_module_opt(Module, backend, atom_to_binary(Backend)).

mongoose_version_is_reported(_Config) ->
mongoose_helper:wait_until(fun mongoose_version_is_reported/0, true).
mongoose_helper:wait_until(fun is_mongoose_version_reported/0, true).

cluster_uptime_is_reported(_Config) ->
mongoose_helper:wait_until(fun cluster_uptime_is_reported/0, true).
mongoose_helper:wait_until(fun is_cluster_uptime_reported/0, true).

xmpp_components_are_reported(Config) ->
CompOpts = ?config(component1, Config),
{Component, Addr, _} = connect_component(CompOpts),
mongoose_helper:wait_until(fun xmpp_components_are_reported/0, true),
mongoose_helper:wait_until(fun are_xmpp_components_reported/0, true),
mongoose_helper:wait_until(fun more_than_one_component_is_reported/0, true),
disconnect_component(Component, Addr).

api_are_reported(_Config) ->
mongoose_helper:wait_until(fun api_are_reported/0, true).
mongoose_helper:wait_until(fun is_api_reported/0, true).

transport_mechanisms_are_reported(_Config) ->
mongoose_helper:wait_until(fun transport_mechanisms_are_reported/0, true).
mongoose_helper:wait_until(fun are_transport_mechanisms_reported/0, true).

outgoing_pools_are_reported(_Config) ->
mongoose_helper:wait_until(fun outgoing_pools_are_reported/0, true).
mongoose_helper:wait_until(fun are_outgoing_pools_reported/0, true).

xmpp_stanzas_counts_are_reported(Config) ->
escalus:story(Config, [{alice,1}, {bob,1}], fun(Alice, Bob) ->
mongoose_helper:wait_until(fun message_count_is_reported/0, true),
mongoose_helper:wait_until(fun iq_count_is_reported/0, true),
mongoose_helper:wait_until(fun is_message_count_reported/0, true),
mongoose_helper:wait_until(fun is_iq_count_reported/0, true),
Sent = get_metric_value(<<"xmppMessageSent">>),
Received = get_metric_value(<<"xmppMessageReceived">>),
escalus:send(Alice, escalus_stanza:chat_to(Bob, <<"Hi">>)),
Expand All @@ -280,7 +283,7 @@ xmpp_stanzas_counts_are_reported(Config) ->
end).

config_type_is_reported(_Config) ->
mongoose_helper:wait_until(fun config_type_is_reported/0, true).
mongoose_helper:wait_until(fun is_config_type_reported/0, true).

just_removed_from_config_logs_question(_Config) ->
disable_system_metrics(mim3()),
Expand Down Expand Up @@ -337,15 +340,64 @@ in_config_with_explicit_reporting_goes_on_silently(_Config) ->
%% Helpers
%%--------------------------------------------------------------------

required_modules(CaseName) ->
lists:filter(fun({Module, _Opts}) -> is_module_supported(Module) end,
modules_to_test(CaseName)).

modules_to_test(module_opts_are_reported) ->
Backend = mongoose_helper:mnesia_or_rdbms_backend(),
[required_module(mod_bosh),
required_module(mod_event_pusher,
#{push => config([modules, mod_event_pusher, push], #{backend => Backend})}),
required_module(mod_last, Backend),
required_module(mod_muc, Backend),
required_module(mod_muc_light, Backend),
required_module(mod_offline, Backend),
required_module(mod_privacy, Backend),
required_module(mod_private, Backend),
required_module(mod_pubsub, Backend),
required_module(mod_push_service_mongoosepush),
required_module(mod_roster, Backend),
required_module(mod_vcard, Backend)];
modules_to_test(rdbms_backend_is_reported) ->
[required_module(mod_auth_token),
required_module(mod_inbox),
required_module(mod_mam_meta)].

required_module(Module) ->
required_module(Module, #{}).

required_module(Module, Backend) when is_atom(Backend) ->
{Module, mod_config(Module, #{backend => Backend})};
required_module(Module, Opts) ->
{Module, mod_config(Module, Opts)}.

check_module_opt(Module, Key, Value) ->
case is_module_supported(Module) of
true ->
?assertEqual(true, is_module_opt_reported(Module, Key, Value));
false ->
ct:log("Skipping unsupported module ~p", [Module])
end.

is_module_supported(Module) ->
is_host_type_static() orelse supports_dynamic_domains(Module).

is_host_type_static() ->
rpc(mim(), mongoose_domain_core, is_static, [host_type()]).

supports_dynamic_domains(Module) ->
rpc(mim(), gen_mod, does_module_support, [Module, dynamic_domains]).

all_event_have_the_same_client_id() ->
Tab = ets:tab2list(?ETS_TABLE),
UniqueSortedTab = lists:usort([Cid || #event{cid = Cid} <- Tab]),
1 = length(UniqueSortedTab).

hosts_count_is_reported() ->
is_hosts_count_reported() ->
is_in_table(<<"hosts">>).

modules_are_reported() ->
are_modules_reported() ->
is_in_table(<<"module">>).

is_in_table(EventCategory) ->
Expand Down Expand Up @@ -427,42 +479,45 @@ events_are_reported_to_tracking_ids(ConfiguredTrackingIds) ->
ExpectedTrackingIds = lists:sort([list_to_binary(Tid) || Tid <- ConfiguredTrackingIds]),
?assertEqual(ExpectedTrackingIds, ActualTrackingIds).

feature_is_reported(EventCategory, EventAction) ->
is_feature_reported(EventCategory, EventAction) ->
length(match_events(EventCategory, EventAction)) > 0.

feature_is_reported(EventCategory, EventAction, EventLabel) ->
is_feature_reported(EventCategory, EventAction, EventLabel) ->
length(match_events(EventCategory, EventAction, EventLabel)) > 0.

mod_vcard_backend_is_reported() ->
feature_is_reported(<<"mod_vcard">>, <<"backend">>).
is_module_backend_reported(Module, Backend) ->
is_feature_reported(atom_to_binary(Module), <<"backend">>, atom_to_binary(Backend)).

is_module_opt_reported(Module, Key, Value) ->
is_feature_reported(atom_to_binary(Module), atom_to_binary(Key), Value).

mongoose_version_is_reported() ->
feature_is_reported(<<"cluster">>, <<"mim_version">>).
is_mongoose_version_reported() ->
is_feature_reported(<<"cluster">>, <<"mim_version">>).

cluster_uptime_is_reported() ->
feature_is_reported(<<"cluster">>, <<"uptime">>).
is_cluster_uptime_reported() ->
is_feature_reported(<<"cluster">>, <<"uptime">>).

xmpp_components_are_reported() ->
feature_is_reported(<<"cluster">>, <<"number_of_components">>).
are_xmpp_components_reported() ->
is_feature_reported(<<"cluster">>, <<"number_of_components">>).

config_type_is_reported() ->
IsToml = feature_is_reported(<<"cluster">>, <<"config_type">>, <<"toml">>),
IsCfg = feature_is_reported(<<"cluster">>, <<"config_type">>, <<"cfg">>),
is_config_type_reported() ->
IsToml = is_feature_reported(<<"cluster">>, <<"config_type">>, <<"toml">>),
IsCfg = is_feature_reported(<<"cluster">>, <<"config_type">>, <<"cfg">>),
IsToml orelse IsCfg.

api_are_reported() ->
is_api_reported() ->
is_in_table(<<"http_api">>).

transport_mechanisms_are_reported() ->
are_transport_mechanisms_reported() ->
is_in_table(<<"transport_mechanism">>).

outgoing_pools_are_reported() ->
are_outgoing_pools_reported() ->
is_in_table(<<"outgoing_pools">>).

iq_count_is_reported() ->
is_iq_count_reported() ->
is_in_table(<<"xmppIqSent">>).

message_count_is_reported() ->
is_message_count_reported() ->
is_in_table(<<"xmppMessageSent">>) andalso is_in_table(<<"xmppMessageReceived">>).

assert_message_count_is_incremented(Sent, Received) ->
Expand Down
Loading

0 comments on commit 13858db

Please sign in to comment.