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([ -export([
all/1, all/1,
groups/2, matrix_to_groups/2,
group_path/1,
init_per_testcase/3, init_per_testcase/3,
end_per_testcase/3, end_per_testcase/3,
boot_modules/1, boot_modules/1,
@ -1377,33 +1378,63 @@ select_free_port(GenModule, Fun) when
ct:pal("Select free OS port: ~p", [Port]), ct:pal("Select free OS port: ~p", [Port]),
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, no_auth],
%% [ssl, basic_auth] %% [ssl, basic_auth]
%% ] %% ]};
%%
%% case2(matrxi) ->
%% {g1, ...}
%% ...
%% %%
%% Return: %% Return:
%% [ {tcp, [], [{no_auth, [], Cases} %%
%% [{g1, [],
%% [ {tcp, [], [{no_auth, [], [case1, case2]}
%% ]}, %% ]},
%% {ssl, [], [{no_auth, [], Cases}, %% {ssl, [], [{no_auth, [], [case1, case2]},
%% {basic_auth, [], Cases} %% {basic_auth, [], [case1, case2]}
%% ]} %% ]}
%% ] %% ]
groups(Matrix, Cases) -> %% }
%% ]
matrix_to_groups(Module, Cases) ->
lists:foldr( lists:foldr(
fun(Row, Acc) -> fun(Case, Acc) ->
add_group(Row, Acc, Cases) add_case_matrix(Module, Case, Acc)
end, 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 Matrix
). ).
add_group([], Acc, Cases) -> add_group([], Acc, Case) ->
lists:usort(Acc ++ Cases); case lists:member(Case, Acc) of
true ->
Acc;
false ->
[Case | Acc]
end;
add_group([Name | More], Acc, Cases) -> add_group([Name | More], Acc, Cases) ->
case lists:keyfind(Name, 1, Acc) of case lists:keyfind(Name, 1, Acc) of
false -> false ->
@ -1412,3 +1443,17 @@ add_group([Name | More], Acc, Cases) ->
New = {Name, [], add_group(More, SubGroup, Cases)}, New = {Name, [], add_group(More, SubGroup, Cases)},
lists:keystore(Name, 1, Acc, New) lists:keystore(Name, 1, Acc, New)
end. 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() -> all() ->
[
{group, all},
{group, rest_api},
{group, publish},
{group, query_mode}
].
groups() ->
case code:get_object_code(cthr) of case code:get_object_code(cthr) of
{Module, Code, Filename} -> {Module, Code, Filename} ->
{module, Module} = code:load_binary(Module, Filename, Code), {module, Module} = code:load_binary(Module, Filename, Code),
@ -66,18 +58,20 @@ groups() ->
error error
end, end,
All0 = emqx_common_test_helpers:all(?MODULE), All0 = emqx_common_test_helpers:all(?MODULE),
All = All = All0 -- matrix_cases(),
All0 -- [t_rest_api, t_publish, t_send_message_with_headers, t_wrong_headers_from_message], Groups = lists:map(fun({G, _, _}) -> {group, G} end, groups()),
[ Groups ++ All.
{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])}
].
sub_groups(Cases) -> groups() ->
Matrix = lists:usort(lists:append([?MODULE:Case(matrix) || Case <- Cases])), emqx_common_test_helpers:matrix_to_groups(?MODULE, matrix_cases()).
emqx_common_test_helpers:groups(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() ->
"test-topic-one-partition". "test-topic-one-partition".
@ -133,26 +127,6 @@ end_per_suite(_Config) ->
_ = application:stop(emqx_connector), _ = application:stop(emqx_connector),
ok. 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) -> init_per_testcase(TestCase, Config) ->
case proplists:get_value(debug_case, Config) of case proplists:get_value(debug_case, Config) of
TestCase -> TestCase ->
@ -204,13 +178,13 @@ t_query_mode_async(CtConfig) ->
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
t_publish(matrix) -> t_publish(matrix) ->
[ {publish, [
[tcp, none, key_dispatch, sync], [tcp, none, key_dispatch, sync],
[ssl, scram_sha512, random, async], [ssl, scram_sha512, random, async],
[ssl, kerberos, random, sync] [ssl, kerberos, random, sync]
]; ]};
t_publish(Config) -> t_publish(Config) ->
Path = proplists:get_value(group_path, Config), Path = group_path(Config),
ct:comment(Path), ct:comment(Path),
[Transport, Auth, Partitioner, QueryMode] = Path, [Transport, Auth, Partitioner, QueryMode] = Path,
Hosts = kafka_hosts_string(Transport, Auth), Hosts = kafka_hosts_string(Transport, Auth),
@ -241,14 +215,14 @@ t_publish(Config) ->
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
t_rest_api(matrix) -> t_rest_api(matrix) ->
[ {rest_api, [
[tcp, none], [tcp, none],
[tcp, plain], [tcp, plain],
[ssl, scram_sha256], [ssl, scram_sha256],
[ssl, kerberos] [ssl, kerberos]
]; ]};
t_rest_api(Config) -> t_rest_api(Config) ->
Path = proplists:get_value(group_path, Config), Path = group_path(Config),
ct:comment(Path), ct:comment(Path),
[Transport, Auth] = Path, [Transport, Auth] = Path,
Hosts = kafka_hosts_string(Transport, Auth), Hosts = kafka_hosts_string(Transport, Auth),
@ -597,9 +571,9 @@ t_nonexistent_topic(_Config) ->
ok. ok.
t_send_message_with_headers(matrix) -> t_send_message_with_headers(matrix) ->
[[sync], [async]]; {query_mode, [[sync], [async]]};
t_send_message_with_headers(Config) -> t_send_message_with_headers(Config) ->
[Mode] = proplists:get_value(group_path, Config), [Mode] = group_path(Config),
ct:comment(Mode), ct:comment(Mode),
HostsString = kafka_hosts_string_sasl(), HostsString = kafka_hosts_string_sasl(),
AuthSettings = valid_sasl_plain_settings(), AuthSettings = valid_sasl_plain_settings(),
@ -837,7 +811,7 @@ t_wrong_headers(_Config) ->
ok. ok.
t_wrong_headers_from_message(matrix) -> t_wrong_headers_from_message(matrix) ->
[[sync], [async]]; {query_mode, [[sync], [async]]};
t_wrong_headers_from_message(Config) -> t_wrong_headers_from_message(Config) ->
HostsString = kafka_hosts_string(), HostsString = kafka_hosts_string(),
AuthSettings = "none", AuthSettings = "none",
@ -931,7 +905,7 @@ do_send(Ref, Config, ResourceId, Msg, State, BridgeV2Id) when is_list(Config) ->
Caller ! {ack, Ref}, Caller ! {ack, Ref},
ok ok
end, end,
case proplists:get_value(group_path, Config) of case group_path(Config) of
[async] -> [async] ->
{ok, _} = ?PRODUCER:on_query_async(ResourceId, {BridgeV2Id, Msg}, {F, []}, State), {ok, _} = ?PRODUCER:on_query_async(ResourceId, {BridgeV2Id, Msg}, {F, []}, State),
ok; ok;
@ -1286,3 +1260,13 @@ bin_map(Map) ->
{erlang:iolist_to_binary(K), erlang:iolist_to_binary(V)} {erlang:iolist_to_binary(K), erlang:iolist_to_binary(V)}
|| {K, V} <- maps:to_list(Map) || {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.