test(emqx): switch most of testsuites to use `emqx_cth_suite`

For increased robustness and isolation.
This commit is contained in:
Andrew Mayorov 2023-12-06 14:07:26 +01:00
parent d643330cb9
commit f06a1f10ef
No known key found for this signature in database
GPG Key ID: 2837C62ACFBFED5D
17 changed files with 100 additions and 108 deletions

View File

@ -26,12 +26,11 @@
all() -> emqx_common_test_helpers:all(?MODULE). all() -> emqx_common_test_helpers:all(?MODULE).
init_per_suite(Config) -> init_per_suite(Config) ->
emqx_common_test_helpers:boot_modules(all), Apps = emqx_cth_suite:start([emqx], #{work_dir => emqx_cth_suite:work_dir(Config)}),
emqx_common_test_helpers:start_apps([]), [{apps, Apps} | Config].
Config.
end_per_suite(_Config) -> end_per_suite(Config) ->
emqx_common_test_helpers:stop_apps([]). emqx_cth_suite:stop(?config(apps, Config)).
t_emqx_pubsub_api(_) -> t_emqx_pubsub_api(_) ->
true = emqx:is_running(node()), true = emqx:is_running(node()),

View File

@ -26,12 +26,14 @@
all() -> emqx_common_test_helpers:all(?MODULE). all() -> emqx_common_test_helpers:all(?MODULE).
init_per_suite(Config) -> init_per_suite(Config) ->
emqx_common_test_helpers:boot_modules([broker]), Apps = emqx_cth_suite:start(
emqx_common_test_helpers:start_apps([]), [{emqx, #{override_env => [{boot_modules, [broker]}]}}],
Config. #{work_dir => emqx_cth_suite:work_dir(Config)}
),
[{apps, Apps} | Config].
end_per_suite(_Config) -> end_per_suite(Config) ->
emqx_common_test_helpers:stop_apps([]). emqx_cth_suite:stop(proplists:get_value(apps, Config)).
init_per_testcase(_, Config) -> init_per_testcase(_, Config) ->
Config. Config.

View File

@ -19,29 +19,25 @@
-compile(export_all). -compile(export_all).
-compile(nowarn_export_all). -compile(nowarn_export_all).
-include_lib("emqx/include/emqx.hrl").
-include_lib("emqx/include/emqx_mqtt.hrl").
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
all() -> emqx_common_test_helpers:all(?MODULE). all() -> emqx_common_test_helpers:all(?MODULE).
init_per_testcase(t_size_limit, Config) -> init_per_testcase(t_size_limit = TC, Config) ->
emqx_common_test_helpers:boot_modules(all), Apps = emqx_cth_suite:start(
emqx_common_test_helpers:start_apps([]), [{emqx, "alarm.size_limit = 2"}],
{ok, _} = emqx:update_config([alarm], #{ #{work_dir => emqx_cth_suite:work_dir(TC, Config)}
<<"size_limit">> => 2 ),
}), [{apps, Apps} | Config];
Config; init_per_testcase(TC, Config) ->
init_per_testcase(_, Config) -> Apps = emqx_cth_suite:start(
emqx_common_test_helpers:boot_modules(all), [{emqx, "alarm.validity_period = \"1s\""}],
emqx_common_test_helpers:start_apps([]), #{work_dir => emqx_cth_suite:work_dir(TC, Config)}
{ok, _} = emqx:update_config([alarm], #{ ),
<<"validity_period">> => <<"1s">> [{apps, Apps} | Config].
}),
Config.
end_per_testcase(_, _Config) -> end_per_testcase(_, Config) ->
emqx_common_test_helpers:stop_apps([]). emqx_cth_suite:stop(proplists:get_value(apps, Config)).
t_alarm(_) -> t_alarm(_) ->
ok = emqx_alarm:activate(unknown_alarm), ok = emqx_alarm:activate(unknown_alarm),

View File

@ -24,12 +24,11 @@
all() -> emqx_common_test_helpers:all(?MODULE). all() -> emqx_common_test_helpers:all(?MODULE).
init_per_suite(Config) -> init_per_suite(Config) ->
emqx_common_test_helpers:boot_modules(all), Apps = emqx_cth_suite:start([emqx], #{work_dir => emqx_cth_suite:work_dir(Config)}),
emqx_common_test_helpers:start_apps([]), [{apps, Apps} | Config].
Config.
end_per_suite(_Config) -> end_per_suite(Config) ->
emqx_common_test_helpers:stop_apps([]). emqx_cth_suite:stop(proplists:get_value(apps, Config)).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Test cases %% Test cases

View File

@ -26,15 +26,11 @@
all() -> emqx_common_test_helpers:all(?MODULE). all() -> emqx_common_test_helpers:all(?MODULE).
init_per_suite(Config) -> init_per_suite(Config) ->
emqx_common_test_helpers:start_apps([]), Apps = emqx_cth_suite:start([emqx], #{work_dir => emqx_cth_suite:work_dir(Config)}),
ok = ekka:start(), [{apps, Apps} | Config].
Config.
end_per_suite(_Config) -> end_per_suite(Config) ->
ekka:stop(), emqx_cth_suite:stop(proplists:get_value(apps, Config)).
mria:stop(),
mria_mnesia:delete_schema(),
emqx_common_test_helpers:stop_apps([]).
t_add_delete(_) -> t_add_delete(_) ->
Banned = #banned{ Banned = #banned{

View File

@ -23,6 +23,13 @@
all() -> emqx_common_test_helpers:all(?MODULE). all() -> emqx_common_test_helpers:all(?MODULE).
init_per_suite(Config) ->
ok = application:load(emqx),
Config.
end_per_suite(_) ->
ok = application:unload(emqx).
t_is_enabled(_) -> t_is_enabled(_) ->
try try
ok = application:set_env(emqx, boot_modules, all), ok = application:set_env(emqx, boot_modules, all),

View File

@ -26,16 +26,13 @@
all() -> emqx_common_test_helpers:all(?MODULE). all() -> emqx_common_test_helpers:all(?MODULE).
init_per_suite(Config) -> init_per_suite(Config) ->
emqx_common_test_helpers:start_apps([emqx]), Apps = emqx_cth_suite:start([emqx], #{work_dir => emqx_cth_suite:work_dir(Config)}),
[mnesia:dirty_write(Rec) || Rec <- fake_records()], [mnesia:dirty_write(Rec) || Rec <- fake_records()],
Config. [{apps, Apps} | Config].
end_per_suite(_Config) -> end_per_suite(Config) ->
meck:unload(), meck:unload(),
[mnesia:dirty_delete({?TAB, Key}) || #?TAB{key = Key} <- fake_records()], emqx_cth_suite:stop(?config(apps, Config)).
emqx_bpapi:announce(emqx),
emqx_common_test_helpers:stop_apps([emqx]),
ok.
t_max_supported_version(_Config) -> t_max_supported_version(_Config) ->
?assertMatch(3, emqx_bpapi:supported_version('fake-node2@localhost', api2)), ?assertMatch(3, emqx_bpapi:supported_version('fake-node2@localhost', api2)),

View File

@ -31,12 +31,11 @@ all() ->
emqx_common_test_helpers:all(?MODULE). emqx_common_test_helpers:all(?MODULE).
init_per_suite(Config) -> init_per_suite(Config) ->
emqx_common_test_helpers:boot_modules(all), Apps = emqx_cth_suite:start([emqx], #{work_dir => emqx_cth_suite:work_dir(Config)}),
emqx_common_test_helpers:start_apps([]), [{apps, Apps} | Config].
Config.
end_per_suite(_Config) -> end_per_suite(Config) ->
emqx_common_test_helpers:stop_apps([]). emqx_cth_suite:stop(?config(apps, Config)).
init_per_testcase(Case, Config) -> init_per_testcase(Case, Config) ->
?MODULE:Case({init, Config}). ?MODULE:Case({init, Config}).

View File

@ -83,14 +83,14 @@ groups() ->
]. ].
init_per_suite(Config) -> init_per_suite(Config) ->
emqx_common_test_helpers:boot_modules(all), Apps = emqx_cth_suite:start(
emqx_common_test_helpers:start_apps([]), [{emqx, "listeners.ssl.default.ssl_options.verify = verify_peer"}],
emqx_config:put_listener_conf(ssl, default, [ssl_options, verify], verify_peer), #{work_dir => emqx_cth_suite:work_dir(Config)}
emqx_listeners:restart_listener('ssl:default'), ),
Config. [{apps, Apps} | Config].
end_per_suite(_Config) -> end_per_suite(Config) ->
emqx_common_test_helpers:stop_apps([]). emqx_cth_suite:stop(?config(apps, Config)).
init_per_testcase(_Case, Config) -> init_per_testcase(_Case, Config) ->
Config. Config.

View File

@ -19,7 +19,6 @@
-compile(export_all). -compile(export_all).
-compile(nowarn_export_all). -compile(nowarn_export_all).
-include_lib("emqx/include/emqx.hrl").
-include_lib("emqx/include/emqx_cm.hrl"). -include_lib("emqx/include/emqx_cm.hrl").
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
-include_lib("snabbkaffe/include/snabbkaffe.hrl"). -include_lib("snabbkaffe/include/snabbkaffe.hrl").
@ -54,12 +53,11 @@ suite() -> [{timetrap, {minutes, 2}}].
all() -> emqx_common_test_helpers:all(?MODULE). all() -> emqx_common_test_helpers:all(?MODULE).
init_per_suite(Config) -> init_per_suite(Config) ->
emqx_common_test_helpers:boot_modules(all), Apps = emqx_cth_suite:start([emqx], #{work_dir => emqx_cth_suite:work_dir(Config)}),
emqx_common_test_helpers:start_apps([]), [{apps, Apps} | Config].
Config.
end_per_suite(_Config) -> end_per_suite(Config) ->
emqx_common_test_helpers:stop_apps([]). emqx_cth_suite:stop(proplists:get_value(apps, Config)).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% TODO: Add more test cases %% TODO: Add more test cases

View File

@ -24,12 +24,11 @@
all() -> emqx_common_test_helpers:all(?MODULE). all() -> emqx_common_test_helpers:all(?MODULE).
init_per_suite(Config) -> init_per_suite(Config) ->
emqx_common_test_helpers:boot_modules(all), Apps = emqx_cth_suite:start([emqx], #{work_dir => emqx_cth_suite:work_dir(Config)}),
emqx_common_test_helpers:start_apps([]), [{apps, Apps} | Config].
Config.
end_per_suite(_Config) -> end_per_suite(Config) ->
emqx_common_test_helpers:stop_apps([]). emqx_cth_suite:stop(proplists:get_value(apps, Config)).
t_start_link(_) -> t_start_link(_) ->
emqx_cm_locker:start_link(). emqx_cm_locker:start_link().

View File

@ -28,12 +28,11 @@
all() -> emqx_common_test_helpers:all(?MODULE). all() -> emqx_common_test_helpers:all(?MODULE).
init_per_suite(Config) -> init_per_suite(Config) ->
emqx_common_test_helpers:boot_modules(all), Apps = emqx_cth_suite:start([emqx], #{work_dir => emqx_cth_suite:work_dir(Config)}),
emqx_common_test_helpers:start_apps([]), [{apps, Apps} | Config].
Config.
end_per_suite(_Config) -> end_per_suite(Config) ->
emqx_common_test_helpers:stop_apps([]). emqx_cth_suite:stop(proplists:get_value(apps, Config)).
init_per_testcase(_TestCase, Config) -> init_per_testcase(_TestCase, Config) ->
Config. Config.

View File

@ -25,12 +25,21 @@
all() -> emqx_common_test_helpers:all(?MODULE). all() -> emqx_common_test_helpers:all(?MODULE).
init_per_suite(Config) -> init_per_suite(Config) ->
emqx_common_test_helpers:boot_modules(all), WorkDir = emqx_cth_suite:work_dir(Config),
emqx_common_test_helpers:start_apps([]), Apps = emqx_cth_suite:start(
Config. [
{emqx, #{
override_env => [
{cluster_override_conf_file, filename:join(WorkDir, "cluster_override.conf")}
]
}}
],
#{work_dir => WorkDir}
),
[{apps, Apps} | Config].
end_per_suite(_Config) -> end_per_suite(Config) ->
emqx_common_test_helpers:stop_apps([]). emqx_cth_suite:stop(?config(apps, Config)).
init_per_testcase(TestCase, Config) -> init_per_testcase(TestCase, Config) ->
try try

View File

@ -30,12 +30,11 @@ all() ->
emqx_common_test_helpers:all(?MODULE). emqx_common_test_helpers:all(?MODULE).
init_per_suite(Config) -> init_per_suite(Config) ->
emqx_common_test_helpers:boot_modules(all), Apps = emqx_cth_suite:start([emqx], #{work_dir => emqx_cth_suite:work_dir(Config)}),
emqx_common_test_helpers:start_apps([]), [{apps, Apps} | Config].
Config.
end_per_suite(_Config) -> end_per_suite(Config) ->
emqx_common_test_helpers:stop_apps([]). emqx_cth_suite:stop(?config(apps, Config)).
init_per_testcase(_Case, Config) -> init_per_testcase(_Case, Config) ->
_ = file:delete(?CLUSTER_CONF), _ = file:delete(?CLUSTER_CONF),

View File

@ -57,10 +57,10 @@ init_per_suite(Config) ->
ok = meck:expect(emqx_alarm, deactivate, fun(_) -> ok end), ok = meck:expect(emqx_alarm, deactivate, fun(_) -> ok end),
ok = meck:expect(emqx_alarm, deactivate, fun(_, _) -> ok end), ok = meck:expect(emqx_alarm, deactivate, fun(_, _) -> ok end),
emqx_common_test_helpers:start_apps([]), Apps = emqx_cth_suite:start([emqx], #{work_dir => emqx_cth_suite:work_dir(Config)}),
Config. [{apps, Apps} | Config].
end_per_suite(_Config) -> end_per_suite(Config) ->
ok = meck:unload(emqx_transport), ok = meck:unload(emqx_transport),
catch meck:unload(emqx_channel), catch meck:unload(emqx_channel),
ok = meck:unload(emqx_cm), ok = meck:unload(emqx_cm),
@ -68,8 +68,8 @@ end_per_suite(_Config) ->
ok = meck:unload(emqx_metrics), ok = meck:unload(emqx_metrics),
ok = meck:unload(emqx_hooks), ok = meck:unload(emqx_hooks),
ok = meck:unload(emqx_alarm), ok = meck:unload(emqx_alarm),
emqx_common_test_helpers:stop_apps([]),
ok. emqx_cth_suite:stop(proplists:get_value(apps, Config)).
init_per_testcase(TestCase, Config) when init_per_testcase(TestCase, Config) when
TestCase =/= t_ws_pingreq_before_connected TestCase =/= t_ws_pingreq_before_connected

View File

@ -34,20 +34,14 @@
all() -> emqx_common_test_helpers:all(?MODULE). all() -> emqx_common_test_helpers:all(?MODULE).
init_per_suite(Config) -> init_per_suite(Config) ->
emqx_common_test_helpers:boot_modules(all), Apps = emqx_cth_suite:start(
emqx_common_test_helpers:start_apps([]), [{emqx, "mqtt.exclusive_subscription = true"}],
ok = ekka:start(), #{work_dir => emqx_cth_suite:work_dir(Config)}
OldConf = emqx:get_config([zones], #{}), ),
emqx_config:put_zone_conf(default, [mqtt, exclusive_subscription], true), [{apps, Apps} | Config].
timer:sleep(50),
[{old_conf, OldConf} | Config].
end_per_suite(Config) -> end_per_suite(Config) ->
emqx_config:put([zones], proplists:get_value(old_conf, Config)), emqx_cth_suite:stop(proplists:get_value(apps, Config)).
ekka:stop(),
mria:stop(),
mria_mnesia:delete_schema(),
emqx_common_test_helpers:stop_apps([]).
end_per_testcase(_TestCase, _Config) -> end_per_testcase(_TestCase, _Config) ->
emqx_exclusive_subscription:clear(). emqx_exclusive_subscription:clear().

View File

@ -30,12 +30,11 @@
all() -> emqx_common_test_helpers:all(?MODULE). all() -> emqx_common_test_helpers:all(?MODULE).
init_per_suite(Config) -> init_per_suite(Config) ->
emqx_common_test_helpers:boot_modules(all), Apps = emqx_cth_suite:start([emqx], #{work_dir => emqx_cth_suite:work_dir(Config)}),
emqx_common_test_helpers:start_apps([]), [{apps, Apps} | Config].
Config.
end_per_suite(_Config) -> end_per_suite(Config) ->
emqx_common_test_helpers:stop_apps([]). emqx_cth_suite:stop(proplists:get_value(apps, Config)).
init_per_testcase(_TestCase, Config) -> init_per_testcase(_TestCase, Config) ->
Init = emqx:get_raw_config(?LISTENERS), Init = emqx:get_raw_config(?LISTENERS),