test: refactor test group generation

This commit is contained in:
Zaiming (Stone) Shi 2023-10-30 01:43:39 +01:00
parent 289428cc5a
commit 2f46e86db8
2 changed files with 96 additions and 67 deletions

View File

@ -22,7 +22,8 @@
-export([
all/1,
groups/2,
matrix_to_groups/2,
group_path/1,
init_per_testcase/3,
end_per_testcase/3,
boot_modules/1,
@ -1377,33 +1378,63 @@ select_free_port(GenModule, Fun) when
ct:pal("Select free OS port: ~p", [Port]),
Port.
%% generate ct group spec
%% Generate ct sub-groups from test-case's 'matrix' clause
%% NOTE: the test cases must have a root group name which
%% is unkonwn to this API.
%%
%% Inputs:
%% e.g.
%% all() -> [{group, g1}].
%%
%% [ [tcp, no_auth],
%% groups() ->
%% emqx_common_test_helpers:groups(?MODULE, [case1, case2]).
%%
%% case1(matrxi) ->
%% {g1, [[tcp, no_auth],
%% [ssl, no_auth],
%% [ssl, basic_auth]
%% ]
%% ]};
%%
%% case2(matrxi) ->
%% {g1, ...}
%% ...
%%
%% Return:
%% [ {tcp, [], [{no_auth, [], Cases}
%%
%% [{g1, [],
%% [ {tcp, [], [{no_auth, [], [case1, case2]}
%% ]},
%% {ssl, [], [{no_auth, [], Cases},
%% {basic_auth, [], Cases}
%% {ssl, [], [{no_auth, [], [case1, case2]},
%% {basic_auth, [], [case1, case2]}
%% ]}
%% ]
groups(Matrix, Cases) ->
%% }
%% ]
matrix_to_groups(Module, Cases) ->
lists:foldr(
fun(Row, Acc) ->
add_group(Row, Acc, Cases)
fun(Case, Acc) ->
add_case_matrix(Module, Case, Acc)
end,
[],
Cases
).
add_case_matrix(Module, Case, Acc0) ->
{RootGroup, Matrix} = Module:Case(matrix),
lists:foldr(
fun(Row, Acc) ->
add_group([RootGroup | Row], Acc, Case)
end,
Acc0,
Matrix
).
add_group([], Acc, Cases) ->
lists:usort(Acc ++ Cases);
add_group([], Acc, Case) ->
case lists:member(Case, Acc) of
true ->
Acc;
false ->
[Case | Acc]
end;
add_group([Name | More], Acc, Cases) ->
case lists:keyfind(Name, 1, Acc) of
false ->
@ -1412,3 +1443,17 @@ add_group([Name | More], Acc, Cases) ->
New = {Name, [], add_group(More, SubGroup, Cases)},
lists:keystore(Name, 1, Acc, New)
end.
group_path(Config) ->
try
Current = proplists:get_value(tc_group_properties, Config),
NameF = fun(Props) ->
{name, Name} = lists:keyfind(name, 1, Props),
Name
end,
Stack = proplists:get_value(tc_group_path, Config),
lists:reverse(lists:map(NameF, [Current | Stack]))
catch
_:_ ->
[]
end.

View File

@ -50,14 +50,6 @@
%%------------------------------------------------------------------------------
all() ->
[
{group, all},
{group, rest_api},
{group, publish},
{group, query_mode}
].
groups() ->
case code:get_object_code(cthr) of
{Module, Code, Filename} ->
{module, Module} = code:load_binary(Module, Filename, Code),
@ -66,18 +58,20 @@ groups() ->
error
end,
All0 = emqx_common_test_helpers:all(?MODULE),
All =
All0 -- [t_rest_api, t_publish, t_send_message_with_headers, t_wrong_headers_from_message],
[
{all, All},
{publish, [], sub_groups([t_publish])},
{rest_api, [], sub_groups([t_rest_api])},
{query_mode, [], sub_groups([t_send_message_with_headers, t_wrong_headers_from_message])}
].
All = All0 -- matrix_cases(),
Groups = lists:map(fun({G, _, _}) -> {group, G} end, groups()),
Groups ++ All.
sub_groups(Cases) ->
Matrix = lists:usort(lists:append([?MODULE:Case(matrix) || Case <- Cases])),
emqx_common_test_helpers:groups(Matrix, Cases).
groups() ->
emqx_common_test_helpers:matrix_to_groups(?MODULE, matrix_cases()).
matrix_cases() ->
[
t_rest_api,
t_publish,
t_send_message_with_headers,
t_wrong_headers_from_message
].
test_topic_one_partition() ->
"test-topic-one-partition".
@ -133,26 +127,6 @@ end_per_suite(_Config) ->
_ = application:stop(emqx_connector),
ok.
init_per_group(all, Config) ->
Config;
init_per_group(rest_api, Config) ->
Config;
init_per_group(publish, Config) ->
Config;
init_per_group(query_mode, Config) ->
Config;
init_per_group(GroupName, Config) ->
case lists:keyfind(group_path, 1, Config) of
{group_path, Path} ->
NewPath = Path ++ [GroupName],
lists:keystore(group_path, 1, Config, {group_path, NewPath});
_ ->
[{group_path, [GroupName]} | Config]
end.
end_per_group(_, _) ->
ok.
init_per_testcase(TestCase, Config) ->
case proplists:get_value(debug_case, Config) of
TestCase ->
@ -204,13 +178,13 @@ t_query_mode_async(CtConfig) ->
%%------------------------------------------------------------------------------
t_publish(matrix) ->
[
{publish, [
[tcp, none, key_dispatch, sync],
[ssl, scram_sha512, random, async],
[ssl, kerberos, random, sync]
];
]};
t_publish(Config) ->
Path = proplists:get_value(group_path, Config),
Path = group_path(Config),
ct:comment(Path),
[Transport, Auth, Partitioner, QueryMode] = Path,
Hosts = kafka_hosts_string(Transport, Auth),
@ -241,14 +215,14 @@ t_publish(Config) ->
%%------------------------------------------------------------------------------
t_rest_api(matrix) ->
[
{rest_api, [
[tcp, none],
[tcp, plain],
[ssl, scram_sha256],
[ssl, kerberos]
];
]};
t_rest_api(Config) ->
Path = proplists:get_value(group_path, Config),
Path = group_path(Config),
ct:comment(Path),
[Transport, Auth] = Path,
Hosts = kafka_hosts_string(Transport, Auth),
@ -597,9 +571,9 @@ t_nonexistent_topic(_Config) ->
ok.
t_send_message_with_headers(matrix) ->
[[sync], [async]];
{query_mode, [[sync], [async]]};
t_send_message_with_headers(Config) ->
[Mode] = proplists:get_value(group_path, Config),
[Mode] = group_path(Config),
ct:comment(Mode),
HostsString = kafka_hosts_string_sasl(),
AuthSettings = valid_sasl_plain_settings(),
@ -837,7 +811,7 @@ t_wrong_headers(_Config) ->
ok.
t_wrong_headers_from_message(matrix) ->
[[sync], [async]];
{query_mode, [[sync], [async]]};
t_wrong_headers_from_message(Config) ->
HostsString = kafka_hosts_string(),
AuthSettings = "none",
@ -931,7 +905,7 @@ do_send(Ref, Config, ResourceId, Msg, State, BridgeV2Id) when is_list(Config) ->
Caller ! {ack, Ref},
ok
end,
case proplists:get_value(group_path, Config) of
case group_path(Config) of
[async] ->
{ok, _} = ?PRODUCER:on_query_async(ResourceId, {BridgeV2Id, Msg}, {F, []}, State),
ok;
@ -1286,3 +1260,13 @@ bin_map(Map) ->
{erlang:iolist_to_binary(K), erlang:iolist_to_binary(V)}
|| {K, V} <- maps:to_list(Map)
]).
%% return the path (reverse of the stack) of the test groups.
%% root group is discarded.
group_path(Config) ->
case emqx_common_test_helpers:group_path(Config) of
[] ->
undefined;
Path ->
tl(Path)
end.