Rewrite and expand EUnit suite; run unit tests in CI from shared test image. Refs EventHub/EventHubBack#36 [skip ci]

This commit is contained in:
2026-07-17 15:16:46 +03:00
parent 9d2780fef4
commit 4fa802702f
54 changed files with 4240 additions and 2281 deletions
@@ -0,0 +1,112 @@
-module(admin_handler_node_metrics_tests).
-include_lib("eunit/include/eunit.hrl").
-include("records.hrl").
-define(TABLES, [admin, admin_audit, node_metric, ticket]).
-define(ADMIN_ID, <<"adm_test_1">>).
setup() ->
eh_test_support:start_mnesia(),
eh_test_support:ensure_tables(?TABLES),
eh_test_support:ensure_jwt(),
eh_test_support:seed_admin(#{id => ?ADMIN_ID}),
ok.
cleanup(_) ->
eh_test_support:unload_cowboy(),
eh_test_support:delete_tables(?TABLES),
eh_test_support:stop_mnesia(),
ok.
admin_node_metrics_test_() ->
{foreach, fun setup/0, fun cleanup/1, [
{"GET empty range", {timeout, 60, fun test_empty/0}},
{"GET with seeded metric", {timeout, 60, fun test_with_data/0}},
{"GET missing from", {timeout, 60, fun test_missing_from/0}},
{"GET unauthorized", {timeout, 60, fun test_unauthorized/0}},
{"POST method not allowed", {timeout, 60, fun test_wrong_method/0}}
]}.
range_qs() ->
From = handler_utils:datetime_to_iso8601(eh_test_support:days_from_now(-1)),
To = handler_utils:datetime_to_iso8601(eh_test_support:days_from_now(1)),
[{<<"from">>, From}, {<<"to">>, To}].
seed_metric() ->
Now = calendar:universal_time(),
Metric = #node_metric{
timestamp = Now,
node = node(),
memory_total = 1000,
memory_processes = 100,
memory_atom = 10,
memory_binary = 20,
memory_ets = 30,
process_count = 5,
run_queue = 0,
mnesia_commits = 1,
mnesia_failures = 0,
table_sizes = #{},
active_sessions = 0,
ws_connections = 0,
io_in = 0,
io_out = 0,
memory_available = 500,
cpu_utilization = 1.5
},
ok = core_node_metric:save(Metric),
Metric.
test_empty() ->
{Status, _, Body} = eh_test_support:call(admin_handler_node_metrics, #{
method => <<"GET">>,
path => <<"/v1/admin/nodes/metrics">>,
qs => range_qs(),
auth => ?ADMIN_ID
}),
?assertEqual(200, Status),
Result = jsx:decode(Body, [return_maps]),
?assertEqual([], Result).
test_with_data() ->
_ = seed_metric(),
{Status, _, Body} = eh_test_support:call(admin_handler_node_metrics, #{
method => <<"GET">>,
path => <<"/v1/admin/nodes/metrics">>,
qs => range_qs(),
auth => ?ADMIN_ID
}),
?assertEqual(200, Status),
Result = jsx:decode(Body, [return_maps]),
?assertEqual(1, length(Result)),
[M] = Result,
?assertEqual(1000, maps:get(<<"memory_total">>, M)),
?assertEqual(5, maps:get(<<"process_count">>, M)).
test_missing_from() ->
To = handler_utils:datetime_to_iso8601(eh_test_support:days_from_now(1)),
{Status, _, _} = eh_test_support:call(admin_handler_node_metrics, #{
method => <<"GET">>,
path => <<"/v1/admin/nodes/metrics">>,
qs => [{<<"to">>, To}],
auth => ?ADMIN_ID
}),
?assertEqual(400, Status).
test_unauthorized() ->
{Status, _, _} = eh_test_support:call(admin_handler_node_metrics, #{
method => <<"GET">>,
path => <<"/v1/admin/nodes/metrics">>,
qs => range_qs(),
auth => none
}),
?assertEqual(401, Status).
test_wrong_method() ->
{Status, _, _} = eh_test_support:call(admin_handler_node_metrics, #{
method => <<"POST">>,
path => <<"/v1/admin/nodes/metrics">>,
qs => range_qs(),
auth => ?ADMIN_ID
}),
?assertEqual(405, Status).